xuanyuanking commented on a change in pull request #32272: URL: https://github.com/apache/spark/pull/32272#discussion_r619785921
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDBFileManager.scala ########## @@ -0,0 +1,165 @@ +/* + * 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.state + +import java.io.File +import java.nio.charset.StandardCharsets.UTF_8 +import java.nio.file.Files + +import scala.collection.Seq + +import com.fasterxml.jackson.annotation.JsonInclude.Include +import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper} +import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper} +import org.json4s.NoTypeHints +import org.json4s.jackson.Serialization + +/** + * Classes to represent metadata of checkpoints saved to DFS. Since this is converted to JSON, any + * changes to this MUST be backward-compatible. + */ +case class RocksDBCheckpointMetadata( + sstFiles: Seq[RocksDBSstFile], + logFiles: Seq[RocksDBLogFile], + numKeys: Long) { + import RocksDBCheckpointMetadata._ + + def json: String = { + // We turn this field into a null to avoid write a empty logFiles field in the json. + val nullified = if (logFiles.isEmpty) this.copy(logFiles = null) else this Review comment: It's related to the usage for RocksDB, we don't always have log files. But we must have sst files. ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDBFileManager.scala ########## @@ -0,0 +1,165 @@ +/* + * 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.state + +import java.io.File +import java.nio.charset.StandardCharsets.UTF_8 +import java.nio.file.Files + +import scala.collection.Seq + +import com.fasterxml.jackson.annotation.JsonInclude.Include +import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper} +import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper} +import org.json4s.NoTypeHints +import org.json4s.jackson.Serialization + +/** + * Classes to represent metadata of checkpoints saved to DFS. Since this is converted to JSON, any + * changes to this MUST be backward-compatible. + */ +case class RocksDBCheckpointMetadata( + sstFiles: Seq[RocksDBSstFile], + logFiles: Seq[RocksDBLogFile], + numKeys: Long) { + import RocksDBCheckpointMetadata._ + + def json: String = { + // We turn this field into a null to avoid write a empty logFiles field in the json. + val nullified = if (logFiles.isEmpty) this.copy(logFiles = null) else this + mapper.writeValueAsString(nullified) + } + + def prettyJson: String = Serialization.writePretty(this)(RocksDBCheckpointMetadata.format) + + def writeToFile(metadataFile: File): Unit = { + val writer = Files.newBufferedWriter(metadataFile.toPath, UTF_8) + try { + writer.write(s"v$VERSION\n") + writer.write(this.json) + } finally { + writer.close() + } + } + + def immutableFiles: Seq[RocksDBImmutableFile] = sstFiles ++ logFiles +} + +/** Helper class for [[RocksDBCheckpointMetadata]] */ +object RocksDBCheckpointMetadata { + val VERSION = 1 + + implicit val format = Serialization.formats(NoTypeHints) + + /** Used to convert between classes and JSON. */ + lazy val mapper = { + val _mapper = new ObjectMapper with ScalaObjectMapper + _mapper.setSerializationInclusion(Include.NON_ABSENT) + _mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + _mapper.registerModule(DefaultScalaModule) + _mapper + } + + def readFromFile(metadataFile: File): RocksDBCheckpointMetadata = { + val reader = Files.newBufferedReader(metadataFile.toPath, UTF_8) + try { + val versionLine = reader.readLine() + if (versionLine != s"v$VERSION") { + throw new IllegalStateException( + s"Cannot read RocksDB checkpoint metadata of version $versionLine") + } + Serialization.read[RocksDBCheckpointMetadata](reader) + } finally { + reader.close() + } + } + + def apply(rocksDBFiles: Seq[RocksDBImmutableFile], numKeys: Long): RocksDBCheckpointMetadata = { + val sstFiles = rocksDBFiles.collect { case file: RocksDBSstFile => file } + val logFiles = rocksDBFiles.collect { case file: RocksDBLogFile => file } + + RocksDBCheckpointMetadata(sstFiles, logFiles, numKeys) + } +} + +/** + * A RocksDBImmutableFile maintains a mapping between a local RocksDB file name and the name of + * its copy on DFS. Since these files are immutable, their DFS copies can be reused. Review comment: Yes. Can be mapped to more than one local file but for different tasks. The most common scenario is task/stage retry. ########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/state/RocksDBFileManager.scala ########## @@ -0,0 +1,165 @@ +/* + * 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.state + +import java.io.File +import java.nio.charset.StandardCharsets.UTF_8 +import java.nio.file.Files + +import scala.collection.Seq + +import com.fasterxml.jackson.annotation.JsonInclude.Include +import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper} +import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper} +import org.json4s.NoTypeHints +import org.json4s.jackson.Serialization + +/** + * Classes to represent metadata of checkpoints saved to DFS. Since this is converted to JSON, any + * changes to this MUST be backward-compatible. + */ +case class RocksDBCheckpointMetadata( + sstFiles: Seq[RocksDBSstFile], + logFiles: Seq[RocksDBLogFile], + numKeys: Long) { + import RocksDBCheckpointMetadata._ + + def json: String = { + // We turn this field into a null to avoid write a empty logFiles field in the json. + val nullified = if (logFiles.isEmpty) this.copy(logFiles = null) else this + mapper.writeValueAsString(nullified) + } + + def prettyJson: String = Serialization.writePretty(this)(RocksDBCheckpointMetadata.format) + + def writeToFile(metadataFile: File): Unit = { + val writer = Files.newBufferedWriter(metadataFile.toPath, UTF_8) + try { + writer.write(s"v$VERSION\n") + writer.write(this.json) + } finally { + writer.close() + } + } + + def immutableFiles: Seq[RocksDBImmutableFile] = sstFiles ++ logFiles +} + +/** Helper class for [[RocksDBCheckpointMetadata]] */ +object RocksDBCheckpointMetadata { + val VERSION = 1 + + implicit val format = Serialization.formats(NoTypeHints) + + /** Used to convert between classes and JSON. */ + lazy val mapper = { + val _mapper = new ObjectMapper with ScalaObjectMapper + _mapper.setSerializationInclusion(Include.NON_ABSENT) + _mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + _mapper.registerModule(DefaultScalaModule) + _mapper + } + + def readFromFile(metadataFile: File): RocksDBCheckpointMetadata = { + val reader = Files.newBufferedReader(metadataFile.toPath, UTF_8) + try { + val versionLine = reader.readLine() + if (versionLine != s"v$VERSION") { + throw new IllegalStateException( + s"Cannot read RocksDB checkpoint metadata of version $versionLine") + } + Serialization.read[RocksDBCheckpointMetadata](reader) + } finally { + reader.close() + } + } + + def apply(rocksDBFiles: Seq[RocksDBImmutableFile], numKeys: Long): RocksDBCheckpointMetadata = { + val sstFiles = rocksDBFiles.collect { case file: RocksDBSstFile => file } + val logFiles = rocksDBFiles.collect { case file: RocksDBLogFile => file } + + RocksDBCheckpointMetadata(sstFiles, logFiles, numKeys) + } +} + +/** + * A RocksDBImmutableFile maintains a mapping between a local RocksDB file name and the name of + * its copy on DFS. Since these files are immutable, their DFS copies can be reused. + */ +sealed trait RocksDBImmutableFile { + def localFileName: String + def dfsFileName: String + def sizeBytes: Long + + /** + * Whether another local file is same as the file described by this class. + * A file is same only when the name and the size are same. + */ + def isSameFile(otherFile: File): Boolean = { + otherFile.getName == localFileName && otherFile.length() == sizeBytes + } Review comment: The DFS file name contains UUID, it shouldn't be the same. Normally we use the local file name to filter whether the file is existing locally. -- 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: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org