Skip to content

Commit

Permalink
fix problems of code formatting validation
Browse files Browse the repository at this point in the history
  • Loading branch information
baiyangzhuhong committed Dec 10, 2024
1 parent f9296ab commit 012d7de
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@
*/
@Slf4j
abstract class AbstractJobFacade implements JobFacade {

protected final ConfigurationService configService;

protected final ShardingService shardingService;

protected final ExecutionContextService executionContextService;

protected final ExecutionService executionService;

protected final FailoverService failoverService;

protected final Collection<ElasticJobListener> elasticJobListeners;

protected final JobTracingEventBus jobTracingEventBus;

public AbstractJobFacade(final CoordinatorRegistryCenter regCenter, final String jobName, final Collection<ElasticJobListener> elasticJobListeners, final TracingConfiguration<?> tracingConfig) {
private final ConfigurationService configService;
private final ShardingService shardingService;
private final ExecutionContextService executionContextService;
private final ExecutionService executionService;
private final FailoverService failoverService;
private final Collection<ElasticJobListener> elasticJobListeners;
private final JobTracingEventBus jobTracingEventBus;
AbstractJobFacade(final CoordinatorRegistryCenter regCenter, final String jobName, final Collection<ElasticJobListener> elasticJobListeners, final TracingConfiguration<?> tracingConfig) {
configService = new ConfigurationService(regCenter, jobName);
shardingService = new ShardingService(regCenter, jobName);
executionContextService = new ExecutionContextService(regCenter, jobName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface JobFacade {
* @param fromCache load from cache or not
* @return job configuration
*/
JobConfiguration loadJobConfiguration(final boolean fromCache);
JobConfiguration loadJobConfiguration(boolean fromCache);

/**
* Check job execution environment.
Expand Down Expand Up @@ -121,7 +121,7 @@ public interface JobFacade {
*
* @param jobExecutionEvent job execution event
*/
void postJobExecutionEvent(final JobExecutionEvent jobExecutionEvent);
void postJobExecutionEvent(JobExecutionEvent jobExecutionEvent);

/**
* Post job status trace event.
Expand All @@ -131,7 +131,7 @@ public interface JobFacade {
* @param message job message
*/
void postJobStatusTraceEvent(String taskId, JobStatusTraceEvent.State state, String message);

/**
* Get job runtime service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,55 @@
package org.apache.shardingsphere.elasticjob.kernel.executor.facade;

import lombok.extern.slf4j.Slf4j;

import org.apache.shardingsphere.elasticjob.kernel.internal.config.ConfigurationService;
import org.apache.shardingsphere.elasticjob.kernel.internal.failover.FailoverService;
import org.apache.shardingsphere.elasticjob.kernel.internal.sharding.ExecutionContextService;
import org.apache.shardingsphere.elasticjob.kernel.internal.sharding.ExecutionService;
import org.apache.shardingsphere.elasticjob.kernel.internal.sharding.ShardingService;
import org.apache.shardingsphere.elasticjob.kernel.tracing.event.JobTracingEventBus;
import org.apache.shardingsphere.elasticjob.spi.listener.ElasticJobListener;
import org.apache.shardingsphere.elasticjob.spi.listener.param.ShardingContexts;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingConfiguration;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* Sharding Job facade.
*/
@Slf4j
public final class ShardingJobFacade extends AbstractJobFacade {

private final ConfigurationService configService;

private final ShardingService shardingService;

private final ExecutionContextService executionContextService;

private final ExecutionService executionService;

private final FailoverService failoverService;

private final Collection<ElasticJobListener> elasticJobListeners;

private final JobTracingEventBus jobTracingEventBus;

public ShardingJobFacade(final CoordinatorRegistryCenter regCenter, final String jobName, final Collection<ElasticJobListener> elasticJobListeners, final TracingConfiguration<?> tracingConfig) {
super(regCenter, jobName, elasticJobListeners, tracingConfig);

configService = new ConfigurationService(regCenter, jobName);
shardingService = new ShardingService(regCenter, jobName);
executionContextService = new ExecutionContextService(regCenter, jobName);
executionService = new ExecutionService(regCenter, jobName);
failoverService = new FailoverService(regCenter, jobName);
this.elasticJobListeners = elasticJobListeners.stream().sorted(Comparator.comparingInt(ElasticJobListener::order)).collect(Collectors.toList());
this.jobTracingEventBus = null == tracingConfig ? new JobTracingEventBus() : new JobTracingEventBus(tracingConfig);
}

/**
* Get sharding contexts.
*
Expand All @@ -58,5 +89,5 @@ public ShardingContexts getShardingContexts() {
shardingItems.removeAll(executionService.getDisabledItems(shardingItems));
return executionContextService.getJobShardingContext(shardingItems);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.kernel.internal.config.ConfigurationService;
import org.apache.shardingsphere.elasticjob.kernel.internal.failover.FailoverService;
import org.apache.shardingsphere.elasticjob.kernel.internal.instance.InstanceService;
import org.apache.shardingsphere.elasticjob.kernel.internal.schedule.JobRegistry;
import org.apache.shardingsphere.elasticjob.kernel.internal.sharding.ExecutionContextService;
import org.apache.shardingsphere.elasticjob.kernel.internal.sharding.ExecutionService;
import org.apache.shardingsphere.elasticjob.kernel.internal.sharding.JobInstance;
import org.apache.shardingsphere.elasticjob.kernel.internal.sharding.ShardingService;
import org.apache.shardingsphere.elasticjob.kernel.internal.storage.JobNodeStorage;
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingConfiguration;
import org.apache.shardingsphere.elasticjob.kernel.tracing.event.JobTracingEventBus;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.spi.listener.ElasticJobListener;
import org.apache.shardingsphere.elasticjob.spi.listener.param.ShardingContexts;
Expand All @@ -40,21 +48,44 @@
*/
@Slf4j
public final class SingleShardingJobFacade extends AbstractJobFacade {


private final ConfigurationService configService;

private final ShardingService shardingService;

private final ExecutionContextService executionContextService;

private final ExecutionService executionService;

private final FailoverService failoverService;

private final Collection<ElasticJobListener> elasticJobListeners;

private final JobTracingEventBus jobTracingEventBus;

private final JobNodeStorage jobNodeStorage;

private final InstanceService instanceService;

public SingleShardingJobFacade(final CoordinatorRegistryCenter regCenter, final String jobName, final Collection<ElasticJobListener> elasticJobListeners, final TracingConfiguration<?> tracingConfig) {

public SingleShardingJobFacade(final CoordinatorRegistryCenter regCenter, final String jobName, final Collection<ElasticJobListener> elasticJobListeners,
final TracingConfiguration<?> tracingConfig) {
super(regCenter, jobName, elasticJobListeners, tracingConfig);


configService = new ConfigurationService(regCenter, jobName);
shardingService = new ShardingService(regCenter, jobName);
executionContextService = new ExecutionContextService(regCenter, jobName);
executionService = new ExecutionService(regCenter, jobName);
failoverService = new FailoverService(regCenter, jobName);
this.elasticJobListeners = elasticJobListeners.stream().sorted(Comparator.comparingInt(ElasticJobListener::order)).collect(Collectors.toList());
this.jobTracingEventBus = null == tracingConfig ? new JobTracingEventBus() : new JobTracingEventBus(tracingConfig);
jobNodeStorage = new JobNodeStorage(regCenter, jobName);
instanceService = new InstanceService(regCenter, jobName);
}

@Override
public void registerJobCompleted(final ShardingContexts shardingContexts) {
super.registerJobCompleted(shardingContexts);

JobConfiguration jobConfig = configService.load(true);
JobInstance jobInst = JobRegistry.getInstance().getJobInstance(jobConfig.getJobName());
if (null == jobInst) {
Expand All @@ -66,21 +97,21 @@ public void registerJobCompleted(final ShardingContexts shardingContexts) {
for (int i = 0; i < availJobInst.size(); i++) {
JobInstance temp = availJobInst.get(i);
if (temp.getServerIp().equals(jobInst.getServerIp())) {
nextIndex = i + 1; // find the current running job instance, and set next one to current index + 1
nextIndex = i + 1;
break;
}
}
if (nextIndex != null) { // the normal case that can find the next index, exclude the bounded scenarios
nextIndex = nextIndex >= availJobInst.size() ? 0 : nextIndex; // Round Robin Loop
if (nextIndex != null) {
nextIndex = nextIndex >= availJobInst.size() ? 0 : nextIndex;
jobNodeStorage.fillEphemeralJobNode("next-job-instance-ip", availJobInst.get(nextIndex).getServerIp());
}

if (log.isDebugEnabled()) {
log.debug("job name: {}, next index: {}, sharding total count: {}",
jobConfig.getJobName(), nextIndex, jobConfig.getShardingTotalCount());
jobConfig.getJobName(), nextIndex, jobConfig.getShardingTotalCount());
}
}

/**
* Get sharding contexts.
*
Expand All @@ -96,32 +127,32 @@ public ShardingContexts getShardingContexts() {
return executionContextService.getJobShardingContext(failoverShardingItems);
}
}

List<Integer> shardingItems;
String nextJobInstIP = null;
if (isNeedSharding()) { // the first initialization or reconcile case
if (isNeedSharding()) {
shardingService.shardingIfNecessary();
shardingItems = shardingService.getLocalShardingItems();
} else {
nextJobInstIP = jobNodeStorage.getJobNodeDataDirectly("next-job-instance-ip");
if (StringUtils.isBlank(nextJobInstIP)) { // if there is no next job instance ip
if (StringUtils.isBlank(nextJobInstIP)) {
shardingService.shardingIfNecessary();
shardingItems = shardingService.getLocalShardingItems();
} else { // when next job instance is specified under normal case
} else {
JobInstance jobInst = JobRegistry.getInstance().getJobInstance(jobConfig.getJobName());
shardingItems = nextJobInstIP.equals(jobInst.getServerIp()) ? Collections.singletonList(0) : new ArrayList<>();
}
}
if (log.isDebugEnabled()) {
log.debug("job name: {}, sharding items: {}, nextJobInstIP: {}, sharding total count: {}, isFailover: {}",
jobConfig.getJobName(), shardingItems, nextJobInstIP, jobConfig.getShardingTotalCount(), isFailover);
jobConfig.getJobName(), shardingItems, nextJobInstIP, jobConfig.getShardingTotalCount(), isFailover);
}

if (isFailover) {
shardingItems.removeAll(failoverService.getLocalTakeOffItems());
}
shardingItems.removeAll(executionService.getDisabledItems(shardingItems));
return executionContextService.getJobShardingContext(shardingItems);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ public JobScheduler(final CoordinatorRegistryCenter regCenter, final ElasticJob
Collection<ElasticJobListener> jobListeners = getElasticJobListeners(this.jobConfig);
setUpFacade = new SetUpFacade(regCenter, this.jobConfig.getJobName(), jobListeners);
schedulerFacade = new SchedulerFacade(regCenter, this.jobConfig.getJobName());

if (1 == this.jobConfig.getShardingTotalCount() // the single sharding scenario
&& "SINGLE_SHARDING_BALANCE".equals(this.jobConfig.getJobShardingStrategyType())) { // the specified SINGLE_SHARDING_BALANCE strategy

// the single sharding scenario and specified SINGLE_SHARDING_BALANCE strategy
if (1 == this.jobConfig.getShardingTotalCount()
&& "SINGLE_SHARDING_BALANCE".equals(this.jobConfig.getJobShardingStrategyType())) {
jobFacade = new SingleShardingJobFacade(regCenter, this.jobConfig.getJobName(), jobListeners, findTracingConfiguration().orElse(null));
} else {
jobFacade = new ShardingJobFacade(regCenter, this.jobConfig.getJobName(), jobListeners, findTracingConfiguration().orElse(null));
}

validateJobProperties();
jobExecutor = new ElasticJobExecutor(elasticJob, this.jobConfig, jobFacade);
setGuaranteeServiceForElasticJobListeners(regCenter, jobListeners);
Expand All @@ -103,14 +104,15 @@ public JobScheduler(final CoordinatorRegistryCenter regCenter, final String elas
Collection<ElasticJobListener> jobListeners = getElasticJobListeners(this.jobConfig);
setUpFacade = new SetUpFacade(regCenter, this.jobConfig.getJobName(), jobListeners);
schedulerFacade = new SchedulerFacade(regCenter, this.jobConfig.getJobName());

if (1 == this.jobConfig.getShardingTotalCount() // the single sharding scenario
&& "SINGLE_SHARDING_BALANCE".equals(this.jobConfig.getJobShardingStrategyType())) { // the specified SINGLE_SHARDING_BALANCE strategy

// the single sharding scenario and specified SINGLE_SHARDING_BALANCE strategy
if (1 == this.jobConfig.getShardingTotalCount()
&& "SINGLE_SHARDING_BALANCE".equals(this.jobConfig.getJobShardingStrategyType())) {
jobFacade = new SingleShardingJobFacade(regCenter, this.jobConfig.getJobName(), jobListeners, findTracingConfiguration().orElse(null));
} else {
jobFacade = new ShardingJobFacade(regCenter, this.jobConfig.getJobName(), jobListeners, findTracingConfiguration().orElse(null));
}

validateJobProperties();
jobExecutor = new ElasticJobExecutor(elasticJobType, this.jobConfig, jobFacade);
setGuaranteeServiceForElasticJobListeners(regCenter, jobListeners);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* 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.shardingsphere.elasticjob.kernel.internal.sharding.strategy.type;

import java.util.ArrayList;
Expand All @@ -22,26 +39,26 @@
* @since 2024-12-03 19:19
*/
public class SingleShardingBalanceJobShardingStrategy implements JobShardingStrategy {

private final AverageAllocationJobShardingStrategy averageAllocationJobShardingStrategy = new AverageAllocationJobShardingStrategy();

@Override
public Map<JobInstance, List<Integer>> sharding(final List<JobInstance> jobInstances, final String jobName, final int shardingTotalCount) {
int shardingUnitsSize = jobInstances.size();
int offset = Math.abs(jobName.hashCode() + ((Long)System.currentTimeMillis()).intValue()) % shardingUnitsSize;

int offset = Math.abs(jobName.hashCode() + ((Long) System.currentTimeMillis()).intValue()) % shardingUnitsSize;
List<JobInstance> result = new ArrayList<>(shardingUnitsSize);
for (int i = 0; i < shardingUnitsSize; i++) {
int index = (i + offset) % shardingUnitsSize;
result.add(jobInstances.get(index));
}

return averageAllocationJobShardingStrategy.sharding(result, jobName, shardingTotalCount);
}

@Override
public String getType() {
return "SINGLE_SHARDING_BALANCE";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ void setUp() {
ReflectionUtils.setSuperclassFieldValue(shardingJobFacade, "executionService", executionService);
ReflectionUtils.setSuperclassFieldValue(shardingJobFacade, "failoverService", failoverService);
ReflectionUtils.setSuperclassFieldValue(shardingJobFacade, "jobTracingEventBus", jobTracingEventBus);
ReflectionUtils.setFieldValue(shardingJobFacade, "configService", configService);
ReflectionUtils.setFieldValue(shardingJobFacade, "shardingService", shardingService);
ReflectionUtils.setFieldValue(shardingJobFacade, "executionContextService", executionContextService);
ReflectionUtils.setFieldValue(shardingJobFacade, "executionService", executionService);
ReflectionUtils.setFieldValue(shardingJobFacade, "failoverService", failoverService);
ReflectionUtils.setFieldValue(shardingJobFacade, "jobTracingEventBus", jobTracingEventBus);
}

@Test
Expand Down
Loading

0 comments on commit 012d7de

Please sign in to comment.