X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fmusic%2Fmain%2FMusicCore.java;h=661d7adcaaee04b31306376e6fef5154ba1cc1c8;hb=5e5d6ed9badebfbc1cc65a90e3de8dad232f9e42;hp=73a6ee55210bbc85c0befd7963edaf7aa53a339e;hpb=ed47d0c5d004b1ce099090100dda6dc1d963782c;p=music.git diff --git a/src/main/java/org/onap/music/main/MusicCore.java b/src/main/java/org/onap/music/main/MusicCore.java index 73a6ee55..661d7adc 100644 --- a/src/main/java/org/onap/music/main/MusicCore.java +++ b/src/main/java/org/onap/music/main/MusicCore.java @@ -22,11 +22,14 @@ package org.onap.music.main; +import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.NoNodeException; import org.onap.music.datastore.MusicDataStore; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonKeySpace; @@ -244,7 +247,7 @@ public class MusicCore { } } - public static ReturnType acquireLock(String key, String lockId) { + public static 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 @@ -255,6 +258,7 @@ public class MusicCore { 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"); @@ -265,6 +269,7 @@ public class MusicCore { } } 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"); @@ -289,6 +294,10 @@ public class MusicCore { logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: You already have the lock!"); return new ReturnType(ResultType.SUCCESS, "You already have the lock!"); } + if (currentMls.getLockStatus() != MusicLockState.LockStatus.UNLOCKED || currentMls.getLockHolder() != null) { + logger.info("In acquire lock: the previous lock has not been released yet! current mls:"+currentMls.toString()); + return new ReturnType(ResultType.FAILURE, "The previous lock has not been released yet."); + } } catch (NullPointerException e) { logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.INVALIDLOCK+lockId,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); } @@ -463,9 +472,9 @@ public class MusicCore { long start = System.currentTimeMillis(); try { getLockingServiceHandle().unlockAndDeleteId(lockId); - } catch (MusicLockingException e) { + } 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"); } @@ -476,6 +485,10 @@ public class MusicCore { 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; @@ -500,20 +513,34 @@ public class MusicCore { } public static void voluntaryReleaseLock(String lockId) throws MusicLockingException{ - getLockingServiceHandle().unlockAndDeleteId(lockId); + try { + getLockingServiceHandle().unlockAndDeleteId(lockId); + String lockName = getLockNameFromId(lockId); + String lockHolder = null; + MusicLockState mls = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder); + try { + getLockingServiceHandle().setLockState(lockName, mls); + } catch (MusicLockingException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.RELEASELOCK+lockId ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); + } + } catch (KeeperException.NoNodeException e) { + // ??? No way + } } /** * * @param lockName + * @throws MusicLockingException */ - public static void deleteLock(String lockName) { + public static 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"); @@ -601,6 +628,7 @@ public class MusicCore { } 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, "Success"); @@ -625,6 +653,7 @@ public class MusicCore { try { MusicLockState mls = getLockingServiceHandle() .getLockState(keyspaceName + "." + tableName + "." + primaryKey); + logger.info("Got MusicLockState object... :"+mls.toString()); if (mls.getLockHolder().equals(lockId) == true) { if (conditionInfo != null) try { @@ -649,6 +678,7 @@ public class MusicCore { "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n" + e.getMessage()); }catch(MusicLockingException ex){ + logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage()); return new ReturnType(ResultType.FAILURE,ex.getMessage()); } @@ -663,7 +693,7 @@ public class MusicCore { * * */ - public static boolean nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException { + public static 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; @@ -673,7 +703,7 @@ public class MusicCore { logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); throw new MusicServiceException(ex.getMessage()); } - return result; + return result?ResultType.SUCCESS:ResultType.FAILURE; } /** @@ -745,7 +775,7 @@ public class MusicCore { ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, queryObject, lockId, conditionInfo); long criticalPutTime = System.currentTimeMillis(); - voluntaryReleaseLock(lockId); + releaseLock(lockId, true); long lockDeleteTime = System.currentTimeMillis(); String timingInfo = "|lock creation time:" + (lockCreationTime - start) + "|lock accquire time:" + (lockAcqTime - lockCreationTime) @@ -755,7 +785,7 @@ public class MusicCore { return criticalPutResult; } else { logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - destroyLockRef(lockId); + releaseLock(lockId, true);; return lockAcqResult; } } @@ -825,10 +855,10 @@ public class MusicCore { logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); ResultSet result = criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); - voluntaryReleaseLock(lockId); + releaseLock(lockId, true); return result; } else { - destroyLockRef(lockId); + releaseLock(lockId, true); logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); return null; } @@ -877,31 +907,33 @@ public class MusicCore { operation); if (!resultMap.isEmpty()) return resultMap; - boolean isAAF = CachingUtil.isAAFApplication(nameSpace); - if (!isAAF && !(operation.equals("createKeySpace"))) { - if(aid == null) { - resultMap.put("Exception", "Aid is mandatory for nonAAF applications "); - return resultMap; - } - if(operation.contains("Lock")) { - resultMap = CachingUtil.authenticateAIDUserLock(aid, nameSpace); - } - else { - resultMap = CachingUtil.authenticateAIDUser(aid, keyspace); - } - - if (!resultMap.isEmpty()) - return resultMap; + String isAAFApp = null; + try { + isAAFApp= CachingUtil.isAAFApplication(nameSpace); + } catch(MusicServiceException e) { + resultMap.put("Exception", e.getMessage()); + return resultMap; + } + if(isAAFApp == null) { + resultMap.put("Exception", "Namespace: "+nameSpace+" doesn't exist. Please make sure ns(appName)" + + " is correct and Application is onboarded."); + return resultMap; } - if (aid == null && (userId == null || password == null)) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.DATAERROR); + boolean isAAF = Boolean.valueOf(isAAFApp); + if (userId == null || password == null) { + logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: " + userId + " :: password: " + password); resultMap.put("Exception", "UserId and Password are mandatory for the operation " + operation); return resultMap; } - + if(!isAAF && !(operation.equals("createKeySpace"))) { + resultMap = CachingUtil.authenticateAIDUser(nameSpace, userId, password, keyspace); + if (!resultMap.isEmpty()) + return resultMap; + + } if (isAAF && nameSpace != null && userId != null && password != null) { boolean isValid = true; try { @@ -939,23 +971,27 @@ public class MusicCore { uuid = CachingUtil.generateUUID(); resultMap.put("uuid", "new"); } - - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspace)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), password)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); - //CachingUtil.updateMusicCache(uuid, keyspace); - MusicCore.eventualPut(pQuery); resultMap.put("aid", uuid); } return resultMap; } + + /** + * @param lockName + * @return + */ + public static Map validateLock(String lockName) { + Map resultMap = new HashMap<>(); + String[] locks = lockName.split("\\."); + if(locks.length < 3) { + resultMap.put("Exception", "Invalid lock. Please make sure lock is of the type keyspaceName.tableName.primaryKey"); + return resultMap; + } + String keyspace= locks[0]; + if(keyspace.startsWith("$")) + keyspace = keyspace.substring(1); + resultMap.put("keyspace",keyspace); + return resultMap; + } }