2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * Modifications Copyright (c) 2018 IBM.
7 * ===================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ===================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
22 * ============LICENSE_END=============================================
23 * ====================================================================
26 package org.onap.music.service.impl;
28 import java.io.StringWriter;
29 import java.util.List;
31 import java.util.StringTokenizer;
33 import javax.ws.rs.core.MultivaluedMap;
35 import org.onap.music.datastore.Condition;
36 import org.onap.music.datastore.MusicDataStore;
37 import org.onap.music.datastore.MusicDataStoreHandle;
38 import org.onap.music.datastore.PreparedQueryObject;
39 import org.onap.music.datastore.jsonobjects.JsonDelete;
40 import org.onap.music.datastore.jsonobjects.JsonIndex;
41 import org.onap.music.datastore.jsonobjects.JsonInsert;
42 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
43 import org.onap.music.datastore.jsonobjects.JsonSelect;
44 import org.onap.music.datastore.jsonobjects.JsonTable;
45 import org.onap.music.datastore.jsonobjects.JsonUpdate;
46 import org.onap.music.eelf.logging.EELFLoggerDelegate;
47 import org.onap.music.eelf.logging.format.AppMessages;
48 import org.onap.music.eelf.logging.format.ErrorSeverity;
49 import org.onap.music.eelf.logging.format.ErrorTypes;
50 import org.onap.music.exceptions.MusicDeadlockException;
51 import org.onap.music.exceptions.MusicLockingException;
52 import org.onap.music.exceptions.MusicQueryException;
53 import org.onap.music.exceptions.MusicServiceException;
54 import org.onap.music.lockingservice.cassandra.CassaLockStore;
55 import org.onap.music.lockingservice.cassandra.CassaLockStore.LockObject;
56 import org.onap.music.lockingservice.cassandra.LockType;
57 import org.onap.music.lockingservice.cassandra.MusicLockState;
58 import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus;
59 import org.onap.music.main.MusicUtil;
60 import org.onap.music.main.ResultType;
61 import org.onap.music.main.ReturnType;
62 import org.onap.music.service.MusicCoreService;
64 import com.datastax.driver.core.DataType;
65 import com.datastax.driver.core.ResultSet;
66 import com.datastax.driver.core.Row;
67 import com.datastax.driver.core.TableMetadata;
69 public class MusicCassaCore implements MusicCoreService {
71 private static CassaLockStore mLockHandle = null;
72 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicCassaCore.class);
73 private static MusicCassaCore musicCassaCoreInstance = null;
75 private MusicCassaCore() {
76 // not going to happen
79 public static CassaLockStore getmLockHandle() {
83 public static void setmLockHandle(CassaLockStore mLockHandle) {
84 MusicCassaCore.mLockHandle = mLockHandle;
87 public static MusicCassaCore getInstance() {
89 if(musicCassaCoreInstance == null) {
90 musicCassaCoreInstance = new MusicCassaCore();
92 return musicCassaCoreInstance;
98 public static CassaLockStore getLockingServiceHandle() throws MusicLockingException {
99 logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring lock store handle");
100 long start = System.currentTimeMillis();
102 if (mLockHandle == null) {
104 mLockHandle = new CassaLockStore(MusicDataStoreHandle.getDSHandle());
105 } catch (Exception e) {
106 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKHANDLE,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
107 throw new MusicLockingException("Failed to aquire Locl store handle " + e);
110 long end = System.currentTimeMillis();
111 logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire lock store handle:" + (end - start) + " ms");
115 public String createLockReference(String fullyQualifiedKey) throws MusicLockingException {
116 return createLockReference(fullyQualifiedKey, LockType.WRITE);
119 public String createLockReference(String fullyQualifiedKey, LockType locktype) throws MusicLockingException {
120 return createLockReference(fullyQualifiedKey, locktype, null);
123 public String createLockReference(String fullyQualifiedKey, LockType locktype, String owner) throws MusicLockingException {
124 String[] splitString = fullyQualifiedKey.split("\\.");
125 String keyspace = splitString[0];
126 String table = splitString[1];
127 String lockName = splitString[2];
129 logger.info(EELFLoggerDelegate.applicationLogger,"Creating lock reference for lock name:" + lockName);
130 long start = System.currentTimeMillis();
131 String lockReference = null;
134 boolean deadlock = getLockingServiceHandle().checkForDeadlock(keyspace, table, lockName, locktype, owner, false);
136 MusicDeadlockException e = new MusicDeadlockException("Deadlock detected when " + owner + " tried to create lock on " + keyspace + "." + table + "." + lockName);
137 e.setValues(owner, keyspace, table, lockName);
140 } catch (MusicDeadlockException e) {
141 //just threw this, no need to wrap it
143 } catch (MusicServiceException | MusicQueryException e) {
144 logger.error(EELFLoggerDelegate.applicationLogger, e);
145 throw new MusicLockingException("Unable to check for deadlock. " + e.getMessage(), e);
149 lockReference = "" + getLockingServiceHandle().genLockRefandEnQueue(keyspace, table, lockName, locktype, owner);
150 } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
151 logger.error(EELFLoggerDelegate.applicationLogger, e);
152 throw new MusicLockingException("Unable to create lock reference. " + e.getMessage(), e);
153 } catch (Exception e) {
154 logger.error(EELFLoggerDelegate.applicationLogger, e);
155 throw new MusicLockingException("Unable to create lock reference. " + e.getMessage(), e);
157 long end = System.currentTimeMillis();
158 logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to create lock reference:" + (end - start) + " ms");
159 return lockReference;
162 public ReturnType promoteLock(String lockId) throws MusicLockingException {
163 String[] splitString = lockId.split("\\.");
164 String keyspace = splitString[0].substring(1);//remove '$'
165 String table = splitString[1];
166 String primaryKeyValue = splitString[2].substring(0, splitString[2].lastIndexOf("$"));
167 String localFullyQualifiedKey = lockId.substring(1, lockId.lastIndexOf("$"));
168 String lockRef = lockId.substring(lockId.lastIndexOf("$")+1); //lockRef is "$" to end
170 logger.info(EELFLoggerDelegate.applicationLogger,"Attempting to promote lock " + lockId);
173 return getLockingServiceHandle().promoteLock(keyspace, table, primaryKeyValue, lockRef);
174 } catch (MusicServiceException e) {
175 throw new MusicLockingException("Unable to promote lock. ", e);
176 } catch (MusicQueryException e) {
177 throw new MusicLockingException("Unable to promote lock. ", e);
183 public ReturnType acquireLockWithLease(String fullyQualifiedKey, String lockReference, long leasePeriod)
184 throws MusicLockingException, MusicQueryException, MusicServiceException {
185 evictExpiredLockHolder(fullyQualifiedKey,leasePeriod);
186 return acquireLock(fullyQualifiedKey, lockReference);
189 private void evictExpiredLockHolder(String fullyQualifiedKey, long leasePeriod)
190 throws MusicLockingException, MusicQueryException, MusicServiceException {
191 String[] splitString = fullyQualifiedKey.split("\\.");
192 String keyspace = splitString[0];
193 String table = splitString[1];
194 String primaryKeyValue = splitString[2];
196 LockObject currentLockHolderObject = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue);
198 if (!currentLockHolderObject.getIsLockOwner()) { // no lock holder
202 * Release the lock of the previous holder if it has expired. if the update to the acquire time has
203 * not reached due to network delays, simply use the create time as the reference
205 long referenceTime = Math.max(Long.parseLong(currentLockHolderObject.getAcquireTime()),
206 Long.parseLong(currentLockHolderObject.getCreateTime()));
207 if ((System.currentTimeMillis() - referenceTime) > leasePeriod) {
208 forciblyReleaseLock(fullyQualifiedKey, currentLockHolderObject.getLockRef() + "");
209 logger.info(EELFLoggerDelegate.applicationLogger, currentLockHolderObject.getLockRef() + " forcibly released");
213 public ReturnType acquireLock(String fullyQualifiedKey, String lockId)
214 throws MusicLockingException, MusicQueryException, MusicServiceException {
215 String[] splitString = lockId.split("\\.");
216 String keyspace = splitString[0].substring(1);//remove '$'
217 String table = splitString[1];
218 String primaryKeyValue = splitString[2].substring(0, splitString[2].lastIndexOf("$"));
219 String localFullyQualifiedKey = lockId.substring(1, lockId.lastIndexOf("$"));
220 String lockRef = lockId.substring(lockId.lastIndexOf("$")+1); //lockRef is "$" to end
222 LockObject lockInfo = getLockingServiceHandle().getLockInfo(keyspace, table, primaryKeyValue, lockRef);
224 if (!lockInfo.getIsLockOwner()) {
225 return new ReturnType(ResultType.FAILURE, lockId + " is not a lock holder");//not top of the lock store q
228 if (getLockingServiceHandle().checkForDeadlock(keyspace, table, primaryKeyValue, lockInfo.getLocktype(), lockInfo.getOwner(), true)) {
229 MusicDeadlockException e = new MusicDeadlockException("Deadlock detected when " + lockInfo.getOwner() + " tried to create lock on " + keyspace + "." + table + "." + primaryKeyValue);
230 e.setValues(lockInfo.getOwner(), keyspace, table, primaryKeyValue);
234 //check to see if the value of the key has to be synced in case there was a forceful release
235 String syncTable = keyspace+".unsyncedKeys_"+table;
236 String query = "select * from "+syncTable+" where key='"+localFullyQualifiedKey+"';";
237 PreparedQueryObject readQueryObject = new PreparedQueryObject();
238 readQueryObject.appendQueryString(query);
239 ResultSet results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(readQueryObject);
240 if (!results.all().isEmpty()) {
241 logger.info("In acquire lock: Since there was a forcible release, need to sync quorum!");
243 syncQuorum(keyspace, table, primaryKeyValue);
244 } catch (Exception e) {
245 StringWriter sw = new StringWriter();
246 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), "[ERR506E] Failed to aquire lock ",
247 ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR, e);
248 String exceptionAsString = sw.toString();
249 return new ReturnType(ResultType.FAILURE, "Exception thrown while syncing key:\n" + exceptionAsString);
251 String cleanQuery = "delete from " + syncTable + " where key='"+localFullyQualifiedKey+"';";
252 PreparedQueryObject deleteQueryObject = new PreparedQueryObject();
253 deleteQueryObject.appendQueryString(cleanQuery);
254 MusicDataStoreHandle.getDSHandle().executePut(deleteQueryObject, "critical");
257 getLockingServiceHandle().updateLockAcquireTime(keyspace, table, primaryKeyValue, lockRef);
259 return new ReturnType(ResultType.SUCCESS, lockRef+" is the lock holder for the key");
266 * @param tableQueryObject
268 * @return Boolean Indicates success or failure
269 * @throws MusicServiceException
273 public ResultType createTable(String keyspace, String table, PreparedQueryObject tableQueryObject,
274 String consistency) throws MusicServiceException {
275 boolean result = false;
278 // create shadow locking table
279 result = getLockingServiceHandle().createLockQueue(keyspace, table);
281 return ResultType.FAILURE;
285 // create table to track unsynced_keys
286 table = "unsyncedKeys_" + table;
289 "CREATE TABLE IF NOT EXISTS " + keyspace + "." + table + " ( key text,PRIMARY KEY (key) );";
290 PreparedQueryObject queryObject = new PreparedQueryObject();
292 queryObject.appendQueryString(tabQuery);
294 result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, "eventual");
296 // create actual table
297 result = MusicDataStoreHandle.getDSHandle().executePut(tableQueryObject, consistency);
298 } catch (MusicQueryException | MusicServiceException | MusicLockingException ex) {
299 logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(), AppMessages.UNKNOWNERROR, ErrorSeverity.WARN,
300 ErrorTypes.MUSICSERVICEERROR);
301 throw new MusicServiceException(ex.getMessage());
303 return result ? ResultType.SUCCESS : ResultType.FAILURE;
306 private static void syncQuorum(String keyspace, String table, String primaryKeyValue) throws Exception {
307 logger.info(EELFLoggerDelegate.applicationLogger,"Performing sync operation---");
308 PreparedQueryObject selectQuery = new PreparedQueryObject();
309 PreparedQueryObject updateQuery = new PreparedQueryObject();
311 // get the primary key d
312 TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, table);
313 String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName(); // we only support single
315 DataType primaryKeyType = tableInfo.getPrimaryKey().get(0).getType();
316 Object cqlFormattedPrimaryKeyValue =
317 MusicUtil.convertToActualDataType(primaryKeyType, primaryKeyValue);
319 // get the row of data from a quorum
320 selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + table + " WHERE "
321 + primaryKeyName + "= ?" + ";");
322 selectQuery.addValue(cqlFormattedPrimaryKeyValue);
323 MusicUtil.writeBackToQuorum(selectQuery, primaryKeyName, updateQuery, keyspace, table,
324 cqlFormattedPrimaryKeyValue);
332 public ResultSet quorumGet(PreparedQueryObject query) {
333 ResultSet results = null;
335 results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(query);
336 } catch (MusicServiceException | MusicQueryException e) {
337 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,
338 ErrorSeverity.MAJOR, ErrorTypes.GENERALSERVICEERROR, e);
344 public String whoseTurnIsIt(String fullyQualifiedKey) {
345 String[] splitString = fullyQualifiedKey.split("\\.");
346 String keyspace = splitString[0];
347 String table = splitString[1];
348 String primaryKeyValue = splitString[2];
350 LockObject lockOwner = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue);
351 if (!lockOwner.getIsLockOwner()) {
354 return "$" + fullyQualifiedKey + "$" + lockOwner.getLockRef();
355 } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
356 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.LOCKINGERROR + fullyQualifiedKey,
357 ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
362 public List<String> getCurrentLockHolders(String fullyQualifiedKey) {
363 String[] splitString = fullyQualifiedKey.split("\\.");
364 String keyspace = splitString[0];
365 String table = splitString[1];
366 String primaryKeyValue = splitString[2];
368 return getLockingServiceHandle().getCurrentLockHolders(keyspace, table, primaryKeyValue);
369 } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
370 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.LOCKINGERROR+fullyQualifiedKey ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
377 * @param lockReference
380 public static String getLockNameFromId(String lockReference) {
381 StringTokenizer st = new StringTokenizer(lockReference);
382 return st.nextToken("$");
386 public void destroyLockRef(String lockId) throws MusicLockingException {
387 long start = System.currentTimeMillis();
388 String fullyQualifiedKey = lockId.substring(1, lockId.lastIndexOf("$"));
389 String lockRef = lockId.substring(lockId.lastIndexOf('$')+1);
390 String[] splitString = fullyQualifiedKey.split("\\.");
391 String keyspace = splitString[0];
392 String table = splitString[1];
393 String primaryKeyValue = splitString[2];
395 getLockingServiceHandle().deQueueLockRef(keyspace, table, primaryKeyValue, lockRef,MusicUtil.getRetryCount());
396 } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
397 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DESTROYLOCK+lockRef,
398 ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR, e);
399 throw new MusicLockingException(e.getMessage());
401 long end = System.currentTimeMillis();
402 logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to destroy lock reference:" + (end - start) + " ms");
405 public MusicLockState destroyLockRef(String fullyQualifiedKey, String lockReference) throws MusicLockingException {
406 long start = System.currentTimeMillis();
407 String[] splitString = fullyQualifiedKey.split("\\.");
408 String keyspace = splitString[0];
409 String table = splitString[1];
410 String primaryKeyValue = splitString[2];
412 getLockingServiceHandle().deQueueLockRef(keyspace, table, primaryKeyValue, lockReference,MusicUtil.getRetryCount());
413 } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
414 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DESTROYLOCK + lockReference,
415 ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR,e);
416 throw new MusicLockingException(e.getMessage());
418 long end = System.currentTimeMillis();
419 logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to destroy lock reference:" + (end - start) + " ms");
420 return new MusicLockState(LockStatus.UNLOCKED, "");
424 public MusicLockState releaseLock(String lockId, boolean voluntaryRelease) throws MusicLockingException {
425 String fullyQualifiedKey = lockId.substring(1, lockId.lastIndexOf("$"));
426 String lockRef = lockId.substring(lockId.lastIndexOf('$')+1);
427 if (voluntaryRelease) {
428 return voluntaryReleaseLock(fullyQualifiedKey, lockRef);
430 return forciblyReleaseLock(fullyQualifiedKey, lockRef);
434 public MusicLockState voluntaryReleaseLock(String fullyQualifiedKey, String lockReference)
435 throws MusicLockingException {
436 MusicLockState result = null;
438 result = destroyLockRef(fullyQualifiedKey, lockReference);
439 } catch (Exception ex) {
440 logger.info(EELFLoggerDelegate.applicationLogger,
441 "Exception in voluntaryReleaseLock() for " + fullyQualifiedKey + "ref: " + lockReference);
442 throw new MusicLockingException(ex.getMessage());
447 public MusicLockState forciblyReleaseLock(String fullyQualifiedKey, String lockReference) throws MusicLockingException {
448 String[] splitString = fullyQualifiedKey.split("\\.");
449 String keyspace = splitString[0];
450 String table = splitString[1];
452 //leave a signal that this key could potentially be unsynchronized
453 String syncTable = keyspace+".unsyncedKeys_"+table;
454 PreparedQueryObject queryObject = new PreparedQueryObject();
455 String values = "(?)";
456 queryObject.addValue(fullyQualifiedKey);
457 String insQuery = "insert into "+syncTable+" (key) values "+values+";";
458 queryObject.appendQueryString(insQuery);
460 MusicDataStoreHandle.getDSHandle().executePut(queryObject, "critical");
461 } catch (Exception e) {
462 logger.error("Cannot forcibly release lock: " + fullyQualifiedKey + " " + lockReference + ". "
463 + e.getMessage(), e);
466 //now release the lock
467 return destroyLockRef(fullyQualifiedKey, lockReference);
471 public List<String> releaseAllLocksForOwner(String ownerId, String keyspace, String table) throws MusicLockingException, MusicServiceException, MusicQueryException {
472 // System.out.println("IN RELEASEALLLOCKSFOROWNER, ");
474 List<String> lockIds = getLockingServiceHandle().getAllLocksForOwner(ownerId, keyspace, table);
475 for (String lockId : lockIds) {
476 // System.out.println(" LOCKID = " + lockId);
477 //return "$" + keyspace + "." + table + "." + lockName + "$" + String.valueOf(lockRef);
478 releaseLock("$" + keyspace + "." + table + "." + lockId, true);
486 * @throws MusicLockingException
489 public void deleteLock(String lockName) throws MusicLockingException {
490 throw new MusicLockingException("Depreciated Method Delete Lock");
493 // Prepared Query Additions.
499 * @throws MusicServiceException
501 public ReturnType eventualPut(PreparedQueryObject queryObject) {
502 boolean result = false;
504 result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, MusicUtil.EVENTUAL);
505 } catch (MusicServiceException | MusicQueryException ex) {
506 logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), "[ERR512E] Failed to get Lock Handle " ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
507 logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() + " " + ex.getCause() + " " + ex);
508 return new ReturnType(ResultType.FAILURE, ex.getMessage());
511 return new ReturnType(ResultType.SUCCESS, "Eventual Operation Successfully performed");
513 return new ReturnType(ResultType.FAILURE, "Eventual Operation failed to perform");
521 * @throws MusicServiceException
523 public ReturnType eventualPut_nb(PreparedQueryObject queryObject,String keyspace,String tablename,String primaryKey) {
524 boolean result = false;
526 PreparedQueryObject getGaurd = new PreparedQueryObject();
527 getGaurd.appendQueryString("SELECT guard FROM "+keyspace+".lockq_"+tablename+ " WHERE key = ? ;");
528 getGaurd.addValue(primaryKey);
530 ResultSet getGaurdResult = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(getGaurd);
531 Row row = getGaurdResult.one();
533 guard = row.getLong("guard");
534 long timeOfWrite = System.currentTimeMillis();
535 long ts = MusicUtil.v2sTimeStampInMicroseconds(guard, timeOfWrite);
536 String query = queryObject.getQuery();
537 if (!queryObject.getQuery().contains("USING TIMESTAMP")) {
538 if (queryObject.getOperation().equalsIgnoreCase("delete"))
539 query = query.replaceFirst("WHERE", " USING TIMESTAMP " + ts + " WHERE ");
541 query = query.replaceFirst("SET", "USING TIMESTAMP " + ts + " SET");
543 queryObject.replaceQueryString(query);
546 } catch (MusicServiceException | MusicQueryException e) {
547 logger.error(EELFLoggerDelegate.applicationLogger,e.getMessage(), e);
550 result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, MusicUtil.EVENTUAL);
551 } catch (MusicServiceException | MusicQueryException ex) {
552 logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(),"[ERR512E] Failed to get Lock Handle ",
553 ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR);
554 logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() + " " + ex.getCause() + " ", ex);
555 return new ReturnType(ResultType.FAILURE, ex.getMessage());
558 return new ReturnType(ResultType.SUCCESS, "Eventual Operation Successfully performed");
560 return new ReturnType(ResultType.FAILURE, "Eventual Operation failed to perform");
568 * @param primaryKeyValue
573 public ReturnType criticalPut(String keyspace, String table, String primaryKeyValue,
574 PreparedQueryObject queryObject, String lockId, Condition conditionInfo) {
575 long start = System.currentTimeMillis();
577 String keyLock = lockId.substring(lockId.lastIndexOf(".") + 1,lockId.lastIndexOf("$"));
578 if (lockId.contains(".") && !keyLock.equals(primaryKeyValue)) {
579 return new ReturnType(ResultType.FAILURE,"Lock value '" + keyLock + "' and key value '"
580 + primaryKeyValue + "' not match. Please check your values: "
583 LockObject lockObject = getLockingServiceHandle().getLockInfo(keyspace, table, primaryKeyValue,
584 lockId.substring(lockId.lastIndexOf("$") + 1));
586 if ( lockObject == null ) {
587 return new ReturnType(ResultType.FAILURE, lockId + " does not exist.");
588 } else if (!lockObject.getIsLockOwner()) {
589 return new ReturnType(ResultType.FAILURE, lockId + " is not the lock holder");
590 } else if (lockObject.getLocktype() != LockType.WRITE) {
591 return new ReturnType(ResultType.FAILURE,
592 "Attempting to do write operation, but " + lockId + " is a read lock");
595 if (conditionInfo != null) {
597 if (conditionInfo.testCondition() == false)
598 return new ReturnType(ResultType.FAILURE, "Lock acquired but the condition is not true");
599 } catch (Exception e) {
600 logger.error(EELFLoggerDelegate.errorLogger, e);
601 return new ReturnType(ResultType.FAILURE,
602 "Exception thrown while checking the condition, check its sanctity:\n" + e.getMessage());
605 String query = queryObject.getQuery();
606 long timeOfWrite = System.currentTimeMillis();
607 long lockOrdinal = Long.parseLong(lockId.substring(lockId.lastIndexOf("$") + 1));
608 long ts = MusicUtil.v2sTimeStampInMicroseconds(lockOrdinal, timeOfWrite);
609 // TODO: use Statement instead of modifying query
610 if (!queryObject.getQuery().contains("USING TIMESTAMP")) {
611 if (queryObject.getOperation().equalsIgnoreCase("delete"))
612 query = query.replaceFirst("WHERE", " USING TIMESTAMP " + ts + " WHERE ");
613 else if (queryObject.getOperation().equalsIgnoreCase("insert"))
614 query = query.replaceFirst(";", " USING TIMESTAMP " + ts + " ; ");
616 query = query.replaceFirst("SET", "USING TIMESTAMP " + ts + " SET");
618 queryObject.replaceQueryString(query);
619 MusicDataStore dsHandle = MusicDataStoreHandle.getDSHandle();
620 dsHandle.executePut(queryObject, MusicUtil.CRITICAL);
621 long end = System.currentTimeMillis();
622 logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms");
623 } catch (MusicQueryException | MusicServiceException | MusicLockingException e) {
624 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), e);
625 return new ReturnType(ResultType.FAILURE,
626 "Exception thrown while doing the critical put: "
629 return new ReturnType(ResultType.SUCCESS, "Update performed");
637 * @return Boolean Indicates success or failure
638 * @throws MusicServiceException
642 public ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException,MusicQueryException {
643 // this is mainly for some functions like keyspace creation etc which does not
644 // really need the bells and whistles of Music locking.
645 boolean result = false;
647 result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, consistency);
648 // } catch (MusicQueryException | MusicServiceException ex) {
649 // logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(), AppMessages.UNKNOWNERROR,
650 // ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR, ex);
651 // throw new MusicServiceException(ex.getMessage(),ex);
653 return result ? ResultType.SUCCESS : ResultType.FAILURE;
657 * This method performs DDL operation on cassandra.
659 * @param queryObject query object containing prepared query and values
661 * @throws MusicServiceException
663 public ResultSet get(PreparedQueryObject queryObject) throws MusicServiceException {
664 ResultSet results = null;
666 results = MusicDataStoreHandle.getDSHandle().executeOneConsistencyGet(queryObject);
667 } catch (MusicQueryException | MusicServiceException e) {
668 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
669 throw new MusicServiceException(e.getMessage());
675 * This method performs DDL operations on cassandra, if the the resource is available. Lock ID
676 * is used to check if the resource is free.
678 * @param keyspace name of the keyspace
679 * @param table name of the table
680 * @param primaryKeyValue primary key value
681 * @param queryObject query object containing prepared query and values
682 * @param lockId lock ID to check if the resource is free to perform the operation.
685 public ResultSet criticalGet(String keyspace, String table, String primaryKeyValue,
686 PreparedQueryObject queryObject, String lockId) throws MusicServiceException {
687 ResultSet results = null;
688 String keyLock = lockId.substring(lockId.lastIndexOf(".") + 1,lockId.lastIndexOf("$"));
690 if (lockId.contains(".") && !keyLock.equals(primaryKeyValue)) {
691 throw new MusicLockingException("Lock value '" + keyLock + "' and key value '"
692 + primaryKeyValue + "' do not match. Please check your values: "
695 LockObject lockObject = getLockingServiceHandle().getLockInfo(keyspace, table, primaryKeyValue,
696 lockId.substring(lockId.lastIndexOf("$") + 1));
697 if (null == lockObject) {
698 throw new MusicLockingException("No Lock Object. Please check if lock name or key is correct."
701 if ( !lockObject.getIsLockOwner()) {
702 return null;// not top of the lock store q
704 results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryObject);
705 } catch ( MusicLockingException e ) {
706 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity
707 .WARN, ErrorTypes.MUSICSERVICEERROR);
708 throw new MusicServiceException(
709 "Cannot perform critical get for key: " + primaryKeyValue + " : " + e.getMessage());
710 } catch (MusicQueryException | MusicServiceException e) {
711 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity
712 .WARN, ErrorTypes.MUSICSERVICEERROR, e);
713 throw new MusicServiceException(
714 "Cannot perform critical get for key: " + primaryKeyValue + " : " + e.getMessage());
720 * This method performs DML operation on cassandra, when the lock of the dd is acquired.
722 * @param keyspaceName name of the keyspace
723 * @param tableName name of the table
724 * @param primaryKey primary key value
725 * @param queryObject query object containing prepared query and values
727 * @throws MusicLockingException
728 * @throws MusicServiceException
729 * @throws MusicQueryException
731 public ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey,
732 PreparedQueryObject queryObject, Condition conditionInfo)
733 throws MusicLockingException, MusicQueryException, MusicServiceException {
734 long start = System.currentTimeMillis();
735 String fullyQualifiedKey = keyspaceName + "." + tableName + "." + primaryKey;
736 String lockId = createLockReference(fullyQualifiedKey, LockType.WRITE);
737 long lockCreationTime = System.currentTimeMillis();
738 ReturnType lockAcqResult = null;
739 logger.info(EELFLoggerDelegate.applicationLogger,
740 "***Acquiring lock for atomicPut() query : " + queryObject.getQuery() + " : " + primaryKey);
741 logger.info(EELFLoggerDelegate.applicationLogger,
742 "***Acquiring lock for atomicPut() values: " + queryObject.getValues().toString());
743 if (conditionInfo != null) {
744 logger.info(EELFLoggerDelegate.applicationLogger,
745 "***Acquiring lock for atomicPut() conditions: " + conditionInfo.toString());
748 lockAcqResult = acquireLockWithLease(fullyQualifiedKey, lockId, MusicUtil.getDefaultLockLeasePeriod());
749 } catch (MusicLockingException ex) {
750 logger.error(EELFLoggerDelegate.errorLogger,
751 "Exception while acquireLockWithLease() in atomic put for key: " + primaryKey);
752 logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage());
753 throw new MusicServiceException(
754 "Cannot perform atomic put for key: " + primaryKey + " : " + ex.getMessage());
756 long lockAcqTime = System.currentTimeMillis();
759 * if (!lockAcqResult.getResult().equals(ResultType.SUCCESS)) { logger.info(EELFLoggerDelegate.
760 * applicationLogger,"unable to acquire lock, id " + lockId);
761 * voluntaryReleaseLock(fullyQualifiedKey,lockId); return lockAcqResult; }
764 logger.info(EELFLoggerDelegate.applicationLogger, "acquired lock with id " + lockId);
765 String lockRef = lockId.substring(lockId.lastIndexOf("$"));
766 ReturnType criticalPutResult = null;
767 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
768 criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, queryObject, lockRef, conditionInfo);
769 long criticalPutTime = System.currentTimeMillis();
770 long lockDeleteTime = System.currentTimeMillis();
771 String timingInfo = "|lock creation time:" + (lockCreationTime - start) + "|lock accquire time:"
772 + (lockAcqTime - lockCreationTime) + "|critical put time:" + (criticalPutTime - lockAcqTime)
773 + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|";
774 criticalPutResult.setTimingInfo(timingInfo);
776 logger.info(EELFLoggerDelegate.applicationLogger, "unable to acquire lock, id " + lockId);
777 criticalPutResult = lockAcqResult;
780 voluntaryReleaseLock(fullyQualifiedKey, lockId);
781 } catch (MusicLockingException ex) {
782 logger.info(EELFLoggerDelegate.applicationLogger,
783 "Exception occured while deleting lock after atomic put for key: " + primaryKey);
784 criticalPutResult.setMessage(criticalPutResult.getMessage() + "Lock release failed");
786 return criticalPutResult;
792 * This method performs DDL operation on cassasndra, when the lock for the resource is acquired.
794 * @param keyspaceName name of the keyspace
795 * @param tableName name of the table
796 * @param primaryKey primary key value
797 * @param queryObject query object containing prepared query and values
799 * @throws MusicServiceException
800 * @throws MusicLockingException
801 * @throws MusicQueryException
803 public ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey,
804 PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException, MusicQueryException {
805 String fullyQualifiedKey = keyspaceName + "." + tableName + "." + primaryKey;
806 String lockId = createLockReference(fullyQualifiedKey, LockType.READ);
807 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
808 ReturnType lockAcqResult = null;
809 ResultSet result = null;
810 logger.info(EELFLoggerDelegate.applicationLogger, "Acquiring lock for atomicGet() : " + queryObject.getQuery());
812 lockAcqResult = acquireLockWithLease(fullyQualifiedKey, lockId, MusicUtil.getDefaultLockLeasePeriod());
813 } catch (MusicLockingException ex) {
814 logger.error(EELFLoggerDelegate.errorLogger,
815 "Exception while acquireLockWithLease() in atomic get for key: " + primaryKey);
816 logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage());
817 throw new MusicServiceException(
818 "Cannot perform atomic get for key: " + primaryKey + " : " + ex.getMessage());
820 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
821 logger.info(EELFLoggerDelegate.applicationLogger, "acquired lock with id " + lockId);
822 String lockRef = lockId.substring(lockId.lastIndexOf("$"));
823 result = criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockRef);
825 logger.info(EELFLoggerDelegate.applicationLogger, "unable to acquire lock, id " + lockId);
828 voluntaryReleaseLock(fullyQualifiedKey, lockId);
829 } catch (MusicLockingException ex) {
830 logger.info(EELFLoggerDelegate.applicationLogger,
831 "Exception occured while deleting lock after atomic put for key: " + primaryKey);
832 throw new MusicLockingException(ex.getMessage());
844 public Map<String, Object> validateLock(String lockName) {
845 return MusicUtil.validateLock(lockName);
850 public ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
851 PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException {
856 public List<String> getLockQueue(String fullyQualifiedKey)
857 throws MusicServiceException, MusicQueryException, MusicLockingException {
858 String[] splitString = fullyQualifiedKey.split("\\.");
859 String keyspace = splitString[0];
860 String table = splitString[1];
861 String primaryKeyValue = splitString[2];
863 return getLockingServiceHandle().getLockQueue(keyspace, table, primaryKeyValue);
866 public long getLockQueueSize(String fullyQualifiedKey)
867 throws MusicServiceException, MusicQueryException, MusicLockingException {
868 String[] splitString = fullyQualifiedKey.split("\\.");
869 String keyspace = splitString[0];
870 String table = splitString[1];
871 String primaryKeyValue = splitString[2];
873 return getLockingServiceHandle().getLockQueueSize(keyspace, table, primaryKeyValue);
878 public ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey,
879 PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException {
884 //Methods added for ORM changes
886 public ResultType createKeyspace(JsonKeySpace jsonKeySpaceObject,String consistencyInfo)
887 throws MusicServiceException,MusicQueryException {
888 ResultType result = nonKeyRelatedPut(jsonKeySpaceObject.genCreateKeyspaceQuery(), consistencyInfo);
889 logger.info(EELFLoggerDelegate.applicationLogger, " Keyspace Creation Process completed successfully");
894 public ResultType dropKeyspace(JsonKeySpace jsonKeySpaceObject, String consistencyInfo)
895 throws MusicServiceException,MusicQueryException {
896 ResultType result = nonKeyRelatedPut(jsonKeySpaceObject.genDropKeyspaceQuery(),
898 logger.info(EELFLoggerDelegate.applicationLogger, " Keyspace deletion Process completed successfully");
902 public ResultType createTable(JsonTable jsonTableObject, String consistencyInfo)
903 throws MusicServiceException, MusicQueryException {
904 ResultType result = null;
906 result = createTable(jsonTableObject.getKeyspaceName(),
907 jsonTableObject.getTableName(), jsonTableObject.genCreateTableQuery(), consistencyInfo);
909 } catch (MusicServiceException ex) {
910 logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(), AppMessages.UNKNOWNERROR, ErrorSeverity.WARN,
911 ErrorTypes.MUSICSERVICEERROR);
912 throw new MusicServiceException(ex.getMessage());
914 logger.info(EELFLoggerDelegate.applicationLogger, " Table Creation Process completed successfully ");
918 public ResultType dropTable(JsonTable jsonTableObject,String consistencyInfo)
919 throws MusicServiceException,MusicQueryException {
920 ResultType result = nonKeyRelatedPut(jsonTableObject.genDropTableQuery(),
922 logger.info(EELFLoggerDelegate.applicationLogger, " Table deletion Process completed successfully ");
928 public ResultType createIndex(JsonIndex jsonIndexObject, String consistencyInfo)
929 throws MusicServiceException, MusicQueryException{
930 ResultType result = nonKeyRelatedPut(jsonIndexObject.genCreateIndexQuery(),
933 logger.info(EELFLoggerDelegate.applicationLogger, " Index creation Process completed successfully ");
938 * This method performs DDL operation on cassandra.
940 * @param queryObject query object containing prepared query and values
942 * @throws MusicServiceException
944 public ResultSet select(JsonSelect jsonSelect, MultivaluedMap<String, String> rowParams)
945 throws MusicServiceException, MusicQueryException {
946 ResultSet results = null;
948 results = get(jsonSelect.genSelectQuery(rowParams));
949 } catch (MusicServiceException e) {
950 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
951 throw new MusicServiceException(e.getMessage());
959 public ResultSet selectCritical(JsonInsert jsonInsertObj, MultivaluedMap<String, String> rowParams)
960 throws MusicLockingException, MusicQueryException, MusicServiceException {
962 ResultSet results = null;
963 String consistency = "";
964 if(null != jsonInsertObj && null != jsonInsertObj.getConsistencyInfo()) {
965 consistency = jsonInsertObj.getConsistencyInfo().get("type");
968 String lockId = jsonInsertObj.getConsistencyInfo().get("lockId");
970 PreparedQueryObject queryObject = jsonInsertObj.genSelectCriticalPreparedQueryObj(rowParams);
972 if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) {
973 results = criticalGet(jsonInsertObj.getKeyspaceName(), jsonInsertObj.getTableName(),
974 jsonInsertObj.getPrimaryKeyVal(), queryObject,lockId);
975 } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) {
976 results = atomicGet(jsonInsertObj.getKeyspaceName(), jsonInsertObj.getTableName(),
977 jsonInsertObj.getPrimaryKeyVal(), queryObject);
984 * this is insert row into Table
986 public ReturnType insertIntoTable(JsonInsert jsonInsertObj)
987 throws MusicLockingException, MusicQueryException, MusicServiceException {
989 String consistency = "";
990 if(null != jsonInsertObj && null != jsonInsertObj.getConsistencyInfo()) {
991 consistency = jsonInsertObj.getConsistencyInfo().get("type");
994 ReturnType result = null;
997 PreparedQueryObject queryObj = null;
998 queryObj = jsonInsertObj.genInsertPreparedQueryObj();
1000 if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) {
1001 result = eventualPut(jsonInsertObj.genInsertPreparedQueryObj());
1002 } else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) {
1003 String lockId = jsonInsertObj.getConsistencyInfo().get("lockId");
1004 if(lockId == null) {
1005 logger.error(EELFLoggerDelegate.errorLogger,"LockId cannot be null. Create lock reference or"
1006 + " use ATOMIC instead of CRITICAL", ErrorSeverity.FATAL, ErrorTypes.MUSICSERVICEERROR);
1007 return new ReturnType(ResultType.FAILURE, "LockId cannot be null. Create lock "
1008 + "and acquire lock or use ATOMIC instead of CRITICAL");
1010 result = criticalPut(jsonInsertObj.getKeyspaceName(),
1011 jsonInsertObj.getTableName(), jsonInsertObj.getPrimaryKeyVal(), jsonInsertObj.genInsertPreparedQueryObj(), lockId,null);
1012 } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) {
1013 result = atomicPut(jsonInsertObj.getKeyspaceName(), jsonInsertObj.getTableName(),
1014 jsonInsertObj.getPrimaryKeyVal(), jsonInsertObj.genInsertPreparedQueryObj(), null);
1016 } catch (Exception ex) {
1017 logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity
1018 .WARN, ErrorTypes.MUSICSERVICEERROR, ex);
1019 return new ReturnType(ResultType.FAILURE, ex.getMessage());
1026 * This is insert row into Table
1028 public ReturnType updateTable(JsonUpdate jsonUpdateObj, MultivaluedMap<String, String> rowParams)
1029 throws MusicLockingException, MusicQueryException, MusicServiceException {
1031 ReturnType result = null;
1032 String consistency = "";
1033 if(null != jsonUpdateObj && null != jsonUpdateObj.getConsistencyInfo()) {
1034 consistency = jsonUpdateObj.getConsistencyInfo().get("type");
1036 PreparedQueryObject queryObject = jsonUpdateObj.genUpdatePreparedQueryObj(rowParams);
1038 Condition conditionInfo;
1039 if (jsonUpdateObj.getConditions() == null) {
1040 conditionInfo = null;
1042 // to avoid parsing repeatedly, just send the select query to obtain row
1043 PreparedQueryObject selectQuery = new PreparedQueryObject();
1044 selectQuery.appendQueryString("SELECT * FROM " + jsonUpdateObj.getKeyspaceName() + "." + jsonUpdateObj.getTableName() + " WHERE "
1045 + jsonUpdateObj.getRowIdString() + ";");
1046 selectQuery.addValue(jsonUpdateObj.getPrimarKeyValue());
1047 conditionInfo = new Condition(jsonUpdateObj.getConditions(), selectQuery);
1051 if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) {
1052 result = eventualPut(queryObject);
1053 } else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) {
1054 String lockId = jsonUpdateObj.getConsistencyInfo().get("lockId");
1055 if(lockId == null) {
1056 logger.error(EELFLoggerDelegate.errorLogger,"LockId cannot be null. Create lock reference or"
1057 + " use ATOMIC instead of CRITICAL", ErrorSeverity.FATAL, ErrorTypes.MUSICSERVICEERROR);
1059 return new ReturnType(ResultType.FAILURE, "LockId cannot be null. Create lock "
1060 + "and acquire lock or use ATOMIC instead of CRITICAL");
1062 result = criticalPut(jsonUpdateObj.getKeyspaceName(), jsonUpdateObj.getTableName(), jsonUpdateObj.getPrimarKeyValue(),
1063 queryObject, lockId, conditionInfo);
1064 } else if (consistency.equalsIgnoreCase("atomic_delete_lock")) {
1065 // this function is mainly for the benchmarks
1067 result = atomicPutWithDeleteLock(jsonUpdateObj.getKeyspaceName(), jsonUpdateObj.getTableName(),
1068 jsonUpdateObj.getPrimarKeyValue(), queryObject, conditionInfo);
1069 } catch (MusicLockingException e) {
1070 logger.error(EELFLoggerDelegate.errorLogger,e, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN,
1071 ErrorTypes.GENERALSERVICEERROR, e);
1072 throw new MusicLockingException(AppMessages.UNKNOWNERROR.toString());
1075 } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) {
1077 result = atomicPut(jsonUpdateObj.getKeyspaceName(), jsonUpdateObj.getTableName(), jsonUpdateObj.getPrimarKeyValue(),
1078 queryObject, conditionInfo);
1079 } catch (MusicLockingException e) {
1080 logger.error(EELFLoggerDelegate.errorLogger,e, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR, e);
1081 throw new MusicLockingException(AppMessages.UNKNOWNERROR.toString());
1083 } else if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL_NB)) {
1085 result = eventualPut_nb(queryObject, jsonUpdateObj.getKeyspaceName(),
1086 jsonUpdateObj.getTableName(), jsonUpdateObj.getPrimarKeyValue());
1087 }catch (Exception e) {
1088 return new ReturnType(ResultType.FAILURE, e.getMessage());
1097 * This method is for Delete From Table
1099 public ReturnType deleteFromTable(JsonDelete jsonDeleteObj, MultivaluedMap<String, String> rowParams)
1100 throws MusicLockingException, MusicQueryException, MusicServiceException {
1102 ReturnType result = null;
1103 String consistency = "";
1104 if(null != jsonDeleteObj && null != jsonDeleteObj.getConsistencyInfo()) {
1105 consistency = jsonDeleteObj.getConsistencyInfo().get("type");
1107 PreparedQueryObject queryObject = jsonDeleteObj.genDeletePreparedQueryObj(rowParams);
1109 // get the conditional, if any
1110 Condition conditionInfo;
1111 if (jsonDeleteObj.getConditions() == null) {
1112 conditionInfo = null;
1114 // to avoid parsing repeatedly, just send the select query to obtain row
1115 PreparedQueryObject selectQuery = new PreparedQueryObject();
1116 selectQuery.appendQueryString("SELECT * FROM " + jsonDeleteObj.getKeyspaceName() + "." + jsonDeleteObj.getTableName() + " WHERE "
1117 + jsonDeleteObj.getRowIdString() + ";");
1118 selectQuery.addValue(jsonDeleteObj.getPrimarKeyValue());
1119 conditionInfo = new Condition(jsonDeleteObj.getConditions(), selectQuery);
1122 if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL))
1123 result = eventualPut(queryObject);
1124 else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) {
1125 String lockId = jsonDeleteObj.getConsistencyInfo().get("lockId");
1126 if(lockId == null) {
1127 logger.error(EELFLoggerDelegate.errorLogger,"LockId cannot be null. Create lock reference or"
1128 + " use ATOMIC instead of CRITICAL", ErrorSeverity.FATAL, ErrorTypes.MUSICSERVICEERROR);
1130 return new ReturnType(ResultType.FAILURE, "LockId cannot be null. Create lock "
1131 + "and acquire lock or use ATOMIC instead of CRITICAL");
1133 result = criticalPut(jsonDeleteObj.getKeyspaceName(),
1134 jsonDeleteObj.getTableName(), jsonDeleteObj.getPrimarKeyValue(),
1135 queryObject, lockId, conditionInfo);
1136 } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) {
1137 result = atomicPut(jsonDeleteObj.getKeyspaceName(),
1138 jsonDeleteObj.getTableName(), jsonDeleteObj.getPrimarKeyValue(),
1139 queryObject, conditionInfo);
1140 } else if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL_NB)) {
1141 result = eventualPut_nb(queryObject, jsonDeleteObj.getKeyspaceName(),
1142 jsonDeleteObj.getTableName(), jsonDeleteObj.getPrimarKeyValue());