SilverNarcissus commented on a change in pull request #2405:
URL: https://github.com/apache/iotdb/pull/2405#discussion_r557176532



##########
File path: server/src/main/java/org/apache/iotdb/db/engine/StorageEngine.java
##########
@@ -319,35 +360,88 @@ public ServiceType getID() {
     return ServiceType.STORAGE_ENGINE_SERVICE;
   }
 
-  public StorageGroupProcessor getProcessor(PartialPath path) throws 
StorageEngineException {
+  /**
+   * This method is for sync, delete tsfile or sth like them, just get storage 
group directly by sg
+   * name
+   *
+   * @param path storage group path
+   * @return storage group processor
+   */
+  public StorageGroupProcessor getProcessorDirectly(PartialPath path)
+      throws StorageEngineException {
     PartialPath storageGroupPath;
     try {
       StorageGroupMNode storageGroupMNode = 
IoTDB.metaManager.getStorageGroupNodeByPath(path);
       storageGroupPath = storageGroupMNode.getPartialPath();
-      StorageGroupProcessor processor = processorMap.get(storageGroupPath);
-      if (processor == null) {
+      return getStorageGroupProcessorByPath(storageGroupPath, 
storageGroupMNode);
+    } catch (StorageGroupProcessorException | MetadataException e) {
+      throw new StorageEngineException(e);
+    }
+  }
+
+  /**
+   * This method is for insert and query or sth like them, this may get a 
virtual storage group
+   *
+   * @param path device path
+   * @return storage group processor
+   */
+  public StorageGroupProcessor getProcessor(PartialPath path) throws 
StorageEngineException {
+    try {
+      StorageGroupMNode storageGroupMNode = 
IoTDB.metaManager.getStorageGroupNodeByPath(path);
+      return getStorageGroupProcessorByPath(path, storageGroupMNode);
+    } catch (StorageGroupProcessorException | MetadataException e) {
+      throw new StorageEngineException(e);
+    }
+  }
+
+  /**
+   * get storage group processor by path
+   * @param storageGroupPath path of the storage group
+   * @param storageGroupMNode mnode of the storage group
+   * @return found or new storage group processor
+   */
+  @SuppressWarnings("java:S2445")
+  // actually storageGroupMNode is a unique object on the mtree, synchronize 
it is reasonable
+  private StorageGroupProcessor getStorageGroupProcessorByPath(PartialPath 
storageGroupPath,
+      StorageGroupMNode storageGroupMNode)
+      throws StorageGroupProcessorException, StorageEngineException {
+    VirtualStorageGroupManager virtualStorageGroupManager = processorMap
+        .get(storageGroupMNode.getPartialPath());
+    if (virtualStorageGroupManager == null) {
+      // if finish recover
+      if (isAllSgReady.get()) {
         waitAllSgReady(storageGroupPath);
-        // if finish recover
-        if (isAllSgReady.get()) {
-          synchronized (storageGroupMNode) {
-            processor = processorMap.get(storageGroupPath);
-            if (processor == null) {
-              logger.info("construct a processor instance, the storage group 
is {}, Thread is {}",
-                  storageGroupPath, Thread.currentThread().getId());
-              processor = new StorageGroupProcessor(systemDir, 
storageGroupPath.getFullPath(),
-                  fileFlushPolicy);
-              processor.setDataTTL(storageGroupMNode.getDataTTL());
-              processor.setCustomFlushListeners(customFlushListeners);
-              processor.setCustomCloseFileListeners(customCloseFileListeners);
-              processorMap.put(storageGroupPath, processor);
-            }
+        synchronized (storageGroupMNode) {
+          virtualStorageGroupManager = 
processorMap.get(storageGroupMNode.getPartialPath());
+          if (virtualStorageGroupManager == null) {
+            virtualStorageGroupManager = new VirtualStorageGroupManager();
+            processorMap.put(storageGroupMNode.getPartialPath(), 
virtualStorageGroupManager);
           }
         }
+      } else {
+        // not finished recover, refuse the request
+        throw new StorageEngineException(
+            "the sg " + storageGroupMNode.getPartialPath()
+                + " may not ready now, please wait and retry later",
+            TSStatusCode.STORAGE_GROUP_NOT_READY.getStatusCode());
       }
-      return processor;
-    } catch (StorageGroupProcessorException | MetadataException e) {
-      throw new StorageEngineException(e);
     }
+    return virtualStorageGroupManager.getProcessor(storageGroupPath, 
storageGroupMNode);
+  }
+
+  public StorageGroupProcessor buildNewStorageGroupProcessor(PartialPath 
storageGroupPath,
+      StorageGroupMNode storageGroupMNode, String storageGroupName)
+      throws StorageGroupProcessorException {
+    StorageGroupProcessor processor;
+    logger.info("construct a processor instance, the storage group is {}, 
Thread is {}",
+        storageGroupPath, Thread.currentThread().getId());
+    processor = new StorageGroupProcessor(systemDir + File.separator + 
storageGroupPath,
+        storageGroupName,
+        fileFlushPolicy, storageGroupMNode.getFullPath());
+    processor.setDataTTL(storageGroupMNode.getDataTTL());
+    processor.setCustomFlushListeners(customFlushListeners);
+    processor.setCustomCloseFileListeners(customCloseFileListeners);
+    return processor;
   }

Review comment:
       I have change the name of these parameters and add comments
   logical storage group name like: root.sg1
   virtual storage group id like: 1




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to