Skip to content

Commit

Permalink
Allow conf loading from different sources
Browse files Browse the repository at this point in the history
  • Loading branch information
obermeier committed Aug 30, 2017
1 parent 05f6c73 commit c27d4c1
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class OnlineBatchSyncCassandra(dbSession: DbSession[Statement, Insert, ResultSet

// Create or use a given DB session.
@transient val session = dbSession
val config: SyncConfiguration = (new SyncConfigurationLoader).loadConfig
val config: SyncConfiguration = SyncConfigurationLoader.loadConfig

val syncTable = SyncTable(config.dbSystem, config.tableName)
val jobLockTable = JobLockTable(config.dbSystem, "JobLockTable")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CassandraJobInfo(

def getLock(dbSession: DbSession[Statement, Insert, ResultSet]): LockApi[Statement, Insert, ResultSet] = {
this.lock = this.lock.orElse {
val configSync: SyncConfiguration = (new SyncConfigurationLoader).loadConfig
val configSync: SyncConfiguration = SyncConfigurationLoader.loadConfig
val table = JobLockTable(configSync.dbSystem, "JobSync")

dbSession.execute(statementGenerator.createKeyspaceCreationStatement(table, configSync.replicationSetting).get).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import scray.querying.sync.conf.SyncConfiguration
class StartTimeDetector(job: JobInfo[Statement, Insert, ResultSet],
dbSession: DbSession[Statement, Insert, ResultSet]) extends LazyLogging {

val configSync: SyncConfiguration = (new SyncConfigurationLoader).loadConfig
val configSync: SyncConfiguration = SyncConfigurationLoader.loadConfig
val startConsensusTable = new Table(configSync.dbSystem, "startconsensus", new StartConsensusRow)

var valueAlreadySet = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TransactionTests extends WordSpec {
"lock job" in {
val jobInfo = CassandraJobInfo(getNextJobName)
val table = new OnlineBatchSyncCassandra(dbconnection)
table.initJob(jobInfo, new SumTestColumns())
table.initJob(jobInfo, new SumTestColumns)

jobInfo.getLock(dbconnection)
assert(true)
Expand Down
3 changes: 0 additions & 3 deletions scray-querying/nohup.out

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,83 @@ import org.yaml.snakeyaml.introspector.PropertyUtils;
import com.typesafe.scalalogging.slf4j.LazyLogging
import com.twitter.chill.config.Config


/**
* Load scray sync configuration
*/
class SyncConfigurationLoader(path: String = "scray-sync.yaml") extends LazyLogging {

def loadConfig: SyncConfiguration = {
object SyncConfigurationLoader extends LazyLogging {

val url: URL = (new File(path)).toURI().toURL
val DEFAULT_CONFIGURATION = "scray-sync.yaml";

val yaml = new Yaml;
val yamlData = yaml.load(url.openStream).asInstanceOf[Map[String, String]]

val conf = new SyncConfiguration
private def getConfigURL: Option[URL] = {
var configUrl = System.getProperty("cassandra.config");


if(yamlData.get("dbSystem") != null) {
conf.dbSystem = yamlData.get("dbSystem")
if (configUrl == null) {
logger.debug(s"Option -Dcassandra.config not foud. Try to use ${DEFAULT_CONFIGURATION}")
configUrl = DEFAULT_CONFIGURATION;
}

if(yamlData.get("tableName") != null) {
conf.tableName = yamlData.get("tableName")

var url: Option[URL] = None
try {
url = Some(new URL(configUrl))
url.get.openStream().close();
} catch {
case e: Exception => {
val loader = SyncConfigurationLoader.getClass.getClassLoader
val resourceURL = loader.getResource(configUrl)

if (resourceURL == null) {
logger.warn(s"No valid configuration file found in ${configUrl}. Use default values")
url = None
} else {
url = Some(resourceURL)
}
}
}

url
}

def loadConfig: SyncConfiguration = this.synchronized {
var conf: Option[SyncConfiguration] = None

getConfigURL.map(url => {

if (conf.isDefined) {
logger.debug(s"Configuration already loaded. Use existing configuration: ${conf.get}")
} else {

conf = Some(new SyncConfiguration)
conf.map(confe => {
try {
val yaml = new Yaml;
val yamlData = yaml.load(url.openStream).asInstanceOf[Map[String, String]]

if (yamlData.get("dbSystem") != null) {
confe.dbSystem = yamlData.get("dbSystem")
}

if (yamlData.get("tableName") != null) {
confe.tableName = yamlData.get("tableName")
}

if (yamlData.get("replication") != null) {
confe.replicationSetting = yamlData.get("replication")
}
} catch {
case e: YAMLException => {
logger.error(s"Invalid sync configuration yaml: ${url}, ${e.getMessage}")
conf = None
}
}
})
}
})

if(yamlData.get("replication") != null) {
conf.replicationSetting = yamlData.get("replication")
// Use default configuration if no configuration file was provided
if(conf.isDefined) {
conf.get
} else {
new SyncConfiguration
}

conf
}
}
3 changes: 3 additions & 0 deletions scray-querying/src/test/resources/scray-sync.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dbSystem: scray
tableName : synctable
replication: "{'class': 'NetworkTopologyStrategy', 'DC1': '5', 'DC2': '3', 'DC3': '0'}"

0 comments on commit c27d4c1

Please sign in to comment.