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 * ====================================================================
22 package org.onap.music.lockingservice.zookeeper;
25 import java.util.List;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
28 import org.apache.zookeeper.CreateMode;
29 import org.apache.zookeeper.KeeperException;
30 import org.apache.zookeeper.KeeperException.NoNodeException;
31 import org.apache.zookeeper.ZooDefs;
32 import org.apache.zookeeper.ZooKeeper;
33 import org.apache.zookeeper.data.ACL;
34 import org.apache.zookeeper.data.Stat;
35 import org.onap.music.datastore.PreparedQueryObject;
36 import org.onap.music.eelf.logging.EELFLoggerDelegate;
37 import org.onap.music.eelf.logging.format.AppMessages;
38 import org.onap.music.eelf.logging.format.ErrorSeverity;
39 import org.onap.music.eelf.logging.format.ErrorTypes;
40 import org.onap.music.main.MusicCore;
41 import org.onap.music.main.MusicUtil;
42 import org.onap.music.service.impl.MusicZKCore;
44 import com.datastax.driver.core.DataType;
47 * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>.
49 * You invoke {@link #lock()} to start the process of grabbing the lock; you may get the lock then
50 * or it may be some time later.
52 * You can register a listener so that you are invoked when you get the lock; otherwise you can ask
53 * if you have the lock by calling {@link #isOwner()}
56 public class ZkStatelessLockService extends ProtocolSupport {
57 public ZkStatelessLockService(ZooKeeper zk) {
61 private static EELFLoggerDelegate logger =
62 EELFLoggerDelegate.getLogger(ZkStatelessLockService.class);
64 protected void createLock(final String path, final byte[] data) {
65 final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
67 retryOperation(new ZooKeeperOperation() {
68 public boolean execute() throws KeeperException, InterruptedException {
69 zookeeper.create(path, data, acl, CreateMode.PERSISTENT);
73 }catch (InterruptedException e) {
74 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
75 }catch (KeeperException e) {
76 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
84 }catch (InterruptedException e) {
85 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
89 public void setNodeData(final String lockName, final byte[] data) {
91 retryOperation(new ZooKeeperOperation() {
92 public boolean execute() throws KeeperException, InterruptedException {
93 zookeeper.getSessionId();
94 zookeeper.setData("/" + lockName, data, -1);
98 }catch (InterruptedException e) {
99 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
100 }catch (KeeperException e) {
101 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
106 public byte[] getNodeData(final String lockName) {
108 if (zookeeper.exists("/" + lockName, null) != null)
109 return zookeeper.getData("/" + lockName, false, null);
113 }catch (InterruptedException e) {
114 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
115 }catch (KeeperException e) {
116 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
121 public boolean checkIfLockExists(String lockName) {
122 boolean result = false;
124 Stat stat = zookeeper.exists(lockName, false);
128 }catch (InterruptedException e) {
129 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
130 }catch (KeeperException e) {
131 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
136 public void createNode(String nodeName) {
137 ensurePathExists(nodeName);
140 public String createLockId(String dir) {
141 ensurePathExists(dir);
142 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir);
145 }catch (InterruptedException e) {
146 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
147 }catch (KeeperException e) {
148 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
154 * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note
155 * that the exclusive lock may be acquired some time later after this method has been invoked
156 * due to the current lock owner going away.
158 public synchronized boolean lock(String dir, String lockId)
159 throws KeeperException, InterruptedException {
163 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId);
164 return (Boolean) retryOperation(zop);
168 * Removes the lock or associated znode if you no longer require the lock. this also removes
169 * your request in the queue for locking in case you do not already hold the lock.
171 * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
172 * @throws NoNodeException
174 public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException {
175 final String id = lockId;
176 if (!isClosed() && id != null) {
178 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
179 public boolean execute() throws KeeperException, InterruptedException {
180 zookeeper.delete(id, -1);
185 } catch (InterruptedException e) {
186 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
187 // set that we have been interrupted.
188 Thread.currentThread().interrupt();
189 } catch (KeeperException.NoNodeException e) {
191 throw new KeeperException.NoNodeException("Lock doesn't exists. Release lock operation failed.");
192 } catch (KeeperException e) {
193 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
194 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
199 public synchronized String currentLockHolder(String mainLock) {
200 final String id = mainLock;
201 if (!isClosed() && id != null) {
204 names = zookeeper.getChildren(id, false);
207 SortedSet<ZNodeName> sortedNames = new TreeSet<>();
208 for (String name : names) {
209 sortedNames.add(new ZNodeName(id + "/" + name));
211 return sortedNames.first().getName();
212 } catch (InterruptedException e) {
213 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
214 // set that we have been interrupted.
215 Thread.currentThread().interrupt();
216 } catch (KeeperException.NoNodeException e) {
218 } catch (KeeperException e) {
219 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
220 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
223 return "No lock holder!";
226 public synchronized void deleteLock(String mainLock) {
227 final String id = mainLock;
228 if (!isClosed() && id != null) {
230 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
231 public boolean execute() throws KeeperException, InterruptedException {
232 List<String> names = zookeeper.getChildren(id, false);
233 for (String name : names) {
234 zookeeper.delete(id + "/" + name, -1);
236 zookeeper.delete(id, -1);
241 } catch (InterruptedException e) {
242 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
243 // set that we have been interrupted.
244 Thread.currentThread().interrupt();
245 } catch (KeeperException.NoNodeException e) {
246 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
248 } catch (KeeperException e) {
249 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
250 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
257 * a zoookeeper operation that is mainly responsible for all the magic required for locking.
259 private class LockZooKeeperOperation implements ZooKeeperOperation {
262 * find if we have been created earlier if not create our node
264 * @param prefix the prefix node
265 * @param zookeeper the zookeeper client
266 * @param dir the dir parent
267 * @throws KeeperException
268 * @throws InterruptedException
271 private String id = null;
273 public String getId() {
277 public LockZooKeeperOperation(String dir) {
281 public LockZooKeeperOperation(String dir, String id) {
287 * the command that is run and retried for actually obtaining the lock
289 * @return if the command was successful or not
291 public boolean execute() throws KeeperException, InterruptedException {
294 String prefix = "x-";
295 byte[] data = {0x12, 0x34};
296 id = zookeeper.create(dir + "/" + prefix, data, getAcl(),
297 CreateMode.PERSISTENT_SEQUENTIAL);
299 if (logger.isDebugEnabled()) {
300 logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
305 stat = zookeeper.exists(id, false);
306 } catch (KeeperException | InterruptedException e1) {
307 e1.printStackTrace();
309 Long ctime = stat.getCtime();
310 MusicUtil.zkNodeMap.put(id, ctime);
311 PreparedQueryObject pQuery = new PreparedQueryObject();
312 pQuery.appendQueryString(
313 "INSERT INTO admin.locks(lock_id, ctime) VALUES (?,?)");
315 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), id));
316 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), ctime));
317 MusicCore.eventualPut(pQuery);
318 } catch (Exception e) {
325 List<String> names = zookeeper.getChildren(dir, false);
326 if (names.isEmpty()) {
327 logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir
328 + " when we've just " + "created one! Lets recreate it...");
329 // lets force the recreation of the id
331 return Boolean.FALSE;
334 // lets sort them explicitly (though they do seem to come back in order
336 ZNodeName idName = new ZNodeName(id);
337 SortedSet<ZNodeName> sortedNames = new TreeSet<>();
338 for (String name : names) {
339 sortedNames.add(new ZNodeName(dir + "/" + name));
341 if (!sortedNames.contains(idName))
342 return Boolean.FALSE;
344 SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
345 if (!lessThanMe.isEmpty()) {
346 ZNodeName lastChildName = lessThanMe.last();
347 String lastChildId = lastChildName.getName();
348 if (logger.isDebugEnabled()) {
349 logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
351 Stat stat = zookeeper.exists(lastChildId, false);
353 return Boolean.FALSE;
355 logger.info(EELFLoggerDelegate.applicationLogger,
356 "Could not find the" + " stats for less than me: "
357 + lastChildName.getName());
363 } while (id == null);
364 return Boolean.FALSE;