Skip to content

Commit 33eded2

Browse files
authored
Merge pull request #2 from jeet1995/thinclientStoreModel
Adding serialization skeleton in ThinClientStoreModel
2 parents 63a1816 + 2e0672d commit 33eded2

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ public Mono<RxDocumentServiceResponse> performRequestInternal(RxDocumentServiceR
275275

276276
try {
277277

278+
// todo: neharao1 - see if the below three statements can be removed since these are part of wrapInHttpRequest
278279
HttpMethod method = getHttpMethod(request);
279280
HttpHeaders httpHeaders = this.getHttpRequestHeaders(request.getHeaders());
280-
281281
Flux<byte[]> contentAsByteArray = request.getContentAsByteArrayFlux();
282282

283283
HttpRequest httpRequest = request
@@ -654,6 +654,10 @@ public void recordOpenConnectionsAndInitCachesStarted(List<CosmosContainerIdenti
654654
//no-op
655655
}
656656

657+
public Map<String, String> getDefaultHeaders() {
658+
return this.defaultHeaders;
659+
}
660+
657661
private void captureSessionToken(RxDocumentServiceRequest request, Map<String, String> responseHeaders) {
658662
if (request.getResourceType() == ResourceType.DocumentCollection &&
659663
request.getOperationType() == OperationType.Delete) {

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@
33
package com.azure.cosmos.implementation;
44

55
import com.azure.cosmos.ConsistencyLevel;
6-
import com.azure.cosmos.CosmosContainerProactiveInitConfig;
7-
import com.azure.cosmos.implementation.faultinjection.IFaultInjectorProvider;
6+
import com.azure.cosmos.implementation.directconnectivity.rntbd.RntbdRequest;
7+
import com.azure.cosmos.implementation.directconnectivity.rntbd.RntbdRequestArgs;
88
import com.azure.cosmos.implementation.http.HttpClient;
9-
import com.azure.cosmos.implementation.throughputControl.ThroughputControlStore;
10-
import com.azure.cosmos.models.CosmosContainerIdentity;
9+
import com.azure.cosmos.implementation.http.HttpHeaders;
10+
import com.azure.cosmos.implementation.http.HttpRequest;
11+
import io.netty.buffer.ByteBuf;
12+
import io.netty.buffer.Unpooled;
13+
import io.netty.handler.codec.http.HttpMethod;
1114
import reactor.core.publisher.Flux;
1215
import reactor.core.publisher.Mono;
1316

17+
import java.net.URI;
1418
import java.util.HashMap;
15-
import java.util.List;
1619
import java.util.Map;
1720

1821
import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull;
1922

2023
/**
2124
* While this class is public, but it is not part of our published public APIs.
22-
* This is meant to be internally used only by our sdk.
25+
* This is meant to be internally used only by our sdk.
2326
*
2427
* Used internally to provide functionality to communicate and process response from THINCLIENT in the Azure Cosmos DB database service.
2528
*/
@@ -76,4 +79,53 @@ protected Map<String, String> getDefaultHeaders(
7679

7780
return defaultHeaders;
7881
}
82+
83+
@Override
84+
public HttpRequest wrapInHttpRequest(RxDocumentServiceRequest request, URI requestUri) throws Exception {
85+
86+
// todo - neharao1 - validate b/w name() v/s toString()
87+
request.setThinclientHeaders(request.getOperationType().name(), request.getResourceType().name());
88+
89+
// todo - neharao1: no concept of a replica / service endpoint that can be passed
90+
RntbdRequestArgs rntbdRequestArgs = new RntbdRequestArgs(request);
91+
92+
// todo - neharao1: validate what HTTP headers are needed - for now have put default ThinClient HTTP headers
93+
// todo - based on fabianm comment - thinClient also takes op type and resource type headers as HTTP headers
94+
HttpHeaders headers = this.getHttpHeaders();
95+
96+
RntbdRequest rntbdRequest = RntbdRequest.from(rntbdRequestArgs);
97+
98+
// todo: neharao1 - validate whether Java heap buffer is okay v/s Direct buffer
99+
ByteBuf byteBuf = Unpooled.buffer();
100+
101+
// todo: comment can be removed - RntbdRequestEncoder does the same - a type of ChannelHandler in ChannelPipeline (a Netty concept)
102+
// todo: lifting the logic from there to encode the RntbdRequest instance into a ByteBuf (ByteBuf is a network compatible format)
103+
// todo: double-check with fabianm to see if RntbdRequest across RNTBD over TCP (Direct connectivity mode) is same as that when using ThinClient proxy
104+
rntbdRequest.encode(byteBuf);
105+
106+
return new HttpRequest(
107+
// todo: HttpMethod when using ThinClient is presumably always an HttpMethod.POST - validate this
108+
HttpMethod.POST,
109+
requestUri,
110+
requestUri.getPort(),
111+
headers,
112+
Flux.just(byteBuf.array()));
113+
}
114+
115+
// todo: neharao1 - validate if RxGatewayStoreModel#unwrapToStoreResponse can be reused
116+
// @Override
117+
// public StoreResponse unwrapToStoreResponse(RxDocumentServiceRequest request, int statusCode, HttpHeaders headers, ByteBuf content) {
118+
// return null;
119+
// }
120+
121+
private HttpHeaders getHttpHeaders() {
122+
HttpHeaders httpHeaders = new HttpHeaders();
123+
Map<String, String> defaultHeaders = this.getDefaultHeaders();
124+
125+
for (Map.Entry<String, String> header : defaultHeaders.entrySet()) {
126+
httpHeaders.set(header.getKey(), header.getValue());
127+
}
128+
129+
return httpHeaders;
130+
}
79131
}

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/directconnectivity/rntbd/RntbdRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static RntbdRequest decode(final ByteBuf in) {
7676
return new RntbdRequest(header, metadata, payload);
7777
}
7878

79-
void encode(final ByteBuf out) {
79+
public void encode(final ByteBuf out) {
8080

8181
final int expectedLength = RntbdRequestFrame.LENGTH + this.headers.computeLength();
8282
final int start = out.writerIndex();

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/directconnectivity/rntbd/RntbdRequestArgs.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public RntbdRequestArgs(final RxDocumentServiceRequest serviceRequest, final Uri
5656
this.transportRequestId = instanceCount.incrementAndGet();
5757
}
5858

59+
public RntbdRequestArgs(final RxDocumentServiceRequest serviceRequest) {
60+
this(serviceRequest, null);
61+
}
62+
5963
// region Accessors
6064

6165
@JsonProperty

0 commit comments

Comments
 (0)