Skip to content

Commit b85b1aa

Browse files
committed
feat: (WIP) add writeTimeout and queryTimeout to ClientConfig. Properties added. Next, apply in transports.
1 parent 36574b9 commit b85b1aa

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed

src/main/java/com/influxdb/v3/client/config/ClientConfig.java

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@
5454
* <li><code>defaultTags</code> - defaultTags added when writing points to InfluxDB</li>
5555
* <li><code>gzipThreshold</code> - threshold when gzip compression is used for writing points to InfluxDB</li>
5656
* <li><code>writeNoSync</code> - skip waiting for WAL persistence on write</li>
57-
* <li><code>responseTimeout</code> - timeout when connecting to InfluxDB</li>
57+
* <li><code>timeout</code> - <i>deprecated in 1.3.0</i> timeout when connecting to InfluxDB,
58+
* please use more informative properties <code>writeTimeout</code> and <code>queryTimeout</code></li>
59+
* <li><code>writeTimeout</code> - timeout when writing data to InfluxDB</li>
60+
* <li><code>queryTimeout</code> - timeout used to calculate a default GRPC deadline when querying InfluxDB.
61+
* Can be <code>null</code>, in which case queries can potentially run forever.</li>
5862
* <li><code>allowHttpRedirects</code> - allow redirects for InfluxDB connections</li>
5963
* <li><code>disableServerCertificateValidation</code> -
6064
* disable server certificate validation for HTTPS connections
@@ -100,6 +104,8 @@ public final class ClientConfig {
100104
private final Boolean writeNoSync;
101105
private final Map<String, String> defaultTags;
102106
private final Duration timeout;
107+
private final Duration writeTimeout;
108+
private final Duration queryTimeout;
103109
private final Boolean allowHttpRedirects;
104110
private final Boolean disableServerCertificateValidation;
105111
private final String proxyUrl;
@@ -212,6 +218,28 @@ public Duration getTimeout() {
212218
return timeout;
213219
}
214220

221+
/**
222+
* Gets the default timeout in seconds to use for REST Write API calls. Default is
223+
* {@value com.influxdb.v3.client.write.WriteOptions#DEFAULT_WRITE_TIMEOUT}
224+
*
225+
* @return the default timeout in seconds to use for REST Write API calls.
226+
*/
227+
@Nonnull
228+
public Duration getWriteTimeout() {
229+
return writeTimeout;
230+
}
231+
232+
/**
233+
* Gets the default timeout in seconds to use for calculating a GRPC Deadline when making Query API calls.
234+
* Can be null, in which case queries can potentially wait or run forever.
235+
*
236+
* @return the default timeout in seconds to use for Query API calls.
237+
*/
238+
@Nullable
239+
public Duration getQueryTimeout() {
240+
return queryTimeout;
241+
}
242+
215243
/**
216244
* Gets the automatically following HTTP 3xx redirects. Default to 'false'.
217245
*
@@ -312,6 +340,8 @@ public boolean equals(final Object o) {
312340
&& Objects.equals(writeNoSync, that.writeNoSync)
313341
&& Objects.equals(defaultTags, that.defaultTags)
314342
&& Objects.equals(timeout, that.timeout)
343+
&& Objects.equals(writeTimeout, that.writeTimeout)
344+
&& Objects.equals(queryTimeout, that.queryTimeout)
315345
&& Objects.equals(allowHttpRedirects, that.allowHttpRedirects)
316346
&& Objects.equals(disableServerCertificateValidation, that.disableServerCertificateValidation)
317347
&& Objects.equals(proxy, that.proxy)
@@ -325,7 +355,7 @@ public boolean equals(final Object o) {
325355
public int hashCode() {
326356
return Objects.hash(host, Arrays.hashCode(token), authScheme, organization,
327357
database, writePrecision, gzipThreshold, writeNoSync,
328-
timeout, allowHttpRedirects, disableServerCertificateValidation,
358+
timeout, writeTimeout, queryTimeout, allowHttpRedirects, disableServerCertificateValidation,
329359
proxy, proxyUrl, authenticator, headers,
330360
defaultTags, sslRootsFilePath);
331361
}
@@ -340,6 +370,8 @@ public String toString() {
340370
.add("gzipThreshold=" + gzipThreshold)
341371
.add("writeNoSync=" + writeNoSync)
342372
.add("timeout=" + timeout)
373+
.add("writeTimeout=" + writeTimeout)
374+
.add("queryTimeout=" + queryTimeout)
343375
.add("allowHttpRedirects=" + allowHttpRedirects)
344376
.add("disableServerCertificateValidation=" + disableServerCertificateValidation)
345377
.add("proxy=" + proxy)
@@ -367,6 +399,8 @@ public static final class Builder {
367399
private Boolean writeNoSync;
368400
private Map<String, String> defaultTags;
369401
private Duration timeout;
402+
private Duration writeTimeout;
403+
private Duration queryTimeout;
370404
private Boolean allowHttpRedirects;
371405
private Boolean disableServerCertificateValidation;
372406
private ProxySelector proxy;
@@ -497,17 +531,50 @@ public Builder defaultTags(@Nullable final Map<String, String> defaultTags) {
497531

498532
/**
499533
* Sets the default timeout to use for the API calls. Default to '10 seconds'.
534+
* <p>
535+
* Note that this parameter is being superseded by clearer writeTimeout.
500536
*
501537
* @param timeout default timeout to use for the API calls. Default to '10 seconds'.
502538
* @return this
503539
*/
540+
@Deprecated
504541
@Nonnull
505542
public Builder timeout(@Nullable final Duration timeout) {
506543

507544
this.timeout = timeout;
508545
return this;
509546
}
510547

548+
/**
549+
* Sets the default writeTimeout to use for Write API calls in the REST client.
550+
* Default is {@value com.influxdb.v3.client.write.WriteOptions#DEFAULT_WRITE_TIMEOUT}
551+
*
552+
* @param writeTimeout default timeout to use for REST API write calls. Default is
553+
* {@value com.influxdb.v3.client.write.WriteOptions#DEFAULT_WRITE_TIMEOUT}
554+
* @return - this
555+
*/
556+
@Nonnull
557+
public Builder writeTimeout(@Nullable final Duration writeTimeout) {
558+
559+
this.writeTimeout = writeTimeout;
560+
return this;
561+
}
562+
563+
/**
564+
* Sets standard query timeout used to calculate a GRPC deadline when making Query API calls.
565+
* If <code>null</code>, queries can potentially wait or run forever.
566+
*
567+
* @param queryTimeout default timeout used to calculate deadline for Query API calls.
568+
* If <code>null</code>, queries can potentially wait or run forever.
569+
* Default value is <code>null</code>.
570+
* @return this
571+
*/
572+
@Nonnull
573+
public Builder queryTimeout(@Nullable final Duration queryTimeout) {
574+
this.queryTimeout = queryTimeout;
575+
return this;
576+
}
577+
511578
/**
512579
* Sets the automatically following HTTP 3xx redirects. Default to 'false'.
513580
*
@@ -719,6 +786,16 @@ public ClientConfig build(@Nonnull final Map<String, String> env, final Properti
719786
if (writeNoSync != null) {
720787
this.writeNoSync(Boolean.parseBoolean(writeNoSync));
721788
}
789+
final String writeTimeout = get.apply("INFLUX_WRITE_TIMEOUT", "influx.writeTimeout");
790+
if (writeTimeout != null) {
791+
long to = Long.parseLong(writeTimeout);
792+
this.writeTimeout(Duration.ofSeconds(to));
793+
}
794+
final String queryTimeout = get.apply("INFLUX_QUERY_TIMEOUT", "influx.queryTimeout");
795+
if (queryTimeout != null) {
796+
long to = Long.parseLong(queryTimeout);
797+
this.queryTimeout(Duration.ofSeconds(to));
798+
}
722799

723800
return new ClientConfig(this);
724801
}
@@ -761,7 +838,10 @@ private ClientConfig(@Nonnull final Builder builder) {
761838
gzipThreshold = builder.gzipThreshold != null ? builder.gzipThreshold : WriteOptions.DEFAULT_GZIP_THRESHOLD;
762839
writeNoSync = builder.writeNoSync != null ? builder.writeNoSync : WriteOptions.DEFAULT_NO_SYNC;
763840
defaultTags = builder.defaultTags;
764-
timeout = builder.timeout != null ? builder.timeout : Duration.ofSeconds(10);
841+
timeout = builder.timeout != null ? builder.timeout : Duration.ofSeconds(WriteOptions.DEFAULT_WRITE_TIMEOUT);
842+
writeTimeout = builder.writeTimeout != null
843+
? builder.writeTimeout : Duration.ofSeconds(WriteOptions.DEFAULT_WRITE_TIMEOUT);
844+
queryTimeout = builder.queryTimeout;
765845
allowHttpRedirects = builder.allowHttpRedirects != null ? builder.allowHttpRedirects : false;
766846
disableServerCertificateValidation = builder.disableServerCertificateValidation != null
767847
? builder.disableServerCertificateValidation : false;

src/main/java/com/influxdb/v3/client/write/WriteOptions.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,17 @@ public final class WriteOptions {
6565
* Default NoSync.
6666
*/
6767
public static final boolean DEFAULT_NO_SYNC = false;
68-
/**
68+
69+
/**
70+
* Default timeout for writes in seconds. Set to {@value}
71+
*/
72+
public static final int DEFAULT_WRITE_TIMEOUT = 10;
73+
74+
/**
6975
* Default WriteOptions.
7076
* Deprecated use {@link #defaultWriteOptions} instead
7177
*/
78+
7279
@Deprecated(forRemoval = true)
7380
public static final WriteOptions DEFAULTS = new WriteOptions(
7481
null, DEFAULT_WRITE_PRECISION, DEFAULT_GZIP_THRESHOLD, DEFAULT_NO_SYNC, null, null);

src/test/java/com/influxdb/v3/client/config/ClientConfigTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class ClientConfigTest {
4242
.database("my-db")
4343
.writePrecision(WritePrecision.NS)
4444
.timeout(Duration.ofSeconds(30))
45+
.writeTimeout(Duration.ofSeconds(30))
46+
.queryTimeout(Duration.ofSeconds(120))
4547
.allowHttpRedirects(true)
4648
.disableServerCertificateValidation(true)
4749
.headers(Map.of("X-device", "ab-01"));
@@ -241,6 +243,24 @@ void fromEnvLongPrecision() {
241243
Assertions.assertThat(cfg.getWritePrecision()).isEqualTo(WritePrecision.S);
242244
}
243245

246+
@Test
247+
void timeoutsFromEnv() {
248+
Map<String, String> env = Map.of(
249+
"INFLUX_HOST", "http://localhost:9999/",
250+
"INFLUX_TOKEN", "my-token",
251+
"INFLUX_WRITE_TIMEOUT", "45",
252+
"INFLUX_QUERY_TIMEOUT", "180");
253+
254+
Duration writeTimeout = Duration.ofSeconds(45);
255+
Duration queryTimeout = Duration.ofSeconds(180);
256+
ClientConfig cfg = new ClientConfig.Builder().build(env, null);
257+
Assertions.assertThat(cfg.getHost()).isEqualTo("http://localhost:9999/");
258+
Assertions.assertThat(cfg.getToken()).isEqualTo("my-token".toCharArray());
259+
Assertions.assertThat(cfg.getWriteTimeout()).isEqualTo(writeTimeout);
260+
Assertions.assertThat(cfg.getQueryTimeout()).isEqualTo(queryTimeout);
261+
262+
}
263+
244264
@Test
245265
void fromSystemProperties() {
246266
// minimal
@@ -335,4 +355,24 @@ void fromSystemPropertiesLongPrecision() throws MalformedURLException {
335355
Assertions.assertThat(cfg.getWritePrecision()).isEqualTo(WritePrecision.S);
336356
}
337357

358+
@Test
359+
void fromSystemPropertiesTimeouts() {
360+
Properties props = new Properties();
361+
props.put("influx.host", "http://localhost:9999/");
362+
props.put("influx.token", "my-token");
363+
props.put("influx.writeTimeout", "20");
364+
props.put("influx.queryTimeout", "300");
365+
366+
ClientConfig cfg = new ClientConfig.Builder().build(new HashMap<>(), props);
367+
368+
Duration writeTimeout = Duration.ofSeconds(20);
369+
Duration queryTimeout = Duration.ofSeconds(300);
370+
371+
Assertions.assertThat(cfg.getHost()).isEqualTo("http://localhost:9999/");
372+
Assertions.assertThat(cfg.getToken()).isEqualTo("my-token".toCharArray());
373+
Assertions.assertThat(cfg.getWriteTimeout()).isEqualTo(writeTimeout);
374+
Assertions.assertThat(cfg.getQueryTimeout()).isEqualTo(queryTimeout);
375+
376+
}
377+
338378
}

0 commit comments

Comments
 (0)