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