mukund-thakur commented on a change in pull request #1899: HADOOP-16914 Adding 
Output Stream Counters in ABFS
URL: https://github.com/apache/hadoop/pull/1899#discussion_r404589011
 
 

 ##########
 File path: 
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAbfsOutputStream.java
 ##########
 @@ -0,0 +1,294 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.fs.azurebfs;
+
+import java.io.IOException;
+import java.util.Random;
+
+import org.junit.Test;
+
+import org.apache.hadoop.fs.Path;
+import 
org.apache.hadoop.fs.azurebfs.contracts.exceptions.AzureBlobFileSystemException;
+import org.apache.hadoop.fs.azurebfs.services.AbfsOutputStream;
+import org.apache.hadoop.fs.azurebfs.services.AbfsOutputStreamStatisticsImpl;
+import org.apache.hadoop.fs.permission.FsPermission;
+
+/**
+ * Test AbfsOutputStream statistics.
+ */
+public class ITestAbfsOutputStream extends AbstractAbfsIntegrationTest {
+  public ITestAbfsOutputStream() throws Exception {
+  }
+
+  private static final int LARGE_OPERATIONS = 10;
+  private static final int LOW_RANGE_FOR_RANDOM_VALUE = 49;
+  private static final int HIGH_RANGE_FOR_RANDOM_VALUE = 9999;
+
+  /**
+   * Tests to check bytes Uploading in {@link AbfsOutputStream}.
+   *
+   * @throws IOException
+   */
+  @Test
+  public void testAbfsOutputStreamUploadingBytes() throws IOException {
+    describe("Testing Bytes uploaded in AbfsOutputSteam");
+    final AzureBlobFileSystem fs = getFileSystem();
+    Path uploadBytesFilePath = new Path("AbfsOutputStreamStatsPath");
+    String testBytesToUpload = "bytes";
+
+    try (AbfsOutputStream outForSomeBytes = createAbfsOutputStream(fs,
+        uploadBytesFilePath)
+    ) {
+
+      //Test for zero bytes To upload
+      assertValues("bytes to upload", 0,
+          outForSomeBytes.getOutputStreamStatistics().getBytesToUpload());
+
+      outForSomeBytes.write(testBytesToUpload.getBytes());
+      outForSomeBytes.flush();
+      AbfsOutputStreamStatisticsImpl abfsOutputStreamStatistics =
+          outForSomeBytes.getOutputStreamStatistics();
+
+      //Test for bytes to upload
+      assertValues("bytes to upload", testBytesToUpload.getBytes().length,
+          abfsOutputStreamStatistics.getBytesToUpload());
+
+      //Test for successful bytes uploaded
+      assertValues("successful bytes uploaded",
+          testBytesToUpload.getBytes().length,
+          abfsOutputStreamStatistics.getBytesUploadSuccessful());
+
+      //Populating random value for bytesFailed
+      int randomBytesFailed = new Random().nextInt(LOW_RANGE_FOR_RANDOM_VALUE);
+      abfsOutputStreamStatistics.uploadFailed(randomBytesFailed);
+      //Test for bytes failed to upload
+      assertValues("number fo bytes failed to upload", randomBytesFailed,
+          abfsOutputStreamStatistics.getBytesUploadFailed());
+
+    }
+
+    try (AbfsOutputStream outForLargeBytes = createAbfsOutputStream(fs,
+        uploadBytesFilePath)) {
+
+      int largeValue = LARGE_OPERATIONS;
+      for (int i = 0; i < largeValue; i++) {
+        outForLargeBytes.write(testBytesToUpload.getBytes());
+      }
+      outForLargeBytes.flush();
+      AbfsOutputStreamStatisticsImpl abfsOutputStreamStatistics =
+          outForLargeBytes.getOutputStreamStatistics();
+
+      //Test for bytes to upload
+      assertValues("bytes to upload",
+          largeValue * (testBytesToUpload.getBytes().length),
+          abfsOutputStreamStatistics.getBytesToUpload());
+
+      //Test for successful bytes uploaded
+      assertValues("successful bytes uploaded",
+          largeValue * (testBytesToUpload.getBytes().length),
+          abfsOutputStreamStatistics.getBytesUploadSuccessful());
+
+      //Populating random values for bytesFailed
+      int randomBytesFailed = new 
Random().nextInt(HIGH_RANGE_FOR_RANDOM_VALUE);
+      abfsOutputStreamStatistics.uploadFailed(randomBytesFailed);
+      //Test for bytes failed to upload
+      assertValues("bytes failed to upload", randomBytesFailed,
+          abfsOutputStreamStatistics.getBytesUploadFailed());
+    }
+  }
+
+  /**
+   * Tests to check time spent on waiting for tasks to be complete on a
+   * blocking queue in {@link AbfsOutputStream}.
+   *
+   * @throws IOException
+   */
+  @Test
+  public void testAbfsOutputStreamTimeSpentOnWaitTask() throws IOException {
+    describe("Testing Time Spend on Waiting for Task to be complete");
+    final AzureBlobFileSystem fs = getFileSystem();
+    Path timeSpendFilePath = new Path("AbfsOutputStreamStatsPath");
+
+    try (AbfsOutputStream out = createAbfsOutputStream(fs, timeSpendFilePath)) 
{
+
+      AbfsOutputStreamStatisticsImpl abfsOutputStreamStatistics =
+          out.getOutputStreamStatistics();
+
+      //Test for initial value of timeSpentWaitTask
+      assertValues("Time spent on waiting tasks", 0,
+          abfsOutputStreamStatistics.getTimeSpendOnTaskWait());
+
+      int smallRandomStartTime =
+          new Random().nextInt(LOW_RANGE_FOR_RANDOM_VALUE);
+      int smallRandomEndTime =
+          new Random().nextInt(LOW_RANGE_FOR_RANDOM_VALUE)
+              + smallRandomStartTime;
+      int smallDiff = smallRandomEndTime - smallRandomStartTime;
+      abfsOutputStreamStatistics
+          .timeSpentTaskWait(smallRandomStartTime, smallRandomEndTime);
+      //Test for small random value of timeSpentWaitTask
+      assertValues("Time spent on waiting tasks", smallDiff,
+          abfsOutputStreamStatistics.getTimeSpendOnTaskWait());
+
+      int largeRandomStartTime =
+          new Random().nextInt(HIGH_RANGE_FOR_RANDOM_VALUE);
+      int largeRandomEndTime = new 
Random().nextInt(HIGH_RANGE_FOR_RANDOM_VALUE)
+          + largeRandomStartTime;
+      int randomDiff = largeRandomEndTime - largeRandomStartTime;
+      abfsOutputStreamStatistics
+          .timeSpentTaskWait(largeRandomStartTime, largeRandomEndTime);
+      /*
+      Test for large random value of timeSpentWaitTask plus the time spent
+      in previous test
+       */
+      assertValues("Time spent on waiting tasks", smallDiff + randomDiff,
+          abfsOutputStreamStatistics.getTimeSpendOnTaskWait());
+    }
+
+  }
+
+  /**
+   * Tests to check number of {@code shrinkWriteOperationQueue()}
+   * calls.
+   * After writing data, AbfsOutputStream doesn't upload the data until
+   * Flushed. Hence, flush() method is called after write() to test Queue
+   * shrink calls.
+   *
+   * @throws IOException
+   */
+  @Test
+  public void testAbfsOutputStreamQueueShrink() throws IOException {
+    describe("Testing Queue Shrink calls in AbfsOutputStream");
+    final AzureBlobFileSystem fs = getFileSystem();
+    Path queueShrinkFilePath = new Path("AbfsOutputStreamStatsPath");
+    String testQueueShrink = "testQueue";
+
+    try (AbfsOutputStream outForOneOp = createAbfsOutputStream(fs,
+        queueShrinkFilePath)) {
+
+      //Test for shrinking Queue zero time
+      assertValues("number of queueShrink() Calls", 0,
+          outForOneOp.getOutputStreamStatistics().getQueueShrink());
+
+      outForOneOp.write(testQueueShrink.getBytes());
+      // Queue is shrunk 2 times when outStream is flushed
+      outForOneOp.flush();
+
+      //Test for shrinking Queue 2 times
+      assertValues("number of queueShrink() Calls", 2,
+          outForOneOp.getOutputStreamStatistics().getQueueShrink());
+
+    }
+
+    try (AbfsOutputStream outForLargeOps = createAbfsOutputStream(fs,
+        queueShrinkFilePath)) {
+
+      int largeValue = LARGE_OPERATIONS;
+      for (int i = 0; i < largeValue; i++) {
+        outForLargeOps.write(testQueueShrink.getBytes());
+        outForLargeOps.flush();
+      }
+
+      //Test for 20 queue shrink calls
+      assertValues("number of queueShrink() Calls",
+          2 * largeValue,
+          outForLargeOps.getOutputStreamStatistics().getQueueShrink());
+    }
+
+  }
+
+  /**
+   * Test to check number of {@code writeCurrentBufferToService()}
+   * calls.
+   * After writing data, AbfsOutputStream doesn't upload data till flush() is
+   * called. Hence, flush() calls were made after write() to simulate the
+   * scenario.
+   *
+   * @throws IOException
+   */
+  @Test
+  public void testAbfsOutputStreamWriteBuffer() throws IOException {
+    describe("Testing writeCurrentBufferToService() calls");
+    final AzureBlobFileSystem fs = getFileSystem();
+    Path writeBufferFilePath = new Path("AbfsOutputStreamStatsPath");
+    String testWriteBuffer = "Buffer";
+
+    try (AbfsOutputStream outForOneOp = createAbfsOutputStream(fs,
+        writeBufferFilePath)) {
+
+      //Test for zero time writing Buffer to service
+      assertValues("number writeCurrentBufferToService() calls", 0,
+          outForOneOp.getOutputStreamStatistics()
+              .getWriteCurrentBufferOperations());
+
+      outForOneOp.write(testWriteBuffer.getBytes());
+      outForOneOp.flush();
+
+      //Test for one time writeCurrentBuffer() call
+      assertValues("number writeCurrentBufferToService() calls", 1,
+          outForOneOp.getOutputStreamStatistics()
+              .getWriteCurrentBufferOperations());
+    }
+
+    try (AbfsOutputStream outForLargeOps = createAbfsOutputStream(fs,
+        writeBufferFilePath)) {
+
+      int largeValue = LARGE_OPERATIONS;
+      for (int i = 0; i < largeValue; i++) {
+        outForLargeOps.write(testWriteBuffer.getBytes());
+        outForLargeOps.flush();
+      }
+      //Test for 10 writeBufferOperations
+      assertValues("number of writeCurrentBufferToService() calls", largeValue,
+          outForLargeOps
+              .getOutputStreamStatistics().getWriteCurrentBufferOperations());
+    }
+
+  }
+
+  /**
+   * Generic create File and setting OutputStreamFlush to false.
+   *
+   * @param fs   AzureBlobFileSystem that is initialised in the test
+   * @param path Path of the file to be created
+   * @return AbfsOutputStream for writing
+   * @throws AzureBlobFileSystemException
+   */
+  private AbfsOutputStream createAbfsOutputStream(AzureBlobFileSystem fs,
+      Path path) throws AzureBlobFileSystemException {
+    AzureBlobFileSystemStore abfss = fs.getAbfsStore();
+    abfss.getAbfsConfiguration().setDisableOutputStreamFlush(false);
+
+    return (AbfsOutputStream) abfss.createFile(path, fs.getFsStatistics(),
+        true, FsPermission.getDefault(), FsPermission.getUMask(fs.getConf()));
+  }
+
+  /**
+   * Generic assert method.
+   *
+   * @param operation     operation being asserted
+   * @param expectedValue value that is expected
+   * @param actualValue   value that is actual
+   */
 
 Review comment:
   Same method was present in earlier patch. Move to base class and reuse.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-issues-h...@hadoop.apache.org

Reply via email to