Login  Register

[GitHub] [carbondata] vikramahuja1001 commented on a change in pull request #4005: [CARBONDATA-3978] Trash Folder support in carbondata

Posted by GitBox on Nov 26, 2020; 11:16am
URL: http://apache-carbondata-dev-mailing-list-archive.168.s1.nabble.com/GitHub-carbondata-vikramahuja1001-opened-a-new-pull-request-4005-WIP-Trash-Folder-support-in-carbonda-tp103155p103639.html


vikramahuja1001 commented on a change in pull request #4005:
URL: https://github.com/apache/carbondata/pull/4005#discussion_r530955721



##########
File path: core/src/main/java/org/apache/carbondata/core/util/CleanFilesUtil.java
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.carbondata.core.util;
+
+import java.io.IOException;
+import java.util.*;
+
+import org.apache.carbondata.common.logging.LogServiceFactory;
+import org.apache.carbondata.core.constants.CarbonCommonConstants;
+import org.apache.carbondata.core.datastore.filesystem.CarbonFile;
+import org.apache.carbondata.core.datastore.impl.FileFactory;
+import org.apache.carbondata.core.metadata.SegmentFileStore;
+import org.apache.carbondata.core.metadata.schema.table.CarbonTable;
+import org.apache.carbondata.core.statusmanager.LoadMetadataDetails;
+import org.apache.carbondata.core.statusmanager.SegmentStatus;
+import org.apache.carbondata.core.statusmanager.SegmentStatusManager;
+import org.apache.carbondata.core.util.path.CarbonTablePath;
+
+import org.apache.hadoop.fs.Path;
+import org.apache.log4j.Logger;
+
+/**
+ * Mantains the clean files command in carbondata. This class has methods for clean files
+ * operation.
+ */
+public class CleanFilesUtil {
+
+  private static final Logger LOGGER =
+      LogServiceFactory.getLogService(CleanFilesUtil.class.getName());
+
+  /**
+   * This method will clean all the stale segments for table given table. In this method, we first
+   * get the stale segments(segments whose entry is not in the table status, but are present in
+   * the metadata folder) or in case when table status is deleted. To identify the stale segments
+   * we compare the segment files in the metadata folder with table status file, if it exists. The
+   * identified stale segments are then copied to the trash folder and then their .segment files
+   * are also deleted from the metadata folder. We only compare with tablestatus file here, not
+   * with tablestatus history file.
+   */
+  public static void cleanStaleSegments(CarbonTable carbonTable)
+    throws IOException {
+    String metaDataLocation = carbonTable.getMetadataPath();
+    long timeStampForTrashFolder = System.currentTimeMillis();
+    String segmentFilesLocation =
+        CarbonTablePath.getSegmentFilesLocation(carbonTable.getTablePath());
+    CarbonFile[] segmentFilesList = FileFactory.getCarbonFile(segmentFilesLocation).listFiles();
+    // there are no segments present in the Metadata folder. Can return here
+    if (segmentFilesList.length == 0) {
+      return;
+    }
+    LoadMetadataDetails[] details = SegmentStatusManager.readLoadMetadata(metaDataLocation);
+    List<String> staleSegments = getStaleSegments(details, segmentFilesList);
+
+    if (staleSegments.size() > 0) {
+      for (String staleSegment : staleSegments) {
+        String segmentNumber = staleSegment.split(CarbonCommonConstants.UNDERSCORE)[0];
+        // for each segment we get the indexfile first, then we get the carbondata file. Move both
+        // of those to trash folder
+        List<CarbonFile> filesToDelete = new ArrayList<>();
+        SegmentFileStore fileStore = new SegmentFileStore(carbonTable.getTablePath(),
+            staleSegment);
+        List<String> indexOrMergeFiles = fileStore.readIndexFiles(SegmentStatus.SUCCESS, true,
+            FileFactory.getConfiguration());
+        for (String file : indexOrMergeFiles) {
+          // copy the index or merge file to the trash folder
+          TrashUtil.copyFileToTrashFolder(file, CarbonTablePath.getTrashFolderPath(carbonTable
+              .getTablePath()) + CarbonCommonConstants.FILE_SEPARATOR + timeStampForTrashFolder +
+              CarbonCommonConstants.FILE_SEPARATOR + CarbonTablePath.SEGMENT_PREFIX +
+              segmentNumber);
+          filesToDelete.add(FileFactory.getCarbonFile(file));
+        }
+        // get carbondata files from here
+        Map<String, List<String>> indexFilesMap = fileStore.getIndexFilesMap();
+        for (Map.Entry<String, List<String>> entry : indexFilesMap.entrySet()) {
+          for (String file : entry.getValue()) {
+            // copy the carbondata file to trash
+            TrashUtil.copyFileToTrashFolder(file, CarbonTablePath.getTrashFolderPath(carbonTable
+                .getTablePath()) + CarbonCommonConstants.FILE_SEPARATOR + timeStampForTrashFolder
+                + CarbonCommonConstants.FILE_SEPARATOR + CarbonTablePath.SEGMENT_PREFIX
+                + segmentNumber);
+            filesToDelete.add(FileFactory.getCarbonFile(file));
+          }
+        }
+        // Delete the segment file too
+        filesToDelete.add(FileFactory.getCarbonFile(CarbonTablePath.getSegmentFilePath(carbonTable
+            .getTablePath(), staleSegment)));
+        // After every file of that segment has been copied, need to delete those files.
+        LOGGER.info("Segment number: " + segmentNumber + "has been successfully copied to the" +
+            " trash folder");
+        try {
+          for (CarbonFile file : filesToDelete) {
+            if (file.isFileExist()) {

Review comment:
       done

##########
File path: core/src/main/java/org/apache/carbondata/core/util/CleanFilesUtil.java
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.carbondata.core.util;
+
+import java.io.IOException;
+import java.util.*;
+
+import org.apache.carbondata.common.logging.LogServiceFactory;
+import org.apache.carbondata.core.constants.CarbonCommonConstants;
+import org.apache.carbondata.core.datastore.filesystem.CarbonFile;
+import org.apache.carbondata.core.datastore.impl.FileFactory;
+import org.apache.carbondata.core.metadata.SegmentFileStore;
+import org.apache.carbondata.core.metadata.schema.table.CarbonTable;
+import org.apache.carbondata.core.statusmanager.LoadMetadataDetails;
+import org.apache.carbondata.core.statusmanager.SegmentStatus;
+import org.apache.carbondata.core.statusmanager.SegmentStatusManager;
+import org.apache.carbondata.core.util.path.CarbonTablePath;
+
+import org.apache.hadoop.fs.Path;
+import org.apache.log4j.Logger;
+
+/**
+ * Mantains the clean files command in carbondata. This class has methods for clean files
+ * operation.
+ */
+public class CleanFilesUtil {
+
+  private static final Logger LOGGER =
+      LogServiceFactory.getLogService(CleanFilesUtil.class.getName());
+
+  /**
+   * This method will clean all the stale segments for table given table. In this method, we first
+   * get the stale segments(segments whose entry is not in the table status, but are present in
+   * the metadata folder) or in case when table status is deleted. To identify the stale segments
+   * we compare the segment files in the metadata folder with table status file, if it exists. The
+   * identified stale segments are then copied to the trash folder and then their .segment files
+   * are also deleted from the metadata folder. We only compare with tablestatus file here, not
+   * with tablestatus history file.
+   */
+  public static void cleanStaleSegments(CarbonTable carbonTable)
+    throws IOException {
+    String metaDataLocation = carbonTable.getMetadataPath();
+    long timeStampForTrashFolder = System.currentTimeMillis();
+    String segmentFilesLocation =
+        CarbonTablePath.getSegmentFilesLocation(carbonTable.getTablePath());
+    CarbonFile[] segmentFilesList = FileFactory.getCarbonFile(segmentFilesLocation).listFiles();
+    // there are no segments present in the Metadata folder. Can return here
+    if (segmentFilesList.length == 0) {
+      return;
+    }
+    LoadMetadataDetails[] details = SegmentStatusManager.readLoadMetadata(metaDataLocation);
+    List<String> staleSegments = getStaleSegments(details, segmentFilesList);
+
+    if (staleSegments.size() > 0) {
+      for (String staleSegment : staleSegments) {
+        String segmentNumber = staleSegment.split(CarbonCommonConstants.UNDERSCORE)[0];
+        // for each segment we get the indexfile first, then we get the carbondata file. Move both
+        // of those to trash folder
+        List<CarbonFile> filesToDelete = new ArrayList<>();
+        SegmentFileStore fileStore = new SegmentFileStore(carbonTable.getTablePath(),
+            staleSegment);
+        List<String> indexOrMergeFiles = fileStore.readIndexFiles(SegmentStatus.SUCCESS, true,
+            FileFactory.getConfiguration());
+        for (String file : indexOrMergeFiles) {
+          // copy the index or merge file to the trash folder
+          TrashUtil.copyFileToTrashFolder(file, CarbonTablePath.getTrashFolderPath(carbonTable
+              .getTablePath()) + CarbonCommonConstants.FILE_SEPARATOR + timeStampForTrashFolder +
+              CarbonCommonConstants.FILE_SEPARATOR + CarbonTablePath.SEGMENT_PREFIX +
+              segmentNumber);
+          filesToDelete.add(FileFactory.getCarbonFile(file));
+        }
+        // get carbondata files from here
+        Map<String, List<String>> indexFilesMap = fileStore.getIndexFilesMap();
+        for (Map.Entry<String, List<String>> entry : indexFilesMap.entrySet()) {
+          for (String file : entry.getValue()) {
+            // copy the carbondata file to trash
+            TrashUtil.copyFileToTrashFolder(file, CarbonTablePath.getTrashFolderPath(carbonTable
+                .getTablePath()) + CarbonCommonConstants.FILE_SEPARATOR + timeStampForTrashFolder
+                + CarbonCommonConstants.FILE_SEPARATOR + CarbonTablePath.SEGMENT_PREFIX
+                + segmentNumber);
+            filesToDelete.add(FileFactory.getCarbonFile(file));
+          }
+        }
+        // Delete the segment file too
+        filesToDelete.add(FileFactory.getCarbonFile(CarbonTablePath.getSegmentFilePath(carbonTable
+            .getTablePath(), staleSegment)));
+        // After every file of that segment has been copied, need to delete those files.
+        LOGGER.info("Segment number: " + segmentNumber + "has been successfully copied to the" +
+            " trash folder");
+        try {
+          for (CarbonFile file : filesToDelete) {
+            if (file.isFileExist()) {
+              FileFactory.deleteFile(file.getAbsolutePath());
+              // deleting empty segment folder in case of normal table and partition folders
+              // in case of partition table
+              SegmentFileStore.deleteEmptyPartitionFolders(FileFactory.getCarbonFile(new Path(file

Review comment:
       done

##########
File path: core/src/main/java/org/apache/carbondata/core/util/TrashUtil.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.carbondata.core.util;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.carbondata.common.logging.LogServiceFactory;
+import org.apache.carbondata.core.constants.CarbonCommonConstants;
+import org.apache.carbondata.core.datastore.filesystem.CarbonFile;
+import org.apache.carbondata.core.datastore.impl.FileFactory;
+import org.apache.carbondata.core.util.path.CarbonTablePath;
+
+import org.apache.hadoop.io.IOUtils;
+import org.apache.log4j.Logger;
+
+/**
+ * Mantains the trash folder in carbondata. This class has methods to copy data to the trash and
+ * remove data from the trash.
+ */
+public final class TrashUtil {
+
+  private static final Logger LOGGER =
+      LogServiceFactory.getLogService(TrashUtil.class.getName());
+
+  /**
+   * Base method to copy the data to the trash folder.
+   *
+   * @param fromPath the path from which to copy the file
+   * @param toPath  the path where the file will be copied
+   * @return
+   */
+  private static void copyToTrashFolder(String fromPath, String toPath) throws IOException {
+    DataOutputStream dataOutputStream = null;
+    DataInputStream dataInputStream = null;
+    try {
+      dataOutputStream = FileFactory.getDataOutputStream(toPath);
+      dataInputStream = FileFactory.getDataInputStream(fromPath);
+      IOUtils.copyBytes(dataInputStream, dataOutputStream, CarbonCommonConstants.BYTEBUFFER_SIZE);
+    } catch (IOException exception) {
+      LOGGER.error("Unable to copy " + fromPath + " to the trash folder", exception);
+      throw exception;
+    } finally {
+      CarbonUtil.closeStreams(dataInputStream, dataOutputStream);
+    }
+  }
+
+  /**
+   * The below method copies the complete a file to the trash folder.
+   *
+   * @param filePathToCopy the files which are to be moved to the trash folder
+   * @param trashFolderWithTimestamp    timestamp, partition folder(if any) and segment number
+   * @return
+   */
+  public static void copyFileToTrashFolder(String filePathToCopy,
+      String trashFolderWithTimestamp) throws IOException {
+    CarbonFile carbonFileToCopy = FileFactory.getCarbonFile(filePathToCopy);
+    try {
+      if (carbonFileToCopy.exists()) {
+        if (!FileFactory.isFileExist(trashFolderWithTimestamp)) {
+          FileFactory.mkdirs(trashFolderWithTimestamp);
+        }
+        if (!FileFactory.isFileExist(trashFolderWithTimestamp + CarbonCommonConstants
+            .FILE_SEPARATOR + carbonFileToCopy.getName())) {
+          copyToTrashFolder(filePathToCopy, trashFolderWithTimestamp + CarbonCommonConstants
+              .FILE_SEPARATOR + carbonFileToCopy.getName());
+        }
+      }
+    } catch (IOException e) {
+      LOGGER.error("Error while creating trash folder or copying data to the trash folder", e);
+      throw e;
+    }
+  }
+
+  /**
+   * The below method copies the complete segment folder to the trash folder. Here, the data files
+   * in segment are listed and copied one by one to the trash folder.
+   *
+   * @param segmentPath the folder which are to be moved to the trash folder
+   * @param trashFolderWithTimestamp trashfolderpath with complete timestamp and segment number
+   * @return
+   */
+  public static void copySegmentToTrash(CarbonFile segmentPath,

Review comment:
       Is now being used for normal table clean files flow




----------------------------------------------------------------
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:
[hidden email]