Push variuos changes
[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
19 package org.onap.music.lockingservice;
20
21
22 import java.util.List;
23 import java.util.SortedSet;
24 import java.util.TreeSet;
25 import org.apache.zookeeper.CreateMode;
26 import org.apache.zookeeper.KeeperException;
27 import org.apache.zookeeper.KeeperException.NoNodeException;
28 import org.apache.zookeeper.ZooDefs;
29 import org.apache.zookeeper.ZooKeeper;
30 import org.apache.zookeeper.data.ACL;
31 import org.apache.zookeeper.data.Stat;
32 import org.onap.music.eelf.logging.EELFLoggerDelegate;
33 import org.onap.music.eelf.logging.format.AppMessages;
34 import org.onap.music.eelf.logging.format.ErrorSeverity;
35 import org.onap.music.eelf.logging.format.ErrorTypes;
36
37 /**
38  * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>.
39  * <p/>
40  * You invoke {@link #lock()} to start the process of grabbing the lock; you may get the lock then
41  * or it may be some time later.
42  * <p/>
43  * You can register a listener so that you are invoked when you get the lock; otherwise you can ask
44  * if you have the lock by calling {@link #isOwner()}
45  *
46  */
47 public class ZkStatelessLockService extends ProtocolSupport {
48     public ZkStatelessLockService(ZooKeeper zk) {
49         zookeeper = zk;
50     }
51
52     private static EELFLoggerDelegate logger =
53                     EELFLoggerDelegate.getLogger(ZkStatelessLockService.class);
54
55     protected void createLock(final String path, final byte[] data) {
56         final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
57         try {
58             retryOperation(new ZooKeeperOperation() {
59                 public boolean execute() throws KeeperException, InterruptedException {
60                     zookeeper.create(path, data, acl, CreateMode.PERSISTENT);
61                     return true;
62                 }
63             });
64         }catch (InterruptedException e) {
65             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
66         }catch (KeeperException e) {
67             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
68         }
69     }
70
71     public void close() {
72         try {
73             zookeeper.close();
74         }catch (InterruptedException e) {
75             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
76         }
77     }
78
79     public void setNodeData(final String lockName, final byte[] data) {
80         try {
81             retryOperation(new ZooKeeperOperation() {
82                 public boolean execute() throws KeeperException, InterruptedException {
83                     zookeeper.getSessionId();
84                     zookeeper.setData("/" + lockName, data, -1);
85                     return true;
86                 }
87             });
88         }catch (InterruptedException e) {
89             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
90         }catch (KeeperException e) {
91             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
92         }
93
94     }
95
96     public byte[] getNodeData(final String lockName) {
97         try {
98             if (zookeeper.exists("/" + lockName, null) != null)
99                 return zookeeper.getData("/" + lockName, false, null);
100             else
101                 return null;
102
103         }catch (InterruptedException e) {
104             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
105         }catch (KeeperException e) {
106             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
107         }
108         return null;
109     }
110
111     public boolean checkIfLockExists(String lockName) {
112         boolean result = false;
113         try {
114             Stat stat = zookeeper.exists(lockName, false);
115             if (stat != null) {
116                 result = true;
117             }
118         }catch (InterruptedException e) {
119             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
120         }catch (KeeperException e) {
121             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
122         }
123         return result;
124     }
125
126     public void createNode(String nodeName) {
127         ensurePathExists(nodeName);
128     }
129
130     public String createLockId(String dir) {
131         ensurePathExists(dir);
132         LockZooKeeperOperation zop = new LockZooKeeperOperation(dir);
133         try {
134             retryOperation(zop);
135         }catch (InterruptedException e) {
136             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
137         }catch (KeeperException e) {
138             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
139         }
140         return zop.getId();
141     }
142
143     /**
144      * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note
145      * that the exclusive lock may be acquired some time later after this method has been invoked
146      * due to the current lock owner going away.
147      */
148     public synchronized boolean lock(String dir, String lockId)
149                     throws KeeperException, InterruptedException {
150         if (isClosed()) {
151             return false;
152         }
153         LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId);
154         return (Boolean) retryOperation(zop);
155     }
156
157     /**
158      * Removes the lock or associated znode if you no longer require the lock. this also removes
159      * your request in the queue for locking in case you do not already hold the lock.
160      * 
161      * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper.
162      * @throws NoNodeException 
163      */
164     public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException {
165         final String id = lockId;
166         if (!isClosed() && id != null) {
167             try {
168                 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
169                     public boolean execute() throws KeeperException, InterruptedException {
170                         zookeeper.delete(id, -1);
171                         return Boolean.TRUE;
172                     }
173                 };
174                 zopdel.execute();
175             } catch (InterruptedException e) {
176                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
177                 // set that we have been interrupted.
178                 Thread.currentThread().interrupt();
179             } catch (KeeperException.NoNodeException e) {
180                 // do nothing
181                 throw new KeeperException.NoNodeException("Lock doesn't exists. Release lock operation failed.");
182             } catch (KeeperException e) {
183                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
184                 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
185             }
186         }
187     }
188
189     public synchronized String currentLockHolder(String mainLock) {
190         final String id = mainLock;
191         if (!isClosed() && id != null) {
192             List<String> names;
193             try {
194                 names = zookeeper.getChildren(id, false);
195                 if (names.isEmpty())
196                     return "";
197                 SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
198                 for (String name : names) {
199                     sortedNames.add(new ZNodeName(id + "/" + name));
200                 }
201                 return sortedNames.first().getName();
202             } catch (InterruptedException e) {
203                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
204                 // set that we have been interrupted.
205                 Thread.currentThread().interrupt();
206             } catch (KeeperException.NoNodeException e) {
207                 // do nothing
208             } catch (KeeperException e) {
209                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
210                 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
211             }
212         }
213         return "No lock holder!";
214     }
215
216     public synchronized void deleteLock(String mainLock) {
217         final String id = mainLock;
218         if (!isClosed() && id != null) {
219             try {
220                 ZooKeeperOperation zopdel = new ZooKeeperOperation() {
221                     public boolean execute() throws KeeperException, InterruptedException {
222                         List<String> names = zookeeper.getChildren(id, false);
223                         for (String name : names) {
224                             zookeeper.delete(id + "/" + name, -1);
225                         }
226                         zookeeper.delete(id, -1);
227                         return Boolean.TRUE;
228                     }
229                 };
230                 zopdel.execute();
231             } catch (InterruptedException e) {
232                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
233                 // set that we have been interrupted.
234                 Thread.currentThread().interrupt();
235             } catch (KeeperException.NoNodeException e) {
236                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
237                 // do nothing
238             } catch (KeeperException e) {
239                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR);
240                 throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e);
241             }
242         }
243
244     }
245
246     /**
247      * a zoookeeper operation that is mainly responsible for all the magic required for locking.
248      */
249     private class LockZooKeeperOperation implements ZooKeeperOperation {
250
251         /**
252          * find if we have been created earlier if not create our node
253          * 
254          * @param prefix the prefix node
255          * @param zookeeper the zookeeper client
256          * @param dir the dir parent
257          * @throws KeeperException
258          * @throws InterruptedException
259          */
260         private String dir;
261         private String id = null;
262
263         public String getId() {
264             return id;
265         }
266
267         public LockZooKeeperOperation(String dir) {
268             this.dir = dir;
269         }
270
271         public LockZooKeeperOperation(String dir, String id) {
272             this.dir = dir;
273             this.id = id;
274         }
275
276         /**
277          * the command that is run and retried for actually obtaining the lock
278          * 
279          * @return if the command was successful or not
280          */
281         public boolean execute() throws KeeperException, InterruptedException {
282             do {
283                 if (id == null) {
284                     String prefix = "x-";
285                     byte[] data = {0x12, 0x34};
286                     id = zookeeper.create(dir + "/" + prefix, data, getAcl(),
287                                     CreateMode.PERSISTENT_SEQUENTIAL);
288
289                     if (logger.isDebugEnabled()) {
290                         logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id);
291                     }
292                     if (id != null)
293                         break;
294                 }
295                 if (id != null) {
296                     List<String> names = zookeeper.getChildren(dir, false);
297                     if (names.isEmpty()) {
298                         logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir
299                                         + " when we've just " + "created one! Lets recreate it...");
300                         // lets force the recreation of the id
301                         id = null;
302                         return Boolean.FALSE;
303
304                     } else {
305                         // lets sort them explicitly (though they do seem to come back in order
306                         // ususally :)
307                         ZNodeName idName = new ZNodeName(id);
308                         SortedSet<ZNodeName> sortedNames = new TreeSet<ZNodeName>();
309                         for (String name : names) {
310                             sortedNames.add(new ZNodeName(dir + "/" + name));
311                         }
312                         if (!sortedNames.contains(idName))
313                             return Boolean.FALSE;
314
315                         SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName);
316                         if (!lessThanMe.isEmpty()) {
317                             ZNodeName lastChildName = lessThanMe.last();
318                             String lastChildId = lastChildName.getName();
319                             if (logger.isDebugEnabled()) {
320                                 logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId);
321                             }
322                             Stat stat = zookeeper.exists(lastChildId, false);
323                             if (stat != null) {
324                                 return Boolean.FALSE;
325                             } else {
326                                 logger.info(EELFLoggerDelegate.applicationLogger,
327                                                 "Could not find the" + " stats for less than me: "
328                                                                 + lastChildName.getName());
329                             }
330                         } else
331                             return Boolean.TRUE;
332                     }
333                 }
334             } while (id == null);
335             return Boolean.FALSE;
336         }
337     }
338
339 }
340