2 * ============LICENSE_START========================================== org.onap.music
3 * =================================================================== Copyright (c) 2017 AT&T
4 * Intellectual Property ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
15 * ============LICENSE_END=============================================
16 * ====================================================================
19 package org.onap.music.lockingservice;
22 import java.util.List;
23 import java.util.SortedSet;
24 import java.util.TreeSet;
25 import org.apache.zookeeper.CreateMode;
26 import org.apache.zookeeper.KeeperException;
27 import org.apache.zookeeper.KeeperException.NoNodeException;
28 import org.apache.zookeeper.ZooDefs;
29 import org.apache.zookeeper.ZooKeeper;
30 import org.apache.zookeeper.data.ACL;
31 import org.apache.zookeeper.data.Stat;
32 import org.onap.music.eelf.logging.EELFLoggerDelegate;
33 import org.onap.music.eelf.logging.format.AppMessages;
34 import org.onap.music.eelf.logging.format.ErrorSeverity;
35 import org.onap.music.eelf.logging.format.ErrorTypes;
38 * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>.
40 * You invoke {@link #lock()} to start the process of grabbing the lock; you may get the lock then
41 * or it may be some time later.
43 * You can register a listener so that you are invoked when you get the lock; otherwise you can ask
44 * if you have the lock by calling {@link #isOwner()}
47 public class ZkStatelessLockService extends ProtocolSupport {
48 public ZkStatelessLockService(ZooKeeper zk) {
52 private static EELFLoggerDelegate logger =
53 EELFLoggerDelegate.getLogger(ZkStatelessLockService.class);
55 protected void createLock(final String path, final byte[] data) {
56 final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
58 retryOperation(new ZooKeeperOperation() {
59 public boolean execute() throws KeeperException, InterruptedException {
60 zookeeper.create(path, data, acl, CreateMode.PERSISTENT);
64 }catch (InterruptedException e) {
65 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
66 }catch (KeeperException e) {
67 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
74 }catch (InterruptedException e) {
75 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
79 public void setNodeData(final String lockName, final byte[] data) {
81 retryOperation(new ZooKeeperOperation() {
82 public boolean execute() throws KeeperException, InterruptedException {
83 zookeeper.getSessionId();
84 zookeeper.setData("/" + lockName, data, -1);
88 }catch (InterruptedException e) {
89 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
90 }catch (KeeperException e) {
91 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
96 public byte[] getNodeData(final String lockName) {
98 if (zookeeper.exists("/" + lockName, null) != null)
99 return zookeeper.getData("/" + lockName, false, null);
103 }catch (InterruptedException e) {
104 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
105 }catch (KeeperException e) {
106 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
111 public boolean checkIfLockExists(String lockName) {
112 boolean result = false;
114 Stat stat = zookeeper.exists(lockName, false);
118 }catch (InterruptedException e) {
119 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
120 }catch (KeeperException e) {
121 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
126 public void createNode(String nodeName) {
127 ensurePathExists(nodeName);
130 public String createLockId(String dir) {
131 ensurePathExists(dir);
132 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir);
135 }catch (InterruptedException e) {
136 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
137 }catch (KeeperException e) {
138 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
144 * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note
145 * that the exclusive lock may be acquired some time later after this method has been invoked
146 * due to the current lock owner going away.
148 public synchronized boolean lock(String dir, String lockId)
149 throws KeeperException, InterruptedException {
153 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId);
154 return (Boolean) retryOperation(zop);
158 * Removes the lock or associated znode if you no longer require the lock. this also removes
159 * your request in the queue for locking in case you do not already hold the lock.
161 * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
162 * @throws NoNodeException
164 public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException {
165 final String id = lockId;
166 if (!isClosed() && id != null) {
168 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
169 public boolean execute() throws KeeperException, InterruptedException {
170 zookeeper.delete(id, -1);
175 } catch (InterruptedException e) {
176 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
177 // set that we have been interrupted.
178 Thread.currentThread().interrupt();
179 } catch (KeeperException.NoNodeException e) {
181 throw new KeeperException.NoNodeException("Lock doesn't exists. Release lock operation failed.");
182 } catch (KeeperException e) {
183 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
184 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
189 public synchronized String currentLockHolder(String mainLock) {
190 final String id = mainLock;
191 if (!isClosed() && id != null) {
194 names = zookeeper.getChildren(id, false);
197 SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
198 for (String name : names) {
199 sortedNames.add(new ZNodeName(id + "/" + name));
201 return sortedNames.first().getName();
202 } catch (InterruptedException e) {
203 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
204 // set that we have been interrupted.
205 Thread.currentThread().interrupt();
206 } catch (KeeperException.NoNodeException e) {
208 } catch (KeeperException e) {
209 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
210 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
213 return "No lock holder!";
216 public synchronized void deleteLock(String mainLock) {
217 final String id = mainLock;
218 if (!isClosed() && id != null) {
220 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
221 public boolean execute() throws KeeperException, InterruptedException {
222 List<String> names = zookeeper.getChildren(id, false);
223 for (String name : names) {
224 zookeeper.delete(id + "/" + name, -1);
226 zookeeper.delete(id, -1);
231 } catch (InterruptedException e) {
232 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
233 // set that we have been interrupted.
234 Thread.currentThread().interrupt();
235 } catch (KeeperException.NoNodeException e) {
236 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
238 } catch (KeeperException e) {
239 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
240 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
247 * a zoookeeper operation that is mainly responsible for all the magic required for locking.
249 private class LockZooKeeperOperation implements ZooKeeperOperation {
252 * find if we have been created earlier if not create our node
254 * @param prefix the prefix node
255 * @param zookeeper the zookeeper client
256 * @param dir the dir parent
257 * @throws KeeperException
258 * @throws InterruptedException
261 private String id = null;
263 public String getId() {
267 public LockZooKeeperOperation(String dir) {
271 public LockZooKeeperOperation(String dir, String id) {
277 * the command that is run and retried for actually obtaining the lock
279 * @return if the command was successful or not
281 public boolean execute() throws KeeperException, InterruptedException {
284 String prefix = "x-";
285 byte[] data = {0x12, 0x34};
286 id = zookeeper.create(dir + "/" + prefix, data, getAcl(),
287 CreateMode.PERSISTENT_SEQUENTIAL);
289 if (logger.isDebugEnabled()) {
290 logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
296 List<String> names = zookeeper.getChildren(dir, false);
297 if (names.isEmpty()) {
298 logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir
299 + " when we've just " + "created one! Lets recreate it...");
300 // lets force the recreation of the id
302 return Boolean.FALSE;
305 // lets sort them explicitly (though they do seem to come back in order
307 ZNodeName idName = new ZNodeName(id);
308 SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
309 for (String name : names) {
310 sortedNames.add(new ZNodeName(dir + "/" + name));
312 if (!sortedNames.contains(idName))
313 return Boolean.FALSE;
315 SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
316 if (!lessThanMe.isEmpty()) {
317 ZNodeName lastChildName = lessThanMe.last();
318 String lastChildId = lastChildName.getName();
319 if (logger.isDebugEnabled()) {
320 logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
322 Stat stat = zookeeper.exists(lastChildId, false);
324 return Boolean.FALSE;
326 logger.info(EELFLoggerDelegate.applicationLogger,
327 "Could not find the" + " stats for less than me: "
328 + lastChildName.getName());
334 } while (id == null);
335 return Boolean.FALSE;