Skip to content

Commit

Permalink
[Improve] Add new sharding strategy, use odevity of job name's hash code
Browse files Browse the repository at this point in the history
to decide ip address asc or desc #10
  • Loading branch information
terrymanu committed Oct 14, 2015
1 parent fe6bb40 commit e5858d9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 1999-2015 dangdang.com.
* <p>
* 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.
* </p>
*/

package com.dangdang.ddframe.job.internal.sharding.strategy;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* 根据作业名的哈希值奇偶数决定IP升降序算法的分片策略.
*
* <p>
* 作业名的哈希值为奇数则IP升序.
* 作业名的哈希值为偶数则IP降序.
* 用于不同的作业平均分配负载至不同的服务器.
* 如:
* 1. 如果有3台服务器, 分成2片, 作业名称的哈希值为奇数, 则每台服务器分到的分片是: 1=[0], 2=[1], 3=[].
* 2. 如果有3台服务器, 分成2片, 作业名称的哈希值为偶数, 则每台服务器分到的分片是: 3=[0], 2=[1], 1=[].
* </p>
*
* @author zhangliang
*/
public final class OdevitySortByNameJobShardingStrategy implements JobShardingStrategy {

private AverageAllocationJobShardingStrategy averageAllocationJobShardingStrategy = new AverageAllocationJobShardingStrategy();

@Override
public Map<String, List<Integer>> sharding(final List<String> serversList, final JobShardingStrategyOption option) {
long jobNameHash = option.getJobName().hashCode();
if (0 == jobNameHash % 2) {
Collections.reverse(serversList);
}
return averageAllocationJobShardingStrategy.sharding(serversList, option);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,6 +84,7 @@
OffsetNodeTest.class,
JobShardingStrategyFactoryTest.class,
AverageAllocationJobShardingStrategyTest.class,
OdevitySortByNameJobShardingStrategyTest.class,
ProcessCountJobTest.class,
ProcessCountStatisticsTest.class,
DisabledJobTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, List<Integer>> expected = new LinkedHashMap<>(3);
expected.put("host0", Arrays.asList(0));
expected.put("host1", Arrays.asList(1));
expected.put("host2", Collections.<Integer>emptyList());
assertThat(
odevitySortByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("1", 2, Collections.<Integer, String>emptyMap())), is(expected));
}

@Test
public void assertshardingByDesc() {
Map<String, List<Integer>> expected = new LinkedHashMap<>(3);
expected.put("host2", Arrays.asList(0));
expected.put("host1", Arrays.asList(1));
expected.put("host0", Collections.<Integer>emptyList());
assertThat(
odevitySortByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("0", 2, Collections.<Integer, String>emptyMap())), is(expected));
}
}

0 comments on commit e5858d9

Please sign in to comment.