jt2594838 commented on a change in pull request #372: 
[IOTDB-198]Reimplementation sync module
URL: https://github.com/apache/incubator-iotdb/pull/372#discussion_r322520129
 
 

 ##########
 File path: 
server/src/main/java/org/apache/iotdb/db/sync/receiver/transfer/SyncServiceImpl.java
 ##########
 @@ -0,0 +1,343 @@
+/**
+ * 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.sync.receiver.transfer;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.iotdb.db.concurrent.ThreadName;
+import org.apache.iotdb.db.conf.IoTDBConfig;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.conf.directories.DirectoryManager;
+import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
+import org.apache.iotdb.db.exception.DiskSpaceInsufficientException;
+import org.apache.iotdb.db.exception.MetadataErrorException;
+import org.apache.iotdb.db.exception.PathErrorException;
+import org.apache.iotdb.db.metadata.MManager;
+import org.apache.iotdb.db.metadata.MetadataConstant;
+import org.apache.iotdb.db.metadata.MetadataOperationType;
+import org.apache.iotdb.db.sync.receiver.load.FileLoader;
+import org.apache.iotdb.db.sync.receiver.load.FileLoaderManager;
+import org.apache.iotdb.db.sync.receiver.recover.SyncReceiverLogAnalyzer;
+import org.apache.iotdb.db.sync.receiver.recover.SyncReceiverLogger;
+import org.apache.iotdb.db.sync.sender.conf.SyncConstant;
+import org.apache.iotdb.db.utils.FilePathUtils;
+import org.apache.iotdb.db.utils.SyncUtils;
+import org.apache.iotdb.service.sync.thrift.ResultStatus;
+import org.apache.iotdb.service.sync.thrift.SyncService;
+import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
+import org.apache.iotdb.tsfile.read.common.Path;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SyncServiceImpl implements SyncService.Iface {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(SyncServiceImpl.class);
+
+  private IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
+
+  private ThreadLocal<String> syncFolderPath = new ThreadLocal<>();
+
+  private ThreadLocal<String> currentSG = new ThreadLocal<>();
+
+  private ThreadLocal<SyncReceiverLogger> syncLog = new ThreadLocal<>();
+
+  private ThreadLocal<String> senderName = new ThreadLocal<>();
+
+  private ThreadLocal<File> currentFile = new ThreadLocal<>();
+
+  private ThreadLocal<FileChannel> currentFileWriter = new ThreadLocal<>();
+
+  private ThreadLocal<MessageDigest> messageDigest = new ThreadLocal<>();
+
+  /**
+   * Verify IP address of sender
+   */
+  @Override
+  public ResultStatus check(String ipAddress, String uuid) {
+    Thread.currentThread().setName(ThreadName.SYNC_SERVER.getName());
+    if (SyncUtils.verifyIPSegment(config.getIpWhiteList(), ipAddress)) {
+      senderName.set(ipAddress + SyncConstant.SYNC_DIR_NAME_SEPARATOR + uuid);
+      if (checkRecovery()) {
+        logger.info("Start to sync with sender {}", senderName.get());
+        return getSuccessResult();
+      } else {
+        return getErrorResult("Receiver is processing data from previous sync 
tasks");
+      }
+    } else {
+      return getErrorResult(
+          "Sender IP is not in the white list of receiver IP and 
synchronization tasks are not allowed.");
+    }
+  }
+
+  private boolean checkRecovery() {
+    try {
+      if (currentFileWriter.get() != null && currentFileWriter.get().isOpen()) 
{
+        currentFileWriter.get().close();
+      }
+      if (syncLog.get() != null) {
+        syncLog.get().close();
+      }
+      return SyncReceiverLogAnalyzer.getInstance().recover(senderName.get());
+    } catch (IOException e) {
+      logger.error("Check recovery state fail", e);
+      return false;
+    }
+  }
+
+  @Override
+  public ResultStatus startSync() {
+    try {
+      initPath();
+      currentSG.remove();
+      FileLoader.createFileLoader(senderName.get(), syncFolderPath.get());
+      syncLog.set(new SyncReceiverLogger(new File(syncFolderPath.get(), 
SyncConstant.SYNC_LOG_NAME)));
+      return getSuccessResult();
+    } catch (DiskSpaceInsufficientException | IOException e) {
+      logger.error("Can not receiver data from sender", e);
+      return getErrorResult(e.getMessage());
+    }
+  }
+
+  /**
+   * Init file path and clear data if last sync process failed.
 
 Review comment:
   I did not see where you cleared the data.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to