forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add TCC three-phase hooks (apache#6731)
- Loading branch information
chengliefeng
committed
Aug 23, 2024
1 parent
5601c36
commit 135f273
Showing
4 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
integration-tx-api/src/main/java/org/apache/seata/integration/tx/api/fence/hook/TccHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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.integration.tx.api.fence.hook; | ||
|
||
|
||
public interface TccHook { | ||
|
||
/** | ||
* before tcc prepare | ||
*/ | ||
void beforeTccPrepare(String xid, Long branchId, String actionName); | ||
|
||
/** | ||
* after tcc prepare | ||
*/ | ||
void afterTccPrepare(String xid, Long branchId, String actionName); | ||
|
||
/** | ||
* before tcc commit | ||
*/ | ||
void beforeTccCommit(String xid, Long branchId, String actionName); | ||
|
||
/** | ||
* after tcc commit | ||
*/ | ||
void afterTccCommit(String xid, Long branchId, String actionName); | ||
|
||
/** | ||
* before tcc rollback | ||
*/ | ||
void beforeTccRollback(String xid, Long branchId, String actionName); | ||
|
||
/** | ||
* after tcc rollback | ||
*/ | ||
void afterTccRollback(String xid, Long branchId, String actionName); | ||
} |
73 changes: 73 additions & 0 deletions
73
...n-tx-api/src/main/java/org/apache/seata/integration/tx/api/fence/hook/TccHookManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.integration.tx.api.fence.hook; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class TccHookManager { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(TccHookManager.class); | ||
|
||
private TccHookManager() { | ||
|
||
} | ||
|
||
private static final List<TccHook> TCC_HOOKS = new CopyOnWriteArrayList<>(); | ||
// Cache unmodifiable lists | ||
private volatile static List<TccHook> CACHED_UNMODIFIABLE_HOOKS = null; | ||
|
||
/** | ||
* get the hooks | ||
* @return tccHook list | ||
*/ | ||
public static List<TccHook> getHooks() { | ||
if (CACHED_UNMODIFIABLE_HOOKS == null) { | ||
synchronized (TccHookManager.class) { | ||
if (CACHED_UNMODIFIABLE_HOOKS == null) { | ||
CACHED_UNMODIFIABLE_HOOKS = Collections.unmodifiableList(TCC_HOOKS); | ||
} | ||
} | ||
} | ||
return CACHED_UNMODIFIABLE_HOOKS; | ||
} | ||
|
||
/** | ||
* add new hook | ||
* @param tccHook tccHook | ||
*/ | ||
public static void registerHook(TccHook tccHook) { | ||
if (tccHook == null) { | ||
throw new NullPointerException("tccHook must not be null"); | ||
} | ||
TCC_HOOKS.add(tccHook); | ||
CACHED_UNMODIFIABLE_HOOKS = null; | ||
LOGGER.info("TccHook registered succeeded! TccHooks size: {}", TCC_HOOKS.size()); | ||
} | ||
|
||
/** | ||
* clear hooks | ||
*/ | ||
public static void clear() { | ||
TCC_HOOKS.clear(); | ||
CACHED_UNMODIFIABLE_HOOKS = null; | ||
LOGGER.info("All TccHooks have been cleared."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters