ovj commented on a change in pull request #651: Spark Stage retry handling
URL: https://github.com/apache/incubator-hudi/pull/651#discussion_r280959686
 
 

 ##########
 File path: hoodie-client/src/main/java/com/uber/hoodie/table/HoodieTable.java
 ##########
 @@ -263,20 +270,123 @@ public HoodieActiveTimeline getActiveTimeline() {
    * @param stats List of HoodieWriteStats
    * @throws HoodieIOException if some paths can't be finalized on storage
    */
-  public void finalizeWrite(JavaSparkContext jsc, List<HoodieWriteStat> stats)
+  public void finalizeWrite(JavaSparkContext jsc, String instantTs, 
List<HoodieWriteStat> stats)
       throws HoodieIOException {
-    if (config.isConsistencyCheckEnabled()) {
-      List<String> pathsToCheck = stats.stream()
-          .map(stat -> stat.getTempPath() != null
-              ? stat.getTempPath() : stat.getPath())
-          .collect(Collectors.toList());
-
-      List<String> failingPaths = new ConsistencyCheck(config.getBasePath(), 
pathsToCheck, jsc,
-          config.getFinalizeWriteParallelism())
-          .check(MAX_CONSISTENCY_CHECKS, 
INITIAL_CONSISTENCY_CHECK_INTERVAL_MS);
-      if (failingPaths.size() > 0) {
-        throw new HoodieIOException("Could not verify consistency of paths : " 
+ failingPaths);
+    cleanFailedWrites(jsc, instantTs, stats, 
config.isConsistencyCheckEnabled());
+  }
+
+  /**
+   * Reconciles WriteStats and marker files to detect and safely delete 
duplicate data files created because of Spark
+   * retries.
+   *
+   * @param jsc       Spark Context
+   * @param instantTs Instant Timestamp
+   * @param stats   Hoodie Write Stat
+   * @param consistencyCheckEnabled  Consistency Check Enabled
+   * @throws HoodieIOException
+   */
+  protected void cleanFailedWrites(JavaSparkContext jsc, String instantTs, 
List<HoodieWriteStat> stats,
+      boolean consistencyCheckEnabled) throws HoodieIOException {
+    try {
+      // Reconcile marker and data files with WriteStats so that partially 
written data-files due to failed
+      // (but succeeded on retry) tasks are removed.
+      String basePath = getMetaClient().getBasePath();
+      FileSystem fs = getMetaClient().getFs();
+      Path markerDir = new Path(metaClient.getMarkerFolderPath(instantTs));
+
+      if (!fs.exists(markerDir)) {
+        // Happens when all writes are appends
+        return;
+      }
+
+      List<String> invalidDataPaths = FSUtils.getAllDataFilesForMarkers(fs, 
basePath, instantTs, markerDir.toString());
+      List<String> validDataPaths = stats.stream().map(w -> 
String.format("%s/%s", basePath, w.getPath()))
+          .filter(p -> p.endsWith(".parquet")).collect(Collectors.toList());
+      // Contains list of partially created files. These needs to be cleaned 
up.
+      invalidDataPaths.removeAll(validDataPaths);
 
 Review comment:
   Assumption - We are writing marker file before even starting to write single 
byte into parquet.
   
   There is a race here which can cause issues in the ingestion job. We can't 
rely on marker file to find out which parquet files are correct or not. We need 
to use WriteStatus for that.
   Let us say that we had 2 files to be written [p1.parquet & p2.parquet]. p1 
was attempted twice and both succeeded then we will have [p1_attempt1.parquet, 
p1_attempt2.parquet, p2_attempt2.parquet, p1_attempt1.marker, 
p1_attempt2.marker, p2_attempt2.marker] and we would say all parquet files are 
valid.
   
   2 possibilities 
   - p1_attempt1.parquet  / p1_attempt2.parquet is corrupted in which case 
queries will break while reading it.
   - - p1_attempt1.parquet  & p1_attempt2.parquet both are correct in which 
case we will have duplicate 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:
[email protected]


With regards,
Apache Git Services

Reply via email to