Browse Source
* Refactor worker (#1) * add TaskResponseProcessor (#1983) * 1, master persistent task 2. extract master and worker communication model (#1992) * 1, master persistent task 2. extract master and worker communication model * 1, master persistent task 2. extract master and worker communication model * 1, master persistent task 2. extract master and worker communication model * add license * modify javadoc error Co-authored-by: qiaozhanwei <qiaozhanwei@outlook.com> * updates Co-authored-by: qiaozhanwei <qiaozhanwei@outlook.com>pull/2/head
Tboy
5 years ago
committed by
GitHub
16 changed files with 942 additions and 86 deletions
@ -0,0 +1,250 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.remote.entity; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* master/worker task transport |
||||
*/ |
||||
public class TaskExecutionContext implements Serializable{ |
||||
|
||||
/** |
||||
* task instance id |
||||
*/ |
||||
private Integer taskId; |
||||
|
||||
|
||||
/** |
||||
* taks name |
||||
*/ |
||||
private String taskName; |
||||
|
||||
/** |
||||
* task start time |
||||
*/ |
||||
private Date startTime; |
||||
|
||||
/** |
||||
* task type |
||||
*/ |
||||
private String taskType; |
||||
|
||||
/** |
||||
* task execute path |
||||
*/ |
||||
private String executePath; |
||||
|
||||
/** |
||||
* task json |
||||
*/ |
||||
private String taskJson; |
||||
|
||||
|
||||
/** |
||||
* process instance id |
||||
*/ |
||||
private Integer processInstanceId; |
||||
|
||||
|
||||
/** |
||||
* process instance schedule time |
||||
*/ |
||||
private Date scheduleTime; |
||||
|
||||
/** |
||||
* process instance global parameters |
||||
*/ |
||||
private String globalParams; |
||||
|
||||
|
||||
/** |
||||
* execute user id |
||||
*/ |
||||
private Integer executorId; |
||||
|
||||
|
||||
/** |
||||
* command type if complement |
||||
*/ |
||||
private Integer cmdTypeIfComplement; |
||||
|
||||
|
||||
/** |
||||
* tenant code |
||||
*/ |
||||
private String tenantCode; |
||||
|
||||
/** |
||||
* task queue |
||||
*/ |
||||
private String queue; |
||||
|
||||
|
||||
/** |
||||
* process define id |
||||
*/ |
||||
private Integer processDefineId; |
||||
|
||||
/** |
||||
* project id |
||||
*/ |
||||
private Integer projectId; |
||||
|
||||
public Integer getTaskId() { |
||||
return taskId; |
||||
} |
||||
|
||||
public void setTaskId(Integer taskId) { |
||||
this.taskId = taskId; |
||||
} |
||||
|
||||
public String getTaskName() { |
||||
return taskName; |
||||
} |
||||
|
||||
public void setTaskName(String taskName) { |
||||
this.taskName = taskName; |
||||
} |
||||
|
||||
public Date getStartTime() { |
||||
return startTime; |
||||
} |
||||
|
||||
public void setStartTime(Date startTime) { |
||||
this.startTime = startTime; |
||||
} |
||||
|
||||
public String getTaskType() { |
||||
return taskType; |
||||
} |
||||
|
||||
public void setTaskType(String taskType) { |
||||
this.taskType = taskType; |
||||
} |
||||
|
||||
public String getExecutePath() { |
||||
return executePath; |
||||
} |
||||
|
||||
public void setExecutePath(String executePath) { |
||||
this.executePath = executePath; |
||||
} |
||||
|
||||
public String getTaskJson() { |
||||
return taskJson; |
||||
} |
||||
|
||||
public void setTaskJson(String taskJson) { |
||||
this.taskJson = taskJson; |
||||
} |
||||
|
||||
public Integer getProcessInstanceId() { |
||||
return processInstanceId; |
||||
} |
||||
|
||||
public void setProcessInstanceId(Integer processInstanceId) { |
||||
this.processInstanceId = processInstanceId; |
||||
} |
||||
|
||||
public Date getScheduleTime() { |
||||
return scheduleTime; |
||||
} |
||||
|
||||
public void setScheduleTime(Date scheduleTime) { |
||||
this.scheduleTime = scheduleTime; |
||||
} |
||||
|
||||
public String getGlobalParams() { |
||||
return globalParams; |
||||
} |
||||
|
||||
public void setGlobalParams(String globalParams) { |
||||
this.globalParams = globalParams; |
||||
} |
||||
|
||||
public String getTenantCode() { |
||||
return tenantCode; |
||||
} |
||||
|
||||
public void setTenantCode(String tenantCode) { |
||||
this.tenantCode = tenantCode; |
||||
} |
||||
|
||||
public String getQueue() { |
||||
return queue; |
||||
} |
||||
|
||||
public void setQueue(String queue) { |
||||
this.queue = queue; |
||||
} |
||||
|
||||
public Integer getProcessDefineId() { |
||||
return processDefineId; |
||||
} |
||||
|
||||
public void setProcessDefineId(Integer processDefineId) { |
||||
this.processDefineId = processDefineId; |
||||
} |
||||
|
||||
public Integer getProjectId() { |
||||
return projectId; |
||||
} |
||||
|
||||
public void setProjectId(Integer projectId) { |
||||
this.projectId = projectId; |
||||
} |
||||
|
||||
public Integer getExecutorId() { |
||||
return executorId; |
||||
} |
||||
|
||||
public void setExecutorId(Integer executorId) { |
||||
this.executorId = executorId; |
||||
} |
||||
|
||||
public Integer getCmdTypeIfComplement() { |
||||
return cmdTypeIfComplement; |
||||
} |
||||
|
||||
public void setCmdTypeIfComplement(Integer cmdTypeIfComplement) { |
||||
this.cmdTypeIfComplement = cmdTypeIfComplement; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "TaskExecutionContext{" + |
||||
"taskId=" + taskId + |
||||
", taskName='" + taskName + '\'' + |
||||
", startTime=" + startTime + |
||||
", taskType='" + taskType + '\'' + |
||||
", executePath='" + executePath + '\'' + |
||||
", taskJson='" + taskJson + '\'' + |
||||
", processInstanceId=" + processInstanceId + |
||||
", scheduleTime=" + scheduleTime + |
||||
", globalParams='" + globalParams + '\'' + |
||||
", executorId=" + executorId + |
||||
", cmdTypeIfComplement=" + cmdTypeIfComplement + |
||||
", tenantCode='" + tenantCode + '\'' + |
||||
", queue='" + queue + '\'' + |
||||
", processDefineId=" + processDefineId + |
||||
", projectId=" + projectId + |
||||
'}'; |
||||
} |
||||
} |
@ -0,0 +1,95 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.master.host; |
||||
|
||||
|
||||
import java.util.Objects; |
||||
|
||||
|
||||
public class Host { |
||||
|
||||
private String address; |
||||
|
||||
private String ip; |
||||
|
||||
private int port; |
||||
|
||||
public Host() { |
||||
} |
||||
|
||||
public Host(String ip, int port) { |
||||
this.ip = ip; |
||||
this.port = port; |
||||
this.address = ip + ":" + port; |
||||
} |
||||
|
||||
public String getAddress() { |
||||
return address; |
||||
} |
||||
|
||||
public void setAddress(String address) { |
||||
this.address = address; |
||||
} |
||||
|
||||
public String getIp() { |
||||
return ip; |
||||
} |
||||
|
||||
public void setIp(String ip) { |
||||
this.ip = ip; |
||||
this.address = ip + ":" + port; |
||||
} |
||||
|
||||
public int getPort() { |
||||
return port; |
||||
} |
||||
|
||||
public void setPort(int port) { |
||||
this.port = port; |
||||
this.address = ip + ":" + port; |
||||
} |
||||
|
||||
public static Host of(String address){ |
||||
String[] parts = address.split(":"); |
||||
if (parts.length != 2) { |
||||
throw new IllegalArgumentException(String.format("Address : %s illegal.", address)); |
||||
} |
||||
Host host = new Host(parts[0], Integer.parseInt(parts[1])); |
||||
return host; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Host{" + |
||||
"address='" + address + '\'' + |
||||
'}'; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object o) { |
||||
if (this == o) return true; |
||||
if (o == null || getClass() != o.getClass()) return false; |
||||
Host host = (Host) o; |
||||
return Objects.equals(getAddress(), host.getAddress()); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return Objects.hash(getAddress()); |
||||
} |
||||
} |
@ -0,0 +1,27 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.master.host; |
||||
|
||||
|
||||
import org.apache.dolphinscheduler.remote.entity.TaskExecutionContext; |
||||
|
||||
public interface HostManager { |
||||
|
||||
Host select(TaskExecutionContext context); |
||||
|
||||
} |
@ -0,0 +1,57 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.master.host; |
||||
|
||||
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
||||
import org.apache.dolphinscheduler.remote.entity.TaskExecutionContext; |
||||
import org.apache.dolphinscheduler.server.master.host.assign.RoundRobinSelector; |
||||
import org.apache.dolphinscheduler.server.registry.ZookeeperNodeManager; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import java.util.List; |
||||
|
||||
|
||||
@Service |
||||
public class RoundRobinHostManager implements HostManager { |
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(RoundRobinHostManager.class); |
||||
|
||||
@Autowired |
||||
private RoundRobinSelector<Host> selector; |
||||
|
||||
@Autowired |
||||
private ZookeeperNodeManager zookeeperNodeManager; |
||||
|
||||
@Override |
||||
public Host select(TaskExecutionContext context){ |
||||
Host host = new Host(); |
||||
Collection<String> nodes = zookeeperNodeManager.getWorkerNodes(); |
||||
if(CollectionUtils.isEmpty(nodes)){ |
||||
return host; |
||||
} |
||||
List<Host> candidateHosts = new ArrayList<>(nodes.size()); |
||||
nodes.stream().forEach(node -> candidateHosts.add(Host.of(node))); |
||||
return selector.select(candidateHosts); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,45 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.master.host.assign; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.Random; |
||||
|
||||
|
||||
public class RandomSelector<T> implements Selector<T> { |
||||
|
||||
private final Random random = new Random(); |
||||
|
||||
@Override |
||||
public T select(final Collection<T> source) { |
||||
|
||||
if (source == null || source.size() == 0) { |
||||
throw new IllegalArgumentException("Empty source."); |
||||
} |
||||
|
||||
if (source.size() == 1) { |
||||
return (T) source.toArray()[0]; |
||||
} |
||||
|
||||
int size = source.size(); |
||||
int randomIndex = random.nextInt(size); |
||||
|
||||
return (T) source.toArray()[randomIndex]; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,40 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.master.host.assign; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
|
||||
public class RoundRobinSelector<T> implements Selector<T> { |
||||
|
||||
private final AtomicInteger index = new AtomicInteger(0); |
||||
|
||||
@Override |
||||
public T select(Collection<T> source) { |
||||
if (source == null || source.size() == 0) { |
||||
throw new IllegalArgumentException("Empty source."); |
||||
} |
||||
|
||||
if (source.size() == 1) { |
||||
return (T)source.toArray()[0]; |
||||
} |
||||
|
||||
int size = source.size(); |
||||
return (T) source.toArray()[index.getAndIncrement() % size]; |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.master.host.assign; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
|
||||
public interface Selector<T> { |
||||
|
||||
T select(Collection<T> source); |
||||
} |
@ -0,0 +1,104 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.master.registry; |
||||
|
||||
import org.apache.curator.framework.CuratorFramework; |
||||
import org.apache.curator.framework.state.ConnectionState; |
||||
import org.apache.curator.framework.state.ConnectionStateListener; |
||||
import org.apache.dolphinscheduler.remote.utils.Constants; |
||||
import org.apache.dolphinscheduler.server.registry.ZookeeperRegistryCenter; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
/** |
||||
* master registry |
||||
*/ |
||||
public class MasterRegistry { |
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MasterRegistry.class); |
||||
|
||||
/** |
||||
* zookeeper registry center |
||||
*/ |
||||
private final ZookeeperRegistryCenter zookeeperRegistryCenter; |
||||
|
||||
/** |
||||
* port |
||||
*/ |
||||
private final int port; |
||||
|
||||
/** |
||||
* construct |
||||
* @param zookeeperRegistryCenter zookeeperRegistryCenter |
||||
* @param port port |
||||
*/ |
||||
public MasterRegistry(ZookeeperRegistryCenter zookeeperRegistryCenter, int port){ |
||||
this.zookeeperRegistryCenter = zookeeperRegistryCenter; |
||||
this.port = port; |
||||
} |
||||
|
||||
/** |
||||
* registry |
||||
*/ |
||||
public void registry() { |
||||
String address = Constants.LOCAL_ADDRESS; |
||||
String localNodePath = getWorkerPath(); |
||||
zookeeperRegistryCenter.getZookeeperCachedOperator().persist(localNodePath, ""); |
||||
zookeeperRegistryCenter.getZookeeperCachedOperator().getZkClient().getConnectionStateListenable().addListener(new ConnectionStateListener() { |
||||
@Override |
||||
public void stateChanged(CuratorFramework client, ConnectionState newState) { |
||||
if(newState == ConnectionState.LOST){ |
||||
logger.error("master : {} connection lost from zookeeper", address); |
||||
} else if(newState == ConnectionState.RECONNECTED){ |
||||
logger.info("master : {} reconnected to zookeeper", address); |
||||
zookeeperRegistryCenter.getZookeeperCachedOperator().persist(localNodePath, ""); |
||||
} else if(newState == ConnectionState.SUSPENDED){ |
||||
logger.warn("master : {} connection SUSPENDED ", address); |
||||
} |
||||
} |
||||
}); |
||||
logger.info("master node : {} registry to ZK successfully.", address); |
||||
} |
||||
|
||||
/** |
||||
* remove registry info |
||||
*/ |
||||
public void unRegistry() { |
||||
String address = getLocalAddress(); |
||||
String localNodePath = getWorkerPath(); |
||||
zookeeperRegistryCenter.getZookeeperCachedOperator().remove(localNodePath); |
||||
logger.info("worker node : {} unRegistry to ZK.", address); |
||||
} |
||||
|
||||
/** |
||||
* get worker path |
||||
* @return |
||||
*/ |
||||
private String getWorkerPath() { |
||||
String address = getLocalAddress(); |
||||
String localNodePath = this.zookeeperRegistryCenter.getWorkerPath() + "/" + address; |
||||
return localNodePath; |
||||
} |
||||
|
||||
/** |
||||
* get local address |
||||
* @return |
||||
*/ |
||||
private String getLocalAddress(){ |
||||
return Constants.LOCAL_ADDRESS + ":" + port; |
||||
} |
||||
} |
@ -0,0 +1,159 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.server.registry; |
||||
|
||||
import org.apache.curator.framework.CuratorFramework; |
||||
|
||||
import org.apache.curator.framework.recipes.cache.TreeCacheEvent; |
||||
import org.apache.dolphinscheduler.service.zk.AbstractListener; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.InitializingBean; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
import java.util.concurrent.locks.Lock; |
||||
import java.util.concurrent.locks.ReentrantLock; |
||||
|
||||
|
||||
@Service |
||||
public abstract class ZookeeperNodeManager implements InitializingBean { |
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ZookeeperNodeManager.class); |
||||
|
||||
private final Lock masterLock = new ReentrantLock(); |
||||
|
||||
private final Lock workerLock = new ReentrantLock(); |
||||
|
||||
private final Set<String> workerNodes = new HashSet<>(); |
||||
|
||||
private final Set<String> masterNodes = new HashSet<>(); |
||||
|
||||
@Autowired |
||||
private ZookeeperRegistryCenter registryCenter; |
||||
|
||||
@Override |
||||
public void afterPropertiesSet() throws Exception { |
||||
load(); |
||||
registryCenter.getZookeeperCachedOperator().addListener(new MasterNodeListener()); |
||||
registryCenter.getZookeeperCachedOperator().addListener(new WorkerNodeListener()); |
||||
} |
||||
|
||||
private void load(){ |
||||
Set<String> schedulerNodes = registryCenter.getMasterNodesDirectly(); |
||||
syncMasterNodes(schedulerNodes); |
||||
Set<String> workersNodes = registryCenter.getWorkerNodesDirectly(); |
||||
syncWorkerNodes(workersNodes); |
||||
} |
||||
|
||||
class WorkerNodeListener extends AbstractListener { |
||||
|
||||
@Override |
||||
protected void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) { |
||||
if(registryCenter.isWorkerPath(path)){ |
||||
try { |
||||
if (event.getType() == TreeCacheEvent.Type.NODE_ADDED) { |
||||
logger.info("worker node : {} added.", path); |
||||
Set<String> previousNodes = new HashSet<>(workerNodes); |
||||
Set<String> currentNodes = registryCenter.getWorkerNodesDirectly(); |
||||
syncWorkerNodes(currentNodes); |
||||
} else if (event.getType() == TreeCacheEvent.Type.NODE_REMOVED) { |
||||
logger.info("worker node : {} down.", path); |
||||
Set<String> previousNodes = new HashSet<>(workerNodes); |
||||
Set<String> currentNodes = registryCenter.getWorkerNodesDirectly(); |
||||
syncWorkerNodes(currentNodes); |
||||
} |
||||
} catch (IllegalArgumentException ignore) { |
||||
logger.warn(ignore.getMessage()); |
||||
} catch (Exception ex) { |
||||
logger.error("WorkerListener capture data change and get data failed", ex); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
class MasterNodeListener extends AbstractListener { |
||||
|
||||
@Override |
||||
protected void dataChanged(CuratorFramework client, TreeCacheEvent event, String path) { |
||||
if (registryCenter.isMasterPath(path)) { |
||||
try { |
||||
if (event.getType() == TreeCacheEvent.Type.NODE_ADDED) { |
||||
logger.info("master node : {} added.", path); |
||||
Set<String> previousNodes = new HashSet<>(masterNodes); |
||||
Set<String> currentNodes = registryCenter.getMasterNodesDirectly(); |
||||
syncMasterNodes(currentNodes); |
||||
} else if (event.getType() == TreeCacheEvent.Type.NODE_REMOVED) { |
||||
logger.info("master node : {} down.", path); |
||||
Set<String> previousNodes = new HashSet<>(masterNodes); |
||||
Set<String> currentNodes = registryCenter.getMasterNodesDirectly(); |
||||
syncMasterNodes(currentNodes); |
||||
} |
||||
} catch (Exception ex) { |
||||
logger.error("MasterNodeListener capture data change and get data failed.", ex); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
public Set<String> getMasterNodes() { |
||||
masterLock.lock(); |
||||
try { |
||||
return Collections.unmodifiableSet(masterNodes); |
||||
} finally { |
||||
masterLock.unlock(); |
||||
} |
||||
} |
||||
|
||||
private void syncMasterNodes(Set<String> nodes){ |
||||
masterLock.lock(); |
||||
try { |
||||
masterNodes.clear(); |
||||
masterNodes.addAll(nodes); |
||||
} finally { |
||||
masterLock.unlock(); |
||||
} |
||||
} |
||||
|
||||
private void syncWorkerNodes(Set<String> nodes){ |
||||
workerLock.lock(); |
||||
try { |
||||
workerNodes.clear(); |
||||
workerNodes.addAll(nodes); |
||||
} finally { |
||||
workerLock.unlock(); |
||||
} |
||||
} |
||||
|
||||
public Set<String> getWorkerNodes(){ |
||||
workerLock.lock(); |
||||
try { |
||||
return Collections.unmodifiableSet(workerNodes); |
||||
} finally { |
||||
workerLock.unlock(); |
||||
} |
||||
} |
||||
|
||||
public void close(){ |
||||
registryCenter.close(); |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.dolphinscheduler.service.zk; |
||||
|
||||
import org.apache.curator.framework.CuratorFramework; |
||||
import org.apache.curator.framework.recipes.cache.TreeCacheEvent; |
||||
import org.apache.curator.framework.recipes.cache.TreeCacheListener; |
||||
|
||||
public abstract class AbstractListener implements TreeCacheListener { |
||||
|
||||
@Override |
||||
public final void childEvent(final CuratorFramework client, final TreeCacheEvent event) throws Exception { |
||||
String path = null == event.getData() ? "" : event.getData().getPath(); |
||||
if (path.isEmpty()) { |
||||
return; |
||||
} |
||||
dataChanged(client, event, path); |
||||
} |
||||
|
||||
protected abstract void dataChanged(final CuratorFramework client, final TreeCacheEvent event, final String path); |
||||
} |
Loading…
Reference in new issue