forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: support xid load balance (apache#4713)
- Loading branch information
1 parent
48a7581
commit a22fafc
Showing
8 changed files
with
104 additions
and
5 deletions.
There are no files selected for viewing
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
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
68 changes: 68 additions & 0 deletions
68
...ery/seata-discovery-core/src/main/java/io/seata/discovery/loadbalance/XIDLoadBalance.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,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); | ||
} | ||
|
||
} |
3 changes: 2 additions & 1 deletion
3
...very-core/src/main/resources/META-INF/services/io.seata.discovery.loadbalance.LoadBalance
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 |
---|---|---|
@@ -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 |
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