fredia commented on code in PR #20217:
URL: https://github.com/apache/flink/pull/20217#discussion_r939805155


##########
flink-runtime/src/main/java/org/apache/flink/runtime/state/LocalStateRegistry.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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.flink.runtime.state;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.Executor;
+import java.util.concurrent.RejectedExecutionException;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
+/** This registry manages handles which is written for local recovery. */
+public class LocalStateRegistry implements Closeable {
+    private static final Logger LOG = 
LoggerFactory.getLogger(LocalStateRegistry.class);
+    /**
+     * All registered handles. (handle,checkpointID) represents a handle and 
the latest checkpoint
+     * that refer to this handle.
+     */
+    private final Map<StreamStateHandle, Long> registeredHandles;
+
+    /** This flag indicates whether the registry is open or if close() was 
called. */
+    private boolean open;
+
+    /** Executor for async state deletion */
+    private final Executor asyncDisposalExecutor;
+
+    public LocalStateRegistry(Executor ioExecutor) {
+        this.registeredHandles = new HashMap<>();
+        this.asyncDisposalExecutor = ioExecutor;
+        this.open = true;
+    }
+
+    public StreamStateHandle register(StreamStateHandle handle, long 
checkpointID) {
+        synchronized (registeredHandles) {
+            checkState(open, "Attempt to register state to closed 
LocalStateRegistry.");
+            if (registeredHandles.containsKey(handle)) {
+                long pre = registeredHandles.get(handle);
+                if (checkpointID > pre) {
+                    registeredHandles.put(handle, checkpointID);
+                }
+            } else {
+                registeredHandles.put(handle, checkpointID);
+            }
+        }
+        return handle;
+    }
+
+    public void unRegister(long upTo) {
+        List<StreamStateHandle> handles = new ArrayList<>();
+        synchronized (registeredHandles) {
+            Iterator<Entry<StreamStateHandle, Long>> iterator =
+                    registeredHandles.entrySet().iterator();
+            while (iterator.hasNext()) {
+                Entry<StreamStateHandle, Long> entry = iterator.next();
+                if (entry.getValue() < upTo) {
+                    handles.add(entry.getKey());
+                    iterator.remove();
+                }

Review Comment:
   Yes,  the actual underlying file might be used by multiple state backends / 
tasks,  and I think the file has been **implicitly** tracked by every backend:
   
   `LocalStateRegistry#register()` is called when 
`ChangelogKeyedStateBackend#notifyCheckpointComplete` 
->`FsStateChangelogWriter#confirm()`.  When a file is still referenced by a 
state backend,  the state backend would update its `LastUsedCheckpointID`. 
Suppose there are three state backend data in file 1,  
   ```
   File 1:
       backend A : c1, c2
       backend B : c1
       backend C : c2, c3
   ```
   
   - checkpoint 1 confirm, LastUsedCheckpointID = 1,  upTo=1 , won't delete;
   - checkpoint 2 confirm, LastUsedCheckpointID = 2, upTo=2, won't delete;
   - checkpoint 3 confirm, LastUsedCheckpointID = 3, upTo=3, won't delete;
   - checkpoint 4 confirm,  LastUsedCheckpointID = 3, upTo=4, **delete**;
   
   
   I think the role of `LocalStateRegistry` is similar to `SharedStateRegisrty` 
on JM. when passing the control of remote handles to JM, the remote handles are 
not explicitly tracked by multi-state backends, so I don't think 
`LocalStateRegistry` needs to be either.



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