|
|
@ -20,11 +20,11 @@ package org.apache.dolphinscheduler.server.master.dispatch.host; |
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
import org.apache.dolphinscheduler.common.utils.HeartBeat; |
|
|
|
import org.apache.dolphinscheduler.common.utils.HeartBeat; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.Host; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.Host; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.NamedThreadFactory; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.context.ExecutionContext; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.context.ExecutionContext; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.host.assign.HostWeight; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.host.assign.HostWeight; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.host.assign.HostWorker; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.host.assign.HostWorker; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.host.assign.LowerWeightRoundRobin; |
|
|
|
import org.apache.dolphinscheduler.server.master.dispatch.host.assign.LowerWeightRoundRobin; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.server.master.registry.WorkerInfoChangeListener; |
|
|
|
import org.apache.dolphinscheduler.spi.utils.StringUtils; |
|
|
|
import org.apache.dolphinscheduler.spi.utils.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.collections.CollectionUtils; |
|
|
|
import org.apache.commons.collections.CollectionUtils; |
|
|
@ -36,14 +36,10 @@ import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
import java.util.concurrent.locks.Lock; |
|
|
|
import java.util.concurrent.locks.Lock; |
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import javax.annotation.PostConstruct; |
|
|
|
import javax.annotation.PreDestroy; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
@ -70,23 +66,12 @@ public class LowerWeightHostManager extends CommonHostManager { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private Lock lock; |
|
|
|
private Lock lock; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* executor service |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private ScheduledExecutorService executorService; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
@PostConstruct |
|
|
|
public void init() { |
|
|
|
public void init() { |
|
|
|
this.selector = new LowerWeightRoundRobin(); |
|
|
|
this.selector = new LowerWeightRoundRobin(); |
|
|
|
this.workerHostWeightsMap = new ConcurrentHashMap<>(); |
|
|
|
this.workerHostWeightsMap = new ConcurrentHashMap<>(); |
|
|
|
this.lock = new ReentrantLock(); |
|
|
|
this.lock = new ReentrantLock(); |
|
|
|
this.executorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("LowerWeightHostManagerExecutor")); |
|
|
|
serverNodeManager.addWorkerInfoChangeListener(new WorkerWeightListener()); |
|
|
|
this.executorService.scheduleWithFixedDelay(new RefreshResourceTask(), 0, 1, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PreDestroy |
|
|
|
|
|
|
|
public void close() { |
|
|
|
|
|
|
|
this.executorService.shutdownNow(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -109,42 +94,32 @@ public class LowerWeightHostManager extends CommonHostManager { |
|
|
|
throw new UnsupportedOperationException("not support"); |
|
|
|
throw new UnsupportedOperationException("not support"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void syncWorkerHostWeight(Map<String, Set<HostWeight>> workerHostWeights) { |
|
|
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
workerHostWeightsMap.clear(); |
|
|
|
|
|
|
|
workerHostWeightsMap.putAll(workerHostWeights); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Set<HostWeight> getWorkerHostWeights(String workerGroup) { |
|
|
|
private class WorkerWeightListener implements WorkerInfoChangeListener { |
|
|
|
lock.lock(); |
|
|
|
@Override |
|
|
|
try { |
|
|
|
public void notify(Map<String, Set<String>> workerGroups, Map<String, String> workerNodeInfo) { |
|
|
|
return workerHostWeightsMap.get(workerGroup); |
|
|
|
syncWorkerResources(workerGroups, workerNodeInfo); |
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
class RefreshResourceTask implements Runnable { |
|
|
|
/** |
|
|
|
|
|
|
|
* Sync worker resource. |
|
|
|
@Override |
|
|
|
* |
|
|
|
public void run() { |
|
|
|
* @param workerGroupNodes worker group nodes, key is worker group, value is worker group nodes. |
|
|
|
|
|
|
|
* @param workerNodeInfoMap worker node info map, key is worker node, value is worker info. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void syncWorkerResources(final Map<String, Set<String>> workerGroupNodes, |
|
|
|
|
|
|
|
final Map<String, String> workerNodeInfoMap) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
Map<String, Set<HostWeight>> workerHostWeights = new HashMap<>(); |
|
|
|
Map<String, Set<HostWeight>> workerHostWeights = new HashMap<>(); |
|
|
|
Map<String, Set<String>> workerGroupNodes = serverNodeManager.getWorkerGroupNodes(); |
|
|
|
|
|
|
|
for (Map.Entry<String, Set<String>> entry : workerGroupNodes.entrySet()) { |
|
|
|
for (Map.Entry<String, Set<String>> entry : workerGroupNodes.entrySet()) { |
|
|
|
String workerGroup = entry.getKey(); |
|
|
|
String workerGroup = entry.getKey(); |
|
|
|
Set<String> nodes = entry.getValue(); |
|
|
|
Set<String> nodes = entry.getValue(); |
|
|
|
Set<HostWeight> hostWeights = new HashSet<>(nodes.size()); |
|
|
|
Set<HostWeight> hostWeights = new HashSet<>(nodes.size()); |
|
|
|
for (String node : nodes) { |
|
|
|
for (String node : nodes) { |
|
|
|
String heartbeat = serverNodeManager.getWorkerNodeInfo(node); |
|
|
|
String heartbeat = workerNodeInfoMap.getOrDefault(node, null); |
|
|
|
Optional<HostWeight> hostWeightOpt = getHostWeight(node, workerGroup, heartbeat); |
|
|
|
Optional<HostWeight> hostWeightOpt = getHostWeight(node, workerGroup, heartbeat); |
|
|
|
if (hostWeightOpt.isPresent()) { |
|
|
|
hostWeightOpt.ifPresent(hostWeights::add); |
|
|
|
hostWeights.add(hostWeightOpt.get()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (!hostWeights.isEmpty()) { |
|
|
|
if (!hostWeights.isEmpty()) { |
|
|
|
workerHostWeights.put(workerGroup, hostWeights); |
|
|
|
workerHostWeights.put(workerGroup, hostWeights); |
|
|
@ -152,11 +127,11 @@ public class LowerWeightHostManager extends CommonHostManager { |
|
|
|
} |
|
|
|
} |
|
|
|
syncWorkerHostWeight(workerHostWeights); |
|
|
|
syncWorkerHostWeight(workerHostWeights); |
|
|
|
} catch (Throwable ex) { |
|
|
|
} catch (Throwable ex) { |
|
|
|
logger.error("RefreshResourceTask error", ex); |
|
|
|
logger.error("Sync worker resource error", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Optional<HostWeight> getHostWeight(String addr, String workerGroup, String heartBeatInfo) { |
|
|
|
private Optional<HostWeight> getHostWeight(String addr, String workerGroup, String heartBeatInfo) { |
|
|
|
if (StringUtils.isEmpty(heartBeatInfo)) { |
|
|
|
if (StringUtils.isEmpty(heartBeatInfo)) { |
|
|
|
logger.warn("worker {} in work group {} have not received the heartbeat", addr, workerGroup); |
|
|
|
logger.warn("worker {} in work group {} have not received the heartbeat", addr, workerGroup); |
|
|
|
return Optional.empty(); |
|
|
|
return Optional.empty(); |
|
|
@ -180,6 +155,25 @@ public class LowerWeightHostManager extends CommonHostManager { |
|
|
|
heartBeat.getCpuUsage(), heartBeat.getMemoryUsage(), heartBeat.getLoadAverage(), |
|
|
|
heartBeat.getCpuUsage(), heartBeat.getMemoryUsage(), heartBeat.getLoadAverage(), |
|
|
|
heartBeat.getWorkerWaitingTaskCount(), heartBeat.getStartupTime())); |
|
|
|
heartBeat.getWorkerWaitingTaskCount(), heartBeat.getStartupTime())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void syncWorkerHostWeight(Map<String, Set<HostWeight>> workerHostWeights) { |
|
|
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
workerHostWeightsMap.clear(); |
|
|
|
|
|
|
|
workerHostWeightsMap.putAll(workerHostWeights); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Set<HostWeight> getWorkerHostWeights(String workerGroup) { |
|
|
|
|
|
|
|
lock.lock(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return workerHostWeightsMap.get(workerGroup); |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|