Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
19695040
C
ClickHouse
项目概览
2dot5
/
ClickHouse
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
ClickHouse
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
19695040
编写于
3月 29, 2016
作者:
A
Alexey Milovidov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixed leader election [#METR-20132].
上级
48d502a2
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
51 addition
and
21 deletion
+51
-21
libs/libzkutil/include/zkutil/LeaderElection.h
libs/libzkutil/include/zkutil/LeaderElection.h
+27
-19
libs/libzkutil/include/zkutil/ZooKeeper.h
libs/libzkutil/include/zkutil/ZooKeeper.h
+24
-2
未找到文件。
libs/libzkutil/include/zkutil/LeaderElection.h
浏览文件 @
19695040
...
@@ -19,13 +19,36 @@ public:
...
@@ -19,13 +19,36 @@ public:
*/
*/
LeaderElection
(
const
std
::
string
&
path_
,
ZooKeeper
&
zookeeper_
,
LeadershipHandler
handler_
,
const
std
::
string
&
identifier_
=
""
)
LeaderElection
(
const
std
::
string
&
path_
,
ZooKeeper
&
zookeeper_
,
LeadershipHandler
handler_
,
const
std
::
string
&
identifier_
=
""
)
:
path
(
path_
),
zookeeper
(
zookeeper_
),
handler
(
handler_
),
identifier
(
identifier_
),
:
path
(
path_
),
zookeeper
(
zookeeper_
),
handler
(
handler_
),
identifier
(
identifier_
),
shutdown
(
false
),
state
(
WAITING_LEADERSHIP
),
log
(
&
Logger
::
get
(
"LeaderElection"
))
log
(
&
Logger
::
get
(
"LeaderElection"
))
{
{
node
=
EphemeralNodeHolder
::
createSequential
(
path
+
"/leader_election-"
,
zookeeper
,
identifier
);
node
=
EphemeralNodeHolder
::
createSequential
(
path
+
"/leader_election-"
,
zookeeper
,
identifier
);
std
::
string
node_path
=
node
->
getPath
();
std
::
string
node_path
=
node
->
getPath
();
node_name
=
node_path
.
substr
(
node_path
.
find_last_of
(
'/'
)
+
1
);
node_name
=
node_path
.
substr
(
node_path
.
find_last_of
(
'/'
)
+
1
);
/** Если есть ноды с таким же ephemeralOwner, то удалим их.
* Такие ноды могли остаться после неуспешного удаления, если сессия при этом не истекла.
*/
zkutil
::
Stat
node_stat
;
zookeeper
.
get
(
node_path
,
&
node_stat
);
Strings
brothers
=
zookeeper
.
getChildren
(
path
);
for
(
const
auto
&
brother
:
brothers
)
{
if
(
brother
==
node_name
)
continue
;
zkutil
::
Stat
brother_stat
;
std
::
string
brother_path
=
path
+
"/"
+
brother
;
zookeeper
.
get
(
brother_path
,
&
brother_stat
);
if
(
brother_stat
.
ephemeralOwner
==
node_stat
.
ephemeralOwner
)
{
LOG_WARNING
(
log
,
"Found obsolete ephemeral node from same session, removing: "
+
brother_path
);
zookeeper
.
tryRemoveWithRetries
(
brother_path
);
}
}
thread
=
std
::
thread
(
&
LeaderElection
::
threadFunction
,
this
);
thread
=
std
::
thread
(
&
LeaderElection
::
threadFunction
,
this
);
}
}
...
@@ -78,10 +101,10 @@ private:
...
@@ -78,10 +101,10 @@ private:
std
::
string
node_name
;
std
::
string
node_name
;
std
::
thread
thread
;
std
::
thread
thread
;
volatile
bool
shutdown
;
volatile
bool
shutdown
=
false
;
zkutil
::
EventPtr
event
=
new
Poco
::
Event
();
zkutil
::
EventPtr
event
=
new
Poco
::
Event
();
State
state
;
State
state
=
WAITING_LEADERSHIP
;
Logger
*
log
;
Logger
*
log
;
...
@@ -111,24 +134,9 @@ private:
...
@@ -111,24 +134,9 @@ private:
success
=
true
;
success
=
true
;
}
}
catch
(
const
DB
::
Exception
&
e
)
{
LOG_ERROR
(
log
,
"Exception in LeaderElection: Code: "
<<
e
.
code
()
<<
". "
<<
e
.
displayText
()
<<
std
::
endl
<<
std
::
endl
<<
"Stack trace:"
<<
std
::
endl
<<
e
.
getStackTrace
().
toString
());
}
catch
(
const
Poco
::
Exception
&
e
)
{
LOG_ERROR
(
log
,
"Poco::Exception in LeaderElection: "
<<
e
.
code
()
<<
". "
<<
e
.
displayText
());
}
catch
(
const
std
::
exception
&
e
)
{
LOG_ERROR
(
log
,
"std::exception in LeaderElection: "
<<
e
.
what
());
}
catch
(...)
catch
(...)
{
{
LOG_ERROR
(
log
,
"Unknown exception in
LeaderElection"
);
DB
::
tryLogCurrentException
(
"
LeaderElection"
);
}
}
if
(
!
success
)
if
(
!
success
)
...
...
libs/libzkutil/include/zkutil/ZooKeeper.h
浏览文件 @
19695040
...
@@ -395,16 +395,38 @@ public:
...
@@ -395,16 +395,38 @@ public:
{
{
try
try
{
{
/** Важно, что в случае недоступности ZooKeeper, делаются повторные попытки удалить ноду
, пока не истечёт сессия
.
/** Важно, что в случае недоступности ZooKeeper, делаются повторные попытки удалить ноду.
* Иначе возможна ситуация, когда объект EphemeralNodeHolder уничтожен,
* Иначе возможна ситуация, когда объект EphemeralNodeHolder уничтожен,
* но сессия восстановится в течние session timeout, и эфемерная нода в ZooKeeper останется ещё надолго.
* но сессия восстановится в течние session timeout, и эфемерная нода в ZooKeeper останется ещё надолго.
*
А это может сломать механизм leader election и блокировок
.
*
Но см. ниже - на самом деле, такая ситуация всё-равно возможна
.
*/
*/
zookeeper
.
tryRemoveWithRetries
(
path
);
zookeeper
.
tryRemoveWithRetries
(
path
);
}
}
catch
(
const
KeeperException
&
e
)
catch
(
const
KeeperException
&
e
)
{
{
LOG_ERROR
(
zookeeper
.
log
,
"~EphemeralNodeHolder(): "
<<
e
.
displayText
());
LOG_ERROR
(
zookeeper
.
log
,
"~EphemeralNodeHolder(): "
<<
e
.
displayText
());
/** На самом деле, сессия может ещё жить после этой ошибки.
* Поэтому не стоит рассчитывать, что эфемерная нода действительно будет удалена.
Ridiculously Long Delay to Expire
When disconnects do happen, the common case should be a very* quick
reconnect to another server, but an extended network outage may
introduce a long delay before a client can reconnect to the ZooKeep‐
er service. Some developers wonder why the ZooKeeper client li‐
brary doesn’t simply decide at some point (perhaps twice the session
timeout) that enough is enough and kill the session itself.
There are two answers to this. First, ZooKeeper leaves this kind of
policy decision up to the developer. Developers can easily implement
such a policy by closing the handle themselves. Second, when a Zoo‐
Keeper ensemble goes down, time freezes. Thus, when the ensemble is
brought back up, session timeouts are restarted. If processes using
ZooKeeper hang in there, they may find out that the long timeout was
due to an extended ensemble failure that has recovered and pick right
up where they left off without any additional startup delay.
ZooKeeper: Distributed Process Coordination p118
*/
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录