jerrypeng commented on code in PR #38517: URL: https://github.com/apache/spark/pull/38517#discussion_r1048930639
########## sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/AsyncOffsetSeqLog.scala: ########## @@ -0,0 +1,185 @@ +/* + * 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.OutputStream +import java.util.concurrent._ +import java.util.concurrent.atomic.AtomicLong + +import scala.collection.JavaConverters._ + +import org.apache.spark.sql.SparkSession +import org.apache.spark.util.{Clock, SystemClock} + +/** + * Used to write entries to the offset log asynchronously + */ +class AsyncOffsetSeqLog( + sparkSession: SparkSession, + path: String, + executorService: ThreadPoolExecutor, + offsetCommitIntervalMs: Long, + clock: Clock = new SystemClock()) + extends OffsetSeqLog(sparkSession, path) { + + // the cache needs to be enabled because we may not be persisting every entry to durable storage + // entries not persisted to durable storage will just be stored in memory for faster lookups + assert(metadataCacheEnabled == true) + + // A map of the current pending offset writes. Key -> batch Id, Value -> CompletableFuture + // Used to determine if a commit log entry for this batch also needs to be persisted to storage + private val pendingOffsetWrites = new ConcurrentHashMap[Long, CompletableFuture[Long]]() + + // Keeps track the last time a commit was issued. Used for issuing commits to storage at + // the configured intervals + private val lastCommitIssuedTimestampMs: AtomicLong = new AtomicLong(-1) + + // A queue of batches written to storage. Used to keep track when to purge old batches + val writtenToDurableStorage = + new ConcurrentLinkedDeque[Long](listBatchesOnDisk.toList.asJavaCollection) + + /** + * Get a async offset write by batch id. To check if a corresponding commit log entry + * needs to be written to durable storage as well + * @param batchId + * @return a option to indicate whether a async offset write was issued for the batch with id + */ + def getAsyncOffsetWrite(batchId: Long): Option[CompletableFuture[Long]] = { + Option(pendingOffsetWrites.get(batchId)) + } + + /** + * Remove the async offset write when we don't need to keep track of it anymore + * @param batchId + */ + def removeAsyncOffsetWrite(batchId: Long): Unit = { + pendingOffsetWrites.remove(batchId) + } + + /** + * Writes a new batch to the offset log asynchronously + * @param batchId id of batch to write + * @param metadata metadata of batch to write + * @return a CompeletableFuture that contains the batch id. The future is completed when + * the async write of the batch is completed. Future may also be completed exceptionally + * to indicate some write error. + */ + def addAsync(batchId: Long, metadata: OffsetSeq): CompletableFuture[(Long, Boolean)] = { + require(metadata != null, "'null' metadata cannot written to a metadata log") + + def issueAsyncWrite(batchId: Long): CompletableFuture[Long] = { + lastCommitIssuedTimestampMs.set(clock.getTimeMillis()) + val future: CompletableFuture[Long] = addNewBatchByStreamAsync(batchId) { output => + serialize(metadata, output) + }.thenApply((ret: Boolean) => { + if (ret) { + batchId + } else { + throw new IllegalStateException( + s"Concurrent update to the log. Multiple streaming jobs detected for $batchId" Review Comment: This is the message in existing implementation. We don't really differentiate which log. However, you should know which log it is based on the stack trace. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
