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 : support least active load balance (apache#2676)
- Loading branch information
Showing
9 changed files
with
315 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* 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.common.rpc; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.concurrent.atomic.LongAdder; | ||
|
||
/** | ||
* The state statistics. | ||
* | ||
* @author ph3636 | ||
*/ | ||
public class RpcStatus { | ||
|
||
private static final ConcurrentMap<String, RpcStatus> SERVICE_STATUS_MAP = new ConcurrentHashMap<>(); | ||
private final AtomicLong active = new AtomicLong(); | ||
private final LongAdder total = new LongAdder(); | ||
|
||
private RpcStatus() { | ||
} | ||
|
||
/** | ||
* get the RpcStatus of this service | ||
* | ||
* @param service the service | ||
* @return RpcStatus | ||
*/ | ||
public static RpcStatus getStatus(String service) { | ||
return SERVICE_STATUS_MAP.computeIfAbsent(service, key -> new RpcStatus()); | ||
} | ||
|
||
/** | ||
* remove the RpcStatus of this service | ||
* | ||
* @param service the service | ||
*/ | ||
public static void removeStatus(String service) { | ||
SERVICE_STATUS_MAP.remove(service); | ||
} | ||
|
||
/** | ||
* begin count | ||
* | ||
* @param service the service | ||
*/ | ||
public static void beginCount(String service) { | ||
getStatus(service).active.incrementAndGet(); | ||
} | ||
|
||
/** | ||
* end count | ||
* | ||
* @param service the service | ||
*/ | ||
public static void endCount(String service) { | ||
RpcStatus rpcStatus = getStatus(service); | ||
rpcStatus.active.decrementAndGet(); | ||
rpcStatus.total.increment(); | ||
} | ||
|
||
/** | ||
* get active. | ||
* | ||
* @return active | ||
*/ | ||
public long getActive() { | ||
return active.get(); | ||
} | ||
|
||
/** | ||
* get total. | ||
* | ||
* @return total | ||
*/ | ||
public long getTotal() { | ||
return total.longValue(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
common/src/test/java/io/seata/common/rpc/RpcStatusTest.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,42 @@ | ||
package io.seata.common.rpc; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* The state statistics test. | ||
* | ||
* @author ph3636 | ||
*/ | ||
public class RpcStatusTest { | ||
|
||
public static final String SERVICE = "127.0.0.1:80"; | ||
|
||
@Test | ||
public void getStatus() { | ||
RpcStatus rpcStatus1 = RpcStatus.getStatus(SERVICE); | ||
Assertions.assertNotNull(rpcStatus1); | ||
RpcStatus rpcStatus2 = RpcStatus.getStatus(SERVICE); | ||
Assertions.assertEquals(rpcStatus1, rpcStatus2); | ||
} | ||
|
||
@Test | ||
public void removeStatus() { | ||
RpcStatus old = RpcStatus.getStatus(SERVICE); | ||
RpcStatus.removeStatus(SERVICE); | ||
Assertions.assertNotEquals(RpcStatus.getStatus(SERVICE), old); | ||
} | ||
|
||
@Test | ||
public void beginCount() { | ||
RpcStatus.beginCount(SERVICE); | ||
Assertions.assertEquals(RpcStatus.getStatus(SERVICE).getActive(), 1); | ||
} | ||
|
||
@Test | ||
public void endCount() { | ||
RpcStatus.endCount(SERVICE); | ||
Assertions.assertEquals(RpcStatus.getStatus(SERVICE).getActive(), 0); | ||
Assertions.assertEquals(RpcStatus.getStatus(SERVICE).getTotal(), 1); | ||
} | ||
} |
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,28 @@ | ||
/* | ||
* 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.core.rpc.hook; | ||
|
||
import io.seata.core.protocol.RpcMessage; | ||
|
||
/** | ||
* @author ph3636 | ||
*/ | ||
public interface RpcHook { | ||
|
||
void doBeforeRequest(String remoteAddr, RpcMessage request); | ||
|
||
void doAfterResponse(String remoteAddr, RpcMessage request, Object response); | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/main/java/io/seata/core/rpc/hook/StatusRpcHook.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,35 @@ | ||
/* | ||
* 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.core.rpc.hook; | ||
|
||
import io.seata.common.rpc.RpcStatus; | ||
import io.seata.core.protocol.RpcMessage; | ||
|
||
/** | ||
* @author ph3636 | ||
*/ | ||
public class StatusRpcHook implements RpcHook { | ||
|
||
@Override | ||
public void doBeforeRequest(String remoteAddr, RpcMessage request) { | ||
RpcStatus.beginCount(remoteAddr); | ||
} | ||
|
||
@Override | ||
public void doAfterResponse(String remoteAddr, RpcMessage request, Object response) { | ||
RpcStatus.endCount(remoteAddr); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
core/src/main/resources/META-INF/services/io.seata.core.rpc.hook.RpcHook
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 @@ | ||
io.seata.core.rpc.hook.StatusRpcHook |
53 changes: 53 additions & 0 deletions
53
...a-discovery-core/src/main/java/io/seata/discovery/loadbalance/LeastActiveLoadBalance.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,53 @@ | ||
/* | ||
* 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 java.util.List; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import io.seata.common.loader.LoadLevel; | ||
import io.seata.common.rpc.RpcStatus; | ||
|
||
/** | ||
* The type Least Active load balance. | ||
* | ||
* @author ph3636 | ||
*/ | ||
@LoadLevel(name = "LeastActiveLoadBalance") | ||
public class LeastActiveLoadBalance extends AbstractLoadBalance { | ||
|
||
@Override | ||
protected <T> T doSelect(List<T> invokers, String xid) { | ||
int length = invokers.size(); | ||
long leastActive = -1; | ||
int leastCount = 0; | ||
int[] leastIndexes = new int[length]; | ||
for (int i = 0; i < length; i++) { | ||
long active = RpcStatus.getStatus(invokers.get(i).toString()).getActive(); | ||
if (leastActive == -1 || active < leastActive) { | ||
leastActive = active; | ||
leastCount = 1; | ||
leastIndexes[0] = i; | ||
} else if (active == leastActive) { | ||
leastIndexes[leastCount++] = i; | ||
} | ||
} | ||
if (leastCount == 1) { | ||
return invokers.get(leastIndexes[0]); | ||
} | ||
return invokers.get(leastIndexes[ThreadLocalRandom.current().nextInt(leastCount)]); | ||
} | ||
} |
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
Oops, something went wrong.