|
@@ -17,7 +17,6 @@
|
|
|
package cn.escheduler.server.worker;
|
|
|
|
|
|
import cn.escheduler.common.Constants;
|
|
|
-import cn.escheduler.common.IStoppable;
|
|
|
import cn.escheduler.common.enums.ExecutionStatus;
|
|
|
import cn.escheduler.common.enums.TaskType;
|
|
|
import cn.escheduler.common.queue.ITaskQueue;
|
|
@@ -28,20 +27,21 @@ import cn.escheduler.common.thread.ThreadUtils;
|
|
|
import cn.escheduler.common.utils.CollectionUtils;
|
|
|
import cn.escheduler.common.utils.OSUtils;
|
|
|
import cn.escheduler.dao.AlertDao;
|
|
|
-import cn.escheduler.dao.DaoFactory;
|
|
|
import cn.escheduler.dao.ProcessDao;
|
|
|
-import cn.escheduler.dao.ServerDao;
|
|
|
-import cn.escheduler.dao.model.ProcessInstance;
|
|
|
import cn.escheduler.dao.model.TaskInstance;
|
|
|
+import cn.escheduler.server.master.AbstractServer;
|
|
|
import cn.escheduler.server.utils.ProcessUtils;
|
|
|
import cn.escheduler.server.worker.runner.FetchTaskThread;
|
|
|
import cn.escheduler.server.zk.ZKWorkerClient;
|
|
|
-import org.apache.commons.configuration.Configuration;
|
|
|
import org.apache.commons.configuration.ConfigurationException;
|
|
|
import org.apache.commons.configuration.PropertiesConfiguration;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.WebApplicationType;
|
|
|
+import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
|
|
|
import java.util.Set;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
@@ -51,55 +51,34 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
* worker server
|
|
|
*/
|
|
|
-public class WorkerServer implements IStoppable {
|
|
|
+@ComponentScan("cn.escheduler")
|
|
|
+public class WorkerServer extends AbstractServer {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(WorkerServer.class);
|
|
|
|
|
|
-
|
|
|
- * conf
|
|
|
- */
|
|
|
- private static Configuration conf;
|
|
|
-
|
|
|
-
|
|
|
- * object lock
|
|
|
- */
|
|
|
- private final Object lock = new Object();
|
|
|
-
|
|
|
-
|
|
|
- * whether or not to close the state
|
|
|
- */
|
|
|
- private boolean terminated = false;
|
|
|
|
|
|
|
|
|
* zk worker client
|
|
|
*/
|
|
|
private static ZKWorkerClient zkWorkerClient = null;
|
|
|
|
|
|
-
|
|
|
- * worker dao database access
|
|
|
- */
|
|
|
- private ServerDao serverDao = null;
|
|
|
-
|
|
|
|
|
|
* process database access
|
|
|
*/
|
|
|
- private final ProcessDao processDao;
|
|
|
+ @Autowired
|
|
|
+ private ProcessDao processDao;
|
|
|
|
|
|
|
|
|
* alert database access
|
|
|
*/
|
|
|
- private final AlertDao alertDao;
|
|
|
+ @Autowired
|
|
|
+ private AlertDao alertDao;
|
|
|
|
|
|
|
|
|
* heartbeat thread pool
|
|
|
*/
|
|
|
private ScheduledExecutorService heartbeatWorerService;
|
|
|
|
|
|
-
|
|
|
- * heartbeat interval, unit second
|
|
|
- */
|
|
|
- private int heartBeatInterval;
|
|
|
-
|
|
|
|
|
|
* task queue impl
|
|
|
*/
|
|
@@ -115,29 +94,57 @@ public class WorkerServer implements IStoppable {
|
|
|
*/
|
|
|
private ExecutorService fetchTaskExecutorService;
|
|
|
|
|
|
- static {
|
|
|
+ public WorkerServer(){}
|
|
|
+
|
|
|
+ public WorkerServer(ProcessDao processDao, AlertDao alertDao){
|
|
|
try {
|
|
|
conf = new PropertiesConfiguration(Constants.WORKER_PROPERTIES_PATH);
|
|
|
}catch (ConfigurationException e){
|
|
|
logger.error("load configuration failed",e);
|
|
|
System.exit(1);
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- public WorkerServer(){
|
|
|
zkWorkerClient = ZKWorkerClient.getZKWorkerClient();
|
|
|
- this.serverDao = zkWorkerClient.getServerDao();
|
|
|
- this.alertDao = DaoFactory.getDaoInstance(AlertDao.class);
|
|
|
- this.processDao = DaoFactory.getDaoInstance(ProcessDao.class);
|
|
|
- taskQueue = TaskQueueFactory.getTaskQueueInstance();
|
|
|
|
|
|
- killExecutorService = ThreadUtils.newDaemonSingleThreadExecutor("Worker-Kill-Thread-Executor");
|
|
|
+ this.taskQueue = TaskQueueFactory.getTaskQueueInstance();
|
|
|
|
|
|
- fetchTaskExecutorService = ThreadUtils.newDaemonSingleThreadExecutor("Worker-Fetch-Thread-Executor");
|
|
|
+ this.killExecutorService = ThreadUtils.newDaemonSingleThreadExecutor("Worker-Kill-Thread-Executor");
|
|
|
|
|
|
+ this.fetchTaskExecutorService = ThreadUtils.newDaemonSingleThreadExecutor("Worker-Fetch-Thread-Executor");
|
|
|
}
|
|
|
|
|
|
- public void run(){
|
|
|
+
|
|
|
+
|
|
|
+ * master server startup
|
|
|
+ *
|
|
|
+ * master server not use web service
|
|
|
+ */
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ SpringApplicationBuilder app = new SpringApplicationBuilder(WorkerServer.class);
|
|
|
+
|
|
|
+ app.web(WebApplicationType.NONE)
|
|
|
+ .run(args);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run(String... args) throws Exception {
|
|
|
+
|
|
|
+ Thread.currentThread().setName("Worker-Main-Thread");
|
|
|
+
|
|
|
+ WorkerServer workerServer = new WorkerServer(processDao,alertDao);
|
|
|
+
|
|
|
+ workerServer.run(processDao,alertDao);
|
|
|
+
|
|
|
+ logger.info("worker server started");
|
|
|
+
|
|
|
+
|
|
|
+ workerServer.awaitTermination();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void run(ProcessDao processDao, AlertDao alertDao){
|
|
|
|
|
|
|
|
|
heartBeatInterval = conf.getInt(Constants.WORKER_HEARTBEAT_INTERVAL,
|
|
@@ -187,45 +194,82 @@ public class WorkerServer implements IStoppable {
|
|
|
|
|
|
|
|
|
fetchTaskExecutorService.execute(fetchTaskThread);
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public synchronized void stop(String cause) {
|
|
|
|
|
|
- }
|
|
|
+ try {
|
|
|
+
|
|
|
+ if(Stopper.isStoped()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- public static void main(String[] args)throws Exception{
|
|
|
+ logger.info("worker server is stopping ..., cause : {}", cause);
|
|
|
|
|
|
-
|
|
|
- Thread.currentThread().setName("Worker-Main-Thread");
|
|
|
+
|
|
|
+ Stopper.stop();
|
|
|
|
|
|
- WorkerServer workerServer = new WorkerServer();
|
|
|
+ try {
|
|
|
+
|
|
|
+ Thread.sleep(3000L);
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.warn("thread sleep exception:" + e.getMessage(), e);
|
|
|
+ }
|
|
|
|
|
|
- workerServer.run();
|
|
|
+ try {
|
|
|
+ heartbeatWorerService.shutdownNow();
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.warn("heartbeat service stopped exception");
|
|
|
+ }
|
|
|
+ logger.info("heartbeat service stopped");
|
|
|
|
|
|
- logger.info("worker server started");
|
|
|
+ try {
|
|
|
+ ThreadPoolExecutors.getInstance().shutdown();
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.warn("threadpool service stopped exception:{}",e.getMessage());
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- workerServer.awaitTermination();
|
|
|
+ logger.info("threadpool service stopped");
|
|
|
|
|
|
+ try {
|
|
|
+ killExecutorService.shutdownNow();
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.warn("worker kill executor service stopped exception:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ logger.info("worker kill executor service stopped");
|
|
|
|
|
|
- }
|
|
|
+ try {
|
|
|
+ fetchTaskExecutorService.shutdownNow();
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.warn("worker fetch task service stopped exception:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ logger.info("worker fetch task service stopped");
|
|
|
|
|
|
+ try{
|
|
|
+ zkWorkerClient.close();
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.warn("zookeeper service stopped exception:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ logger.info("zookeeper service stopped");
|
|
|
|
|
|
-
|
|
|
- * blocking implement
|
|
|
- * @throws InterruptedException
|
|
|
- */
|
|
|
- public void awaitTermination() throws InterruptedException {
|
|
|
- synchronized (lock) {
|
|
|
- while (!terminated) {
|
|
|
- lock.wait();
|
|
|
+
|
|
|
+ synchronized (lock) {
|
|
|
+ terminated = true;
|
|
|
+ lock.notifyAll();
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("worker server stop exception : " + e.getMessage(), e);
|
|
|
+ System.exit(-1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
|
|
|
* heartbeat thread implement
|
|
|
* @return
|
|
|
*/
|
|
|
- public Runnable heartBeatThread(){
|
|
|
+ private Runnable heartBeatThread(){
|
|
|
Runnable heartBeatThread = new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
@@ -240,11 +284,12 @@ public class WorkerServer implements IStoppable {
|
|
|
return heartBeatThread;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
|
|
|
* kill process thread implement
|
|
|
* @return
|
|
|
*/
|
|
|
- public Runnable getKillProcessThread(){
|
|
|
+ private Runnable getKillProcessThread(){
|
|
|
Runnable killProcessThread = new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
@@ -286,74 +331,5 @@ public class WorkerServer implements IStoppable {
|
|
|
return killProcessThread;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- @Override
|
|
|
- public synchronized void stop(String cause) {
|
|
|
-
|
|
|
- try {
|
|
|
-
|
|
|
- if(Stopper.isStoped()){
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- logger.info("worker server is stopping ..., cause : {}", cause);
|
|
|
-
|
|
|
-
|
|
|
- Stopper.stop();
|
|
|
-
|
|
|
- try {
|
|
|
-
|
|
|
- Thread.sleep(3000L);
|
|
|
- }catch (Exception e){
|
|
|
- logger.warn("thread sleep exception:" + e.getMessage(), e);
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- heartbeatWorerService.shutdownNow();
|
|
|
- }catch (Exception e){
|
|
|
- logger.warn("heartbeat service stopped exception");
|
|
|
- }
|
|
|
- logger.info("heartbeat service stopped");
|
|
|
-
|
|
|
- try {
|
|
|
- ThreadPoolExecutors.getInstance().shutdown();
|
|
|
- }catch (Exception e){
|
|
|
- logger.warn("threadpool service stopped exception:{}",e.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- logger.info("threadpool service stopped");
|
|
|
-
|
|
|
- try {
|
|
|
- killExecutorService.shutdownNow();
|
|
|
- }catch (Exception e){
|
|
|
- logger.warn("worker kill executor service stopped exception:{}",e.getMessage());
|
|
|
- }
|
|
|
- logger.info("worker kill executor service stopped");
|
|
|
-
|
|
|
- try {
|
|
|
- fetchTaskExecutorService.shutdownNow();
|
|
|
- }catch (Exception e){
|
|
|
- logger.warn("worker fetch task service stopped exception:{}",e.getMessage());
|
|
|
- }
|
|
|
- logger.info("worker fetch task service stopped");
|
|
|
-
|
|
|
- try{
|
|
|
- zkWorkerClient.close();
|
|
|
- }catch (Exception e){
|
|
|
- logger.warn("zookeeper service stopped exception:{}",e.getMessage());
|
|
|
- }
|
|
|
- logger.info("zookeeper service stopped");
|
|
|
-
|
|
|
-
|
|
|
- synchronized (lock) {
|
|
|
- terminated = true;
|
|
|
- lock.notifyAll();
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- logger.error("worker server stop exception : " + e.getMessage(), e);
|
|
|
- System.exit(-1);
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|