Skip to content

Commit 4f02df0

Browse files
Added Dedicated Gateway MaxIntegratedCacheStaleness to CosmosItemRequestOptions and CosmosQueryRequestOptions (Azure#20971)
* Added Dedicated Gateway Request Options * Reused code blocks and added tests for negative values of maxIntegratedCacheStaleness
1 parent fcef4ce commit 4f02df0

File tree

10 files changed

+398
-10
lines changed

10 files changed

+398
-10
lines changed

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/HttpConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ public static class HttpHeaders {
261261

262262
// Backend request duration header
263263
public static final String BACKEND_REQUEST_DURATION_MILLISECONDS = "x-ms-request-duration-ms";
264+
265+
// Dedicated Gateway Headers
266+
public static final String DEDICATED_GATEWAY_PER_REQUEST_CACHE_STALENESS = "x-ms-dedicatedgateway-max-age";
264267
}
265268

266269
public static class A_IMHeaderValues {

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RequestOptions.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.azure.cosmos.implementation;
55

66
import com.azure.cosmos.ConsistencyLevel;
7+
import com.azure.cosmos.models.DedicatedGatewayRequestOptions;
78
import com.azure.cosmos.models.IndexingDirective;
89
import com.azure.cosmos.models.PartitionKey;
910
import com.azure.cosmos.models.ThroughputProperties;
@@ -36,6 +37,7 @@ public class RequestOptions {
3637
private Boolean contentResponseOnWriteEnabled;
3738
private String filterPredicate;
3839
private String throughputControlGroupName;
40+
private DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions;
3941

4042
/**
4143
* Gets the triggers to be invoked before the operation.
@@ -407,4 +409,12 @@ public String getThroughputControlGroupName() {
407409
public void setThroughputControlGroupName(String throughputControlGroupName) {
408410
this.throughputControlGroupName = throughputControlGroupName;
409411
}
412+
413+
public DedicatedGatewayRequestOptions getDedicatedGatewayRequestOptions() {
414+
return dedicatedGatewayRequestOptions;
415+
}
416+
417+
public void setDedicatedGatewayRequestOptions(DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions) {
418+
this.dedicatedGatewayRequestOptions = dedicatedGatewayRequestOptions;
419+
}
410420
}

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import java.net.URI;
7777
import java.net.URLEncoder;
7878
import java.nio.ByteBuffer;
79+
import java.time.Duration;
7980
import java.time.Instant;
8081
import java.util.ArrayList;
8182
import java.util.Collections;
@@ -1215,6 +1216,12 @@ private Map<String, String> getRequestHeaders(RequestOptions options, ResourceTy
12151216
headers.put(HttpConstants.HttpHeaders.SCRIPT_ENABLE_LOGGING, String.valueOf(true));
12161217
}
12171218

1219+
if (options.getDedicatedGatewayRequestOptions() != null &&
1220+
options.getDedicatedGatewayRequestOptions().getMaxIntegratedCacheStaleness() != null) {
1221+
headers.put(HttpConstants.HttpHeaders.DEDICATED_GATEWAY_PER_REQUEST_CACHE_STALENESS,
1222+
String.valueOf(Utils.getMaxIntegratedCacheStalenessInMillis(options.getDedicatedGatewayRequestOptions())));
1223+
}
1224+
12181225
return headers;
12191226
}
12201227

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Utils.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,17 @@
44

55
import com.azure.cosmos.ConsistencyLevel;
66
import com.azure.cosmos.implementation.apachecommons.lang.StringUtils;
7-
import com.azure.cosmos.implementation.changefeed.implementation.ChangeFeedStartFromInternal;
8-
import com.azure.cosmos.implementation.changefeed.implementation.ChangeFeedStartFromInternalDeserializer;
9-
import com.azure.cosmos.implementation.changefeed.implementation.ChangeFeedState;
10-
import com.azure.cosmos.implementation.changefeed.implementation.ChangeFeedStateDeserializer;
11-
import com.azure.cosmos.implementation.feedranges.FeedRangeContinuation;
12-
import com.azure.cosmos.implementation.feedranges.FeedRangeContinuationDeserializer;
13-
import com.azure.cosmos.implementation.feedranges.FeedRangeInternal;
14-
import com.azure.cosmos.implementation.feedranges.FeedRangeInternalDeserializer;
157
import com.azure.cosmos.implementation.uuid.EthernetAddress;
168
import com.azure.cosmos.implementation.uuid.Generators;
179
import com.azure.cosmos.implementation.uuid.impl.TimeBasedGenerator;
1810
import com.azure.cosmos.models.CosmosChangeFeedRequestOptions;
1911
import com.azure.cosmos.models.CosmosQueryRequestOptions;
12+
import com.azure.cosmos.models.DedicatedGatewayRequestOptions;
2013
import com.azure.cosmos.models.ModelBridgeInternal;
2114
import com.fasterxml.jackson.core.JsonParser;
2215
import com.fasterxml.jackson.core.JsonProcessingException;
2316
import com.fasterxml.jackson.databind.DeserializationFeature;
2417
import com.fasterxml.jackson.databind.ObjectMapper;
25-
import com.fasterxml.jackson.databind.module.SimpleModule;
2618
import com.fasterxml.jackson.databind.node.ObjectNode;
2719
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
2820
import io.netty.buffer.ByteBuf;
@@ -39,6 +31,7 @@
3931
import java.net.URLEncoder;
4032
import java.nio.ByteBuffer;
4133
import java.nio.charset.StandardCharsets;
34+
import java.time.Duration;
4235
import java.time.Instant;
4336
import java.time.OffsetDateTime;
4437
import java.time.ZoneId;
@@ -741,4 +734,15 @@ public static byte[] serializeObjectToByteArray(Object obj) throws IOException {
741734
os.writeObject(obj);
742735
return out.toByteArray();
743736
}
737+
738+
public static long getMaxIntegratedCacheStalenessInMillis(DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions) {
739+
Duration maxIntegratedCacheStaleness = dedicatedGatewayRequestOptions.getMaxIntegratedCacheStaleness();
740+
if (maxIntegratedCacheStaleness.toNanos() > 0 && maxIntegratedCacheStaleness.toMillis() <= 0) {
741+
throw new IllegalArgumentException("MaxIntegratedCacheStaleness granularity is milliseconds");
742+
}
743+
if (maxIntegratedCacheStaleness.toMillis() < 0) {
744+
throw new IllegalArgumentException("MaxIntegratedCacheStaleness duration cannot be negative");
745+
}
746+
return maxIntegratedCacheStaleness.toMillis();
747+
}
744748
}

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/DocumentQueryExecutionContextBase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import reactor.core.publisher.Flux;
3030
import reactor.core.publisher.Mono;
3131

32+
import java.time.Duration;
3233
import java.util.HashMap;
3334
import java.util.List;
3435
import java.util.Map;
@@ -218,6 +219,12 @@ public Map<String, String> createCommonHeadersAsync(CosmosQueryRequestOptions co
218219
requestHeaders.put(HttpConstants.HttpHeaders.POPULATE_QUERY_METRICS, String.valueOf(cosmosQueryRequestOptions.isQueryMetricsEnabled()));
219220
}
220221

222+
if (cosmosQueryRequestOptions.getDedicatedGatewayRequestOptions() != null &&
223+
cosmosQueryRequestOptions.getDedicatedGatewayRequestOptions().getMaxIntegratedCacheStaleness() != null) {
224+
requestHeaders.put(HttpConstants.HttpHeaders.DEDICATED_GATEWAY_PER_REQUEST_CACHE_STALENESS,
225+
String.valueOf(Utils.getMaxIntegratedCacheStalenessInMillis(cosmosQueryRequestOptions.getDedicatedGatewayRequestOptions())));
226+
}
227+
221228
return requestHeaders;
222229
}
223230

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosItemRequestOptions.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class CosmosItemRequestOptions {
2323
private String ifNoneMatchETag;
2424
private Boolean contentResponseOnWriteEnabled;
2525
private String throughputControlGroupName;
26+
private DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions;
2627

2728
/**
2829
* copy constructor
@@ -38,6 +39,7 @@ public class CosmosItemRequestOptions {
3839
ifNoneMatchETag = options.ifNoneMatchETag;
3940
contentResponseOnWriteEnabled = options.contentResponseOnWriteEnabled;
4041
throughputControlGroupName = options.throughputControlGroupName;
42+
dedicatedGatewayRequestOptions = options.dedicatedGatewayRequestOptions;
4143
}
4244

4345

@@ -251,6 +253,26 @@ public CosmosItemRequestOptions setContentResponseOnWriteEnabled(Boolean content
251253
return this;
252254
}
253255

256+
/**
257+
* Gets the Dedicated Gateway Request Options
258+
* @return the Dedicated Gateway Request Options
259+
*/
260+
@Beta(value = Beta.SinceVersion.V4_15_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING)
261+
public DedicatedGatewayRequestOptions getDedicatedGatewayRequestOptions() {
262+
return this.dedicatedGatewayRequestOptions;
263+
}
264+
265+
/**
266+
* Sets the Dedicated Gateway Request Options
267+
* @param dedicatedGatewayRequestOptions Dedicated Gateway Request Options
268+
* @return the CosmosItemRequestOptions
269+
*/
270+
@Beta(value = Beta.SinceVersion.V4_15_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING)
271+
public CosmosItemRequestOptions setDedicatedGatewayRequestOptions(DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions) {
272+
this.dedicatedGatewayRequestOptions = dedicatedGatewayRequestOptions;
273+
return this;
274+
}
275+
254276
/**
255277
* Gets the partition key
256278
*
@@ -284,6 +306,7 @@ RequestOptions toRequestOptions() {
284306
requestOptions.setPartitionKey(partitionKey);
285307
requestOptions.setContentResponseOnWriteEnabled(contentResponseOnWriteEnabled);
286308
requestOptions.setThroughputControlGroupName(throughputControlGroupName);
309+
requestOptions.setDedicatedGatewayRequestOptions(dedicatedGatewayRequestOptions);
287310
return requestOptions;
288311
}
289312

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosQueryRequestOptions.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class CosmosQueryRequestOptions {
3535
private FeedRange feedRange;
3636
private OperationContextAndListenerTuple operationContextAndListenerTuple;
3737
private String throughputControlGroupName;
38+
private DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions;
3839

3940
/**
4041
* Instantiates a new query request options.
@@ -64,6 +65,7 @@ public CosmosQueryRequestOptions() {
6465
this.emptyPagesAllowed = options.emptyPagesAllowed;
6566
this.throughputControlGroupName = options.throughputControlGroupName;
6667
this.operationContextAndListenerTuple = options.operationContextAndListenerTuple;
68+
this.dedicatedGatewayRequestOptions = options.dedicatedGatewayRequestOptions;
6769
}
6870

6971
void setOperationContextAndListenerTuple(OperationContextAndListenerTuple operationContextAndListenerTuple) {
@@ -433,6 +435,26 @@ public CosmosQueryRequestOptions setThroughputControlGroupName(String throughput
433435
return this;
434436
}
435437

438+
/**
439+
* Gets the Dedicated Gateway Request Options
440+
* @return the Dedicated Gateway Request Options
441+
*/
442+
@Beta(value = Beta.SinceVersion.V4_15_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING)
443+
public DedicatedGatewayRequestOptions getDedicatedGatewayRequestOptions() {
444+
return this.dedicatedGatewayRequestOptions;
445+
}
446+
447+
/**
448+
* Sets the Dedicated Gateway Request Options
449+
* @param dedicatedGatewayRequestOptions Dedicated Gateway Request Options
450+
* @return the CosmosQueryRequestOptions
451+
*/
452+
@Beta(value = Beta.SinceVersion.V4_15_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING)
453+
public CosmosQueryRequestOptions setDedicatedGatewayRequestOptions(DedicatedGatewayRequestOptions dedicatedGatewayRequestOptions) {
454+
this.dedicatedGatewayRequestOptions = dedicatedGatewayRequestOptions;
455+
return this;
456+
}
457+
436458
///////////////////////////////////////////////////////////////////////////////////////////
437459
// the following helper/accessor only helps to access this class outside of this package.//
438460
///////////////////////////////////////////////////////////////////////////////////////////
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure.cosmos.models;
4+
5+
import com.azure.cosmos.util.Beta;
6+
7+
import java.time.Duration;
8+
9+
/**
10+
* Dedicated Gateway Request Options
11+
*/
12+
@Beta(value = Beta.SinceVersion.V4_15_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING)
13+
public final class DedicatedGatewayRequestOptions {
14+
15+
private Duration maxIntegratedCacheStaleness;
16+
17+
@Beta(value = Beta.SinceVersion.V4_15_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING)
18+
public DedicatedGatewayRequestOptions() {
19+
20+
}
21+
22+
/**
23+
* Gets the staleness value associated with the request in the Azure CosmosDB service. For requests where the {@link
24+
* com.azure.cosmos.ConsistencyLevel} is {@link com.azure.cosmos.ConsistencyLevel#EVENTUAL}, responses from the
25+
* integrated cache are guaranteed to be no staler than value indicated by this maxIntegratedCacheStaleness.
26+
*
27+
* <p>Default value is null</p>
28+
*
29+
* <p>Cache Staleness is supported in milliseconds granularity. Anything smaller than milliseconds will be ignored.</p>
30+
*
31+
* @return Duration of maxIntegratedCacheStaleness
32+
*/
33+
@Beta(value = Beta.SinceVersion.V4_15_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING)
34+
public Duration getMaxIntegratedCacheStaleness() {
35+
return maxIntegratedCacheStaleness;
36+
}
37+
38+
/**
39+
* Sets the staleness value associated with the request in the Azure CosmosDB service. For requests where the {@link
40+
* com.azure.cosmos.ConsistencyLevel} is {@link com.azure.cosmos.ConsistencyLevel#EVENTUAL}, responses from the
41+
* integrated cache are guaranteed to be no staler than value indicated by this maxIntegratedCacheStaleness.
42+
*
43+
* <p>Default value is null</p>
44+
*
45+
* <p>Cache Staleness is supported in milliseconds granularity. Anything smaller than milliseconds will be ignored.</p>
46+
*
47+
* @param maxIntegratedCacheStaleness Max Integrated Cache Staleness duration
48+
* @return this DedicatedGatewayRequestOptions
49+
*/
50+
@Beta(value = Beta.SinceVersion.V4_15_0, warningText = Beta.PREVIEW_SUBJECT_TO_CHANGE_WARNING)
51+
public DedicatedGatewayRequestOptions setMaxIntegratedCacheStaleness(Duration maxIntegratedCacheStaleness) {
52+
this.maxIntegratedCacheStaleness = maxIntegratedCacheStaleness;
53+
return this;
54+
}
55+
}

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/util/Beta.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public enum SinceVersion {
6060
/** v4.13.0 */
6161
V4_13_0,
6262
/** v4.14.0 */
63-
V4_14_0
63+
V4_14_0,
64+
/** v4.15.0 */
65+
V4_15_0;
6466
}
6567
}

0 commit comments

Comments
 (0)