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/21 12:12:55 UTC

[GitHub] [iotdb] CRZbulabula commented on a change in pull request #5274: [To rel/0.13] DoubleWrite for 0.13 version

CRZbulabula commented on a change in pull request #5274:
URL: https://github.com/apache/iotdb/pull/5274#discussion_r831036802



##########
File path: server/src/main/java/org/apache/iotdb/db/doublewrite/DoubleWriteProtectorService.java
##########
@@ -0,0 +1,221 @@
+/*
+ * 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.doublewrite;
+
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.engine.fileSystem.SystemFileFactory;
+import org.apache.iotdb.db.writelog.io.LogWriter;
+import org.apache.iotdb.session.pool.SessionPool;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Random;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/** DoubleWriteProtectorService manages the creation of log files and the scheduling of protector */
+public class DoubleWriteProtectorService implements Runnable {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DoubleWriteProtectorService.class);
+
+  private final Lock firstLogLock;
+  private final Condition firstLogCondition;
+  private final Lock logWriterLock;
+
+  private static final String logFileDir = "data" + File.separator + "doublewrite";
+  private static final String logFileName = "DBLog";
+  private int logFileID;
+  private File logFile;
+  private LogWriter logWriter;
+  private final int maxLogFileSize;
+  private final int maxLogFileCount;
+  private final int maxWaitingTime;
+
+  private final SessionPool doubleWriteSessionPool;
+  private final ExecutorService protectorThreadPool;
+
+  public DoubleWriteProtectorService(
+      Lock firstLogLock, Condition firstLogCondition, SessionPool doubleWriteSessionPool) {
+    this.firstLogLock = firstLogLock;
+    this.firstLogCondition = firstLogCondition;
+    this.logFileID = 0;
+    this.doubleWriteSessionPool = doubleWriteSessionPool;
+    this.logWriterLock = new ReentrantLock();
+
+    IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+    maxLogFileSize = config.getDoubleWriteMaxLogSize();
+    maxLogFileCount = config.getDoubleWriteProtectorMaxPoolSize();
+    maxWaitingTime = config.getDoubleWriteProtectorMaxWaitingTime() * 60 * 1000;
+    protectorThreadPool =
+        new ThreadPoolExecutor(
+            config.getDoubleWriteProtectorCorePoolSize(),
+            config.getDoubleWriteProtectorMaxPoolSize(),
+            config.getDoubleWriteProtectorKeepAliveTime(),
+            TimeUnit.HOURS,
+            new ArrayBlockingQueue<>(config.getDoubleWriteProtectorMaxPoolSize()),
+            Executors.defaultThreadFactory(),
+            new ThreadPoolExecutor.DiscardOldestPolicy());
+  }
+
+  @Override
+  public void run() {
+    // re-transmit remain data when recover
+    for (int ID = 0; ID < maxLogFileCount; ID++) {
+      File file =
+          SystemFileFactory.INSTANCE.getFile(logFileDir + File.separator + logFileName + ID);
+      if (file.exists()) {
+
+        protectorThreadPool.execute(
+            new DoubleWriteProtector(
+                SystemFileFactory.INSTANCE.getFile(logFileDir + File.separator + logFileName + ID),
+                doubleWriteSessionPool));
+      }
+    }
+
+    firstLogLock.lock();
+    // create first DoubleWriteLog
+    logWriterLock.lock();
+    createNewLogFile();
+    firstLogCondition.signal();
+    firstLogLock.unlock();

Review comment:
       This unlock statement is only for thread-safely create the firset DoubleWriteLog.

##########
File path: server/src/main/java/org/apache/iotdb/db/service/thrift/impl/TSServiceImpl.java
##########
@@ -302,9 +320,92 @@ public TSFetchResultsResp call() throws Exception {
 
   protected final ServiceProvider serviceProvider;
 
+  /* Double write module, temporary use */
+  // DOUBLEWRITE GENERAL
+  private final boolean isEnableDoubleWrite;
+  private final boolean isSyncDoubleWrite;
+  private final SessionPool doubleWriteSessionPool;
+  private final DoubleWriteProtectorService doubleWriteProtectorService;
+
+  // DOUBLEWRITE SYNC
+  private final ExecutorService doubleWriteTaskThreadPool;
+
+  // DOUBLEWRITE ASYNC
+  private final DoubleWriteProducer doubleWriteProducer;
+
   public TSServiceImpl() {
     super();
     serviceProvider = IoTDB.serviceProvider;
+
+    IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+    if (config.isEnableDoubleWrite()) {
+      /* Open double write */
+
+      // basic config
+      isEnableDoubleWrite = config.isEnableDoubleWrite();
+      isSyncDoubleWrite = config.isSyncDoubleWrite();
+
+      // create SessionPool for double write
+      doubleWriteSessionPool =
+          new SessionPool(
+              config.getSecondaryAddress(),
+              config.getSecondaryPort(),
+              config.getSecondaryUser(),
+              config.getSecondaryPassword(),
+              5);
+
+      // create DoubleWriteProtectorService
+      Lock firstLogLock = new ReentrantLock();
+      Condition firstLogCondition = firstLogLock.newCondition();
+      doubleWriteProtectorService =
+          new DoubleWriteProtectorService(firstLogLock, firstLogCondition, doubleWriteSessionPool);
+      new Thread(doubleWriteProtectorService).start();
+      try {
+        firstLogLock.lock();
+        firstLogCondition.await();
+        firstLogLock.unlock();

Review comment:
       This unlock statement is only for thread-safely create the firset DoubleWriteLog.




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