-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Improve] Add new sharding strategy, use odevity of job name's hash code
to decide ip address asc or desc #10
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...dangdang/ddframe/job/internal/sharding/strategy/OdevitySortByNameJobShardingStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...dang/ddframe/job/internal/sharding/strategy/OdevitySortByNameJobShardingStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |