forked from IBM/etcd-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEtcdLeaderElection.java
More file actions
171 lines (140 loc) · 5.17 KB
/
EtcdLeaderElection.java
File metadata and controls
171 lines (140 loc) · 5.17 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2017, 2018 IBM Corp. All Rights Reserved.
*
* 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.
*/
package com.ibm.etcd.client.utils;
import static com.ibm.etcd.client.KeyUtils.bs;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.protobuf.ByteString;
import com.ibm.etcd.client.EtcdClient;
import com.ibm.etcd.client.utils.RangeCache.Listener;
import com.ibm.etcd.api.KeyValue;
/**
* Etcd-based leader election pattern
*
*/
public class EtcdLeaderElection implements AutoCloseable {
protected static final Logger logger = LoggerFactory.getLogger(EtcdLeaderElection.class);
protected final ByteString bytesPrefix;
protected final int prefixLen;
protected final String id;
protected final RangeCache candidates;
protected final PersistentLeaseKey ourCandidate; // null if id is null
protected final List<ElectionListener> listeners = new CopyOnWriteArrayList<>();
protected volatile String leaderId;
protected volatile boolean leader; // us
protected boolean initialized;
/**
* Observer-only mode, won't participate in election, leaders have no effect
*/
public EtcdLeaderElection(EtcdClient client, ByteString prefix) {
this(client, prefix, null);
}
// maybe use serializedexecutor here
public EtcdLeaderElection(EtcdClient client, ByteString prefix, String candidateId) {
if(candidateId != null && candidateId.contains("\\n")) {
throw new IllegalArgumentException("id can't contain linebreak");
}
this.bytesPrefix = prefix;
this.prefixLen = bytesPrefix.size();
this.id = candidateId;
this.candidates = new RangeCache(client, prefix, true);
candidates.addListener((event,kv) -> {
if(!initialized) {
if(event != Listener.EventType.INITIALIZED) return;
initialized = true;
}
updateLeader();
});
ourCandidate = id == null ? null
: new PersistentLeaseKey(client,
cacheKey(id), bs(id), candidates);
}
private void updateLeader() {
synchronized(candidates) {
if(candidates.isClosed()) return;
KeyValue chosen = null;
for(KeyValue kv : candidates) if(chosen == null ||
kv.getCreateRevision() < chosen.getCreateRevision()) chosen = kv;
String chosenId = chosen == null ? null : candId(chosen);
String priorLeaderId = leaderId;
leaderId = chosenId;
if(id == null) return; // observer only
boolean wasUs = id.equals(priorLeaderId), isUs = id.equals(chosenId);
if(wasUs ^ isUs) {
leader = isUs;
notifyListeners(isUs);
}
}
}
public String getId() {
return id;
}
public String getLeaderId() {
return leaderId;
}
public boolean isLeader() {
return leader;
}
public void addListener(ElectionListener listener) {
listeners.add(listener);
}
public boolean removeListener(ElectionListener listener) {
return listeners.remove(listener);
}
//TODO custom executors ?
protected void notifyListeners(boolean isLeader) {
for(ElectionListener l : listeners) try {
l.leadershipChange(isLeader);
} catch(Exception e) {
logger.warn("ElectionListener threw exception", e);
}
}
public void start() throws Exception {
synchronized(candidates) {
candidates.start();
if(ourCandidate != null) ourCandidate.start();
}
}
@Override
public synchronized void close() {
synchronized(candidates) {
if(ourCandidate != null) ourCandidate.close();
candidates.close();
leader = false;
leaderId = null;
}
}
// ------ static conversion utility methods
protected ByteString cacheKey(String str) {
return str == null ? null : bytesPrefix.concat(bs(str));
}
protected static String candId(KeyValue kv) {
String str = kv.getValue().toStringUtf8();
int nl = str.indexOf('\n');
return nl == -1 ? str : str.substring(0, nl);
}
// ------ election listener class
public interface ElectionListener {
/**
* Called if our leadership status changes
*
* @param isLeader true if we are the leader, false otherwise
*/
void leadershipChange(boolean isLeader);
}
}