2 * ============LICENSE_START========================================== org.onap.music
3 * ===================================================================
4 * Copyright (c) 2017 AT&T Intellectual Property
5 * ===================================================================
6 * Modifications Copyright (c) 2018 IBM.
7 * Modifications Copyright (c) 2019 Samsung.
8 * ===================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
10 * in compliance with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software distributed under the License
15 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
16 * or implied. See the License for the specific language governing permissions and limitations under
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.lockingservice.zookeeper;
26 import java.util.List;
27 import java.util.SortedSet;
28 import java.util.TreeSet;
29 import org.apache.zookeeper.CreateMode;
30 import org.apache.zookeeper.KeeperException;
31 import org.apache.zookeeper.KeeperException.NoNodeException;
32 import org.apache.zookeeper.ZooDefs;
33 import org.apache.zookeeper.ZooKeeper;
34 import org.apache.zookeeper.data.ACL;
35 import org.apache.zookeeper.data.Stat;
36 import org.onap.music.datastore.PreparedQueryObject;
37 import org.onap.music.eelf.logging.EELFLoggerDelegate;
38 import org.onap.music.eelf.logging.format.AppMessages;
39 import org.onap.music.eelf.logging.format.ErrorSeverity;
40 import org.onap.music.eelf.logging.format.ErrorTypes;
41 import org.onap.music.main.MusicCore;
42 import org.onap.music.main.MusicUtil;
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 Thread.currentThread().interrupt();
131 }catch (KeeperException e) {
132 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
137 public void createNode(String nodeName) {
138 ensurePathExists(nodeName);
141 public String createLockId(String dir) {
142 ensurePathExists(dir);
143 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir);
146 }catch (InterruptedException e) {
147 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
148 Thread.currentThread().interrupt();
149 }catch (KeeperException e) {
150 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
156 * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note
157 * that the exclusive lock may be acquired some time later after this method has been invoked
158 * due to the current lock owner going away.
160 public synchronized boolean lock(String dir, String lockId)
161 throws KeeperException, InterruptedException {
165 LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId);
166 return (Boolean) retryOperation(zop);
170 * Removes the lock or associated znode if you no longer require the lock. this also removes
171 * your request in the queue for locking in case you do not already hold the lock.
173 * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
174 * @throws NoNodeException
176 public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException {
177 final String id = lockId;
178 if (!isClosed() && id != null) {
180 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
181 public boolean execute() throws KeeperException, InterruptedException {
182 zookeeper.delete(id, -1);
187 } catch (InterruptedException e) {
188 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
189 // set that we have been interrupted.
190 Thread.currentThread().interrupt();
191 } catch (KeeperException.NoNodeException e) {
193 throw new KeeperException.NoNodeException("Lock doesn't exists. Release lock operation failed.");
194 } catch (KeeperException e) {
195 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
196 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
201 public synchronized String currentLockHolder(String mainLock) {
202 final String id = mainLock;
203 if (!isClosed() && id != null) {
206 names = zookeeper.getChildren(id, false);
209 SortedSet<ZNodeName> sortedNames = new TreeSet<>();
210 for (String name : names) {
211 sortedNames.add(new ZNodeName(id + "/" + name));
213 return sortedNames.first().getName();
214 } catch (InterruptedException e) {
215 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
216 // set that we have been interrupted.
217 Thread.currentThread().interrupt();
218 } catch (KeeperException.NoNodeException e) {
220 } catch (KeeperException e) {
221 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
222 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
225 return "No lock holder!";
228 public synchronized void deleteLock(String mainLock) {
229 final String id = mainLock;
230 if (!isClosed() && id != null) {
232 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
233 public boolean execute() throws KeeperException, InterruptedException {
234 List<String> names = zookeeper.getChildren(id, false);
235 for (String name : names) {
236 zookeeper.delete(id + "/" + name, -1);
238 zookeeper.delete(id, -1);
243 } catch (InterruptedException e) {
244 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
245 // set that we have been interrupted.
246 Thread.currentThread().interrupt();
247 } catch (KeeperException.NoNodeException e) {
248 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
250 } catch (KeeperException e) {
251 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
252 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
259 * a zoookeeper operation that is mainly responsible for all the magic required for locking.
261 private class LockZooKeeperOperation implements ZooKeeperOperation {
264 * find if we have been created earlier if not create our node
266 * @param prefix the prefix node
267 * @param zookeeper the zookeeper client
268 * @param dir the dir parent
269 * @throws KeeperException
270 * @throws InterruptedException
273 private String id = null;
275 public String getId() {
279 public LockZooKeeperOperation(String dir) {
283 public LockZooKeeperOperation(String dir, String id) {
289 * the command that is run and retried for actually obtaining the lock
291 * @return if the command was successful or not
293 public boolean execute() throws KeeperException, InterruptedException {
296 String prefix = "x-";
297 byte[] data = {0x12, 0x34};
298 id = zookeeper.create(dir + "/" + prefix, data, getAcl(),
299 CreateMode.PERSISTENT_SEQUENTIAL);
301 if (logger.isDebugEnabled()) {
302 logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
307 stat = zookeeper.exists(id, false);
308 } catch (InterruptedException e) {
309 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
310 Thread.currentThread().interrupt();
311 } catch (KeeperException e) {
312 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
316 Long ctime = stat.getCtime();
317 MusicUtil.zkNodeMap.put(id, ctime);
318 PreparedQueryObject pQuery = new PreparedQueryObject();
319 pQuery.appendQueryString(
320 "INSERT INTO admin.locks(lock_id, ctime) VALUES (?,?)");
322 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), id));
323 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), ctime));
324 MusicCore.eventualPut(pQuery);
325 } catch (Exception e) {
326 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
333 List<String> names = zookeeper.getChildren(dir, false);
334 if (names.isEmpty()) {
335 logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir
336 + " when we've just " + "created one! Lets recreate it...");
337 // lets force the recreation of the id
339 return Boolean.FALSE;
342 // lets sort them explicitly (though they do seem to come back in order
344 ZNodeName idName = new ZNodeName(id);
345 SortedSet<ZNodeName> sortedNames = new TreeSet<>();
346 for (String name : names) {
347 sortedNames.add(new ZNodeName(dir + "/" + name));
349 if (!sortedNames.contains(idName))
350 return Boolean.FALSE;
352 SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
353 if (!lessThanMe.isEmpty()) {
354 ZNodeName lastChildName = lessThanMe.last();
355 String lastChildId = lastChildName.getName();
356 if (logger.isDebugEnabled()) {
357 logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
359 Stat stat = zookeeper.exists(lastChildId, false);
361 return Boolean.FALSE;
363 logger.info(EELFLoggerDelegate.applicationLogger,
364 "Could not find the" + " stats for less than me: "
365 + lastChildName.getName());
371 } while (id == null);
372 return Boolean.FALSE;