Search in sources :

Example 6 with CommonsConfiguration

use of com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration in project titan by thinkaurelius.

the class TitanFactory method getLocalConfiguration.

//###################################
//          HELPER METHODS
//###################################
private static ReadConfiguration getLocalConfiguration(String shortcutOrFile) {
    File file = new File(shortcutOrFile);
    if (file.exists())
        return getLocalConfiguration(file);
    else {
        int pos = shortcutOrFile.indexOf(':');
        if (pos < 0)
            pos = shortcutOrFile.length();
        String backend = shortcutOrFile.substring(0, pos);
        Preconditions.checkArgument(StandardStoreManager.getAllManagerClasses().containsKey(backend.toLowerCase()), "Backend shorthand unknown: %s", backend);
        String secondArg = null;
        if (pos + 1 < shortcutOrFile.length())
            secondArg = shortcutOrFile.substring(pos + 1).trim();
        BaseConfiguration config = new BaseConfiguration();
        ModifiableConfiguration writeConfig = new ModifiableConfiguration(ROOT_NS, new CommonsConfiguration(config), BasicConfiguration.Restriction.NONE);
        writeConfig.set(STORAGE_BACKEND, backend);
        ConfigOption option = Backend.getOptionForShorthand(backend);
        if (option == null) {
            Preconditions.checkArgument(secondArg == null);
        } else if (option == STORAGE_DIRECTORY || option == STORAGE_CONF_FILE) {
            Preconditions.checkArgument(StringUtils.isNotBlank(secondArg), "Need to provide additional argument to initialize storage backend");
            writeConfig.set(option, getAbsolutePath(secondArg));
        } else if (option == STORAGE_HOSTS) {
            Preconditions.checkArgument(StringUtils.isNotBlank(secondArg), "Need to provide additional argument to initialize storage backend");
            writeConfig.set(option, new String[] { secondArg });
        } else
            throw new IllegalArgumentException("Invalid configuration option for backend " + option);
        return new CommonsConfiguration(config);
    }
}
Also used : BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) File(java.io.File)

Example 7 with CommonsConfiguration

use of com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration in project titan by thinkaurelius.

the class TitanFactory method getLocalConfiguration.

/**
     * Load a properties file containing a Titan graph configuration.
     * <p/>
     * <ol>
     * <li>Load the file contents into a {@link org.apache.commons.configuration.PropertiesConfiguration}</li>
     * <li>For each key that points to a configuration object that is either a directory
     * or local file, check
     * whether the associated value is a non-null, non-absolute path. If so,
     * then prepend the absolute path of the parent directory of the provided configuration {@code file}.
     * This has the effect of making non-absolute backend
     * paths relative to the config file's directory rather than the JVM's
     * working directory.
     * <li>Return the {@link ReadConfiguration} for the prepared configuration file</li>
     * </ol>
     * <p/>
     *
     * @param file A properties file to load
     * @return A configuration derived from {@code file}
     */
@SuppressWarnings("unchecked")
private static ReadConfiguration getLocalConfiguration(File file) {
    Preconditions.checkArgument(file != null && file.exists() && file.isFile() && file.canRead(), "Need to specify a readable configuration file, but was given: %s", file.toString());
    try {
        PropertiesConfiguration configuration = new PropertiesConfiguration(file);
        final File tmpParent = file.getParentFile();
        final File configParent;
        if (null == tmpParent) {
            /*
                 * null usually means we were given a Titan config file path
                 * string like "foo.properties" that refers to the current
                 * working directory of the process.
                 */
            configParent = new File(System.getProperty("user.dir"));
        } else {
            configParent = tmpParent;
        }
        Preconditions.checkNotNull(configParent);
        Preconditions.checkArgument(configParent.isDirectory());
        // TODO this mangling logic is a relic from the hardcoded string days; it should be deleted and rewritten as a setting on ConfigOption
        final Pattern p = Pattern.compile("(" + Pattern.quote(STORAGE_NS.getName()) + "\\..*" + "(" + Pattern.quote(STORAGE_DIRECTORY.getName()) + "|" + Pattern.quote(STORAGE_CONF_FILE.getName()) + ")" + "|" + Pattern.quote(INDEX_NS.getName()) + "\\..*" + "(" + Pattern.quote(INDEX_DIRECTORY.getName()) + "|" + Pattern.quote(INDEX_CONF_FILE.getName()) + ")" + ")");
        final Iterator<String> keysToMangle = Iterators.filter(configuration.getKeys(), new Predicate<String>() {

            @Override
            public boolean apply(String key) {
                if (null == key)
                    return false;
                return p.matcher(key).matches();
            }
        });
        while (keysToMangle.hasNext()) {
            String k = keysToMangle.next();
            Preconditions.checkNotNull(k);
            String s = configuration.getString(k);
            Preconditions.checkArgument(StringUtils.isNotBlank(s), "Invalid Configuration: key %s has null empty value", k);
            configuration.setProperty(k, getAbsolutePath(configParent, s));
        }
        return new CommonsConfiguration(configuration);
    } catch (ConfigurationException e) {
        throw new IllegalArgumentException("Could not load configuration at: " + file, e);
    }
}
Also used : Pattern(java.util.regex.Pattern) ConfigurationException(org.apache.commons.configuration.ConfigurationException) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) File(java.io.File)

Example 8 with CommonsConfiguration

use of com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration in project titan by thinkaurelius.

the class ElasticSearchConfigTest method testLocalNodeUsingExt.

@Test
public void testLocalNodeUsingExt() throws BackendException, InterruptedException {
    String baseDir = Joiner.on(File.separator).join("target", "es", "jvmlocal_ext");
    assertFalse(new File(baseDir + File.separator + "data").exists());
    CommonsConfiguration cc = new CommonsConfiguration(new BaseConfiguration());
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.node.data", "true");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.node.client", "false");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.node.local", "true");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.path.data", baseDir + File.separator + "data");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.path.work", baseDir + File.separator + "work");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.path.logs", baseDir + File.separator + "logs");
    ModifiableConfiguration config = new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, cc, BasicConfiguration.Restriction.NONE);
    config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME);
    Configuration indexConfig = config.restrictTo(INDEX_NAME);
    IndexProvider idx = new ElasticSearchIndex(indexConfig);
    simpleWriteAndQuery(idx);
    idx.close();
    assertTrue(new File(baseDir + File.separator + "data").exists());
}
Also used : BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) Configuration(com.thinkaurelius.titan.diskstorage.configuration.Configuration) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) BasicConfiguration(com.thinkaurelius.titan.diskstorage.configuration.BasicConfiguration) GraphDatabaseConfiguration(com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) ModifiableConfiguration(com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) ModifiableConfiguration(com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration) ElasticSearchIndex(com.thinkaurelius.titan.diskstorage.es.ElasticSearchIndex) File(java.io.File) Test(org.junit.Test)

Example 9 with CommonsConfiguration

use of com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration in project titan by thinkaurelius.

the class ConfigurationLint method validate.

public static Status validate(String filename) throws IOException {
    Properties p = new Properties();
    FileInputStream fis = new FileInputStream(filename);
    p.load(fis);
    fis.close();
    final PropertiesConfiguration apc;
    try {
        apc = new PropertiesConfiguration(filename);
    } catch (ConfigurationException e) {
        throw new IOException(e);
    }
    //        new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS,
    //                , BasicConfiguration.Restriction.NONE);
    Iterator<String> iter = apc.getKeys();
    int totalKeys = 0;
    int keysVerified = 0;
    while (iter.hasNext()) {
        totalKeys++;
        String key = iter.next();
        String value = apc.getString(key);
        try {
            ConfigElement.PathIdentifier pid = ConfigElement.parse(GraphDatabaseConfiguration.ROOT_NS, key);
            // ConfigElement shouldn't return null; failure here probably relates to titan-core, not the file
            Preconditions.checkState(null != pid);
            Preconditions.checkState(null != pid.element);
            if (!pid.element.isOption()) {
                log.warn("Config key {} is a namespace (only options can be keys)", key);
                continue;
            }
            final ConfigOption<?> opt;
            try {
                opt = (ConfigOption<?>) pid.element;
            } catch (RuntimeException re) {
                // This shouldn't happen given the preceding check, but catch it anyway
                log.warn("Config key {} maps to the element {}, but it could not be cast to an option", key, pid.element, re);
                continue;
            }
            try {
                Object o = new CommonsConfiguration(apc).get(key, opt.getDatatype());
                opt.verify(o);
                keysVerified++;
            } catch (RuntimeException re) {
                log.warn("Config key {} is recognized, but its value {} could not be validated", key, value);
                log.debug("Validation exception on {}={} follows", key, value, re);
            }
        } catch (RuntimeException re) {
            log.warn("Unknown config key {}", key);
        }
    }
    return new Status(totalKeys, totalKeys - keysVerified);
}
Also used : ConfigElement(com.thinkaurelius.titan.diskstorage.configuration.ConfigElement) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) IOException(java.io.IOException) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) FileInputStream(java.io.FileInputStream) ConfigurationException(org.apache.commons.configuration.ConfigurationException)

Example 10 with CommonsConfiguration

use of com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration in project titan by thinkaurelius.

the class ElasticSearchConfigTest method testNetworkNodeUsingExt.

@Test
public void testNetworkNodeUsingExt() throws BackendException, InterruptedException {
    ElasticsearchRunner esr = new ElasticsearchRunner(".", "networkNodeUsingExt.yml");
    esr.start();
    CommonsConfiguration cc = new CommonsConfiguration(new BaseConfiguration());
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.node.data", "false");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.node.client", "true");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.cluster.name", "networkNodeUsingExt");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.discovery.zen.ping.multicast.enabled", "false");
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.discovery.zen.ping.unicast.hosts", "localhost,127.0.0.1:9300");
    ModifiableConfiguration config = new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, cc, BasicConfiguration.Restriction.NONE);
    config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME);
    Configuration indexConfig = config.restrictTo(INDEX_NAME);
    IndexProvider idx = new ElasticSearchIndex(indexConfig);
    simpleWriteAndQuery(idx);
    idx.close();
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.discovery.zen.ping.unicast.hosts", "10.11.12.13");
    config = new ModifiableConfiguration(GraphDatabaseConfiguration.ROOT_NS, cc, BasicConfiguration.Restriction.NONE);
    config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME);
    config.set(HEALTH_REQUEST_TIMEOUT, "5s", INDEX_NAME);
    indexConfig = config.restrictTo(INDEX_NAME);
    Throwable failure = null;
    try {
        idx = new ElasticSearchIndex(indexConfig);
    } catch (Throwable t) {
        failure = t;
    }
    // idx.close();
    Assert.assertNotNull("ES client failed to throw exception on connection failure", failure);
    esr.stop();
}
Also used : BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) Configuration(com.thinkaurelius.titan.diskstorage.configuration.Configuration) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) BasicConfiguration(com.thinkaurelius.titan.diskstorage.configuration.BasicConfiguration) GraphDatabaseConfiguration(com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration) BaseConfiguration(org.apache.commons.configuration.BaseConfiguration) ModifiableConfiguration(com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration) CommonsConfiguration(com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration) ModifiableConfiguration(com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration) ElasticSearchIndex(com.thinkaurelius.titan.diskstorage.es.ElasticSearchIndex) Test(org.junit.Test)

Aggregations

CommonsConfiguration (com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration)11 BaseConfiguration (org.apache.commons.configuration.BaseConfiguration)8 Test (org.junit.Test)6 BasicConfiguration (com.thinkaurelius.titan.diskstorage.configuration.BasicConfiguration)5 ModifiableConfiguration (com.thinkaurelius.titan.diskstorage.configuration.ModifiableConfiguration)5 GraphDatabaseConfiguration (com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration)5 Configuration (com.thinkaurelius.titan.diskstorage.configuration.Configuration)4 ElasticSearchIndex (com.thinkaurelius.titan.diskstorage.es.ElasticSearchIndex)4 File (java.io.File)4 ConfigurationException (org.apache.commons.configuration.ConfigurationException)2 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 TitanGraph (com.thinkaurelius.titan.core.TitanGraph)1 ConfigElement (com.thinkaurelius.titan.diskstorage.configuration.ConfigElement)1 WriteConfiguration (com.thinkaurelius.titan.diskstorage.configuration.WriteConfiguration)1 StandardTitanGraph (com.thinkaurelius.titan.graphdb.database.StandardTitanGraph)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Duration (java.time.Duration)1 ChronoUnit (java.time.temporal.ChronoUnit)1