adixitconfluent commented on code in PR #16263:
URL: https://github.com/apache/kafka/pull/16263#discussion_r1634927843


##########
core/src/main/java/kafka/server/ShareSessionContext.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * 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 kafka.server;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.NoSuchElementException;
+import kafka.server.SharePartitionManager.ErroneousAndValidPartitionData;
+import org.apache.kafka.common.TopicIdPartition;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.message.ShareFetchResponseData;
+import org.apache.kafka.common.message.ShareFetchResponseData.PartitionData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.requests.ShareFetchMetadata;
+import org.apache.kafka.common.requests.ShareFetchRequest;
+import org.apache.kafka.common.requests.ShareFetchRequest.SharePartitionData;
+import org.apache.kafka.common.requests.ShareFetchResponse;
+import org.apache.kafka.server.share.CachedSharePartition;
+import org.apache.kafka.server.share.ShareSession;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import scala.Tuple2;
+
+/**
+ * The context for a share session fetch request.
+ */
+public class ShareSessionContext extends ShareFetchContext {
+
+    private final ShareFetchMetadata reqMetadata;
+    private Map<TopicIdPartition, SharePartitionData> shareFetchData;
+    private final boolean isSubsequent;
+    private ShareSession session;
+
+    private final Logger log = 
LoggerFactory.getLogger(ShareSessionContext.class);
+
+    /**
+     * The share fetch context for the first request that starts a share 
session.
+     *
+     * @param reqMetadata        The request metadata.
+     * @param shareFetchData     The share partition data from the share fetch 
request.
+     */
+    public ShareSessionContext(ShareFetchMetadata reqMetadata,
+                               Map<TopicIdPartition, 
ShareFetchRequest.SharePartitionData> shareFetchData) {
+        this.reqMetadata = reqMetadata;
+        this.shareFetchData = shareFetchData;
+        this.isSubsequent = false;
+    }
+
+    /**
+     * The share fetch context for a subsequent request that utilizes an 
existing share session.
+     *
+     * @param reqMetadata  The request metadata.
+     * @param session      The subsequent fetch request session.
+     */
+    public ShareSessionContext(ShareFetchMetadata reqMetadata, ShareSession 
session) {
+        this.reqMetadata = reqMetadata;
+        this.session = session;
+        this.isSubsequent = true;
+    }
+
+    public Map<TopicIdPartition, ShareFetchRequest.SharePartitionData> 
shareFetchData() {
+        return shareFetchData;
+    }
+
+    public boolean isSubsequent() {
+        return isSubsequent;
+    }
+
+    public ShareSession session() {
+        return session;
+    }
+
+    @Override
+    ShareFetchResponse throttleResponse(int throttleTimeMs) {
+        if (!isSubsequent) {
+            return new 
ShareFetchResponse(ShareFetchResponse.toMessage(Errors.NONE, throttleTimeMs,
+                    Collections.emptyIterator(), Collections.emptyList()));
+        } else {
+            int expectedEpoch = 
ShareFetchMetadata.nextEpoch(reqMetadata.epoch());
+            int sessionEpoch;
+            synchronized (session) {
+                sessionEpoch = session.epoch;
+            }
+            if (sessionEpoch != expectedEpoch) {
+                log.debug("Subsequent share session {} expected epoch {}, but 
got {}. " +
+                        "Possible duplicate request.", session.key(), 
expectedEpoch, sessionEpoch);
+                return new 
ShareFetchResponse(ShareFetchResponse.toMessage(Errors.INVALID_SHARE_SESSION_EPOCH,
+                        throttleTimeMs, Collections.emptyIterator(), 
Collections.emptyList()));
+            } else
+                return new 
ShareFetchResponse(ShareFetchResponse.toMessage(Errors.NONE, throttleTimeMs,
+                        Collections.emptyIterator(), Collections.emptyList()));
+        }
+    }
+
+    // Iterator that goes over the given partition map and selects partitions 
that need to be included in the response.
+    // If updateShareContextAndRemoveUnselected is set to true, the share 
context will be updated for the selected
+    // partitions and also remove unselected ones as they are encountered.
+    private class PartitionIterator implements
+            Iterator<Entry<TopicIdPartition, PartitionData>> {
+        private final Iterator<Map.Entry<TopicIdPartition, 
ShareFetchResponseData.PartitionData>> iterator;
+        private final boolean updateShareContextAndRemoveUnselected;
+        private Map.Entry<TopicIdPartition, 
ShareFetchResponseData.PartitionData> nextElement;
+
+
+        public PartitionIterator(Iterator<Map.Entry<TopicIdPartition, 
ShareFetchResponseData.PartitionData>> iterator, boolean 
updateShareContextAndRemoveUnselected) {
+            this.iterator = iterator;
+            this.updateShareContextAndRemoveUnselected = 
updateShareContextAndRemoveUnselected;
+        }
+
+        @Override
+        public boolean hasNext() {
+            while ((nextElement == null) && iterator.hasNext()) {
+                Map.Entry<TopicIdPartition, 
ShareFetchResponseData.PartitionData> element = iterator.next();
+                TopicIdPartition topicPart = element.getKey();
+                ShareFetchResponseData.PartitionData respData = 
element.getValue();
+                CachedSharePartition cachedPart = 
session.partitionMap().find(new CachedSharePartition(topicPart));
+                boolean mustRespond = 
cachedPart.maybeUpdateResponseData(respData, 
updateShareContextAndRemoveUnselected);
+                if (mustRespond) {
+                    nextElement = element;
+                    if (updateShareContextAndRemoveUnselected && 
ShareFetchResponse.recordsSize(respData) > 0) {
+                        // Session.partitionMap is of type 
ImplicitLinkedHashCollection<> which tracks the order of insertion of elements.
+                        // Since, we are updating an element in this case, we 
need to perform a remove and then a mustAdd to maintain the correct order
+                        session.partitionMap().remove(cachedPart);
+                        session.partitionMap().mustAdd(cachedPart);
+                    }
+                } else {
+                    if (updateShareContextAndRemoveUnselected) {
+                        iterator.remove();
+                    }
+                }
+            }
+            return nextElement != null;
+        }
+
+        @Override
+        public Map.Entry<TopicIdPartition, 
ShareFetchResponseData.PartitionData> next() {
+            if (!hasNext()) throw new NoSuchElementException();
+            Map.Entry<TopicIdPartition, ShareFetchResponseData.PartitionData> 
element = nextElement;
+            nextElement = null;
+            return element;
+        }
+
+        @Override
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+
+    @Override
+    int responseSize(LinkedHashMap<TopicIdPartition, PartitionData> updates,
+                     short version) {
+        if (!isSubsequent)
+            return ShareFetchResponse.sizeOf(version, 
updates.entrySet().iterator());
+        else {
+            synchronized (session) {
+                int expectedEpoch = 
ShareFetchMetadata.nextEpoch(reqMetadata.epoch());
+                if (session.epoch != expectedEpoch) {
+                    return ShareFetchResponse.sizeOf(version, 
Collections.emptyIterator());
+                } else {
+                    // Pass the partition iterator which updates neither the 
share fetch context nor the partition map.
+                    return ShareFetchResponse.sizeOf(version, new 
PartitionIterator(updates.entrySet().iterator(), false));
+                }
+            }
+        }
+    }
+
+    @Override
+    ShareFetchResponse updateAndGenerateResponseData(String groupId, Uuid 
memberId,
+                                                     
LinkedHashMap<TopicIdPartition, ShareFetchResponseData.PartitionData> updates) {
+        if (!isSubsequent) {
+            return new ShareFetchResponse(ShareFetchResponse.toMessage(
+                    Errors.NONE, 0, updates.entrySet().iterator(), 
Collections.emptyList()));
+        } else {
+            int expectedEpoch = 
ShareFetchMetadata.nextEpoch(reqMetadata.epoch());
+            int sessionEpoch;
+            synchronized (session) {
+                sessionEpoch = session.epoch;
+            }
+            if (session.epoch != expectedEpoch) {

Review Comment:
   yeah, I'll replace session.epoch here with sessionEpoch.



-- 
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

Reply via email to