Skip to content

Commit e3418dd

Browse files
authored
feat: add PingService to check status of OSS and Cloud instance (#272)
1 parent e5ac3b3 commit e3418dd

19 files changed

Lines changed: 264 additions & 49 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
## 3.5.0 [unreleased]
22

3+
### Deprecates
4+
- `InfluxDBClient.health()`: instead use `InfluxDBClient.ping()`
5+
- `InfluxDBClientKotlin.health()`: instead use `InfluxDBClientKotlin.ping()`
6+
- `InfluxDBClientScala.health()`: instead use `InfluxDBClientScala.ping()`
7+
8+
### Features
9+
1. [#272](https://github.com/influxdata/influxdb-client-java/pull/272): Add `PingService` to check status of OSS and Cloud instance
10+
311
## 3.4.0 [2021-10-22]
412

513
### Features

client-core/src/main/java/com/influxdb/internal/AbstractRestClient.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import okhttp3.MediaType;
5151
import okhttp3.RequestBody;
52+
import okhttp3.ResponseBody;
5253
import okhttp3.logging.HttpLoggingInterceptor;
5354
import retrofit2.Call;
5455
import retrofit2.Response;
@@ -165,4 +166,32 @@ private boolean isCloseException(@Nonnull final Exception exception) {
165166

166167
return exception instanceof EOFException;
167168
}
169+
170+
@Nonnull
171+
protected Boolean ping(@Nonnull final Call<ResponseBody> responseBody) {
172+
173+
Arguments.checkNotNull(responseBody, "responseBody");
174+
175+
try {
176+
return responseBody.execute().isSuccessful();
177+
} catch (IOException e) {
178+
179+
LOG.log(Level.WARNING, "Ping request wasn't successful", e);
180+
return false;
181+
}
182+
}
183+
184+
@Nonnull
185+
protected String version(@Nonnull final Call<ResponseBody> ping) {
186+
try {
187+
String version = ping.execute().headers().get("X-Influxdb-Version");
188+
if (version != null) {
189+
return version;
190+
}
191+
192+
return "unknown";
193+
} catch (IOException e) {
194+
throw new InfluxException(e);
195+
}
196+
}
168197
}

client-kotlin/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ influxDBClient.setLogLevel(LogLevel.HEADERS)
251251

252252
### Check the server status
253253

254-
Server availability can be checked using the `influxDBClient.health()` endpoint.
254+
Server availability can be checked using the `influxDBClient.ping()` endpoint.
255255

256256
### Construct queries using the [flux-dsl](../flux-dsl) query builder
257257

client-kotlin/src/main/kotlin/com/influxdb/client/kotlin/InfluxDBClientKotlin.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package com.influxdb.client.kotlin
2424
import com.influxdb.LogLevel
2525
import com.influxdb.client.domain.HealthCheck
2626
import java.io.Closeable
27+
import javax.annotation.Nonnull
2728

2829
/**
2930
* The reference Kotlin client that allows query and write for the InfluxDB 2.0 by Kotlin Channel coroutines.
@@ -51,8 +52,25 @@ interface InfluxDBClientKotlin : Closeable {
5152
*
5253
* @return health of an instance
5354
*/
55+
@Deprecated("This method is obsolete. Use `ping()` or `version()`", ReplaceWith("ping()"))
5456
fun health(): HealthCheck
5557

58+
/**
59+
* Check the status of InfluxDB Server.
60+
*
61+
* @return `true` if server is healthy otherwise return `false`
62+
*/
63+
@Nonnull
64+
fun ping(): Boolean
65+
66+
/**
67+
* Returns the version of the connected InfluxDB Server.
68+
*
69+
* @return the version String, otherwise unknown.
70+
*/
71+
@Nonnull
72+
fun version(): String
73+
5674
/**
5775
* Gets the [LogLevel] that is used for logging requests and responses.
5876
*

client-kotlin/src/main/kotlin/com/influxdb/client/kotlin/internal/InfluxDBClientKotlinImpl.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ internal class InfluxDBClientKotlinImpl(options: InfluxDBClientOptions) : Abstra
4848
return health(healthService.getHealth(null))
4949
}
5050

51+
override fun ping(): Boolean {
52+
return ping(pingService.ping)
53+
}
54+
55+
override fun version(): String {
56+
return version(pingService.ping)
57+
}
58+
5159
override fun getLogLevel(): LogLevel {
5260
return getLogLevel(loggingInterceptor)
5361
}

client-kotlin/src/test/kotlin/com/influxdb/client/kotlin/ITInfluxDBClientKotlin.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package com.influxdb.client.kotlin
2323

2424
import com.influxdb.LogLevel
2525
import com.influxdb.client.domain.HealthCheck
26+
import com.influxdb.exceptions.InfluxException
2627
import org.assertj.core.api.Assertions
2728
import org.junit.jupiter.api.Test
2829
import org.junit.platform.runner.JUnitPlatform
@@ -66,6 +67,31 @@ internal class ITInfluxDBClientKotlin : AbstractITInfluxDBClientKotlin() {
6667
clientNotRunning.close()
6768
}
6869

70+
@Test
71+
fun ping() {
72+
Assertions.assertThat(influxDBClient.ping()).isTrue
73+
}
74+
75+
@Test
76+
fun pingNotRunningInstance() {
77+
val clientNotRunning = InfluxDBClientKotlinFactory.create("http://localhost:8099")
78+
Assertions.assertThat(clientNotRunning.ping()).isFalse
79+
clientNotRunning.close()
80+
}
81+
82+
@Test
83+
fun version() {
84+
Assertions.assertThat(influxDBClient.version()).isNotBlank
85+
}
86+
87+
@Test
88+
fun versionNotRunningInstance() {
89+
val clientNotRunning = InfluxDBClientKotlinFactory.create("http://localhost:8099")
90+
Assertions.assertThatThrownBy { clientNotRunning.version() }
91+
.isInstanceOf(InfluxException::class.java)
92+
clientNotRunning.close()
93+
}
94+
6995
@Test
7096
fun logLevel() {
7197

client-legacy/src/main/java/com/influxdb/client/flux/internal/FluxApiImpl.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@
2121
*/
2222
package com.influxdb.client.flux.internal;
2323

24-
import java.io.IOException;
2524
import java.util.ArrayList;
2625
import java.util.List;
2726
import java.util.function.BiConsumer;
2827
import java.util.function.Consumer;
29-
import java.util.logging.Level;
30-
import java.util.logging.Logger;
3128
import javax.annotation.Nonnull;
3229
import javax.annotation.Nullable;
3330

@@ -36,7 +33,6 @@
3633
import com.influxdb.LogLevel;
3734
import com.influxdb.client.flux.FluxClient;
3835
import com.influxdb.client.flux.FluxConnectionOptions;
39-
import com.influxdb.exceptions.InfluxException;
4036
import com.influxdb.internal.AbstractQueryApi;
4137
import com.influxdb.internal.UserAgentInterceptor;
4238
import com.influxdb.query.FluxRecord;
@@ -55,8 +51,6 @@
5551
*/
5652
public class FluxApiImpl extends AbstractQueryApi implements FluxClient {
5753

58-
private static final Logger LOG = Logger.getLogger(FluxApiImpl.class.getName());
59-
6054
private final FluxService fluxService;
6155

6256
private final HttpLoggingInterceptor loggingInterceptor;
@@ -353,33 +347,14 @@ public void queryRaw(@Nonnull final String query,
353347
@Override
354348
public Boolean ping() {
355349

356-
Call<ResponseBody> ping = fluxService.ping();
357-
358-
try {
359-
return ping.execute().isSuccessful();
360-
} catch (IOException e) {
361-
362-
LOG.log(Level.WARNING, "Ping request wasn't successful", e);
363-
return false;
364-
}
350+
return ping(fluxService.ping());
365351
}
366352

367353
@Override
368354
@Nonnull
369355
public String version() {
370356

371-
Call<ResponseBody> ping = fluxService.ping();
372-
373-
try {
374-
String version = ping.execute().headers().get("X-Influxdb-Version");
375-
if (version != null) {
376-
return version;
377-
}
378-
379-
return "unknown";
380-
} catch (IOException e) {
381-
throw new InfluxException(e);
382-
}
357+
return version(fluxService.ping());
383358
}
384359

385360
@Nonnull

client-osgi/src/test/java/com/influxdb/client/osgi/ITConnectionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ public class ITConnectionTest extends InfluxDBConnectorTest {
3737
void testConnectorWithToken() {
3838
super.testConnectorWithToken();
3939

40-
assertThat(client.getValue().health().getStatus(), equalTo(HealthCheck.StatusEnum.PASS));
40+
assertThat(client.getValue().ping(), equalTo(true));
4141
}
4242

4343
@Test
4444
@Override
4545
void testConnectorV1() {
4646
super.testConnectorV1();
4747

48-
assertThat(client.getValue().health().getStatus(), equalTo(HealthCheck.StatusEnum.PASS));
48+
assertThat(client.getValue().ping(), equalTo(true));
4949
}
5050

5151
@Test
5252
@Override
5353
void testConnectorWithUsernameAndPassword() {
5454
super.testConnectorWithUsernameAndPassword();
5555

56-
assertThat(client.getValue().health().getStatus(), equalTo(HealthCheck.StatusEnum.PASS));
56+
assertThat(client.getValue().ping(), equalTo(true));
5757
}
5858
}

client-scala/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ influxDBClient.setLogLevel(LogLevel.HEADERS)
191191

192192
### Check the server status
193193

194-
Server availability can be checked using the `influxDBClient.health()` endpoint.
194+
Server availability can be checked using the `influxDBClient.ping()` endpoint.
195195

196196
### Construct queries using the [flux-dsl](../flux-dsl) query builder
197197

client-scala/src/main/scala/com/influxdb/client/scala/InfluxDBClientScala.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,23 @@ trait InfluxDBClientScala {
4444
*
4545
* @return health of an instance
4646
*/
47+
@deprecated("This method is obsolete. Use `ping()` or `version()`")
4748
@Nonnull def health: HealthCheck
4849

50+
/**
51+
* Check the status of InfluxDB Server.
52+
*
53+
* @return true if server is healthy otherwise return false
54+
*/
55+
@Nonnull def ping: Boolean
56+
57+
/**
58+
* Returns the version of the connected InfluxDB Server.
59+
*
60+
* @return the version String, otherwise unknown.
61+
*/
62+
@Nonnull def version: String
63+
4964
/**
5065
* Gets the [[LogLevel]] that is used for logging requests and responses.
5166
*

0 commit comments

Comments
 (0)