diff --git a/elastic-job-core/src/main/java/com/dangdang/ddframe/job/internal/sharding/strategy/OdevitySortByNameJobShardingStrategy.java b/elastic-job-core/src/main/java/com/dangdang/ddframe/job/internal/sharding/strategy/OdevitySortByNameJobShardingStrategy.java new file mode 100644 index 0000000000..3b35dcf773 --- /dev/null +++ b/elastic-job-core/src/main/java/com/dangdang/ddframe/job/internal/sharding/strategy/OdevitySortByNameJobShardingStrategy.java @@ -0,0 +1,50 @@ +/** + * Copyright 1999-2015 dangdang.com. + *

+ * Licensed 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 com.dangdang.ddframe.job.internal.sharding.strategy; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +/** + * 根据作业名的哈希值奇偶数决定IP升降序算法的分片策略. + * + *

+ * 作业名的哈希值为奇数则IP升序. + * 作业名的哈希值为偶数则IP降序. + * 用于不同的作业平均分配负载至不同的服务器. + * 如: + * 1. 如果有3台服务器, 分成2片, 作业名称的哈希值为奇数, 则每台服务器分到的分片是: 1=[0], 2=[1], 3=[]. + * 2. 如果有3台服务器, 分成2片, 作业名称的哈希值为偶数, 则每台服务器分到的分片是: 3=[0], 2=[1], 1=[]. + *

+ * + * @author zhangliang + */ +public final class OdevitySortByNameJobShardingStrategy implements JobShardingStrategy { + + private AverageAllocationJobShardingStrategy averageAllocationJobShardingStrategy = new AverageAllocationJobShardingStrategy(); + + @Override + public Map> sharding(final List serversList, final JobShardingStrategyOption option) { + long jobNameHash = option.getJobName().hashCode(); + if (0 == jobNameHash % 2) { + Collections.reverse(serversList); + } + return averageAllocationJobShardingStrategy.sharding(serversList, option); + } +} diff --git a/elastic-job-core/src/test/java/com/dangdang/ddframe/job/AllJobTests.java b/elastic-job-core/src/test/java/com/dangdang/ddframe/job/AllJobTests.java index 7412539b60..5d267a8b70 100644 --- a/elastic-job-core/src/test/java/com/dangdang/ddframe/job/AllJobTests.java +++ b/elastic-job-core/src/test/java/com/dangdang/ddframe/job/AllJobTests.java @@ -52,6 +52,7 @@ import com.dangdang.ddframe.job.internal.sharding.ShardingServiceTest; import com.dangdang.ddframe.job.internal.sharding.strategy.AverageAllocationJobShardingStrategyTest; import com.dangdang.ddframe.job.internal.sharding.strategy.JobShardingStrategyFactoryTest; +import com.dangdang.ddframe.job.internal.sharding.strategy.OdevitySortByNameJobShardingStrategyTest; import com.dangdang.ddframe.job.internal.statistics.ProcessCountJobTest; import com.dangdang.ddframe.job.internal.statistics.ProcessCountStatisticsTest; import com.dangdang.ddframe.job.internal.storage.JobNodePathTest; @@ -83,6 +84,7 @@ OffsetNodeTest.class, JobShardingStrategyFactoryTest.class, AverageAllocationJobShardingStrategyTest.class, + OdevitySortByNameJobShardingStrategyTest.class, ProcessCountJobTest.class, ProcessCountStatisticsTest.class, DisabledJobTest.class, diff --git a/elastic-job-core/src/test/java/com/dangdang/ddframe/job/internal/sharding/strategy/OdevitySortByNameJobShardingStrategyTest.java b/elastic-job-core/src/test/java/com/dangdang/ddframe/job/internal/sharding/strategy/OdevitySortByNameJobShardingStrategyTest.java new file mode 100644 index 0000000000..0761de9c28 --- /dev/null +++ b/elastic-job-core/src/test/java/com/dangdang/ddframe/job/internal/sharding/strategy/OdevitySortByNameJobShardingStrategyTest.java @@ -0,0 +1,37 @@ +package com.dangdang.ddframe.job.internal.sharding.strategy; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; + +public final class OdevitySortByNameJobShardingStrategyTest { + + private OdevitySortByNameJobShardingStrategy odevitySortByNameJobShardingStrategy = new OdevitySortByNameJobShardingStrategy(); + + @Test + public void assertshardingByAsc() { + Map> expected = new LinkedHashMap<>(3); + expected.put("host0", Arrays.asList(0)); + expected.put("host1", Arrays.asList(1)); + expected.put("host2", Collections.emptyList()); + assertThat( + odevitySortByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("1", 2, Collections.emptyMap())), is(expected)); + } + + @Test + public void assertshardingByDesc() { + Map> expected = new LinkedHashMap<>(3); + expected.put("host2", Arrays.asList(0)); + expected.put("host1", Arrays.asList(1)); + expected.put("host0", Collections.emptyList()); + assertThat( + odevitySortByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("0", 2, Collections.emptyMap())), is(expected)); + } +}