31
31
import com .datastax .oss .driver .api .core .config .DriverExecutionProfile ;
32
32
import com .datastax .oss .driver .api .core .loadbalancing .LoadBalancingPolicy ;
33
33
import com .datastax .oss .driver .api .core .loadbalancing .LocalDcAwareLoadBalancingPolicy ;
34
+ import com .datastax .oss .driver .api .core .loadbalancing .NodeDistanceEvaluator ;
35
+ import com .datastax .oss .driver .api .core .metadata .Node ;
34
36
import com .datastax .oss .driver .api .core .session .ProgrammaticArguments ;
35
37
import com .datastax .oss .driver .api .core .session .Session ;
36
38
import com .datastax .oss .driver .api .core .uuid .Uuids ;
39
+ import com .datastax .oss .driver .internal .core .ConsistencyLevelRegistry ;
37
40
import com .datastax .oss .driver .internal .core .context .DefaultDriverContext ;
41
+ import com .datastax .oss .driver .internal .core .context .InternalDriverContext ;
38
42
import com .datastax .oss .driver .internal .core .context .StartupOptionsBuilder ;
43
+ import com .datastax .oss .driver .internal .core .loadbalancing .DefaultLoadBalancingPolicy ;
44
+ import com .datastax .oss .driver .shaded .guava .common .collect .ImmutableList ;
39
45
import com .datastax .oss .driver .shaded .guava .common .collect .ImmutableMap ;
40
46
import com .datastax .oss .protocol .internal .request .Startup ;
41
47
import com .tngtech .java .junit .dataprovider .DataProvider ;
42
48
import com .tngtech .java .junit .dataprovider .DataProviderRunner ;
43
49
import edu .umd .cs .findbugs .annotations .NonNull ;
50
+ import edu .umd .cs .findbugs .annotations .Nullable ;
51
+ import java .util .Collections ;
52
+ import java .util .List ;
44
53
import java .util .Map ;
54
+ import java .util .Optional ;
45
55
import java .util .UUID ;
46
56
import org .junit .Before ;
47
57
import org .junit .Test ;
@@ -86,8 +96,7 @@ public Map<String, LoadBalancingPolicy> getLoadBalancingPolicies() {
86
96
localDcPerProfile .forEach (
87
97
(profile , dc ) -> {
88
98
LocalDcAwareLoadBalancingPolicy loadBalancingPolicy =
89
- mock (LocalDcAwareLoadBalancingPolicy .class );
90
- when (loadBalancingPolicy .getLocalDatacenter ()).thenReturn (dc );
99
+ mockLoadBalancingPolicy (profile , dc , 0 , false , Collections .emptyList ());
91
100
map .put (profile , loadBalancingPolicy );
92
101
});
93
102
return map .build ();
@@ -97,6 +106,55 @@ public Map<String, LoadBalancingPolicy> getLoadBalancingPolicies() {
97
106
};
98
107
}
99
108
109
+ private LocalDcAwareLoadBalancingPolicy mockLoadBalancingPolicy (
110
+ String profile ,
111
+ String localDc ,
112
+ int maxNodesPerRemoteDc ,
113
+ boolean allowRemoteSatisfyLocalDc ,
114
+ List <String > preferredRemoteDcs ) {
115
+ // mock execution profile
116
+ DriverExecutionProfile executionProfile = mock (DriverExecutionProfile .class );
117
+ when (executionProfile .getInt (
118
+ DefaultDriverOption .LOAD_BALANCING_DC_FAILOVER_MAX_NODES_PER_REMOTE_DC ))
119
+ .thenReturn (maxNodesPerRemoteDc );
120
+ when (executionProfile .getBoolean (
121
+ DefaultDriverOption .LOAD_BALANCING_DC_FAILOVER_ALLOW_FOR_LOCAL_CONSISTENCY_LEVELS ))
122
+ .thenReturn (allowRemoteSatisfyLocalDc );
123
+ when (executionProfile .getStringList (
124
+ DefaultDriverOption .LOAD_BALANCING_DC_FAILOVER_PREFERRED_REMOTE_DCS ))
125
+ .thenReturn (preferredRemoteDcs );
126
+
127
+ // mock driver config
128
+ DriverConfig driverConfig = mock (DriverConfig .class );
129
+ when (driverConfig .getProfile (profile )).thenReturn (executionProfile );
130
+
131
+ // mock driver context
132
+ InternalDriverContext driverContext = mock (InternalDriverContext .class );
133
+ when (driverContext .getConfig ()).thenReturn (driverConfig );
134
+ when (driverContext .getConsistencyLevelRegistry ())
135
+ .thenReturn (mock (ConsistencyLevelRegistry .class ));
136
+
137
+ // mock load balancing policy
138
+ LocalDcAwareLoadBalancingPolicy loadBalancingPolicy =
139
+ new DefaultLoadBalancingPolicy (driverContext , profile ) {
140
+ @ NonNull
141
+ @ Override
142
+ protected Optional <String > discoverLocalDc (@ NonNull Map <UUID , Node > nodes ) {
143
+ return Optional .of (localDc );
144
+ }
145
+
146
+ @ NonNull
147
+ @ Override
148
+ protected NodeDistanceEvaluator createNodeDistanceEvaluator (
149
+ @ Nullable String localDc , @ NonNull Map <UUID , Node > nodes ) {
150
+ return mock (NodeDistanceEvaluator .class );
151
+ }
152
+ };
153
+ loadBalancingPolicy .init (
154
+ Collections .emptyMap (), mock (LoadBalancingPolicy .DistanceReporter .class ));
155
+ return loadBalancingPolicy ;
156
+ }
157
+
100
158
private void assertDefaultStartupOptions (Startup startup ) {
101
159
assertThat (startup .options ).containsEntry (Startup .CQL_VERSION_KEY , "3.0.0" );
102
160
assertThat (startup .options )
@@ -188,7 +246,9 @@ public void should_build_startup_options_with_all_options() {
188
246
.containsEntry (StartupOptionsBuilder .CLIENT_ID_KEY , customClientId .toString ())
189
247
.containsEntry (StartupOptionsBuilder .APPLICATION_NAME_KEY , "Custom_App_Name" )
190
248
.containsEntry (StartupOptionsBuilder .APPLICATION_VERSION_KEY , "Custom_App_Version" )
191
- .containsEntry (StartupOptionsBuilder .DRIVER_LOCAL_DC , "default: dc6" );
249
+ .containsEntry (
250
+ StartupOptionsBuilder .DRIVER_BAGGAGE ,
251
+ "{\" default\" : {\" DefaultLoadBalancingPolicy\" :{\" localDc\" :\" dc6\" }}}" );
192
252
assertThat (startup .options ).containsEntry (Startup .COMPRESSION_KEY , "snappy" );
193
253
assertDefaultStartupOptions (startup );
194
254
}
@@ -226,7 +286,9 @@ public void should_ignore_configuration_when_programmatic_values_provided() {
226
286
assertThat (startup .options )
227
287
.containsEntry (StartupOptionsBuilder .APPLICATION_NAME_KEY , "Custom_App_Name" )
228
288
.containsEntry (StartupOptionsBuilder .APPLICATION_VERSION_KEY , "Custom_App_Version" )
229
- .containsEntry (StartupOptionsBuilder .DRIVER_LOCAL_DC , "default: us-west-2" );
289
+ .containsEntry (
290
+ StartupOptionsBuilder .DRIVER_BAGGAGE ,
291
+ "{\" default\" : {\" DefaultLoadBalancingPolicy\" :{\" localDc\" :\" us-west-2\" }}}" );
230
292
}
231
293
232
294
@ Test
@@ -246,12 +308,14 @@ public void should_include_all_local_dc_in_startup_message() {
246
308
.containsEntry (StartupOptionsBuilder .APPLICATION_NAME_KEY , "Custom_App_Name" )
247
309
.containsEntry (StartupOptionsBuilder .APPLICATION_VERSION_KEY , "Custom_App_Version" )
248
310
.containsEntry (
249
- StartupOptionsBuilder .DRIVER_LOCAL_DC ,
250
- "default: us-west-2, oltp: us-east-2, olap: eu-central-1" );
311
+ StartupOptionsBuilder .DRIVER_BAGGAGE ,
312
+ "{\" default\" : {\" DefaultLoadBalancingPolicy\" :{\" localDc\" :\" us-west-2\" }}, "
313
+ + "\" oltp\" : {\" DefaultLoadBalancingPolicy\" :{\" localDc\" :\" us-east-2\" }}, "
314
+ + "\" olap\" : {\" DefaultLoadBalancingPolicy\" :{\" localDc\" :\" eu-central-1\" }}}" );
251
315
}
252
316
253
317
@ Test
254
- public void should_skip_non_local_dc_lbp_in_startup_message () {
318
+ public void should_include_all_lbp_details_in_startup_message () {
255
319
when (defaultProfile .getString (DefaultDriverOption .PROTOCOL_COMPRESSION , "none" ))
256
320
.thenReturn ("none" );
257
321
when (defaultProfile .getName ()).thenReturn (DriverExecutionProfile .DEFAULT_NAME );
@@ -263,16 +327,22 @@ public void should_skip_non_local_dc_lbp_in_startup_message() {
263
327
@ Override
264
328
public Map <String , LoadBalancingPolicy > getLoadBalancingPolicies () {
265
329
ImmutableMap .Builder <String , LoadBalancingPolicy > map = ImmutableMap .builder ();
266
- LocalDcAwareLoadBalancingPolicy loadBalancingPolicy =
267
- mock (LocalDcAwareLoadBalancingPolicy .class );
268
- when (loadBalancingPolicy .getLocalDatacenter ()).thenReturn ("dc1" );
269
- map .put ("oltp" , loadBalancingPolicy );
330
+ map .put (
331
+ "oltp" ,
332
+ mockLoadBalancingPolicy ("oltp" , "dc1" , 2 , true , ImmutableList .of ("dc2" , "dc3" )));
270
333
map .put ("default" , mock (LoadBalancingPolicy .class ));
271
334
return map .build ();
272
335
}
273
336
};
274
337
Startup startup = new Startup (driverContext .getStartupOptions ());
275
338
276
- assertThat (startup .options ).containsEntry (StartupOptionsBuilder .DRIVER_LOCAL_DC , "oltp: dc1" );
339
+ assertThat (startup .options )
340
+ .containsEntry (
341
+ StartupOptionsBuilder .DRIVER_BAGGAGE ,
342
+ "{\" oltp\" : {\" DefaultLoadBalancingPolicy\" :{"
343
+ + "\" localDc\" :\" dc1\" ,"
344
+ + "\" preferredRemoteDcs\" :[\" dc2\" , \" dc3\" ],"
345
+ + "\" allowDcFailoverForLocalCl\" :true,"
346
+ + "\" maxNodesPerRemoteDc\" :2}}}" );
277
347
}
278
348
}
0 commit comments