rkhachatryan commented on code in PR #19142:
URL: https://github.com/apache/flink/pull/19142#discussion_r854505121


##########
flink-runtime/src/main/java/org/apache/flink/runtime/state/StateBackendLoader.java:
##########
@@ -358,17 +360,51 @@ public static boolean 
stateBackendFromApplicationOrConfigOrDefaultUseManagedMemo
         return false;
     }
 
+    /**
+     * Load state backend which may wrap the original state backend for 
recovery.
+     *
+     * @param originalStateBackend StateBackend loaded from application or 
config.
+     * @param classLoader User code classloader.
+     * @param keyedStateHandles The state handles for restore.
+     * @return Wrapped state backend for recovery.
+     * @throws DynamicCodeLoadingException Thrown if keyed state handles of 
wrapped state backend
+     *     are found and the class was not found or could not be instantiated.
+     */
+    public static StateBackend loadStateBackendFromKeyedStateHandles(
+            StateBackend originalStateBackend,
+            ClassLoader classLoader,
+            Collection<KeyedStateHandle> keyedStateHandles)
+            throws DynamicCodeLoadingException {
+        if (keyedStateHandles.stream()
+                        .anyMatch(stateHandle -> stateHandle instanceof 
ChangelogStateBackendHandle)
+                && !isChangelogStateBackend(originalStateBackend)) {
+            return loadChangelogStateBackend(originalStateBackend, 
classLoader, true);
+        }
+        return originalStateBackend;
+    }
+
+    private static boolean isChangelogStateBackend(StateBackend backend)
+            throws DynamicCodeLoadingException {
+        try {
+            return Class.forName(CHANGELOG_STATE_BACKEND) == 
backend.getClass();
+        } catch (ClassNotFoundException e) {
+            throw new DynamicCodeLoadingException(
+                    "Cannot find DelegateStateBackend class: " + 
CHANGELOG_STATE_BACKEND, e);
+        }
+    }
+
     private static StateBackend loadChangelogStateBackend(
-            StateBackend backend, ClassLoader classLoader) throws 
DynamicCodeLoadingException {
+            StateBackend backend, ClassLoader classLoader, boolean forSwitch)
+            throws DynamicCodeLoadingException {
 
         // ChangelogStateBackend resides in a separate module, load it using 
reflection
         try {
             Constructor<? extends DelegatingStateBackend> constructor =
                     Class.forName(CHANGELOG_STATE_BACKEND, false, classLoader)
                             .asSubclass(DelegatingStateBackend.class)
-                            .getDeclaredConstructor(StateBackend.class);
+                            .getDeclaredConstructor(StateBackend.class, 
boolean.class);
             constructor.setAccessible(true);
-            return constructor.newInstance(backend);
+            return constructor.newInstance(backend, forSwitch);

Review Comment:
   The difficult part :) 
   How about `DeactivatedChangelogStateBackend`?



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to