diff options
author | srupane kondreddy <srupane.kondreddy@att.com> | 2018-11-13 15:27:24 -0500 |
---|---|---|
committer | srupane kondreddy <sk5300@research.att.com> | 2018-11-13 15:48:52 -0500 |
commit | 7addd52dc73e6571028ca7f6a018e75ec1c9b0ca (patch) | |
tree | 85250211fb6e3d67a849edffd40535992ec1b78c /src/main/java/org/onap/music/datastore | |
parent | ac3cfd1cd5f18d72948b70db39cc7db32a392133 (diff) |
music core implemented as a service
Change-Id: I4075d0efb03bf2a153976354947be1329660c1f1
Issue-ID: MUSIC-148
Signed-off-by: srupane kondreddy <sk5300@research.att.com>
Diffstat (limited to 'src/main/java/org/onap/music/datastore')
-rw-r--r-- | src/main/java/org/onap/music/datastore/CassaLockStore.java | 173 | ||||
-rw-r--r-- | src/main/java/org/onap/music/datastore/Condition.java | 53 | ||||
-rw-r--r-- | src/main/java/org/onap/music/datastore/MusicDataStore.java (renamed from src/main/java/org/onap/music/datastore/CassaDataStore.java) | 10 | ||||
-rw-r--r-- | src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java | 115 | ||||
-rw-r--r-- | src/main/java/org/onap/music/datastore/MusicLockState.java | 41 |
5 files changed, 173 insertions, 219 deletions
diff --git a/src/main/java/org/onap/music/datastore/CassaLockStore.java b/src/main/java/org/onap/music/datastore/CassaLockStore.java deleted file mode 100644 index e03a1c07..00000000 --- a/src/main/java/org/onap/music/datastore/CassaLockStore.java +++ /dev/null @@ -1,173 +0,0 @@ -package org.onap.music.datastore; - -import java.util.UUID; - -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.exceptions.MusicQueryException; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.MusicUtil; - -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.utils.UUIDs; - -/* - * This is the lock store that is built on top of Cassandra that is used by MUSIC to maintain lock state. - */ - -public class CassaLockStore { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CassaLockStore.class); - - public class LockObject{ - public String lockRef; - public String createTime; - public String acquireTime; - public LockObject(String lockRef, String createTime, String acquireTime) { - this.lockRef = lockRef; - this.acquireTime = acquireTime; - this.createTime = createTime; - - } - } - CassaDataStore dsHandle; - public CassaLockStore() { - dsHandle = new CassaDataStore(); - } - - public CassaLockStore(CassaDataStore dsHandle) { - this.dsHandle=dsHandle; - } - - - /** - * - * This method creates a shadow locking table for every main table in Cassandra. This table tracks all information regarding locks. - * @param keyspace of the application. - * @param table of the application. - * @return true if the operation was successful. - * @throws MusicServiceException - * @throws MusicQueryException - */ - public 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 = dsHandle.executePut(queryObject, "eventual"); - return result; - } - - /** - * This method creates a lock reference for each invocation. The lock references are monotonically increasing timestamps. - * @param keyspace of the locks. - * @param table of the locks. - * @param lockName is the primary key of the lock table - * @return the UUID lock reference. - * @throws MusicServiceException - * @throws MusicQueryException - */ - public String genLockRefandEnQueue(String keyspace, String table, String lockName) throws MusicServiceException, MusicQueryException { - logger.info(EELFLoggerDelegate.applicationLogger, - "Create lock reference for " + keyspace + "." + table + "." + lockName); - table = "lockQ_" + table; - long lockEpochMillis = System.currentTimeMillis(); - long lockRef = lockEpochMillis; - - logger.info(EELFLoggerDelegate.applicationLogger, - "Created lock reference for " + keyspace + "." + table + "." + lockName + ":" + lockRef); - - PreparedQueryObject queryObject = new PreparedQueryObject(); - String defaultQuery = " UPDATE " + keyspace + "." + table + " SET guard=-1 WHERE key=? IF guard = NULL;"; - - queryObject.addValue(lockName); - queryObject.appendQueryString(defaultQuery); - boolean dqResult = dsHandle.executePut(queryObject, "critical"); -// System.out.println("dqResult: " + dqResult); - - - queryObject = new PreparedQueryObject(); - String insQuery = "BEGIN BATCH" + - " UPDATE " + keyspace + "." + table + " SET guard=? WHERE key=? IF guard < ?;" + - " INSERT INTO " + keyspace + "." + table + - "(key, lockReference, createTime, acquireTime) VALUES (?,?,?,?) IF NOT EXISTS; APPLY BATCH;"; - - queryObject.addValue(lockRef); - queryObject.addValue(lockName); - queryObject.addValue(lockRef); - - queryObject.addValue(lockName); - queryObject.addValue(lockRef); - queryObject.addValue(String.valueOf(lockEpochMillis)); - queryObject.addValue("0"); - queryObject.appendQueryString(insQuery); - boolean pResult = dsHandle.executePut(queryObject, "critical"); -// System.out.println("pResult: " + pResult); - return String.valueOf(lockRef); - } - - - /** - * This method returns the top of lock table/queue for the key. - * @param keyspace of the application. - * @param table of the application. - * @param key is the primary key of the application table - * @return the UUID lock reference. - * @throws MusicServiceException - * @throws MusicQueryException - */ - public LockObject peekLockQueue(String keyspace, String table, String key) throws MusicServiceException, MusicQueryException{ - logger.info(EELFLoggerDelegate.applicationLogger, - "Peek in lock table for " + keyspace+"."+table+"."+key); - table = "lockQ_"+table; - String selectQuery = "select * from "+keyspace+"."+table+" where key='"+key+"' LIMIT 1;"; - PreparedQueryObject queryObject = new PreparedQueryObject(); - queryObject.appendQueryString(selectQuery); - ResultSet results = dsHandle.executeEventualGet(queryObject); - Row row = results.one(); - String lockReference = "" + row.getLong("lockReference"); - String createTime = row.getString("createTime"); - String acquireTime = row.getString("acquireTime"); - - return new LockObject(lockReference, createTime,acquireTime); - } - - - /** - * This method removes the lock ref from the lock table/queue for the key. - * @param keyspace of the application. - * @param table of the application. - * @param key is the primary key of the application table - * @param lockReference the lock reference that needs to be dequeued. - * @throws MusicServiceException - * @throws MusicQueryException - */ - public void deQueueLockRef(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ - table = "lockQ_"+table; - PreparedQueryObject queryObject = new PreparedQueryObject(); - Long lockReferenceL = Long.parseLong(lockReference); - String deleteQuery = "delete from "+keyspace+"."+table+" where key='"+key+"' AND lockReference ="+lockReferenceL+" IF EXISTS;"; - queryObject.appendQueryString(deleteQuery); - dsHandle.executePut(queryObject, "critical"); - } - - - public void updateLockAcquireTime(String keyspace, String table, String key, String lockReference) throws MusicServiceException, MusicQueryException{ - table = "lockQ_"+table; - PreparedQueryObject queryObject = new PreparedQueryObject(); - Long lockReferenceL = Long.parseLong(lockReference); - String updateQuery = "update "+keyspace+"."+table+" set acquireTime='"+ System.currentTimeMillis()+"' where key='"+key+"' AND lockReference = "+lockReferenceL+" IF EXISTS;"; - queryObject.appendQueryString(updateQuery); - dsHandle.executePut(queryObject, "eventual"); - - } - - -} diff --git a/src/main/java/org/onap/music/datastore/Condition.java b/src/main/java/org/onap/music/datastore/Condition.java new file mode 100644 index 00000000..f08a8754 --- /dev/null +++ b/src/main/java/org/onap/music/datastore/Condition.java @@ -0,0 +1,53 @@ +/* + * ============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.datastore; + +import java.util.Map; + +import org.onap.music.eelf.logging.EELFLoggerDelegate; +import org.onap.music.service.MusicCoreService; +import org.onap.music.service.impl.MusicCassaCore; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; + + + + public class Condition { + Map<String, Object> conditions; + PreparedQueryObject selectQueryForTheRow; + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(Condition.class); + private static MusicCoreService musicCore = MusicCassaCore.getInstance(); + + public Condition(Map<String, Object> conditions, PreparedQueryObject selectQueryForTheRow) { + this.conditions = conditions; + this.selectQueryForTheRow = selectQueryForTheRow; + } + + public boolean testCondition() throws Exception { + // first generate the row + ResultSet results = musicCore.quorumGet(selectQueryForTheRow); + Row row = results.one(); + return MusicDataStoreHandle.getDSHandle().doesRowSatisfyCondition(row, conditions); + } + } + diff --git a/src/main/java/org/onap/music/datastore/CassaDataStore.java b/src/main/java/org/onap/music/datastore/MusicDataStore.java index 14934f6e..725cefe6 100644 --- a/src/main/java/org/onap/music/datastore/CassaDataStore.java +++ b/src/main/java/org/onap/music/datastore/MusicDataStore.java @@ -89,7 +89,7 @@ import com.sun.jersey.core.util.Base64; * @author bharathb * */ -public class CassaDataStore { +public class MusicDataStore { private Session session; private Cluster cluster; @@ -119,12 +119,12 @@ public class CassaDataStore { - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CassaDataStore.class); + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStore.class); /** * */ - public CassaDataStore() { + public MusicDataStore() { connectToCassaCluster(); } @@ -133,7 +133,7 @@ public class CassaDataStore { * @param cluster * @param session */ - public CassaDataStore(Cluster cluster, Session session) { + public MusicDataStore(Cluster cluster, Session session) { this.session = session; this.cluster = cluster; } @@ -143,7 +143,7 @@ public class CassaDataStore { * @param remoteIp * @throws MusicServiceException */ - public CassaDataStore(String remoteIp) { + public MusicDataStore(String remoteIp) { try { connectToCassaCluster(remoteIp); } catch (MusicServiceException e) { diff --git a/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java new file mode 100644 index 00000000..a2d9386b --- /dev/null +++ b/src/main/java/org/onap/music/datastore/MusicDataStoreHandle.java @@ -0,0 +1,115 @@ +/* + * ============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.datastore; + +import java.util.HashMap; +import java.util.Map; + +import org.onap.music.eelf.logging.EELFLoggerDelegate; +import org.onap.music.exceptions.MusicServiceException; +import org.onap.music.main.MusicUtil; +import org.onap.music.service.impl.MusicCassaCore; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.TableMetadata; + +public class MusicDataStoreHandle { + + public static MusicDataStore mDstoreHandle = null; + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStoreHandle.class); + + /** + * + * @param remoteIp + * @return + */ + public static MusicDataStore getDSHandle(String remoteIp) { + logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle"); + long start = System.currentTimeMillis(); + if (mDstoreHandle == null) { + try { + MusicUtil.loadProperties(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "No properties file defined. Falling back to default."); + } + mDstoreHandle = new MusicDataStore(remoteIp); + } + long end = System.currentTimeMillis(); + logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms"); + return mDstoreHandle; + } + + /** + * + * @return + * @throws MusicServiceException + */ + public static MusicDataStore getDSHandle() throws MusicServiceException { + + logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle"); + long start = System.currentTimeMillis(); + if (mDstoreHandle == null) { + try { + MusicUtil.loadProperties(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "No properties file defined. Falling back to default."); + } + // Quick Fix - Best to put this into every call to getDSHandle? + if (! MusicUtil.getMyCassaHost().equals("localhost") ) { + mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost()); + } else { + mDstoreHandle = new MusicDataStore(); + } + } + if(mDstoreHandle.getSession() == null) { + String message = "Connection to Cassandra has not been enstablished." + + " Please check connection properites and reboot."; + logger.info(EELFLoggerDelegate.applicationLogger, message); + throw new MusicServiceException(message); + } + long end = System.currentTimeMillis(); + logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms"); + return mDstoreHandle; + } + + /** + * + * @param keyspace + * @param tablename + * @return + * @throws MusicServiceException + */ + public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException { + return MusicDataStoreHandle.getDSHandle().returnColumnMetadata(keyspace, tablename); + } + + /** + * + * @param results + * @return + * @throws MusicServiceException + */ + public static Map<String, HashMap<String, Object>> marshallResults(ResultSet results) throws MusicServiceException { + return MusicDataStoreHandle.getDSHandle().marshalData(results); + } + +} diff --git a/src/main/java/org/onap/music/datastore/MusicLockState.java b/src/main/java/org/onap/music/datastore/MusicLockState.java deleted file mode 100644 index 60a15b13..00000000 --- a/src/main/java/org/onap/music/datastore/MusicLockState.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.onap.music.datastore; -public class MusicLockState { - public enum LockStatus { - UNLOCKED, BEING_LOCKED, LOCKED - };// captures the state of the lock - - private LockStatus lockStatus; - private String lockHolder; - private String errorMessage = null; - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - - public MusicLockState(LockStatus lockStatus, String lockHolder) { - super(); - this.lockStatus = lockStatus; - this.lockHolder = lockHolder; - } - - public LockStatus getLockStatus() { - return lockStatus; - } - - public void setLockStatus(LockStatus lockStatus) { - this.lockStatus = lockStatus; - } - - public String getLockHolder() { - return lockHolder; - } - - public void setLockHolder(String lockHolder) { - this.lockHolder = lockHolder; - } - -} |