You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2022/01/07 01:07:36 UTC

[iotdb] branch master updated: fix tag recovery bug after tag upsert (#4718)

This is an automated email from the ASF dual-hosted git repository.

jackietien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new f7affbb  fix tag recovery bug after tag upsert (#4718)
f7affbb is described below

commit f7affbb3bfd1f04f5dc93d847895e06df46c5703
Author: Marcos_Zyk <38...@users.noreply.github.com>
AuthorDate: Fri Jan 7 09:07:03 2022 +0800

    fix tag recovery bug after tag upsert (#4718)
---
 .../org/apache/iotdb/db/metadata/MManager.java     | 14 +++--
 .../apache/iotdb/db/metadata/tag/TagManager.java   |  7 +++
 .../iotdb/db/metadata/MManagerBasicTest.java       | 62 ++++++++++++++++++++++
 3 files changed, 75 insertions(+), 8 deletions(-)

diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/MManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/MManager.java
index c763d2f..a256198 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/MManager.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/MManager.java
@@ -118,7 +118,6 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -548,14 +547,13 @@ public class MManager {
       mNodeCache.invalidate(path.getDevicePath());
 
       // update tag index
-      if (plan.getTags() != null) {
+
+      if (offset != -1) {
+        // offset != -1 means the timeseries has already been created and now system is recovering
+        tagManager.recoverIndex(offset, leafMNode);
+      } else if (plan.getTags() != null) {
         // tag key, tag value
-        for (Entry<String, String> entry : plan.getTags().entrySet()) {
-          if (entry.getKey() == null || entry.getValue() == null) {
-            continue;
-          }
-          tagManager.addIndex(entry.getKey(), entry.getValue(), leafMNode);
-        }
+        tagManager.addIndex(plan.getTags(), leafMNode);
       }
 
       // update statistics and schemaDataTypeNumMap
diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/tag/TagManager.java b/server/src/main/java/org/apache/iotdb/db/metadata/tag/TagManager.java
index 2ef99b1..1af1f60 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/tag/TagManager.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/tag/TagManager.java
@@ -85,7 +85,14 @@ public class TagManager {
     tagLogFile = new TagLogFile(config.getSchemaDir(), MetadataConstant.TAG_LOG);
   }
 
+  public void recoverIndex(long offset, IMeasurementMNode measurementMNode) throws IOException {
+    addIndex(tagLogFile.readTag(config.getTagAttributeTotalSize(), offset), measurementMNode);
+  }
+
   public void addIndex(String tagKey, String tagValue, IMeasurementMNode measurementMNode) {
+    if (tagKey == null || tagValue == null || measurementMNode == null) {
+      return;
+    }
     tagIndex
         .computeIfAbsent(tagKey, k -> new ConcurrentHashMap<>())
         .computeIfAbsent(tagValue, v -> Collections.synchronizedSet(new HashSet<>()))
diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java
index d25d0dd..70e1716 100644
--- a/server/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/metadata/MManagerBasicTest.java
@@ -36,6 +36,7 @@ import org.apache.iotdb.db.qp.physical.sys.CreateTimeSeriesPlan;
 import org.apache.iotdb.db.qp.physical.sys.SetTemplatePlan;
 import org.apache.iotdb.db.qp.physical.sys.ShowTimeSeriesPlan;
 import org.apache.iotdb.db.qp.physical.sys.UnsetTemplatePlan;
+import org.apache.iotdb.db.query.context.QueryContext;
 import org.apache.iotdb.db.query.dataset.ShowTimeSeriesResult;
 import org.apache.iotdb.db.service.IoTDB;
 import org.apache.iotdb.db.utils.EnvironmentUtils;
@@ -56,8 +57,10 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.stream.Collectors;
@@ -2198,4 +2201,63 @@ public class MManagerBasicTest {
     manager.deleteTimeseries(new PartialPath("root.sg.d2.s2"));
     assertFalse(manager.isPathExist(new PartialPath("root.sg.d2")));
   }
+
+  @Test
+  public void testTagIndexRecovery() throws Exception {
+    MManager manager = IoTDB.metaManager;
+    PartialPath path = new PartialPath("root.sg.d.s");
+    Map<String, String> tags = new HashMap<>();
+    tags.put("description", "oldValue");
+    manager.createTimeseries(
+        new CreateTimeSeriesPlan(
+            path,
+            TSDataType.valueOf("INT32"),
+            TSEncoding.valueOf("RLE"),
+            compressionType,
+            null,
+            tags,
+            null,
+            null));
+
+    ShowTimeSeriesPlan showTimeSeriesPlan =
+        new ShowTimeSeriesPlan(
+            new PartialPath("root.sg.d.s"), true, "description", "Value", 0, 0, false);
+    List<ShowTimeSeriesResult> results =
+        manager.showTimeseries(showTimeSeriesPlan, new QueryContext());
+
+    assertEquals(1, results.size());
+    Map<String, String> resultTag = results.get(0).getTag();
+    assertEquals("oldValue", resultTag.get("description"));
+
+    tags.put("description", "newValue");
+    manager.upsertTagsAndAttributes(null, tags, null, path);
+
+    showTimeSeriesPlan =
+        new ShowTimeSeriesPlan(
+            new PartialPath("root.sg.d.s"), true, "description", "Value", 0, 0, false);
+    results = manager.showTimeseries(showTimeSeriesPlan, new QueryContext());
+
+    assertEquals(1, results.size());
+    resultTag = results.get(0).getTag();
+    assertEquals("newValue", resultTag.get("description"));
+
+    manager.clear();
+    manager.init();
+
+    showTimeSeriesPlan =
+        new ShowTimeSeriesPlan(
+            new PartialPath("root.sg.d.s"), true, "description", "oldValue", 0, 0, false);
+    results = manager.showTimeseries(showTimeSeriesPlan, new QueryContext());
+
+    assertEquals(0, results.size());
+
+    showTimeSeriesPlan =
+        new ShowTimeSeriesPlan(
+            new PartialPath("root.sg.d.s"), true, "description", "Value", 0, 0, false);
+    results = manager.showTimeseries(showTimeSeriesPlan, new QueryContext());
+
+    assertEquals(1, results.size());
+    resultTag = results.get(0).getTag();
+    assertEquals("newValue", resultTag.get("description"));
+  }
 }