Zakelly commented on code in PR #24057:
URL: https://github.com/apache/flink/pull/24057#discussion_r1447239317


##########
flink-runtime/src/main/java/org/apache/flink/runtime/state/ttl/TtlListState.java:
##########
@@ -109,14 +109,32 @@ public List<TtlValue<T>> getUnexpiredOrNull(@Nonnull 
List<TtlValue<T>> ttlValues
         }
 
         long currentTimestamp = timeProvider.currentTimestamp();
-        List<TtlValue<T>> unexpired = new ArrayList<>(ttlValues.size());
         TypeSerializer<TtlValue<T>> elementSerializer =
                 ((ListSerializer<TtlValue<T>>) original.getValueSerializer())
                         .getElementSerializer();
-        for (TtlValue<T> ttlValue : ttlValues) {
-            if (!TtlUtils.expired(ttlValue, ttl, currentTimestamp)) {
+        int firstExpireElementIndex = -1;
+        for (int i = 0; i < ttlValues.size(); i++) {
+            TtlValue<T> ttlValue = ttlValues.get(i);
+            if (TtlUtils.expired(ttlValue, ttl, currentTimestamp)) {
+                firstExpireElementIndex = i;
+                break;
+            }
+        }
+        if (firstExpireElementIndex == -1) {
+            return ttlValues;
+        }
+
+        List<TtlValue<T>> unexpired = new ArrayList<>(ttlValues.size());
+        for (int i = 0; i < ttlValues.size(); i++) {
+            TtlValue<T> ttlValue = ttlValues.get(i);
+            if (i < firstExpireElementIndex) {
                 // we have to do the defensive copy to update the value
                 unexpired.add(elementSerializer.copy(ttlValue));
+            } else if (i > firstExpireElementIndex) {
+                if (!TtlUtils.expired(ttlValue, ttl, currentTimestamp)) {

Review Comment:
   How about merge these branches into one?
   ```
   if (i < firstExpireElementIndex || (i > firstExpireElementIndex && 
!TtlUtils.expired(ttlValue, ttl, currentTimestamp))) {
      //...
   }
   ```



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

Reply via email to