Skip to content

feature: add TCC three-phase hooks #6766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6226](https://github.com/apache/incubator-seata/pull/6226)] multi-version seata protocol support
- [[#6537](https://github.com/apache/incubator-seata/pull/6537)] support Namingserver
- [[#6538](https://github.com/apache/incubator-seata/pull/6538)] Integration of naming server on the Seata server side
- [[#6766](https://github.com/apache/incubator-seata/pull/6766)] add TCC three-phase hooks

### bugfix:
- [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async annotation not working in ClusterWatcherManager
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [[#6226](https://github.com/apache/incubator-seata/pull/6226)] 支持seata私有协议多版本兼容
- [[#6537](https://github.com/apache/incubator-seata/pull/6537)] 支持 Namingserver
- [[#6538](https://github.com/apache/incubator-seata/pull/6538)] seata server端集成naming server
- [[#6766](https://github.com/apache/incubator-seata/pull/6766)] 添加tcc三阶段钩子函数,方便用户拓展业务逻辑(比如多数据源情况下可以用于切换数据源)

### bugfix:
- [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async注解ClusterWatcherManager中不生效的问题
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 org.apache.seata.rm.tcc.api.BusinessActionContext;

public interface TccHook {

/**
* before tcc prepare
*/
void beforeTccPrepare(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc prepare
*/
void afterTccPrepare(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* before tcc commit
*/
void beforeTccCommit(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc commit
*/
void afterTccCommit(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* before tcc rollback
*/
void beforeTccRollback(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc rollback
*/
void afterTccRollback(String xid, Long branchId, String actionName, BusinessActionContext context);
}
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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.seata.common.Constants;
Expand All @@ -32,6 +33,8 @@
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.core.context.RootContext;
import org.apache.seata.integration.tx.api.fence.DefaultCommonFenceHandler;
import org.apache.seata.integration.tx.api.fence.hook.TccHook;
import org.apache.seata.integration.tx.api.fence.hook.TccHookManager;
import org.apache.seata.integration.tx.api.util.JsonUtil;
import org.apache.seata.rm.DefaultResourceManager;
import org.apache.seata.rm.tcc.api.BusinessActionContext;
Expand Down Expand Up @@ -87,7 +90,7 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
try {
//share actionContext implicitly
BusinessActionContextUtil.setContext(actionContext);

doBeforeTccPrepare(xid, branchId, actionName, actionContext);
if (businessActionParam.getUseCommonFence()) {
try {
// Use common Fence, and return the business result
Expand All @@ -105,6 +108,7 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
}
} finally {
try {
doAfterTccPrepare(xid, branchId, actionName, actionContext);
//to report business action context finally if the actionContext.getUpdated() is true
BusinessActionContextUtil.reportContext(actionContext);
} finally {
Expand All @@ -119,6 +123,40 @@ public Object proceed(Method method, Object[] arguments, String xid, TwoPhaseBus
}
}

/**
* to do some business operations before tcc prepare
* @param xid the xid
* @param branchId the branchId
* @param actionName the actionName
* @param context the business action context
*/
private void doBeforeTccPrepare(String xid, String branchId, String actionName, BusinessActionContext context) {
List<TccHook> hooks = TccHookManager.getHooks();
if (hooks.isEmpty()) {
return;
}
for (TccHook hook : hooks) {
hook.beforeTccPrepare(xid, Long.valueOf(branchId), actionName, context);
}
}

/**
* to do some business operations after tcc prepare
* @param xid the xid
* @param branchId the branchId
* @param actionName the actionName
* @param context the business action context
*/
private void doAfterTccPrepare(String xid, String branchId, String actionName, BusinessActionContext context) {
List<TccHook> hooks = TccHookManager.getHooks();
if (hooks.isEmpty()) {
return;
}
for (TccHook hook : hooks) {
hook.afterTccPrepare(xid, Long.valueOf(branchId), actionName, context);
}
}

/**
* Get or create action context, and reset to arguments
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;


public class TccHookManagerTest {

@BeforeEach
public void setUp() {
TccHookManager.clear();
}

@Test
public void testRegisterHook() {
TccHook hook = mock(TccHook.class);
TccHookManager.registerHook(hook);

List<TccHook> hooks = TccHookManager.getHooks();
assertEquals(1, hooks.size());
assertTrue(hooks.contains(hook));
}

@Test
public void testClear() {
TccHook hook = mock(TccHook.class);
TccHookManager.registerHook(hook);
List<TccHook> hooks = TccHookManager.getHooks();
assertEquals(1, hooks.size());
assertTrue(hooks.contains(hook));

TccHookManager.clear();

assertTrue(TccHookManager.getHooks().isEmpty());
}

@Test
public void testGetHooks() {
TccHook hook1 = mock(TccHook.class);
TccHook hook2 = mock(TccHook.class);
TccHookManager.registerHook(hook1);
TccHookManager.registerHook(hook2);

List<TccHook> hooks = TccHookManager.getHooks();
assertEquals(2, hooks.size());
assertTrue(hooks.contains(hook1));
assertTrue(hooks.contains(hook2));

// Check unmodifiable list
assertThrows(UnsupportedOperationException.class, () -> hooks.add(mock(TccHook.class)));
}
}
Loading
Loading