diff options
Diffstat (limited to 'src/main')
16 files changed, 24 insertions, 2090 deletions
diff --git a/src/main/java/org/onap/music/authentication/CachingUtil.java b/src/main/java/org/onap/music/authentication/CachingUtil.java index 534038e3..80eed1e6 100755 --- a/src/main/java/org/onap/music/authentication/CachingUtil.java +++ b/src/main/java/org/onap/music/authentication/CachingUtil.java @@ -64,11 +64,12 @@ public class CachingUtil implements Runnable { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CachingUtil.class); + /** keyspace & ns */ private static CacheAccess<String, String> musicCache = JCS.getInstance("musicCache"); - private static CacheAccess<String, Map<String, String>> aafCache = JCS.getInstance("aafCache"); + /** cache to hold isaaf application */ private static CacheAccess<String, String> appNameCache = JCS.getInstance("appNameCache"); + /** hold user creds for namespace */ private static CacheAccess<String, Map<String, String>> musicValidateCache = JCS.getInstance("musicValidateCache"); - private static CacheAccess<String, List<String>> callbackNotifyList = JCS.getInstance("eternalCache"); private static Map<String, Number> userAttempts = new HashMap<>(); private static Map<String, Calendar> lastFailedTime = new HashMap<>(); private static CacheAccess<String, PreparedStatement> queryBank = JCS.getInstance("statementBank"); @@ -98,26 +99,6 @@ public class CachingUtil implements Runnable { private static final String USERNAME="username"; private static final String PASSWORD="password"; - - public boolean isCacheRefreshNeeded() { - if (aafCache.get("initBlankMap") == null) - return true; - return false; - } - - public static void updateCallbackNotifyList(List<String> notifyList) { - logger.info("callbackNotifyList: updating cache....."); - callbackNotifyList.put("callbackNotify", notifyList); - } - - public static List<String> getCallbackNotifyList() { - return callbackNotifyList.get("callbackNotify"); - } - - public void initializeMusicCache() { - logger.info(EELFLoggerDelegate.applicationLogger,"Initializing Music Cache..."); - musicCache.put("isInitialized", "true"); - } public void initializeAafCache() throws MusicServiceException { logger.info(EELFLoggerDelegate.applicationLogger,"Resetting and initializing AAF Cache..."); @@ -145,7 +126,7 @@ public class CachingUtil implements Runnable { if (responseObj) { map = new HashMap<>(); map.put(userId, password); - aafCache.put(nameSpace, map); + musicValidateCache.put(nameSpace, map); musicCache.put(keySpace, nameSpace); logger.debug("Cronjob: Cache Updated with AAF response for namespace " + nameSpace); @@ -171,10 +152,10 @@ public class CachingUtil implements Runnable { public static boolean authenticateAAFUser(String nameSpace, String userId, String password, String keySpace) throws Exception { - if (aafCache.get(nameSpace) != null && musicCache.get(keySpace)!=null) { + if (musicValidateCache.get(nameSpace) != null && musicCache.get(keySpace)!=null) { if (keySpace != null && !musicCache.get(keySpace).equals(nameSpace)) { logger.info(EELFLoggerDelegate.applicationLogger,"Create new application for the same namespace."); - } else if (aafCache.get(nameSpace).get(userId).equals(password)) { + } else if (musicValidateCache.get(nameSpace).get(userId).equals(password)) { logger.info(EELFLoggerDelegate.applicationLogger,"Authenticated with cache value.."); // reset invalid attempts to 0 userAttempts.put(nameSpace, 0); @@ -214,7 +195,7 @@ public class CachingUtil implements Runnable { logger.info(EELFLoggerDelegate.applicationLogger,"Valid user. Cache is updated for "+nameSpace); Map<String, String> map = new HashMap<>(); map.put(userId, password); - aafCache.put(nameSpace, map); + musicValidateCache.put(nameSpace, map); musicCache.put(keySpace, nameSpace); return true; } @@ -475,16 +456,4 @@ public class CachingUtil implements Runnable { CachingUtil.updateMusicValidateCache(nameSpace, userId, pwd); return resultMap; } - - public static void deleteKeysFromDB(String deleteKeys) { - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "DELETE FROM admin.locks WHERE lock_id IN ("+deleteKeys+")"); - try { - MusicCore.nonKeyRelatedPut(pQuery, "eventual"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), "Deleting keys from " - + "DB failed."); - } - } } diff --git a/src/main/java/org/onap/music/authentication/MusicAuthentication.java b/src/main/java/org/onap/music/authentication/MusicAuthentication.java index 10f1e30c..6c38e6df 100644 --- a/src/main/java/org/onap/music/authentication/MusicAuthentication.java +++ b/src/main/java/org/onap/music/authentication/MusicAuthentication.java @@ -62,8 +62,7 @@ public class MusicAuthentication implements MusicAuthenticator { */ @Deprecated public static Map<String, Object> autheticateUser(String nameSpace, String userId, - String password, String keyspace, String aid, String operation) - throws Exception { + String password, String keyspace, String aid, String operation) { logger.info(EELFLoggerDelegate.applicationLogger,"Inside User Authentication......."); Map<String, Object> resultMap = new HashMap<>(); String uuid = null; @@ -132,10 +131,17 @@ public class MusicAuthentication implements MusicAuthenticator { PreparedQueryObject pQuery = new PreparedQueryObject(); pQuery.appendQueryString( "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); + try { + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), + MusicUtil.DEFAULTKEYSPACENAME)); + } catch (Exception e1) { + logger.error(EELFLoggerDelegate.errorLogger, e1, "Can not authenticate for createkeyspace", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); + resultMap.put("Exception", "Cannot authenticate for createKeyspace"); + return resultMap; + } + try { Row rs = MusicCore.get(pQuery).one(); diff --git a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java index c523d5f4..6ea05c7b 100644 --- a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java +++ b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java @@ -52,7 +52,6 @@ import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; import org.onap.music.response.jsonobjects.JsonResponse; import org.onap.music.rest.RestMusicAdminAPI; -import org.onap.music.service.impl.MusicZKCore; import org.onap.music.authentication.MusicAuthentication; import org.onap.music.conductor.*; diff --git a/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java b/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java index 1f9fe5ba..079fc579 100644 --- a/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java +++ b/src/main/java/org/onap/music/eelf/healthcheck/MusicHealthCheck.java @@ -33,10 +33,8 @@ import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; import org.onap.music.exceptions.MusicLockingException; import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.zookeeper.MusicLockingService; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; -import org.onap.music.service.impl.MusicZKCore; import org.onap.music.main.MusicCore; import com.datastax.driver.core.ConsistencyLevel; @@ -50,7 +48,6 @@ public class MusicHealthCheck { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class); private String cassandrHost; - private String zookeeperHost; public String getCassandraStatus(String consistency) { logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Cassandra"); @@ -67,7 +64,7 @@ public class MusicHealthCheck { try { result = getAdminKeySpace(consistency); } catch (MusicServiceException e1) { - logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); + logger.error(EELFLoggerDelegate.errorLogger, e1, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); } } else { logger.error("Error", e); @@ -98,29 +95,11 @@ public class MusicHealthCheck { try { rs = MusicCore.nonKeyRelatedPut(pQuery, ConsistencyLevel.ONE.toString()); } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); + logger.error(EELFLoggerDelegate.errorLogger, e, AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); } return rs != null && rs.getResult().toLowerCase().contains("success"); } - public String getZookeeperStatus() { - - String host = MusicUtil.getMyZkHost(); - logger.info(EELFLoggerDelegate.applicationLogger, "Getting Status for Zookeeper Host: " + host); - try { - MusicLockingService lockingService = MusicZKCore.getLockingServiceHandle(); - // additionally need to call the ZK to create,aquire and delete lock - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR, - ErrorTypes.CONNECTIONERROR, ErrorSeverity.CRITICAL); - return "INACTIVE"; - } - - logger.info(EELFLoggerDelegate.applicationLogger, "Zookeeper is Active and Running"); - return "ACTIVE"; - - } - public String getCassandrHost() { return cassandrHost; } @@ -129,12 +108,4 @@ public class MusicHealthCheck { this.cassandrHost = cassandrHost; } - public String getZookeeperHost() { - return zookeeperHost; - } - - public void setZookeeperHost(String zookeeperHost) { - this.zookeeperHost = zookeeperHost; - } - } diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java b/src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java deleted file mode 100644 index e8ad3a5f..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/LockListener.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.lockingservice.zookeeper; - -/** - * This class has two methods which are call back methods when a lock is acquired and when the lock - * is released. - * - */ -public interface LockListener { - /** - * call back called when the lock is acquired - */ - public void lockAcquired(); - - /** - * call back called when the lock is released. - */ - public void lockReleased(); -} diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java b/src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java deleted file mode 100644 index a35e236b..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/MusicLockingService.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.lockingservice.zookeeper; - - -import java.io.IOException; -import java.util.StringTokenizer; -import java.util.concurrent.CountDownLatch; - -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.Watcher.Event.KeeperState; -import org.apache.zookeeper.ZooKeeper; -import org.onap.music.authentication.CachingUtil; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.onap.music.exceptions.MusicLockingException; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.MusicLockState; -import org.onap.music.main.MusicUtil; - - -public class MusicLockingService implements Watcher { - - - private static final int SESSION_TIMEOUT = 180000; - ZkStatelessLockService zkLockHandle = null; - private CountDownLatch connectedSignal = new CountDownLatch(1); - private static EELFLoggerDelegate logger = - EELFLoggerDelegate.getLogger(MusicLockingService.class); - - public MusicLockingService() throws MusicServiceException { - try { - ZooKeeper zk = new ZooKeeper(MusicUtil.getMyZkHost(), SESSION_TIMEOUT, this); - connectedSignal.await(); - zkLockHandle = new ZkStatelessLockService(zk); - } catch (IOException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); - throw new MusicServiceException("IO Error has occured" + e.getMessage()); - } catch (InterruptedException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw new MusicServiceException("Exception Occured " + e.getMessage()); - } - } - - public MusicLockingService(String lockServer) { - try { - ZooKeeper zk = new ZooKeeper(lockServer, SESSION_TIMEOUT, this); - connectedSignal.await(); - zkLockHandle = new ZkStatelessLockService(zk); - } catch (IOException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR); - }catch( InterruptedException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch(Exception e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - } - - public ZkStatelessLockService getzkLockHandle() { - return zkLockHandle; - } - - public void createLockaIfItDoesNotExist(String lockName) { - if (!zkLockHandle.checkIfLockExists(lockName)) { - String lockHolder = null; - MusicLockState ml = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder); - byte[] data = ml.serialize(); - zkLockHandle.createLock(lockName, data); - } - } - - public void setLockState(String lockName, MusicLockState mls) { - byte[] data = mls.serialize(); - zkLockHandle.setNodeData(lockName, data); - } - - public MusicLockState getLockState(String lockName) throws MusicLockingException { - - byte[] data = null; - try{ - data = zkLockHandle.getNodeData(lockName); - }catch (Exception ex){ - logger.error(EELFLoggerDelegate.errorLogger, ex,AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - if(data !=null) - return MusicLockState.deSerialize(data); - else { - logger.error(EELFLoggerDelegate.errorLogger,"Invalid lock or acquire failed",AppMessages.INVALIDLOCK, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw new MusicLockingException("Invalid lock or acquire failed"); - } - } - - public String createLockId(String lockName) { - String lockIdWithSlash = zkLockHandle.createLockId(lockName); - return lockIdWithSlash.replace('/', '$'); - } - - public boolean isMyTurn(String lockIdWithDollar) { - String lockId = lockIdWithDollar.replace('$', '/'); - StringTokenizer st = new StringTokenizer(lockId); - String lockName = "/" + st.nextToken("/"); - try { - return zkLockHandle.lock(lockName, lockId); - } catch (KeeperException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.LOCKINGERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch( InterruptedException e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch(Exception e) { - logger.error("Error", e); - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - return false; - } - - public void unlockAndDeleteId(String lockIdWithDollar) throws KeeperException.NoNodeException { - String lockId = lockIdWithDollar.replace('$', '/'); - zkLockHandle.unlock(lockId); - CachingUtil.deleteKeysFromDB("'"+lockId+"'"); - } - - public void deleteLock(String lockName) throws MusicLockingException { - if(lockIdExists(lockName)) - zkLockHandle.deleteLock(lockName); - else{ - throw new MusicLockingException("Lock does not exist.Please check the lock: " + lockName + " and try again"); - } - } - - public String whoseTurnIsIt(String lockName) { - String lockHolder = zkLockHandle.currentLockHolder(lockName); - return lockHolder.replace('/', '$'); - - } - - public void process(WatchedEvent event) { // Watcher interface - if (event.getState() == KeeperState.SyncConnected) { - connectedSignal.countDown(); - } - } - - - public void close() { - zkLockHandle.close(); - } - - public boolean lockIdExists(String lockIdWithDollar) { - String lockId = lockIdWithDollar.replace('$', '/'); - return zkLockHandle.checkIfLockExists(lockId); - } - -} diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java deleted file mode 100644 index 2e88cc7a..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ProtocolSupport.java +++ /dev/null @@ -1,217 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * Modifications Copyright (C) 2018 IBM. - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.lockingservice.zookeeper; - -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.ZooKeeper; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.onap.music.lockingservice.zookeeper.ZooKeeperOperation; - -import java.util.List; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * A base class for protocol implementations which provides a number of higher level helper methods - * for working with ZooKeeper along with retrying synchronous operations if the connection to - * ZooKeeper closes such as {@link #retryOperation(ZooKeeperOperation)} - * - */ -class ProtocolSupport { - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProtocolSupport.class); - - protected ZooKeeper zookeeper; - private AtomicBoolean closed = new AtomicBoolean(false); - private long retryDelay = 500L; - private int retryCount = 10; - private List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE; - - /** - * Closes this strategy and releases any ZooKeeper resources; but keeps the ZooKeeper instance - * open - */ - public void close() { - if (closed.compareAndSet(false, true)) { - doClose(); - } - } - - /** - * return zookeeper client instance - * - * @return zookeeper client instance - */ - public ZooKeeper getZookeeper() { - return zookeeper; - } - - /** - * return the acl its using - * - * @return the acl. - */ - public List<ACL> getAcl() { - return acl; - } - - /** - * set the acl - * - * @param acl the acl to set to - */ - public void setAcl(List<ACL> acl) { - this.acl = acl; - } - - /** - * get the retry delay in milliseconds - * - * @return the retry delay - */ - public long getRetryDelay() { - return retryDelay; - } - - /** - * Sets the time waited between retry delays - * - * @param retryDelay the retry delay - */ - public void setRetryDelay(long retryDelay) { - this.retryDelay = retryDelay; - } - - /** - * Allow derived classes to perform some custom closing operations to release resources - */ - protected void doClose() { - throw new UnsupportedOperationException(); - } - - - /** - * Perform the given operation, retrying if the connection fails - * - * @return object. it needs to be cast to the callee's expected return type. - * @param operation FILL IN - * @throws KeeperException FILL IN - * @throws InterruptedException FILL IN - */ - protected Object retryOperation(ZooKeeperOperation operation) - throws KeeperException, InterruptedException { - KeeperException exception = null; - for (int i = 0; i < retryCount; i++) { - try { - return operation.execute(); - } catch (KeeperException.SessionExpiredException e) { - logger.error(EELFLoggerDelegate.errorLogger, e,AppMessages.SESSIONEXPIRED+" for: " + zookeeper + " so reconnecting due to: " + e, ErrorSeverity.ERROR, ErrorTypes.SESSIONEXPIRED); - throw e; - } catch (KeeperException.ConnectionLossException e) { - if (exception == null) { - exception = e; - } - logger.error(EELFLoggerDelegate.errorLogger, e,AppMessages.CONNCECTIVITYERROR, ErrorSeverity.ERROR, ErrorTypes.SESSIONEXPIRED); - logger.error(EELFLoggerDelegate.errorLogger, e,"Attempt " + i + " failed with connection loss so attempting to reconnect: " + e); - - retryDelay(i); - } - } - if(exception == null) - { - throw new NullPointerException(); - } - else{ - throw exception; - } - } - - /** - * Ensures that the given path exists with no data, the current ACL and no flags - * - * @param path the lock path - */ - protected void ensurePathExists(String path) { - ensureExists(path, null, acl, CreateMode.PERSISTENT); - } - - /** - * Ensures that the given path exists with the given data, ACL and flags - * - * @param path the lock path - * @param data the data - * @param acl list of ACLs applying to the path - * @param flags create mode flags - */ - protected void ensureExists(final String path, final byte[] data, final List<ACL> acl, - final CreateMode flags) { - try { - retryOperation(new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - Stat stat = zookeeper.exists(path, false); - if (stat != null) { - return true; - } - zookeeper.create(path, data, acl, flags); - return true; - } - }); - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - } - - /** - * Returns true if this protocol has been closed - * - * @return true if this protocol is closed - */ - protected boolean isClosed() { - return closed.get(); - } - - /** - * Performs a retry delay if this is not the first attempt - * - * @param attemptCount the number of the attempts performed so far - */ - protected void retryDelay(int attemptCount) { - if (attemptCount > 0) { - try { - Thread.sleep(attemptCount * retryDelay); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e,AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR); - logger.error(EELFLoggerDelegate.errorLogger, e,"Thread failed to sleep: " + e); - Thread.currentThread().interrupt(); - } - } - } -} diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java deleted file mode 100644 index 5bdc5457..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ZNodeName.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (c) 2018 IBM - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.lockingservice.zookeeper; - -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; - -/** - * Represents an ephemeral znode name which has an ordered sequence number and can be sorted in - * order - * - */ -class ZNodeName implements Comparable<ZNodeName> { - private final String name; - private String prefix; - private int sequence = -1; - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ZNodeName.class); - - public ZNodeName(String name) { - if (name == null) { - throw new NullPointerException("id cannot be null"); - } - this.name = name; - this.prefix = name; - int idx = name.lastIndexOf('-'); - if (idx >= 0) { - this.prefix = name.substring(0, idx); - try { - this.sequence = Integer.parseInt(name.substring(idx + 1)); - // If an exception occurred we misdetected a sequence suffix, - // so return -1. - } catch (NumberFormatException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Number format exception "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR); - } catch (ArrayIndexOutOfBoundsException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Array out of bounds for "+idx, ErrorSeverity.ERROR, ErrorTypes.GENERALSERVICEERROR); - } - } - } - - @Override - public String toString() { - return name; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - - ZNodeName sequence = (ZNodeName) o; - - if (!name.equals(sequence.name)) - return false; - - return true; - } - - @Override - public int hashCode() { - return name.hashCode() + 37; - } - - public int compareTo(ZNodeName that) { - int answer = this.prefix.compareTo(that.prefix); - if (answer == 0) { - int s1 = this.sequence; - int s2 = that.sequence; - if (s1 == -1 && s2 == -1) { - return this.name.compareTo(that.name); - } - answer = s1 == -1 ? 1 : s2 == -1 ? -1 : s1 - s2; - } - return answer; - } - - /** - * Returns the name of the znode - */ - public String getName() { - return name; - } - - /** - * Returns the sequence number - */ - public int getZNodeName() { - return sequence; - } - - /** - * Returns the text prefix before the sequence number - */ - public String getPrefix() { - return prefix; - } -} diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java deleted file mode 100644 index e8ed257a..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java +++ /dev/null @@ -1,377 +0,0 @@ -/* - * ============LICENSE_START========================================== org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (c) 2018 IBM. - * Modifications Copyright (c) 2019 Samsung. - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.lockingservice.zookeeper; - - -import java.util.List; -import java.util.SortedSet; -import java.util.TreeSet; -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.KeeperException.NoNodeException; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.ZooKeeper; -import org.apache.zookeeper.data.ACL; -import org.apache.zookeeper.data.Stat; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; - -import com.datastax.driver.core.DataType; - -/** - * A <a href="package.html">protocol to implement an exclusive write lock or to elect a leader</a>. - * <p/> - * You invoke {@link #lock()} to start the process of grabbing the lock; you may get the lock then - * or it may be some time later. - * <p/> - * You can register a listener so that you are invoked when you get the lock; otherwise you can ask - * if you have the lock by calling {@link #isOwner()} - * - */ -public class ZkStatelessLockService extends ProtocolSupport { - public ZkStatelessLockService(ZooKeeper zk) { - zookeeper = zk; - } - - private static EELFLoggerDelegate logger = - EELFLoggerDelegate.getLogger(ZkStatelessLockService.class); - - protected void createLock(final String path, final byte[] data) { - final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE; - try { - retryOperation(new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - zookeeper.create(path, data, acl, CreateMode.PERSISTENT); - return true; - } - }); - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - } - - @Override - public void close() { - try { - zookeeper.close(); - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - } - - public void setNodeData(final String lockName, final byte[] data) { - try { - retryOperation(new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - zookeeper.getSessionId(); - zookeeper.setData("/" + lockName, data, -1); - return true; - } - }); - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - - } - - public byte[] getNodeData(final String lockName) { - try { - if (zookeeper.exists("/" + lockName, null) != null) - return zookeeper.getData("/" + lockName, false, null); - else - return null; - - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - return null; - } - - public boolean checkIfLockExists(String lockName) { - boolean result = false; - try { - Stat stat = zookeeper.exists(lockName, false); - if (stat != null) { - result = true; - } - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - Thread.currentThread().interrupt(); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - return result; - } - - public void createNode(String nodeName) { - ensurePathExists(nodeName); - } - - public String createLockId(String dir) { - ensurePathExists(dir); - LockZooKeeperOperation zop = new LockZooKeeperOperation(dir); - try { - retryOperation(zop); - }catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - Thread.currentThread().interrupt(); - }catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - return zop.getId(); - } - - /** - * Attempts to acquire the exclusive write lock returning whether or not it was acquired. Note - * that the exclusive lock may be acquired some time later after this method has been invoked - * due to the current lock owner going away. - */ - public synchronized boolean lock(String dir, String lockId) - throws KeeperException, InterruptedException { - if (isClosed()) { - return false; - } - LockZooKeeperOperation zop = new LockZooKeeperOperation(dir, lockId); - return (Boolean) retryOperation(zop); - } - - /** - * Removes the lock or associated znode if you no longer require the lock. this also removes - * your request in the queue for locking in case you do not already hold the lock. - * - * @throws RuntimeException throws a runtime exception if it cannot connect to zookeeper. - * @throws NoNodeException - */ - public synchronized void unlock(String lockId) throws RuntimeException, KeeperException.NoNodeException { - final String id = lockId; - if (!isClosed() && id != null) { - try { - ZooKeeperOperation zopdel = new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - zookeeper.delete(id, -1); - return Boolean.TRUE; - } - }; - zopdel.execute(); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - // set that we have been interrupted. - Thread.currentThread().interrupt(); - } catch (KeeperException.NoNodeException e) { - // do nothing - throw new KeeperException.NoNodeException("Lock doesn't exists. Release lock operation failed."); - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e); - } - } - } - - public synchronized String currentLockHolder(String mainLock) { - final String id = mainLock; - if (!isClosed() && id != null) { - List<String> names; - try { - names = zookeeper.getChildren(id, false); - if (names.isEmpty()) - return ""; - SortedSet<ZNodeName> sortedNames = new TreeSet<>(); - for (String name : names) { - sortedNames.add(new ZNodeName(id + "/" + name)); - } - return sortedNames.first().getName(); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - // set that we have been interrupted. - Thread.currentThread().interrupt(); - } catch (KeeperException.NoNodeException e) { - // do nothing - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e); - } - } - return "No lock holder!"; - } - - public synchronized void deleteLock(String mainLock) { - final String id = mainLock; - if (!isClosed() && id != null) { - try { - ZooKeeperOperation zopdel = new ZooKeeperOperation() { - public boolean execute() throws KeeperException, InterruptedException { - List<String> names = zookeeper.getChildren(id, false); - for (String name : names) { - zookeeper.delete(id + "/" + name, -1); - } - zookeeper.delete(id, -1); - return Boolean.TRUE; - } - }; - zopdel.execute(); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - // set that we have been interrupted. - Thread.currentThread().interrupt(); - } catch (KeeperException.NoNodeException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - // do nothing - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - throw (RuntimeException) new RuntimeException(e.getMessage()).initCause(e); - } - } - - } - - /** - * a zoookeeper operation that is mainly responsible for all the magic required for locking. - */ - private class LockZooKeeperOperation implements ZooKeeperOperation { - - /** - * find if we have been created earlier if not create our node - * - * @param prefix the prefix node - * @param zookeeper the zookeeper client - * @param dir the dir parent - * @throws KeeperException - * @throws InterruptedException - */ - private String dir; - private String id = null; - - public String getId() { - return id; - } - - public LockZooKeeperOperation(String dir) { - this.dir = dir; - } - - public LockZooKeeperOperation(String dir, String id) { - this.dir = dir; - this.id = id; - } - - /** - * the command that is run and retried for actually obtaining the lock - * - * @return if the command was successful or not - */ - public boolean execute() throws KeeperException, InterruptedException { - do { - if (id == null) { - String prefix = "x-"; - byte[] data = {0x12, 0x34}; - id = zookeeper.create(dir + "/" + prefix, data, getAcl(), - CreateMode.PERSISTENT_SEQUENTIAL); - - if (logger.isDebugEnabled()) { - logger.debug(EELFLoggerDelegate.debugLogger, "Created id: " + id); - } - if (id != null) { - Stat stat = null; - try { - stat = zookeeper.exists(id, false); - } catch (InterruptedException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - Thread.currentThread().interrupt(); - } catch (KeeperException e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); - } - - if (stat != null){ - Long ctime = stat.getCtime(); - MusicUtil.zkNodeMap.put(id, ctime); - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "INSERT INTO admin.locks(lock_id, ctime) VALUES (?,?)"); - try { - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), id)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), ctime)); - MusicCore.eventualPut(pQuery); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); - } - } - break; - } - } - if (id != null) { - List<String> names = zookeeper.getChildren(dir, false); - if (names.isEmpty()) { - logger.info(EELFLoggerDelegate.applicationLogger, "No children in: " + dir - + " when we've just " + "created one! Lets recreate it..."); - // lets force the recreation of the id - id = null; - return Boolean.FALSE; - - } else { - // lets sort them explicitly (though they do seem to come back in order - // ususally :) - ZNodeName idName = new ZNodeName(id); - SortedSet<ZNodeName> sortedNames = new TreeSet<>(); - for (String name : names) { - sortedNames.add(new ZNodeName(dir + "/" + name)); - } - if (!sortedNames.contains(idName)) - return Boolean.FALSE; - - SortedSet<ZNodeName> lessThanMe = sortedNames.headSet(idName); - if (!lessThanMe.isEmpty()) { - ZNodeName lastChildName = lessThanMe.last(); - String lastChildId = lastChildName.getName(); - if (logger.isDebugEnabled()) { - logger.debug(EELFLoggerDelegate.debugLogger, "watching less than me node: " + lastChildId); - } - Stat stat = zookeeper.exists(lastChildId, false); - if (stat != null) { - return Boolean.FALSE; - } else { - logger.info(EELFLoggerDelegate.applicationLogger, - "Could not find the" + " stats for less than me: " - + lastChildName.getName()); - } - } else - return Boolean.TRUE; - } - } - } while (id == null); - return Boolean.FALSE; - } - } - -} - diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java deleted file mode 100644 index 34efebf2..00000000 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ZooKeeperOperation.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.lockingservice.zookeeper; - -import org.apache.zookeeper.KeeperException; - -/** - * A callback object which can be used for implementing retry-able operations in the - * {@link org.onap.music.lockingservice.zookeeper.ProtocolSupport} class - * - */ -public interface ZooKeeperOperation { - - /** - * Performs the operation - which may be involved multiple times if the connection - * to ZooKeeper closes during this operation - * - * @return the result of the operation or null - * @throws KeeperException FILL IN - * @throws InterruptedException FILL IN - */ - public boolean execute() throws KeeperException, InterruptedException; -} diff --git a/src/main/java/org/onap/music/main/CronJobManager.java b/src/main/java/org/onap/music/main/CronJobManager.java deleted file mode 100644 index 0d7d9239..00000000 --- a/src/main/java/org/onap/music/main/CronJobManager.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (c) 2019 Samsung - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.main; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Iterator; -import org.onap.music.authentication.CachingUtil; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.onap.music.exceptions.MusicLockingException; -import org.onap.music.exceptions.MusicServiceException; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; - -@Component -public class CronJobManager { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class); - - private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); - - - @Scheduled(cron = "0 0 0 * * ?") - public void scheduleTaskWithFixedRate() { - logger.info("Executing cronjob to cleanup locks..", dateTimeFormatter.format(LocalDateTime.now()) ); - deleteLocksFromDB(); - } - - public void deleteLocksFromDB() { - PreparedQueryObject pQuery = new PreparedQueryObject(); - String consistency = MusicUtil.EVENTUAL; - pQuery.appendQueryString("CREATE TABLE IF NOT EXISTS admin.locks ( lock_id text PRIMARY KEY, ctime text)"); - try { - ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency); - if ( result.equals(ResultType.FAILURE)) { - logger.error(EELFLoggerDelegate.errorLogger,"Error creating Admin.locks table.",AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR); - } - } catch (MusicServiceException e1) { - logger.error(EELFLoggerDelegate.errorLogger,e1,AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR); - } - - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "select * from admin.locks"); - try { - ResultSet rs = MusicCore.get(pQuery); - Iterator<Row> it = rs.iterator(); - StringBuilder deleteKeys = new StringBuilder(); - Boolean expiredKeys = false; - while (it.hasNext()) { - Row row = it.next(); - String id = row.getString("lock_id"); - long ctime = Long.parseLong(row.getString("ctime")); - if(System.currentTimeMillis() >= ctime + 24 * 60 * 60 * 1000) { - expiredKeys = true; - String new_id = id.substring(1); - try { - MusicCore.deleteLock(new_id); - } catch (MusicLockingException e) { - logger.info(EELFLoggerDelegate.applicationLogger, - e.getMessage()); - } - deleteKeys.append("'").append(id).append("'").append(","); - } - } - if(expiredKeys) { - deleteKeys.deleteCharAt(deleteKeys.length()-1); - CachingUtil.deleteKeysFromDB(deleteKeys.toString()); - } - } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e,AppMessages.CACHEERROR,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - } - } -} diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java index 06eeb805..aa82ea97 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -58,7 +58,6 @@ import org.onap.music.exceptions.MusicQueryException; import org.onap.music.exceptions.MusicServiceException; import org.onap.music.service.MusicCoreService; import org.onap.music.service.impl.MusicCassaCore; -import org.onap.music.service.impl.MusicZKCore; import com.datastax.driver.core.ConsistencyLevel; import com.datastax.driver.core.DataType; @@ -94,7 +93,6 @@ public class MusicUtil { public static final String USERID = "userId"; public static final String PASSWORD = "password"; public static final String CASSANDRA = "cassandra"; - public static final String ZOOKEEPER = "zookeeper"; public static final String AUTHORIZATION = "Authorization"; @@ -788,8 +786,6 @@ public class MusicUtil { public static MusicCoreService getMusicCoreService() { if(getLockUsing().equals(MusicUtil.CASSANDRA)) return MusicCassaCore.getInstance(); - else if (getLockUsing().equals(MusicUtil.ZOOKEEPER)) - return MusicZKCore.getInstance(); else return MusicCassaCore.getInstance(); } diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java index f72a1ac9..a7522b90 100755 --- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java @@ -862,10 +862,6 @@ public class RestMusicDataAPI { result = MusicCore.atomicPut(keyspace, tablename, primaryKey, queryObject, null); } - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - result = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, primaryKey, queryObject, null); - - } } catch (Exception ex) { logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); @@ -1238,11 +1234,7 @@ public class RestMusicDataAPI { } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, queryObject, conditionInfo); - } - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, - queryObject, conditionInfo); - }else if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL_NB)) { + } else if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL_NB)) { operationResult = MusicCore.eventualPut_nb(queryObject, keyspace, tablename, rowId.primarKeyValue); } @@ -1397,10 +1389,6 @@ public class RestMusicDataAPI { } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { results = MusicCore.atomicGet(keyspace, tablename, rowId.primarKeyValue, queryObject); } - - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - results = MusicCore.atomicGetWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, queryObject); - } if(results!=null && results.getAvailableWithoutFetching() >0) { return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicDataStoreHandle.marshallResults(results)).toMap()).build(); } diff --git a/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java b/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java index a9b4c7af..cb8965ee 100644 --- a/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java @@ -91,24 +91,6 @@ public class RestMusicHealthCheckAPI { } @GET - @Path("/pingZookeeper") - @ApiOperation(value = "Get Health Status", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response ZKStatus(@Context HttpServletResponse response) { - logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Zookeeper"); - Map<String, Object> resultMap = new HashMap<>(); - MusicHealthCheck ZKHealthCheck = new MusicHealthCheck(); - String status = ZKHealthCheck.getZookeeperStatus(); - if(status.equals(ACTIVE_STATUS)) { - resultMap.put(ACTIVE_STATUS, "Zookeeper is Active and Running"); - return Response.status(Status.OK).entity(resultMap).build(); - }else { - resultMap.put("INACTIVE", "Zookeeper is not responding"); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - } - - @GET @Path("/musicHealthCheck") @ApiOperation(value = "Get Health Status", response = Map.class) @Produces(MediaType.APPLICATION_JSON) @@ -116,13 +98,8 @@ public class RestMusicHealthCheckAPI { logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for Health Check status for MUSIC"); Map<String, Object> resultMap = new HashMap<>(); MusicHealthCheck healthCheck = new MusicHealthCheck(); - String status = healthCheck.getZookeeperStatus(); - if(status.equals(ACTIVE_STATUS)) { - resultMap.put("ZooKeeper", "Active"); - }else { - resultMap.put("ZooKeeper", "Inactive"); - } - status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString()); + + String status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString()); if(status.equals(ACTIVE_STATUS)) { resultMap.put("Cassandra", "Active"); } else { diff --git a/src/main/java/org/onap/music/rest/RestMusicQAPI.java b/src/main/java/org/onap/music/rest/RestMusicQAPI.java index 800dad71..0865eafe 100755 --- a/src/main/java/org/onap/music/rest/RestMusicQAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicQAPI.java @@ -57,7 +57,6 @@ import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.service.impl.MusicZKCore; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; diff --git a/src/main/java/org/onap/music/service/impl/MusicZKCore.java b/src/main/java/org/onap/music/service/impl/MusicZKCore.java deleted file mode 100644 index 93c5abc8..00000000 --- a/src/main/java/org/onap/music/service/impl/MusicZKCore.java +++ /dev/null @@ -1,888 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (c) 2019 Samsung - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ - -package org.onap.music.service.impl; - - -import java.io.StringWriter; -import java.util.List; -import java.util.Map; -import java.util.StringTokenizer; - -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.KeeperException.NoNodeException; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.datastore.jsonobjects.JsonKeySpace; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.onap.music.exceptions.MusicLockingException; -import org.onap.music.exceptions.MusicQueryException; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.lockingservice.cassandra.MusicLockState; -import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus; -import org.onap.music.lockingservice.zookeeper.MusicLockingService; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.service.MusicCoreService; -import org.onap.music.datastore.*; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.TableMetadata; - -/** - * This class ..... - * - * - */ -public class MusicZKCore implements MusicCoreService { - - public static MusicLockingService mLockHandle = null; - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicZKCore.class); - private static MusicZKCore musicZKCoreInstance = null; - - private MusicZKCore() { - - } - public static MusicZKCore getInstance() { - - if(musicZKCoreInstance == null) { - musicZKCoreInstance = new MusicZKCore(); - } - return musicZKCoreInstance; - } - - - - - - public static MusicLockingService getLockingServiceHandle() throws MusicLockingException { - logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring lock store handle"); - long start = System.currentTimeMillis(); - - if (mLockHandle == null) { - try { - mLockHandle = new MusicLockingService(); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKHANDLE,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - throw new MusicLockingException("Failed to aquire Locl store handle " + e); - } - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire lock store handle:" + (end - start) + " ms"); - return mLockHandle; - } - - - - public String createLockReference(String lockName) { - logger.info(EELFLoggerDelegate.applicationLogger,"Creating lock reference for lock name:" + lockName); - long start = System.currentTimeMillis(); - String lockId = null; - try { - lockId = getLockingServiceHandle().createLockId("/" + lockName); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.CREATELOCK+lockName,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to create lock reference:" + (end - start) + " ms"); - return lockId; - } - - /** - * - * @param key - * @return - */ - public static boolean isTableOrKeySpaceLock(String key) { - String[] splitString = key.split("\\."); - if (splitString.length > 2) - return false; - else - return true; - } - - /** - * - * @param key - * @return - */ - public static MusicLockState getMusicLockState(String key) { - long start = System.currentTimeMillis(); - try { - String[] splitString = key.split("\\."); - String keyspaceName = splitString[0]; - String tableName = splitString[1]; - String primaryKey = splitString[2]; - MusicLockState mls; - String lockName = keyspaceName + "." + tableName + "." + primaryKey; - mls = getLockingServiceHandle().getLockState(lockName); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to get lock state:" + (end - start) + " ms"); - return mls; - } catch (NullPointerException | MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.INVALIDLOCK,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - return null; - } - - public ReturnType acquireLockWithLease(String key, String lockId, long leasePeriod) { - try { - long start = System.currentTimeMillis(); - /* check if the current lock has exceeded its lease and if yes, release that lock */ - MusicLockState mls = getMusicLockState(key); - if (mls != null) { - if (mls.getLockStatus().equals(LockStatus.LOCKED)) { - logger.info(EELFLoggerDelegate.applicationLogger,"The current lock holder for " + key + " is " + mls.getLockHolder() - + ". Checking if it has exceeded lease"); - long currentLockPeriod = System.currentTimeMillis() - mls.getLeaseStartTime(); - long currentLeasePeriod = mls.getLeasePeriod(); - if (currentLockPeriod > currentLeasePeriod) { - logger.info(EELFLoggerDelegate.applicationLogger,"Lock period " + currentLockPeriod - + " has exceeded lease period " + currentLeasePeriod); - boolean voluntaryRelease = false; - String currentLockHolder = mls.getLockHolder(); - mls = releaseLock(currentLockHolder, voluntaryRelease); - } - } - } else - logger.error(EELFLoggerDelegate.errorLogger,key, AppMessages.INVALIDLOCK,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - - /* - * call the traditional acquire lock now and if the result returned is true, set the - * begin time-stamp and lease period - */ - if (acquireLock(key, lockId).getResult() == ResultType.SUCCESS) { - mls = getMusicLockState(key);// get latest state - if ( mls == null ) { - logger.info(EELFLoggerDelegate.applicationLogger,"Music Lock State is null"); - return new ReturnType(ResultType.FAILURE, "Could not acquire lock, Lock State is null"); - } - if (mls.getLeaseStartTime() == -1) {// set it again only if it is not set already - mls.setLeaseStartTime(System.currentTimeMillis()); - mls.setLeasePeriod(leasePeriod); - getLockingServiceHandle().setLockState(key, mls); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire leased lock:" + (end - start) + " ms"); - return new ReturnType(ResultType.SUCCESS, "Accquired lock"); - } else { - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to fail to acquire leased lock:" + (end - start) + " ms"); - return new ReturnType(ResultType.FAILURE, "Could not acquire lock"); - } - } catch (Exception e) { - StringWriter sw = new StringWriter(); - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR506E] Failed to aquire lock ",ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - - String exceptionAsString = sw.toString(); - return new ReturnType(ResultType.FAILURE, - "Exception thrown in acquireLockWithLease:\n" + exceptionAsString); - } - } - - public ReturnType acquireLock(String key, String lockId) throws MusicLockingException { - /* - * first check if I am on top. Since ids are not reusable there is no need to check - * lockStatus If the status is unlocked, then the above call will automatically return - * false. - */ - Boolean result = false; - try { - result = getLockingServiceHandle().isMyTurn(lockId); - } catch (MusicLockingException e2) { - logger.error(EELFLoggerDelegate.errorLogger,AppMessages.INVALIDLOCK + lockId + " " + e2); - throw new MusicLockingException(); - } - if (!result) { - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Not your turn, someone else has the lock"); - try { - if (!getLockingServiceHandle().lockIdExists(lockId)) { - logger.info(EELFLoggerDelegate.applicationLogger, "In acquire lock: this lockId doesn't exist"); - return new ReturnType(ResultType.FAILURE, "Lockid doesn't exist"); - } - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.INVALIDLOCK+lockId,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - throw new MusicLockingException(); - } - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: returning failure"); - return new ReturnType(ResultType.FAILURE, "Not your turn, someone else has the lock"); - } - - - // this is for backward compatibility where locks could also be acquired on just - // keyspaces or tables. - if (isTableOrKeySpaceLock(key)) { - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: A table or keyspace lock so no need to perform sync...so returning true"); - return new ReturnType(ResultType.SUCCESS, "A table or keyspace lock so no need to perform sync...so returning true"); - } - - // read the lock name corresponding to the key and if the status is locked or being locked, - // then return false - MusicLockState currentMls = null; - MusicLockState newMls = null; - try { - currentMls = getMusicLockState(key); - String currentLockHolder = null; - if(currentMls != null) { currentLockHolder = currentMls.getLockHolder(); }; - if (lockId.equals(currentLockHolder)) { - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: You already have the lock!"); - return new ReturnType(ResultType.SUCCESS, "You already have the lock!"); - } - } catch (NullPointerException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.INVALIDLOCK+lockId,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - - // change status to "being locked". This state transition is necessary to ensure syncing - // before granting the lock - String lockHolder = null; - boolean needToSyncQuorum = false; - if (currentMls != null) - needToSyncQuorum = currentMls.isNeedToSyncQuorum(); - - - newMls = new MusicLockState(MusicLockState.LockStatus.BEING_LOCKED, lockHolder, - needToSyncQuorum); - try { - getLockingServiceHandle().setLockState(key, newMls); - } catch (MusicLockingException e1) { - logger.error(EELFLoggerDelegate.errorLogger,e1.getMessage(), AppMessages.LOCKSTATE+key,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Set lock state to being_locked"); - - // do syncing if this was a forced lock release - if (needToSyncQuorum) { - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Since there was a forcible release, need to sync quorum!"); - try { - syncQuorum(key); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,"Failed to set Lock state " + e); - } - } - - // change status to locked - lockHolder = lockId; - needToSyncQuorum = false; - newMls = new MusicLockState(MusicLockState.LockStatus.LOCKED, lockHolder, needToSyncQuorum); - try { - getLockingServiceHandle().setLockState(key, newMls); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKSTATE+key,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Set lock state to locked and assigned current lock ref " - + lockId + " as holder"); - - return new ReturnType(result?ResultType.SUCCESS:ResultType.FAILURE, "Set lock state to locked and assigned a lock holder"); - } - - - - /** - * - * @param keyspaceName - * @param kspObject - * @return - * @throws Exception - */ - public boolean createKeyspace(String keyspaceName, JsonKeySpace kspObject) throws Exception { - return true; - } - - - private static void syncQuorum(String key) throws Exception { - logger.info(EELFLoggerDelegate.applicationLogger,"Performing sync operation---"); - String[] splitString = key.split("\\."); - String keyspaceName = splitString[0]; - String tableName = splitString[1]; - String primaryKeyValue = splitString[2]; - PreparedQueryObject selectQuery = new PreparedQueryObject(); - PreparedQueryObject updateQuery = new PreparedQueryObject(); - - // get the primary key d - TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspaceName, tableName); - String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName();// we only support single - // primary key - DataType primaryKeyType = tableInfo.getPrimaryKey().get(0).getType(); - Object cqlFormattedPrimaryKeyValue = - MusicUtil.convertToActualDataType(primaryKeyType, primaryKeyValue); - - // get the row of data from a quorum - selectQuery.appendQueryString("SELECT * FROM " + keyspaceName + "." + tableName + " WHERE " - + primaryKeyName + "= ?" + ";"); - selectQuery.addValue(cqlFormattedPrimaryKeyValue); - MusicUtil.writeBackToQuorum(selectQuery, primaryKeyName, updateQuery, keyspaceName, tableName, - cqlFormattedPrimaryKeyValue); - } - - - - - /** - * - * @param query - * @return ResultSet - */ - public ResultSet quorumGet(PreparedQueryObject query) { - ResultSet results = null; - try { - results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(query); - } catch (MusicServiceException | MusicQueryException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.MAJOR, ErrorTypes.GENERALSERVICEERROR); - - } - return results; - - } - - - - /** - * - * @param lockName - * @return - */ - public String whoseTurnIsIt(String lockName) { - - try { - return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + ""; - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - return null; - - - } - - /** - * - * @param lockId - * @return - */ - public static String getLockNameFromId(String lockId) { - StringTokenizer st = new StringTokenizer(lockId); - return st.nextToken("$"); - } - - public void destroyLockRef(String lockId) { - long start = System.currentTimeMillis(); - try { - getLockingServiceHandle().unlockAndDeleteId(lockId); - } catch (MusicLockingException | NoNodeException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DESTROYLOCK+lockId ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to destroy lock reference:" + (end - start) + " ms"); - } - - public MusicLockState releaseLock(String lockId, boolean voluntaryRelease) { - long start = System.currentTimeMillis(); - try { - getLockingServiceHandle().unlockAndDeleteId(lockId); - } catch (MusicLockingException e1) { - logger.error(EELFLoggerDelegate.errorLogger,e1.getMessage(), AppMessages.RELEASELOCK+lockId ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } catch (KeeperException.NoNodeException nne) { - logger.error(EELFLoggerDelegate.errorLogger,"Failed to release Lock " + lockId + " " + nne); - MusicLockState mls = new MusicLockState("Lock doesn't exists. Release lock operation failed."); - return mls; - } - String lockName = getLockNameFromId(lockId); - MusicLockState mls; - String lockHolder = null; - if (voluntaryRelease) { - mls = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder); - logger.info(EELFLoggerDelegate.applicationLogger,"In unlock: lock voluntarily released for " + lockId); - } else { - boolean needToSyncQuorum = true; - mls = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder, - needToSyncQuorum); - logger.info(EELFLoggerDelegate.applicationLogger,"In unlock: lock forcibly released for " + lockId); - } - try { - getLockingServiceHandle().setLockState(lockName, mls); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.RELEASELOCK+lockId ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to release lock:" + (end - start) + " ms"); - return mls; - } - - public static void voluntaryReleaseLock(String lockId) throws MusicLockingException{ - try { - getLockingServiceHandle().unlockAndDeleteId(lockId); - } catch (KeeperException.NoNodeException e) { - // ??? No way - } - } - - /** - * - * @param lockName - * @throws MusicLockingException - */ - public void deleteLock(String lockName) throws MusicLockingException { - long start = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Deleting lock for " + lockName); - try { - getLockingServiceHandle().deleteLock("/" + lockName); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DELTELOCK+lockName ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - throw new MusicLockingException(e.getMessage()); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to delete lock:" + (end - start) + " ms"); - } - - - /** - * - * @param nodeName - */ - public static void pureZkCreate(String nodeName) { - try { - getLockingServiceHandle().getzkLockHandle().createNode(nodeName); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - } - - /** - * - * @param nodeName - * @param data - */ - public static void pureZkWrite(String nodeName, byte[] data) { - long start = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Performing zookeeper write to " + nodeName); - try { - getLockingServiceHandle().getzkLockHandle().setNodeData(nodeName, data); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - logger.info(EELFLoggerDelegate.applicationLogger,"Performed zookeeper write to " + nodeName); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the actual zk put:" + (end - start) + " ms"); - } - - /** - * - * @param nodeName - * @return - */ - public static byte[] pureZkRead(String nodeName) { - long start = System.currentTimeMillis(); - byte[] data = null; - try { - data = getLockingServiceHandle().getzkLockHandle().getNodeData(nodeName); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - } - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the actual zk put:" + (end - start) + " ms"); - return data; - } - - - - // Prepared Query Additions. - - /** - * - * @param keyspaceName - * @param tableName - * @param primaryKey - * @param queryObject - * @return ReturnType - * @throws MusicServiceException - */ - public ReturnType eventualPut(PreparedQueryObject queryObject) { - boolean result = false; - try { - result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, MusicUtil.EVENTUAL); - } catch (MusicServiceException | MusicQueryException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), "[ERR512E] Failed to get ZK Lock Handle " ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() + " " + ex.getCause() + " " + ex); - return new ReturnType(ResultType.FAILURE, ex.getMessage()); - } - if (result) { - return new ReturnType(ResultType.SUCCESS, "Eventual Operation Successfully performed"); - } else { - return new ReturnType(ResultType.FAILURE, "Eventual Operation failed to perform"); - } - } - - /** - * - * @param keyspaceName - * @param tableName - * @param primaryKey - * @param queryObject - * @param lockId - * @return - */ - public ReturnType criticalPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, String lockId, Condition conditionInfo) { - long start = System.currentTimeMillis(); - - try { - MusicLockState mls = getLockingServiceHandle() - .getLockState(keyspaceName + "." + tableName + "." + primaryKey); - if (mls.getLockHolder().equals(lockId) == true) { - if (conditionInfo != null) - try { - if (conditionInfo.testCondition() == false) - return new ReturnType(ResultType.FAILURE, - "Lock acquired but the condition is not true"); - } catch (Exception e) { - return new ReturnType(ResultType.FAILURE, - "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n" - + e.getMessage()); - } - boolean result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, MusicUtil.CRITICAL); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms"); - if (result) { - return new ReturnType(ResultType.SUCCESS, "Update performed"); - } else { - return new ReturnType(ResultType.FAILURE, "Update failed to perform"); - } - } else - return new ReturnType(ResultType.FAILURE, - "Cannot perform operation since you are the not the lock holder"); - } catch (MusicQueryException | MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - return new ReturnType(ResultType.FAILURE, - "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n" - + e.getMessage()); - }catch(MusicLockingException ex){ - return new ReturnType(ResultType.FAILURE,ex.getMessage()); - } - - } - - /** - * - * @param queryObject - * @param consistency - * @return Boolean Indicates success or failure - * @throws MusicServiceException - * - * - */ - public ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException { - // this is mainly for some functions like keyspace creation etc which does not - // really need the bells and whistles of Music locking. - boolean result = false; - try { - result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, consistency); - } catch (MusicQueryException | MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - throw new MusicServiceException(ex.getMessage()); - } - return result?ResultType.SUCCESS:ResultType.FAILURE; - } - - /** - * This method performs DDL operation on cassandra. - * - * @param queryObject query object containing prepared query and values - * @return ResultSet - * @throws MusicServiceException - */ - public ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException { - ResultSet results = null; - try { - results = MusicDataStoreHandle.getDSHandle().executeOneConsistencyGet(queryObject); - } catch (MusicQueryException | MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - throw new MusicServiceException(e.getMessage()); - } - return results; - } - - public static String getMyHostId() { - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString("SELECT HOST_ID FROM SYSTEM.LOCAL"); - ResultSet rs = null; - try { - rs = MusicDataStoreHandle.getDSHandle().executeOneConsistencyGet(pQuery); - Row row = rs.one(); - return (row == null) ? "UNKNOWN" : row.getUUID("HOST_ID").toString(); - } catch (Exception e) { - e.printStackTrace(); - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - } - logger.error(EELFLoggerDelegate.errorLogger, "Some issue during MusicCore.getMyHostId"); - return "UNKNOW"; - } - - /** - * This method performs DDL operations on cassandra, if the the resource is available. Lock ID - * is used to check if the resource is free. - * - * @param keyspaceName name of the keyspace - * @param tableName name of the table - * @param primaryKey primary key value - * @param queryObject query object containing prepared query and values - * @param lockId lock ID to check if the resource is free to perform the operation. - * @return ResultSet - */ - public ResultSet criticalGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, String lockId) throws MusicServiceException { - ResultSet results = null; - try { - MusicLockState mls = getLockingServiceHandle() - .getLockState(keyspaceName + "." + tableName + "." + primaryKey); - if (mls.getLockHolder().equals(lockId)) { - results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryObject); - } else - throw new MusicServiceException("YOU DO NOT HAVE THE LOCK"); - } catch (MusicQueryException | MusicServiceException | MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - } - return results; - } - - /** - * This method performs DML operation on cassandra, when the lock of the dd is acquired. - * - * @param keyspaceName name of the keyspace - * @param tableName name of the table - * @param primaryKey primary key value - * @param queryObject query object containing prepared query and values - * @return ReturnType - * @throws MusicLockingException - */ - public ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { - - long start = System.currentTimeMillis(); - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long lockCreationTime = System.currentTimeMillis(); - ReturnType lockAcqResult = acquireLock(key, lockId); - long lockAcqTime = System.currentTimeMillis(); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, - queryObject, lockId, conditionInfo); - long criticalPutTime = System.currentTimeMillis(); - voluntaryReleaseLock(lockId); - long lockDeleteTime = System.currentTimeMillis(); - String timingInfo = "|lock creation time:" + (lockCreationTime - start) - + "|lock accquire time:" + (lockAcqTime - lockCreationTime) - + "|critical put time:" + (criticalPutTime - lockAcqTime) - + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|"; - criticalPutResult.setTimingInfo(timingInfo); - return criticalPutResult; - } else { - logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - destroyLockRef(lockId); - return lockAcqResult; - } - } - - /** - * this function is mainly for the benchmarks to see the effect of lock deletion. - * - * @param keyspaceName - * @param tableName - * @param primaryKey - * @param queryObject - * @param conditionInfo - * @return - * @throws MusicLockingException - */ - public ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, - String primaryKey, PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { - - long start = System.currentTimeMillis(); - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long lockCreationTime = System.currentTimeMillis(); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLock(key, lockId); - long lockAcqTime = System.currentTimeMillis(); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, - queryObject, lockId, conditionInfo); - long criticalPutTime = System.currentTimeMillis(); - deleteLock(key); - long lockDeleteTime = System.currentTimeMillis(); - String timingInfo = "|lock creation time:" + (lockCreationTime - start) - + "|lock accquire time:" + (lockAcqTime - lockCreationTime) - + "|critical put time:" + (criticalPutTime - lockAcqTime) - + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|"; - criticalPutResult.setTimingInfo(timingInfo); - return criticalPutResult; - } else { - logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - deleteLock(key); - return lockAcqResult; - } - } - - - - - /** - * This method performs DDL operation on cassasndra, when the lock for the resource is acquired. - * - * @param keyspaceName name of the keyspace - * @param tableName name of the table - * @param primaryKey primary key value - * @param queryObject query object containing prepared query and values - * @return ResultSet - * @throws MusicServiceException - * @throws MusicLockingException - */ - public ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException { - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLock(key, lockId); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - ResultSet result = - criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); - voluntaryReleaseLock(lockId); - return result; - } else { - destroyLockRef(lockId); - logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - return null; - } - } - - public ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException { - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - - ReturnType lockAcqResult = acquireLock(key, lockId); - - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger, "acquired lock with id " + lockId); - ResultSet result = criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); - deleteLock(key); - return result; - } else { - deleteLock(key); - logger.info(EELFLoggerDelegate.applicationLogger, "unable to acquire lock, id " + lockId); - return null; - } - } - - /** - * @param lockName - * @return - */ - public Map<String, Object> validateLock(String lockName) { - return MusicUtil.validateLock(lockName); - } - - @Override - public ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject, - String consistency) throws MusicServiceException { - boolean result = false; - try { - //create shadow locking table - //result = createLockQueue(keyspace, table); - if(result == false) - return ResultType.FAILURE; - - result = false; - - //create table to track unsynced_keys - table = "unsyncedKeys_"+table; - - String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table - + " ( key text,PRIMARY KEY (key) );"; - System.out.println(tabQuery); - PreparedQueryObject queryObject = new PreparedQueryObject(); - - queryObject.appendQueryString(tabQuery); - result = false; - result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, "eventual"); - - - //create actual table - result = MusicDataStoreHandle.getDSHandle().executePut(tableQueryObject, consistency); - } catch (MusicQueryException | MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - throw new MusicServiceException(ex.getMessage()); - } - return result?ResultType.SUCCESS:ResultType.FAILURE; - } - - public static boolean createLockQueue(String keyspace, String table) throws MusicServiceException, MusicQueryException { - logger.info(EELFLoggerDelegate.applicationLogger, - "Create lock queue/table for " + keyspace+"."+table); - table = "lockQ_"+table; - String tabQuery = "CREATE TABLE IF NOT EXISTS "+keyspace+"."+table - + " ( key text, lockReference bigint, createTime text, acquireTime text, guard bigint static, PRIMARY KEY ((key), lockReference) ) " - + "WITH CLUSTERING ORDER BY (lockReference ASC);"; - System.out.println(tabQuery); - PreparedQueryObject queryObject = new PreparedQueryObject(); - - queryObject.appendQueryString(tabQuery); - boolean result; - result = MusicDataStoreHandle.mDstoreHandle.executePut(queryObject, "eventual"); - return result; - } - - - @Override - public List<String> getLockQueue(String fullyQualifiedKey) - throws MusicServiceException, MusicQueryException, MusicLockingException { - // TODO Auto-generated method stub - return null; - } - - - - @Override - public long getLockQueueSize(String fullyQualifiedKey) - throws MusicServiceException, MusicQueryException, MusicLockingException { - // TODO Auto-generated method stub - return 0; - } - @Override - public ReturnType eventualPut_nb(PreparedQueryObject queryObject, String keyspace, String tablename, - String primaryKey) { - return eventualPut(queryObject); - } - - - - -} |