20
20
import com .datastax .dse .driver .api .core .config .DseDriverOption ;
21
21
import com .datastax .oss .driver .api .core .config .DefaultDriverOption ;
22
22
import com .datastax .oss .driver .api .core .config .DriverExecutionProfile ;
23
+ import com .datastax .oss .driver .api .core .loadbalancing .LoadBalancingPolicy ;
24
+ import com .datastax .oss .driver .api .core .loadbalancing .LocalDcAwareLoadBalancingPolicy ;
23
25
import com .datastax .oss .driver .api .core .session .Session ;
24
26
import com .datastax .oss .driver .api .core .uuid .Uuids ;
25
27
import com .datastax .oss .protocol .internal .request .Startup ;
@@ -43,7 +45,6 @@ public class StartupOptionsBuilder {
43
45
private UUID clientId ;
44
46
private String applicationName ;
45
47
private String applicationVersion ;
46
- private String applicationLocalDc ;
47
48
48
49
public StartupOptionsBuilder (InternalDriverContext context ) {
49
50
this .context = context ;
@@ -122,9 +123,8 @@ public Map<String, String> build() {
122
123
if (applicationVersion != null ) {
123
124
builder .put (APPLICATION_VERSION_KEY , applicationVersion );
124
125
}
125
- if (applicationLocalDc == null ) {
126
- applicationLocalDc = localDc (config );
127
- }
126
+ // do not cache local DC as it can change within LBP implementation
127
+ String applicationLocalDc = localDc (config );
128
128
if (applicationLocalDc != null ) {
129
129
builder .put (DRIVER_LOCAL_DC , applicationLocalDc );
130
130
}
@@ -154,10 +154,16 @@ protected String getDriverVersion() {
154
154
155
155
private String localDc (DriverExecutionProfile profile ) {
156
156
String dc = context .getLocalDatacenter (profile .getName ()); // DC set programmatically
157
+ if (dc == null ) {
158
+ // DC from load balancing policy
159
+ LoadBalancingPolicy lbp = context .getLoadBalancingPolicy (DriverExecutionProfile .DEFAULT_NAME );
160
+ if (lbp instanceof LocalDcAwareLoadBalancingPolicy ) {
161
+ dc = ((LocalDcAwareLoadBalancingPolicy ) lbp ).getLocalDatacenter ();
162
+ }
163
+ }
157
164
if (dc == null && profile .isDefined (DefaultDriverOption .LOAD_BALANCING_LOCAL_DATACENTER )) {
158
- dc =
159
- profile .getString (
160
- DefaultDriverOption .LOAD_BALANCING_LOCAL_DATACENTER ); // DC from configuration
165
+ // DC from configuration
166
+ dc = profile .getString (DefaultDriverOption .LOAD_BALANCING_LOCAL_DATACENTER );
161
167
}
162
168
return dc ;
163
169
}
0 commit comments