Skip to content

Commit 101621a

Browse files
Lookup data center from LBP
1 parent 1afcf84 commit 101621a

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package com.datastax.oss.driver.api.core.loadbalancing;
19+
20+
import edu.umd.cs.findbugs.annotations.Nullable;
21+
22+
/** Load balancing policy taking into account local datacenter of the application. */
23+
public interface LocalDcAwareLoadBalancingPolicy extends LoadBalancingPolicy {
24+
25+
/** Returns the local datacenter name, if known; empty otherwise. */
26+
@Nullable
27+
String getLocalDatacenter();
28+
}

core/src/main/java/com/datastax/oss/driver/internal/core/context/StartupOptionsBuilder.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.datastax.dse.driver.api.core.config.DseDriverOption;
2121
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
2222
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;
2325
import com.datastax.oss.driver.api.core.session.Session;
2426
import com.datastax.oss.driver.api.core.uuid.Uuids;
2527
import com.datastax.oss.protocol.internal.request.Startup;
@@ -43,7 +45,6 @@ public class StartupOptionsBuilder {
4345
private UUID clientId;
4446
private String applicationName;
4547
private String applicationVersion;
46-
private String applicationLocalDc;
4748

4849
public StartupOptionsBuilder(InternalDriverContext context) {
4950
this.context = context;
@@ -122,9 +123,8 @@ public Map<String, String> build() {
122123
if (applicationVersion != null) {
123124
builder.put(APPLICATION_VERSION_KEY, applicationVersion);
124125
}
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);
128128
if (applicationLocalDc != null) {
129129
builder.put(DRIVER_LOCAL_DC, applicationLocalDc);
130130
}
@@ -154,10 +154,16 @@ protected String getDriverVersion() {
154154

155155
private String localDc(DriverExecutionProfile profile) {
156156
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+
}
157164
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);
161167
}
162168
return dc;
163169
}

core/src/main/java/com/datastax/oss/driver/internal/core/loadbalancing/BasicLoadBalancingPolicy.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.datastax.oss.driver.api.core.context.DriverContext;
2525
import com.datastax.oss.driver.api.core.cql.Statement;
2626
import com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy;
27+
import com.datastax.oss.driver.api.core.loadbalancing.LocalDcAwareLoadBalancingPolicy;
2728
import com.datastax.oss.driver.api.core.loadbalancing.NodeDistance;
2829
import com.datastax.oss.driver.api.core.loadbalancing.NodeDistanceEvaluator;
2930
import com.datastax.oss.driver.api.core.metadata.Node;
@@ -99,7 +100,7 @@
99100
* DefaultLoadBalancingPolicy}</b>.
100101
*/
101102
@ThreadSafe
102-
public class BasicLoadBalancingPolicy implements LoadBalancingPolicy {
103+
public class BasicLoadBalancingPolicy implements LocalDcAwareLoadBalancingPolicy {
103104

104105
private static final Logger LOG = LoggerFactory.getLogger(BasicLoadBalancingPolicy.class);
105106

@@ -155,7 +156,8 @@ public BasicLoadBalancingPolicy(@NonNull DriverContext context, @NonNull String
155156
* Before initialization, this method always returns null.
156157
*/
157158
@Nullable
158-
protected String getLocalDatacenter() {
159+
@Override
160+
public String getLocalDatacenter() {
159161
return localDc;
160162
}
161163

0 commit comments

Comments
 (0)