Skip to content

Commit 1afcf84

Browse files
CASSJAVA-92: Local DC provided for nodetool clientstats
1 parent 342e2dc commit 1afcf84

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.datastax.oss.driver.internal.core.context;
1919

2020
import com.datastax.dse.driver.api.core.config.DseDriverOption;
21+
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
2122
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
2223
import com.datastax.oss.driver.api.core.session.Session;
2324
import com.datastax.oss.driver.api.core.uuid.Uuids;
@@ -33,6 +34,7 @@ public class StartupOptionsBuilder {
3334

3435
public static final String DRIVER_NAME_KEY = "DRIVER_NAME";
3536
public static final String DRIVER_VERSION_KEY = "DRIVER_VERSION";
37+
public static final String DRIVER_LOCAL_DC = "DRIVER_LOCAL_DC";
3638
public static final String APPLICATION_NAME_KEY = "APPLICATION_NAME";
3739
public static final String APPLICATION_VERSION_KEY = "APPLICATION_VERSION";
3840
public static final String CLIENT_ID_KEY = "CLIENT_ID";
@@ -41,6 +43,7 @@ public class StartupOptionsBuilder {
4143
private UUID clientId;
4244
private String applicationName;
4345
private String applicationVersion;
46+
private String applicationLocalDc;
4447

4548
public StartupOptionsBuilder(InternalDriverContext context) {
4649
this.context = context;
@@ -119,6 +122,12 @@ public Map<String, String> build() {
119122
if (applicationVersion != null) {
120123
builder.put(APPLICATION_VERSION_KEY, applicationVersion);
121124
}
125+
if (applicationLocalDc == null) {
126+
applicationLocalDc = localDc(config);
127+
}
128+
if (applicationLocalDc != null) {
129+
builder.put(DRIVER_LOCAL_DC, applicationLocalDc);
130+
}
122131

123132
return builder.build();
124133
}
@@ -142,4 +151,14 @@ protected String getDriverName() {
142151
protected String getDriverVersion() {
143152
return Session.OSS_DRIVER_COORDINATES.getVersion().toString();
144153
}
154+
155+
private String localDc(DriverExecutionProfile profile) {
156+
String dc = context.getLocalDatacenter(profile.getName()); // DC set programmatically
157+
if (dc == null && profile.isDefined(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER)) {
158+
dc =
159+
profile.getString(
160+
DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER); // DC from configuration
161+
}
162+
return dc;
163+
}
145164
}

core/src/test/java/com/datastax/dse/driver/internal/core/context/DseStartupOptionsBuilderTest.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ public void before() {
6060
when(defaultProfile.isDefined(DseDriverOption.CONTINUOUS_PAGING_PAGE_SIZE)).thenReturn(true);
6161
}
6262

63-
private void buildContext(UUID clientId, String applicationName, String applicationVersion) {
64-
this.driverContext =
65-
new DefaultDriverContext(
66-
configLoader,
67-
ProgrammaticArguments.builder()
68-
.withStartupClientId(clientId)
69-
.withStartupApplicationName(applicationName)
70-
.withStartupApplicationVersion(applicationVersion)
71-
.build());
63+
private void buildContext(
64+
UUID clientId, String applicationName, String applicationVersion, String localDc) {
65+
ProgrammaticArguments.Builder builder =
66+
ProgrammaticArguments.builder()
67+
.withStartupClientId(clientId)
68+
.withStartupApplicationName(applicationName)
69+
.withStartupApplicationVersion(applicationVersion);
70+
if (localDc != null) {
71+
builder.withLocalDatacenter(DriverExecutionProfile.DEFAULT_NAME, localDc);
72+
}
73+
this.driverContext = new DefaultDriverContext(configLoader, builder.build());
7274
}
7375

7476
private void assertDefaultStartupOptions(Startup startup) {
@@ -86,7 +88,7 @@ private void assertDefaultStartupOptions(Startup startup) {
8688
public void should_build_startup_options_with_no_compression_if_undefined() {
8789
when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"))
8890
.thenReturn("none");
89-
buildContext(null, null, null);
91+
buildContext(null, null, null, null);
9092
Startup startup = new Startup(driverContext.getStartupOptions());
9193
assertThat(startup.options).doesNotContainKey(Startup.COMPRESSION_KEY);
9294
assertDefaultStartupOptions(startup);
@@ -97,7 +99,7 @@ public void should_build_startup_options_with_no_compression_if_undefined() {
9799
public void should_build_startup_options_with_compression(String compression) {
98100
when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"))
99101
.thenReturn(compression);
100-
buildContext(null, null, null);
102+
buildContext(null, null, null, null);
101103
Startup startup = new Startup(driverContext.getStartupOptions());
102104
// assert the compression option is present
103105
assertThat(startup.options).containsEntry(Startup.COMPRESSION_KEY, compression);
@@ -110,7 +112,7 @@ public void should_build_startup_options_with_compression(String compression) {
110112
public void should_fail_to_build_startup_options_with_invalid_compression() {
111113
when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"))
112114
.thenReturn("foobar");
113-
buildContext(null, null, null);
115+
buildContext(null, null, null, null);
114116
assertThatIllegalArgumentException()
115117
.isThrownBy(() -> new Startup(driverContext.getStartupOptions()));
116118
}
@@ -120,7 +122,7 @@ public void should_build_startup_options_with_client_id() {
120122
when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"))
121123
.thenReturn("none");
122124
UUID customClientId = Uuids.random();
123-
buildContext(customClientId, null, null);
125+
buildContext(customClientId, null, null, null);
124126
Startup startup = new Startup(driverContext.getStartupOptions());
125127
// assert the client id is present
126128
assertThat(startup.options)
@@ -135,7 +137,7 @@ public void should_build_startup_options_with_client_id() {
135137
public void should_build_startup_options_with_application_version_and_name() {
136138
when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"))
137139
.thenReturn("none");
138-
buildContext(null, "Custom_App_Name", "Custom_App_Version");
140+
buildContext(null, "Custom_App_Name", "Custom_App_Version", null);
139141
Startup startup = new Startup(driverContext.getStartupOptions());
140142
// assert the app name and version are present
141143
assertThat(startup.options)
@@ -151,15 +153,17 @@ public void should_build_startup_options_with_all_options() {
151153
// mock config to specify "snappy" compression
152154
when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"))
153155
.thenReturn("snappy");
156+
when(defaultProfile.getName()).thenReturn(DriverExecutionProfile.DEFAULT_NAME);
154157

155158
UUID customClientId = Uuids.random();
156159

157-
buildContext(customClientId, "Custom_App_Name", "Custom_App_Version");
160+
buildContext(customClientId, "Custom_App_Name", "Custom_App_Version", "dc6");
158161
Startup startup = new Startup(driverContext.getStartupOptions());
159162
assertThat(startup.options)
160163
.containsEntry(StartupOptionsBuilder.CLIENT_ID_KEY, customClientId.toString())
161164
.containsEntry(StartupOptionsBuilder.APPLICATION_NAME_KEY, "Custom_App_Name")
162-
.containsEntry(StartupOptionsBuilder.APPLICATION_VERSION_KEY, "Custom_App_Version");
165+
.containsEntry(StartupOptionsBuilder.APPLICATION_VERSION_KEY, "Custom_App_Version")
166+
.containsEntry(StartupOptionsBuilder.DRIVER_LOCAL_DC, "dc6");
163167
assertThat(startup.options).containsEntry(Startup.COMPRESSION_KEY, "snappy");
164168
assertDefaultStartupOptions(startup);
165169
}
@@ -172,25 +176,33 @@ public void should_use_configuration_when_no_programmatic_values_provided() {
172176
.thenReturn("Config_App_Version");
173177
when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"))
174178
.thenReturn("none");
179+
when(defaultProfile.getString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER))
180+
.thenReturn("Config_DC_1");
181+
when(defaultProfile.isDefined(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER))
182+
.thenReturn(true);
183+
when(defaultProfile.getName()).thenReturn(DriverExecutionProfile.DEFAULT_NAME);
175184

176-
buildContext(null, null, null);
185+
buildContext(null, null, null, null);
177186
Startup startup = new Startup(driverContext.getStartupOptions());
178187

179188
assertThat(startup.options)
180189
.containsEntry(StartupOptionsBuilder.APPLICATION_NAME_KEY, "Config_App_Name")
181-
.containsEntry(StartupOptionsBuilder.APPLICATION_VERSION_KEY, "Config_App_Version");
190+
.containsEntry(StartupOptionsBuilder.APPLICATION_VERSION_KEY, "Config_App_Version")
191+
.containsEntry(StartupOptionsBuilder.DRIVER_LOCAL_DC, "Config_DC_1");
182192
}
183193

184194
@Test
185195
public void should_ignore_configuration_when_programmatic_values_provided() {
186196
when(defaultProfile.getString(DefaultDriverOption.PROTOCOL_COMPRESSION, "none"))
187197
.thenReturn("none");
198+
when(defaultProfile.getName()).thenReturn(DriverExecutionProfile.DEFAULT_NAME);
188199

189-
buildContext(null, "Custom_App_Name", "Custom_App_Version");
200+
buildContext(null, "Custom_App_Name", "Custom_App_Version", "us-west-2");
190201
Startup startup = new Startup(driverContext.getStartupOptions());
191202

192203
assertThat(startup.options)
193204
.containsEntry(StartupOptionsBuilder.APPLICATION_NAME_KEY, "Custom_App_Name")
194-
.containsEntry(StartupOptionsBuilder.APPLICATION_VERSION_KEY, "Custom_App_Version");
205+
.containsEntry(StartupOptionsBuilder.APPLICATION_VERSION_KEY, "Custom_App_Version")
206+
.containsEntry(StartupOptionsBuilder.DRIVER_LOCAL_DC, "us-west-2");
195207
}
196208
}

0 commit comments

Comments
 (0)