com.alibaba.dts.client.executor.grid.timer.ExecutionCounterUpdateTimer Maven / Gradle / Ivy
package com.alibaba.dts.client.executor.grid.timer;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.alibaba.dts.client.executor.job.context.ClientContextImpl;
import com.alibaba.dts.client.store.access.ExecutionCounterDao;
import com.alibaba.dts.common.domain.store.ExecutionCounter;
import com.alibaba.dts.common.exception.AccessException;
import com.alibaba.dts.common.exception.InitException;
import com.alibaba.dts.common.logger.SchedulerXLoggerFactory;
import com.alibaba.dts.common.logger.innerlog.Logger;
/**
* ExecutionCounter persistence time, used for initialing execution counters stored in db, starting the update timer
*
*
* @author Ronan Zhan
* @date 2016/10/18.
*/
public class ExecutionCounterUpdateTimer {
private static final Logger logger = SchedulerXLoggerFactory.getLogger(ExecutionCounterUpdateTimer.class);
private ClientContextImpl clientContext;
public ExecutionCounterUpdateTimer(ClientContextImpl clientContext) {
this.clientContext = clientContext;
}
public void init() throws InitException {
final ConcurrentHashMap>> executionCounterTable = clientContext.getExecutionCounterTable();
try {
loadExecutionCounters(executionCounterTable);
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
for (Map.Entry>> executionCounterEntry : executionCounterTable.entrySet()) {
Long jobInstanceId = executionCounterEntry.getKey();
ConcurrentHashMap> executionCounterMapByReceiveNode = executionCounterEntry.getValue();
for (Map.Entry> entryByReceiveNode : executionCounterMapByReceiveNode.entrySet()) {
String receiveNode = entryByReceiveNode.getKey();
ConcurrentHashMap executionCounterMapByTaskName = entryByReceiveNode.getValue();
for (Map.Entry entryByTaskName : executionCounterMapByTaskName.entrySet()) {
String taskName = entryByTaskName.getKey();
ExecutionCounter executionCounter = entryByTaskName.getValue();
executionCounter.setJobInstanceId(jobInstanceId);
executionCounter.setReceiveNode(receiveNode);
executionCounter.setTaskName(taskName);
ExecutionCounter executionCounterDb = clientContext.getStore().getExecutionCounterDao().getByJobInstanceAndExecutionNodeAndTaskName(jobInstanceId, receiveNode, taskName);
if (executionCounterDb == null) {
clientContext.getStore().getExecutionCounterDao().createExecutionCounter(executionCounter);
} else {
if (executionCounter.getId() == 0) {
executionCounter.setId(executionCounterDb.getId());
}
clientContext.getStore().getExecutionCounterDao().updateExecutionCounter(executionCounter);
}
}
}
}
} catch (Throwable throwable) {
logger.error(throwable.getMessage(), throwable);
}
}
}, 10, 1, TimeUnit.SECONDS);
} catch (Throwable throwable) {
logger.error("ExecutionCounterUpdateTimer init error");
throw new InitException("ExecutionCounterUpdateTimer init error", throwable);
}
}
/**
* load execution counter from db
*
* executionCounterTable
*/
private void loadExecutionCounters(ConcurrentHashMap>> executionCounterTable) {
List executionCounters = null;
try {
executionCounters = clientContext.getStore().getExecutionCounterDao().list();
} catch (AccessException e) {
logger.error("list executions failed", e);
}
if (executionCounters == null || executionCounters.isEmpty()) {
return;
}
for (ExecutionCounter executionCounter : executionCounters) {
Long jobInstanceId = executionCounter.getJobInstanceId();
String receiveNode = executionCounter.getReceiveNode();
String taskName = executionCounter.getTaskName();
ConcurrentHashMap> executionCounterMapByReceiveNode = executionCounterTable.get(jobInstanceId);
if (executionCounterMapByReceiveNode == null) {
executionCounterMapByReceiveNode = new ConcurrentHashMap>();
ConcurrentHashMap> executionCounterMapByReceiveNodeExist = clientContext.getExecutionCounterTable().putIfAbsent(jobInstanceId, executionCounterMapByReceiveNode);
if (executionCounterMapByReceiveNodeExist != null) {
executionCounterMapByReceiveNode = executionCounterMapByReceiveNodeExist;
}
}
ConcurrentHashMap executionCounterMapByTaskName = executionCounterMapByReceiveNode.get(receiveNode);
if (executionCounterMapByTaskName == null) {
executionCounterMapByTaskName = new ConcurrentHashMap();
ConcurrentHashMap executionCounterMapByTaskNameExist = executionCounterMapByReceiveNode.putIfAbsent(receiveNode, executionCounterMapByTaskName);
if (executionCounterMapByTaskNameExist != null) {
executionCounterMapByTaskName = executionCounterMapByTaskNameExist;
}
}
executionCounterMapByTaskName.putIfAbsent(taskName, executionCounter);
}
}
}