dengziming commented on code in PR #12177: URL: https://github.com/apache/kafka/pull/12177#discussion_r879273738
########## metadata/src/main/java/org/apache/kafka/controller/QuorumController.java: ########## @@ -1735,17 +1742,38 @@ public CompletableFuture<UpdateFeaturesResponseData> updateFeatures( ControllerRequestContext context, UpdateFeaturesRequestData request ) { - return appendWriteEvent("updateFeatures", context.deadlineNs(), () -> { - Map<String, Short> updates = new HashMap<>(); - Map<String, FeatureUpdate.UpgradeType> upgradeTypes = new HashMap<>(); - request.featureUpdates().forEach(featureUpdate -> { - String featureName = featureUpdate.feature(); - upgradeTypes.put(featureName, FeatureUpdate.UpgradeType.fromCode(featureUpdate.upgradeType())); - updates.put(featureName, featureUpdate.maxVersionLevel()); - }); - return featureControl.updateFeatures(updates, upgradeTypes, clusterControl.brokerSupportedVersions(), - request.validateOnly()); + final AtomicLong offsetToSnapshot = new AtomicLong(0L); + return appendWriteEvent("updateFeatures", context.deadlineNs(), new ControllerWriteOperation<Map<String, ApiError>>() { + @Override + public ControllerResult<Map<String, ApiError>> generateRecordsAndResult() throws Exception { + Map<String, Short> updates = new HashMap<>(); + Map<String, FeatureUpdate.UpgradeType> upgradeTypes = new HashMap<>(); + request.featureUpdates().forEach(featureUpdate -> { + String featureName = featureUpdate.feature(); + upgradeTypes.put(featureName, FeatureUpdate.UpgradeType.fromCode(featureUpdate.upgradeType())); + updates.put(featureName, featureUpdate.maxVersionLevel()); + }); + return featureControl.updateFeatures(updates, upgradeTypes, clusterControl.brokerSupportedVersions(), + request.validateOnly()); + } + + @Override + public void processBatchEndOffset(long previousBatchEndOffset, long offset) { + log.debug("Offset before metadata.version update {}. Offset after update {}", previousBatchEndOffset, offset); + offsetToSnapshot.set(previousBatchEndOffset); + } + }).thenApply(result -> { + // The future that this chained callback is attached to is completed synchronously from the Raft thread via + // the controller purgatory. See QuorumMetaLogListener#handleCommit. Since this callback is synchronous, + // we can safely create a snapshot of the prior offset before it gets cleaned up by the Raft thread. + long offset = offsetToSnapshot.get(); + if (offset > 0 && snapshotGeneratorManager.generator == null) { Review Comment: Currently, we are using a single thread `KafkaEventQueue` to handle snapshot events, but this callback seems processed in another thread, let's call it Thread-2, so is it possible for an edge case here? 1. time1: snapshotGeneratorManager.generator == null 2. time2: `KafkaEventQueue` thread set snapshotGeneratorManager.generator = foo 3. time3: Thread-2 throw an exception snapshotGeneratorManager.generator != null -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org