You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@carbondata.apache.org by GitBox <gi...@apache.org> on 2020/09/15 07:25:22 UTC

[GitHub] [carbondata] ajantha-bhat commented on a change in pull request #3871: [CARBONDATA-3986] Fix multiple issues during compaction and concurrent scenarios

ajantha-bhat commented on a change in pull request #3871:
URL: https://github.com/apache/carbondata/pull/3871#discussion_r488444308



##########
File path: processing/src/main/java/org/apache/carbondata/processing/loading/TableProcessingOperations.java
##########
@@ -64,45 +68,89 @@ public static void deletePartialLoadDataIfExist(CarbonTable carbonTable,
       if (allSegments == null || allSegments.length == 0) {
         return;
       }
-      LoadMetadataDetails[] details = SegmentStatusManager.readLoadMetadata(metaDataLocation);
-      // there is no segment or failed to read tablestatus file.
-      // so it should stop immediately.
-      if (details == null || details.length == 0) {
-        return;
-      }
-      Set<String> metadataSet = new HashSet<>(details.length);
-      for (LoadMetadataDetails detail : details) {
-        metadataSet.add(detail.getLoadName());
-      }
-      List<CarbonFile> staleSegments = new ArrayList<>(allSegments.length);
-      for (CarbonFile segment : allSegments) {
-        String segmentName = segment.getName();
-        // check segment folder pattern
-        if (segmentName.startsWith(CarbonTablePath.SEGMENT_PREFIX)) {
-          String[] parts = segmentName.split(CarbonCommonConstants.UNDERSCORE);
-          if (parts.length == 2) {
-            boolean isOriginal = !parts[1].contains(".");
-            if (isCompactionFlow) {
-              // in compaction flow, it should be big segment and segment metadata is not exists
-              if (!isOriginal && !metadataSet.contains(parts[1])) {
-                staleSegments.add(segment);
+      int retryCount = CarbonLockUtil
+          .getLockProperty(CarbonCommonConstants.NUMBER_OF_TRIES_FOR_CONCURRENT_LOCK,
+              CarbonCommonConstants.NUMBER_OF_TRIES_FOR_CONCURRENT_LOCK_DEFAULT);
+      int maxTimeout = CarbonLockUtil
+          .getLockProperty(CarbonCommonConstants.MAX_TIMEOUT_FOR_CONCURRENT_LOCK,
+              CarbonCommonConstants.MAX_TIMEOUT_FOR_CONCURRENT_LOCK_DEFAULT);
+      ICarbonLock carbonTableStatusLock = CarbonLockFactory
+          .getCarbonLockObj(carbonTable.getAbsoluteTableIdentifier(), LockUsage.TABLE_STATUS_LOCK);
+      try {
+        if (carbonTableStatusLock.lockWithRetries(retryCount, maxTimeout)) {
+          LoadMetadataDetails[] details = SegmentStatusManager.readLoadMetadata(metaDataLocation);
+          // there is no segment or failed to read tablestatus file.
+          // so it should stop immediately.
+          if (details == null || details.length == 0) {
+            return;
+          }
+          Set<String> metadataSet = new HashSet<>(details.length);
+          for (LoadMetadataDetails detail : details) {
+            metadataSet.add(detail.getLoadName());
+          }
+          List<CarbonFile> staleSegments = new ArrayList<>(allSegments.length);
+          Set<String> staleSegmentsId = new HashSet<>(allSegments.length);
+          for (CarbonFile segment : allSegments) {
+            String segmentName = segment.getName();
+            // check segment folder pattern
+            if (segmentName.startsWith(CarbonTablePath.SEGMENT_PREFIX)) {
+              String[] parts = segmentName.split(CarbonCommonConstants.UNDERSCORE);
+              if (parts.length == 2) {
+                boolean isOriginal = !parts[1].contains(".");
+                if (isCompactionFlow) {
+                  // in compaction flow, it should be big segment and segment metadata is not exists
+                  if (!isOriginal && !metadataSet.contains(parts[1])) {
+                    staleSegments.add(segment);
+                    staleSegmentsId.add(parts[1]);
+                  }
+                } else {
+                  // in loading flow,
+                  // it should be original segment and segment metadata is not exists
+                  if (isOriginal && !metadataSet.contains(parts[1])) {
+                    staleSegments.add(segment);
+                    staleSegmentsId.add(parts[1]);
+                  }
+                }
               }
-            } else {
-              // in loading flow, it should be original segment and segment metadata is not exists
-              if (isOriginal && !metadataSet.contains(parts[1])) {
-                staleSegments.add(segment);
+            }
+          }
+          // delete segment folders one by one
+          for (CarbonFile staleSegment : staleSegments) {
+            try {
+              CarbonUtil.deleteFoldersAndFiles(staleSegment);
+            } catch (IOException | InterruptedException e) {
+              LOGGER.error("Unable to delete the given path :: " + e.getMessage(), e);
+            }
+          }
+          if (staleSegments.size() > 0) {
+            // collect the segment metadata path
+            String segmentFilesLocation =
+                CarbonTablePath.getSegmentFilesLocation(carbonTable.getTablePath());
+            CarbonFile[] allSegmentMetadataFiles =
+                FileFactory.getCarbonFile(segmentFilesLocation).listFiles();
+            // delete the segment metadata files also
+            for (CarbonFile segmentMetadataFile : allSegmentMetadataFiles) {
+              String segmentId =
+                  segmentMetadataFile.getName().split(CarbonCommonConstants.UNDERSCORE)[0];
+              if (staleSegmentsId.contains(segmentId)) {
+                segmentMetadataFile.delete();
               }
             }
           }
+        } else {
+          LOGGER.error(
+              "Not able to acquire the Table status lock for partial load deletion for table "
+                  + carbonTable.getDatabaseName() + "." + carbonTable.getTableName()
+                  + ", retry compaction");

Review comment:
       assigned to a variable

##########
File path: processing/src/main/java/org/apache/carbondata/processing/loading/TableProcessingOperations.java
##########
@@ -64,45 +68,89 @@ public static void deletePartialLoadDataIfExist(CarbonTable carbonTable,
       if (allSegments == null || allSegments.length == 0) {
         return;
       }
-      LoadMetadataDetails[] details = SegmentStatusManager.readLoadMetadata(metaDataLocation);
-      // there is no segment or failed to read tablestatus file.
-      // so it should stop immediately.
-      if (details == null || details.length == 0) {
-        return;
-      }
-      Set<String> metadataSet = new HashSet<>(details.length);
-      for (LoadMetadataDetails detail : details) {
-        metadataSet.add(detail.getLoadName());
-      }
-      List<CarbonFile> staleSegments = new ArrayList<>(allSegments.length);
-      for (CarbonFile segment : allSegments) {
-        String segmentName = segment.getName();
-        // check segment folder pattern
-        if (segmentName.startsWith(CarbonTablePath.SEGMENT_PREFIX)) {
-          String[] parts = segmentName.split(CarbonCommonConstants.UNDERSCORE);
-          if (parts.length == 2) {
-            boolean isOriginal = !parts[1].contains(".");
-            if (isCompactionFlow) {
-              // in compaction flow, it should be big segment and segment metadata is not exists
-              if (!isOriginal && !metadataSet.contains(parts[1])) {
-                staleSegments.add(segment);
+      int retryCount = CarbonLockUtil
+          .getLockProperty(CarbonCommonConstants.NUMBER_OF_TRIES_FOR_CONCURRENT_LOCK,
+              CarbonCommonConstants.NUMBER_OF_TRIES_FOR_CONCURRENT_LOCK_DEFAULT);
+      int maxTimeout = CarbonLockUtil
+          .getLockProperty(CarbonCommonConstants.MAX_TIMEOUT_FOR_CONCURRENT_LOCK,
+              CarbonCommonConstants.MAX_TIMEOUT_FOR_CONCURRENT_LOCK_DEFAULT);
+      ICarbonLock carbonTableStatusLock = CarbonLockFactory
+          .getCarbonLockObj(carbonTable.getAbsoluteTableIdentifier(), LockUsage.TABLE_STATUS_LOCK);
+      try {
+        if (carbonTableStatusLock.lockWithRetries(retryCount, maxTimeout)) {
+          LoadMetadataDetails[] details = SegmentStatusManager.readLoadMetadata(metaDataLocation);
+          // there is no segment or failed to read tablestatus file.
+          // so it should stop immediately.
+          if (details == null || details.length == 0) {
+            return;
+          }
+          Set<String> metadataSet = new HashSet<>(details.length);
+          for (LoadMetadataDetails detail : details) {
+            metadataSet.add(detail.getLoadName());
+          }
+          List<CarbonFile> staleSegments = new ArrayList<>(allSegments.length);
+          Set<String> staleSegmentsId = new HashSet<>(allSegments.length);
+          for (CarbonFile segment : allSegments) {
+            String segmentName = segment.getName();
+            // check segment folder pattern
+            if (segmentName.startsWith(CarbonTablePath.SEGMENT_PREFIX)) {
+              String[] parts = segmentName.split(CarbonCommonConstants.UNDERSCORE);
+              if (parts.length == 2) {
+                boolean isOriginal = !parts[1].contains(".");
+                if (isCompactionFlow) {
+                  // in compaction flow, it should be big segment and segment metadata is not exists
+                  if (!isOriginal && !metadataSet.contains(parts[1])) {
+                    staleSegments.add(segment);
+                    staleSegmentsId.add(parts[1]);
+                  }
+                } else {
+                  // in loading flow,
+                  // it should be original segment and segment metadata is not exists
+                  if (isOriginal && !metadataSet.contains(parts[1])) {
+                    staleSegments.add(segment);
+                    staleSegmentsId.add(parts[1]);
+                  }
+                }
               }
-            } else {
-              // in loading flow, it should be original segment and segment metadata is not exists
-              if (isOriginal && !metadataSet.contains(parts[1])) {
-                staleSegments.add(segment);
+            }
+          }
+          // delete segment folders one by one
+          for (CarbonFile staleSegment : staleSegments) {
+            try {
+              CarbonUtil.deleteFoldersAndFiles(staleSegment);
+            } catch (IOException | InterruptedException e) {
+              LOGGER.error("Unable to delete the given path :: " + e.getMessage(), e);
+            }
+          }
+          if (staleSegments.size() > 0) {
+            // collect the segment metadata path
+            String segmentFilesLocation =
+                CarbonTablePath.getSegmentFilesLocation(carbonTable.getTablePath());
+            CarbonFile[] allSegmentMetadataFiles =

Review comment:
       ok

##########
File path: core/src/main/java/org/apache/carbondata/core/writer/CarbonIndexFileMergeWriter.java
##########
@@ -251,9 +261,29 @@ public String writeMergeIndexFileBasedOnSegmentFile(String segmentId,
     String path = CarbonTablePath.getSegmentFilesLocation(table.getTablePath())
         + CarbonCommonConstants.FILE_SEPARATOR + newSegmentFileName;
     if (!table.isHivePartitionTable()) {
-      SegmentFileStore.writeSegmentFile(segmentFileStore.getSegmentFile(), path);
-      SegmentFileStore.updateTableStatusFile(table, segmentId, newSegmentFileName,
+      String content = SegmentStatusManager.readFileAsString(path);
+      try {
+        SegmentFileStore.writeSegmentFile(segmentFileStore.getSegmentFile(), path);
+      } catch (Exception ex) {
+        // delete merge index file if created,

Review comment:
       In both the callers, they are catching these exceptions and adding log. Same behavior.

##########
File path: core/src/main/java/org/apache/carbondata/core/writer/CarbonIndexFileMergeWriter.java
##########
@@ -233,15 +234,24 @@ public String writeMergeIndexFileBasedOnSegmentFile(String segmentId,
         }
         if (FileFactory.getCarbonFile(entry.getKey()).equals(FileFactory.getCarbonFile(location))) {
           segment.getValue().setMergeFileName(mergeIndexFile);
-          segment.getValue().setFiles(new HashSet<String>());
+          mergeIndexFiles
+              .add(entry.getKey() + CarbonCommonConstants.FILE_SEPARATOR + mergeIndexFile);
+          segment.getValue().setFiles(new HashSet<>());
           break;
         }
       }
       if (table.isHivePartitionTable()) {
         for (PartitionSpec partitionSpec : partitionSpecs) {
           if (partitionSpec.getLocation().toString().equals(partitionPath)) {
-            SegmentFileStore.writeSegmentFile(table.getTablePath(), mergeIndexFile, partitionPath,
-                segmentId + "_" + uuid + "", partitionSpec.getPartitions(), true);
+            try {
+              SegmentFileStore.writeSegmentFile(table.getTablePath(), mergeIndexFile, partitionPath,
+                  segmentId + "_" + uuid + "", partitionSpec.getPartitions(), true);
+            } catch (Exception ex) {
+              // delete merge index file if created,
+              // keep only index files as segment file writing is failed

Review comment:
       In both the callers, they are catching this exceptions and adding log. Same behavior.

##########
File path: core/src/main/java/org/apache/carbondata/core/writer/CarbonIndexFileMergeWriter.java
##########
@@ -233,15 +234,24 @@ public String writeMergeIndexFileBasedOnSegmentFile(String segmentId,
         }
         if (FileFactory.getCarbonFile(entry.getKey()).equals(FileFactory.getCarbonFile(location))) {
           segment.getValue().setMergeFileName(mergeIndexFile);
-          segment.getValue().setFiles(new HashSet<String>());
+          mergeIndexFiles
+              .add(entry.getKey() + CarbonCommonConstants.FILE_SEPARATOR + mergeIndexFile);
+          segment.getValue().setFiles(new HashSet<>());
           break;
         }
       }
       if (table.isHivePartitionTable()) {
         for (PartitionSpec partitionSpec : partitionSpecs) {
           if (partitionSpec.getLocation().toString().equals(partitionPath)) {
-            SegmentFileStore.writeSegmentFile(table.getTablePath(), mergeIndexFile, partitionPath,
-                segmentId + "_" + uuid + "", partitionSpec.getPartitions(), true);
+            try {
+              SegmentFileStore.writeSegmentFile(table.getTablePath(), mergeIndexFile, partitionPath,
+                  segmentId + "_" + uuid + "", partitionSpec.getPartitions(), true);

Review comment:
       > name the boolean variable
   This is java. Handled only _ change.

##########
File path: core/src/main/java/org/apache/carbondata/core/writer/CarbonIndexFileMergeWriter.java
##########
@@ -233,15 +234,24 @@ public String writeMergeIndexFileBasedOnSegmentFile(String segmentId,
         }
         if (FileFactory.getCarbonFile(entry.getKey()).equals(FileFactory.getCarbonFile(location))) {
           segment.getValue().setMergeFileName(mergeIndexFile);
-          segment.getValue().setFiles(new HashSet<String>());
+          mergeIndexFiles
+              .add(entry.getKey() + CarbonCommonConstants.FILE_SEPARATOR + mergeIndexFile);
+          segment.getValue().setFiles(new HashSet<>());
           break;
         }
       }
       if (table.isHivePartitionTable()) {
         for (PartitionSpec partitionSpec : partitionSpecs) {
           if (partitionSpec.getLocation().toString().equals(partitionPath)) {
-            SegmentFileStore.writeSegmentFile(table.getTablePath(), mergeIndexFile, partitionPath,
-                segmentId + "_" + uuid + "", partitionSpec.getPartitions(), true);
+            try {
+              SegmentFileStore.writeSegmentFile(table.getTablePath(), mergeIndexFile, partitionPath,
+                  segmentId + "_" + uuid + "", partitionSpec.getPartitions(), true);

Review comment:
       > name the boolean variable
   
   This is java. Handled only _ change.




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