com.hmsonline.cassandra.triggers.CassandraServerTriggerAspect Maven / Gradle / Ivy
package com.hmsonline.cassandra.triggers;
import java.util.ArrayList;
import java.util.List;
import org.apache.cassandra.db.IMutation;
import org.apache.cassandra.db.RowMutation;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Aspect
public class CassandraServerTriggerAspect {
private static Logger logger = LoggerFactory.getLogger(CassandraServerTriggerAspect.class);
private static final int UPDATE_PRIORITY = 2;
@Around("execution(* org.apache.cassandra.thrift.CassandraServer.doInsert(..))")
public void writeToCommitLog(ProceedingJoinPoint thisJoinPoint) throws Throwable {
if (ConfigurationStore.getStore().isCommitLogEnabled()) {
List logEntries = null;
try {
ConsistencyLevel consistencyLevel = (ConsistencyLevel) thisJoinPoint.getArgs()[0];
@SuppressWarnings("unchecked")
List mutations = (List) thisJoinPoint.getArgs()[1];
logEntries = writePending(consistencyLevel, mutations);
thisJoinPoint.proceed(thisJoinPoint.getArgs());
writeCommitted(logEntries);
}
catch (InvalidRequestException e) {
if(logEntries != null) {
for(LogEntry logEntry : logEntries) {
CommitLog.getCommitLog().remove(logEntry);
}
}
}
} else {
thisJoinPoint.proceed(thisJoinPoint.getArgs());
}
}
/**
* Logs an error message for unhandled exception thrown from the target method.
*
* @param joinPoint - the joint point cut that contains information about the target
* @param throwable - the cause of the exception from the target method invocation
*/
@AfterThrowing(pointcut = "execution(* org.apache.cassandra.thrift.CassandraServer.doInsert(..))", throwing = "throwable")
public void logErrorFromThrownException(final JoinPoint joinPoint, final Throwable throwable) {
final String className = joinPoint.getTarget().getClass().getName();
final String methodName = joinPoint.getSignature().getName();
logger.error("Could not write to cassandra! Method: " + className + "."+ methodName + "()", throwable);
}
private List writePending(ConsistencyLevel consistencyLevel, List mutations) throws Throwable {
List logEntries = new ArrayList();
for (IMutation mutation : mutations) {
if (mutation instanceof RowMutation) {
RowMutation rowMutation = (RowMutation) mutation;
logger.debug("Mutation for [" + rowMutation.getTable() + "] with consistencyLevel [" + consistencyLevel
+ "]");
if (!rowMutation.getTable().equals(CommitLog.KEYSPACE)) {
logEntries.addAll(CommitLog.getCommitLog().writePending(consistencyLevel, rowMutation));
}
}
}
return logEntries;
}
private void writeCommitted(List logEntries) throws Throwable {
for (LogEntry logEntry : logEntries) {
logEntry.setStatus(LogEntryStatus.COMMITTED);
CommitLog.getCommitLog().write(logEntry, UPDATE_PRIORITY);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy