You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2022/03/09 01:20:25 UTC

[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #5187: [IOTDB-2705] Separate CQ persistence from MLog

SteveYurongSu commented on a change in pull request #5187:
URL: https://github.com/apache/iotdb/pull/5187#discussion_r822206911



##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cq/CQLogWriter.java
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.engine.cq;
+
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.engine.fileSystem.SystemFileFactory;
+import org.apache.iotdb.db.qp.physical.sys.CreateContinuousQueryPlan;
+import org.apache.iotdb.db.qp.physical.sys.DropContinuousQueryPlan;
+import org.apache.iotdb.db.writelog.io.LogWriter;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+public class CQLogWriter implements AutoCloseable {
+
+  private static final String TOO_LARGE_RECORD_EXCEPTION =
+      "Current CQ management operation plan is too large to write into buffer, please increase cqlog_buffer_size.";
+
+  private final ByteBuffer logBuffer;
+  private final LogWriter logWriter;
+
+  public CQLogWriter(String logFilePath) throws IOException {
+    logBuffer = ByteBuffer.allocate(IoTDBDescriptor.getInstance().getConfig().getTlogBufferSize());
+    File logFile = SystemFileFactory.INSTANCE.getFile(logFilePath);

Review comment:
       logFile is not closed in this class.

##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cq/CQLogWriter.java
##########
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.db.engine.cq;
+
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.engine.fileSystem.SystemFileFactory;
+import org.apache.iotdb.db.qp.physical.sys.CreateContinuousQueryPlan;
+import org.apache.iotdb.db.qp.physical.sys.DropContinuousQueryPlan;
+import org.apache.iotdb.db.writelog.io.LogWriter;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+public class CQLogWriter implements AutoCloseable {
+
+  private static final String TOO_LARGE_RECORD_EXCEPTION =
+      "Current CQ management operation plan is too large to write into buffer, please increase cqlog_buffer_size.";
+
+  private final ByteBuffer logBuffer;
+  private final LogWriter logWriter;
+
+  public CQLogWriter(String logFilePath) throws IOException {
+    logBuffer = ByteBuffer.allocate(IoTDBDescriptor.getInstance().getConfig().getTlogBufferSize());
+    File logFile = SystemFileFactory.INSTANCE.getFile(logFilePath);
+    logWriter = new LogWriter(logFile, true);
+  }
+
+  public synchronized void createContinuousQuery(
+      CreateContinuousQueryPlan createContinuousQueryPlan) throws IOException {
+    try {
+      createContinuousQueryPlan.serialize(logBuffer);
+      logWriter.write(logBuffer);
+    } catch (BufferOverflowException e) {
+      throw new IOException(TOO_LARGE_RECORD_EXCEPTION, e);
+    } finally {
+      logBuffer.clear();
+    }
+  }
+
+  public synchronized void dropContinuousQuery(DropContinuousQueryPlan dropContinuousQueryPlan)
+      throws IOException {
+    try {
+      dropContinuousQueryPlan.serialize(logBuffer);
+      logWriter.write(logBuffer);
+    } catch (BufferOverflowException e) {
+      throw new IOException(TOO_LARGE_RECORD_EXCEPTION, e);
+    } finally {
+      logBuffer.clear();
+    }
+  }
+
+  @Override
+  public void close() throws IOException {
+    logWriter.close();

Review comment:
       logFile should be closed too.

##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cq/ContinuousQueryService.java
##########
@@ -113,15 +162,26 @@ private void checkAndSubmitTasks() {
 
   @Override
   public void stop() {
-    if (continuousQueryTaskSubmitThread != null) {
-      continuousQueryTaskSubmitThread.shutdown();
-      try {
-        continuousQueryTaskSubmitThread.awaitTermination(600, TimeUnit.MILLISECONDS);
-      } catch (InterruptedException e) {
-        LOGGER.warn("Check thread still doesn't exit after 60s");
-        continuousQueryTaskSubmitThread.shutdownNow();
-        Thread.currentThread().interrupt();
+    try {
+      if (continuousQueryTaskSubmitThread != null) {
+        continuousQueryTaskSubmitThread.shutdown();
+        try {
+          continuousQueryTaskSubmitThread.awaitTermination(600, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException e) {
+          LOGGER.warn("Check thread still doesn't exit after 60s");
+          continuousQueryTaskSubmitThread.shutdownNow();
+          Thread.currentThread().interrupt();
+        }
       }
+
+      continuousQueryPlans.clear();
+
+      if (logWriter != null) {
+        logWriter.close();
+        logWriter = null;
+      }
+    } catch (IOException ignored) {
+

Review comment:
       We'd better log the exception.

##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cq/ContinuousQueryService.java
##########
@@ -51,35 +55,80 @@
       ContinuousQueryTaskPoolManager.getInstance();
   private static final long TASK_SUBMIT_CHECK_INTERVAL =
       IoTDBDescriptor.getInstance().getConfig().getContinuousQueryMinimumEveryInterval() / 2;
+
+  private static final String LOG_FILE_DIR =
+      IoTDBDescriptor.getInstance().getConfig().getSystemDir() + File.separator;
+  private static final String LOG_FILE_NAME = LOG_FILE_DIR + "cqlog.bin";
+
   private ScheduledExecutorService continuousQueryTaskSubmitThread;
 
   private final ConcurrentHashMap<String, CreateContinuousQueryPlan> continuousQueryPlans =
       new ConcurrentHashMap<>();
   private final ConcurrentHashMap<String, Long> nextExecutionTimestamps = new ConcurrentHashMap<>();
 
+  private CQLogWriter logWriter;
+
+  public void doRecovery() throws StartupException {
+    try {
+      File logFile = SystemFileFactory.INSTANCE.getFile(LOG_FILE_NAME);
+      if (!logFile.exists()) {
+        return;

Review comment:
       Should we close the logFile here?

##########
File path: server/src/main/java/org/apache/iotdb/db/engine/cq/ContinuousQueryService.java
##########
@@ -51,35 +55,80 @@
       ContinuousQueryTaskPoolManager.getInstance();
   private static final long TASK_SUBMIT_CHECK_INTERVAL =
       IoTDBDescriptor.getInstance().getConfig().getContinuousQueryMinimumEveryInterval() / 2;
+
+  private static final String LOG_FILE_DIR =

Review comment:
       It seems that the dir data/system/cq/cqlog.bin is better.




-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org