Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(log): introduce async close #1503

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions core/src/main/scala/kafka/log/LocalLog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ class LocalLog(@volatile protected var _dir: File,
// AutoMQ inject end
}

/**
* Asynchronously closes the segments of the log.
*/
private[log] def asyncClose(): CompletableFuture[Void] = {
CompletableFuture.completedFuture(() -> {
close()
})
}

/**
* Completely delete this log directory with no delay.
*/
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/scala/kafka/log/UnifiedLog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,10 @@ class UnifiedLog(@volatile var logStartOffset: Long,
* The memory mapped buffer for index files of this log will be left open until the log is deleted.
*/
override def close(): Unit = {
asyncClose().get()
}

def asyncClose(): CompletableFuture[Void] = {
debug("Closing log")
lock synchronized {
logOffsetsListener = LogOffsetsListener.NO_OP_OFFSETS_LISTENER
Expand All @@ -662,7 +666,7 @@ class UnifiedLog(@volatile var logStartOffset: Long,
// (the clean shutdown file is written after the logs are all closed).
producerStateManager.takeSnapshot()
}
localLog.close()
localLog.asyncClose()
}
}

Expand Down
12 changes: 12 additions & 0 deletions core/src/main/scala/kafka/log/streamaspect/ElasticLog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,9 @@ class ElasticLog(val metaStream: MetaStream,
newSegment
}

/**
* ref. LocalLog#close
*/
override private[log] def close(): CompletableFuture[Void] = {
// already flush in UnifiedLog#close, so it's safe to set cleaned shutdown.
partitionMeta.setCleanedShutdown(true)
Expand All @@ -466,6 +469,15 @@ class ElasticLog(val metaStream: MetaStream,
closeStreams()
}

/**
* ref. LocalLog#asyncClose
*/
override private[log] def asyncClose(): CompletableFuture[Void] = {
CompletableFuture.completedFuture(() -> {
close()
})
}

/**
* Directly close all streams of the log.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ class ElasticUnifiedLog(_logStartOffset: Long,
asyncClose().get()
}

// TODO: invoke async close
def asyncClose(): CompletableFuture[Void] = {
override def asyncClose(): CompletableFuture[Void] = {
ElasticUnifiedLog.Logs.remove(elasticLog.topicPartition, this)
val closeFuture = lock synchronized {
maybeFlushMetadataFile()
Expand All @@ -200,7 +199,7 @@ class ElasticUnifiedLog(_logStartOffset: Long,
}
// flush all inflight data/index
flush(true)
elasticLog.close()
elasticLog.asyncClose()
}
elasticLog.segments.clear()
closeFuture.whenComplete((_, _) => {
Expand Down
Loading