1
/**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.zookeeper;
20
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.CopyOnWriteArrayList;
26 import java.util.concurrent.CountDownLatch;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.classification.InterfaceAudience;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.Abortable;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
35 import org.apache.hadoop.hbase.util.Threads;
36 import org.apache.zookeeper.KeeperException;
37 import org.apache.zookeeper.WatchedEvent;
38 import org.apache.zookeeper.Watcher;
39 import org.apache.zookeeper.ZooDefs;
40 import org.apache.zookeeper.data.ACL;
41
42 /**
43 * Acts as the single ZooKeeper Watcher. One instance of this is instantiated
44 * for each Master, RegionServer, and client process.
45 *
46 * <p>This is the only class that implements {@link Watcher}. Other internal
47 * classes which need to be notified of ZooKeeper events must register with
48 * the local instance of this watcher via {@link #registerListener}.
49 *
50 * <p>This class also holds and manages the connection to ZooKeeper. Code to
51 * deal with connection related events and exceptions are handled here.
52 */
53 @InterfaceAudience.Private
54 public class ZooKeeperWatcher implements Watcher, Abortable, Closeable {
55 private static final Log LOG = LogFactory.getLog(ZooKeeperWatcher. class);
56
57 // Identifier for this watcher (for logging only). It is made of the prefix
58 // passed on construction and the zookeeper sessionid.
59 private String identifier;
60
61 // zookeeper quorum
62 private String quorum;
63
64 // zookeeper connection
65 private RecoverableZooKeeper recoverableZooKeeper;
66
67 // abortable in case of zk failure
68 protected Abortable abortable;
69 // Used if abortable is null
70 private boolean aborted = false;
71
72 // listeners to be notified
73 private final List<ZooKeeperListener> listeners =
74 new CopyOnWriteArrayList<ZooKeeperListener>();
75
76 // Used by ZKUtil:waitForZKConnectionIfAuthenticating to wait for SASL
77 // negotiation to complete
78 public CountDownLatch saslLatch = new CountDownLatch(1);
79
80 // node names
81
82 // base znode for this cluster
83 public String baseZNode;
84 // znode containing location of server hosting meta region
85 public String metaServerZNode;
86 // znode containing ephemeral nodes of the regionservers
87 public String rsZNode;
88 // znode containing ephemeral nodes of the draining regionservers
89 public String drainingZNode;
90 // znode of currently active master
91 private String masterAddressZNode;
92 // znode of this master in backup master directory, if not the active master
93 public String backupMasterAddressesZNode;
94 // znode containing the current cluster state
95 public String clusterStateZNode;
96 // znode used for region transitioning and assignment
97 public String assignmentZNode;
98 // znode used for table disabling/enabling
99 public String tableZNode;
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 package org.apache.hadoop.hbase.zookeeper;
20
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.CopyOnWriteArrayList;
26 import java.util.concurrent.CountDownLatch;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.classification.InterfaceAudience;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.Abortable;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
35 import org.apache.hadoop.hbase.util.Threads;
36 import org.apache.zookeeper.KeeperException;
37 import org.apache.zookeeper.WatchedEvent;
38 import org.apache.zookeeper.Watcher;
39 import org.apache.zookeeper.ZooDefs;
40 import org.apache.zookeeper.data.ACL;
41
42 /**
43 * Acts as the single ZooKeeper Watcher. One instance of this is instantiated
44 * for each Master, RegionServer, and client process.
45 *
46 * <p>This is the only class that implements {@link Watcher}. Other internal
47 * classes which need to be notified of ZooKeeper events must register with
48 * the local instance of this watcher via {@link #registerListener}.
49 *
50 * <p>This class also holds and manages the connection to ZooKeeper. Code to
51 * deal with connection related events and exceptions are handled here.
52 */
53 @InterfaceAudience.Private
54 public class ZooKeeperWatcher implements Watcher, Abortable, Closeable {
55 private static final Log LOG = LogFactory.getLog(ZooKeeperWatcher. class);
56
57 // Identifier for this watcher (for logging only). It is made of the prefix
58 // passed on construction and the zookeeper sessionid.
59 private String identifier;
60
61 // zookeeper quorum
62 private String quorum;
63
64 // zookeeper connection
65 private RecoverableZooKeeper recoverableZooKeeper;
66
67 // abortable in case of zk failure
68 protected Abortable abortable;
69 // Used if abortable is null
70 private boolean aborted = false;
71
72 // listeners to be notified
73 private final List<ZooKeeperListener> listeners =
74 new CopyOnWriteArrayList<ZooKeeperListener>();
75
76 // Used by ZKUtil:waitForZKConnectionIfAuthenticating to wait for SASL
77 // negotiation to complete
78 public CountDownLatch saslLatch = new CountDownLatch(1);
79
80 // node names
81
82 // base znode for this cluster
83 public String baseZNode;
84 // znode containing location of server hosting meta region
85 public String metaServerZNode;
86 // znode containing ephemeral nodes of the regionservers
87 public String rsZNode;
88 // znode containing ephemeral nodes of the draining regionservers
89 public String drainingZNode;
90 // znode of currently active master
91 private String masterAddressZNode;
92 // znode of this master in backup master directory, if not the active master
93 public String backupMasterAddressesZNode;
94 // znode containing the current cluster state
95 public String clusterStateZNode;
96 // znode used for region transitioning and assignment
97 public String assignmentZNode;
98 // znode used for table disabling/enabling
99 public String tableZNode;