Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add PingService to check status of OSS and Cloud instance #272

Merged
merged 2 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## 3.5.0 [unreleased]

### Deprecates
- `InfluxDBClient.health()`: instead use `InfluxDBClient.ping()`
- `InfluxDBClientKotlin.health()`: instead use `InfluxDBClientKotlin.ping()`
- `InfluxDBClientScala.health()`: instead use `InfluxDBClientScala.ping()`

### Features
1. [#272](https://github.com/influxdata/influxdb-client-java/pull/272): Add `PingService` to check status of OSS and Cloud instance

## 3.4.0 [2021-10-22]

### Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Response;
Expand Down Expand Up @@ -165,4 +166,32 @@ private boolean isCloseException(@Nonnull final Exception exception) {

return exception instanceof EOFException;
}

@Nonnull
protected Boolean ping(@Nonnull final Call<ResponseBody> responseBody) {

Arguments.checkNotNull(responseBody, "responseBody");

try {
return responseBody.execute().isSuccessful();
} catch (IOException e) {

LOG.log(Level.WARNING, "Ping request wasn't successful", e);
return false;
}
}

@Nonnull
protected String version(@Nonnull final Call<ResponseBody> ping) {
try {
String version = ping.execute().headers().get("X-Influxdb-Version");
if (version != null) {
return version;
}

return "unknown";
} catch (IOException e) {
throw new InfluxException(e);
}
}
}
2 changes: 1 addition & 1 deletion client-kotlin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ influxDBClient.setLogLevel(LogLevel.HEADERS)

### Check the server status

Server availability can be checked using the `influxDBClient.health()` endpoint.
Server availability can be checked using the `influxDBClient.ping()` endpoint.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package com.influxdb.client.kotlin
import com.influxdb.LogLevel
import com.influxdb.client.domain.HealthCheck
import java.io.Closeable
import javax.annotation.Nonnull

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

/**
* Check the status of InfluxDB Server.
*
* @return `true` if server is healthy otherwise return `false`
*/
@Nonnull
fun ping(): Boolean

/**
* Returns the version of the connected InfluxDB Server.
*
* @return the version String, otherwise unknown.
*/
@Nonnull
fun version(): String

/**
* Gets the [LogLevel] that is used for logging requests and responses.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ internal class InfluxDBClientKotlinImpl(options: InfluxDBClientOptions) : Abstra
return health(healthService.getHealth(null))
}

override fun ping(): Boolean {
return ping(pingService.ping)
}

override fun version(): String {
return version(pingService.ping)
}

override fun getLogLevel(): LogLevel {
return getLogLevel(loggingInterceptor)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package com.influxdb.client.kotlin

import com.influxdb.LogLevel
import com.influxdb.client.domain.HealthCheck
import com.influxdb.exceptions.InfluxException
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.platform.runner.JUnitPlatform
Expand Down Expand Up @@ -66,6 +67,31 @@ internal class ITInfluxDBClientKotlin : AbstractITInfluxDBClientKotlin() {
clientNotRunning.close()
}

@Test
fun ping() {
Assertions.assertThat(influxDBClient.ping()).isTrue
}

@Test
fun pingNotRunningInstance() {
val clientNotRunning = InfluxDBClientKotlinFactory.create("http://localhost:8099")
Assertions.assertThat(clientNotRunning.ping()).isFalse
clientNotRunning.close()
}

@Test
fun version() {
Assertions.assertThat(influxDBClient.version()).isNotBlank
}

@Test
fun versionNotRunningInstance() {
val clientNotRunning = InfluxDBClientKotlinFactory.create("http://localhost:8099")
Assertions.assertThatThrownBy { clientNotRunning.version() }
.isInstanceOf(InfluxException::class.java)
clientNotRunning.close()
}

@Test
fun logLevel() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,10 @@
*/
package com.influxdb.client.flux.internal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -36,7 +33,6 @@
import com.influxdb.LogLevel;
import com.influxdb.client.flux.FluxClient;
import com.influxdb.client.flux.FluxConnectionOptions;
import com.influxdb.exceptions.InfluxException;
import com.influxdb.internal.AbstractQueryApi;
import com.influxdb.internal.UserAgentInterceptor;
import com.influxdb.query.FluxRecord;
Expand All @@ -55,8 +51,6 @@
*/
public class FluxApiImpl extends AbstractQueryApi implements FluxClient {

private static final Logger LOG = Logger.getLogger(FluxApiImpl.class.getName());

private final FluxService fluxService;

private final HttpLoggingInterceptor loggingInterceptor;
Expand Down Expand Up @@ -353,33 +347,14 @@ public void queryRaw(@Nonnull final String query,
@Override
public Boolean ping() {

Call<ResponseBody> ping = fluxService.ping();

try {
return ping.execute().isSuccessful();
} catch (IOException e) {

LOG.log(Level.WARNING, "Ping request wasn't successful", e);
return false;
}
return ping(fluxService.ping());
}

@Override
@Nonnull
public String version() {

Call<ResponseBody> ping = fluxService.ping();

try {
String version = ping.execute().headers().get("X-Influxdb-Version");
if (version != null) {
return version;
}

return "unknown";
} catch (IOException e) {
throw new InfluxException(e);
}
return version(fluxService.ping());
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ public class ITConnectionTest extends InfluxDBConnectorTest {
void testConnectorWithToken() {
super.testConnectorWithToken();

assertThat(client.getValue().health().getStatus(), equalTo(HealthCheck.StatusEnum.PASS));
assertThat(client.getValue().ping(), equalTo(true));
}

@Test
@Override
void testConnectorV1() {
super.testConnectorV1();

assertThat(client.getValue().health().getStatus(), equalTo(HealthCheck.StatusEnum.PASS));
assertThat(client.getValue().ping(), equalTo(true));
}

@Test
@Override
void testConnectorWithUsernameAndPassword() {
super.testConnectorWithUsernameAndPassword();

assertThat(client.getValue().health().getStatus(), equalTo(HealthCheck.StatusEnum.PASS));
assertThat(client.getValue().ping(), equalTo(true));
}
}
2 changes: 1 addition & 1 deletion client-scala/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ influxDBClient.setLogLevel(LogLevel.HEADERS)

### Check the server status

Server availability can be checked using the `influxDBClient.health()` endpoint.
Server availability can be checked using the `influxDBClient.ping()` endpoint.

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,23 @@ trait InfluxDBClientScala {
*
* @return health of an instance
*/
@deprecated("This method is obsolete. Use `ping()` or `version()`")
@Nonnull def health: HealthCheck

/**
* Check the status of InfluxDB Server.
*
* @return true if server is healthy otherwise return false
*/
@Nonnull def ping: Boolean

/**
* Returns the version of the connected InfluxDB Server.
*
* @return the version String, otherwise unknown.
*/
@Nonnull def version: String

/**
* Gets the [[LogLevel]] that is used for logging requests and responses.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ class InfluxDBClientScalaImpl(@Nonnull options: InfluxDBClientOptions) extends A
*/
override def health: HealthCheck = health(healthService.getHealth(null))

/**
* Check the status of InfluxDB Server.
*
* @return true if server is healthy otherwise return false
*/
override def ping: Boolean = ping(pingService.getPing)

/**
* Returns the version of the connected InfluxDB Server.
*
* @return the version String, otherwise unknown.
*/
override def version: String = version(pingService.getPing)

/**
* Gets the [[LogLevel]] that is used for logging requests and responses.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ class ITInfluxDBClientScala extends AbstractITQueryScalaApi with Matchers {
client.close()
}

test("ping") {

val ping = influxDBClient.ping

ping should be(true)
}

test("ping not running") {
val clientNotRunning = InfluxDBClientScalaFactory.create("http://localhost:8099")

val ping = clientNotRunning.ping
ping should be(false)

clientNotRunning.close()
}

test("version") {
val version = influxDBClient.version

version should not be empty
}

test("version not running") {
val clientNotRunning = InfluxDBClientScalaFactory.create("http://localhost:8099")

assertThrows[com.influxdb.exceptions.InfluxException] { // Result type: Assertion
clientNotRunning.version
}

clientNotRunning.close()
}

test("log level") {

influxDBClient.getLogLevel should be(LogLevel.NONE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.influxdb.client.service;

import retrofit2.Call;
import retrofit2.http.*;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.MultipartBody;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface PingService {
/**
* Checks the status of InfluxDB instance and version of InfluxDB.
*
* @return Call&lt;ResponseBody&gt;
*/
@GET("ping")
Call<ResponseBody> getPing();


/**
* Checks the status of InfluxDB instance and version of InfluxDB.
*
* @return Call&lt;ResponseBody&gt;
*/
@HEAD("ping")
Call<ResponseBody> headPing();


}
Loading