Update Master to include jar folder.
[music.git] / jar / src / main / java / org / onap / music / lockingservice / ZkStatelessLockService.java
1 /*
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
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
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
13  * the License.
14  * 
15  * ============LICENSE_END=============================================
16  * ====================================================================
17  */
18 package org.onap.music.lockingservice;
19
20
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;
35
36 /**
37  * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>.
38  * <p/>
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.
41  * <p/>
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()}
44  *
45  */
46 public class ZkStatelessLockService extends ProtocolSupport {
47     public ZkStatelessLockService(ZooKeeper zk) {
48         zookeeper = zk;
49     }
50
51     private static EELFLoggerDelegate logger =
52                     EELFLoggerDelegate.getLogger(ZkStatelessLockService.class);
53
54     protected void createLock(final String path, final byte[] data) {
55         final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
56         try {
57             retryOperation(new ZooKeeperOperation() {
58                 public boolean execute() throws KeeperException, InterruptedException {
59                     zookeeper.create(path, data, acl, CreateMode.PERSISTENT);
60                     return true;
61                 }
62             });
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);
67         }
68     }
69
70     public void close() {
71         try {
72             zookeeper.close();
73         }catch (InterruptedException e) {
74                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
75         }
76     }
77
78     public void setNodeData(final String lockName, final byte[] data) {
79         try {
80             retryOperation(new ZooKeeperOperation() {
81                 public boolean execute() throws KeeperException, InterruptedException {
82                     zookeeper.getSessionId();
83                     zookeeper.setData("/" + lockName, data, -1);
84                     return true;
85                 }
86             });
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);
91         }
92
93     }
94
95     public byte[] getNodeData(final String lockName) {
96         try {
97             if (zookeeper.exists("/" + lockName, null) != null)
98                 return zookeeper.getData("/" + lockName, false, null);
99             else
100                 return null;
101
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);
106         }
107         return null;
108     }
109
110     public boolean checkIfLockExists(String lockName) {
111         boolean result = false;
112         try {
113             Stat stat = zookeeper.exists(lockName, false);
114             if (stat != null) {
115                 result = true;
116             }
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);
121         }
122         return result;
123     }
124
125     public void createNode(String nodeName) {
126         ensurePathExists(nodeName);
127     }
128
129     public String createLockId(String dir) {
130         ensurePathExists(dir);
131         LockZooKeeperOperation zop = new LockZooKeeperOperation(dir);
132         try {
133             retryOperation(zop);
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);
138         }
139         return zop.getId();
140     }
141
142     /**
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.
146      */
147     public synchronized boolean lock(String dir, String lockId)
148                     throws KeeperException, InterruptedException {
149         if (isClosed()) {
150             return false;
151         }
152         LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId);
153         return (Boolean) retryOperation(zop);
154     }
155
156     /**
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.
159      * 
160      * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
161      * @throws NoNodeException 
162      */
163     public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException {
164         final String id = lockId;
165         if (!isClosed() && id != null) {
166             try {
167                 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
168                     public boolean execute() throws KeeperException, InterruptedException {
169                         zookeeper.delete(id, -1);
170                         return Boolean.TRUE;
171                     }
172                 };
173                 zopdel.execute();
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) {
179                 // do nothing
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);
184             }
185         }
186     }
187
188     public synchronized String currentLockHolder(String mainLock) {
189         final String id = mainLock;
190         if (!isClosed() && id != null) {
191             List<String> names;
192             try {
193                 names = zookeeper.getChildren(id, false);
194                 if (names.isEmpty())
195                     return "";
196                 SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
197                 for (String name : names) {
198                     sortedNames.add(new ZNodeName(id + "/" + name));
199                 }
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) {
206                 // do nothing
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);
210             }
211         }
212         return "No lock holder!";
213     }
214
215     public synchronized void deleteLock(String mainLock) {
216         final String id = mainLock;
217         if (!isClosed() && id != null) {
218             try {
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);
224                         }
225                         zookeeper.delete(id, -1);
226                         return Boolean.TRUE;
227                     }
228                 };
229                 zopdel.execute();
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);
236                 // do nothing
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);
240             }
241         }
242
243     }
244
245     /**
246      * a zoookeeper operation that is mainly responsible for all the magic required for locking.
247      */
248     private class LockZooKeeperOperation implements ZooKeeperOperation {
249
250         /**
251          * find if we have been created earlier if not create our node
252          * 
253          * @param prefix the prefix node
254          * @param zookeeper the zookeeper client
255          * @param dir the dir parent
256          * @throws KeeperException
257          * @throws InterruptedException
258          */
259         private String dir;
260         private String id = null;
261
262         public String getId() {
263             return id;
264         }
265
266         public LockZooKeeperOperation(String dir) {
267             this.dir = dir;
268         }
269
270         public LockZooKeeperOperation(String dir, String id) {
271             this.dir = dir;
272             this.id = id;
273         }
274
275         /**
276          * the command that is run and retried for actually obtaining the lock
277          * 
278          * @return if the command was successful or not
279          */
280         public boolean execute() throws KeeperException, InterruptedException {
281             do {
282                 if (id == null) {
283                     String prefix = "x-";
284                     byte[] data = {0x12, 0x34};
285                     id = zookeeper.create(dir + "/" + prefix, data, getAcl(),
286                                     CreateMode.PERSISTENT_SEQUENTIAL);
287
288                     if (logger.isDebugEnabled()) {
289                         logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
290                     }
291                     if (id != null)
292                         break;
293                 }
294                 if (id != null) {
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
300                         id = null;
301                         return Boolean.FALSE;
302
303                     } else {
304                         // lets sort them explicitly (though they do seem to come back in order
305                         // ususally :)
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));
310                         }
311                         if (!sortedNames.contains(idName))
312                             return Boolean.FALSE;
313
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);
320                             }
321                             Stat stat = zookeeper.exists(lastChildId, false);
322                             if (stat != null) {
323                                 return Boolean.FALSE;
324                             } else {
325                                 logger.info(EELFLoggerDelegate.applicationLogger,
326                                                 "Could not find the" + " stats for less than me: "
327                                                                 + lastChildName.getName());
328                             }
329                         } else
330                             return Boolean.TRUE;
331                     }
332                 }
333             } while (id == null);
334             return Boolean.FALSE;
335         }
336     }
337
338 }
339