fix invalid time ranges in shards while loading
authorJeroen van der Heijden <jeroen@transceptor.technology>
Fri, 22 Feb 2019 16:27:35 +0000 (17:27 +0100)
committerJeroen van der Heijden <jeroen@transceptor.technology>
Fri, 22 Feb 2019 16:27:35 +0000 (17:27 +0100)
src/siri/db/series.c
src/siri/db/shard.c

index 3d6d12b86fc67690671459d77ff88c5c55ab4707..3dce1c73c4affed649a8e3a356b827021ece6001 100644 (file)
@@ -1033,7 +1033,6 @@ int siridb_series_optimize_shard(
     uint16_t cinfo = 0;
     uint64_t duration = (shard->tp == SIRIDB_SHARD_TP_NUMBER) ?
                 siridb->duration_num : siridb->duration_log;
-
     max_ts = (shard->id + duration) - series->mask;
 
     rc = new_idx = end = i = size = start = 0;
@@ -1113,6 +1112,7 @@ int siridb_series_optimize_shard(
         {
             pend = size;
         }
+
         if ((pos = siridb_shard_write_points(
                 siridb,
                 series,
index a88adcfa58e8215dc92cb50430aafe5c49b97b2e..fbb401a06a8f438a159f6e3aa944ea9844a27668 100644 (file)
@@ -1652,7 +1652,7 @@ static ssize_t SHARD_apply_idx(
                     shard->fn,
                     pos);
             shard->flags |= SIRIDB_SHARD_IS_CORRUPT;
-            return -1;
+            return size;
         }
 
         /* this shard has remove series, make sure the flag is set */
@@ -1671,15 +1671,37 @@ static ssize_t SHARD_apply_idx(
     }
     else
     {
+        uint64_t start_ts = is_ts64 ? /* START_TS IN HEADER  */
+                (uint64_t) *((uint64_t *) (pt + 4)) :
+                (uint64_t) *((uint32_t *) (pt + 4));
+        uint64_t end_ts = is_ts64 ? /* END_TS IN HEADER  */
+                (uint64_t) *((uint64_t *) (pt + 12)) :
+                (uint64_t) *((uint32_t *) (pt + 8));
+        uint64_t start = shard->id - series->mask;
+        uint64_t end = start + ((shard->tp == SIRIDB_SHARD_TP_NUMBER) ?
+                    siridb->duration_num : siridb->duration_log);
+
+        if (start_ts < start || end_ts >= end)
+        {
+            log_error(
+                    "Unexpected Time range for series ID %" PRIu32
+                    " is found in shard %" PRIu64 " (%s) at "
+                    "position %ld. This indicates that this shard is "
+                    "probably corrupt. The next optimize cycle will most "
+                    "likely fix this shard but you might loose some data.",
+                    series_id,
+                    shard->id,
+                    shard->fn,
+                    pos);
+            shard->flags |= SIRIDB_SHARD_IS_CORRUPT;
+            return size;
+        }
+
         if (siridb_series_add_idx(
                 series,
                 shard,
-                is_ts64 ? /* START_TS IN HEADER  */
-                        (uint64_t) *((uint64_t *) (pt + 4)) :
-                        (uint64_t) *((uint32_t *) (pt + 4)),
-                is_ts64 ? /* END_TS IN HEADER  */
-                        (uint64_t) *((uint64_t *) (pt + 12)) :
-                        (uint64_t) *((uint32_t *) (pt + 8)),
+                start_ts,
+                end_ts,
                 (uint32_t) pos,
                 len,
                 cinfo) == 0)