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;
26 import java.util.List;
28 import org.onap.music.datastore.MusicDataStoreHandle;
29 import org.onap.music.datastore.PreparedQueryObject;
30 import org.onap.music.eelf.logging.EELFLoggerDelegate;
31 import org.onap.music.exceptions.MusicQueryException;
32 import org.onap.music.exceptions.MusicServiceException;
33 import org.onap.music.main.MusicCore;
34 import org.onap.music.main.MusicUtil;
35 import com.datastax.driver.core.ResultSet;
36 import com.datastax.driver.core.Row;
38 public class LockCleanUpDaemon extends Thread {
40 boolean terminated = false;
41 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LockCleanUpDaemon.class);
44 public LockCleanUpDaemon() {
49 if (MusicUtil.getLockDaemonSleepTimeMs()<0) {
55 } catch (MusicServiceException e) {
56 logger.warn(EELFLoggerDelegate.applicationLogger, "Unable to clean up locks", e);
59 Thread.sleep(MusicUtil.getLockDaemonSleepTimeMs());
60 } catch (InterruptedException e) {
66 private void cleanupStaleLocks() throws MusicServiceException {
67 Set<String> lockQTables = getLockQTables();
68 logger.info(EELFLoggerDelegate.applicationLogger, "Lock q tables found: " + lockQTables);
69 for(String lockTable: lockQTables) {
71 cleanUpLocksFromTable(lockTable);
72 } catch (MusicServiceException e) {
73 logger.warn(EELFLoggerDelegate.applicationLogger, "Unable to clear locks on table " + lockTable, e);
79 private Set<String> getLockQTables() throws MusicServiceException {
80 Set<String> keyspacesToCleanUp = MusicUtil.getKeyspacesToCleanLocks();
81 Set<String> lockQTables = new HashSet<>();
83 PreparedQueryObject query = new PreparedQueryObject();
84 query.appendQueryString("SELECT keyspace_name, table_name FROM system_schema.tables;");
85 ResultSet results = MusicCore.get(query);
87 for (Row row: results) {
88 if (keyspacesToCleanUp.contains(row.getString("keyspace_name"))
89 && row.getString("table_name").toLowerCase().startsWith(CassaLockStore.table_prepend_name.toLowerCase()) ) {
90 lockQTables.add(row.getString("keyspace_name") + "." + row.getString("table_name"));
96 private void cleanUpLocksFromTable(String lockTable) throws MusicServiceException {
97 PreparedQueryObject query = new PreparedQueryObject();
98 query.appendQueryString("SELECT * from " + lockTable);
99 ResultSet results = MusicCore.get(query);
100 for (Row lock: results) {
101 if (!lock.isNull("lockreference")) {
103 deleteLockIfStale(lockTable, lock);
104 } catch (MusicServiceException e) {
105 logger.warn(EELFLoggerDelegate.applicationLogger, "Unable to delete a potentially stale lock " + lock, e);
112 private void deleteLockIfStale(String lockTable, Row lock) throws MusicServiceException {
113 if (lock.isNull("createtime") && lock.isNull("acquiretime")) {
117 long createTime = lock.isNull("createtime") ? 0 : Long.parseLong(lock.getString("createtime"));
118 long acquireTime = lock.isNull("acquiretime") ? 0 : Long.parseLong(lock.getString("acquiretime"));
119 long row_access_time = Math.max(createTime, acquireTime);
120 if (System.currentTimeMillis() > row_access_time + MusicUtil.getDefaultLockLeasePeriod()) {
121 logger.info(EELFLoggerDelegate.applicationLogger, "Stale lock detected and being removed: " + lock);
122 PreparedQueryObject query = new PreparedQueryObject();
123 query.appendQueryString("DELETE FROM " + lockTable + " WHERE key='" + lock.getString("key") + "' AND " +
124 "lockreference=" + lock.getLong("lockreference") + " IF EXISTS;");
125 MusicDataStoreHandle.getDSHandle().getSession().execute(query.getQuery());
129 public void terminate() {