You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "funky-eyes (via GitHub)" <gi...@apache.org> on 2026/10/10 06:40:51 UTC
Re: [PR] refactor(file): introduce pluggable file store runtime [incubator-seata]
funky-eyes commented on code in PR #8217:
URL: https://github.com/apache/incubator-seata/pull/8217#discussion_r4236616039
##########
server/src/main/java/org/apache/seata/server/storage/file/lock/FileLockManager.java:
##########
@@ -17,44 +17,46 @@
package org.apache.seata.server.storage.file.lock;
import org.apache.seata.common.loader.LoadLevel;
+import org.apache.seata.common.loader.Scope;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.lock.Locker;
import org.apache.seata.server.lock.AbstractLockManager;
import org.apache.seata.server.session.BranchSession;
import org.apache.seata.server.session.GlobalSession;
-import org.apache.seata.server.storage.raft.lock.RaftLockManager;
-import org.slf4j.MDC;
-
-import java.util.List;
-
-import static org.apache.seata.core.context.RootContext.MDC_KEY_BRANCH_ID;
+import org.apache.seata.server.storage.file.spi.FileLockStore;
/**
* The type file lock manager.
*
*/
-@LoadLevel(name = "file")
+@LoadLevel(name = "file", scope = Scope.PROTOTYPE)
Review Comment:
Why do we need to use Scope.PROTOTYPE here?
##########
server/src/main/java/org/apache/seata/server/coordinator/AbstractCore.java:
##########
@@ -62,8 +60,6 @@ public abstract class AbstractCore implements Core {
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractCore.class);
- protected LockManager lockManager = LockerManagerFactory.getLockManager();
Review Comment:
Removing this `protected` field can break existing subclasses with `NoSuchFieldError` and prevents recompiled subclasses from using the old member. The analogous field is also removed from `AbstractService`. If these classes are supported extension points, could the old members be retained or a migration path and supported ABI boundary be documented?
##########
server/src/main/java/org/apache/seata/server/session/SessionHolder.java:
##########
@@ -97,56 +108,60 @@ public static void init() {
* @param sessionMode the store mode: file, db, redis
* @throws IOException the io exception
*/
- public static void init(SessionMode sessionMode) {
+ public static synchronized void init(SessionMode sessionMode) {
+ if (FILE_STORE_RUNTIME != null) {
+ throw new StoreException("FileStoreRuntime is already initialized; destroy it before reinitializing");
+ }
if (null == sessionMode) {
sessionMode = StoreConfig.getSessionMode();
}
LOGGER.info("use session store mode: {}", sessionMode.getName());
+ LockMode lockMode = StoreConfig.getLockMode();
+ boolean fileSessionMode = SessionMode.FILE.equals(sessionMode);
+ boolean fileLockMode = LockMode.FILE.equals(lockMode);
+ if (fileSessionMode != fileLockMode) {
Review Comment:
In 2.x, `store.session.mode` and `store.lock.mode` are resolved independently, so combinations such as File sessions with DB locks were accepted. This guard adds a compatibility restriction. Removing it alone is insufficient because File lock-manager installation is now coupled to opening a File runtime; could you decouple those lifecycles and add startup/locking coverage for both mixed-mode combinations?
##########
server/src/main/java/org/apache/seata/server/session/SessionHolder.java:
##########
@@ -97,56 +108,60 @@ public static void init() {
* @param sessionMode the store mode: file, db, redis
* @throws IOException the io exception
*/
- public static void init(SessionMode sessionMode) {
+ public static synchronized void init(SessionMode sessionMode) {
+ if (FILE_STORE_RUNTIME != null) {
+ throw new StoreException("FileStoreRuntime is already initialized; destroy it before reinitializing");
+ }
if (null == sessionMode) {
sessionMode = StoreConfig.getSessionMode();
}
LOGGER.info("use session store mode: {}", sessionMode.getName());
+ LockMode lockMode = StoreConfig.getLockMode();
+ boolean fileSessionMode = SessionMode.FILE.equals(sessionMode);
+ boolean fileLockMode = LockMode.FILE.equals(lockMode);
+ if (fileSessionMode != fileLockMode) {
+ String message = fileSessionMode
+ ? "file session mode must use file lock mode"
+ : "file lock mode must use file session mode";
+ throw new StoreException(message);
+ }
+ if (!fileSessionMode) {
+ initLegacyMode(sessionMode);
+ return;
+ }
+
+ FileStoreRuntime runtime = null;
+ boolean lockManagerInstalled = false;
+ try {
+ runtime = openFileStoreRuntime();
+ FILE_STORE_RUNTIME = runtime;
+ lockManagerInstalled = LockerManagerFactory.init(
+ LockMode.FILE, new Class<?>[] {FileLockStore.class}, new Object[] {runtime.lockStore()});
Review Comment:
This loads the `file` `LockManager` using a `(FileLockStore)` constructor. Existing `LockManager` providers registered for `file` use the old no-argument SPI and will no longer resolve through this path. Could the PR preserve that provider contract, or define and test an explicit migration path?
##########
server/src/main/java/org/apache/seata/server/storage/file/lock/FileLockManager.java:
##########
@@ -17,44 +17,46 @@
package org.apache.seata.server.storage.file.lock;
import org.apache.seata.common.loader.LoadLevel;
+import org.apache.seata.common.loader.Scope;
import org.apache.seata.core.exception.TransactionException;
import org.apache.seata.core.lock.Locker;
import org.apache.seata.server.lock.AbstractLockManager;
import org.apache.seata.server.session.BranchSession;
import org.apache.seata.server.session.GlobalSession;
-import org.apache.seata.server.storage.raft.lock.RaftLockManager;
-import org.slf4j.MDC;
-
-import java.util.List;
-
-import static org.apache.seata.core.context.RootContext.MDC_KEY_BRANCH_ID;
+import org.apache.seata.server.storage.file.spi.FileLockStore;
/**
* The type file lock manager.
*
*/
-@LoadLevel(name = "file")
+@LoadLevel(name = "file", scope = Scope.PROTOTYPE)
public class FileLockManager extends AbstractLockManager {
+ private final FileLockStore lockStore;
+
+ protected FileLockManager() {
Review Comment:
This changes the previously public implicit no-argument constructor to `protected`, so external callers compiled against `new FileLockManager()` can fail with `IllegalAccessError`. Could the public no-argument constructor remain as a delegating overload, or should this API break be explicitly documented as unsupported?
##########
server/src/main/java/org/apache/seata/server/storage/file/DefaultFileStoreProvider.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.seata.server.storage.file;
+
+import org.apache.seata.common.exception.StoreException;
+import org.apache.seata.common.loader.LoadLevel;
+import org.apache.seata.server.storage.file.lock.DefaultFileLockStore;
+import org.apache.seata.server.storage.file.session.FileSessionManager;
+import org.apache.seata.server.storage.file.spi.FileLockStore;
+import org.apache.seata.server.storage.file.spi.FileStoreContext;
+import org.apache.seata.server.storage.file.spi.FileStoreProvider;
+import org.apache.seata.server.storage.file.spi.FileStoreRuntime;
+
+import java.io.IOException;
+
+@LoadLevel(name = "default")
+public class DefaultFileStoreProvider implements FileStoreProvider {
+
+ @Override
+ public FileStoreRuntime open(FileStoreContext context) {
+ FileSessionManager sessionManager = null;
+ try {
+ sessionManager = createSessionManager(context);
+ FileLockStore lockStore = createLockStore();
+ return new DefaultFileStoreRuntime(sessionManager, lockStore);
+ } catch (IOException e) {
+ StoreException failure = new StoreException(e, "open default file store runtime failed");
+ closeSessionManager(sessionManager, failure);
+ throw failure;
+ } catch (RuntimeException | Error failure) {
+ closeSessionManager(sessionManager, failure);
+ throw failure;
+ }
+ }
+
+ protected FileSessionManager createSessionManager(FileStoreContext context) throws IOException {
+ return new FileSessionManager(
Review Comment:
In 2.x, File sessions are loaded through `EnhancedServiceLoader.load(SessionManager.class, "file", ...)`. Constructing `FileSessionManager` directly here bypasses existing `SessionManager` providers registered for `file` when the default engine is selected. Please preserve that extension point or provide a documented migration path to `FileStoreProvider`, with a compatibility test.
##########
server/src/main/java/org/apache/seata/server/storage/file/DefaultFileStoreRuntime.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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.seata.server.storage.file;
+
+import org.apache.seata.common.exception.StoreException;
+import org.apache.seata.core.exception.TransactionException;
+import org.apache.seata.server.session.SessionManager;
+import org.apache.seata.server.storage.file.session.FileSessionManager;
+import org.apache.seata.server.storage.file.spi.FileLockStore;
+import org.apache.seata.server.storage.file.spi.FileStoreRuntime;
+import org.apache.seata.server.storage.file.spi.SessionRecoveryConsumer;
+
+public class DefaultFileStoreRuntime implements FileStoreRuntime {
+
+ private final FileSessionManager sessionManager;
+ private final FileLockStore lockStore;
+ private State state = State.OPEN;
+ private boolean recoveryComplete;
+
+ DefaultFileStoreRuntime(FileSessionManager sessionManager, FileLockStore lockStore) {
+ this.sessionManager = sessionManager;
+ this.lockStore = lockStore;
+ }
+
+ @Override
+ public SessionManager sessionManager() {
+ return sessionManager;
+ }
+
+ @Override
+ public FileLockStore lockStore() {
+ return lockStore;
+ }
+
+ @Override
+ public synchronized void recover(SessionRecoveryConsumer recoveryConsumer) {
+ requireOpen();
+ if (recoveryComplete) {
+ throw new IllegalStateException("file store recovery already completed");
+ }
+ sessionManager.reload();
+ try {
+ recoveryConsumer.accept(sessionManager.allSessions());
+ recoveryComplete = true;
+ } catch (TransactionException e) {
+ throw new StoreException(e, "recover default file store sessions failed");
+ }
+ }
+
+ @Override
+ public synchronized void startBackgroundServices() {
+ requireOpen();
+ if (!recoveryComplete) {
+ throw new IllegalStateException("file store recovery must complete before background services start");
+ }
+ state = State.STARTING_BACKGROUND;
+ state = State.RUNNING;
+ }
+
+ @Override
+ public synchronized void close() {
+ if (state == State.CLOSING || state == State.CLOSED) {
+ return;
+ }
+ state = State.CLOSING;
+ try {
+ sessionManager.destroy();
Review Comment:
Closing the runtime destroys the session manager but leaves the default `FileLocker.LOCK_MAP` static map intact. On a same-JVM close/reopen, reacquiring the same transaction's key can leave the old `BranchSession` mapped without recording it in the new session's lock holder, so the new session's unlock cannot clear it. Could the default runtime clean up its lock store on close, with a same-JVM lifecycle test? This does not affect a normal process restart.
##########
server/src/main/java/org/apache/seata/server/session/SessionHolder.java:
##########
@@ -158,15 +173,80 @@ public static void init(SessionMode sessionMode) {
}
}
+ private static FileStoreRuntime openFileStoreRuntime() {
+ String engineName = StoreConfig.getFileEngineName();
+ FileStoreProvider provider = FileStoreProviderFactory.getProvider(engineName);
+ String sessionStoreDir = CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, DEFAULT_SESSION_STORE_FILE_DIR);
+ if (StringUtils.isBlank(sessionStoreDir)) {
+ throw new StoreException("the {store.file.dir} is empty.");
+ }
+ String sessionStorePath = sessionStoreDir + separator + XID.getPort();
+ return provider.open(new FileStoreContext(ROOT_SESSION_MANAGER_NAME, Paths.get(sessionStorePath)));
+ }
+
+ private static void initFileMode(FileStoreRuntime runtime) {
+ String vGroupMappingStoreDir =
+ CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, DEFAULT_VGROUP_MAPPING_STORE_FILE_DIR);
+ if (StringUtils.isBlank(vGroupMappingStoreDir)) {
+ throw new StoreException("the {store.file.dir} is empty.");
+ }
+ String vGroupMappingStorePath = vGroupMappingStoreDir + separator + XID.getPort();
+ VGroupMappingStoreManager vGroupMappingManager = EnhancedServiceLoader.load(
+ VGroupMappingStoreManager.class, SessionMode.FILE.getName(), new Object[] {vGroupMappingStorePath});
+ DistributedLocker distributedLocker = DistributedLockerFactory.getDistributedLocker(SessionMode.FILE.getName());
+ ROOT_VGROUP_MAPPING_MANAGER = vGroupMappingManager;
+ DISTRIBUTED_LOCKER = distributedLocker;
+ ROOT_SESSION_MANAGER = runtime.sessionManager();
+ runtime.recover(sessions -> reload(sessions, SessionMode.FILE, true, true));
Review Comment:
This recovery callback still treats a `false` from `branchSession.lock()` as success: `lockBranchSessions` discards the boolean and only fails on an exception. A restored session that cannot reacquire its lock can therefore pass recovery and let startup continue. This behavior predates the PR, but this lifecycle refactor is a good place to propagate failure and add a regression test.
##########
server/src/main/java/org/apache/seata/server/lock/LockerManagerFactory.java:
##########
@@ -49,28 +46,62 @@ public static LockManager getLockManager() {
return LOCK_MANAGER;
}
- public static void init() {
- init(null);
+ public static boolean init() {
Review Comment:
Changing these public methods from `void` to `boolean` changes their JVM descriptors from `()V`/`(LockMode)V` to `()Z`/`(LockMode)Z`. Existing extensions compiled against the old API can therefore fail with `NoSuchMethodError`, even if source callers that ignore the return value still compile. Please preserve the original `void` overloads and expose the installation result through a separately named API.
--
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: notifications-unsubscribe@seata.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org