use of com.dangdang.ddframe.job.lite.api.strategy.JobShardingStrategy in project elastic-job by dangdangdotcom.
the class ShardingService method shardingIfNecessary.
/**
* 如果需要分片且当前节点为主节点, 则作业分片.
* 如果当前无可用节点则不分片.
*/
public void shardingIfNecessary() {
List<String> availableShardingServers = serverService.getAvailableShardingServers();
if (availableShardingServers.isEmpty()) {
clearShardingInfo();
return;
}
if (!isNeedSharding()) {
return;
}
if (!leaderElectionService.isLeader()) {
blockUntilShardingCompleted();
return;
}
LiteJobConfiguration liteJobConfig = configService.load(false);
if (liteJobConfig.isMonitorExecution()) {
waitingOtherJobCompleted();
}
log.debug("Job '{}' sharding begin.", jobName);
jobNodeStorage.fillEphemeralJobNode(ShardingNode.PROCESSING, "");
clearShardingInfo();
JobShardingStrategy jobShardingStrategy = JobShardingStrategyFactory.getStrategy(liteJobConfig.getJobShardingStrategyClass());
JobShardingStrategyOption option = new JobShardingStrategyOption(jobName, liteJobConfig.getTypeConfig().getCoreConfig().getShardingTotalCount());
jobNodeStorage.executeInTransaction(new PersistShardingInfoTransactionExecutionCallback(jobShardingStrategy.sharding(availableShardingServers, option)));
log.debug("Job '{}' sharding complete.", jobName);
}
Aggregations