Skip to content

Commit

Permalink
Add worker-group-refresh-interval in master config (#12601)
Browse files Browse the repository at this point in the history
* Add worker-group-refresh-interval in master config

* Set interval cannot smaller than 10s

* Update dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/config/MasterConfig.java

Co-authored-by: kezhenxu94 <kezhenxu94@apache.org>
  • Loading branch information
ruanwenjun and kezhenxu94 authored Oct 31, 2022
1 parent d84f1ef commit e6da1cc
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 86 deletions.
1 change: 1 addition & 0 deletions docs/docs/en/architecture/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ Location: `master-server/conf/application.yaml`
|master.kill-yarn-job-when-task-failover|true|whether to kill yarn job when failover taskInstance|
|master.registry-disconnect-strategy.strategy|stop|Used when the master disconnect from registry, default value: stop. Optional values include stop, waiting|
|master.registry-disconnect-strategy.max-waiting-time|100s|Used when the master disconnect from registry, and the disconnect strategy is waiting, this config means the master will waiting to reconnect to registry in given times, and after the waiting times, if the master still cannot connect to registry, will stop itself, if the value is 0s, the Master will waitting infinitely|
|master.worker-group-refresh-interval|10s|The interval to refresh worker group from db to memory|

### Worker Server related configuration

Expand Down
1 change: 1 addition & 0 deletions docs/docs/zh/architecture/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ common.properties配置文件目前主要是配置hadoop/s3/yarn相关的配置
|master.kill-yarn-job-when-task-failover|true|当任务实例failover时,是否kill掉yarn job|
|master.registry-disconnect-strategy.strategy|stop|当Master与注册中心失联之后采取的策略, 默认值是: stop. 可选值包括: stop, waiting|
|master.registry-disconnect-strategy.max-waiting-time|100s|当Master与注册中心失联之后重连时间, 之后当strategy为waiting时,该值生效。 该值表示当Master与注册中心失联时会在给定时间之内进行重连, 在给定时间之内重连失败将会停止自己,在重连时,Master会丢弃目前正在执行的工作流,值为0表示会无限期等待 |
|master.master.worker-group-refresh-interval|10s|定期将workerGroup从数据库中同步到内存的时间间隔|

## Worker Server相关配置

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public class MasterConfig implements Validator {
private boolean killYarnJobWhenTaskFailover = true;
private ConnectStrategyProperties registryDisconnectStrategy = new ConnectStrategyProperties();

private Duration workerGroupRefreshInterval = Duration.ofSeconds(10L);

// ip:listenPort
private String masterAddress;

Expand Down Expand Up @@ -140,6 +142,10 @@ public void validate(Object target, Errors errors) {
if (masterConfig.getMaxCpuLoadAvg() <= 0) {
masterConfig.setMaxCpuLoadAvg(Runtime.getRuntime().availableProcessors() * 2);
}
if (masterConfig.getWorkerGroupRefreshInterval().getSeconds() < 10) {
errors.rejectValue("worker-group-refresh-interval", null, "should >= 10s");
}

masterConfig.setMasterAddress(NetUtils.getAddr(masterConfig.getListenPort()));
masterConfig.setMasterRegistryPath(REGISTRY_DOLPHINSCHEDULER_MASTERS + "/" + masterConfig.getMasterAddress());
printConfig();
Expand All @@ -163,5 +169,6 @@ private void printConfig() {
logger.info("Master config: registryDisconnectStrategy -> {} ", registryDisconnectStrategy);
logger.info("Master config: masterAddress -> {} ", masterAddress);
logger.info("Master config: masterRegistryPath -> {} ", masterRegistryPath);
logger.info("Master config: workerGroupRefreshInterval -> {} ", workerGroupRefreshInterval);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.dolphinscheduler.server.master.dispatch.host;

import org.apache.dolphinscheduler.common.model.WorkerHeartBeat;
import org.apache.dolphinscheduler.remote.utils.Host;
import org.apache.dolphinscheduler.server.master.dispatch.context.ExecutionContext;
import org.apache.dolphinscheduler.server.master.dispatch.enums.ExecutorType;
Expand Down Expand Up @@ -78,8 +77,9 @@ protected List<HostWorker> getWorkerCandidates(String workerGroup) {
Set<String> nodes = serverNodeManager.getWorkerGroupNodes(workerGroup);
if (CollectionUtils.isNotEmpty(nodes)) {
for (String node : nodes) {
WorkerHeartBeat workerNodeInfo = serverNodeManager.getWorkerNodeInfo(node);
hostWorkers.add(HostWorker.of(node, workerNodeInfo.getWorkerHostWeight(), workerGroup));
serverNodeManager.getWorkerNodeInfo(node).ifPresent(
workerNodeInfo -> hostWorkers
.add(HostWorker.of(node, workerNodeInfo.getWorkerHostWeight(), workerGroup)));
}
}
return hostWorkers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
Expand All @@ -66,9 +67,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* server node manager
*/
@Service
public class ServerNodeManager implements InitializingBean {

Expand All @@ -89,9 +87,6 @@ public class ServerNodeManager implements InitializingBean {
*/
private final ConcurrentHashMap<String, Set<String>> workerGroupNodes = new ConcurrentHashMap<>();

/**
* master nodes
*/
private final Set<String> masterNodes = new HashSet<>();

private final Map<String, WorkerHeartBeat> workerNodeInfo = new HashMap<>();
Expand All @@ -115,35 +110,36 @@ public class ServerNodeManager implements InitializingBean {
@Autowired
private MasterConfig masterConfig;

private List<WorkerInfoChangeListener> workerInfoChangeListeners = new ArrayList<>();
private final List<WorkerInfoChangeListener> workerInfoChangeListeners = new ArrayList<>();

private static volatile int MASTER_SLOT = 0;
private volatile int currentSlot = 0;

private static volatile int MASTER_SIZE = 0;
private volatile int totalSlot = 0;

public static int getSlot() {
return MASTER_SLOT;
public int getSlot() {
return currentSlot;
}

public static int getMasterSize() {
return MASTER_SIZE;
public int getMasterSize() {
return totalSlot;
}

/**
* init listener
*
* @throws Exception if error throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {

// load nodes from zookeeper
load();
updateMasterNodes();
updateWorkerNodes();
updateWorkerGroupMappings();

// init executor service
executorService =
Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("ServerNodeManagerExecutor"));
executorService.scheduleWithFixedDelay(new WorkerNodeInfoAndGroupDbSyncTask(), 0, 10, TimeUnit.SECONDS);
executorService.scheduleWithFixedDelay(
new WorkerNodeInfoAndGroupDbSyncTask(),
0,
masterConfig.getWorkerGroupRefreshInterval().getSeconds(),
TimeUnit.SECONDS);

// init MasterNodeListener listener
registryClient.subscribe(REGISTRY_DOLPHINSCHEDULER_MASTERS, new MasterDataListener());
Expand All @@ -152,19 +148,6 @@ public void afterPropertiesSet() throws Exception {
registryClient.subscribe(REGISTRY_DOLPHINSCHEDULER_WORKERS, new WorkerDataListener());
}

/**
* load nodes from zookeeper
*/
public void load() {
// master nodes from zookeeper
updateMasterNodes();
updateWorkerNodes();
updateWorkerGroupMappings();
}

/**
* worker node info and worker group db sync task
*/
class WorkerNodeInfoAndGroupDbSyncTask implements Runnable {

@Override
Expand Down Expand Up @@ -251,8 +234,8 @@ public void notify(Event event) {
}

private void updateMasterNodes() {
MASTER_SLOT = 0;
MASTER_SIZE = 0;
currentSlot = 0;
totalSlot = 0;
this.masterNodes.clear();
String nodeLock = Constants.REGISTRY_DOLPHINSCHEDULER_LOCK_MASTERS;
try {
Expand Down Expand Up @@ -325,14 +308,12 @@ private void syncMasterNodes(Collection<String> nodes, List<Server> masterNodes)
this.masterPriorityQueue.putList(masterNodes);
int index = masterPriorityQueue.getIndex(masterConfig.getMasterAddress());
if (index >= 0) {
MASTER_SIZE = nodes.size();
MASTER_SLOT = index;
totalSlot = nodes.size();
currentSlot = index;
} else {
logger.warn("current addr:{} is not in active master list",
masterConfig.getMasterAddress());
logger.warn("Current master is not in active master list");
}
logger.info("update master nodes, master size: {}, slot: {}, addr: {}", MASTER_SIZE,
MASTER_SLOT, masterConfig.getMasterAddress());
logger.info("Update master nodes, total master size: {}, current slot: {}", totalSlot, currentSlot);
} finally {
masterLock.unlock();
}
Expand Down Expand Up @@ -360,10 +341,10 @@ public Set<String> getWorkerGroupNodes(String workerGroup) {
workerGroup = Constants.DEFAULT_WORKER_GROUP;
}
Set<String> nodes = workerGroupNodes.get(workerGroup);
if (CollectionUtils.isNotEmpty(nodes)) {
return Collections.unmodifiableSet(nodes);
if (CollectionUtils.isEmpty(nodes)) {
return Collections.emptySet();
}
return nodes;
return Collections.unmodifiableSet(nodes);
} finally {
workerGroupReadLock.unlock();
}
Expand All @@ -373,45 +354,19 @@ public Map<String, WorkerHeartBeat> getWorkerNodeInfo() {
return Collections.unmodifiableMap(workerNodeInfo);
}

/**
* get worker node info
*
* @param workerNode worker node
* @return worker node info
*/
public WorkerHeartBeat getWorkerNodeInfo(String workerNode) {
public Optional<WorkerHeartBeat> getWorkerNodeInfo(String workerServerAddress) {
workerNodeInfoReadLock.lock();
try {
return workerNodeInfo.getOrDefault(workerNode, null);
return Optional.ofNullable(workerNodeInfo.getOrDefault(workerServerAddress, null));
} finally {
workerNodeInfoReadLock.unlock();
}
}

/**
* sync worker node info
*
* @param newWorkerNodeInfo new worker node info
*/
private void syncAllWorkerNodeInfo(Map<String, String> newWorkerNodeInfo) {
workerNodeInfoWriteLock.lock();
try {
workerNodeInfo.clear();
for (Map.Entry<String, String> entry : newWorkerNodeInfo.entrySet()) {
workerNodeInfo.put(entry.getKey(), JSONUtils.parseObject(entry.getValue(), WorkerHeartBeat.class));
}
} finally {
workerNodeInfoWriteLock.unlock();
}
}

/**
* sync single worker node info
*/
private void syncSingleWorkerNodeInfo(String node, WorkerHeartBeat info) {
private void syncSingleWorkerNodeInfo(String workerAddress, WorkerHeartBeat info) {
workerNodeInfoWriteLock.lock();
try {
workerNodeInfo.put(node, info);
workerNodeInfo.put(workerAddress, info);
} finally {
workerNodeInfoWriteLock.unlock();
}
Expand All @@ -434,9 +389,6 @@ private void notifyWorkerInfoChangeListeners() {
}
}

/**
* destroy
*/
@PreDestroy
public void destroy() {
executorService.shutdownNow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public class MasterSchedulerBootstrap extends BaseDaemonThread implements AutoCl
@Autowired
private WorkflowEventLooper workflowEventLooper;

@Autowired
private ServerNodeManager serverNodeManager;

private String masterAddress;

protected MasterSchedulerBootstrap() {
Expand Down Expand Up @@ -260,8 +263,8 @@ private List<ProcessInstance> command2ProcessInstance(List<Command> commands) th
private List<Command> findCommands() throws MasterException {
try {
long scheduleStartTime = System.currentTimeMillis();
int thisMasterSlot = ServerNodeManager.getSlot();
int masterCount = ServerNodeManager.getMasterSize();
int thisMasterSlot = serverNodeManager.getSlot();
int masterCount = serverNodeManager.getMasterSize();
if (masterCount <= 0) {
logger.warn("Master count: {} is invalid, the current slot: {}", masterCount, thisMasterSlot);
return Collections.emptyList();
Expand All @@ -283,8 +286,8 @@ private List<Command> findCommands() throws MasterException {
}

private SlotCheckState slotCheck(Command command) {
int slot = ServerNodeManager.getSlot();
int masterSize = ServerNodeManager.getMasterSize();
int slot = serverNodeManager.getSlot();
int masterSize = serverNodeManager.getMasterSize();
SlotCheckState state;
if (masterSize <= 0) {
state = SlotCheckState.CHANGE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ master:
strategy: waiting
# The max waiting time to reconnect to registry if you set the strategy to waiting
max-waiting-time: 100s
worker-group-refresh-interval: 10s

server:
port: 5679
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.apache.dolphinscheduler.server.master.dispatch.context.ExecutionContext;
import org.apache.dolphinscheduler.server.master.registry.ServerNodeManager;

import java.util.Optional;

import org.assertj.core.util.Strings;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -57,7 +59,8 @@ public void testSelectWithEmptyResult() {
@Test
public void testSelectWithResult() {
Mockito.when(serverNodeManager.getWorkerGroupNodes("default")).thenReturn(Sets.newHashSet("192.168.1.1:22"));
Mockito.when(serverNodeManager.getWorkerNodeInfo("192.168.1.1:22")).thenReturn(new WorkerHeartBeat());
Mockito.when(serverNodeManager.getWorkerNodeInfo("192.168.1.1:22"))
.thenReturn(Optional.of(new WorkerHeartBeat()));
ExecutionContext context = ExecutionContextTestUtils.getExecutionContext(10000);
Host host = roundRobinHostManager.select(context);
Assertions.assertFalse(Strings.isNullOrEmpty(host.getAddress()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ master:
failover-interval: 10m
# kill yarn jon when failover taskInstance, default true
kill-yarn-job-when-task-failover: true
worker-group-refresh-interval: 10s

worker:
# worker listener port
Expand Down

0 comments on commit e6da1cc

Please sign in to comment.