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/19 12:15:02 UTC

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

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



##########
File path: server/src/main/java/org/apache/iotdb/db/doublewrite/DoubleWriteConsumer.java
##########
@@ -0,0 +1,82 @@
+/*
+ * 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.rpc.IoTDBConnectionException;
+import org.apache.iotdb.session.pool.SessionPool;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+public class DoubleWriteConsumer implements Runnable {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DoubleWriteConsumer.class);
+
+  private final BlockingQueue<ByteBuffer> doubleWriteQueue;
+  private final SessionPool doubleWriteSessionPool;
+
+  public DoubleWriteConsumer(
+      BlockingQueue<ByteBuffer> doubleWriteQueue, SessionPool doubleWriteSessionPool) {
+    this.doubleWriteQueue = doubleWriteQueue;
+    this.doubleWriteSessionPool = doubleWriteSessionPool;
+  }
+
+  @Override
+  public void run() {
+    while (true) {
+      ByteBuffer head;
+      try {
+        head = doubleWriteQueue.take();
+      } catch (InterruptedException e) {
+        LOGGER.error("There is an exception in DoubleWriteConsumer: ", e);
+        continue;
+      }
+
+      // transmit PhysicalPlan until it has been received
+      while (true) {
+        boolean transmitStatus = false;
+
+        try {
+          head.position(0);
+          transmitStatus = doubleWriteSessionPool.doubleWriteTransmit(head);
+        } catch (IoTDBConnectionException connectionException) {
+          // warn IoTDBConnectionException and retry
+          LOGGER.warn("DoubleWriteProtector can't transmit, retrying...", connectionException);
+        } catch (Exception e) {
+          // error exception and break
+          LOGGER.error("DoubleWriteProtector can't transmit", e);
+          break;
+        }
+
+        if (transmitStatus) {
+          break;
+        } else {
+          try {
+            TimeUnit.SECONDS.sleep(1);
+          } catch (InterruptedException ignore) {

Review comment:
       Interrupted exception should be logged. It's means other thread want to interrupt this thread rather than timer is ring.

##########
File path: server/src/main/java/org/apache/iotdb/db/doublewrite/DoubleWriteProtector.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.qp.physical.PhysicalPlan;
+import org.apache.iotdb.db.writelog.io.SingleFileLogReader;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.session.pool.SessionPool;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.concurrent.TimeUnit;
+
+/** DoubleWriteProtector is used for transmit data in a DoubleWriteLog */
+public class DoubleWriteProtector implements Runnable {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(DoubleWriteProtector.class);
+
+  private final SessionPool doubleWriteSessionPool;
+
+  private final File logFile;
+  private SingleFileLogReader doubleWriteLogReader;
+
+  private static final int MAX_PHYSICALPLAN_SIZE = 16 * 1024 * 1024;
+  private final ByteArrayOutputStream doubleWriteByteStream;
+  private final DataOutputStream doubleWriteSerializeStream;
+
+  DoubleWriteProtector(File logFile, SessionPool doubleWriteSessionPool) {
+    this.doubleWriteSessionPool = doubleWriteSessionPool;
+    this.logFile = logFile;
+
+    // For serialize PhysicalPlan
+    doubleWriteByteStream = new ByteArrayOutputStream(MAX_PHYSICALPLAN_SIZE);
+    doubleWriteSerializeStream = new DataOutputStream(doubleWriteByteStream);
+
+    // init DoubleWriteLog reader
+    try {
+      doubleWriteLogReader = new SingleFileLogReader(logFile);
+    } catch (FileNotFoundException ignore) {
+      doubleWriteLogReader = null;
+    }
+  }
+
+  @Override
+  public void run() {
+    if (doubleWriteLogReader == null) {
+      return;
+    }
+
+    while (doubleWriteLogReader.hasNext()) {
+      // read and re-serialize the PhysicalPlan
+      PhysicalPlan nextPlan = doubleWriteLogReader.next();
+      try {
+        nextPlan.serialize(doubleWriteSerializeStream);
+      } catch (IOException e) {
+        LOGGER.error("DoubleWriteProtector can't serialize PhysicalPlan", e);
+        continue;
+      }
+      ByteBuffer nextBuffer = ByteBuffer.wrap(doubleWriteByteStream.toByteArray());
+      doubleWriteByteStream.reset();
+
+      while (true) {
+        // transmit DoubleWriteRequest until it's been received
+        boolean transmitStatus = false;
+
+        try {
+          // try double write
+          nextBuffer.position(0);
+          transmitStatus = doubleWriteSessionPool.doubleWriteTransmit(nextBuffer);
+        } catch (IoTDBConnectionException connectionException) {
+          // warn IoTDBConnectionException and retry
+          LOGGER.warn("DoubleWriteProtector can't transmit, retrying...", connectionException);
+        } catch (Exception e) {
+          // error exception and break
+          LOGGER.error("DoubleWriteProtector can't transmit", e);
+          break;
+        }
+
+        if (transmitStatus) {
+          break;
+        } else {
+          try {
+            TimeUnit.SECONDS.sleep(1);
+          } catch (InterruptedException ignore) {
+            // ignore
+          }
+        }
+      }
+    }
+
+    doubleWriteLogReader.close();
+    try {
+      // sleep one second then delete DoubleWriteLog file
+      TimeUnit.SECONDS.sleep(1);
+    } catch (InterruptedException ignore) {
+      // ignore

Review comment:
       Interrupted exception should be logged. It's means other thread want to interrupt this thread rather than timer is ring.

##########
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:
       unlock should be in finally block.

##########
File path: server/src/main/java/org/apache/iotdb/db/doublewrite/DoubleWriteProtector.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.qp.physical.PhysicalPlan;
+import org.apache.iotdb.db.writelog.io.SingleFileLogReader;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.session.pool.SessionPool;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.concurrent.TimeUnit;
+
+/** DoubleWriteProtector is used for transmit data in a DoubleWriteLog */
+public class DoubleWriteProtector implements Runnable {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(DoubleWriteProtector.class);
+
+  private final SessionPool doubleWriteSessionPool;
+
+  private final File logFile;
+  private SingleFileLogReader doubleWriteLogReader;
+
+  private static final int MAX_PHYSICALPLAN_SIZE = 16 * 1024 * 1024;
+  private final ByteArrayOutputStream doubleWriteByteStream;
+  private final DataOutputStream doubleWriteSerializeStream;
+
+  DoubleWriteProtector(File logFile, SessionPool doubleWriteSessionPool) {
+    this.doubleWriteSessionPool = doubleWriteSessionPool;
+    this.logFile = logFile;
+
+    // For serialize PhysicalPlan
+    doubleWriteByteStream = new ByteArrayOutputStream(MAX_PHYSICALPLAN_SIZE);
+    doubleWriteSerializeStream = new DataOutputStream(doubleWriteByteStream);
+
+    // init DoubleWriteLog reader
+    try {
+      doubleWriteLogReader = new SingleFileLogReader(logFile);
+    } catch (FileNotFoundException ignore) {
+      doubleWriteLogReader = null;
+    }
+  }
+
+  @Override
+  public void run() {
+    if (doubleWriteLogReader == null) {
+      return;
+    }
+
+    while (doubleWriteLogReader.hasNext()) {
+      // read and re-serialize the PhysicalPlan
+      PhysicalPlan nextPlan = doubleWriteLogReader.next();
+      try {
+        nextPlan.serialize(doubleWriteSerializeStream);
+      } catch (IOException e) {
+        LOGGER.error("DoubleWriteProtector can't serialize PhysicalPlan", e);
+        continue;
+      }
+      ByteBuffer nextBuffer = ByteBuffer.wrap(doubleWriteByteStream.toByteArray());
+      doubleWriteByteStream.reset();
+
+      while (true) {
+        // transmit DoubleWriteRequest until it's been received
+        boolean transmitStatus = false;
+
+        try {
+          // try double write
+          nextBuffer.position(0);
+          transmitStatus = doubleWriteSessionPool.doubleWriteTransmit(nextBuffer);
+        } catch (IoTDBConnectionException connectionException) {
+          // warn IoTDBConnectionException and retry
+          LOGGER.warn("DoubleWriteProtector can't transmit, retrying...", connectionException);
+        } catch (Exception e) {
+          // error exception and break
+          LOGGER.error("DoubleWriteProtector can't transmit", e);
+          break;
+        }
+
+        if (transmitStatus) {
+          break;
+        } else {
+          try {
+            TimeUnit.SECONDS.sleep(1);
+          } catch (InterruptedException ignore) {
+            // ignore

Review comment:
       Interrupted exception should be logged. It's means other thread want to interrupt this thread rather than timer is ring.

##########
File path: server/src/main/java/org/apache/iotdb/db/doublewrite/DoubleWriteProducer.java
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.IoTDBDescriptor;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * DoubleWriteProducer using BlockingQueue to cache PhysicalPlan. And persist some PhysicalPlan when
+ * they are too many to transmit
+ */
+public class DoubleWriteProducer {
+  private static final Logger LOGGER = LoggerFactory.getLogger(DoubleWriteProducer.class);
+
+  private final BlockingQueue<ByteBuffer> doubleWriteQueue;
+  private final int doubleWriteCacheSize;
+  private final DoubleWriteProtectorService service;
+
+  public DoubleWriteProducer(
+      BlockingQueue<ByteBuffer> doubleWriteQueue, DoubleWriteProtectorService service) {
+    this.doubleWriteQueue = doubleWriteQueue;
+    this.service = service;
+    doubleWriteCacheSize =
+        IoTDBDescriptor.getInstance().getConfig().getDoubleWriteProducerCacheSize();
+  }
+
+  public void put(ByteBuffer planBuffer) {
+    // It's better to go through producer-consumer module
+    if (doubleWriteQueue.size() == doubleWriteCacheSize) {
+      try {
+        TimeUnit.SECONDS.sleep(1);
+      } catch (InterruptedException ignore) {
+        // ignore

Review comment:
       Interrupted exception should be logged. It's means other thread want to interrupt this thread rather than timer is ring.

##########
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:
       unlock should be in finally block

##########
File path: server/src/main/java/org/apache/iotdb/db/doublewrite/DoubleWriteProtector.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.qp.physical.PhysicalPlan;
+import org.apache.iotdb.db.writelog.io.SingleFileLogReader;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.session.pool.SessionPool;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.concurrent.TimeUnit;
+
+/** DoubleWriteProtector is used for transmit data in a DoubleWriteLog */
+public class DoubleWriteProtector implements Runnable {
+
+  private static final Logger LOGGER = LoggerFactory.getLogger(DoubleWriteProtector.class);
+
+  private final SessionPool doubleWriteSessionPool;
+
+  private final File logFile;
+  private SingleFileLogReader doubleWriteLogReader;
+
+  private static final int MAX_PHYSICALPLAN_SIZE = 16 * 1024 * 1024;
+  private final ByteArrayOutputStream doubleWriteByteStream;
+  private final DataOutputStream doubleWriteSerializeStream;
+
+  DoubleWriteProtector(File logFile, SessionPool doubleWriteSessionPool) {
+    this.doubleWriteSessionPool = doubleWriteSessionPool;
+    this.logFile = logFile;
+
+    // For serialize PhysicalPlan
+    doubleWriteByteStream = new ByteArrayOutputStream(MAX_PHYSICALPLAN_SIZE);
+    doubleWriteSerializeStream = new DataOutputStream(doubleWriteByteStream);
+
+    // init DoubleWriteLog reader
+    try {
+      doubleWriteLogReader = new SingleFileLogReader(logFile);
+    } catch (FileNotFoundException ignore) {
+      doubleWriteLogReader = null;
+    }
+  }
+
+  @Override
+  public void run() {
+    if (doubleWriteLogReader == null) {
+      return;
+    }
+
+    while (doubleWriteLogReader.hasNext()) {
+      // read and re-serialize the PhysicalPlan
+      PhysicalPlan nextPlan = doubleWriteLogReader.next();
+      try {
+        nextPlan.serialize(doubleWriteSerializeStream);
+      } catch (IOException e) {
+        LOGGER.error("DoubleWriteProtector can't serialize PhysicalPlan", e);
+        continue;
+      }
+      ByteBuffer nextBuffer = ByteBuffer.wrap(doubleWriteByteStream.toByteArray());
+      doubleWriteByteStream.reset();
+
+      while (true) {
+        // transmit DoubleWriteRequest until it's been received
+        boolean transmitStatus = false;
+
+        try {
+          // try double write
+          nextBuffer.position(0);
+          transmitStatus = doubleWriteSessionPool.doubleWriteTransmit(nextBuffer);
+        } catch (IoTDBConnectionException connectionException) {
+          // warn IoTDBConnectionException and retry
+          LOGGER.warn("DoubleWriteProtector can't transmit, retrying...", connectionException);
+        } catch (Exception e) {
+          // error exception and break
+          LOGGER.error("DoubleWriteProtector can't transmit", e);
+          break;
+        }
+
+        if (transmitStatus) {
+          break;
+        } else {
+          try {
+            TimeUnit.SECONDS.sleep(1);
+          } catch (InterruptedException ignore) {
+            // ignore
+          }
+        }
+      }
+    }
+
+    doubleWriteLogReader.close();
+    try {
+      // sleep one second then delete DoubleWriteLog file
+      TimeUnit.SECONDS.sleep(1);
+    } catch (InterruptedException ignore) {
+      // ignore
+    }
+    while (true) {

Review comment:
       This may cause death loop. You may add a counter to avoid it.




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