Skip to content

Commit

Permalink
fix for checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Oct 15, 2015
1 parent 7a018c1 commit c99ba2e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,28 @@
/**
* 根据作业名的哈希值对服务器列表进行轮转的分片策略.
*
* <p>
* 可实现一定的服务器负载均衡
* </p>
*
* @author weishubin
*/
public class RotateServerByNameJobShardingStrategy implements JobShardingStrategy {
private AverageAllocationJobShardingStrategy averageAllocationJobShardingStrategy = new AverageAllocationJobShardingStrategy();

private AverageAllocationJobShardingStrategy averageAllocationJobShardingStrategy = new AverageAllocationJobShardingStrategy();

@Override
public Map<String, List<Integer>> sharding(final List<String> serversList, final JobShardingStrategyOption option) {
List<String> roated = rotateServerList(serversList, option.getJobName());
return averageAllocationJobShardingStrategy.sharding(roated, option);
return averageAllocationJobShardingStrategy.sharding(rotateServerList(serversList, option.getJobName()), option);
}

private List<String> rotateServerList(List<String> serversList, String jobName) {
int serverSize = serversList.size();
long jobNameHash = jobName.hashCode();
int offset = (int) (jobNameHash % serverSize);
if (offset == 0) {
return serversList;
}
List<String> result = new ArrayList<String>(serverSize);
for (int i = 0; i < serverSize; i++) {
int index = (i + offset) % serverSize;
result.add(serversList.get(index));
}
return result;
private List<String> rotateServerList(final List<String> serversList, final String jobName) {
int serverSize = serversList.size();
int offset = jobName.hashCode() % serverSize;
if (0 == offset) {
return serversList;
}
List<String> result = new ArrayList<String>(serverSize);
for (int i = 0; i < serverSize; i++) {
int index = (i + offset) % serverSize;
result.add(serversList.get(index));
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

import com.dangdang.ddframe.job.internal.sharding.strategy.JobShardingStrategyOption;

public class RotateServerByNameJobShardingStrategyTest {
public final class RotateServerByNameJobShardingStrategyTest {

private RotateServerByNameJobShardingStrategy odevitySortByNameJobShardingStrategy = new RotateServerByNameJobShardingStrategy();
private RotateServerByNameJobShardingStrategy rotateServerByNameJobShardingStrategy = new RotateServerByNameJobShardingStrategy();

@Test
public void assertsharding1() {
Expand All @@ -24,7 +24,7 @@ public void assertsharding1() {
expected.put("host1", Arrays.asList(0));
expected.put("host2", Arrays.asList(1));
assertThat(
odevitySortByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("1", 2, Collections.<Integer, String>emptyMap())), is(expected));
rotateServerByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("1", 2, Collections.<Integer, String>emptyMap())), is(expected));
}

@Test
Expand All @@ -34,7 +34,7 @@ public void assertsharding2() {
expected.put("host1", Collections.<Integer>emptyList());
expected.put("host2", Arrays.asList(0));
assertThat(
odevitySortByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("2", 2, Collections.<Integer, String>emptyMap())), is(expected));
rotateServerByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("2", 2, Collections.<Integer, String>emptyMap())), is(expected));
}

@Test
Expand All @@ -44,6 +44,6 @@ public void assertsharding3() {
expected.put("host1", Arrays.asList(1));
expected.put("host2", Collections.<Integer>emptyList());
assertThat(
odevitySortByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("3", 2, Collections.<Integer, String>emptyMap())), is(expected));
rotateServerByNameJobShardingStrategy.sharding(Arrays.asList("host0", "host1", "host2"), new JobShardingStrategyOption("3", 2, Collections.<Integer, String>emptyMap())), is(expected));
}
}

0 comments on commit c99ba2e

Please sign in to comment.