Skip to content

Commit a3aea6f

Browse files
committed
Synching changes from TFS
GatewayProxy - Share PoolingClientConnectionManager. Bug Fix: Java SDK QueryIterable sets this.hasStarted after query, to prevent it from getting set without knowing a exception happens.
1 parent 6e01007 commit a3aea6f

File tree

4 files changed

+69
-36
lines changed

4 files changed

+69
-36
lines changed

src/com/microsoft/azure/documentdb/BackoffRetryUtility.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static void execute(BackoffRetryUtilityDelegate delegate, ResourceThrottl
1616
} catch (Exception e) {
1717
boolean retry = retryPolicy.shouldRetry(e);
1818
if (!retry) {
19+
e.printStackTrace();
1920
throw new IllegalStateException("Exception not retriable", e);
2021
}
2122

src/com/microsoft/azure/documentdb/GatewayProxy.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ final class GatewayProxy {
4444
private ConnectionPolicy connectionPolicy;
4545
private HttpClient httpClient;
4646
private HttpClient mediaHttpClient;
47+
private PoolingClientConnectionManager connectionManager;
4748

4849
public GatewayProxy(URI serviceEndpoint,
4950
ConnectionPolicy connectionPolicy,
@@ -67,6 +68,12 @@ public GatewayProxy(URI serviceEndpoint,
6768
this.connectionPolicy = connectionPolicy;
6869
this.masterKey = masterKey;
6970
this.resourceTokens = resourceTokens;
71+
72+
// Initialize connection manager.
73+
this.connectionManager = new PoolingClientConnectionManager(SchemeRegistryFactory.createDefault());
74+
this.connectionManager.setMaxTotal(this.connectionPolicy.getMaxPoolSize());
75+
this.connectionManager.setDefaultMaxPerRoute(this.connectionPolicy.getMaxPoolSize());
76+
this.connectionManager.closeIdleConnections(this.connectionPolicy.getIdleConnectionTimeout(), TimeUnit.SECONDS);
7077
}
7178

7279
public DocumentServiceResponse doCreate(DocumentServiceRequest request)
@@ -126,12 +133,7 @@ private HttpClient getHttpClient(boolean isForMedia) {
126133
* @return the created HttpClient
127134
*/
128135
private HttpClient createHttpClient(boolean isForMedia) {
129-
PoolingClientConnectionManager conMan = new PoolingClientConnectionManager(
130-
SchemeRegistryFactory.createDefault());
131-
conMan.setMaxTotal(this.connectionPolicy.getMaxPoolSize());
132-
conMan.setDefaultMaxPerRoute(this.connectionPolicy.getMaxPoolSize());
133-
conMan.closeIdleConnections(this.connectionPolicy.getIdleConnectionTimeout(), TimeUnit.SECONDS);
134-
HttpClient httpClient = new DefaultHttpClient(conMan);
136+
HttpClient httpClient = new DefaultHttpClient(this.connectionManager);
135137
HttpParams httpParams = httpClient.getParams();
136138

137139
if (isForMedia) {

src/com/microsoft/azure/documentdb/QueryIterable.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ private int fetchNextBlock()
131131

132132
while (!this.isNullEmptyOrFalse(this.continuation) ||
133133
!this.hasStarted) {
134-
if (!this.hasStarted) {
135-
this.hasStarted = true;
136-
}
137-
138134
if (!this.isNullEmptyOrFalse(this.continuation)) {
139135
request.getHeaders().put(HttpConstants.HttpHeaders.CONTINUATION,
140136
this.continuation);
@@ -146,6 +142,13 @@ private int fetchNextBlock()
146142
response = this.client.doQuery(request);
147143
}
148144

145+
// A retriable exception may happen. "this.hasStarted" and "this.continuation" must not be set
146+
// value before this line.
147+
148+
if (!this.hasStarted) {
149+
this.hasStarted = true;
150+
}
151+
149152
this.responseHeaders = response.getResponseHeaders();
150153
this.continuation = this.responseHeaders.get(HttpConstants.HttpHeaders.CONTINUATION);
151154

src/com/microsoft/azure/documentdb/test/GatewayTests.java

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void tearDown() throws DocumentClientException {
8181
private static String getStringFromInputStream(InputStream is) {
8282
BufferedReader br = null;
8383
StringBuilder sb = new StringBuilder();
84-
84+
8585
String line;
8686
try {
8787
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
@@ -258,6 +258,33 @@ public void testCollectionCrud() throws DocumentClientException {
258258
}
259259
}
260260

261+
@Test
262+
public void testQueryIterableCrud() throws DocumentClientException {
263+
DocumentClient client = new DocumentClient(HOST,
264+
MASTER_KEY,
265+
ConnectionPolicy.GetDefault(),
266+
ConsistencyLevel.Session);
267+
268+
List<Document> documents = client.readDocuments(this.collectionForTest.getSelfLink(),
269+
null).getQueryIterable().toList();
270+
int beforeCreateDocumentsCount = documents.size();
271+
272+
// Create 20 collection.
273+
for (int i = 0; i < 20; ++i) {
274+
Document documentDefinition = new Document("{ 'name': 'For test' }");
275+
client.createDocument(this.collectionForTest.getSelfLink(), documentDefinition, null, false);
276+
}
277+
278+
FeedOptions fo = new FeedOptions();
279+
// Page size of "1" is for test only. Please choose a more reasonable value in practice.
280+
fo.setPageSize(1);
281+
282+
documents = client.readDocuments(this.collectionForTest.getSelfLink(),
283+
null).getQueryIterable().toList();
284+
285+
Assert.assertEquals(beforeCreateDocumentsCount + 20, documents.size());
286+
}
287+
261288
@Test
262289
public void testCollectionIndexingPolicy() throws DocumentClientException {
263290
DocumentClient client = new DocumentClient(HOST,
@@ -324,7 +351,7 @@ public void testCollectionIndexingPolicy() throws DocumentClientException {
324351
collectionWithSecondaryIndex.getIndexingPolicy().getIncludedPaths().iterator().next().getIndexType());
325352
Assert.assertEquals(1, collectionWithSecondaryIndex.getIndexingPolicy().getExcludedPaths().size());
326353
}
327-
354+
328355
@Test
329356
public void testDocumentCrud() throws DocumentClientException {
330357
DocumentClient client = new DocumentClient(HOST,
@@ -384,7 +411,7 @@ public void testDocumentCrud() throws DocumentClientException {
384411
// Read document.
385412
Document oneDocumentFromRead = client.readDocument(replacedDocument.getSelfLink(), null).getResource();
386413
Assert.assertEquals(replacedDocument.getId(), oneDocumentFromRead.getId());
387-
414+
388415
AccessCondition accessCondition = new AccessCondition();
389416
accessCondition.setCondition(oneDocumentFromRead.getETag()) ;
390417
accessCondition.setType(AccessConditionType.IfNoneMatch);
@@ -409,31 +436,31 @@ public void testDocumentCrud() throws DocumentClientException {
409436

410437
class TestPOJOInner {
411438
public int intProperty;
412-
413-
public TestPOJOInner(int i) {
439+
440+
public TestPOJOInner(int i) {
414441
this.intProperty = i;
415442
}
416443
}
417-
444+
418445
class TestPOJO {
419-
420-
private String privateStringProperty;
421-
446+
447+
private String privateStringProperty;
448+
422449
public String id;
423450
public int intProperty;
424451
public String stringProperty;
425452
public TestPOJOInner objectProperty;
426453
public List<String> stringList;
427454
public String[] stringArray;
428455

429-
public TestPOJO(int i) {
430-
this.intProperty = i;
431-
456+
public TestPOJO(int i) {
457+
this.intProperty = i;
458+
432459
this.stringList = new ArrayList<String>();
433460
this.stringList.add("ONE");
434461
this.stringList.add("TWO");
435462
this.stringList.add("THREE");
436-
463+
437464
this.stringArray = new String[] { "One", "Two", "Three" };
438465
}
439466

@@ -444,27 +471,27 @@ public void setPrivateStringProperty(String value) {
444471
this.privateStringProperty = value;
445472
}
446473
}
447-
474+
448475
@Test
449476
public void testPOJODocumentCrud() throws DocumentClientException {
450-
451-
477+
478+
452479
DocumentClient client = new DocumentClient(HOST,
453480
MASTER_KEY,
454481
ConnectionPolicy.GetDefault(),
455482
ConsistencyLevel.Session);
456-
483+
457484
TestPOJO testPojo = new TestPOJO(10);
458485
testPojo.id= "MyPojoObejct" + GatewayTests.getUID();
459486
testPojo.stringProperty = "testString";
460487
testPojo.objectProperty = new TestPOJOInner(100);
461488
testPojo.setPrivateStringProperty("testStringAccess");
462-
489+
463490
Document document = client.createDocument(this.collectionForTest.getSelfLink(),
464491
testPojo,
465492
null,
466493
false).getResource();
467-
494+
468495
Assert.assertEquals(document.getInt("intProperty").intValue(), testPojo.intProperty);
469496

470497
Assert.assertEquals(document.getString("stringProperty"), testPojo.stringProperty);
@@ -487,7 +514,7 @@ public void testPOJODocumentCrud() throws DocumentClientException {
487514
testPojo.stringProperty = "updatedTestString";
488515
document = client.replaceDocument(document.getSelfLink(), testPojo, null).getResource();
489516
Assert.assertEquals(document.getString("stringProperty"), testPojo.stringProperty);
490-
517+
491518
}
492519

493520
@Test
@@ -807,7 +834,7 @@ public void testStoredProcedureFunctionality()
807834
// POJO
808835
class TempPOJO {
809836
@SuppressWarnings("unused")
810-
public String temp = "so2";
837+
public String temp = "so2";
811838
}
812839
TempPOJO tempPOJO = new TempPOJO();
813840
StoredProcedure sproc4 = new StoredProcedure(
@@ -969,7 +996,7 @@ public void testPermissionCrud() throws DocumentClientException {
969996
null).getQueryIterable().toList();
970997
Assert.assertEquals(1, permissions.size());
971998

972-
// Replace permission.
999+
// Replace permission.
9731000
permission.setId("replaced permission");
9741001
Permission replacedPermission = client.replacePermission(
9751002
permission, null).getResource();
@@ -997,12 +1024,12 @@ public void testDatabaseAccount() throws DocumentClientException {
9971024
MASTER_KEY,
9981025
ConnectionPolicy.GetDefault(),
9991026
ConsistencyLevel.Session);
1000-
1027+
10011028
DatabaseAccount dba = client.getDatabaseAccount();
1002-
Assert.assertNotNull("dba Address link works", dba.getAddressesLink());
1029+
Assert.assertNotNull("dba Address link works", dba.getAddressesLink());
10031030
Assert.assertTrue("provision storage must larger than 10000MB",
10041031
dba.getProvisionedDocumentStorageInMB() > 10000);
1005-
1032+
10061033
if (dba.getConsistencyPolicy().getDefaultConsistencyLevel() == ConsistencyLevel.BoundedStaleness) {
10071034
Assert.assertTrue("StaleInternal should be larger than 5 seconds",
10081035
dba.getConsistencyPolicy().getMaxStalenessIntervalInSeconds() >= 5);
@@ -1044,7 +1071,7 @@ public void testAuthorization() throws DocumentClientException {
10441071
" 'permissionMode': 'Read'," +
10451072
" 'resource': '%s'" +
10461073
"}", collectionForTest.getSelfLink()));
1047-
// Create permission for collectionForTest
1074+
// Create permission for collectionForTest
10481075
Permission permission1 = client.createPermission(user1.getSelfLink(),
10491076
permission1Definition,
10501077
null).getResource();

0 commit comments

Comments
 (0)