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.KeeperException.NoNodeException;
27 import org.apache.zookeeper.ZooDefs;
28 import org.apache.zookeeper.ZooKeeper;
29 import org.apache.zookeeper.data.ACL;
30 import org.apache.zookeeper.data.Stat;
31 import org.onap.music.eelf.logging.EELFLoggerDelegate;
32 import org.onap.music.eelf.logging.format.AppMessages;
33 import org.onap.music.eelf.logging.format.ErrorSeverity;
34 import org.onap.music.eelf.logging.format.ErrorTypes;
37 * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>.
39 * You invoke {@link #lock()} to start the process of grabbing the lock; you may get the lock then
40 * or it may be some time later.
42 * You can register a listener so that you are invoked when you get the lock; otherwise you can ask
43 * if you have the lock by calling {@link #isOwner()}
46 public class ZkStatelessLockService extends ProtocolSupport {
47 public ZkStatelessLockService(ZooKeeper zk) {
51 private static EELFLoggerDelegate logger =
52 EELFLoggerDelegate.getLogger(ZkStatelessLockService.class);
54 protected void createLock(final String path, final byte[] data) {
55 final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
57 retryOperation(new ZooKeeperOperation() {
58 public boolean execute() throws KeeperException, InterruptedException {
59 zookeeper.create(path, data, acl, CreateMode.PERSISTENT);
63 }catch (InterruptedException e) {
64 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
65 }catch (KeeperException e) {
66 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
73 }catch (InterruptedException e) {
74 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
78 public void setNodeData(final String lockName, final byte[] data) {
80 retryOperation(new ZooKeeperOperation() {
81 public boolean execute() throws KeeperException, InterruptedException {
82 zookeeper.getSessionId();
83 zookeeper.setData("/" + lockName, data, -1);
87 }catch (InterruptedException e) {
88 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
89 }catch (KeeperException e) {
90 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
95 public byte[] getNodeData(final String lockName) {
97 if (zookeeper.exists("/" + lockName, null) != null)
98 return zookeeper.getData("/" + lockName, false, null);
102 }catch (InterruptedException e) {
103 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
104 }catch (KeeperException e) {
105 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
110 public boolean checkIfLockExists(String lockName) {
111 boolean result = false;
113 Stat stat = zookeeper.exists(lockName, false);
117 }catch (InterruptedException e) {
118 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
119 }catch (KeeperException e) {
120 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
125 public void createNode(String nodeName) {
126 ensurePathExists(nodeName);
129 public String createLockId(String dir) {
130 ensurePathExists(dir);
131 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir);
134 }catch (InterruptedException e) {
135 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
136 }catch (KeeperException e) {
137 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
143 * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note
144 * that the exclusive lock may be acquired some time later after this method has been invoked
145 * due to the current lock owner going away.
147 public synchronized boolean lock(String dir, String lockId)
148 throws KeeperException, InterruptedException {
152 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId);
153 return (Boolean) retryOperation(zop);
157 * Removes the lock or associated znode if you no longer require the lock. this also removes
158 * your request in the queue for locking in case you do not already hold the lock.
160 * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
161 * @throws NoNodeException
163 public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException {
164 final String id = lockId;
165 if (!isClosed() && id != null) {
167 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
168 public boolean execute() throws KeeperException, InterruptedException {
169 zookeeper.delete(id, -1);
174 } catch (InterruptedException e) {
175 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
176 // set that we have been interrupted.
177 Thread.currentThread().interrupt();
178 } catch (KeeperException.NoNodeException e) {
180 throw new KeeperException.NoNodeException("Lock doesn't exists. Release lock operation failed.");
181 } catch (KeeperException e) {
182 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
183 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
188 public synchronized String currentLockHolder(String mainLock) {
189 final String id = mainLock;
190 if (!isClosed() && id != null) {
193 names = zookeeper.getChildren(id, false);
196 SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
197 for (String name : names) {
198 sortedNames.add(new ZNodeName(id + "/" + name));
200 return sortedNames.first().getName();
201 } catch (InterruptedException e) {
202 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
203 // set that we have been interrupted.
204 Thread.currentThread().interrupt();
205 } catch (KeeperException.NoNodeException e) {
207 } catch (KeeperException e) {
208 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
209 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
212 return "No lock holder!";
215 public synchronized void deleteLock(String mainLock) {
216 final String id = mainLock;
217 if (!isClosed() && id != null) {
219 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
220 public boolean execute() throws KeeperException, InterruptedException {
221 List<String> names = zookeeper.getChildren(id, false);
222 for (String name : names) {
223 zookeeper.delete(id + "/" + name, -1);
225 zookeeper.delete(id, -1);
230 } catch (InterruptedException e) {
231 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
232 // set that we have been interrupted.
233 Thread.currentThread().interrupt();
234 } catch (KeeperException.NoNodeException e) {
235 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
237 } catch (KeeperException e) {
238 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
239 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
246 * a zoookeeper operation that is mainly responsible for all the magic required for locking.
248 private class LockZooKeeperOperation implements ZooKeeperOperation {
251 * find if we have been created earlier if not create our node
253 * @param prefix the prefix node
254 * @param zookeeper the zookeeper client
255 * @param dir the dir parent
256 * @throws KeeperException
257 * @throws InterruptedException
260 private String id = null;
262 public String getId() {
266 public LockZooKeeperOperation(String dir) {
270 public LockZooKeeperOperation(String dir, String id) {
276 * the command that is run and retried for actually obtaining the lock
278 * @return if the command was successful or not
280 public boolean execute() throws KeeperException, InterruptedException {
283 String prefix = "x-";
284 byte[] data = {0x12, 0x34};
285 id = zookeeper.create(dir + "/" + prefix, data, getAcl(),
286 CreateMode.PERSISTENT_SEQUENTIAL);
288 if (logger.isDebugEnabled()) {
289 logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
295 List<String> names = zookeeper.getChildren(dir, false);
296 if (names.isEmpty()) {
297 logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir
298 + " when we've just " + "created one! Lets recreate it...");
299 // lets force the recreation of the id
301 return Boolean.FALSE;
304 // lets sort them explicitly (though they do seem to come back in order
306 ZNodeName idName = new ZNodeName(id);
307 SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
308 for (String name : names) {
309 sortedNames.add(new ZNodeName(dir + "/" + name));
311 if (!sortedNames.contains(idName))
312 return Boolean.FALSE;
314 SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
315 if (!lessThanMe.isEmpty()) {
316 ZNodeName lastChildName = lessThanMe.last();
317 String lastChildId = lastChildName.getName();
318 if (logger.isDebugEnabled()) {
319 logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
321 Stat stat = zookeeper.exists(lastChildId, false);
323 return Boolean.FALSE;
325 logger.info(EELFLoggerDelegate.applicationLogger,
326 "Could not find the" + " stats for less than me: "
327 + lastChildName.getName());
333 } while (id == null);
334 return Boolean.FALSE;