Skip to content

Commit

Permalink
[influxdb] Handle exceptions gracefully (openhab#15062)
Browse files Browse the repository at this point in the history
* [influxdb] Handle exceptions gracefully

Signed-off-by: Jan N. Klug <[email protected]>

* also catch InfluxDBIOExceptions

Signed-off-by: Jan N. Klug <[email protected]>

---------

Signed-off-by: Jan N. Klug <[email protected]>
Signed-off-by: Matt Myers <[email protected]>
  • Loading branch information
J-N-K authored and matchews committed Aug 9, 2023
1 parent bc9f1f0 commit 3e318cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -130,7 +131,7 @@ public boolean write(List<InfluxPoint> influxPoints) {
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;
}
Expand Down Expand Up @@ -165,15 +166,19 @@ private Optional<Point> convertPointToClientFormat(InfluxPoint point) {

@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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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;
Expand Down Expand Up @@ -138,7 +139,7 @@ public boolean write(List<InfluxPoint> influxPoints) {
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;
}
Expand Down Expand Up @@ -173,7 +174,7 @@ public boolean remove(FilterCriteria filter) {
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;
}
Expand Down Expand Up @@ -203,14 +204,18 @@ private Optional<Point> convertPointToClientFormat(InfluxPoint point) {

@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();
}
}
Expand Down

0 comments on commit 3e318cc

Please sign in to comment.