com.microsoft.azure.storage.SendingRequestEvent Java Examples

The following examples show how to use com.microsoft.azure.storage.SendingRequestEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudFileTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException {
    final String fileName = FileTestHelper.generateRandomFileName();
    final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = FileTestHelper.getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            connection.setRequestProperty("x-ms-foo", "");
        }
    });

    fileRef.upload(srcStream, length, null, null, context);
    fileRef.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #2
Source File: CloudPageBlobTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBlobEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException {
    final String pageBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testPageBlob");
    final CloudPageBlob pageBlobRef = this.container.getPageBlobReference(pageBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            connection.setRequestProperty("x-ms-foo", "");
        }
    });

    pageBlobRef.upload(srcStream, length, null, null, context);
    pageBlobRef.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #3
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 6 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testBlobEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException {
    final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            connection.setRequestProperty("x-ms-foo", "");
        }
    });

    blockBlobRef.upload(srcStream, -1, null, null, context);
    blockBlobRef.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #4
Source File: CloudBlobContainerTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testCloudBlobContainerInvalidMetadata() throws StorageException{
    // test client-side fails correctly
    testMetadataFailures(this.container, null, "value1", true);
    testMetadataFailures(this.container, "", "value1", true);
    testMetadataFailures(this.container, " ", "value1", true);
    testMetadataFailures(this.container, "\n \t", "value1", true);

    testMetadataFailures(this.container, "key1", null, false);
    testMetadataFailures(this.container, "key1", "", false);
    testMetadataFailures(this.container, "key1", " ", false);
    testMetadataFailures(this.container, "key1", "\n \t", false);

    // test client can get empty metadata
    this.container.create();

    OperationContext opContext = new OperationContext();
    opContext.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {
        // insert a metadata element with an empty value
        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection request = (HttpURLConnection) eventArg.getConnectionObject();
            request.setRequestProperty(Constants.HeaderConstants.PREFIX_FOR_STORAGE_METADATA + "key1", "");
        }
    });
    this.container.uploadMetadata(null, null, opContext);

    this.container.downloadAttributes();
    assertEquals(1, this.container.getMetadata().size());
    assertEquals("", this.container.getMetadata().get("key1"));
}
 
Example #5
Source File: TableTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloudTableDeleteIfExistsErrorCode() throws StorageException, URISyntaxException {
    final CloudTable table = TableTestHelper.getRandomTableReference();

    try {
        assertFalse(table.deleteIfExists());
        OperationContext ctx = new OperationContext();
        ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

            @Override
            public void eventOccurred(SendingRequestEvent eventArg) {
                if (((HttpURLConnection) eventArg.getConnectionObject()).getRequestMethod().equals("DELETE")) {
                    try {
                        table.delete();
                        assertFalse(table.exists());
                    } catch (StorageException e) {
                        fail("Delete should succeed.");
                    }
                }
            }
        });

        table.create();

        // The second delete of a table will return a 404
        assertFalse(table.deleteIfExists(null, ctx));
    } finally {
        table.deleteIfExists();
    }
}
 
Example #6
Source File: FileSasTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileSASWithSharedAccessFileHeaders() throws InvalidKeyException, IllegalArgumentException,
        StorageException, URISyntaxException, InterruptedException {
    SharedAccessFilePolicy policy = createSharedAccessPolicy(EnumSet.of(SharedAccessFilePermissions.READ,
            SharedAccessFilePermissions.WRITE, SharedAccessFilePermissions.LIST), 300);
    FileSharePermissions perms = new FileSharePermissions();

    perms.getSharedAccessPolicies().put("rwperm", policy);
    this.share.uploadPermissions(perms);
    Thread.sleep(30000);

    SharedAccessFileHeaders headers = new SharedAccessFileHeaders();
    headers.setCacheControl("no-cache");
    headers.setContentDisposition("attachment; filename=\"fname.ext\"");
    headers.setContentEncoding("gzip");
    headers.setContentLanguage("da");
    headers.setContentType("text/html; charset=utf-8");

    CloudFile sasFile = new CloudFile(
            new URI(this.file.getUri().toString() + "?" + this.file.generateSharedAccessSignature(null, headers, "rwperm")));
    OperationContext context = new OperationContext();

    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {
        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            assertEquals("no-cache", connection.getHeaderField(Constants.HeaderConstants.CACHE_CONTROL));
            assertEquals("attachment; filename=\"fname.ext\"",
                    connection.getHeaderField(Constants.HeaderConstants.CONTENT_DISPOSITION));
            assertEquals("gzip", connection.getHeaderField(Constants.HeaderConstants.CONTENT_ENCODING));
            assertEquals("da", connection.getHeaderField(Constants.HeaderConstants.CONTENT_LANGUAGE));
            assertEquals("text/html; charset=utf-8",
                    connection.getHeaderField(Constants.HeaderConstants.CONTENT_TYPE));
        }
    });

    sasFile.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #7
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testBlobMultiConditionHeaders() throws URISyntaxException, StorageException, IOException {
    final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    final CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);
    OperationContext context = new OperationContext();
    blockBlobRef.upload(srcStream, -1, null, null, context);

    AccessCondition condition = AccessCondition.generateIfMatchCondition(context.getLastResult().getEtag());
    condition.setIfUnmodifiedSinceDate(context.getLastResult().getStartDate());

    StorageEvent<SendingRequestEvent> event = new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            assertNotNull(connection.getRequestProperty("If-Unmodified-Since"));
            assertNotNull(connection.getRequestProperty("If-Match"));
        }
    };

    context.getSendingRequestEventHandler().addListener(event);

    blockBlobRef.upload(srcStream, -1, condition, null, context);
}
 
Example #8
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * @throws StorageException
 * @throws URISyntaxException
 * @throws IOException
 * @throws InterruptedException
 */
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testSendingRequestEventBlob() throws StorageException, URISyntaxException, IOException {
    final int length = 128;

    final ArrayList<Boolean> callList = new ArrayList<Boolean>();
    OperationContext sendingRequestEventContext = new OperationContext();
    sendingRequestEventContext.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            assertEquals(eventArg.getRequestResult(), eventArg.getOpContext().getLastResult());
            callList.add(true);
        }
    });

    assertEquals(0, callList.size());

    //Put blob
    CloudBlob blob = BlobTestHelper.uploadNewBlob(this.container, BlobType.BLOCK_BLOB, "bb", length,
            sendingRequestEventContext);

    assertEquals(1, callList.size());

    //Get blob
    blob.download(new ByteArrayOutputStream(), null, null, sendingRequestEventContext);
    assertEquals(2, callList.size());

    //uploadMetadata
    blob.uploadMetadata(null, null, sendingRequestEventContext);
    assertEquals(3, callList.size());

    //uploadMetadata
    blob.downloadAttributes(null, null, sendingRequestEventContext);
    assertEquals(4, callList.size());

}
 
Example #9
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testDeleteBlobIfExists() throws URISyntaxException, StorageException, IOException {
    final CloudBlockBlob blob1 = this.container.getBlockBlobReference(BlobTestHelper
            .generateRandomBlobNameWithPrefix("testBlob"));

    assertFalse(blob1.exists());
    assertFalse(blob1.deleteIfExists());

    blob1.uploadText("test1");
    assertTrue(blob1.exists());

    assertTrue(blob1.deleteIfExists());
    assertFalse(blob1.deleteIfExists());

    // check if second condition works in delete if exists
    OperationContext ctx = new OperationContext();
    ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            if (((HttpURLConnection) eventArg.getConnectionObject()).getRequestMethod().equals("DELETE")) {
                try {
                    blob1.delete();
                    assertFalse(blob1.exists());
                }
                catch (StorageException e) {
                    fail("Delete should succeed.");
                }
            }
        }
    });

    // The second delete of a blob will return a 404
    blob1.uploadText("test1");
    assertFalse(blob1.deleteIfExists(DeleteSnapshotsOption.NONE, null, null, ctx));
}
 
Example #10
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyWithChineseChars() throws StorageException, IOException, URISyntaxException {
    String data = "sample data chinese chars 阿䶵";
    CloudBlockBlob copySource = container.getBlockBlobReference("sourcechinescharsblob阿䶵.txt");
    copySource.uploadText(data);

    assertEquals(this.container.getUri() + "/sourcechinescharsblob阿䶵.txt", copySource.getUri().toString());
    assertEquals(this.container.getUri() + "/sourcechinescharsblob%E9%98%BF%E4%B6%B5.txt",
            copySource.getUri().toASCIIString());

    CloudBlockBlob copyDestination = container.getBlockBlobReference("destchinesecharsblob阿䶵.txt");

    assertEquals(this.container.getUri() + "/destchinesecharsblob阿䶵.txt", copyDestination.getUri().toString());
    assertEquals(this.container.getUri() + "/destchinesecharsblob%E9%98%BF%E4%B6%B5.txt",
            copyDestination.getUri().toASCIIString());

    OperationContext ctx = new OperationContext();
    ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {
        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection con = (HttpURLConnection) eventArg.getConnectionObject();

            // Test the copy destination request url
            assertEquals(CloudBlockBlobTests.this.container.getUri() + "/destchinesecharsblob%E9%98%BF%E4%B6%B5.txt",
                    con.getURL().toString());

            // Test the copy source request property
            assertEquals(CloudBlockBlobTests.this.container.getUri() + "/sourcechinescharsblob%E9%98%BF%E4%B6%B5.txt",
                    con.getRequestProperty("x-ms-copy-source"));
        }
    });

    copyDestination.startCopy(copySource.getUri(), null, null, null, ctx);
    copyDestination.startCopy(copySource, null, null, null, ctx);
}
 
Example #11
Source File: CloudAppendBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testBlobEmptyHeaderSigningTest() throws URISyntaxException,
        StorageException, IOException {
    final String appendBlobName = BlobTestHelper
            .generateRandomBlobNameWithPrefix("testAppendBlob");
    final CloudAppendBlob appendBlobRef = this.container
            .getAppendBlobReference(appendBlobName);

    final int length = 2 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper
            .getRandomDataStream(length);

    OperationContext context = new OperationContext();
    context.getSendingRequestEventHandler().addListener(
            new StorageEvent<SendingRequestEvent>() {

                @Override
                public void eventOccurred(SendingRequestEvent eventArg) {
                    HttpURLConnection connection = (HttpURLConnection) eventArg
                            .getConnectionObject();
                    connection.setRequestProperty("x-ms-foo", "");
                }
            });

    appendBlobRef.upload(srcStream, length, null, null, context);
    appendBlobRef
            .download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #12
Source File: CloudAppendBlobTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
/**
 * Delete an append blob if it exists.
 * 
 * @throws StorageException
 * @throws URISyntaxException 
 */
@Test
public void testAppendBlobDeleteIfExists() throws URISyntaxException, StorageException {
    final CloudAppendBlob blob = this.container.getAppendBlobReference(BlobTestHelper
            .generateRandomBlobNameWithPrefix("testBlob"));

    assertFalse(blob.exists());
    assertFalse(blob.deleteIfExists());

    blob.createOrReplace();
    assertTrue(blob.exists());

    assertTrue(blob.deleteIfExists());
    assertFalse(blob.deleteIfExists());

    // check if second condition works in delete if exists
    OperationContext ctx = new OperationContext();
    ctx.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            if (((HttpURLConnection) eventArg.getConnectionObject()).getRequestMethod().equals("DELETE")) {
                try {
                    blob.delete();
                    assertFalse(blob.exists());
                }
                catch (StorageException e) {
                    fail("Delete should succeed.");
                }
            }
        }
    });

    // The second delete of a blob will return a 404
    blob.createOrReplace();
    assertFalse(blob.deleteIfExists(DeleteSnapshotsOption.NONE, null, null, ctx));
}
 
Example #13
Source File: SasTests.java    From azure-storage-android with Apache License 2.0 5 votes vote down vote up
@Test
@Category(SlowTests.class)
public void testBlobSaSWithSharedAccessBlobHeaders() throws InvalidKeyException, IllegalArgumentException,
        StorageException, URISyntaxException,  InterruptedException {
    SharedAccessBlobPolicy sp = createSharedAccessPolicy(EnumSet.of(SharedAccessBlobPermissions.READ,
            SharedAccessBlobPermissions.WRITE, SharedAccessBlobPermissions.LIST), 3600);
    BlobContainerPermissions perms = new BlobContainerPermissions();

    perms.getSharedAccessPolicies().put("readperm", sp);
    this.container.uploadPermissions(perms);
    Thread.sleep(30000);

    SharedAccessBlobHeaders headers = new SharedAccessBlobHeaders();
    headers.setCacheControl("no-cache");
    headers.setContentDisposition("attachment; filename=\"fname.ext\"");
    headers.setContentEncoding("gzip");
    headers.setContentLanguage("da");
    headers.setContentType("text/html; charset=utf-8");

    CloudBlockBlob sasBlob = new CloudBlockBlob(new URI(this.blob.getUri().toString() + "?"
            + this.blob.generateSharedAccessSignature(null, headers, "readperm")));
    OperationContext context = new OperationContext();

    context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject();
            assertEquals("no-cache", connection.getHeaderField(Constants.HeaderConstants.CACHE_CONTROL));
            assertEquals("attachment; filename=\"fname.ext\"",
                    connection.getHeaderField(Constants.HeaderConstants.CONTENT_DISPOSITION));
            assertEquals("gzip", connection.getHeaderField(Constants.HeaderConstants.CONTENT_ENCODING));
            assertEquals("da", connection.getHeaderField(Constants.HeaderConstants.CONTENT_LANGUAGE));
            assertEquals("text/html; charset=utf-8",
                    connection.getHeaderField(Constants.HeaderConstants.CONTENT_TYPE));
        }
    });

    sasBlob.download(new ByteArrayOutputStream(), null, null, context);
}
 
Example #14
Source File: TestAzureFileSystemErrorConditions.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void eventOccurred(SendingRequestEvent eventArg) {
  HttpURLConnection connection = (HttpURLConnection)eventArg.getConnectionObject();
  if (!connectionRecognizer.isTargetConnection(connection)) {
    return;
  }
  if (!injectedErrorOnce) {
    connection.setReadTimeout(1);
    connection.disconnect();
    injectedErrorOnce = true;
  }
}
 
Example #15
Source File: TestAzureFileSystemErrorConditions.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void eventOccurred(SendingRequestEvent eventArg) {
  HttpURLConnection connection = (HttpURLConnection)eventArg.getConnectionObject();
  if (!connectionRecognizer.isTargetConnection(connection)) {
    return;
  }
  if (!injectedErrorOnce) {
    connection.setReadTimeout(1);
    connection.disconnect();
    injectedErrorOnce = true;
  }
}
 
Example #16
Source File: SelfThrottlingIntercept.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public void sendingRequest(SendingRequestEvent sendEvent) {
  long lastLatency;
  boolean operationIsRead; // for logging
  synchronized (this) {

    lastLatency = this.lastE2Elatency;
  }

  float sleepMultiple;
  HttpURLConnection urlConnection = (HttpURLConnection) sendEvent
      .getConnectionObject();

  // Azure REST API never uses POST, so PUT is a sufficient test for an
  // upload.
  if (urlConnection.getRequestMethod().equalsIgnoreCase("PUT")) {
    operationIsRead = false;
    sleepMultiple = (1 / writeFactor) - 1;
  } else {
    operationIsRead = true;
    sleepMultiple = (1 / readFactor) - 1;
  }

  long sleepDuration = (long) (sleepMultiple * lastLatency);
  if (sleepDuration < 0) {
    sleepDuration = 0;
  }

  if (sleepDuration > 0) {
    try {
      // Thread.sleep() is not exact but it seems sufficiently accurate for
      // our needs. If needed this could become a loop of small waits that
      // tracks actual
      // elapsed time.
      Thread.sleep(sleepDuration);
    } catch (InterruptedException ie) {
      Thread.currentThread().interrupt();
    }

    // reset to avoid counting the sleep against request latency
    sendEvent.getRequestResult().setStartDate(new Date());
  }

  if (LOG.isDebugEnabled()) {
    boolean isFirstRequest = (lastLatency == 0);
    long threadId = Thread.currentThread().getId();
    LOG.debug(String
        .format(
            " SelfThrottlingIntercept:: SendingRequest:   threadId=%d, requestType=%s, isFirstRequest=%b, sleepDuration=%d",
            threadId, operationIsRead ? "read " : "write", isFirstRequest,
            sleepDuration));
  }
}
 
Example #17
Source File: CloudBlockBlobTests.java    From azure-storage-android with Apache License 2.0 4 votes vote down vote up
@Test
@Category({ DevFabricTests.class, DevStoreTests.class })
public void testBlockBlobUploadContentMD5() throws URISyntaxException, StorageException, IOException {
    final String blockBlobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testBlockBlob");
    CloudBlockBlob blockBlobRef = this.container.getBlockBlobReference(blockBlobName);

    int length = 16 * 1024;
    ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length);

    final ArrayList<Boolean> callList = new ArrayList<Boolean>();
    OperationContext sendingRequestEventContext = new OperationContext();
    StorageEvent<SendingRequestEvent> event = new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            assertNotNull(((HttpURLConnection) eventArg.getConnectionObject())
                    .getRequestProperty(BlobConstants.BLOB_CONTENT_MD5_HEADER));
            callList.add(true);
        }
    };

    sendingRequestEventContext.getSendingRequestEventHandler().addListener(event);
    assertEquals(0, callList.size());

    // Upload with length less than single threshold. Make sure we calculate the contentMD5.
    blockBlobRef.upload(srcStream, length, null, null, sendingRequestEventContext);
    assertEquals(1, callList.size());

    sendingRequestEventContext.getSendingRequestEventHandler().removeListener(event);
    callList.clear();

    event = new StorageEvent<SendingRequestEvent>() {

        @Override
        public void eventOccurred(SendingRequestEvent eventArg) {
            callList.add(true);
        }
    };

    length = 33 * 1024 * 1024;
    srcStream = BlobTestHelper.getRandomDataStream(length);

    sendingRequestEventContext.getSendingRequestEventHandler().addListener(event);
    assertEquals(0, callList.size());

    // Upload with length greater than single threshold. This will do multiple block uploads.
    blockBlobRef.upload(srcStream, length, null, null, sendingRequestEventContext);
    assertTrue(callList.size() > 1);
}
 
Example #18
Source File: SelfThrottlingIntercept.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public void eventOccurred(SendingRequestEvent event) {
  sendingRequest(event);
}
 
Example #19
Source File: SelfThrottlingIntercept.java    From big-c with Apache License 2.0 4 votes vote down vote up
public void sendingRequest(SendingRequestEvent sendEvent) {
  long lastLatency;
  boolean operationIsRead; // for logging
  synchronized (this) {

    lastLatency = this.lastE2Elatency;
  }

  float sleepMultiple;
  HttpURLConnection urlConnection = (HttpURLConnection) sendEvent
      .getConnectionObject();

  // Azure REST API never uses POST, so PUT is a sufficient test for an
  // upload.
  if (urlConnection.getRequestMethod().equalsIgnoreCase("PUT")) {
    operationIsRead = false;
    sleepMultiple = (1 / writeFactor) - 1;
  } else {
    operationIsRead = true;
    sleepMultiple = (1 / readFactor) - 1;
  }

  long sleepDuration = (long) (sleepMultiple * lastLatency);
  if (sleepDuration < 0) {
    sleepDuration = 0;
  }

  if (sleepDuration > 0) {
    try {
      // Thread.sleep() is not exact but it seems sufficiently accurate for
      // our needs. If needed this could become a loop of small waits that
      // tracks actual
      // elapsed time.
      Thread.sleep(sleepDuration);
    } catch (InterruptedException ie) {
      Thread.currentThread().interrupt();
    }

    // reset to avoid counting the sleep against request latency
    sendEvent.getRequestResult().setStartDate(new Date());
  }

  if (LOG.isDebugEnabled()) {
    boolean isFirstRequest = (lastLatency == 0);
    long threadId = Thread.currentThread().getId();
    LOG.debug(String
        .format(
            " SelfThrottlingIntercept:: SendingRequest:   threadId=%d, requestType=%s, isFirstRequest=%b, sleepDuration=%d",
            threadId, operationIsRead ? "read " : "write", isFirstRequest,
            sleepDuration));
  }
}
 
Example #20
Source File: SelfThrottlingIntercept.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public void eventOccurred(SendingRequestEvent event) {
  sendingRequest(event);
}