Skip to content

Commit

Permalink
feature: support xid load balance (apache#4713)
Browse files Browse the repository at this point in the history
  • Loading branch information
funky-eyes committed Jun 24, 2022
1 parent 48a7581 commit a22fafc
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changes/en-us/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ Add changes here for all PR submitted to the develop branch.
<!-- Please add the `changes` to the following location(feature/bugfix/optimize/test) based on the type of PR -->

### feature:
- [[#4661](https://github.com/seata/seata/pull/4713)] support xid load balance
- [[#4676](https://github.com/seata/seata/pull/4676)] support server to expose Nacos services by mounting SLB


### bugfix:
- [[#4515](https://github.com/seata/seata/pull/4515)] fix the error of SeataTCCFenceAutoConfiguration when database unused
Expand Down Expand Up @@ -36,6 +38,8 @@ Thanks to these contributors for their code commits. Please report an unintended
- [Ifdevil](https://github.com/Ifdevil)
- [wingchi-leung](https://github.com/wingchi-leung)
- [liurong](https://github.com/robynron)
- [a364176773](https://github.com/a364176773)
- [2129zxl](https://github.com/2129zxl)


Also, we receive many valuable issues, questions and advices from our community. Thanks for you all.
3 changes: 3 additions & 0 deletions changes/zh-cn/develop.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
<!-- 请根据PR的类型添加 `变更记录` 到以下对应位置(feature/bugfix/optimize/test) 下 -->

### feature:
- [[#4661](https://github.com/seata/seata/pull/4713)] 支持根据xid负载均衡算法
- [[#4676](https://github.com/seata/seata/pull/4676)] 支持Nacos作为注册中心时,server通过挂载SLB暴露服务


### bugfix:
- [[#4515](https://github.com/seata/seata/pull/4515)] 修复develop分支SeataTCCFenceAutoConfiguration在客户端未使用DB时,启动抛出ClassNotFoundException的问题。
- [[#4661](https://github.com/seata/seata/pull/4661)] 修复控制台中使用PostgreSQL出现的SQL异常
Expand Down Expand Up @@ -36,5 +38,6 @@
- [Ifdevil](https://github.com/Ifdevil)
- [wingchi-leung](https://github.com/wingchi-leung)
- [liurong](https://github.com/robynron)
- [a364176773](https://github.com/a364176773)

同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
2 changes: 1 addition & 1 deletion common/src/main/java/io/seata/common/DefaultValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public interface DefaultValues {

boolean DEFAULT_SERVER_ENABLE_CHECK_AUTH = true;

String DEFAULT_LOAD_BALANCE = "RandomLoadBalance";
String DEFAULT_LOAD_BALANCE = "XID";
int VIRTUAL_NODES_DEFAULT = 10;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public class LoadBalanceFactory {

public static final String LOAD_BALANCE_TYPE = LOAD_BALANCE_PREFIX + "type";

public static final String RANDOM_LOAD_BALANCE = DEFAULT_LOAD_BALANCE;
public static final String RANDOM_LOAD_BALANCE = "RandomLoadBalance";

public static final String XID_LOAD_BALANCE = "XID";

public static final String ROUND_ROBIN_LOAD_BALANCE = "RoundRobinLoadBalance";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 1999-2019 Seata.io Group.
*
* Licensed 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 io.seata.discovery.loadbalance;

import static io.seata.discovery.loadbalance.LoadBalanceFactory.XID_LOAD_BALANCE;


import java.net.InetSocketAddress;
import java.util.List;
import java.util.Objects;

import io.seata.common.loader.EnhancedServiceLoader;
import io.seata.common.loader.LoadLevel;
import io.seata.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The type xid load balance.
*
* @author funkye
*/
@LoadLevel(name = XID_LOAD_BALANCE)
public class XIDLoadBalance implements LoadBalance {

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

private static final LoadBalance RANDOM_LOAD_BALANCE = EnhancedServiceLoader.load(LoadBalance.class,
LoadBalanceFactory.RANDOM_LOAD_BALANCE);

private static final String SPLIT = ":";

@Override
public <T> T select(List<T> invokers, String xid) throws Exception {
if (StringUtils.isNotBlank(xid) && xid.contains(SPLIT)) {
// ip:port:transactionId -> ip:port
String serverAddress = xid.substring(0, xid.lastIndexOf(SPLIT));
// ip:port -> port
int index = serverAddress.lastIndexOf(SPLIT);
int port = Integer.parseInt(serverAddress.substring(index + 1));
// ipv4/v6
String ip = serverAddress.substring(0, index);
InetSocketAddress xidInetSocketAddress = new InetSocketAddress(ip, port);
for (T invoker : invokers) {
InetSocketAddress inetSocketAddress = (InetSocketAddress)invoker;
if (Objects.equals(xidInetSocketAddress, inetSocketAddress)) {
return (T)inetSocketAddress;
}
}
LOGGER.error("not found seata-server channel,xid: {}, try use random load balance", xid);
}
return RANDOM_LOAD_BALANCE.select(invokers, xid);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
io.seata.discovery.loadbalance.RoundRobinLoadBalance
io.seata.discovery.loadbalance.RandomLoadBalance
io.seata.discovery.loadbalance.ConsistentHashLoadBalance
io.seata.discovery.loadbalance.LeastActiveLoadBalance
io.seata.discovery.loadbalance.LeastActiveLoadBalance
io.seata.discovery.loadbalance.XIDLoadBalance
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ public void testRoundRobinLoadBalance_select(List<InetSocketAddress> addresses)
}
}

/**
* Test xid load load balance select.
*
* @param addresses the addresses
*/
@ParameterizedTest
@MethodSource("addressProvider")
public void testXIDLoadBalance_select(List<InetSocketAddress> addresses) throws Exception {
XIDLoadBalance loadBalance = new XIDLoadBalance();
// ipv4
InetSocketAddress inetSocketAddress = loadBalance.select(addresses, "127.0.0.1:8092:123456");
Assertions.assertNotNull(inetSocketAddress);
// ipv6
inetSocketAddress = loadBalance.select(addresses, "2000:0000:0000:0000:0001:2345:6789:abcd:8092:123456");
Assertions.assertNotNull(inetSocketAddress);
// test not found tc channel
inetSocketAddress = loadBalance.select(addresses, "127.0.0.1:8199:123456");
Assertions.assertNotEquals(inetSocketAddress.getPort(),8199);
}

/**
* Test consistent hash load load balance select.
*
Expand Down Expand Up @@ -156,7 +176,8 @@ static Stream<List<InetSocketAddress>> addressProvider() {
new InetSocketAddress("127.0.0.1", 8092),
new InetSocketAddress("127.0.0.1", 8093),
new InetSocketAddress("127.0.0.1", 8094),
new InetSocketAddress("127.0.0.1", 8095))
new InetSocketAddress("127.0.0.1", 8095),
new InetSocketAddress("2000:0000:0000:0000:0001:2345:6789:abcd", 8092))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void testUndoProperties() {

@Test
public void testLoadBalanceProperties() {
assertEquals("RandomLoadBalance", context.getBean(LoadBalanceProperties.class).getType());
assertEquals("XID", context.getBean(LoadBalanceProperties.class).getType());
assertEquals(10, context.getBean(LoadBalanceProperties.class).getVirtualNodes());
}

Expand Down

0 comments on commit a22fafc

Please sign in to comment.