2 * ============LICENSE_START========================================== org.onap.music
3 * ===================================================================
4 * Copyright (c) 2017 AT&T Intellectual Property
5 * ===================================================================
6 * Modifications Copyright (c) 2018 IBM.
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software distributed under the License
14 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15 * or implied. See the License for the specific language governing permissions and limitations under
18 * ============LICENSE_END=============================================
19 * ====================================================================
21 package org.onap.music.lockingservice;
24 import java.util.List;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27 import org.apache.zookeeper.CreateMode;
28 import org.apache.zookeeper.KeeperException;
29 import org.apache.zookeeper.KeeperException.NoNodeException;
30 import org.apache.zookeeper.ZooDefs;
31 import org.apache.zookeeper.ZooKeeper;
32 import org.apache.zookeeper.data.ACL;
33 import org.apache.zookeeper.data.Stat;
34 import org.onap.music.datastore.PreparedQueryObject;
35 import org.onap.music.eelf.logging.EELFLoggerDelegate;
36 import org.onap.music.eelf.logging.format.AppMessages;
37 import org.onap.music.eelf.logging.format.ErrorSeverity;
38 import org.onap.music.eelf.logging.format.ErrorTypes;
39 import org.onap.music.main.MusicCore;
40 import org.onap.music.main.MusicUtil;
42 import com.datastax.driver.core.DataType;
45 * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>.
47 * You invoke {@link #lock()} to start the process of grabbing the lock; you may get the lock then
48 * or it may be some time later.
50 * You can register a listener so that you are invoked when you get the lock; otherwise you can ask
51 * if you have the lock by calling {@link #isOwner()}
54 public class ZkStatelessLockService extends ProtocolSupport {
55 public ZkStatelessLockService(ZooKeeper zk) {
59 private static EELFLoggerDelegate logger =
60 EELFLoggerDelegate.getLogger(ZkStatelessLockService.class);
62 protected void createLock(final String path, final byte[] data) {
63 final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
65 retryOperation(new ZooKeeperOperation() {
66 public boolean execute() throws KeeperException, InterruptedException {
67 zookeeper.create(path, data, acl, CreateMode.PERSISTENT);
71 }catch (InterruptedException e) {
72 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
73 }catch (KeeperException e) {
74 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
82 }catch (InterruptedException e) {
83 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
87 public void setNodeData(final String lockName, final byte[] data) {
89 retryOperation(new ZooKeeperOperation() {
90 public boolean execute() throws KeeperException, InterruptedException {
91 zookeeper.getSessionId();
92 zookeeper.setData("/" + lockName, data, -1);
96 }catch (InterruptedException e) {
97 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
98 }catch (KeeperException e) {
99 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
104 public byte[] getNodeData(final String lockName) {
106 if (zookeeper.exists("/" + lockName, null) != null)
107 return zookeeper.getData("/" + lockName, false, null);
111 }catch (InterruptedException e) {
112 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
113 }catch (KeeperException e) {
114 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
119 public boolean checkIfLockExists(String lockName) {
120 boolean result = false;
122 Stat stat = zookeeper.exists(lockName, false);
126 }catch (InterruptedException e) {
127 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
128 }catch (KeeperException e) {
129 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
134 public void createNode(String nodeName) {
135 ensurePathExists(nodeName);
138 public String createLockId(String dir) {
139 ensurePathExists(dir);
140 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir);
143 }catch (InterruptedException e) {
144 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
145 }catch (KeeperException e) {
146 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
152 * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note
153 * that the exclusive lock may be acquired some time later after this method has been invoked
154 * due to the current lock owner going away.
156 public synchronized boolean lock(String dir, String lockId)
157 throws KeeperException, InterruptedException {
161 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId);
162 return (Boolean) retryOperation(zop);
166 * Removes the lock or associated znode if you no longer require the lock. this also removes
167 * your request in the queue for locking in case you do not already hold the lock.
169 * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
170 * @throws NoNodeException
172 public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException {
173 final String id = lockId;
174 if (!isClosed() && id != null) {
176 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
177 public boolean execute() throws KeeperException, InterruptedException {
178 zookeeper.delete(id, -1);
183 } catch (InterruptedException e) {
184 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
185 // set that we have been interrupted.
186 Thread.currentThread().interrupt();
187 } catch (KeeperException.NoNodeException e) {
189 throw new KeeperException.NoNodeException("Lock doesn't exists. Release lock operation failed.");
190 } catch (KeeperException e) {
191 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
192 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
197 public synchronized String currentLockHolder(String mainLock) {
198 final String id = mainLock;
199 if (!isClosed() && id != null) {
202 names = zookeeper.getChildren(id, false);
205 SortedSet<ZNodeName> sortedNames = new TreeSet<>();
206 for (String name : names) {
207 sortedNames.add(new ZNodeName(id + "/" + name));
209 return sortedNames.first().getName();
210 } catch (InterruptedException e) {
211 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
212 // set that we have been interrupted.
213 Thread.currentThread().interrupt();
214 } catch (KeeperException.NoNodeException e) {
216 } catch (KeeperException e) {
217 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
218 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
221 return "No lock holder!";
224 public synchronized void deleteLock(String mainLock) {
225 final String id = mainLock;
226 if (!isClosed() && id != null) {
228 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
229 public boolean execute() throws KeeperException, InterruptedException {
230 List<String> names = zookeeper.getChildren(id, false);
231 for (String name : names) {
232 zookeeper.delete(id + "/" + name, -1);
234 zookeeper.delete(id, -1);
239 } catch (InterruptedException e) {
240 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
241 // set that we have been interrupted.
242 Thread.currentThread().interrupt();
243 } catch (KeeperException.NoNodeException e) {
244 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
246 } catch (KeeperException e) {
247 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
248 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
255 * a zoookeeper operation that is mainly responsible for all the magic required for locking.
257 private class LockZooKeeperOperation implements ZooKeeperOperation {
260 * find if we have been created earlier if not create our node
262 * @param prefix the prefix node
263 * @param zookeeper the zookeeper client
264 * @param dir the dir parent
265 * @throws KeeperException
266 * @throws InterruptedException
269 private String id = null;
271 public String getId() {
275 public LockZooKeeperOperation(String dir) {
279 public LockZooKeeperOperation(String dir, String id) {
285 * the command that is run and retried for actually obtaining the lock
287 * @return if the command was successful or not
289 public boolean execute() throws KeeperException, InterruptedException {
292 String prefix = "x-";
293 byte[] data = {0x12, 0x34};
294 id = zookeeper.create(dir + "/" + prefix, data, getAcl(),
295 CreateMode.PERSISTENT_SEQUENTIAL);
297 if (logger.isDebugEnabled()) {
298 logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
303 stat = zookeeper.exists(id, false);
304 } catch (KeeperException | InterruptedException e1) {
305 logger.error(EELFLoggerDelegate.errorLogger, "Error in execute: " + e1);
307 Long ctime = stat.getCtime();
308 MusicUtil.zkNodeMap.put(id, ctime);
309 PreparedQueryObject pQuery = new PreparedQueryObject();
310 pQuery.appendQueryString(
311 "INSERT INTO admin.locks(lock_id, ctime) VALUES (?,?)");
313 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), id));
314 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), ctime));
315 MusicCore.eventualPut(pQuery);
316 } catch (Exception e) {
317 logger.error(EELFLoggerDelegate.errorLogger, "Error in execute: " + e);
323 List<String> names = zookeeper.getChildren(dir, false);
324 if (names.isEmpty()) {
325 logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir
326 + " when we've just " + "created one! Lets recreate it...");
327 // lets force the recreation of the id
329 return Boolean.FALSE;
332 // lets sort them explicitly (though they do seem to come back in order
334 ZNodeName idName = new ZNodeName(id);
335 SortedSet<ZNodeName> sortedNames = new TreeSet<>();
336 for (String name : names) {
337 sortedNames.add(new ZNodeName(dir + "/" + name));
339 if (!sortedNames.contains(idName))
340 return Boolean.FALSE;
342 SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
343 if (!lessThanMe.isEmpty()) {
344 ZNodeName lastChildName = lessThanMe.last();
345 String lastChildId = lastChildName.getName();
346 if (logger.isDebugEnabled()) {
347 logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
349 Stat stat = zookeeper.exists(lastChildId, false);
351 return Boolean.FALSE;
353 logger.info(EELFLoggerDelegate.applicationLogger,
354 "Could not find the" + " stats for less than me: "
355 + lastChildName.getName());
361 } while (id == null);
362 return Boolean.FALSE;