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