[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:
parent
d40c20b2dd
commit
d2e10ab282
|
@ -30,6 +30,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.influxdb.InfluxDB;
|
import org.influxdb.InfluxDB;
|
||||||
import org.influxdb.InfluxDBFactory;
|
import org.influxdb.InfluxDBFactory;
|
||||||
|
import org.influxdb.InfluxDBIOException;
|
||||||
import org.influxdb.dto.BatchPoints;
|
import org.influxdb.dto.BatchPoints;
|
||||||
import org.influxdb.dto.Point;
|
import org.influxdb.dto.Point;
|
||||||
import org.influxdb.dto.Pong;
|
import org.influxdb.dto.Pong;
|
||||||
|
@ -130,7 +131,7 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
|
||||||
BatchPoints batchPoints = BatchPoints.database(configuration.getDatabaseName())
|
BatchPoints batchPoints = BatchPoints.database(configuration.getDatabaseName())
|
||||||
.retentionPolicy(configuration.getRetentionPolicy()).points(points).build();
|
.retentionPolicy(configuration.getRetentionPolicy()).points(points).build();
|
||||||
currentClient.write(batchPoints);
|
currentClient.write(batchPoints);
|
||||||
} catch (InfluxException e) {
|
} catch (InfluxException | InfluxDBIOException e) {
|
||||||
logger.debug("Writing to database failed", e);
|
logger.debug("Writing to database failed", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -165,15 +166,19 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<InfluxRow> query(FilterCriteria filter, String retentionPolicy) {
|
public List<InfluxRow> query(FilterCriteria filter, String retentionPolicy) {
|
||||||
final InfluxDB currentClient = client;
|
try {
|
||||||
if (currentClient != null) {
|
final InfluxDB currentClient = client;
|
||||||
String query = queryCreator.createQuery(filter, retentionPolicy);
|
if (currentClient != null) {
|
||||||
logger.trace("Query {}", query);
|
String query = queryCreator.createQuery(filter, retentionPolicy);
|
||||||
Query parsedQuery = new Query(query, configuration.getDatabaseName());
|
logger.trace("Query {}", query);
|
||||||
List<QueryResult.Result> results = currentClient.query(parsedQuery, TimeUnit.MILLISECONDS).getResults();
|
Query parsedQuery = new Query(query, configuration.getDatabaseName());
|
||||||
return convertClientResultToRepository(results);
|
List<QueryResult.Result> results = currentClient.query(parsedQuery, TimeUnit.MILLISECONDS).getResults();
|
||||||
} else {
|
return convertClientResultToRepository(results);
|
||||||
logger.warn("Returning empty list because queryAPI isn't present");
|
} else {
|
||||||
|
throw new InfluxException("API not present");
|
||||||
|
}
|
||||||
|
} catch (InfluxException | InfluxDBIOException e) {
|
||||||
|
logger.warn("Failed to execute query '{}': {}", filter, e.getMessage());
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.influxdb.InfluxDBIOException;
|
||||||
import org.openhab.core.persistence.FilterCriteria;
|
import org.openhab.core.persistence.FilterCriteria;
|
||||||
import org.openhab.persistence.influxdb.internal.FilterCriteriaQueryCreator;
|
import org.openhab.persistence.influxdb.internal.FilterCriteriaQueryCreator;
|
||||||
import org.openhab.persistence.influxdb.internal.InfluxDBConfiguration;
|
import org.openhab.persistence.influxdb.internal.InfluxDBConfiguration;
|
||||||
|
@ -138,7 +139,7 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
|
||||||
List<Point> clientPoints = influxPoints.stream().map(this::convertPointToClientFormat)
|
List<Point> clientPoints = influxPoints.stream().map(this::convertPointToClientFormat)
|
||||||
.filter(Optional::isPresent).map(Optional::get).toList();
|
.filter(Optional::isPresent).map(Optional::get).toList();
|
||||||
currentWriteAPI.writePoints(clientPoints);
|
currentWriteAPI.writePoints(clientPoints);
|
||||||
} catch (InfluxException e) {
|
} catch (InfluxException | InfluxDBIOException e) {
|
||||||
logger.debug("Writing to database failed", e);
|
logger.debug("Writing to database failed", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -173,7 +174,7 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
|
||||||
try {
|
try {
|
||||||
deleteAPI.delete(start, stop, predicate, configuration.getRetentionPolicy(),
|
deleteAPI.delete(start, stop, predicate, configuration.getRetentionPolicy(),
|
||||||
configuration.getDatabaseName());
|
configuration.getDatabaseName());
|
||||||
} catch (InfluxException e) {
|
} catch (InfluxException | InfluxDBIOException e) {
|
||||||
logger.debug("Deleting from database failed", e);
|
logger.debug("Deleting from database failed", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -203,14 +204,18 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<InfluxRow> query(FilterCriteria filter, String retentionPolicy) {
|
public List<InfluxRow> query(FilterCriteria filter, String retentionPolicy) {
|
||||||
final QueryApi currentQueryAPI = queryAPI;
|
try {
|
||||||
if (currentQueryAPI != null) {
|
final QueryApi currentQueryAPI = queryAPI;
|
||||||
String query = queryCreator.createQuery(filter, retentionPolicy);
|
if (currentQueryAPI != null) {
|
||||||
logger.trace("Query {}", query);
|
String query = queryCreator.createQuery(filter, retentionPolicy);
|
||||||
List<FluxTable> clientResult = currentQueryAPI.query(query);
|
logger.trace("Query {}", query);
|
||||||
return clientResult.stream().flatMap(this::mapRawResultToHistoric).toList();
|
List<FluxTable> clientResult = currentQueryAPI.query(query);
|
||||||
} else {
|
return clientResult.stream().flatMap(this::mapRawResultToHistoric).toList();
|
||||||
logger.warn("Returning empty list because queryAPI isn't present");
|
} else {
|
||||||
|
throw new InfluxException("API not present");
|
||||||
|
}
|
||||||
|
} catch (InfluxException | InfluxDBIOException e) {
|
||||||
|
logger.warn("Failed to execute query '{}': {}", filter, e.getMessage());
|
||||||
return List.of();
|
return List.of();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue