|
| 1 | +package org.apache.spark.streaming.storage |
| 2 | + |
| 3 | +import java.nio.ByteBuffer |
| 4 | + |
| 5 | +import scala.collection.mutable.ArrayBuffer |
| 6 | +import scala.concurrent.{ExecutionContext, Future} |
| 7 | + |
| 8 | +import org.apache.hadoop.conf.Configuration |
| 9 | +import org.apache.hadoop.fs.Path |
| 10 | +import org.apache.hadoop.fs.permission.FsPermission |
| 11 | +import org.apache.spark.Logging |
| 12 | +import org.apache.spark.streaming.storage.WriteAheadLogManager._ |
| 13 | +import org.apache.spark.streaming.util.{Clock, SystemClock} |
| 14 | +import org.apache.spark.util.Utils |
| 15 | + |
| 16 | +private[streaming] class WriteAheadLogManager( |
| 17 | + logDirectory: String, |
| 18 | + hadoopConf: Configuration, |
| 19 | + rollingIntervalSecs: Int = 60, |
| 20 | + maxFailures: Int = 3, |
| 21 | + callerName: String = "", |
| 22 | + clock: Clock = new SystemClock |
| 23 | + ) extends Logging { |
| 24 | + |
| 25 | + private val pastLogs = new ArrayBuffer[LogInfo] |
| 26 | + private val callerNameTag = |
| 27 | + if (callerName != null && callerName.nonEmpty) s" for $callerName" else "" |
| 28 | + private val threadpoolName = s"WriteAheadLogManager $callerNameTag" |
| 29 | + implicit private val executionContext = ExecutionContext.fromExecutorService( |
| 30 | + Utils.newDaemonFixedThreadPool(1, threadpoolName)) |
| 31 | + override protected val logName = s"WriteAheadLogManager $callerNameTag" |
| 32 | + |
| 33 | + private var currentLogPath: String = null |
| 34 | + private var currentLogWriter: WriteAheadLogWriter = null |
| 35 | + private var currentLogWriterStartTime: Long = -1L |
| 36 | + private var currentLogWriterStopTime: Long = -1L |
| 37 | + |
| 38 | + initializeOrRecover() |
| 39 | + |
| 40 | + def writeToLog(byteBuffer: ByteBuffer): FileSegment = synchronized { |
| 41 | + var fileSegment: FileSegment = null |
| 42 | + var failures = 0 |
| 43 | + var lastException: Exception = null |
| 44 | + var succeeded = false |
| 45 | + while (!succeeded && failures < maxFailures) { |
| 46 | + try { |
| 47 | + fileSegment = getLogWriter(clock.currentTime).write(byteBuffer) |
| 48 | + succeeded = true |
| 49 | + } catch { |
| 50 | + case ex: Exception => |
| 51 | + lastException = ex |
| 52 | + logWarning("Failed to ...") |
| 53 | + resetWriter() |
| 54 | + failures += 1 |
| 55 | + } |
| 56 | + } |
| 57 | + if (fileSegment == null) { |
| 58 | + throw lastException |
| 59 | + } |
| 60 | + fileSegment |
| 61 | + } |
| 62 | + |
| 63 | + def readFromLog(): Iterator[ByteBuffer] = synchronized { |
| 64 | + val logFilesToRead = pastLogs.map{ _.path} ++ Option(currentLogPath) |
| 65 | + logInfo("Reading from the logs: " + logFilesToRead.mkString("\n")) |
| 66 | + logFilesToRead.iterator.map { file => |
| 67 | + logDebug(s"Creating log reader with $file") |
| 68 | + new WriteAheadLogReader(file, hadoopConf) |
| 69 | + } flatMap { x => x } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Delete the log files that are older than the threshold time. |
| 74 | + * |
| 75 | + * Its important to note that the threshold time is based on the time stamps used in the log |
| 76 | + * files, and is therefore based on the local system time. So if there is coordination necessary |
| 77 | + * between the node calculating the threshTime (say, driver node), and the local system time |
| 78 | + * (say, worker node), the caller has to take account of possible time skew. |
| 79 | + */ |
| 80 | + def cleanupOldLogs(threshTime: Long): Unit = { |
| 81 | + val oldLogFiles = synchronized { pastLogs.filter { _.endTime < threshTime } } |
| 82 | + logInfo(s"Attempting to clear ${oldLogFiles.size} old log files in $logDirectory " + |
| 83 | + s"older than $threshTime: ${oldLogFiles.map { _.path }.mkString("\n")}") |
| 84 | + |
| 85 | + def deleteFiles() { |
| 86 | + oldLogFiles.foreach { logInfo => |
| 87 | + try { |
| 88 | + val path = new Path(logInfo.path) |
| 89 | + val fs = hadoopConf.synchronized { path.getFileSystem(hadoopConf) } |
| 90 | + fs.delete(path, true) |
| 91 | + synchronized { pastLogs -= logInfo } |
| 92 | + logDebug(s"Cleared log file $logInfo") |
| 93 | + } catch { |
| 94 | + case ex: Exception => |
| 95 | + logWarning(s"Error clearing log file $logInfo", ex) |
| 96 | + } |
| 97 | + } |
| 98 | + logInfo(s"Cleared log files in $logDirectory older than $threshTime") |
| 99 | + } |
| 100 | + if (!executionContext.isShutdown) { |
| 101 | + Future { deleteFiles() } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + def stop(): Unit = synchronized { |
| 106 | + if (currentLogWriter != null) { |
| 107 | + currentLogWriter.close() |
| 108 | + } |
| 109 | + executionContext.shutdown() |
| 110 | + logInfo("Stopped log manager") |
| 111 | + } |
| 112 | + |
| 113 | + private def getLogWriter(currentTime: Long): WriteAheadLogWriter = synchronized { |
| 114 | + if (currentLogWriter == null || currentTime > currentLogWriterStopTime) { |
| 115 | + resetWriter() |
| 116 | + if (currentLogPath != null) { |
| 117 | + pastLogs += LogInfo(currentLogWriterStartTime, currentLogWriterStopTime, currentLogPath) |
| 118 | + } |
| 119 | + currentLogWriterStartTime = currentTime |
| 120 | + currentLogWriterStopTime = currentTime + (rollingIntervalSecs * 1000) |
| 121 | + val newLogPath = new Path(logDirectory, |
| 122 | + timeToLogFile(currentLogWriterStartTime, currentLogWriterStopTime)) |
| 123 | + currentLogPath = newLogPath.toString |
| 124 | + currentLogWriter = new WriteAheadLogWriter(currentLogPath, hadoopConf) |
| 125 | + } |
| 126 | + currentLogWriter |
| 127 | + } |
| 128 | + |
| 129 | + private def initializeOrRecover(): Unit = synchronized { |
| 130 | + val logDirectoryPath = new Path(logDirectory) |
| 131 | + val fileSystem = logDirectoryPath.getFileSystem(hadoopConf) |
| 132 | + |
| 133 | + if (fileSystem.exists(logDirectoryPath) && fileSystem.getFileStatus(logDirectoryPath).isDir) { |
| 134 | + val logFileInfo = logFilesTologInfo(fileSystem.listStatus(logDirectoryPath).map { _.getPath }) |
| 135 | + pastLogs.clear() |
| 136 | + pastLogs ++= logFileInfo |
| 137 | + logInfo(s"Recovered ${logFileInfo.size} log files from $logDirectory") |
| 138 | + logDebug(s"Recovered files are:\n${logFileInfo.map(_.path).mkString("\n")}") |
| 139 | + } else { |
| 140 | + fileSystem.mkdirs(logDirectoryPath, |
| 141 | + FsPermission.createImmutable(Integer.parseInt("770", 8).toShort)) |
| 142 | + logInfo(s"Created ${logDirectory} for log files") |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private def resetWriter(): Unit = synchronized { |
| 147 | + if (currentLogWriter != null) { |
| 148 | + currentLogWriter.close() |
| 149 | + currentLogWriter = null |
| 150 | + } |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +private[storage] object WriteAheadLogManager { |
| 155 | + |
| 156 | + case class LogInfo(startTime: Long, endTime: Long, path: String) |
| 157 | + |
| 158 | + val logFileRegex = """log-(\d+)-(\d+)""".r |
| 159 | + |
| 160 | + def timeToLogFile(startTime: Long, stopTime: Long): String = { |
| 161 | + s"log-$startTime-$stopTime" |
| 162 | + } |
| 163 | + |
| 164 | + def logFilesTologInfo(files: Seq[Path]): Seq[LogInfo] = { |
| 165 | + files.flatMap { file => |
| 166 | + logFileRegex.findFirstIn(file.getName()) match { |
| 167 | + case Some(logFileRegex(startTimeStr, stopTimeStr)) => |
| 168 | + val startTime = startTimeStr.toLong |
| 169 | + val stopTime = stopTimeStr.toLong |
| 170 | + Some(LogInfo(startTime, stopTime, file.toString)) |
| 171 | + case None => |
| 172 | + None |
| 173 | + } |
| 174 | + }.sortBy { _.startTime } |
| 175 | + } |
| 176 | +} |
0 commit comments