|
@@ -153,7 +153,13 @@ public class ZKRMStateStore extends RMStateStore {
|
|
|
|
|
|
@VisibleForTesting
|
|
|
protected ZooKeeper zkClient;
|
|
|
- private ZooKeeper oldZkClient;
|
|
|
+
|
|
|
+ /* activeZkClient is not used to do actual operations,
|
|
|
+ * it is only used to verify client session for watched events and
|
|
|
+ * it gets activated into zkClient on connection event.
|
|
|
+ */
|
|
|
+ @VisibleForTesting
|
|
|
+ ZooKeeper activeZkClient;
|
|
|
|
|
|
/** Fencing related variables */
|
|
|
private static final String FENCING_LOCK = "RM_ZK_FENCING_LOCK";
|
|
@@ -355,21 +361,14 @@ public class ZKRMStateStore extends RMStateStore {
|
|
|
}
|
|
|
|
|
|
private synchronized void closeZkClients() throws IOException {
|
|
|
- if (zkClient != null) {
|
|
|
+ zkClient = null;
|
|
|
+ if (activeZkClient != null) {
|
|
|
try {
|
|
|
- zkClient.close();
|
|
|
+ activeZkClient.close();
|
|
|
} catch (InterruptedException e) {
|
|
|
throw new IOException("Interrupted while closing ZK", e);
|
|
|
}
|
|
|
- zkClient = null;
|
|
|
- }
|
|
|
- if (oldZkClient != null) {
|
|
|
- try {
|
|
|
- oldZkClient.close();
|
|
|
- } catch (InterruptedException e) {
|
|
|
- throw new IOException("Interrupted while closing old ZK", e);
|
|
|
- }
|
|
|
- oldZkClient = null;
|
|
|
+ activeZkClient = null;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -860,11 +859,16 @@ public class ZKRMStateStore extends RMStateStore {
|
|
|
* hides the ZK methods of the store from its public interface
|
|
|
*/
|
|
|
private final class ForwardingWatcher implements Watcher {
|
|
|
+ private ZooKeeper watchedZkClient;
|
|
|
+
|
|
|
+ public ForwardingWatcher(ZooKeeper client) {
|
|
|
+ this.watchedZkClient = client;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void process(WatchedEvent event) {
|
|
|
try {
|
|
|
- ZKRMStateStore.this.processWatchEvent(event);
|
|
|
+ ZKRMStateStore.this.processWatchEvent(watchedZkClient, event);
|
|
|
} catch (Throwable t) {
|
|
|
LOG.error("Failed to process watcher event " + event + ": "
|
|
|
+ StringUtils.stringifyException(t));
|
|
@@ -875,8 +879,16 @@ public class ZKRMStateStore extends RMStateStore {
|
|
|
@VisibleForTesting
|
|
|
@Private
|
|
|
@Unstable
|
|
|
- public synchronized void processWatchEvent(WatchedEvent event)
|
|
|
- throws Exception {
|
|
|
+ public synchronized void processWatchEvent(ZooKeeper zk,
|
|
|
+ WatchedEvent event) throws Exception {
|
|
|
+ // only process watcher event from current ZooKeeper Client session.
|
|
|
+ if (zk != activeZkClient) {
|
|
|
+ LOG.info("Ignore watcher event type: " + event.getType() +
|
|
|
+ " with state:" + event.getState() + " for path:" +
|
|
|
+ event.getPath() + " from old session");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
Event.EventType eventType = event.getType();
|
|
|
LOG.info("Watcher event type: " + eventType + " with state:"
|
|
|
+ event.getState() + " for path:" + event.getPath() + " for " + this);
|
|
@@ -887,17 +899,15 @@ public class ZKRMStateStore extends RMStateStore {
|
|
|
switch (event.getState()) {
|
|
|
case SyncConnected:
|
|
|
LOG.info("ZKRMStateStore Session connected");
|
|
|
- if (oldZkClient != null) {
|
|
|
+ if (zkClient == null) {
|
|
|
// the SyncConnected must be from the client that sent Disconnected
|
|
|
- zkClient = oldZkClient;
|
|
|
- oldZkClient = null;
|
|
|
+ zkClient = activeZkClient;
|
|
|
ZKRMStateStore.this.notifyAll();
|
|
|
LOG.info("ZKRMStateStore Session restored");
|
|
|
}
|
|
|
break;
|
|
|
case Disconnected:
|
|
|
LOG.info("ZKRMStateStore Session disconnected");
|
|
|
- oldZkClient = zkClient;
|
|
|
zkClient = null;
|
|
|
break;
|
|
|
case Expired:
|
|
@@ -1127,7 +1137,8 @@ public class ZKRMStateStore extends RMStateStore {
|
|
|
for (int retries = 0; retries < numRetries && zkClient == null;
|
|
|
retries++) {
|
|
|
try {
|
|
|
- zkClient = getNewZooKeeper();
|
|
|
+ activeZkClient = getNewZooKeeper();
|
|
|
+ zkClient = activeZkClient;
|
|
|
for (ZKUtil.ZKAuthInfo zkAuth : zkAuths) {
|
|
|
zkClient.addAuthInfo(zkAuth.getScheme(), zkAuth.getAuth());
|
|
|
}
|
|
@@ -1157,7 +1168,7 @@ public class ZKRMStateStore extends RMStateStore {
|
|
|
protected synchronized ZooKeeper getNewZooKeeper()
|
|
|
throws IOException, InterruptedException {
|
|
|
ZooKeeper zk = new ZooKeeper(zkHostPort, zkSessionTimeout, null);
|
|
|
- zk.register(new ForwardingWatcher());
|
|
|
+ zk.register(new ForwardingWatcher(zk));
|
|
|
return zk;
|
|
|
}
|
|
|
|