HeartSaVioR commented on a change in pull request #32272: URL: https://github.com/apache/spark/pull/32272#discussion_r624388116
########## 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) Review comment: Would it produce same output with `json`? Since this doesn't manipulate empty logFiles field. Otherwise is it by intention to handle json and prettyJson differently? ########## File path: sql/core/src/test/scala/org/apache/spark/sql/execution/streaming/state/RocksDBSuite.scala ########## @@ -0,0 +1,65 @@ +/* + * 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._ +import java.nio.charset.Charset + +import org.apache.commons.io.FileUtils + +import org.apache.spark._ +import org.apache.spark.util.Utils + +class RocksDBSuite extends SparkFunSuite { + + test("checkpoint metadata serde roundtrip") { + def checkJsonRoundtrip(metadata: RocksDBCheckpointMetadata, json: String): Unit = { + assert(metadata.json == json) + withTempDirectory { dir => + val file = new File(dir, "json") + FileUtils.write(file, s"v1\n$json", Charset.defaultCharset) + assert(metadata == RocksDBCheckpointMetadata.readFromFile(file)) + } + } + val sstFiles = Seq(RocksDBSstFile("00001.sst", "00001-uuid.sst", 12345678901234L)) + val logFiles = Seq(RocksDBLogFile("00001.log", "00001-uuid.log", 12345678901234L)) + + // scalastyle:off line.size.limit + // should always include sstFiles and numKeys + checkJsonRoundtrip( + RocksDBCheckpointMetadata(Seq.empty, 0L), + """{"sstFiles":[],"numKeys":0}""" + ) + // shouldn't include the "logFiles" field in json when it's empty + checkJsonRoundtrip( + RocksDBCheckpointMetadata(sstFiles, 12345678901234L), + """{"sstFiles":[{"localFileName":"00001.sst","dfsSstFileName":"00001-uuid.sst","sizeBytes":12345678901234}],"numKeys":12345678901234}""" + ) + checkJsonRoundtrip( + RocksDBCheckpointMetadata(sstFiles, logFiles, 12345678901234L), + """{"sstFiles":[{"localFileName":"00001.sst","dfsSstFileName":"00001-uuid.sst","sizeBytes":12345678901234}],"logFiles":[{"localFileName":"00001.log","dfsLogFileName":"00001-uuid.log","sizeBytes":12345678901234}],"numKeys":12345678901234}""") + // scalastyle:on line.size.limit + } + + private def withTempDirectory(f: File => Unit): Unit = { Review comment: If I remember correctly, withTempDir is defined in SparkFunSuite so you can just leverage the method. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
