Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11625#discussion_r55775160
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/HDFSMetadataLog.scala
 ---
    @@ -0,0 +1,168 @@
    +/*
    +* 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.spark.sql.execution.streaming
    +
    +import java.io.{DataOutput, EOFException, IOException}
    +import java.nio.ByteBuffer
    +
    +import scala.reflect.ClassTag
    +
    +import org.apache.hadoop.fs.{Path, PathFilter}
    +
    +import org.apache.spark.SparkException
    +import org.apache.spark.serializer.JavaSerializer
    +import org.apache.spark.sql.SQLContext
    +import org.apache.spark.util.Utils
    +
    +/**
    + * A [[MetadataLog]] implementation based on HDFS. [[HDFSMetadataLog]] 
uses the specified `path`
    + * as the metadata storage.
    + *
    + * There should only be one [[HDFSMetadataLog]] using `path` at the same 
time. [[HDFSMetadataLog]]
    + * uses a file ".lock" in the directory to make sure there is always only 
one user using `path`.
    + * When [[HDFSMetadataLog]] is created, it will create a ".lock" file. If 
this step fails,
    + * [[HDFSMetadataLog]] will throw an exception saying there is someone 
using the same directory.
    + * When [[HDFSMetadataLog]] is stopped, it will delete the ".lock" file.
    + *
    + * However, in extreme case, e.g., power outage, [[HDFSMetadataLog]] won't 
be able to delete ".lock"
    + * file. Then [[HDFSMetadataLog]] cannot be created even if no one is 
using the `path`. In such
    + * case, the user has to delete the lock file in the exception message to 
restart their application.
    + *
    + * Note: [[HDFSMetadataLog]] doesn't support S3-like file systems as they 
don't guarantee listing
    + * files in a directory always shows the latest files.
    + */
    +class HDFSMetadataLog[T: ClassTag](sqlContext: SQLContext, path: String) 
extends MetadataLog[T] {
    +
    +  private val metadataPath = new Path(path)
    +  private val fs = 
metadataPath.getFileSystem(sqlContext.sparkContext.hadoopConfiguration)
    +  if (!fs.exists(metadataPath)) {
    +    fs.mkdirs(metadataPath)
    +  }
    +
    +  tryAcquireLock()
    +
    +  /**
    +   * A `PathFilter` to filter only batch files
    +   */
    +  private val batchFilesFilter = new PathFilter {
    +    override def accept(path: Path): Boolean = try {
    +      path.getName.toLong
    +      true
    +    } catch {
    +      case _: NumberFormatException => false
    +    }
    +  }
    +
    +  private val serializer = new 
JavaSerializer(sqlContext.sparkContext.conf).newInstance()
    +
    +  private def tryAcquireLock(): Unit = {
    --- End diff --
    
    If we don't use a global `.lock` file, there are two cases when writing a 
log entry fails because of FileAlreadyExistsException:
    
    1. There is another HDFSMetadataLog using the same path
    2. The file is corrupted. We just restarted from a failure and tried to 
rerun a batch.
    
    For case 1, we want to throw `ConcurrentUpdateException`; for case 2, we 
need to override the file.
    
    As we need to figure out which situation we are in, we will try to read the 
file to see if it's completed. However, we also have two possibilities if we 
find the file is corrupted:
    
    1. Another HDFSMetadataLog is writing the file.
    2. Nobody is using the path and the file is corrupted.
    
    So how can we know which case is right?
    
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to