Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.uniffle.client.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -86,7 +87,8 @@ public class ShuffleWriteClientImpl implements ShuffleWriteClient {
private int retryMax;
private long retryIntervalMax;
private List<CoordinatorClient> coordinatorClients = Lists.newLinkedList();
private Set<ShuffleServerInfo> shuffleServerInfoSet = Sets.newConcurrentHashSet();
//appId -> shuffleId -> servers
private Map<String, Map<Integer, Set<ShuffleServerInfo>>> shuffleServerInfoMap = Maps.newConcurrentMap();
private CoordinatorClientFactory coordinatorClientFactory;
private ExecutorService heartBeatExecutorService;
private int replica;
Expand Down Expand Up @@ -350,7 +352,7 @@ public void registerShuffle(
String msg = "Error happened when registerShuffle with appId[" + appId + "], shuffleId[" + shuffleId
+ "], " + shuffleServerInfo;
throwExceptionIfNecessary(response, msg);
shuffleServerInfoSet.add(shuffleServerInfo);
addShuffleServer(appId, shuffleId, shuffleServerInfo);
}

@Override
Expand Down Expand Up @@ -551,7 +553,8 @@ public Roaring64NavigableMap getShuffleResultForMultiPart(String clientType,
public void sendAppHeartbeat(String appId, long timeoutMs) {
RssAppHeartBeatRequest request = new RssAppHeartBeatRequest(appId, timeoutMs);
List<Callable<Void>> callableList = Lists.newArrayList();
shuffleServerInfoSet.stream().forEach(shuffleServerInfo -> {
Set<ShuffleServerInfo> allShuffleServers = getAllShuffleServers(appId);
allShuffleServers.forEach(shuffleServerInfo -> {
callableList.add(() -> {
try {
ShuffleServerClient client =
Expand Down Expand Up @@ -607,7 +610,16 @@ public void unregisterShuffle(String appId, int shuffleId) {
RssUnregisterShuffleRequest request = new RssUnregisterShuffleRequest(appId, shuffleId);
List<Callable<Void>> callableList = Lists.newArrayList();

shuffleServerInfoSet.stream().forEach(shuffleServerInfo -> {
Map<Integer, Set<ShuffleServerInfo>> appServerMap = shuffleServerInfoMap.get(appId);
if (appServerMap == null) {
return;
}
Set<ShuffleServerInfo> shuffleServerInfos = appServerMap.get(shuffleId);
if (shuffleServerInfos == null) {
return;
}

shuffleServerInfos.forEach(shuffleServerInfo -> {
callableList.add(() -> {
try {
ShuffleServerClient client =
Expand All @@ -628,7 +640,7 @@ public void unregisterShuffle(String appId, int shuffleId) {
try {
executorService =
Executors.newFixedThreadPool(
Math.min(unregisterThreadPoolSize, shuffleServerInfoSet.size()),
Math.min(unregisterThreadPoolSize, shuffleServerInfos.size()),
ThreadUtils.getThreadFactory("unregister-shuffle-%d")
);
List<Future<Void>> futures = executorService.invokeAll(callableList, unregisterRequestTimeSec, TimeUnit.SECONDS);
Expand All @@ -643,6 +655,7 @@ public void unregisterShuffle(String appId, int shuffleId) {
if (executorService != null) {
executorService.shutdownNow();
}
removeShuffleServer(appId, shuffleId);
}
}

Expand All @@ -653,9 +666,43 @@ private void throwExceptionIfNecessary(ClientResponse response, String errorMsg)
}
}

Set<ShuffleServerInfo> getAllShuffleServers(String appId) {
Map<Integer, Set<ShuffleServerInfo>> appServerMap = shuffleServerInfoMap.get(appId);
if (appServerMap == null) {
return Collections.EMPTY_SET;
}
Set<ShuffleServerInfo> serverInfos = Sets.newHashSet();
appServerMap.values().forEach((serverSet) -> {
serverInfos.addAll(serverSet);
});
return serverInfos;
}

@VisibleForTesting
public ShuffleServerClient getShuffleServerClient(ShuffleServerInfo shuffleServerInfo) {
return ShuffleServerClientFactory.getInstance().getShuffleServerClient(clientType, shuffleServerInfo);
}

@VisibleForTesting
void addShuffleServer(String appId, int shuffleId, ShuffleServerInfo serverInfo) {
Map<Integer, Set<ShuffleServerInfo>> appServerMap = shuffleServerInfoMap.get(appId);
if (appServerMap == null) {
appServerMap = Maps.newConcurrentMap();
shuffleServerInfoMap.put(appId, appServerMap);
}
Set<ShuffleServerInfo> shuffleServerInfos = appServerMap.get(shuffleId);
if (shuffleServerInfos == null) {
shuffleServerInfos = Sets.newConcurrentHashSet();
appServerMap.put(shuffleId, shuffleServerInfos);
}
shuffleServerInfos.add(serverInfo);
}

@VisibleForTesting
void removeShuffleServer(String appId, int shuffleId) {
Map<Integer, Set<ShuffleServerInfo>> appServerMap = shuffleServerInfoMap.get(appId);
if (appServerMap != null) {
appServerMap.remove(shuffleId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.uniffle.common.ShuffleBlockInfo;
import org.apache.uniffle.common.ShuffleServerInfo;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
Expand All @@ -56,4 +57,23 @@ public void testSendData() {

assertTrue(result.getFailedBlockIds().contains(10L));
}

@Test
public void testRegisterAndUnRegisterShuffleServer() {
ShuffleWriteClientImpl shuffleWriteClient =
new ShuffleWriteClientImpl("GRPC", 3, 2000, 4, 1, 1, 1, true, 1, 1, 10, 10);
String appId1 = "testRegisterAndUnRegisterShuffleServer-1";
String appId2 = "testRegisterAndUnRegisterShuffleServer-2";
ShuffleServerInfo server1 = new ShuffleServerInfo("host1-0", "host1", 0);
ShuffleServerInfo server2 = new ShuffleServerInfo("host2-0", "host2", 0);
ShuffleServerInfo server3 = new ShuffleServerInfo("host3-0", "host3", 0);
shuffleWriteClient.addShuffleServer(appId1, 0, server1);
shuffleWriteClient.addShuffleServer(appId1, 1, server2);
shuffleWriteClient.addShuffleServer(appId2, 1, server3);
assertEquals(2, shuffleWriteClient.getAllShuffleServers(appId1).size());
assertEquals(1, shuffleWriteClient.getAllShuffleServers(appId2).size());
shuffleWriteClient.addShuffleServer(appId1, 1, server1);
shuffleWriteClient.unregisterShuffle(appId1, 1);
assertEquals(1, shuffleWriteClient.getAllShuffleServers(appId1).size());
}
}