2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2019 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.lockingservice.cassandra;
25 import java.util.HashSet;
27 import org.onap.music.datastore.MusicDataStoreHandle;
28 import org.onap.music.datastore.PreparedQueryObject;
29 import org.onap.music.eelf.logging.EELFLoggerDelegate;
30 import org.onap.music.exceptions.MusicServiceException;
31 import org.onap.music.main.MusicCore;
32 import org.onap.music.main.MusicUtil;
33 import com.datastax.driver.core.ResultSet;
34 import com.datastax.driver.core.Row;
36 public class LockCleanUpDaemon extends Thread {
38 boolean terminated = false;
39 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LockCleanUpDaemon.class);
42 public LockCleanUpDaemon() {
47 if (MusicUtil.getLockDaemonSleepTimeMs()<0) {
53 } catch (MusicServiceException e) {
54 logger.warn(EELFLoggerDelegate.applicationLogger, "Unable to clean up locks", e);
57 Thread.sleep(MusicUtil.getLockDaemonSleepTimeMs());
58 } catch (InterruptedException e) {
59 logger.warn(EELFLoggerDelegate.applicationLogger, "Interrupted exception", e);
65 private void cleanupStaleLocks() throws MusicServiceException {
66 Set<String> lockQTables = getLockQTables();
67 logger.info(EELFLoggerDelegate.applicationLogger, "Lock q tables found: " + lockQTables);
68 for(String lockTable: lockQTables) {
70 cleanUpLocksFromTable(lockTable);
71 } catch (MusicServiceException e) {
72 logger.warn(EELFLoggerDelegate.applicationLogger, "Unable to clear locks on table " + lockTable, e);
78 private Set<String> getLockQTables() throws MusicServiceException {
79 Set<String> keyspacesToCleanUp = MusicUtil.getKeyspacesToCleanLocks();
80 Set<String> lockQTables = new HashSet<>();
82 PreparedQueryObject query = new PreparedQueryObject();
83 query.appendQueryString("SELECT keyspace_name, table_name FROM system_schema.tables;");
84 ResultSet results = MusicCore.get(query);
86 for (Row row: results) {
87 if (keyspacesToCleanUp.contains(row.getString("keyspace_name"))
88 && row.getString("table_name").toLowerCase().startsWith(CassaLockStore.table_prepend_name.toLowerCase()) ) {
89 lockQTables.add(row.getString("keyspace_name") + "." + row.getString("table_name"));
95 private void cleanUpLocksFromTable(String lockTable) throws MusicServiceException {
96 PreparedQueryObject query = new PreparedQueryObject();
97 query.appendQueryString("SELECT * from " + lockTable);
98 ResultSet results = MusicCore.get(query);
99 for (Row lock: results) {
100 if (!lock.isNull("lockreference")) {
102 deleteLockIfStale(lockTable, lock);
103 } catch (MusicServiceException e) {
104 logger.warn(EELFLoggerDelegate.applicationLogger, "Unable to delete a potentially stale lock " + lock, e);
111 private void deleteLockIfStale(String lockTable, Row lock) throws MusicServiceException {
112 final String CREATETIME="createtime";
113 final String ACQUIRETIME="acquiretime";
114 if (lock.isNull(CREATETIME) && lock.isNull("acquiretime")) {
118 long createTime = lock.isNull(CREATETIME) ? 0 : Long.parseLong(lock.getString(CREATETIME));
119 long acquireTime = lock.isNull(ACQUIRETIME) ? 0 : Long.parseLong(lock.getString(ACQUIRETIME));
120 long row_access_time = Math.max(createTime, acquireTime);
121 if (System.currentTimeMillis() > row_access_time + MusicUtil.getDefaultLockLeasePeriod()) {
122 logger.info(EELFLoggerDelegate.applicationLogger, "Stale lock detected and being removed: " + lock);
123 PreparedQueryObject query = new PreparedQueryObject();
124 query.appendQueryString("DELETE FROM " + lockTable + " WHERE key='" + lock.getString("key") + "' AND " +
125 "lockreference=" + lock.getLong("lockreference") + " IF EXISTS;");
126 MusicDataStoreHandle.getDSHandle().getSession().execute(query.getQuery());
130 public void terminate() {