]> git.basschouten.com Git - openhab-addons.git/commitdiff
[rrd4j] fixed oom when requesting data and boundary=true (#14292)
authorBoris Krivonog <boris.krivonog@inova.si>
Wed, 1 Feb 2023 20:06:27 +0000 (21:06 +0100)
committerGitHub <noreply@github.com>
Wed, 1 Feb 2023 20:06:27 +0000 (21:06 +0100)
* Fixed end date when requesting data using rrd4j and boundary=true

Signed-off-by: Boris Krivonog <boris.krivonog@inova.si>
bundles/org.openhab.persistence.rrd4j/src/main/java/org/openhab/persistence/rrd4j/internal/RRD4jPersistenceService.java

index 4359dc8b6a7532e1afca1c125b01b34f89a2be32..1cc6eb9c185660c06d0e2880781f59376aa965a4 100644 (file)
@@ -252,6 +252,12 @@ public class RRD4jPersistenceService implements QueryablePersistenceService {
 
     @Override
     public Iterable<HistoricItem> query(FilterCriteria filter) {
+        ZonedDateTime filterBeginDate = filter.getBeginDate();
+        ZonedDateTime filterEndDate = filter.getEndDate();
+        if (filterBeginDate != null && filterEndDate != null && filterBeginDate.isAfter(filterEndDate)) {
+            throw new IllegalArgumentException("begin (" + filterBeginDate + ") before end (" + filterEndDate + ")");
+        }
+
         String itemName = filter.getItemName();
 
         RrdDb db = null;
@@ -280,14 +286,11 @@ public class RRD4jPersistenceService implements QueryablePersistenceService {
         }
 
         long start = 0L;
-        // set end to {@link Instant#MAX} instead of current timestamp to enable requesting future time ranges including
-        // boundary values via REST API
-        // see discussion in https://github.com/openhab/openhab-addons/pull/14238
-        long end = filter.getEndDate() == null ? Instant.MAX.getEpochSecond()
-                : filter.getEndDate().toInstant().getEpochSecond();
+        long end = filterEndDate == null ? System.currentTimeMillis() / 1000
+                : filterEndDate.toInstant().getEpochSecond();
 
         try {
-            if (filter.getBeginDate() == null) {
+            if (filterBeginDate == null) {
                 // as rrd goes back for years and gets more and more
                 // inaccurate, we only support descending order
                 // and a single return value
@@ -296,7 +299,7 @@ public class RRD4jPersistenceService implements QueryablePersistenceService {
                 // query, which we want to support
                 if (filter.getOrdering() == Ordering.DESCENDING && filter.getPageSize() == 1
                         && filter.getPageNumber() == 0) {
-                    if (filter.getEndDate() == null) {
+                    if (filterEndDate == null) {
                         // we are asked only for the most recent value!
                         double lastValue = db.getLastDatasourceValue(DATASOURCE_STATE);
                         if (!Double.isNaN(lastValue)) {
@@ -315,13 +318,13 @@ public class RRD4jPersistenceService implements QueryablePersistenceService {
                             "rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested");
                 }
             } else {
-                start = filter.getBeginDate().toInstant().getEpochSecond();
+                start = filterBeginDate.toInstant().getEpochSecond();
             }
 
             // do not call method {@link RrdDb#createFetchRequest(ConsolFun, long, long, long)} if start > end to avoid
             // an IAE to be thrown
             if (start > end) {
-                logger.warn("Could not query rrd4j database for item '{}': start ({}) > end ({})", itemName, start,
+                logger.debug("Could not query rrd4j database for item '{}': start ({}) > end ({})", itemName, start,
                         end);
                 return List.of();
             }