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 * ====================================================================
18 package org.onap.music.lockingservice;
21 import java.util.List;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24 import org.apache.zookeeper.CreateMode;
25 import org.apache.zookeeper.KeeperException;
26 import org.apache.zookeeper.ZooDefs;
27 import org.apache.zookeeper.ZooKeeper;
28 import org.apache.zookeeper.data.ACL;
29 import org.apache.zookeeper.data.Stat;
30 import org.onap.music.eelf.logging.EELFLoggerDelegate;
31 import org.onap.music.eelf.logging.format.AppMessages;
32 import org.onap.music.eelf.logging.format.ErrorSeverity;
33 import org.onap.music.eelf.logging.format.ErrorTypes;
36 * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>.
38 * You invoke {@link #lock()} to start the process of grabbing the lock; you may get the lock then
39 * or it may be some time later.
41 * You can register a listener so that you are invoked when you get the lock; otherwise you can ask
42 * if you have the lock by calling {@link #isOwner()}
45 public class ZkStatelessLockService extends ProtocolSupport {
46 public ZkStatelessLockService(ZooKeeper zk) {
50 private static EELFLoggerDelegate logger =
51 EELFLoggerDelegate.getLogger(ZkStatelessLockService.class);
53 protected void createLock(final String path, final byte[] data) {
54 final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
56 retryOperation(new ZooKeeperOperation() {
57 public boolean execute() throws KeeperException, InterruptedException {
58 zookeeper.create(path, data, acl, CreateMode.PERSISTENT);
62 }catch (InterruptedException e) {
63 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
64 }catch (KeeperException e) {
65 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
72 }catch (InterruptedException e) {
73 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
77 public void setNodeData(final String lockName, final byte[] data) {
79 retryOperation(new ZooKeeperOperation() {
80 public boolean execute() throws KeeperException, InterruptedException {
81 zookeeper.getSessionId();
82 zookeeper.setData("/" + lockName, data, -1);
86 }catch (InterruptedException e) {
87 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
88 }catch (KeeperException e) {
89 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
94 public byte[] getNodeData(final String lockName) {
96 if (zookeeper.exists("/" + lockName, null) != null)
97 return zookeeper.getData("/" + lockName, false, null);
101 }catch (InterruptedException e) {
102 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
103 }catch (KeeperException e) {
104 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
109 public boolean checkIfLockExists(String lockName) {
110 boolean result = false;
112 Stat stat = zookeeper.exists(lockName, false);
116 }catch (InterruptedException e) {
117 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
118 }catch (KeeperException e) {
119 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
124 public void createNode(String nodeName) {
125 ensurePathExists(nodeName);
128 public String createLockId(String dir) {
129 ensurePathExists(dir);
130 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir);
133 }catch (InterruptedException e) {
134 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
135 }catch (KeeperException e) {
136 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
142 * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note
143 * that the exclusive lock may be acquired some time later after this method has been invoked
144 * due to the current lock owner going away.
146 public synchronized boolean lock(String dir, String lockId)
147 throws KeeperException, InterruptedException {
151 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId);
152 return (Boolean) retryOperation(zop);
156 * Removes the lock or associated znode if you no longer require the lock. this also removes
157 * your request in the queue for locking in case you do not already hold the lock.
159 * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
161 public synchronized void unlock(String lockId) throws RuntimeException {
162 final String id = lockId;
163 if (!isClosed() && id != null) {
165 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
166 public boolean execute() throws KeeperException, InterruptedException {
167 zookeeper.delete(id, -1);
172 } catch (InterruptedException e) {
173 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
174 // set that we have been interrupted.
175 Thread.currentThread().interrupt();
176 } catch (KeeperException.NoNodeException e) {
178 } catch (KeeperException e) {
179 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
180 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
185 public synchronized String currentLockHolder(String mainLock) {
186 final String id = mainLock;
187 if (!isClosed() && id != null) {
190 names = zookeeper.getChildren(id, false);
193 SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
194 for (String name : names) {
195 sortedNames.add(new ZNodeName(id + "/" + name));
197 return sortedNames.first().getName();
198 } catch (InterruptedException e) {
199 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
200 // set that we have been interrupted.
201 Thread.currentThread().interrupt();
202 } catch (KeeperException.NoNodeException e) {
204 } catch (KeeperException e) {
205 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
206 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
209 return "No lock holder!";
212 public synchronized void deleteLock(String mainLock) {
213 final String id = mainLock;
214 if (!isClosed() && id != null) {
216 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
217 public boolean execute() throws KeeperException, InterruptedException {
218 List<String> names = zookeeper.getChildren(id, false);
219 for (String name : names) {
220 zookeeper.delete(id + "/" + name, -1);
222 zookeeper.delete(id, -1);
227 } catch (InterruptedException e) {
228 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
229 // set that we have been interrupted.
230 Thread.currentThread().interrupt();
231 } catch (KeeperException.NoNodeException e) {
232 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
234 } catch (KeeperException e) {
235 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
236 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
243 * a zoookeeper operation that is mainly responsible for all the magic required for locking.
245 private class LockZooKeeperOperation implements ZooKeeperOperation {
248 * find if we have been created earlier if not create our node
250 * @param prefix the prefix node
251 * @param zookeeper the zookeeper client
252 * @param dir the dir parent
253 * @throws KeeperException
254 * @throws InterruptedException
257 private String id = null;
259 public String getId() {
263 public LockZooKeeperOperation(String dir) {
267 public LockZooKeeperOperation(String dir, String id) {
273 * the command that is run and retried for actually obtaining the lock
275 * @return if the command was successful or not
277 public boolean execute() throws KeeperException, InterruptedException {
280 String prefix = "x-";
281 byte[] data = {0x12, 0x34};
282 id = zookeeper.create(dir + "/" + prefix, data, getAcl(),
283 CreateMode.PERSISTENT_SEQUENTIAL);
285 if (logger.isDebugEnabled()) {
286 logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
292 List<String> names = zookeeper.getChildren(dir, false);
293 if (names.isEmpty()) {
294 logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir
295 + " when we've just " + "created one! Lets recreate it...");
296 // lets force the recreation of the id
298 return Boolean.FALSE;
301 // lets sort them explicitly (though they do seem to come back in order
303 ZNodeName idName = new ZNodeName(id);
304 SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
305 for (String name : names) {
306 sortedNames.add(new ZNodeName(dir + "/" + name));
308 if (!sortedNames.contains(idName))
309 return Boolean.FALSE;
311 SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
312 if (!lessThanMe.isEmpty()) {
313 ZNodeName lastChildName = lessThanMe.last();
314 String lastChildId = lastChildName.getName();
315 if (logger.isDebugEnabled()) {
316 logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
318 Stat stat = zookeeper.exists(lastChildId, false);
320 return Boolean.FALSE;
322 logger.info(EELFLoggerDelegate.applicationLogger,
323 "Could not find the" + " stats for less than me: "
324 + lastChildName.getName());
330 } while (id == null);
331 return Boolean.FALSE;