Skip to content
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

Add seata-solon-plugin adaptation (draft) #6952

Draft
wants to merge 20 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
edd2042
Add `seata-solon-plugin` adaptation (draft)
noear Oct 25, 2024
a9bc60e
Add `seata-solon-plugin` adaptation (draft)
noear Oct 25, 2024
64df747
seata-solon-plugin: Add some comments
noear Oct 25, 2024
018cc9b
Merge branch '2.x' into 2.x
slievrly Oct 26, 2024
00724e2
seata-solon-plugin: Adding support for solon RoutingDataSource
noear Oct 26, 2024
be49b43
seata-solon-plugin: use 3.0.3-SNAPSHOT
noear Oct 26, 2024
3e87a2b
seata-solon-plugin: Move to the 'integration' directory
noear Oct 26, 2024
cbd2f17
Merge branch '2.x' into 2.x
lovepoem Oct 27, 2024
d720830
seata-solon-plugin: Folder renamed solon-plugin to keep with dubbo li…
noear Oct 28, 2024
bc03d81
seata-solon-plugin: Folder renamed solon-plugin to keep with dubbo li…
noear Oct 28, 2024
25fa9a4
seata-solon-plugin: add simple demo
noear Oct 28, 2024
9967cd4
Merge branch '2.x' of https://github.com/noear/seata into 2.x
noear Oct 28, 2024
7e9f3e0
seata-solon-plugin: add properties class for client
noear Oct 28, 2024
3cf0e6d
seata-solon-plugin: Adjust the configuration loading handling
noear Oct 28, 2024
954c869
seata-solon-plugin: add SolonConfigurationProvider and more propertie…
noear Oct 28, 2024
7750221
seata-solon-plugin: Adjust the configuration loading order
noear Oct 28, 2024
0a26dbd
seata-solon-plugin: Add some unit tests
noear Oct 30, 2024
ed37484
seata-solon-plugin: Adjusted some unit tests
noear Nov 6, 2024
23e0cb1
Add class license header
noear Nov 8, 2024
7489479
seata-solon-plugin: Adjusted some unit tests
noear Nov 8, 2024
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
109 changes: 109 additions & 0 deletions integration/solon-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

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.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.seata</groupId>
<artifactId>seata-parent</artifactId>
<version>${revision}</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>seata-solon-plugin</artifactId>
<packaging>jar</packaging>
<name>seata-solon-plugin ${project.version}</name>
<description>solon-plugin for Seata built with Maven</description>

<properties>
<solon.version>3.0.3-SNAPSHOT</solon.version>
</properties>

<dependencies>
<!--seata-->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-tm</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>seata-integration-tx-api</artifactId>
<version>${project.version}</version>
</dependency>

<!--solon-->
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-data</artifactId>
<version>${solon.version}</version>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-net-httputils</artifactId>
<version>${solon.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>nami</artifactId>
<version>${solon.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-boot-jdkhttp</artifactId>
<version>${solon.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-test</artifactId>
<version>${solon.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
* 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.solon.annotation;

import org.apache.seata.common.util.StringUtils;
import org.apache.seata.config.CachedConfigurationChangeListener;
import org.apache.seata.config.ConfigurationChangeEvent;
import org.apache.seata.config.ConfigurationFactory;
import org.apache.seata.core.constants.ConfigurationKeys;
import org.apache.seata.core.rpc.ShutdownHook;
import org.apache.seata.core.rpc.netty.RmNettyRemotingClient;
import org.apache.seata.core.rpc.netty.TmNettyRemotingClient;
import org.apache.seata.rm.RMClient;
import org.apache.seata.tm.TMClient;
import org.apache.seata.tm.api.FailureHandler;
import org.apache.seata.tm.api.FailureHandlerHolder;
import org.noear.solon.core.bean.LifecycleBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.apache.seata.common.DefaultValues.*;

/**
* The type Global transaction lifecycle (old: The type Global transaction scanner.)
*
*/
public class GlobalTransactionLifecycle implements CachedConfigurationChangeListener, LifecycleBean {

private static final long serialVersionUID = 1L;

private static final Logger LOGGER = LoggerFactory.getLogger(GlobalTransactionLifecycle.class);

private static final int AT_MODE = 1;
private static final int MT_MODE = 2;

private static final int ORDER_NUM = 1024;
private static final int DEFAULT_MODE = AT_MODE + MT_MODE;

private final String applicationId;
private final String txServiceGroup;
private static String accessKey;
private static String secretKey;
private volatile boolean disableGlobalTransaction = ConfigurationFactory.getInstance().getBoolean(
ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, DEFAULT_DISABLE_GLOBAL_TRANSACTION);
private final AtomicBoolean initialized = new AtomicBoolean(false);

private final FailureHandler failureHandlerHook;


/**
* Instantiates a new Global transaction scanner.
*
* @param applicationId the application id
* @param txServiceGroup the tx service group
* @param exposeProxy the exposeProxy
* @param failureHandlerHook the failure handler hook
*/
public GlobalTransactionLifecycle(String applicationId, String txServiceGroup, boolean exposeProxy,
FailureHandler failureHandlerHook) {
this(applicationId, txServiceGroup, DEFAULT_MODE, exposeProxy, failureHandlerHook);
}

/**
* Instantiates a new Global transaction scanner.
*
* @param applicationId the application id
* @param txServiceGroup the tx service group
* @param mode the mode
* @param exposeProxy the exposeProxy
* @param failureHandlerHook the failure handler hook
*/
public GlobalTransactionLifecycle(String applicationId, String txServiceGroup, int mode, boolean exposeProxy,
FailureHandler failureHandlerHook) {
this.applicationId = applicationId;
this.txServiceGroup = txServiceGroup;
this.failureHandlerHook = failureHandlerHook;
FailureHandlerHolder.setFailureHandler(this.failureHandlerHook);
}

/**
* Sets access key.
*
* @param accessKey the access key
*/
public static void setAccessKey(String accessKey) {
GlobalTransactionLifecycle.accessKey = accessKey;
}

/**
* Sets secret key.
*
* @param secretKey the secret key
*/
public static void setSecretKey(String secretKey) {
GlobalTransactionLifecycle.secretKey = secretKey;
}


protected void initClient() {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Initializing Global Transaction Clients ... ");
}
if (DEFAULT_TX_GROUP_OLD.equals(txServiceGroup)) {
LOGGER.warn("the default value of seata.tx-service-group: {} has already changed to {} since Seata 1.5, " +
"please change your default configuration as soon as possible " +
"and we don't recommend you to use default tx-service-group's value provided by seata",
DEFAULT_TX_GROUP_OLD, DEFAULT_TX_GROUP);
}
if (StringUtils.isNullOrEmpty(applicationId) || StringUtils.isNullOrEmpty(txServiceGroup)) {
throw new IllegalArgumentException(String.format("applicationId: %s, txServiceGroup: %s", applicationId, txServiceGroup));
}
//init TM
TMClient.init(applicationId, txServiceGroup, accessKey, secretKey);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Transaction Manager Client is initialized. applicationId[{}] txServiceGroup[{}]", applicationId, txServiceGroup);
}
//init RM
RMClient.init(applicationId, txServiceGroup);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Resource Manager is initialized. applicationId[{}] txServiceGroup[{}]", applicationId, txServiceGroup);
}

if (LOGGER.isInfoEnabled()) {
LOGGER.info("Global Transaction Clients are initialized. ");
}
registerSpringShutdownHook();

}

protected void registerSpringShutdownHook() {
ShutdownHook.getInstance().addDisposable(TmNettyRemotingClient.getInstance(applicationId, txServiceGroup, accessKey, secretKey));
ShutdownHook.getInstance().addDisposable(RmNettyRemotingClient.getInstance(applicationId, txServiceGroup));
}


@Override
public void start() {
if (disableGlobalTransaction) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Global transaction is disabled.");
}
ConfigurationFactory.getInstance().addConfigListener(ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, (CachedConfigurationChangeListener) this);
return;
}

if (initialized.compareAndSet(false, true)) {
initClient();
}
}

@Override
public void stop() {
ShutdownHook.getInstance().destroyAll();
}


@Override
public void onChangeEvent(ConfigurationChangeEvent event) {
if (ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION.equals(event.getDataId())) {
disableGlobalTransaction = Boolean.parseBoolean(event.getNewValue().trim());
if (!disableGlobalTransaction && initialized.compareAndSet(false, true)) {
LOGGER.info("{} config changed, old value:true, new value:{}", ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION,
event.getNewValue());
initClient();
ConfigurationFactory.getInstance().removeConfigListener(ConfigurationKeys.DISABLE_GLOBAL_TRANSACTION, this);
}
}
}

public String getApplicationId() {
return applicationId;
}

public String getTxServiceGroup() {
return txServiceGroup;
}

public static String getAccessKey() {
return accessKey;
}

public static String getSecretKey() {
return secretKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.solon.annotation;

import org.apache.seata.integration.tx.api.interceptor.InvocationWrapper;
import org.apache.seata.integration.tx.api.interceptor.handler.GlobalTransactionalInterceptorHandler;
import org.apache.seata.integration.tx.api.interceptor.handler.ProxyInvocationHandler;
import org.apache.seata.tm.api.FailureHandlerHolder;
import org.noear.solon.core.aspect.Invocation;
import org.noear.solon.core.aspect.MethodInterceptor;

import java.util.HashSet;
import java.util.Set;

/**
* GlobalTransactional annotation interceptor
*
* @author noear 2024/10/25 created
*/
public class GlobalTransactionalInterceptor implements MethodInterceptor {
private final Set<String> methodsToProxy = new HashSet<>();
@Override
public Object doIntercept(Invocation inv) throws Throwable {
InvocationWrapper invocationWrapper = new SolonInvocationWrapper(inv);

return this.createProxyInvocationHandler().invoke(invocationWrapper);
}

protected ProxyInvocationHandler createProxyInvocationHandler() {
return new GlobalTransactionalInterceptorHandler(FailureHandlerHolder.getFailureHandler(), methodsToProxy);
}
}
Loading