This repository was archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpeers.py
96 lines (76 loc) · 3.14 KB
/
peers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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.
import json
from charms.leadership import leader_get
from charms.reactive import RelationBase, hook, scopes
class ZookeeperPeers(RelationBase):
scope = scopes.UNIT
@hook('{peers:zookeeper-quorum}-relation-joined')
def joined(self):
conv = self.conversation()
conv.remove_state('{relation_name}.departed')
conv.set_state('{relation_name}.joined')
@hook('{peers:zookeeper-quorum}-relation-departed')
def departed(self):
conv = self.conversation()
conv.remove_state('{relation_name}.joined')
conv.set_state('{relation_name}.departed')
@hook('{peers:zookeeper-quorum}-relation-changed')
def changed(self):
conv = self.conversation()
conv.set_state('{relation_name}.changed')
def dismiss_departed(self):
for conv in self.conversations():
conv.remove_state('{relation_name}.departed')
def dismiss_joined(self):
for conv in self.conversations():
conv.remove_state('{relation_name}.joined')
def dismiss_changed(self):
for conv in self.conversations():
conv.remove_state('{relation_name}.changed')
def get_nodes(self):
nodes = []
for conv in self.conversations():
nodes.append((conv.scope, conv.get_remote('private-address')))
return nodes
def restarted_nodes(self):
nodes = []
nonce = leader_get('restart_nonce')
if not nonce:
return nodes # We're not restarting if no nonce is set.
for conv in self.conversations():
if conv.get_remote('restarted.{}'.format(nonce)):
nodes.append((conv.scope, conv.get_remote('private-address')))
return nodes
def set_zk_leader(self):
'''
Inform peers that the unit that calls this method is the Zookeeper leader.
Note that Zookeeper tracks leadership separately from juju;
the Zookeeper leader is not necessarily the Juju leader.
'''
for conv in self.conversations():
conv.set_remote('is_zk_leader', True)
def find_zk_leader(self):
'''
Find the private address of the leader.
'''
for conv in self.conversations():
if conv.get_remote('is_zk_leader'):
return conv.get_remote('private_address')
def inform_restart(self):
'''
Inform our peers that we have restarted, usually as part of a
rolling restart.
'''
for conv in self.conversations():
nonce = leader_get('restart_nonce')
conv.set_remote('restarted.{}'.format(nonce), json.dumps(True))