Skip to content

Commit

Permalink
Move Typesafe Config and Kryo dependencies to Copycat core. Refactor …
Browse files Browse the repository at this point in the history
…Log and Raft configurations to read from Map directly.
  • Loading branch information
kuujo committed Feb 17, 2015
1 parent f05f7d3 commit 6880233
Show file tree
Hide file tree
Showing 23 changed files with 174 additions and 169 deletions.
15 changes: 15 additions & 0 deletions core/pom.xml
Expand Up @@ -11,12 +11,27 @@
<artifactId>copycat-core</artifactId>
<name>Copycat Core</name>

<properties>
<kryo.version>3.0.0</kryo.version>
<config.version>1.2.1</config.version>
</properties>

<dependencies>
<dependency>
<groupId>net.kuujo.copycat</groupId>
<artifactId>copycat-raft</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>${kryo.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>${config.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
File renamed without changes.
17 changes: 0 additions & 17 deletions core/src/test/resources/log-test.conf

This file was deleted.

4 changes: 0 additions & 4 deletions log/src/main/java/net/kuujo/copycat/log/BufferedLog.java
Expand Up @@ -32,10 +32,6 @@ public BufferedLog(Map<String, Object> config) {
super(config);
}

public BufferedLog(String resource) {
super(resource);
}

private BufferedLog(BufferedLog log) {
super(log);
}
Expand Down
20 changes: 5 additions & 15 deletions log/src/main/java/net/kuujo/copycat/log/FileLog.java
Expand Up @@ -15,7 +15,6 @@
*/
package net.kuujo.copycat.log;

import com.typesafe.config.ConfigValueFactory;
import net.kuujo.copycat.util.internal.Assert;

import java.io.File;
Expand All @@ -29,6 +28,8 @@
public class FileLog extends Log {
private static final String FILE_LOG_DIRECTORY = "directory";

private static final String DEFAULT_FILE_LOG_DIRECTORY = System.getProperty("user.dir");

public FileLog() {
super();
}
Expand All @@ -37,18 +38,6 @@ public FileLog(Map<String, Object> config) {
super(config);
}

protected FileLog(Map<String, Object> config, String... resources) {
super(config, resources);
}

public FileLog(String resource) {
super(resource);
}

protected FileLog(String... resources) {
super(resources);
}

protected FileLog(FileLog log) {
super(log);
}
Expand All @@ -65,7 +54,7 @@ public FileLog copy() {
* @throws java.lang.NullPointerException If the directory is {@code null}
*/
public void setDirectory(String directory) {
this.config = config.withValue(FILE_LOG_DIRECTORY, ConfigValueFactory.fromAnyRef(Assert.notNull(directory, "directory")));
config.put(FILE_LOG_DIRECTORY, Assert.notNull(directory, "directory"));
}

/**
Expand All @@ -84,7 +73,8 @@ public void setDirectory(File directory) {
* @return The log directory.
*/
public File getDirectory() {
return new File(config.hasPath(FILE_LOG_DIRECTORY) ? config.getString(FILE_LOG_DIRECTORY) : System.getProperty("user.dir"));
String directory = (String) config.get(FILE_LOG_DIRECTORY);
return directory != null ? new File(directory) : new File(DEFAULT_FILE_LOG_DIRECTORY);
}

/**
Expand Down
16 changes: 3 additions & 13 deletions log/src/main/java/net/kuujo/copycat/log/Log.java
Expand Up @@ -28,26 +28,16 @@ protected Log() {
super();
}

protected Log(Map<String, Object> config, String... resources) {
super(config, resources);
protected Log(Map<String, Object> config) {
super(config);
}

protected Log(Log log) {
super(log);
}

protected Log(String resource) {
super(resource);
}

protected Log(String... resources) {
super(resources);
}

@Override
public Log copy() {
return (Log) super.copy();
}
public abstract Log copy();

@Override
public Log withSegmentSize(int segmentSize) {
Expand Down
56 changes: 31 additions & 25 deletions log/src/main/java/net/kuujo/copycat/log/LogConfig.java
Expand Up @@ -15,46 +15,45 @@
*/
package net.kuujo.copycat.log;

import com.typesafe.config.ConfigValueFactory;
import net.kuujo.copycat.util.AbstractConfigurable;
import net.kuujo.copycat.util.Configurable;
import net.kuujo.copycat.util.internal.Assert;

import java.util.HashMap;
import java.util.Map;

/**
* Log configuration.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public abstract class LogConfig extends AbstractConfigurable implements Configurable {
public abstract class LogConfig implements Configurable {
private static final String LOG_SEGMENT_SIZE = "segment.size";
private static final String LOG_SEGMENT_INTERVAL = "segment.interval";
private static final String LOG_FLUSH_ON_WRITE = "flush.on-write";
private static final String LOG_FLUSH_INTERVAL = "flush.interval";

private static final String DEFAULT_CONFIGURATION = "log-defaults";
private static final String CONFIGURATION = "log";
private static final int DEFAULT_LOG_SEGMENT_SIZE = 1024 * 1024 * 32;
private static final long DEFAULT_LOG_SEGMENT_INTERVAL = Long.MAX_VALUE;
private static final boolean DEFAULT_LOG_FLUSH_ON_WRITE = false;
private static final long DEFAULT_LOG_FLUSH_INTERVAL = Long.MAX_VALUE;

protected Map<String, Object> config;

protected LogConfig() {
super(CONFIGURATION, DEFAULT_CONFIGURATION);
this.config = new HashMap<>(128);
}

protected LogConfig(Map<String, Object> config, String... resources) {
super(config, AbstractConfigurable.addResources(resources, CONFIGURATION, DEFAULT_CONFIGURATION));
protected LogConfig(Map<String, Object> config) {
this.config = config;
}

protected LogConfig(LogConfig log) {
super(log);
}

protected LogConfig(String... resources) {
super(AbstractConfigurable.addResources(resources, CONFIGURATION, DEFAULT_CONFIGURATION));
this.config = new HashMap<>(log.toMap());
}

@Override
public LogConfig copy() {
return (LogConfig) super.copy();
public void configure(Map<String, Object> config) {
this.config = config;
}

/**
Expand All @@ -64,7 +63,7 @@ public LogConfig copy() {
* @throws java.lang.IllegalArgumentException If the segment size is not positive
*/
public void setSegmentSize(int segmentSize) {
this.config = config.withValue(LOG_SEGMENT_SIZE, ConfigValueFactory.fromAnyRef(Assert.arg(segmentSize, segmentSize > 0, "segment size must be positive")));
config.put(LOG_SEGMENT_SIZE, Assert.arg(segmentSize, Assert.POSITIVE, "segment size must be positive"));
}

/**
Expand All @@ -73,7 +72,8 @@ public void setSegmentSize(int segmentSize) {
* @return The log segment size in bytes.
*/
public int getSegmentSize() {
return config.getInt(LOG_SEGMENT_SIZE);
Integer segmentSize = (Integer) config.get(LOG_SEGMENT_SIZE);
return segmentSize != null ? segmentSize : DEFAULT_LOG_SEGMENT_SIZE;
}

/**
Expand All @@ -95,7 +95,7 @@ public LogConfig withSegmentSize(int segmentSize) {
* @throws java.lang.IllegalArgumentException If the segment interval is not positive
*/
public void setSegmentInterval(long segmentInterval) {
this.config = config.withValue(LOG_SEGMENT_INTERVAL, ConfigValueFactory.fromAnyRef(Assert.arg(segmentInterval, segmentInterval > 0, "segment interval must be positive")));
config.put(LOG_SEGMENT_INTERVAL, Assert.arg(segmentInterval, Assert.POSITIVE, "segment interval must be positive"));
}

/**
Expand All @@ -104,8 +104,8 @@ public void setSegmentInterval(long segmentInterval) {
* @return The log segment interval.
*/
public long getSegmentInterval() {
long interval = config.getLong(LOG_SEGMENT_INTERVAL);
return interval > -1 ? interval : Long.MAX_VALUE;
Long segmentInterval = (Long) config.get(LOG_SEGMENT_INTERVAL);
return segmentInterval != null ? segmentInterval : DEFAULT_LOG_SEGMENT_INTERVAL;
}

/**
Expand All @@ -126,7 +126,7 @@ public LogConfig withSegmentInterval(long segmentInterval) {
* @param flushOnWrite Whether to flush the log to disk on every write.
*/
public void setFlushOnWrite(boolean flushOnWrite) {
this.config = config.withValue(LOG_FLUSH_ON_WRITE, ConfigValueFactory.fromAnyRef(flushOnWrite));
config.put(LOG_FLUSH_ON_WRITE, flushOnWrite);
}

/**
Expand All @@ -135,7 +135,8 @@ public void setFlushOnWrite(boolean flushOnWrite) {
* @return Whether to flush the log to disk on every write.
*/
public boolean isFlushOnWrite() {
return config.getBoolean(LOG_FLUSH_ON_WRITE);
Boolean flushOnWrite = (Boolean) config.get(LOG_FLUSH_ON_WRITE);
return flushOnWrite != null ? flushOnWrite : DEFAULT_LOG_FLUSH_ON_WRITE;
}

/**
Expand All @@ -156,7 +157,7 @@ public LogConfig withFlushOnWrite(boolean flushOnWrite) {
* @throws java.lang.IllegalArgumentException If the flush interval is not positive
*/
public void setFlushInterval(long flushInterval) {
this.config = config.withValue(LOG_FLUSH_INTERVAL, ConfigValueFactory.fromAnyRef(Assert.arg(flushInterval, flushInterval > 0, "flush interval must be positive")));
config.put(LOG_FLUSH_INTERVAL, Assert.arg(flushInterval, Assert.POSITIVE, "flush interval must be positive"));
}

/**
Expand All @@ -165,8 +166,8 @@ public void setFlushInterval(long flushInterval) {
* @return The log flush interval.
*/
public long getFlushInterval() {
long interval = config.getLong(LOG_FLUSH_INTERVAL);
return interval > -1 ? interval : Long.MAX_VALUE;
Long flushInterval = (Long) config.get(LOG_FLUSH_INTERVAL);
return flushInterval != null ? flushInterval : DEFAULT_LOG_FLUSH_INTERVAL;
}

/**
Expand All @@ -181,4 +182,9 @@ public LogConfig withFlushInterval(long flushInterval) {
return this;
}

@Override
public Map<String, Object> toMap() {
return config;
}

}
11 changes: 1 addition & 10 deletions log/src/test/java/net/kuujo/copycat/log/BufferedLogTest.java
Expand Up @@ -17,7 +17,7 @@

import org.testng.annotations.Test;

import static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;

/**
* Buffered log test.
Expand All @@ -40,15 +40,6 @@ public void testConfigurationDefaults() throws Throwable {
assertEquals(log.getSegmentInterval(), 60000);
}

/**
* Tests configuring the buffered log via a configuration file.
*/
public void testConfigurationFile() throws Throwable {
Log log = new BufferedLog("log-test");
assertEquals(log.getSegmentSize(), 1024 * 1024);
assertEquals(log.getSegmentInterval(), 60000);
}

@Override
protected AbstractLogManager createLog() throws Throwable {
return (AbstractLogManager) new BufferedLog().withSegmentSize(segmentSize).getLogManager("test");
Expand Down
9 changes: 0 additions & 9 deletions log/src/test/java/net/kuujo/copycat/log/FileLogTest.java
Expand Up @@ -47,15 +47,6 @@ public void testConfigurationDefaults() throws Throwable {
assertEquals(log.getSegmentInterval(), 60000);
}

/**
* Tests configuring the buffered log via a configuration file.
*/
public void testConfigurationFile() throws Throwable {
Log log = new FileLog("log-test");
assertEquals(log.getSegmentSize(), 1024 * 1024);
assertEquals(log.getSegmentInterval(), 60000);
}

@AfterTest
protected void cleanLogDir() throws IOException {
Path directory = Paths.get("target/test-logs/");
Expand Down

0 comments on commit 6880233

Please sign in to comment.