You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "adixitconfluent (via GitHub)" <gi...@apache.org> on 2026/01/07 10:15:16 UTC
[PR] Strategy based implementation of acquire mode in SharePartition [kafka]
adixitconfluent opened a new pull request, #21262:
URL: https://github.com/apache/kafka/pull/21262
### About
This PR performs a refactor of `SharePartition.java` `acquire` functionality. The functionality had become quite huge and adding new code in this method looks daunting. Hence, I have refactored the code to use `Strategy` design pattern to distinguish code for acquire mode thereby creating a clean separation between acquisition modes
### Testing
The originally present unit and integrayion tests should cover this change.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #21262:
URL: https://github.com/apache/kafka/pull/21262#discussion_r2690644791
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/AcquireStrategy.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
Review Comment:
I think it's better to have in the fetch package, as of now, and later required then we can create new package.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #21262: Strategy based implementation of acquire mode in SharePartition
URL: https://github.com/apache/kafka/pull/21262
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "adixitconfluent (via GitHub)" <gi...@apache.org>.
adixitconfluent commented on code in PR #21262:
URL: https://github.com/apache/kafka/pull/21262#discussion_r2674883564
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/AcquireStrategy.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
+
+import org.apache.kafka.common.message.ShareFetchResponseData.AcquiredRecords;
+import org.apache.kafka.server.share.fetch.InFlightBatch;
+
+import java.util.List;
+
+/**
+ * Strategy interface for acquiring records in different modes.
+ * <p>
+ * Two modes are supported:
+ * <ul>
+ * <li>{@code BATCH_OPTIMIZED} - Acquires records in batches optimized for throughput,
+ * potentially splitting large fetches into multiple batches based on batch size.</li>
+ * <li>{@code RECORD_LIMIT} - Acquires up to the requested number of records,
+ * creating a single batch only.</li>
+ * </ul>
+ */
+public interface AcquireStrategy {
Review Comment:
The point of the refactor is to move the code only which is different for the strategies. If we move the entire acquire logic outside `SharePartition`, there will be a lot of duplicate code that we need to manage. Also, for adding new strategy, we will require more code to implement it along with possible duplication of some code. Hence, the best idea to me was to decide the places in acquire which are different and acordingly design the APIs.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #21262:
URL: https://github.com/apache/kafka/pull/21262#discussion_r2691618844
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/BatchCreationContext.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
+
+import org.apache.kafka.common.record.RecordBatch;
+import org.apache.kafka.server.share.fetch.AcquisitionLockTimerTask;
+
+/**
+ * Context object containing all parameters needed for batch creation.
+ */
+public record BatchCreationContext(
+ String memberId,
+ Iterable<? extends RecordBatch> batches,
+ long firstAcquiredOffset,
+ long lastAcquiredOffset,
+ int batchSize,
+ int maxFetchRecords,
+ BatchCacheOperations cacheOperations // Callback interface for operations that require SharePartition state
+) {
+ /**
+ * Interface for operations that interact with SharePartition's cached state.
+ * This allows the strategy to perform necessary state updates without
+ * direct access to SharePartition internals.
+ */
+ public interface BatchCacheOperations {
+ /**
+ * Adds a new in-flight batch to the cache with acquisition lock scheduled.
+ */
+ void addBatchToCache(
+ String memberId,
+ long firstOffset,
+ long lastOffset,
+ AcquisitionLockTimerTask timerTask
+ );
+
+ /**
+ * Adds a new in-flight batch with offset state initialized for partial acquisition.
+ * Used in record_limit mode when batch exceeds max fetch records.
+ */
+ void addBatchWithOffsetState(
+ String memberId,
+ long firstOffset,
+ long lastOffset,
+ long acquiredLastOffset,
+ int delayMs
+ );
+
+ /**
+ * Schedules an acquisition lock timeout for the given offset range.
+ */
+ AcquisitionLockTimerTask scheduleAcquisitionLockTimeout(
+ String memberId,
+ long firstOffset,
+ long lastOffset
+ );
Review Comment:
Why a `scheduleAcquisitionLockTimeout` is a cache operation, and same for others like `recordLockDurationMs`, `updateFindNextFetchOffset` and `recordInFlightBatchMessageCount`? Seeems like you needed callback but calling them cache operations is incorrect.
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/RecordLimitStrategy.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
+
+import org.apache.kafka.common.message.ShareFetchResponseData.AcquiredRecords;
+import org.apache.kafka.server.share.fetch.AcquisitionLockTimerTask;
+import org.apache.kafka.server.share.fetch.InFlightBatch;
+
+import java.util.List;
+
+/**
+ * Strategy for record-limit acquisition mode.
+ * <p>
+ * This mode provides precise control over the number of records acquired by:
+ * <ul>
+ * <li>Creating a single batch containing all records</li>
+ * <li>Acquiring only up to maxFetchRecords, leaving remaining offsets AVAILABLE</li>
+ * <li>Initializing offset state when batch exceeds the limit</li>
+ * </ul>
+ */
+public class RecordLimitStrategy implements AcquireStrategy {
+
+ public static final RecordLimitStrategy INSTANCE = new RecordLimitStrategy();
Review Comment:
Will the instance always be created even Share Fetches are not happening on the cluster? If yes, though it's cheap but why to create an instance which might never be used.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "apoorvmittal10 (via GitHub)" <gi...@apache.org>.
apoorvmittal10 commented on code in PR #21262:
URL: https://github.com/apache/kafka/pull/21262#discussion_r2672966231
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/AcquireStrategy.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
Review Comment:
I think the package should be fetch itself. Same for other classes in the PR.
```suggestion
package org.apache.kafka.server.share.fetch;
```
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/BatchOptimizedStrategy.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
+
+import org.apache.kafka.common.message.ShareFetchResponseData.AcquiredRecords;
+import org.apache.kafka.common.record.RecordBatch;
+import org.apache.kafka.server.share.fetch.AcquisitionLockTimerTask;
+import org.apache.kafka.server.share.fetch.InFlightBatch;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Strategy for batch-optimized acquisition mode.
+ * <p>
+ * This mode optimizes for throughput by:
+ * <ul>
+ * <li>Splitting large fetches into multiple batches based on configured batch size</li>
+ * <li>Acquiring complete batches without per-record limits</li>
+ * <li>All records in the range are acquired and scheduled with acquisition locks</li>
+ * </ul>
+ */
+public class BatchOptimizedStrategy implements AcquireStrategy {
+
+ public static final BatchOptimizedStrategy INSTANCE = new BatchOptimizedStrategy();
+
+ private BatchOptimizedStrategy() {
+ // Singleton class.
+ }
+
+ @Override
+ public boolean requiresSubsetMatch(InFlightBatch inFlightBatch, int maxRecordsToAcquire, int acquiredCount) {
+ // batch_optimized mode doesn't force subset matching based on record count alone.
+ // Subset matching is determined by other factors (fullMatch, offsetState, throttling).
+ return false;
+ }
Review Comment:
If this is not needed in BatchOptimized mode then this API is not needed in interface. Remove the method from interface.
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/AcquireStrategy.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
+
+import org.apache.kafka.common.message.ShareFetchResponseData.AcquiredRecords;
+import org.apache.kafka.server.share.fetch.InFlightBatch;
+
+import java.util.List;
+
+/**
+ * Strategy interface for acquiring records in different modes.
+ * <p>
+ * Two modes are supported:
+ * <ul>
+ * <li>{@code BATCH_OPTIMIZED} - Acquires records in batches optimized for throughput,
+ * potentially splitting large fetches into multiple batches based on batch size.</li>
+ * <li>{@code RECORD_LIMIT} - Acquires up to the requested number of records,
+ * creating a single batch only.</li>
+ * </ul>
+ */
+public interface AcquireStrategy {
Review Comment:
Just on a high level, not totally aligned with the APIs exposed here in the method. I was thinking that you just need one method `acquire` which shall process as per the Strategy.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "adixitconfluent (via GitHub)" <gi...@apache.org>.
adixitconfluent commented on code in PR #21262:
URL: https://github.com/apache/kafka/pull/21262#discussion_r2674882366
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/AcquireStrategy.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
Review Comment:
The only reason I created `acquire` package inside `fetch` was since these are 5 new classes already which are specific to acquire itself and with more refactors in the future, the classes under acquire will only increase, hence I thought to segregate it. I am open to changing it if you think otherwise.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "adixitconfluent (via GitHub)" <gi...@apache.org>.
adixitconfluent commented on code in PR #21262:
URL: https://github.com/apache/kafka/pull/21262#discussion_r2674891227
##########
server/src/main/java/org/apache/kafka/server/share/fetch/acquire/BatchOptimizedStrategy.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.kafka.server.share.fetch.acquire;
+
+import org.apache.kafka.common.message.ShareFetchResponseData.AcquiredRecords;
+import org.apache.kafka.common.record.RecordBatch;
+import org.apache.kafka.server.share.fetch.AcquisitionLockTimerTask;
+import org.apache.kafka.server.share.fetch.InFlightBatch;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Strategy for batch-optimized acquisition mode.
+ * <p>
+ * This mode optimizes for throughput by:
+ * <ul>
+ * <li>Splitting large fetches into multiple batches based on configured batch size</li>
+ * <li>Acquiring complete batches without per-record limits</li>
+ * <li>All records in the range are acquired and scheduled with acquisition locks</li>
+ * </ul>
+ */
+public class BatchOptimizedStrategy implements AcquireStrategy {
+
+ public static final BatchOptimizedStrategy INSTANCE = new BatchOptimizedStrategy();
+
+ private BatchOptimizedStrategy() {
+ // Singleton class.
+ }
+
+ @Override
+ public boolean requiresSubsetMatch(InFlightBatch inFlightBatch, int maxRecordsToAcquire, int acquiredCount) {
+ // batch_optimized mode doesn't force subset matching based on record count alone.
+ // Subset matching is determined by other factors (fullMatch, offsetState, throttling).
+ return false;
+ }
Review Comment:
This API is exposed to `SharePartition` and determines if the in-flight batch requires subset matching based on the acquire mode. In a way it decides if you need to maintain the in-flight batch at per offset level and acquire only a subset of those offsets or you should be good with the entire batch. Hence, it is needed in the interface itself.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #21262:
URL: https://github.com/apache/kafka/pull/21262#issuecomment-4456977815
This PR has been closed since it has not had any activity in 120 days. If you feel like this
was a mistake, or you would like to continue working on it, please feel free to re-open the
PR and ask for a review.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Strategy based implementation of acquire mode in SharePartition [kafka]
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #21262:
URL: https://github.com/apache/kafka/pull/21262#issuecomment-4249097580
This PR is being marked as stale since it has not had any activity in 90 days. If you
would like to keep this PR alive, please leave a comment asking for a review. If the PR has
merge conflicts, update it with the latest from the base branch.
<p>
If you are having difficulty finding a reviewer, please reach out on the
[mailing list](https://kafka.apache.org/contact).
<p>
If this PR is no longer valid or desired, please feel free to close it. If no activity
occurs in the next 30 days, it will be automatically closed.
--
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-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org