@@ -63,14 +63,18 @@ private[streaming] class WriteAheadLogManager(
6363 Utils .newDaemonFixedThreadPool(1 , threadpoolName))
6464 override protected val logName = s " WriteAheadLogManager $callerNameTag"
6565
66- private var currentLogPath : String = null
66+ private var currentLogPath : Option [ String ] = None
6767 private var currentLogWriter : WriteAheadLogWriter = null
6868 private var currentLogWriterStartTime : Long = - 1L
6969 private var currentLogWriterStopTime : Long = - 1L
7070
7171 initializeOrRecover()
7272
73- /** Write a byte buffer to the log file */
73+ /**
74+ * Write a byte buffer to the log file. This method synchronously writes the data in the
75+ * ByteBuffer to HDFS. When this method returns, the data is guaranteed to have been flushed
76+ * to HDFS, and will be available for readers to read.
77+ */
7478 def writeToLog (byteBuffer : ByteBuffer ): FileSegment = synchronized {
7579 var fileSegment : FileSegment = null
7680 var failures = 0
@@ -99,13 +103,13 @@ private[streaming] class WriteAheadLogManager(
99103 * Read all the existing logs from the log directory.
100104 *
101105 * Note that this is typically called when the caller is initializing and wants
102- * to recover past state from the write ahead logs (that is, before making any writes).
106+ * to recover past state from the write ahead logs (that is, before making any writes).
103107 * If this is called after writes have been made using this manager, then it may not return
104108 * the latest the records. This does not deal with currently active log files, and
105109 * hence the implementation is kept simple.
106110 */
107111 def readFromLog (): Iterator [ByteBuffer ] = synchronized {
108- val logFilesToRead = pastLogs.map{ _.path} ++ Option ( currentLogPath)
112+ val logFilesToRead = pastLogs.map{ _.path} ++ currentLogPath
109113 logInfo(" Reading from the logs: " + logFilesToRead.mkString(" \n " ))
110114 logFilesToRead.iterator.map { file =>
111115 logDebug(s " Creating log reader with $file" )
@@ -130,7 +134,7 @@ private[streaming] class WriteAheadLogManager(
130134 oldLogFiles.foreach { logInfo =>
131135 try {
132136 val path = new Path (logInfo.path)
133- val fs = hadoopConf. synchronized { path.getFileSystem( hadoopConf) }
137+ val fs = HdfsUtils .getFileSystemForPath(path, hadoopConf)
134138 fs.delete(path, true )
135139 synchronized { pastLogs -= logInfo }
136140 logDebug(s " Cleared log file $logInfo" )
@@ -159,34 +163,30 @@ private[streaming] class WriteAheadLogManager(
159163 private def getLogWriter (currentTime : Long ): WriteAheadLogWriter = synchronized {
160164 if (currentLogWriter == null || currentTime > currentLogWriterStopTime) {
161165 resetWriter()
162- if ( currentLogPath != null ) {
163- pastLogs += LogInfo (currentLogWriterStartTime, currentLogWriterStopTime, currentLogPath )
166+ currentLogPath.foreach {
167+ pastLogs += LogInfo (currentLogWriterStartTime, currentLogWriterStopTime, _ )
164168 }
165169 currentLogWriterStartTime = currentTime
166170 currentLogWriterStopTime = currentTime + (rollingIntervalSecs * 1000 )
167171 val newLogPath = new Path (logDirectory,
168172 timeToLogFile(currentLogWriterStartTime, currentLogWriterStopTime))
169- currentLogPath = newLogPath.toString
170- currentLogWriter = new WriteAheadLogWriter (currentLogPath, hadoopConf)
173+ currentLogPath = Some ( newLogPath.toString)
174+ currentLogWriter = new WriteAheadLogWriter (currentLogPath.get , hadoopConf)
171175 }
172176 currentLogWriter
173177 }
174178
175179 /** Initialize the log directory or recover existing logs inside the directory */
176180 private def initializeOrRecover (): Unit = synchronized {
177181 val logDirectoryPath = new Path (logDirectory)
178- val fileSystem = logDirectoryPath.getFileSystem( hadoopConf)
182+ val fileSystem = HdfsUtils .getFileSystemForPath(logDirectoryPath, hadoopConf)
179183
180184 if (fileSystem.exists(logDirectoryPath) && fileSystem.getFileStatus(logDirectoryPath).isDir) {
181185 val logFileInfo = logFilesTologInfo(fileSystem.listStatus(logDirectoryPath).map { _.getPath })
182186 pastLogs.clear()
183187 pastLogs ++= logFileInfo
184188 logInfo(s " Recovered ${logFileInfo.size} write ahead log files from $logDirectory" )
185189 logDebug(s " Recovered files are: \n ${logFileInfo.map(_.path).mkString(" \n " )}" )
186- } else {
187- fileSystem.mkdirs(logDirectoryPath,
188- FsPermission .createImmutable(Integer .parseInt(" 770" , 8 ).toShort))
189- logInfo(s " Created ${logDirectory} for write ahead log files " )
190190 }
191191 }
192192
0 commit comments