Permalink
Browse files

First baby step for locking

  • Loading branch information...
1 parent 8a46a91 commit fc1c57e3a20acf34d1963b8901ff9c176f126395 @patricioe patricioe committed Jul 15, 2012
@@ -0,0 +1,137 @@
+package me.prettyprint.cassandra.locking;
+
+import java.util.Arrays;
+
+import me.prettyprint.cassandra.service.AbstractCluster;
+import me.prettyprint.cassandra.service.CassandraHostConfigurator;
+import me.prettyprint.cassandra.service.ThriftKsDef;
+import me.prettyprint.hector.api.Cluster;
+import me.prettyprint.hector.api.Keyspace;
+import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
+import me.prettyprint.hector.api.ddl.ComparatorType;
+import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
+import me.prettyprint.hector.api.factory.HFactory;
+import me.prettyprint.hector.api.locking.HLockManager;
+import me.prettyprint.hector.api.locking.HLockManagerConfigurator;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class AbstractLockManager implements HLockManager {
+
+ private static final Logger log = LoggerFactory.getLogger(AbstractLockManager.class);
+
+ protected Cluster cluster;
+ protected Keyspace keyspace;
+ protected HLockManagerConfigurator lockManagerConfigurator;
+
+ public AbstractLockManager(Cluster cluster, Keyspace keyspace, HLockManagerConfigurator lockManagerConfigurator) {
+ if (cluster == null)
+ throw new RuntimeException("Cluster cannot be null for LockManager");
+
+ this.cluster = cluster;
+
+ if (lockManagerConfigurator == null) {
+ this.lockManagerConfigurator = new HLockManagerConfigurator();
+ }
+
+ if (keyspace == null) {
+ this.keyspace = HFactory.createKeyspace(lockManagerConfigurator.getKeyspaceName(), cluster);
+ } else {
+ // Set the Keyspace name in order to keep the info consistent
+ lockManagerConfigurator.setKeyspaceName(keyspace.getKeyspaceName());
+ }
+
+ }
+
+ public AbstractLockManager(Cluster cluster) {
+ this(cluster, null, null);
+ }
+
+ public AbstractLockManager(Cluster cluster, Keyspace keyspace) {
+ this(cluster, keyspace, null);
+ }
+
+ @Override
+ public void init() {
+ checkCreateLockSchema();
+ }
+
+ private void checkCreateLockSchema() {
+
+ KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspace.getKeyspaceName());
+
+ if (keyspaceDef == null) {
+
+ ColumnFamilyDefinition cfDef = createColumnFamilyDefinition();
+
+ KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition(keyspace.getKeyspaceName(),
+ ThriftKsDef.DEF_STRATEGY_CLASS,
+ lockManagerConfigurator.getReplicationFactor(),
+ Arrays.asList(cfDef));
+
+ log.info("Creating Keyspace and Column Family for LockManager with name (KSPS/CF): (" + newKeyspace.getName() + " / " + cfDef.getName());
+ cluster.addKeyspace(newKeyspace, true);
+ } else {
+ log.info("Keyspace for LockManager already exists. Skipping creation.");
+
+ // The Keyspace exists but we don't know anything about the CF yet.
+ if (!doesLockCFExist(keyspaceDef)) {
+ // create it
+ ColumnFamilyDefinition cfDef = createColumnFamilyDefinition();
+ log.info("Creating Column Family for LockManager with name: " + cfDef.getName());
+ cluster.addColumnFamily(cfDef, true);
+ } else {
+ log.info("Column Family for LockManager already exists. Skipping creation.");
+ }
+ }
+
+ }
+
+ private ColumnFamilyDefinition createColumnFamilyDefinition() {
+ return HFactory.createColumnFamilyDefinition(keyspace.getKeyspaceName(),
+ lockManagerConfigurator.getLockManagerCF(),
+ ComparatorType.BYTESTYPE);
+ }
+
+ private boolean doesLockCFExist(KeyspaceDefinition keyspaceDef) {
+ for (ColumnFamilyDefinition cfdef : keyspaceDef.getCfDefs()) {
+ if (cfdef.getName().equals(lockManagerConfigurator.getLockManagerCF())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private CassandraHostConfigurator getConfigurator() {
+ return ((AbstractCluster) cluster).getConfigurator();
+ }
+
+ @Override
+ public Cluster getCluster() {
+ return cluster;
+ }
+
+ public void setCluster(Cluster cluster) {
+ this.cluster = cluster;
+ }
+
+ @Override
+ public Keyspace getKeyspace() {
+ return keyspace;
+ }
+
+ public void setKeyspace(Keyspace keyspace) {
+ this.keyspace = keyspace;
+ }
+
+ @Override
+ public HLockManagerConfigurator getLockManagerConfigurator() {
+ return lockManagerConfigurator;
+ }
+
+ public void setLockManagerConfigurator(HLockManagerConfigurator lockManagerConfigurator) {
+ this.lockManagerConfigurator = lockManagerConfigurator;
+ }
+
+}
@@ -0,0 +1,71 @@
+package me.prettyprint.cassandra.locking;
+
+import me.prettyprint.hector.api.Cluster;
+import me.prettyprint.hector.api.Keyspace;
+import me.prettyprint.hector.api.factory.HFactory;
+import me.prettyprint.hector.api.locking.HLockManager;
+import me.prettyprint.hector.api.locking.HLockManagerConfigurator;
+
+public abstract class AbstractLockManagerImpl implements HLockManager {
+
+ protected Cluster cluster;
+ protected Keyspace keyspace;
+ protected HLockManagerConfigurator lockManagerConfigurator;
+
+ public AbstractLockManagerImpl(Cluster cluster, Keyspace keyspace, HLockManagerConfigurator lockManagerConfigurator) {
+ if (cluster == null) throw new RuntimeException("Cluster cannot be null for LockManager");
+
+ this.cluster = cluster;
+
+ if (lockManagerConfigurator == null) {
+ this.lockManagerConfigurator = new HLockManagerConfigurator();
+ }
+
+ if (keyspace == null) {
+ this.keyspace = HFactory.createKeyspace(lockManagerConfigurator.getKeyspaceName(), cluster);
+ }
+
+ }
+
+ public AbstractLockManagerImpl(Cluster cluster) {
+ this(cluster, null, null);
+ }
+
+ public AbstractLockManagerImpl(Cluster cluster, Keyspace keyspace) {
+ this(cluster, keyspace, null);
+ }
+
@zznate
zznate Jul 16, 2012 Collaborator

instead of multiple c-tor signatures, how about a builder:
new LockManagerBuilder(cluster).keyspace(keyspace).config(hlockConfigurator).build() // init code in the build method

@patricioe
patricioe Jul 16, 2012 Collaborator

I thought about that and mentioned it when I filed the issue. The problem I found with that approach is that it doesn't tell what are the minimum about of parameters you need to specified.

this way, you can just pass Cluster and everything else is default.

What is the down side of having three constructors ?

@zznate
zznate Jul 16, 2012 Collaborator

The only required parameter on a builder would be the LockManagerBuilder constructor. The chained method calls are optional. Builders keep code cleaner by deferring optional args to methods with clearer names (but I will admin it's a little bit less necessary here because the arguments are strongly typed).
The other benefit over a constructor is type deference to runtime - you force the adherence to the Interface and thus keep caller code more maintainable.

+ @Override
+ public void init() {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public Cluster getCluster() {
+ return cluster;
+ }
+
+ public void setCluster(Cluster cluster) {
+ this.cluster = cluster;
+ }
+
+ @Override
+ public Keyspace getKeyspace() {
+ return keyspace;
+ }
+
+ public void setKeyspace(Keyspace keyspace) {
+ this.keyspace = keyspace;
+ }
+
+ @Override
+ public HLockManagerConfigurator getLockManagerConfigurator() {
+ return lockManagerConfigurator;
+ }
+
+ public void setLockManagerConfigurator(HLockManagerConfigurator lockManagerConfigurator) {
+ this.lockManagerConfigurator = lockManagerConfigurator;
+ }
+
+}
@@ -0,0 +1,34 @@
+package me.prettyprint.cassandra.locking;
+
+import me.prettyprint.hector.api.Cluster;
+import me.prettyprint.hector.api.Keyspace;
+import me.prettyprint.hector.api.locking.HLock;
+import me.prettyprint.hector.api.locking.HLockManagerConfigurator;
+
+public class HLockManagerImpl extends AbstractLockManager {
+
+ public HLockManagerImpl(Cluster cluster, Keyspace keyspace) {
+ super(cluster, keyspace);
+ }
+
+ public HLockManagerImpl(Cluster cluster, Keyspace keyspace, HLockManagerConfigurator lockManagerConfigurator) {
+ super(cluster, keyspace, lockManagerConfigurator);
+ }
+
+ public HLockManagerImpl(Cluster cluster) {
+ super(cluster);
+ }
+
+ @Override
+ public void acquire(HLock lock) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void release(HLock lock) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
@@ -0,0 +1,9 @@
+package me.prettyprint.hector.api.locking;
+
+public interface HLock {
+
+ String getPath();
+
+ String setPath();
+
+}
@@ -0,0 +1,20 @@
+package me.prettyprint.hector.api.locking;
+
+import me.prettyprint.hector.api.Cluster;
+import me.prettyprint.hector.api.Keyspace;
+
+public interface HLockManager {
+
+
+ void init();
+
+ Cluster getCluster();
+
+ Keyspace getKeyspace();
+
+ HLockManagerConfigurator getLockManagerConfigurator();
+
+ void acquire(HLock lock);
+
+ void release(HLock lock);
+}
@@ -0,0 +1,71 @@
+package me.prettyprint.hector.api.locking;
+
+public class HLockManagerConfigurator {
+
+ public static final String DEFAUT_LOCK_MANAGER_CF = "HLocks";
+ private static final String DEFAULT_LOCK_MANAGER_KEYSPACE_NAME = "HLockingManager";
+
+ private String keyspaceName = DEFAULT_LOCK_MANAGER_KEYSPACE_NAME;
+ private String lockManagerCF = DEFAUT_LOCK_MANAGER_CF;
+ private boolean rowsCacheEnabled = true;
+ private long locksTTLInMillis = 5000L;
+ private long backOffRetryDelayInMillis = 500L;
+ private int replicationFactor = 3;
+
+ public String getLockManagerCF() {
+ return lockManagerCF;
+ }
+ public void setLockManagerCF(String lockManagerCF) {
+ this.lockManagerCF = lockManagerCF;
+ }
+ public boolean isRowsCacheEnabled() {
+ return rowsCacheEnabled;
+ }
+ public void setRowsCacheEnabled(boolean rowsCacheEnabled) {
+ this.rowsCacheEnabled = rowsCacheEnabled;
+ }
+ public long getLocksTTLInMillis() {
+ return locksTTLInMillis;
+ }
+ public void setLocksTTLInMillis(long locksTTLInMillis) {
+ this.locksTTLInMillis = locksTTLInMillis;
+ }
+ public long getBackOffRetryDelayInMillis() {
+ return backOffRetryDelayInMillis;
+ }
+ public void setBackOffRetryDelayInMillis(long backOffRetryDelayInMillis) {
+ this.backOffRetryDelayInMillis = backOffRetryDelayInMillis;
+ }
+ public String getKeyspaceName() {
+ return this.keyspaceName;
+ }
+ public void setKeyspaceName(String keyspaceName) {
+ this.keyspaceName = keyspaceName;
+ }
+
+ public int getReplicationFactor() {
+ return replicationFactor;
+ }
+ public void setReplicationFactor(int replicationFactor) {
+ this.replicationFactor = replicationFactor;
+ }
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append("HLockManagerConfigurator [keyspaceName=");
+ builder.append(keyspaceName);
+ builder.append(", lockManagerCF=");
+ builder.append(lockManagerCF);
+ builder.append(", rowsCacheEnabled=");
+ builder.append(rowsCacheEnabled);
+ builder.append(", locksTTLInMillis=");
+ builder.append(locksTTLInMillis);
+ builder.append(", backOffRetryDelayInMillis=");
+ builder.append(backOffRetryDelayInMillis);
+ builder.append(", replicationFactor=");
+ builder.append(replicationFactor);
+ builder.append("]");
+ return builder.toString();
+ }
+
+}
Oops, something went wrong.

0 comments on commit fc1c57e

Please sign in to comment.