Skip to content

Commit fbc2eaf

Browse files
committed
feat: add configurable DNS cache duration to ClientRoutesConfig
1 parent 122da13 commit fbc2eaf

2 files changed

Lines changed: 98 additions & 4 deletions

File tree

core/src/main/java/com/datastax/oss/driver/api/core/config/ClientRoutesConfig.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* .addEndpoint(new ClientRoutesEndpoint(
4545
* UUID.fromString("12345678-1234-1234-1234-123456789012"),
4646
* "my-privatelink.us-east-1.aws.scylladb.com:9042"))
47+
* .withDnsCacheDuration(1000L) // Cache DNS for 1 second (default: 500ms)
4748
* .build();
4849
*
4950
* CqlSession session = CqlSession.builder()
@@ -57,15 +58,23 @@
5758
@Immutable
5859
public class ClientRoutesConfig {
5960

61+
private static final long DEFAULT_DNS_CACHE_DURATION_MILLIS = 500L;
62+
6063
private final List<ClientRoutesEndpoint> endpoints;
6164
private final String tableName;
65+
private final long dnsCacheDurationMillis;
6266

63-
private ClientRoutesConfig(List<ClientRoutesEndpoint> endpoints, String tableName) {
67+
private ClientRoutesConfig(
68+
List<ClientRoutesEndpoint> endpoints, String tableName, long dnsCacheDurationMillis) {
6469
if (endpoints == null || endpoints.isEmpty()) {
6570
throw new IllegalArgumentException("At least one endpoint must be specified");
6671
}
72+
if (dnsCacheDurationMillis < 0) {
73+
throw new IllegalArgumentException("DNS cache duration must be non-negative");
74+
}
6775
this.endpoints = Collections.unmodifiableList(new ArrayList<>(endpoints));
6876
this.tableName = tableName;
77+
this.dnsCacheDurationMillis = dnsCacheDurationMillis;
6978
}
7079

7180
/**
@@ -88,6 +97,19 @@ public String getTableName() {
8897
return tableName;
8998
}
9099

100+
/**
101+
* Returns the DNS cache duration in milliseconds.
102+
*
103+
* <p>This controls how long resolved DNS entries are cached before being re-resolved. A shorter
104+
* duration is appropriate for dynamic environments where DNS mappings change frequently, while a
105+
* longer duration can reduce DNS lookup overhead in stable environments.
106+
*
107+
* @return the DNS cache duration in milliseconds (default: 500ms).
108+
*/
109+
public long getDnsCacheDurationMillis() {
110+
return dnsCacheDurationMillis;
111+
}
112+
91113
/**
92114
* Creates a new builder for constructing a {@link ClientRoutesConfig}.
93115
*
@@ -107,12 +129,14 @@ public boolean equals(Object o) {
107129
return false;
108130
}
109131
ClientRoutesConfig that = (ClientRoutesConfig) o;
110-
return endpoints.equals(that.endpoints) && Objects.equals(tableName, that.tableName);
132+
return dnsCacheDurationMillis == that.dnsCacheDurationMillis
133+
&& endpoints.equals(that.endpoints)
134+
&& Objects.equals(tableName, that.tableName);
111135
}
112136

113137
@Override
114138
public int hashCode() {
115-
return Objects.hash(endpoints, tableName);
139+
return Objects.hash(endpoints, tableName, dnsCacheDurationMillis);
116140
}
117141

118142
@Override
@@ -123,13 +147,16 @@ public String toString() {
123147
+ ", tableName='"
124148
+ tableName
125149
+ '\''
150+
+ ", dnsCacheDurationMillis="
151+
+ dnsCacheDurationMillis
126152
+ '}';
127153
}
128154

129155
/** Builder for {@link ClientRoutesConfig}. */
130156
public static class Builder {
131157
private final List<ClientRoutesEndpoint> endpoints = new ArrayList<>();
132158
private String tableName;
159+
private long dnsCacheDurationMillis = DEFAULT_DNS_CACHE_DURATION_MILLIS;
133160

134161
/**
135162
* Adds an endpoint to the configuration.
@@ -177,6 +204,29 @@ public Builder withTableName(@Nullable String tableName) {
177204
return this;
178205
}
179206

207+
/**
208+
* Sets the DNS cache duration in milliseconds.
209+
*
210+
* <p>This controls how long resolved DNS entries are cached before being re-resolved. A shorter
211+
* duration is appropriate for dynamic environments where DNS mappings change frequently (e.g.,
212+
* during rolling updates), while a longer duration can reduce DNS lookup overhead in stable
213+
* environments.
214+
*
215+
* <p>Default: 500ms
216+
*
217+
* @param durationMillis the cache duration in milliseconds (must be non-negative).
218+
* @return this builder.
219+
* @throws IllegalArgumentException if the duration is negative.
220+
*/
221+
@NonNull
222+
public Builder withDnsCacheDuration(long durationMillis) {
223+
if (durationMillis < 0) {
224+
throw new IllegalArgumentException("DNS cache duration must be non-negative");
225+
}
226+
this.dnsCacheDurationMillis = durationMillis;
227+
return this;
228+
}
229+
180230
/**
181231
* Builds the {@link ClientRoutesConfig} with the configured endpoints and table name.
182232
*
@@ -185,7 +235,7 @@ public Builder withTableName(@Nullable String tableName) {
185235
*/
186236
@NonNull
187237
public ClientRoutesConfig build() {
188-
return new ClientRoutesConfig(endpoints, tableName);
238+
return new ClientRoutesConfig(endpoints, tableName, dnsCacheDurationMillis);
189239
}
190240
}
191241
}

core/src/test/java/com/datastax/oss/driver/api/core/config/ClientRoutesConfigTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,48 @@ public void should_replace_endpoints_with_withEndpoints() {
120120
assertThat(config.getEndpoints().get(0).getConnectionId()).isEqualTo(connectionId2);
121121
assertThat(config.getEndpoints().get(1).getConnectionId()).isEqualTo(connectionId3);
122122
}
123+
124+
@Test
125+
public void should_use_default_dns_cache_duration() {
126+
ClientRoutesConfig config =
127+
ClientRoutesConfig.builder()
128+
.addEndpoint(new ClientRoutesEndpoint(UUID.randomUUID()))
129+
.build();
130+
131+
assertThat(config.getDnsCacheDurationMillis()).isEqualTo(500L);
132+
}
133+
134+
@Test
135+
public void should_build_config_with_custom_dns_cache_duration() {
136+
ClientRoutesConfig config =
137+
ClientRoutesConfig.builder()
138+
.addEndpoint(new ClientRoutesEndpoint(UUID.randomUUID()))
139+
.withDnsCacheDuration(1000L)
140+
.build();
141+
142+
assertThat(config.getDnsCacheDurationMillis()).isEqualTo(1000L);
143+
}
144+
145+
@Test
146+
public void should_allow_zero_dns_cache_duration() {
147+
ClientRoutesConfig config =
148+
ClientRoutesConfig.builder()
149+
.addEndpoint(new ClientRoutesEndpoint(UUID.randomUUID()))
150+
.withDnsCacheDuration(0L)
151+
.build();
152+
153+
assertThat(config.getDnsCacheDurationMillis()).isEqualTo(0L);
154+
}
155+
156+
@Test
157+
public void should_fail_when_dns_cache_duration_is_negative() {
158+
assertThatThrownBy(
159+
() ->
160+
ClientRoutesConfig.builder()
161+
.addEndpoint(new ClientRoutesEndpoint(UUID.randomUUID()))
162+
.withDnsCacheDuration(-1L)
163+
.build())
164+
.isInstanceOf(IllegalArgumentException.class)
165+
.hasMessageContaining("DNS cache duration must be non-negative");
166+
}
123167
}

0 commit comments

Comments
 (0)