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

[influxdb] Handle exceptions gracefully #15062

Merged
merged 2 commits into from
Jun 13, 2023
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
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