forked from comeara/pillar
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add replication strategy support (#1)
* Basic setup for replication configuration support for Pillar. * Removed ReplicationOptions as it was redundant. * Error handling for reading ReplicationStrategy from our config. * Tweaked the error handling. * Modified the CommandExecutorTest and added a new test. * Add qualifications for config params. * added cassandra unit and tweaked app default params * Improved README
- Loading branch information
Showing
25 changed files
with
1,125 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,3 @@ jdk: | |
- oraclejdk8 | ||
scala: | ||
- 2.11.8 | ||
services: | ||
- cassandra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
src/main/scala/de/kaufhof/pillar/ReplicationStrategy.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package de.kaufhof.pillar | ||
|
||
/** | ||
* Defines all possible ReplicationStrategy configurations. | ||
* A NetworkTopologyStrategy will require the appropriate snitch. | ||
*/ | ||
sealed trait ReplicationStrategy { | ||
def cql: String | ||
override def toString: String = cql | ||
} | ||
|
||
final case class SimpleStrategy(replicationFactor: Int = 3) extends ReplicationStrategy { | ||
require(replicationFactor > 0) | ||
|
||
override def cql: String = s"{'class' : 'SimpleStrategy', 'replication_factor' : $replicationFactor}" | ||
} | ||
|
||
final case class NetworkTopologyStrategy(dataCenters: Seq[CassandraDataCenter]) extends ReplicationStrategy { | ||
require(dataCenters.nonEmpty) | ||
|
||
override def cql: String = { | ||
val replicationFacString = dataCenters.map { dc => | ||
s"'${dc.name}' : ${dc.replicationFactor} " | ||
}.mkString(", ") | ||
|
||
s"{'class' : 'NetworkTopologyStrategy', $replicationFacString }" | ||
} | ||
} | ||
|
||
final case class CassandraDataCenter(name: String, replicationFactor: Int){ | ||
require(replicationFactor > 0 && name.nonEmpty) | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/scala/de/kaufhof/pillar/ReplicationStrategyBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package de.kaufhof.pillar | ||
|
||
import java.util.Map.Entry | ||
|
||
import com.typesafe.config.ConfigException.BadValue | ||
import com.typesafe.config.{Config, ConfigException, ConfigObject, ConfigValue} | ||
|
||
import scala.util.{Failure, Success, Try} | ||
|
||
final case class ReplicationStrategyConfigError(msg: String) extends Exception | ||
|
||
object ReplicationStrategyBuilder { | ||
|
||
/** | ||
* Parses replication settings from a config that looks like: | ||
* {{{ | ||
* replicationStrategy: "SimpleStrategy" | ||
* replicationFactor: 3 | ||
* }}} | ||
* | ||
* or: | ||
* | ||
* {{{ | ||
* replicationStrategy: "NetworkTopologyStrategy" | ||
* replicationFactor: [ | ||
* {dc1: 3}, | ||
* {dc2: 3} | ||
* ] | ||
* }}} | ||
* | ||
* @param configuration The applications Typesafe config | ||
* @param dataStoreName The target data store, as defined in application.conf | ||
* @param environment The environment, as defined in application.conf (i.e. "pillar.dataStoreName.environment {...}) | ||
* @return ReplicationOptions with a default of Simple Strategy with a replication factor of 3. | ||
*/ | ||
def getReplicationStrategy(configuration: Config, dataStoreName: String, environment: String): ReplicationStrategy = try { | ||
|
||
val repStrategyStr = Try(configuration.getString(s"pillar.$dataStoreName.$environment.replicationStrategy")) | ||
|
||
repStrategyStr match { | ||
case Success(repStrategy) => repStrategy match { | ||
case "SimpleStrategy" => | ||
val repFactor = configuration.getInt(s"pillar.$dataStoreName.$environment.replicationFactor") | ||
SimpleStrategy(repFactor) | ||
|
||
case "NetworkTopologyStrategy" => | ||
import scala.collection.JavaConverters._ | ||
val dcConfigBuffer = configuration | ||
.getObjectList(s"pillar.$dataStoreName.$environment.replicationFactor") | ||
.asScala | ||
|
||
val dcBuffer = for { | ||
item: ConfigObject <- dcConfigBuffer | ||
entry: Entry[String, ConfigValue] <- item.entrySet().asScala | ||
dcName = entry.getKey | ||
dcRepFactor = entry.getValue.unwrapped().toString.toInt | ||
} yield (dcName, dcRepFactor) | ||
|
||
val datacenters = dcBuffer | ||
.map(dc => CassandraDataCenter(dc._1, dc._2)) | ||
.toList | ||
|
||
NetworkTopologyStrategy(datacenters) | ||
|
||
case _ => | ||
throw new ReplicationStrategyConfigError(s"$repStrategy is not a valid replication strategy.") | ||
} | ||
|
||
case Failure(e: ConfigException.Missing) => SimpleStrategy() | ||
case Failure(e) => throw e | ||
} | ||
} catch { | ||
case e: IllegalArgumentException => throw new BadValue(s"pillar.$dataStoreName.$environment", e.getMessage) | ||
case e: Exception => throw e | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.