[influxdb] Handle exceptions gracefully (#15062)

* [influxdb] Handle exceptions gracefully

Signed-off-by: Jan N. Klug <github@klug.nrw>

* also catch InfluxDBIOExceptions

Signed-off-by: Jan N. Klug <github@klug.nrw>

---------

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2023-06-13 20:54:27 +02:00 committed by GitHub
parent d40c20b2dd
commit d2e10ab282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 20 deletions

View File

@ -30,6 +30,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.InfluxDBIOException;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Pong;
@ -130,7 +131,7 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
BatchPoints batchPoints = BatchPoints.database(configuration.getDatabaseName())
.retentionPolicy(configuration.getRetentionPolicy()).points(points).build();
currentClient.write(batchPoints);
} catch (InfluxException e) {
} catch (InfluxException | InfluxDBIOException e) {
logger.debug("Writing to database failed", e);
return false;
}
@ -165,15 +166,19 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
@Override
public List<InfluxRow> query(FilterCriteria filter, String retentionPolicy) {
final InfluxDB currentClient = client;
if (currentClient != null) {
String query = queryCreator.createQuery(filter, retentionPolicy);
logger.trace("Query {}", query);
Query parsedQuery = new Query(query, configuration.getDatabaseName());
List<QueryResult.Result> results = currentClient.query(parsedQuery, TimeUnit.MILLISECONDS).getResults();
return convertClientResultToRepository(results);
} else {
logger.warn("Returning empty list because queryAPI isn't present");
try {
final InfluxDB currentClient = client;
if (currentClient != null) {
String query = queryCreator.createQuery(filter, retentionPolicy);
logger.trace("Query {}", query);
Query parsedQuery = new Query(query, configuration.getDatabaseName());
List<QueryResult.Result> results = currentClient.query(parsedQuery, TimeUnit.MILLISECONDS).getResults();
return convertClientResultToRepository(results);
} else {
throw new InfluxException("API not present");
}
} catch (InfluxException | InfluxDBIOException e) {
logger.warn("Failed to execute query '{}': {}", filter, e.getMessage());
return List.of();
}
}

View File

@ -27,6 +27,7 @@ import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.influxdb.InfluxDBIOException;
import org.openhab.core.persistence.FilterCriteria;
import org.openhab.persistence.influxdb.internal.FilterCriteriaQueryCreator;
import org.openhab.persistence.influxdb.internal.InfluxDBConfiguration;
@ -138,7 +139,7 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
List<Point> clientPoints = influxPoints.stream().map(this::convertPointToClientFormat)
.filter(Optional::isPresent).map(Optional::get).toList();
currentWriteAPI.writePoints(clientPoints);
} catch (InfluxException e) {
} catch (InfluxException | InfluxDBIOException e) {
logger.debug("Writing to database failed", e);
return false;
}
@ -173,7 +174,7 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
try {
deleteAPI.delete(start, stop, predicate, configuration.getRetentionPolicy(),
configuration.getDatabaseName());
} catch (InfluxException e) {
} catch (InfluxException | InfluxDBIOException e) {
logger.debug("Deleting from database failed", e);
return false;
}
@ -203,14 +204,18 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
@Override
public List<InfluxRow> query(FilterCriteria filter, String retentionPolicy) {
final QueryApi currentQueryAPI = queryAPI;
if (currentQueryAPI != null) {
String query = queryCreator.createQuery(filter, retentionPolicy);
logger.trace("Query {}", query);
List<FluxTable> clientResult = currentQueryAPI.query(query);
return clientResult.stream().flatMap(this::mapRawResultToHistoric).toList();
} else {
logger.warn("Returning empty list because queryAPI isn't present");
try {
final QueryApi currentQueryAPI = queryAPI;
if (currentQueryAPI != null) {
String query = queryCreator.createQuery(filter, retentionPolicy);
logger.trace("Query {}", query);
List<FluxTable> clientResult = currentQueryAPI.query(query);
return clientResult.stream().flatMap(this::mapRawResultToHistoric).toList();
} else {
throw new InfluxException("API not present");
}
} catch (InfluxException | InfluxDBIOException e) {
logger.warn("Failed to execute query '{}': {}", filter, e.getMessage());
return List.of();
}
}