2009-04-19 10:58:20 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009 Scalable Solutions.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package se.scalablesolutions.akka.kernel
|
|
|
|
|
|
|
|
|
|
import java.io.File
|
2009-05-01 13:25:43 +02:00
|
|
|
import java.lang.reflect.Constructor
|
2009-04-19 10:58:20 +02:00
|
|
|
|
|
|
|
|
import org.apache.cassandra.config.DatabaseDescriptor
|
|
|
|
|
import org.apache.cassandra.service._
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2009-04-27 19:55:57 +02:00
|
|
|
final object CassandraNode extends Logging {
|
|
|
|
|
|
2009-05-13 19:28:55 +02:00
|
|
|
// NOTE: requires command line options:
|
|
|
|
|
// -Dcassandra -Dstorage-config=config/ -Dpidfile=akka.pid
|
2009-04-27 19:55:57 +02:00
|
|
|
val TABLE_NAME = "akka"
|
|
|
|
|
val ACTOR_KEY_PREFIX = "actor"
|
|
|
|
|
val ACTOR_MAP_COLUMN_FAMILY = "map"
|
|
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
// TODO: make pluggable (JSON, Thrift, Protobuf etc.)
|
|
|
|
|
private[this] var serializer: Serializer = new JavaSerializationSerializer
|
|
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
// TODO: is this server thread-safe or needed to be wrapped up in an actor?
|
2009-05-13 19:28:55 +02:00
|
|
|
private[this] val server = classOf[CassandraServer].newInstance.asInstanceOf[CassandraServer]
|
2009-05-01 13:25:43 +02:00
|
|
|
|
2009-04-27 19:55:57 +02:00
|
|
|
def start = {
|
|
|
|
|
try {
|
|
|
|
|
server.start
|
|
|
|
|
log.info("Persistent storage has started up successfully");
|
|
|
|
|
} catch {
|
|
|
|
|
case e =>
|
|
|
|
|
log.error("Could not start up persistent storage")
|
|
|
|
|
throw e
|
|
|
|
|
}
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
def stop = {}
|
2009-04-27 19:55:57 +02:00
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
def insertActorStorageEntry(actorName: String, entry: String, content: AnyRef) = {
|
2009-04-27 19:55:57 +02:00
|
|
|
server.insert(
|
|
|
|
|
TABLE_NAME,
|
|
|
|
|
ACTOR_KEY_PREFIX + ":" + actorName,
|
|
|
|
|
ACTOR_MAP_COLUMN_FAMILY + ":" + entry,
|
2009-05-01 13:25:43 +02:00
|
|
|
serializer.out(content),
|
2009-05-13 19:28:55 +02:00
|
|
|
System.currentTimeMillis,
|
|
|
|
|
false) // FIXME: what is this flag for?
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
def insertActorStorageEntries(actorName: String, entries: List[Tuple2[String, AnyRef]]) = {
|
2009-04-27 19:55:57 +02:00
|
|
|
import java.util.{Map, HashMap, List, ArrayList}
|
|
|
|
|
val columns: Map[String, List[column_t]] = new HashMap
|
|
|
|
|
for (entry <- entries) {
|
|
|
|
|
val cls: List[column_t] = new ArrayList
|
2009-05-01 13:25:43 +02:00
|
|
|
cls.add(new column_t(entry._1, serializer.out(entry._2), System.currentTimeMillis))
|
2009-04-27 19:55:57 +02:00
|
|
|
columns.put(ACTOR_MAP_COLUMN_FAMILY, cls)
|
|
|
|
|
}
|
2009-05-13 19:28:55 +02:00
|
|
|
server.batch_insert(new batch_mutation_t(
|
2009-04-27 19:55:57 +02:00
|
|
|
TABLE_NAME,
|
|
|
|
|
ACTOR_KEY_PREFIX + ":" + actorName,
|
2009-05-13 19:28:55 +02:00
|
|
|
columns),
|
|
|
|
|
false) // non-blocking
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
def getActorStorageEntryFor(actorName: String, entry: AnyRef): Option[AnyRef] = {
|
2009-04-27 19:55:57 +02:00
|
|
|
try {
|
|
|
|
|
val column = server.get_column(TABLE_NAME, ACTOR_KEY_PREFIX + ":" + actorName, ACTOR_MAP_COLUMN_FAMILY + ":" + entry)
|
2009-05-01 13:25:43 +02:00
|
|
|
Some(serializer.in(column.value))
|
2009-04-27 19:55:57 +02:00
|
|
|
} catch { case e => None }
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
def getActorStorageFor(actorName: String): List[Tuple2[String, AnyRef]] = {
|
2009-04-27 19:55:57 +02:00
|
|
|
val columns = server.get_columns_since(TABLE_NAME, ACTOR_KEY_PREFIX, ACTOR_MAP_COLUMN_FAMILY, -1)
|
|
|
|
|
.toArray.toList.asInstanceOf[List[org.apache.cassandra.service.column_t]]
|
|
|
|
|
for {
|
|
|
|
|
column <- columns
|
2009-05-01 13:25:43 +02:00
|
|
|
col = (column.columnName, serializer.in(column.value))
|
2009-04-27 19:55:57 +02:00
|
|
|
} yield col
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def getActorStorageSizeFor(actorName: String): Int =
|
|
|
|
|
server.get_column_count(TABLE_NAME, ACTOR_KEY_PREFIX + ":" + actorName, ACTOR_MAP_COLUMN_FAMILY)
|
|
|
|
|
|
|
|
|
|
def removeActorStorageFor(actorName: String) =
|
2009-05-01 13:25:43 +02:00
|
|
|
server.remove(TABLE_NAME, ACTOR_KEY_PREFIX + ":" + actorName, ACTOR_MAP_COLUMN_FAMILY, System.currentTimeMillis, false)
|
2009-04-27 19:55:57 +02:00
|
|
|
|
2009-05-01 13:25:43 +02:00
|
|
|
def getActorStorageRange(actorName: String, start: Int, count: Int): List[Tuple2[String, AnyRef]] =
|
2009-04-27 19:55:57 +02:00
|
|
|
server.get_slice(TABLE_NAME, ACTOR_KEY_PREFIX + ":" + actorName, ACTOR_MAP_COLUMN_FAMILY, start, count)
|
2009-05-01 13:25:43 +02:00
|
|
|
.toArray.toList.asInstanceOf[List[Tuple2[String, AnyRef]]]
|
2009-04-27 19:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This code is only for starting up the Cassandra Thrift server, perhaps later
|
|
|
|
|
|
|
|
|
|
import scala.actors.Actor._
|
|
|
|
|
|
|
|
|
|
import com.facebook.thrift.protocol.TBinaryProtocol
|
|
|
|
|
import com.facebook.thrift.protocol.TProtocolFactory
|
|
|
|
|
import com.facebook.thrift.server.TThreadPoolServer
|
|
|
|
|
import com.facebook.thrift.transport.TServerSocket
|
|
|
|
|
import com.facebook.thrift.transport.TTransportException
|
|
|
|
|
import com.facebook.thrift.transport.TTransportFactory
|
|
|
|
|
import com.facebook.thrift.TProcessorFactory
|
|
|
|
|
|
|
|
|
|
private[this] val serverEngine: TThreadPoolServer = try {
|
2009-04-19 10:58:20 +02:00
|
|
|
val pidFile = System.getProperty("pidfile")
|
|
|
|
|
if (pidFile != null) new File(pidFile).deleteOnExit();
|
|
|
|
|
val listenPort = DatabaseDescriptor.getThriftPort
|
|
|
|
|
|
|
|
|
|
val processor = new Cassandra.Processor(server)
|
|
|
|
|
val tServerSocket = new TServerSocket(listenPort)
|
|
|
|
|
val tProtocolFactory = new TBinaryProtocol.Factory
|
|
|
|
|
|
|
|
|
|
val options = new TThreadPoolServer.Options
|
|
|
|
|
options.minWorkerThreads = 64
|
|
|
|
|
new TThreadPoolServer(new TProcessorFactory(processor),
|
|
|
|
|
tServerSocket,
|
|
|
|
|
new TTransportFactory,
|
|
|
|
|
new TTransportFactory,
|
|
|
|
|
tProtocolFactory,
|
|
|
|
|
tProtocolFactory,
|
|
|
|
|
options)
|
|
|
|
|
} catch {
|
|
|
|
|
case e =>
|
|
|
|
|
log.error("Could not start up persistent storage node.")
|
|
|
|
|
throw e
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
private[this] val serverDaemon = actor {
|
|
|
|
|
receive {
|
|
|
|
|
case Start =>
|
|
|
|
|
log.info("Persistent storage node starting up...")
|
|
|
|
|
serverEngine.serve
|
|
|
|
|
case Stop =>
|
|
|
|
|
log.info("Persistent storage node shutting down...")
|
|
|
|
|
serverEngine.stop
|
|
|
|
|
//case Insert(..) =>
|
|
|
|
|
// server.
|
2009-04-19 10:58:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2009-04-27 19:55:57 +02:00
|
|
|
*/
|
|
|
|
|
|