private Log openLog(String logManagerName, String logName) {
   try {
     ModifiableConfiguration configuration =
         new ModifiableConfiguration(
             GraphDatabaseConfiguration.ROOT_NS,
             config.copy(),
             BasicConfiguration.Restriction.NONE);
     configuration.set(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID, "reader");
     configuration.set(
         GraphDatabaseConfiguration.LOG_READ_INTERVAL, Duration.ofMillis(500L), logManagerName);
     if (logStoreManager == null) {
       logStoreManager = Backend.getStorageManager(configuration);
     }
     StoreFeatures f = logStoreManager.getFeatures();
     boolean part = f.isDistributed() && f.isKeyOrdered();
     if (part) {
       for (String logname : new String[] {USER_LOG, TRANSACTION_LOG, MANAGEMENT_LOG})
         configuration.set(KCVSLogManager.LOG_MAX_PARTITIONS, 8, logname);
     }
     assert logStoreManager != null;
     if (!logManagers.containsKey(logManagerName)) {
       // Open log manager - only supports KCVSLog
       Configuration logConfig = configuration.restrictTo(logManagerName);
       Preconditions.checkArgument(
           logConfig.get(LOG_BACKEND).equals(LOG_BACKEND.getDefaultValue()));
       logManagers.put(logManagerName, new KCVSLogManager(logStoreManager, logConfig));
     }
     assert logManagers.containsKey(logManagerName);
     return logManagers.get(logManagerName).openLog(logName);
   } catch (BackendException e) {
     throw new TitanException("Could not open log: " + logName, e);
   }
 }
  @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());
  }
 public void clopen(Object... settings) {
   config = getConfiguration();
   if (mgmt != null && mgmt.isOpen()) mgmt.rollback();
   if (null != tx && tx.isOpen()) tx.commit();
   if (settings != null && settings.length > 0) {
     Map<TestConfigOption, Object> options = validateConfigOptions(settings);
     TitanManagement gconf = null;
     ModifiableConfiguration lconf =
         new ModifiableConfiguration(
             GraphDatabaseConfiguration.ROOT_NS, config, BasicConfiguration.Restriction.LOCAL);
     for (Map.Entry<TestConfigOption, Object> option : options.entrySet()) {
       if (option.getKey().option.isLocal()) {
         lconf.set(option.getKey().option, option.getValue(), option.getKey().umbrella);
       } else {
         if (gconf == null) gconf = graph.openManagement();
         gconf.set(
             ConfigElement.getPath(option.getKey().option, option.getKey().umbrella),
             option.getValue());
       }
     }
     if (gconf != null) gconf.commit();
     lconf.close();
   }
   if (null != graph && graph.isOpen()) graph.close();
   Preconditions.checkNotNull(config);
   open(config);
 }
 public static void clearGraph(WriteConfiguration config) throws BackendException {
   ModifiableConfiguration adjustedConfig =
       new ModifiableConfiguration(
           GraphDatabaseConfiguration.ROOT_NS, config.copy(), BasicConfiguration.Restriction.NONE);
   adjustedConfig.set(GraphDatabaseConfiguration.LOCK_LOCAL_MEDIATOR_GROUP, "tmp");
   adjustedConfig.set(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID, "inst");
   Backend backend = new Backend(adjustedConfig);
   backend.initialize(adjustedConfig);
   backend.clearStorage();
 }
 @Override
 public LogManager openLogManager(String senderId, boolean requiresOrderPreserving)
     throws BackendException {
   storeManager = openStorageManager();
   ModifiableConfiguration config = GraphDatabaseConfiguration.buildGraphConfiguration();
   config.set(GraphDatabaseConfiguration.UNIQUE_INSTANCE_ID, senderId);
   config.set(GraphDatabaseConfiguration.LOG_READ_INTERVAL, Duration.ofMillis(500L), LOG_NAME);
   // To ensure that the write order is preserved in reading, we need to ensure that all writes go
   // to the same partition
   // otherwise readers will independently read from the partitions out-of-order by design to avoid
   // having to synchronize
   config.set(KCVSLogManager.LOG_FIXED_PARTITION, requiresOrderPreserving, LOG_NAME);
   return new KCVSLogManager(storeManager, config.restrictTo(LOG_NAME));
 }
  @Test
  public void testNetworkNodeUsingYaml() throws BackendException, InterruptedException {
    ElasticsearchRunner esr = new ElasticsearchRunner(".", "networkNodeUsingYaml.yml");
    esr.start();
    ModifiableConfiguration config = GraphDatabaseConfiguration.buildGraphConfiguration();
    config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME);
    config.set(
        INDEX_CONF_FILE,
        Joiner.on(File.separator).join("target", "test-classes", "es_cfg_nodeclient.yml"),
        INDEX_NAME);
    Configuration indexConfig = config.restrictTo(INDEX_NAME);
    IndexProvider idx = new ElasticSearchIndex(indexConfig);
    simpleWriteAndQuery(idx);
    idx.close();

    config = GraphDatabaseConfiguration.buildGraphConfiguration();
    config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME);
    config.set(HEALTH_REQUEST_TIMEOUT, "5s", INDEX_NAME);
    config.set(
        INDEX_CONF_FILE,
        Joiner.on(File.separator).join("target", "test-classes", "es_cfg_bogus_nodeclient.yml"),
        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();
  }
  @Test
  public void testTransportClient() throws BackendException, InterruptedException {
    ElasticsearchRunner esr = new ElasticsearchRunner(".", "transportClient.yml");
    esr.start();
    ModifiableConfiguration config = GraphDatabaseConfiguration.buildGraphConfiguration();
    config.set(INTERFACE, ElasticSearchSetup.TRANSPORT_CLIENT.toString(), INDEX_NAME);
    config.set(INDEX_HOSTS, new String[] {"127.0.0.1"}, INDEX_NAME);
    Configuration indexConfig = config.restrictTo(INDEX_NAME);
    IndexProvider idx = new ElasticSearchIndex(indexConfig);
    simpleWriteAndQuery(idx);
    idx.close();

    config = GraphDatabaseConfiguration.buildGraphConfiguration();
    config.set(INTERFACE, ElasticSearchSetup.TRANSPORT_CLIENT.toString(), INDEX_NAME);
    config.set(INDEX_HOSTS, new String[] {"10.11.12.13"}, 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();
  }
  @Test
  public void testLocalNodeUsingYaml() throws BackendException, InterruptedException {

    String baseDir = Joiner.on(File.separator).join("target", "es", "jvmlocal_yml");

    assertFalse(new File(baseDir + File.separator + "data").exists());

    ModifiableConfiguration config = GraphDatabaseConfiguration.buildGraphConfiguration();
    config.set(INTERFACE, ElasticSearchSetup.NODE.toString(), INDEX_NAME);
    config.set(
        INDEX_CONF_FILE,
        Joiner.on(File.separator).join("target", "test-classes", "es_jvmlocal.yml"),
        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());
  }
  @Test
  public void testIndexCreationOptions() throws InterruptedException, BackendException {
    final int shards = 77;

    ElasticsearchRunner esr = new ElasticsearchRunner(".", "indexCreationOptions.yml");
    esr.start();
    CommonsConfiguration cc = new CommonsConfiguration(new BaseConfiguration());
    cc.set(
        "index." + INDEX_NAME + ".elasticsearch.create.ext.number_of_shards",
        String.valueOf(shards));
    cc.set("index." + INDEX_NAME + ".elasticsearch.ext.cluster.name", "indexCreationOptions");
    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);

    ImmutableSettings.Builder settingsBuilder = ImmutableSettings.settingsBuilder();
    settingsBuilder.put("discovery.zen.ping.multicast.enabled", "false");
    settingsBuilder.put("discovery.zen.ping.unicast.hosts", "localhost,127.0.0.1:9300");
    settingsBuilder.put("cluster.name", "indexCreationOptions");
    NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().settings(settingsBuilder.build());
    nodeBuilder.client(true).data(false).local(false);
    Node n = nodeBuilder.build().start();

    GetSettingsResponse response =
        n.client()
            .admin()
            .indices()
            .getSettings(new GetSettingsRequest().indices("titan"))
            .actionGet();
    assertEquals(String.valueOf(shards), response.getSetting("titan", "index.number_of_shards"));

    idx.close();
    n.stop();
    esr.stop();
  }
  @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();
  }
 public WriteConfiguration getConfiguration() {
   ModifiableConfiguration config = GraphDatabaseConfiguration.buildGraphConfiguration();
   config.set(GraphDatabaseConfiguration.STORAGE_BACKEND, "inmemory");
   return config.getConfiguration();
 }