Skip to content

Commit 647e3b2

Browse files
authored
Add OperationContext to Binding (#1097)
JAVA-4907
1 parent 814fd15 commit 647e3b2

File tree

89 files changed

+752
-601
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+752
-601
lines changed

driver-core/src/main/com/mongodb/internal/binding/AsyncClusterBinding.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.mongodb.internal.async.SingleResultCallback;
2727
import com.mongodb.internal.connection.AsyncConnection;
2828
import com.mongodb.internal.connection.Cluster;
29+
import com.mongodb.internal.connection.OperationContext;
2930
import com.mongodb.internal.connection.ReadConcernAwareNoOpSessionContext;
3031
import com.mongodb.internal.connection.Server;
3132
import com.mongodb.internal.selector.ReadPreferenceServerSelector;
@@ -51,6 +52,7 @@ public class AsyncClusterBinding extends AbstractReferenceCounted implements Asy
5152
@Nullable
5253
private final ServerApi serverApi;
5354
private final RequestContext requestContext;
55+
private final OperationContext operationContext;
5456

5557
/**
5658
* Creates an instance.
@@ -69,6 +71,7 @@ public AsyncClusterBinding(final Cluster cluster, final ReadPreference readPrefe
6971
this.readConcern = (notNull("readConcern", readConcern));
7072
this.serverApi = serverApi;
7173
this.requestContext = notNull("requestContext", requestContext);
74+
operationContext = new OperationContext();
7275
}
7376

7477
@Override
@@ -103,6 +106,11 @@ public RequestContext getRequestContext() {
103106
return requestContext;
104107
}
105108

109+
@Override
110+
public OperationContext getOperationContext() {
111+
return operationContext;
112+
}
113+
106114
@Override
107115
public void getReadConnectionSource(final SingleResultCallback<AsyncConnectionSource> callback) {
108116
getAsyncClusterBindingConnectionSource(new ReadPreferenceServerSelector(readPreference), callback);
@@ -117,7 +125,7 @@ public void getReadConnectionSource(final int minWireVersion, final ReadPreferen
117125
} else {
118126
ReadPreferenceWithFallbackServerSelector readPreferenceWithFallbackServerSelector
119127
= new ReadPreferenceWithFallbackServerSelector(readPreference, minWireVersion, fallbackReadPreference);
120-
cluster.selectServerAsync(readPreferenceWithFallbackServerSelector, (result, t) -> {
128+
cluster.selectServerAsync(readPreferenceWithFallbackServerSelector, operationContext, (result, t) -> {
121129
if (t != null) {
122130
callback.onResult(null, t);
123131
} else {
@@ -140,7 +148,7 @@ public void getConnectionSource(final ServerAddress serverAddress, final SingleR
140148

141149
private void getAsyncClusterBindingConnectionSource(final ServerSelector serverSelector,
142150
final SingleResultCallback<AsyncConnectionSource> callback) {
143-
cluster.selectServerAsync(serverSelector, (result, t) -> {
151+
cluster.selectServerAsync(serverSelector, operationContext, (result, t) -> {
144152
if (t != null) {
145153
callback.onResult(null, t);
146154
} else {
@@ -184,14 +192,19 @@ public RequestContext getRequestContext() {
184192
return requestContext;
185193
}
186194

195+
@Override
196+
public OperationContext getOperationContext() {
197+
return operationContext;
198+
}
199+
187200
@Override
188201
public ReadPreference getReadPreference() {
189202
return appliedReadPreference;
190203
}
191204

192205
@Override
193206
public void getConnection(final SingleResultCallback<AsyncConnection> callback) {
194-
server.getConnectionAsync(callback);
207+
server.getConnectionAsync(operationContext, callback);
195208
}
196209

197210
public AsyncConnectionSource retain() {

driver-core/src/main/com/mongodb/internal/binding/AsyncConnectionSource.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@
1717
package com.mongodb.internal.binding;
1818

1919
import com.mongodb.ReadPreference;
20-
import com.mongodb.RequestContext;
21-
import com.mongodb.ServerApi;
22-
import com.mongodb.internal.async.SingleResultCallback;
2320
import com.mongodb.connection.ServerDescription;
21+
import com.mongodb.internal.async.SingleResultCallback;
2422
import com.mongodb.internal.connection.AsyncConnection;
25-
import com.mongodb.internal.session.SessionContext;
26-
import com.mongodb.lang.Nullable;
2723

2824
/**
2925
* A source of connections to a single MongoDB server.
3026
*
3127
* <p>This class is not part of the public API and may be removed or changed at any time</p>
3228
*/
33-
public interface AsyncConnectionSource extends ReferenceCounted {
29+
public interface AsyncConnectionSource extends BindingContext, ReferenceCounted {
3430

3531
/**
3632
* Gets the current description of this source.
@@ -39,20 +35,6 @@ public interface AsyncConnectionSource extends ReferenceCounted {
3935
*/
4036
ServerDescription getServerDescription();
4137

42-
/**
43-
* Gets the session context for this source
44-
*
45-
* @return the session context, which may not be null
46-
*
47-
* <p>This class is not part of the public API and may be removed or changed at any time</p>
48-
*/
49-
SessionContext getSessionContext();
50-
51-
@Nullable
52-
ServerApi getServerApi();
53-
54-
RequestContext getRequestContext();
55-
5638
/**
5739
* Gets the read preference that was applied when selecting this source.
5840
*

driver-core/src/main/com/mongodb/internal/binding/AsyncReadBinding.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,20 @@
1717
package com.mongodb.internal.binding;
1818

1919
import com.mongodb.ReadPreference;
20-
import com.mongodb.RequestContext;
21-
import com.mongodb.ServerApi;
2220
import com.mongodb.internal.async.SingleResultCallback;
23-
import com.mongodb.internal.session.SessionContext;
24-
import com.mongodb.lang.Nullable;
2521

2622
/**
2723
* An asynchronous factory of connection sources to servers that can be read from and that satisfy the specified read preference.
2824
*
2925
* <p>This class is not part of the public API and may be removed or changed at any time</p>
3026
*/
31-
public interface AsyncReadBinding extends ReferenceCounted {
27+
public interface AsyncReadBinding extends BindingContext, ReferenceCounted {
3228
/**
3329
* The read preference that all connection sources returned by this instance will satisfy.
3430
* @return the non-null read preference
3531
*/
3632
ReadPreference getReadPreference();
3733

38-
/**
39-
* Gets the session context for this binding.
40-
*
41-
* @return the session context, which may not be null
42-
*
43-
*/
44-
SessionContext getSessionContext();
45-
46-
@Nullable
47-
ServerApi getServerApi();
48-
49-
RequestContext getRequestContext();
50-
5134
/**
5235
* Returns a connection source to a server that satisfies the read preference with which this instance is configured.
5336
* @param callback the to be passed the connection source

driver-core/src/main/com/mongodb/internal/binding/AsyncWriteBinding.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@
1616

1717
package com.mongodb.internal.binding;
1818

19-
import com.mongodb.RequestContext;
20-
import com.mongodb.ServerApi;
2119
import com.mongodb.internal.async.SingleResultCallback;
22-
import com.mongodb.internal.session.SessionContext;
23-
import com.mongodb.lang.Nullable;
2420

2521
/**
2622
* An asynchronous factory of connection sources to servers that can be written to, e.g, a standalone, a mongos, or a replica set primary.
2723
*
2824
* <p>This class is not part of the public API and may be removed or changed at any time</p>
2925
*/
30-
public interface AsyncWriteBinding extends ReferenceCounted {
26+
public interface AsyncWriteBinding extends BindingContext, ReferenceCounted {
3127

3228
/**
3329
* Supply a connection source to a server that can be written to
@@ -36,19 +32,6 @@ public interface AsyncWriteBinding extends ReferenceCounted {
3632
*/
3733
void getWriteConnectionSource(SingleResultCallback<AsyncConnectionSource> callback);
3834

39-
/**
40-
* Gets the session context for this binding.
41-
*
42-
* @return the session context, which may not be null
43-
*
44-
*/
45-
SessionContext getSessionContext();
46-
47-
@Nullable
48-
ServerApi getServerApi();
49-
50-
RequestContext getRequestContext();
51-
5235
@Override
5336
AsyncWriteBinding retain();
5437
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.internal.binding;
18+
19+
import com.mongodb.RequestContext;
20+
import com.mongodb.ServerApi;
21+
import com.mongodb.internal.connection.OperationContext;
22+
import com.mongodb.internal.session.SessionContext;
23+
import com.mongodb.lang.Nullable;
24+
25+
26+
/**
27+
* <p>This class is not part of the public API and may be removed or changed at any time</p>
28+
*/
29+
public interface BindingContext {
30+
SessionContext getSessionContext();
31+
32+
@Nullable
33+
ServerApi getServerApi();
34+
35+
RequestContext getRequestContext();
36+
37+
OperationContext getOperationContext();
38+
}

driver-core/src/main/com/mongodb/internal/binding/ClusterBinding.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.mongodb.ServerAddress;
2323
import com.mongodb.ServerApi;
2424
import com.mongodb.connection.ClusterConnectionMode;
25+
import com.mongodb.internal.connection.OperationContext;
2526
import com.mongodb.connection.ServerDescription;
2627
import com.mongodb.internal.connection.Cluster;
2728
import com.mongodb.internal.connection.Connection;
@@ -50,6 +51,7 @@ public class ClusterBinding extends AbstractReferenceCounted implements ClusterA
5051
@Nullable
5152
private final ServerApi serverApi;
5253
private final RequestContext requestContext;
54+
private final OperationContext operationContext;
5355

5456
/**
5557
* Creates an instance.
@@ -66,6 +68,7 @@ public ClusterBinding(final Cluster cluster, final ReadPreference readPreference
6668
this.readConcern = notNull("readConcern", readConcern);
6769
this.serverApi = serverApi;
6870
this.requestContext = notNull("requestContext", requestContext);
71+
operationContext = new OperationContext();
6972
}
7073

7174
/**
@@ -103,9 +106,14 @@ public RequestContext getRequestContext() {
103106
return requestContext;
104107
}
105108

109+
@Override
110+
public OperationContext getOperationContext() {
111+
return operationContext;
112+
}
113+
106114
@Override
107115
public ConnectionSource getReadConnectionSource() {
108-
return new ClusterBindingConnectionSource(cluster.selectServer(new ReadPreferenceServerSelector(readPreference)), readPreference);
116+
return new ClusterBindingConnectionSource(cluster.selectServer(new ReadPreferenceServerSelector(readPreference), operationContext), readPreference);
109117
}
110118

111119
@Override
@@ -116,19 +124,19 @@ public ConnectionSource getReadConnectionSource(final int minWireVersion, final
116124
} else {
117125
ReadPreferenceWithFallbackServerSelector readPreferenceWithFallbackServerSelector
118126
= new ReadPreferenceWithFallbackServerSelector(readPreference, minWireVersion, fallbackReadPreference);
119-
ServerTuple serverTuple = cluster.selectServer(readPreferenceWithFallbackServerSelector);
127+
ServerTuple serverTuple = cluster.selectServer(readPreferenceWithFallbackServerSelector, operationContext);
120128
return new ClusterBindingConnectionSource(serverTuple, readPreferenceWithFallbackServerSelector.getAppliedReadPreference());
121129
}
122130
}
123131

124132
@Override
125133
public ConnectionSource getWriteConnectionSource() {
126-
return new ClusterBindingConnectionSource(cluster.selectServer(new WritableServerSelector()), readPreference);
134+
return new ClusterBindingConnectionSource(cluster.selectServer(new WritableServerSelector(), operationContext), readPreference);
127135
}
128136

129137
@Override
130138
public ConnectionSource getConnectionSource(final ServerAddress serverAddress) {
131-
return new ClusterBindingConnectionSource(cluster.selectServer(new ServerAddressSelector(serverAddress)), readPreference);
139+
return new ClusterBindingConnectionSource(cluster.selectServer(new ServerAddressSelector(serverAddress), operationContext), readPreference);
132140
}
133141

134142
private final class ClusterBindingConnectionSource extends AbstractReferenceCounted implements ConnectionSource {
@@ -153,6 +161,11 @@ public SessionContext getSessionContext() {
153161
return new ReadConcernAwareNoOpSessionContext(readConcern);
154162
}
155163

164+
@Override
165+
public OperationContext getOperationContext() {
166+
return operationContext;
167+
}
168+
156169
@Override
157170
public ServerApi getServerApi() {
158171
return serverApi;
@@ -170,7 +183,7 @@ public ReadPreference getReadPreference() {
170183

171184
@Override
172185
public Connection getConnection() {
173-
return server.getConnection();
186+
return server.getConnection(operationContext);
174187
}
175188

176189
public ConnectionSource retain() {

driver-core/src/main/com/mongodb/internal/binding/ConnectionSource.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,18 @@
1717
package com.mongodb.internal.binding;
1818

1919
import com.mongodb.ReadPreference;
20-
import com.mongodb.RequestContext;
21-
import com.mongodb.ServerApi;
2220
import com.mongodb.connection.ServerDescription;
2321
import com.mongodb.internal.connection.Connection;
24-
import com.mongodb.internal.session.SessionContext;
25-
import com.mongodb.lang.Nullable;
2622

2723
/**
2824
* A source of connections to a single MongoDB server.
2925
*
3026
* <p>This class is not part of the public API and may be removed or changed at any time</p>
3127
*/
32-
public interface ConnectionSource extends ReferenceCounted {
28+
public interface ConnectionSource extends BindingContext, ReferenceCounted {
3329

3430
ServerDescription getServerDescription();
3531

36-
SessionContext getSessionContext();
37-
38-
@Nullable
39-
ServerApi getServerApi();
40-
41-
RequestContext getRequestContext();
42-
4332
ReadPreference getReadPreference();
4433

4534
Connection getConnection();

driver-core/src/main/com/mongodb/internal/binding/ReadBinding.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@
1717
package com.mongodb.internal.binding;
1818

1919
import com.mongodb.ReadPreference;
20-
import com.mongodb.RequestContext;
21-
import com.mongodb.ServerApi;
22-
import com.mongodb.internal.session.SessionContext;
23-
import com.mongodb.lang.Nullable;
2420

2521
/**
2622
* A factory of connection sources to servers that can be read from and that satisfy the specified read preference.
2723
*
2824
* <p>This class is not part of the public API and may be removed or changed at any time</p>
2925
*/
30-
public interface ReadBinding extends ReferenceCounted {
26+
public interface ReadBinding extends BindingContext, ReferenceCounted {
3127
ReadPreference getReadPreference();
3228

3329
/**
@@ -48,13 +44,6 @@ public interface ReadBinding extends ReferenceCounted {
4844
*/
4945
ConnectionSource getReadConnectionSource(int minWireVersion, ReadPreference fallbackReadPreference);
5046

51-
SessionContext getSessionContext();
52-
53-
@Nullable
54-
ServerApi getServerApi();
55-
56-
RequestContext getRequestContext();
57-
5847
@Override
5948
ReadBinding retain();
6049
}

0 commit comments

Comments
 (0)