All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hmsonline.cassandra.triggers.CommitLog Maven / Gradle / Ivy

Go to download

Cassandra Triggers is a lightweight mechanism to implement trigger-like functionality for Cassandra.

There is a newer version: 1.0.1
Show newest version
package com.hmsonline.cassandra.triggers;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.db.RowMutation;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommitLog extends  LogEntryStore {
    private static Logger logger = LoggerFactory.getLogger(CommitLog.class);

    public static final String KEYSPACE = "triggers";
    public static final String COLUMN_FAMILY = "CommitLog";
    public static final int MAX_NUMBER_COLUMNS = 100;
    public static final int BATCH_SIZE = 1000;

    private Thread triggerThread = null;
    private static final long MAX_LOG_ENTRY_AGE = 5000; // age of entry, at
                                                        // which time any node
                                                        // can process it.

    public CommitLog() throws Exception {
        super(KEYSPACE, COLUMN_FAMILY);
        logger.warn("Instantiated commit log.");
        this.getHostName();
        triggerThread = new Thread(new TriggerTask());
        triggerThread.start();
        logger.debug("Started Trigger Task thread.");
    }
    
    private static CommitLog instance = null;

    public static synchronized CommitLog getCommitLog() throws Exception {
        if (instance == null) {
            instance = new CommitLog();
        }
        return instance;
    }

    public List writePending(ConsistencyLevel consistencyLevel, RowMutation rowMutation) throws Throwable {
        String keyspace = rowMutation.getTable();
        ByteBuffer rowKey = rowMutation.key();
        List entries = new ArrayList();
        for (Integer cfId : rowMutation.getColumnFamilyIds()) {
            ColumnFamily columnFamily = rowMutation.getColumnFamily(cfId);
            String path = keyspace + ":" + columnFamily.metadata().cfName;
            List triggers = TriggerStore.getStore().getTriggers().get(path);
            if(triggers != null && triggers.size() > 0) {
                String hostName = this.getHostName();
                LogEntry entry = new LogEntry(keyspace, columnFamily, rowKey, consistencyLevel, hostName,
                                              System.currentTimeMillis());
                entries.add(entry);
                write(entry);
            }
        }
        return entries;
    }

    public List getPending() throws Throwable {
        List result = new ArrayList();
        SlicePredicate predicate = new SlicePredicate();
        SliceRange range = new SliceRange(ByteBufferUtil.bytes(""), ByteBufferUtil.bytes(""), false, MAX_NUMBER_COLUMNS);
        predicate.setSlice_range(range);

        KeyRange keyRange = new KeyRange(BATCH_SIZE);
        keyRange.setStart_key(ByteBufferUtil.bytes(""));
        keyRange.setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER);
        ColumnParent parent = new ColumnParent(COLUMN_FAMILY);
        
        List rows = getConnection(KEYSPACE).get_range_slices(parent, predicate, keyRange, ConsistencyLevel.ALL);

        result.addAll(toLogEntry(rows));
        return result;
    }

    public boolean isMine(LogEntry logEntry) throws UnknownHostException, SocketException {
        return (logEntry.getHost().equals(this.getHostName()));
    }

    public boolean isOld(LogEntry logEntry) {
        long now = System.currentTimeMillis();
        long age = now - logEntry.getTimestamp();
        return (age > CommitLog.MAX_LOG_ENTRY_AGE);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy