Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
jenkins
提交
e4458fbe
J
jenkins
项目概览
xxadev
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e4458fbe
编写于
4月 29, 2015
作者:
K
Kohsuke Kawaguchi
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1660 from deadmoose/really_sync
Fix concurrency issues in ConsistentHash
上级
e75bea89
57255ccb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
66 addition
and
20 deletion
+66
-20
core/src/main/java/hudson/util/ConsistentHash.java
core/src/main/java/hudson/util/ConsistentHash.java
+19
-20
core/src/test/java/hudson/util/ConsistentHashTest.java
core/src/test/java/hudson/util/ConsistentHashTest.java
+47
-0
未找到文件。
core/src/main/java/hudson/util/ConsistentHash.java
浏览文件 @
e4458fbe
...
...
@@ -64,6 +64,7 @@ public class ConsistentHash<T> {
* All the items in the hash, to their replication factors.
*/
private
final
Map
<
T
,
Point
[]>
items
=
new
HashMap
<
T
,
Point
[]>();
private
int
numPoints
;
private
final
int
defaultReplication
;
private
final
Hash
<
T
>
hash
;
...
...
@@ -100,8 +101,13 @@ public class ConsistentHash<T> {
private
final
Object
[]
owner
;
// really T[]
private
Table
()
{
int
r
=
0
;
for
(
Point
[]
v
:
items
.
values
())
r
+=
v
.
length
;
numPoints
=
r
;
// merge all points from all nodes and sort them into a single array
Point
[]
allPoints
=
new
Point
[
countAllPoints
()
];
Point
[]
allPoints
=
new
Point
[
numPoints
];
int
p
=
0
;
for
(
Point
[]
v
:
items
.
values
())
{
System
.
arraycopy
(
v
,
0
,
allPoints
,
p
,
v
.
length
);
...
...
@@ -186,18 +192,18 @@ public class ConsistentHash<T> {
String
hash
(
T
t
);
}
static
final
Hash
DEFAULT_HASH
=
new
Hash
()
{
static
final
Hash
<?>
DEFAULT_HASH
=
new
Hash
<
Object
>
()
{
public
String
hash
(
Object
o
)
{
return
o
.
toString
();
}
};
public
ConsistentHash
()
{
this
(
DEFAULT_HASH
);
this
(
(
Hash
<
T
>)
DEFAULT_HASH
);
}
public
ConsistentHash
(
int
defaultReplication
)
{
this
(
DEFAULT_HASH
,
defaultReplication
);
this
(
(
Hash
<
T
>)
DEFAULT_HASH
,
defaultReplication
);
}
public
ConsistentHash
(
Hash
<
T
>
hash
)
{
...
...
@@ -211,23 +217,20 @@ public class ConsistentHash<T> {
}
public
int
countAllPoints
()
{
int
r
=
0
;
for
(
Point
[]
v
:
items
.
values
())
r
+=
v
.
length
;
return
r
;
return
numPoints
;
}
/**
* Adds a new node with the default number of replica.
*/
public
void
add
(
T
node
)
{
public
synchronized
void
add
(
T
node
)
{
add
(
node
,
defaultReplication
);
}
/**
* Calls {@link #add(Object)} with all the arguments.
*/
public
void
addAll
(
T
...
nodes
)
{
public
synchronized
void
addAll
(
T
...
nodes
)
{
for
(
T
node
:
nodes
)
addInternal
(
node
,
defaultReplication
);
refreshTable
();
...
...
@@ -236,7 +239,7 @@ public class ConsistentHash<T> {
/**
* Calls {@link #add(Object)} with all the arguments.
*/
public
void
addAll
(
Collection
<?
extends
T
>
nodes
)
{
public
synchronized
void
addAll
(
Collection
<?
extends
T
>
nodes
)
{
for
(
T
node
:
nodes
)
addInternal
(
node
,
defaultReplication
);
refreshTable
();
...
...
@@ -245,7 +248,7 @@ public class ConsistentHash<T> {
/**
* Calls {@link #add(Object,int)} with all the arguments.
*/
public
void
addAll
(
Map
<?
extends
T
,
Integer
>
nodes
)
{
public
synchronized
void
addAll
(
Map
<?
extends
T
,
Integer
>
nodes
)
{
for
(
Map
.
Entry
<?
extends
T
,
Integer
>
node
:
nodes
.
entrySet
())
addInternal
(
node
.
getKey
(),
node
.
getValue
());
refreshTable
();
...
...
@@ -254,23 +257,20 @@ public class ConsistentHash<T> {
/**
* Removes the node entirely. This is the same as {@code add(node,0)}
*/
public
void
remove
(
T
node
)
{
public
synchronized
void
remove
(
T
node
)
{
add
(
node
,
0
);
}
/**
* Adds a new node with the given number of replica.
*
* <p>
* This is the only function that manipulates {@link #items}.
*/
public
synchronized
void
add
(
T
node
,
int
replica
)
{
addInternal
(
node
,
replica
);
refreshTable
();
}
private
void
addInternal
(
T
node
,
int
replica
)
{
if
(
replica
==
0
)
{
private
synchronized
void
addInternal
(
T
node
,
int
replica
)
{
if
(
replica
==
0
)
{
items
.
remove
(
node
);
}
else
{
Point
[]
points
=
new
Point
[
replica
];
...
...
@@ -281,11 +281,10 @@ public class ConsistentHash<T> {
}
}
private
void
refreshTable
()
{
private
synchronized
void
refreshTable
()
{
table
=
new
Table
();
}
/**
* Compresses a string into an integer with MD5.
*/
...
...
core/src/test/java/hudson/util/ConsistentHashTest.java
浏览文件 @
e4458fbe
...
...
@@ -122,11 +122,58 @@ public class ConsistentHashTest {
@Test
public
void
emptyBehavior
()
{
ConsistentHash
<
String
>
hash
=
new
ConsistentHash
<
String
>();
assertEquals
(
0
,
hash
.
countAllPoints
());
assertFalse
(
hash
.
list
(
0
).
iterator
().
hasNext
());
assertNull
(
hash
.
lookup
(
0
));
assertNull
(
hash
.
lookup
(
999
));
}
@Test
public
void
countAllPoints
()
{
ConsistentHash
<
String
>
hash
=
new
ConsistentHash
<
String
>();
assertEquals
(
0
,
hash
.
countAllPoints
());
hash
.
add
(
"foo"
,
10
);
assertEquals
(
10
,
hash
.
countAllPoints
());
hash
.
add
(
"bar"
,
5
);
assertEquals
(
15
,
hash
.
countAllPoints
());
hash
.
remove
(
"foo"
);
assertEquals
(
5
,
hash
.
countAllPoints
());
}
@Test
public
void
defaultReplicationIsOneHundred
()
{
ConsistentHash
<
String
>
hash
=
new
ConsistentHash
<
String
>();
assertEquals
(
0
,
hash
.
countAllPoints
());
hash
.
add
(
"foo"
);
assertEquals
(
100
,
hash
.
countAllPoints
());
}
@Test
public
void
setCustomDefaultReplication
()
{
ConsistentHash
<
String
>
hash
=
new
ConsistentHash
<
String
>((
ConsistentHash
.
Hash
<
String
>)
ConsistentHash
.
DEFAULT_HASH
,
7
);
assertEquals
(
0
,
hash
.
countAllPoints
());
hash
.
add
(
"foo"
);
assertEquals
(
7
,
hash
.
countAllPoints
());
}
@Test
public
void
usesCustomHash
()
{
final
RuntimeException
exception
=
new
RuntimeException
();
ConsistentHash
.
Hash
<
String
>
hashFunction
=
new
ConsistentHash
.
Hash
<
String
>()
{
public
String
hash
(
String
str
)
{
throw
exception
;
}
};
try
{
ConsistentHash
<
String
>
hash
=
new
ConsistentHash
<
String
>(
hashFunction
);
hash
.
add
(
"foo"
);
fail
(
"Didn't use custom hash function"
);
}
catch
(
RuntimeException
e
)
{
assertSame
(
exception
,
e
);
}
}
/**
* This test doesn't fail but it's written to measure the performance of the consistent hash function with large data set.
*/
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录