Skip to content
Permalink
Browse files

Refactored configs some more so that the LocalFileSystem Repo is cons…

…istent with the rest.
  • Loading branch information...
FelixGV committed Nov 7, 2014
1 parent b636e57 commit 35f01e4b4b2de9d39065d4fd4d8a6873a2074546
@@ -39,17 +39,17 @@ The schema repo gets configured via a .properties file passed as the first comma

All configuration properties are injected via Guice. However, you are not obligated to use Guice if you do not wish to. You can also feed the required properties to the various constructors directly by code, if you wish to wire in your own config management solution.

### File-based Backend
### Local File System Backend

The file-based backend is a single node, persistent, implementation. For production usage, it is recommended to at least use this backend, and not the in-memory one, otherwise a server shutdown or crash will result in the loss of all of its state. This file-based backend, however, is not considered highly-available nor fault-tolerant, unless you can somehow set its storage path to be on a mounted file-system that you would consider to be highly-available.
The local file system backend is a single node, persistent, implementation. For production usage, it is recommended to at least use this backend, and not the in-memory one, otherwise a server shutdown or crash will result in the loss of all of its state. This file-based backend, however, is not considered highly-available nor fault-tolerant, unless you can somehow set its storage path to be on a mounted file system that you would consider to be highly-available.

In order to use the file-based backend, set these configuration properties:

# FQCN of the file-based backend:
schema-repo.class=org.schemarepo.FileRepository
schema-repo.class=org.schemarepo.LocalFileSystemRepository

# Relative or absolute path to where you wish to store the state of the repo:
schema-repo.schema-repo.file-repo-path=relative/path/to/storage/directory/
schema-repo.local-file-system.path=relative/path/to/storage/directory/

### ZooKeeper Backend

@@ -1,3 +1,2 @@
schema-repo.class=org.schemarepo.InMemoryRepository
#schema-repo.cached=org.schemarepo.CacheRepository
schema-repo.file-repo-path=target/data/
@@ -0,0 +1,3 @@
schema-repo.class=org.schemarepo.LocalFileSystemRepository
#schema-repo.cached=org.schemarepo.CacheRepository
schema-repo.local-file-system.path=target/data/
@@ -18,6 +18,8 @@

package org.schemarepo;

import org.schemarepo.config.ConfigKeys;

import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
@@ -62,7 +64,7 @@
* the name of which is the schema id followed by the postfix '.schema'.</li>
*
*/
public class FileRepository implements Repository, Closeable {
public class LocalFileSystemRepository implements Repository, Closeable {

private static final String LOCKFILE = ".repo.lock";
private static final String SUBJECT_PROPERTIES = "subject.properties";
@@ -77,18 +79,17 @@
private boolean closed = false;

/**
* Create a FileRepository in the directory path provided. Locks a file
* Create a LocalFileSystemRepository in the directory path provided. Locks a file
* "repository.lock" to ensure no other object or process is running a
* FileRepository from the same place. The lock is released if
* LocalFileSystemRepository from the same place. The lock is released if
* {@link #close()} is called, the object is finalized, or the JVM exits.
*
* Not all platforms support file locks. See {@link FileLock}
*
* @param repoPath
* The
* @param repoPath The path where to store the Repository's state
*/
@Inject
public FileRepository(@Named("schema-repo.file-repo-path") String repoPath, ValidatorFactory validators) {
public LocalFileSystemRepository(@Named(ConfigKeys.LOCAL_FILE_SYSTEM_PATH) String repoPath, ValidatorFactory validators) {
this.validators = validators;
this.rootDir = new File(repoPath);
if ((!rootDir.exists() && !rootDir.mkdirs()) || !rootDir.isDirectory()) {
@@ -128,7 +129,7 @@ private void loadSubjects(File repoDir, SubjectCache subjects) {

private void isValid() {
if (closed) {
throw new IllegalStateException("FileRepository is closed");
throw new IllegalStateException("LocalFileSystemRepository is closed");
}
}

@@ -0,0 +1,33 @@
package org.schemarepo.config;

/**
* Convenience class for importing the configuration keys for specific dependencies.
*/
public class ConfigKeys {
// General Schema Repo configs
private static final String GLOBAL_PREFIX = "schema-repo.";
public static final String REPO_CLASS = GLOBAL_PREFIX + "class";
public static final String REPO_CACHE = GLOBAL_PREFIX + "cache";
public static final String VALIDATOR_PREFIX = GLOBAL_PREFIX + "validator.";

// Jetty configs
private static final String JETTY_PREFIX = GLOBAL_PREFIX + "jetty.";
public static final String JETTY_HOST = JETTY_PREFIX + "host";
public static final String JETTY_PORT = JETTY_PREFIX + "port";
public static final String JETTY_PATH = JETTY_PREFIX + "path";
public static final String JETTY_HEADER_SIZE = JETTY_PREFIX + "header.size";
public static final String JETTY_BUFFER_SIZE = JETTY_PREFIX + "buffer.size";

// Local file system backend configs
private static final String LOCAL_FILE_SYSTEM_PREFIX = GLOBAL_PREFIX + "local-file-system.";
public static final String LOCAL_FILE_SYSTEM_PATH = LOCAL_FILE_SYSTEM_PREFIX + "path";

// ZooKeeper backend configs
private static final String ZK_PREFIX = GLOBAL_PREFIX + "zookeeper.";
public static final String ZK_ENSEMBLE = ZK_PREFIX + "ensemble";
public static final String ZK_PATH_PREFIX = ZK_PREFIX + "path-prefix";
public static final String ZK_SESSION_TIMEOUT = ZK_PREFIX + "session-timeout";
public static final String ZK_CONNECTION_TIMEOUT = ZK_PREFIX + "connection-timeout";
public static final String ZK_CURATOR_SLEEP_TIME_BETWEEN_RETRIES = ZK_PREFIX + "curator.sleep-time-between-retries";
public static final String ZK_CURATOR_NUMBER_OF_RETRIES = ZK_PREFIX + "curator.number-of-retries";
}
@@ -29,9 +29,9 @@
import org.junit.BeforeClass;
import org.junit.Test;

public class TestFileRepository extends AbstractTestRepository<FileRepository> {
private static final String TEST_PATH = "target/test/TestFileRepository-paths/";
private static final String REPO_PATH = "target/test/TestFileRepository/";
public class TestLocalFileSystemRepository extends AbstractTestRepository<LocalFileSystemRepository> {
private static final String TEST_PATH = "target/test/TestLocalFileSystemRepository-paths/";
private static final String REPO_PATH = "target/test/TestLocalFileSystemRepository/";

@BeforeClass
public static void setup() {
@@ -45,14 +45,14 @@ public void cleanUp() throws IOException {
}

@Override
protected FileRepository createRepository() {
protected LocalFileSystemRepository createRepository() {
// Clean up the repo's content before giving it out
rmDir(new File(REPO_PATH));
return newRepo(REPO_PATH);
}

private FileRepository newRepo(String path) {
return new FileRepository(path, new ValidatorFactory.Builder().build());
private LocalFileSystemRepository newRepo(String path) {
return new LocalFileSystemRepository(path, new ValidatorFactory.Builder().build());
}

@Test
@@ -62,7 +62,7 @@ public void testPathHandling() throws SchemaValidationException {
"/tmp/file_repo/", "/tmp/file_repo/" };

for (String path : paths) {
FileRepository r = newRepo(TEST_PATH + path);
LocalFileSystemRepository r = newRepo(TEST_PATH + path);
try {
File expected = new File(TEST_PATH, path);
assertTrue("Expected directory not created: " +
@@ -81,7 +81,7 @@ public void testPathHandling() throws SchemaValidationException {
@Test
public void testReadWritten() throws SchemaValidationException {
String path = TEST_PATH + "/readWrite";
FileRepository r = newRepo(path);
LocalFileSystemRepository r = newRepo(path);
try {
r.register("sub1", null).register("sc1");
r.register("sub2", null).register("sc2");
@@ -122,7 +122,7 @@ public void testReadWrittenMultiLineSchema() throws SchemaValidationException {
String multiLineSchema1 = "first line" + endOfLine + "second line";
String multiLineSchema2 = "first line" + endOfLine + "second line" + endOfLine;

FileRepository r = newRepo(path);
LocalFileSystemRepository r = newRepo(path);
try {
r.register("sub1", null).register(multiLineSchema1);
r.register("sub1", null).register(multiLineSchema2);
@@ -162,13 +162,13 @@ public void testInvalidDir() throws IOException {
String badPath = TEST_PATH + "/bad";
new File(TEST_PATH).mkdirs();
new File(badPath).createNewFile();
FileRepository r = newRepo(badPath);
LocalFileSystemRepository r = newRepo(badPath);
r.close();
}

@Test(expected = IllegalStateException.class)
public void testCantUseClosedRepo() {
FileRepository r = newRepo(TEST_PATH + "/tmp/repo");
LocalFileSystemRepository r = newRepo(TEST_PATH + "/tmp/repo");
r.close();
r.lookup("nothing");
}
25 run.sh
@@ -2,15 +2,24 @@

cd `dirname "$0"` # connect to root

function usage() {
echo "Usage: $0 <backend-type>"
echo ""
echo "backend-type : Specify which backend to run. Valid values are: in-memory, file-system and zookeeper."
exit 1
}

if [[ $# == 0 ]]; then
echo "Starting Schema Repo Server"
java -cp bundle/target/schema-repo-bundle-*-withdeps.jar org.schemarepo.server.RepositoryServer bundle/config/config.properties
elif [[ $1 == '--zk' ]]; then
echo "Starting Schema Repo Server on ZK"
usage
elif [[ $1 == 'in-memory' ]]; then
echo "Starting Schema Repo Server with in In-Memory backend"
java -cp bundle/target/schema-repo-bundle-*-withdeps.jar org.schemarepo.server.RepositoryServer bundle/config/in-memory-config.properties
elif [[ $1 == 'file-system' ]]; then
echo "Starting Schema Repo Server with local file system backend"
java -cp bundle/target/schema-repo-bundle-*-withdeps.jar org.schemarepo.server.RepositoryServer bundle/config/local-file-system-config.properties
elif [[ $1 == 'zookeeper' ]]; then
echo "Starting Schema Repo Server with ZooKeeper backend"
java -cp zk-bundle/target/schema-repo-zk-bundle-*-withdeps.jar org.schemarepo.server.RepositoryServer zk-bundle/config/config.properties
else
echo "Usage: $0 [--zk]"
echo ""
echo "--zk Use this flag to run the ZK-backed repo."
exit 1
usage
fi
@@ -1,4 +1,4 @@
package org.schemarepo.server;
package org.schemarepo.config;

import java.io.PrintStream;
import java.util.Properties;
@@ -36,7 +36,7 @@
* will set a validator named "backwards_compatible" to an instance of the
* class com.foo.BackwardsCompatible.
*/
class ConfigModule implements Module {
public class ConfigModule implements Module {

private static final Properties DEFAULTS = new Properties();
static {

This file was deleted.

Oops, something went wrong.
@@ -39,6 +39,8 @@
import com.google.inject.servlet.GuiceFilter;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
import org.schemarepo.config.ConfigKeys;
import org.schemarepo.config.ConfigModule;

/**
* A {@link RepositoryServer} is a stand-alone server for running a
@@ -1,4 +1,4 @@
package org.schemarepo.server;
package org.schemarepo.config;

import java.util.Properties;

@@ -31,7 +31,7 @@
import org.schemarepo.Subject;
import org.schemarepo.SubjectConfig;
import org.schemarepo.ValidatorFactory;
import org.schemarepo.server.ConfigKeys;
import org.schemarepo.config.ConfigKeys;

/**
* This {@link org.schemarepo.Repository} implementation stores its state using Zookeeper.

0 comments on commit 35f01e4

Please sign in to comment.
You can’t perform that action at this time.