Skip to content

Commit

Permalink
Synching changes from TFS
Browse files Browse the repository at this point in the history
GatewayProxy - Share PoolingClientConnectionManager.

Bug Fix: Java SDK QueryIterable sets this.hasStarted after query, to
prevent it from getting set without knowing a exception happens.
  • Loading branch information
aliuy committed Dec 10, 2014
1 parent 6e01007 commit a3aea6f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static void execute(BackoffRetryUtilityDelegate delegate, ResourceThrottl
} catch (Exception e) {
boolean retry = retryPolicy.shouldRetry(e);
if (!retry) {
e.printStackTrace();
throw new IllegalStateException("Exception not retriable", e);
}

Expand Down
14 changes: 8 additions & 6 deletions src/com/microsoft/azure/documentdb/GatewayProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ final class GatewayProxy {
private ConnectionPolicy connectionPolicy;
private HttpClient httpClient;
private HttpClient mediaHttpClient;
private PoolingClientConnectionManager connectionManager;

public GatewayProxy(URI serviceEndpoint,
ConnectionPolicy connectionPolicy,
Expand All @@ -67,6 +68,12 @@ public GatewayProxy(URI serviceEndpoint,
this.connectionPolicy = connectionPolicy;
this.masterKey = masterKey;
this.resourceTokens = resourceTokens;

// Initialize connection manager.
this.connectionManager = new PoolingClientConnectionManager(SchemeRegistryFactory.createDefault());
this.connectionManager.setMaxTotal(this.connectionPolicy.getMaxPoolSize());
this.connectionManager.setDefaultMaxPerRoute(this.connectionPolicy.getMaxPoolSize());
this.connectionManager.closeIdleConnections(this.connectionPolicy.getIdleConnectionTimeout(), TimeUnit.SECONDS);
}

public DocumentServiceResponse doCreate(DocumentServiceRequest request)
Expand Down Expand Up @@ -126,12 +133,7 @@ private HttpClient getHttpClient(boolean isForMedia) {
* @return the created HttpClient
*/
private HttpClient createHttpClient(boolean isForMedia) {
PoolingClientConnectionManager conMan = new PoolingClientConnectionManager(
SchemeRegistryFactory.createDefault());
conMan.setMaxTotal(this.connectionPolicy.getMaxPoolSize());
conMan.setDefaultMaxPerRoute(this.connectionPolicy.getMaxPoolSize());
conMan.closeIdleConnections(this.connectionPolicy.getIdleConnectionTimeout(), TimeUnit.SECONDS);
HttpClient httpClient = new DefaultHttpClient(conMan);
HttpClient httpClient = new DefaultHttpClient(this.connectionManager);
HttpParams httpParams = httpClient.getParams();

if (isForMedia) {
Expand Down
11 changes: 7 additions & 4 deletions src/com/microsoft/azure/documentdb/QueryIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ private int fetchNextBlock()

while (!this.isNullEmptyOrFalse(this.continuation) ||
!this.hasStarted) {
if (!this.hasStarted) {
this.hasStarted = true;
}

if (!this.isNullEmptyOrFalse(this.continuation)) {
request.getHeaders().put(HttpConstants.HttpHeaders.CONTINUATION,
this.continuation);
Expand All @@ -146,6 +142,13 @@ private int fetchNextBlock()
response = this.client.doQuery(request);
}

// A retriable exception may happen. "this.hasStarted" and "this.continuation" must not be set
// value before this line.

if (!this.hasStarted) {
this.hasStarted = true;
}

this.responseHeaders = response.getResponseHeaders();
this.continuation = this.responseHeaders.get(HttpConstants.HttpHeaders.CONTINUATION);

Expand Down
79 changes: 53 additions & 26 deletions src/com/microsoft/azure/documentdb/test/GatewayTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void tearDown() throws DocumentClientException {
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();

String line;
try {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
Expand Down Expand Up @@ -258,6 +258,33 @@ public void testCollectionCrud() throws DocumentClientException {
}
}

@Test
public void testQueryIterableCrud() throws DocumentClientException {
DocumentClient client = new DocumentClient(HOST,
MASTER_KEY,
ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);

List<Document> documents = client.readDocuments(this.collectionForTest.getSelfLink(),
null).getQueryIterable().toList();
int beforeCreateDocumentsCount = documents.size();

// Create 20 collection.
for (int i = 0; i < 20; ++i) {
Document documentDefinition = new Document("{ 'name': 'For test' }");
client.createDocument(this.collectionForTest.getSelfLink(), documentDefinition, null, false);
}

FeedOptions fo = new FeedOptions();
// Page size of "1" is for test only. Please choose a more reasonable value in practice.
fo.setPageSize(1);

documents = client.readDocuments(this.collectionForTest.getSelfLink(),
null).getQueryIterable().toList();

Assert.assertEquals(beforeCreateDocumentsCount + 20, documents.size());
}

@Test
public void testCollectionIndexingPolicy() throws DocumentClientException {
DocumentClient client = new DocumentClient(HOST,
Expand Down Expand Up @@ -324,7 +351,7 @@ public void testCollectionIndexingPolicy() throws DocumentClientException {
collectionWithSecondaryIndex.getIndexingPolicy().getIncludedPaths().iterator().next().getIndexType());
Assert.assertEquals(1, collectionWithSecondaryIndex.getIndexingPolicy().getExcludedPaths().size());
}

@Test
public void testDocumentCrud() throws DocumentClientException {
DocumentClient client = new DocumentClient(HOST,
Expand Down Expand Up @@ -384,7 +411,7 @@ public void testDocumentCrud() throws DocumentClientException {
// Read document.
Document oneDocumentFromRead = client.readDocument(replacedDocument.getSelfLink(), null).getResource();
Assert.assertEquals(replacedDocument.getId(), oneDocumentFromRead.getId());

AccessCondition accessCondition = new AccessCondition();
accessCondition.setCondition(oneDocumentFromRead.getETag()) ;
accessCondition.setType(AccessConditionType.IfNoneMatch);
Expand All @@ -409,31 +436,31 @@ public void testDocumentCrud() throws DocumentClientException {

class TestPOJOInner {
public int intProperty;
public TestPOJOInner(int i) {

public TestPOJOInner(int i) {
this.intProperty = i;
}
}

class TestPOJO {
private String privateStringProperty;

private String privateStringProperty;

public String id;
public int intProperty;
public String stringProperty;
public TestPOJOInner objectProperty;
public List<String> stringList;
public String[] stringArray;

public TestPOJO(int i) {
this.intProperty = i;
public TestPOJO(int i) {
this.intProperty = i;

this.stringList = new ArrayList<String>();
this.stringList.add("ONE");
this.stringList.add("TWO");
this.stringList.add("THREE");

this.stringArray = new String[] { "One", "Two", "Three" };
}

Expand All @@ -444,27 +471,27 @@ public void setPrivateStringProperty(String value) {
this.privateStringProperty = value;
}
}

@Test
public void testPOJODocumentCrud() throws DocumentClientException {


DocumentClient client = new DocumentClient(HOST,
MASTER_KEY,
ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);

TestPOJO testPojo = new TestPOJO(10);
testPojo.id= "MyPojoObejct" + GatewayTests.getUID();
testPojo.stringProperty = "testString";
testPojo.objectProperty = new TestPOJOInner(100);
testPojo.setPrivateStringProperty("testStringAccess");

Document document = client.createDocument(this.collectionForTest.getSelfLink(),
testPojo,
null,
false).getResource();

Assert.assertEquals(document.getInt("intProperty").intValue(), testPojo.intProperty);

Assert.assertEquals(document.getString("stringProperty"), testPojo.stringProperty);
Expand All @@ -487,7 +514,7 @@ public void testPOJODocumentCrud() throws DocumentClientException {
testPojo.stringProperty = "updatedTestString";
document = client.replaceDocument(document.getSelfLink(), testPojo, null).getResource();
Assert.assertEquals(document.getString("stringProperty"), testPojo.stringProperty);

}

@Test
Expand Down Expand Up @@ -807,7 +834,7 @@ public void testStoredProcedureFunctionality()
// POJO
class TempPOJO {
@SuppressWarnings("unused")
public String temp = "so2";
public String temp = "so2";
}
TempPOJO tempPOJO = new TempPOJO();
StoredProcedure sproc4 = new StoredProcedure(
Expand Down Expand Up @@ -969,7 +996,7 @@ public void testPermissionCrud() throws DocumentClientException {
null).getQueryIterable().toList();
Assert.assertEquals(1, permissions.size());

// Replace permission.
// Replace permission.
permission.setId("replaced permission");
Permission replacedPermission = client.replacePermission(
permission, null).getResource();
Expand Down Expand Up @@ -997,12 +1024,12 @@ public void testDatabaseAccount() throws DocumentClientException {
MASTER_KEY,
ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);

DatabaseAccount dba = client.getDatabaseAccount();
Assert.assertNotNull("dba Address link works", dba.getAddressesLink());
Assert.assertNotNull("dba Address link works", dba.getAddressesLink());
Assert.assertTrue("provision storage must larger than 10000MB",
dba.getProvisionedDocumentStorageInMB() > 10000);

if (dba.getConsistencyPolicy().getDefaultConsistencyLevel() == ConsistencyLevel.BoundedStaleness) {
Assert.assertTrue("StaleInternal should be larger than 5 seconds",
dba.getConsistencyPolicy().getMaxStalenessIntervalInSeconds() >= 5);
Expand Down Expand Up @@ -1044,7 +1071,7 @@ public void testAuthorization() throws DocumentClientException {
" 'permissionMode': 'Read'," +
" 'resource': '%s'" +
"}", collectionForTest.getSelfLink()));
// Create permission for collectionForTest
// Create permission for collectionForTest
Permission permission1 = client.createPermission(user1.getSelfLink(),
permission1Definition,
null).getResource();
Expand Down

0 comments on commit a3aea6f

Please sign in to comment.