Java Code Examples for org.elasticsearch.node.Node

The following are top voted examples for showing how to use org.elasticsearch.node.Node. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: log4j2-elasticsearch   File: BulkProcessorObjectFactoryTest.java   Source Code and License 8 votes vote down vote up
public EmbeddedElasticsearchServer(String storagePath) {
    this.storagePath = storagePath;
    try {
        tempFile = File.createTempFile("elasticsearch", "test");
        this.storagePath = tempFile.getParent();
        tempFile.deleteOnExit();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Settings.Builder elasticsearchSettings = Settings.builder()
            .put("http.enabled", "false")
            .put("path.data", this.storagePath)
            .put("path.home", System.getProperty("user.dir"))
            .put("transport.type", "local");

    node = new Node(elasticsearchSettings.build());
    client = node.client();
}
 
Example 2
Project: elasticsearch_my   File: TribeUnitTests.java   Source Code and License 7 votes vote down vote up
private static void assertTribeNodeSuccessfullyCreated(Settings extraSettings) throws Exception {
    //The tribe clients do need it to make sure they can find their corresponding tribes using the proper transport
    Settings settings = Settings.builder().put(NetworkModule.HTTP_ENABLED.getKey(), false).put("node.name", "tribe_node")
            .put("transport.type", MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME).put("discovery.type", "local")
            .put("tribe.t1.transport.type", MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME)
            .put("tribe.t2.transport.type",MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME)
            .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
            .put(extraSettings).build();

    try (Node node = new MockNode(settings, Arrays.asList(MockTcpTransportPlugin.class, TestZenDiscovery.TestPlugin.class)).start()) {
        try (Client client = node.client()) {
            assertBusy(() -> {
                ClusterState state = client.admin().cluster().prepareState().clear().setNodes(true).get().getState();
                assertThat(state.getClusterName().value(), equalTo("tribe_node_cluster"));
                assertThat(state.getNodes().getSize(), equalTo(5));
                for (DiscoveryNode discoveryNode : state.getNodes()) {
                    assertThat(discoveryNode.getName(), either(equalTo("tribe1_node")).or(equalTo("tribe2_node"))
                            .or(equalTo("tribe_node")).or(equalTo("tribe_node/t1")).or(equalTo("tribe_node/t2")));
                }
            });
        }
    }
}
 
Example 3
Project: elasticsearch_my   File: SimpleNetty4TransportTests.java   Source Code and License 6 votes vote down vote up
public void testBindUnavailableAddress() {
    // this is on a lower level since it needs access to the TransportService before it's started
    int port = serviceA.boundAddress().publishAddress().getPort();
    Settings settings = Settings.builder()
        .put(Node.NODE_NAME_SETTING.getKey(), "foobar")
        .put(TransportService.TRACE_LOG_INCLUDE_SETTING.getKey(), "")
        .put(TransportService.TRACE_LOG_EXCLUDE_SETTING.getKey(), "NOTHING")
        .put("transport.tcp.port", port)
        .build();
    ClusterSettings clusterSettings = new ClusterSettings(settings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
    BindTransportException bindTransportException = expectThrows(BindTransportException.class, () -> {
        MockTransportService transportService = nettyFromThreadPool(settings, threadPool, Version.CURRENT, clusterSettings, true);
        try {
            transportService.start();
        } finally {
            transportService.stop();
            transportService.close();
        }
    });
    assertEquals("Failed to bind to ["+ port + "]", bindTransportException.getMessage());
}
 
Example 4
Project: elasticsearch_my   File: EvilLoggerTests.java   Source Code and License 6 votes vote down vote up
public void testProperties() throws IOException, UserException {
    final Settings.Builder builder = Settings.builder().put("cluster.name", randomAsciiOfLength(16));
    if (randomBoolean()) {
        builder.put("node.name", randomAsciiOfLength(16));
    }
    final Settings settings = builder.build();
    setupLogging("minimal", settings);

    assertNotNull(System.getProperty("es.logs.base_path"));

    assertThat(System.getProperty("es.logs.cluster_name"), equalTo(ClusterName.CLUSTER_NAME_SETTING.get(settings).value()));
    if (Node.NODE_NAME_SETTING.exists(settings)) {
        assertThat(System.getProperty("es.logs.node_name"), equalTo(Node.NODE_NAME_SETTING.get(settings)));
    } else {
        assertNull(System.getProperty("es.logs.node_name"));
    }
}
 
Example 5
Project: elasticsearch_my   File: AbstractSimpleTransportTestCase.java   Source Code and License 6 votes vote down vote up
private MockTransportService buildService(final String name, final Version version, ClusterSettings clusterSettings,
                                          Settings settings, boolean acceptRequests, boolean doHandshake) {
    MockTransportService service = build(
        Settings.builder()
            .put(settings)
            .put(Node.NODE_NAME_SETTING.getKey(), name)
            .put(TransportService.TRACE_LOG_INCLUDE_SETTING.getKey(), "")
            .put(TransportService.TRACE_LOG_EXCLUDE_SETTING.getKey(), "NOTHING")
            .build(),
        version,
        clusterSettings, doHandshake);
    if (acceptRequests) {
        service.acceptIncomingRequests();
    }
    return service;
}
 
Example 6
Project: elasticsearch_my   File: InternalTestCluster.java   Source Code and License 6 votes vote down vote up
/**
 * returns a suffix string based on the node role. If no explicit role is defined, the suffix will be empty
 */
private String getRoleSuffix(Settings settings) {
    String suffix = "";
    if (Node.NODE_MASTER_SETTING.exists(settings) && Node.NODE_MASTER_SETTING.get(settings)) {
        suffix = suffix + Role.MASTER.getAbbreviation();
    }
    if (Node.NODE_DATA_SETTING.exists(settings) && Node.NODE_DATA_SETTING.get(settings)) {
        suffix = suffix + Role.DATA.getAbbreviation();
    }
    if (Node.NODE_MASTER_SETTING.exists(settings) && Node.NODE_MASTER_SETTING.get(settings) == false &&
        Node.NODE_DATA_SETTING.exists(settings) && Node.NODE_DATA_SETTING.get(settings) == false
        ) {
        suffix = suffix + "c";
    }
    return suffix;
}
 
Example 7
Project: elasticsearch_my   File: InternalTestCluster.java   Source Code and License 6 votes vote down vote up
/**
 * Starts multiple nodes with the given settings and returns their names
 */
public synchronized List<String> startNodes(Settings... settings) {
    final int defaultMinMasterNodes;
    if (autoManageMinMasterNodes) {
        int mastersDelta = (int) Stream.of(settings).filter(Node.NODE_MASTER_SETTING::get).count();
        defaultMinMasterNodes = getMinMasterNodes(getMasterNodesCount() + mastersDelta);
    } else {
        defaultMinMasterNodes = -1;
    }
    List<NodeAndClient> nodes = new ArrayList<>();
    for (Settings nodeSettings: settings) {
        nodes.add(buildNode(nodeSettings, defaultMinMasterNodes));
    }
    startAndPublishNodesAndClients(nodes);
    if (autoManageMinMasterNodes) {
        validateClusterFormed();
    }

    return nodes.stream().map(NodeAndClient::getName).collect(Collectors.toList());
}
 
Example 8
Project: elasticsearch_my   File: InternalTestCluster.java   Source Code and License 6 votes vote down vote up
synchronized String routingKeyForShard(Index index, int shard, Random random) {
    assertThat(shard, greaterThanOrEqualTo(0));
    assertThat(shard, greaterThanOrEqualTo(0));
    for (NodeAndClient n : nodes.values()) {
        Node node = n.node;
        IndicesService indicesService = getInstanceFromNode(IndicesService.class, node);
        ClusterService clusterService = getInstanceFromNode(ClusterService.class, node);
        IndexService indexService = indicesService.indexService(index);
        if (indexService != null) {
            assertThat(indexService.getIndexSettings().getSettings().getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, -1), greaterThan(shard));
            OperationRouting operationRouting = clusterService.operationRouting();
            while (true) {
                String routing = RandomStrings.randomAsciiOfLength(random, 10);
                final int targetShard = operationRouting.indexShards(clusterService.state(), index.getName(), null, routing).shardId().getId();
                if (shard == targetShard) {
                    return routing;
                }
            }
        }
    }
    fail("Could not find a node that holds " + index);
    return null;
}
 
Example 9
Project: elasticsearch_my   File: TribeService.java   Source Code and License 6 votes vote down vote up
public void startNodes() {
    for (Node node : nodes) {
        try {
            getClusterService(node).addListener(new TribeClusterStateListener(node));
            node.start();
        } catch (Exception e) {
            // calling close is safe for non started nodes, we can just iterate over all
            for (Node otherNode : nodes) {
                try {
                    otherNode.close();
                } catch (Exception inner) {
                    inner.addSuppressed(e);
                    logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to close node {} on failed start", otherNode), inner);
                }
            }
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            }
            throw new ElasticsearchException(e);
        }
    }
}
 
Example 10
Project: elasticsearch_my   File: PublishClusterStateActionTests.java   Source Code and License 6 votes vote down vote up
public void testFailToPublishWithLessThanMinMasterNodes() throws Exception {
    final int masterNodes = randomIntBetween(1, 10);

    MockNode master = createMockNode("master");
    DiscoveryNodes.Builder discoveryNodesBuilder = DiscoveryNodes.builder().add(master.discoveryNode);
    for (int i = 1; i < masterNodes; i++) {
        discoveryNodesBuilder.add(createMockNode("node" + i).discoveryNode);
    }
    final int dataNodes = randomIntBetween(0, 5);
    final Settings dataSettings = Settings.builder().put(Node.NODE_MASTER_SETTING.getKey(), false).build();
    for (int i = 0; i < dataNodes; i++) {
        discoveryNodesBuilder.add(createMockNode("data_" + i, dataSettings, null).discoveryNode);
    }
    discoveryNodesBuilder.localNodeId(master.discoveryNode.getId()).masterNodeId(master.discoveryNode.getId());
    DiscoveryNodes discoveryNodes = discoveryNodesBuilder.build();
    MetaData metaData = MetaData.EMPTY_META_DATA;
    ClusterState clusterState = ClusterState.builder(CLUSTER_NAME).metaData(metaData).nodes(discoveryNodes).build();
    ClusterState previousState = master.clusterState;
    try {
        publishState(master.action, clusterState, previousState, masterNodes + randomIntBetween(1, 5));
        fail("cluster state publishing didn't fail despite of not having enough nodes");
    } catch (Discovery.FailedToCommitClusterStateException expected) {
        logger.debug("failed to publish as expected", expected);
    }
}
 
Example 11
Project: elasticsearch_my   File: ZenFaultDetectionTests.java   Source Code and License 6 votes vote down vote up
protected MockTransportService build(Settings settings, Version version) {
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
    MockTransportService transportService =
        new MockTransportService(
            Settings.builder()
                .put(settings)
                // trace zenfd actions but keep the default otherwise
                .put(TransportService.TRACE_LOG_EXCLUDE_SETTING.getKey(), singleton(TransportLivenessAction.NAME))
                .build(),
            new MockTcpTransport(settings, threadPool, BigArrays.NON_RECYCLING_INSTANCE, circuitBreakerService,
                namedWriteableRegistry, new NetworkService(settings, Collections.emptyList()), version),
            threadPool,
            TransportService.NOOP_TRANSPORT_INTERCEPTOR,
            (boundAddress) ->
                new DiscoveryNode(Node.NODE_NAME_SETTING.get(settings), boundAddress.publishAddress(),
                    Node.NODE_ATTRIBUTES.get(settings).getAsMap(), DiscoveryNode.getRolesFromSettings(settings), version),
            null);
    transportService.start();
    transportService.acceptIncomingRequests();
    return transportService;
}
 
Example 12
Project: elasticsearch_my   File: GatewayIndexStateIT.java   Source Code and License 6 votes vote down vote up
public void testJustMasterNode() throws Exception {
    logger.info("--> cleaning nodes");

    logger.info("--> starting 1 master node non data");
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build());

    logger.info("--> create an index");
    client().admin().indices().prepareCreate("test").setWaitForActiveShards(ActiveShardCount.NONE).execute().actionGet();

    logger.info("--> closing master node");
    internalCluster().closeNonSharedNodes(false);

    logger.info("--> starting 1 master node non data again");
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build());

    logger.info("--> waiting for test index to be created");
    ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setIndices("test")
        .execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));

    logger.info("--> verify we have an index");
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().setIndices("test").execute().actionGet();
    assertThat(clusterStateResponse.getState().metaData().hasIndex("test"), equalTo(true));
}
 
Example 13
Project: elasticsearch_my   File: TribeIT.java   Source Code and License 6 votes vote down vote up
private Settings.Builder createTribeSettings(Predicate<InternalTestCluster> filter) {
    assertNotNull(filter);

    final Settings.Builder settings = Settings.builder();
    settings.put(Node.NODE_NAME_SETTING.getKey(), TRIBE_NODE);
    settings.put(Node.NODE_DATA_SETTING.getKey(), false);
    settings.put(Node.NODE_MASTER_SETTING.getKey(), true);
    settings.put(NetworkModule.HTTP_ENABLED.getKey(), false);
    settings.put(NetworkModule.TRANSPORT_TYPE_SETTING.getKey(), MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME);

    doWithAllClusters(filter, c -> {
        String tribeSetting = "tribe." + c.getClusterName() + ".";
        settings.put(tribeSetting + ClusterName.CLUSTER_NAME_SETTING.getKey(), c.getClusterName());
        settings.put(tribeSetting + DiscoverySettings.INITIAL_STATE_TIMEOUT_SETTING.getKey(), "100ms");
        settings.put(tribeSetting + NetworkModule.TRANSPORT_TYPE_SETTING.getKey(), MockTcpTransportPlugin.MOCK_TCP_TRANSPORT_NAME);
    });

    return settings;
}
 
Example 14
Project: elasticsearch_my   File: SpecificMasterNodesIT.java   Source Code and License 6 votes vote down vote up
public void testElectOnlyBetweenMasterNodes() throws IOException {
    logger.info("--> start data node / non master node");
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true).put(Node.NODE_MASTER_SETTING.getKey(), false).put("discovery.initial_state_timeout", "1s"));
    try {
        assertThat(client().admin().cluster().prepareState().setMasterNodeTimeout("100ms").execute().actionGet().getState().nodes().getMasterNodeId(), nullValue());
        fail("should not be able to find master");
    } catch (MasterNotDiscoveredException e) {
        // all is well, no master elected
    }
    logger.info("--> start master node (1)");
    final String masterNodeName = internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).put(Node.NODE_MASTER_SETTING.getKey(), true));
    assertThat(internalCluster().nonMasterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().getMasterNode().getName(), equalTo(masterNodeName));
    assertThat(internalCluster().masterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().getMasterNode().getName(), equalTo(masterNodeName));

    logger.info("--> start master node (2)");
    final String nextMasterEligableNodeName = internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).put(Node.NODE_MASTER_SETTING.getKey(), true));
    assertThat(internalCluster().nonMasterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().getMasterNode().getName(), equalTo(masterNodeName));
    assertThat(internalCluster().nonMasterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().getMasterNode().getName(), equalTo(masterNodeName));
    assertThat(internalCluster().masterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().getMasterNode().getName(), equalTo(masterNodeName));

    logger.info("--> closing master node (1)");
    internalCluster().stopCurrentMasterNode();
    assertThat(internalCluster().nonMasterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().getMasterNode().getName(), equalTo(nextMasterEligableNodeName));
    assertThat(internalCluster().masterClient().admin().cluster().prepareState().execute().actionGet().getState().nodes().getMasterNode().getName(), equalTo(nextMasterEligableNodeName));
}
 
Example 15
Project: elasticsearch_my   File: SpecificMasterNodesIT.java   Source Code and License 6 votes vote down vote up
/**
 * Tests that putting custom default mapping and then putting a type mapping will have the default mapping merged
 * to the type mapping.
 */
public void testCustomDefaultMapping() throws Exception {
    logger.info("--> start master node / non data");
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).put(Node.NODE_MASTER_SETTING.getKey(), true));

    logger.info("--> start data node / non master node");
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true).put(Node.NODE_MASTER_SETTING.getKey(), false));

    createIndex("test");
    assertAcked(client().admin().indices().preparePutMapping("test").setType("_default_").setSource("timestamp", "type=date"));

    MappingMetaData defaultMapping = client().admin().cluster().prepareState().get().getState().getMetaData().getIndices().get("test").getMappings().get("_default_");
    Map<?,?> properties = (Map<?, ?>) defaultMapping.getSourceAsMap().get("properties");
    assertThat(properties.get("timestamp"), notNullValue());

    assertAcked(client().admin().indices().preparePutMapping("test").setType("_default_").setSource("timestamp", "type=date"));

    assertAcked(client().admin().indices().preparePutMapping("test").setType("type1").setSource("foo", "enabled=true"));
    MappingMetaData type1Mapping = client().admin().cluster().prepareState().get().getState().getMetaData().getIndices().get("test").getMappings().get("type1");
    properties = (Map<?, ?>) type1Mapping.getSourceAsMap().get("properties");
    assertThat(properties.get("timestamp"), notNullValue());
}
 
Example 16
Project: elasticsearch_my   File: UpdateSettingsValidationIT.java   Source Code and License 6 votes vote down vote up
public void testUpdateSettingsValidation() throws Exception {
    internalCluster().startNodes(
            Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).build(),
            Settings.builder().put(Node.NODE_MASTER_SETTING.getKey(), false).build(),
            Settings.builder().put(Node.NODE_MASTER_SETTING.getKey(), false).build()
    );

    createIndex("test");
    NumShards test = getNumShards("test");

    ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth("test").setWaitForEvents(Priority.LANGUID).setWaitForNodes("3").setWaitForGreenStatus().execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    assertThat(healthResponse.getIndices().get("test").getActiveShards(), equalTo(test.totalNumShards));

    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put("index.number_of_replicas", 0)).execute().actionGet();
    healthResponse = client().admin().cluster().prepareHealth("test").setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    assertThat(healthResponse.getIndices().get("test").getActiveShards(), equalTo(test.numPrimaries));

    try {
        client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put("index.refresh_interval", "")).execute().actionGet();
        fail();
    } catch (IllegalArgumentException ex) {
        logger.info("Error message: [{}]", ex.getMessage());
    }
}
 
Example 17
Project: elasticsearch_my   File: ScheduleWithFixedDelayTests.java   Source Code and License 6 votes vote down vote up
public void testOnRejectionCausesCancellation() throws Exception {
    final TimeValue delay = TimeValue.timeValueMillis(10L);
    terminate(threadPool);
    threadPool = new ThreadPool(Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), "fixed delay tests").build()) {
        @Override
        public ScheduledFuture<?> schedule(TimeValue delay, String executor, Runnable command) {
            if (command instanceof ReschedulingRunnable) {
                ((ReschedulingRunnable) command).onRejection(new EsRejectedExecutionException());
            } else {
                fail("this should only be called with a rescheduling runnable in this test");
            }
            return null;
        }
    };
    Runnable runnable = () -> {};
    ReschedulingRunnable reschedulingRunnable = new ReschedulingRunnable(runnable, delay, Names.GENERIC, threadPool);
    assertTrue(reschedulingRunnable.isCancelled());
}
 
Example 18
Project: elasticsearch_my   File: SimpleThreadPoolIT.java   Source Code and License 6 votes vote down vote up
public void testThreadPoolLeakingThreadsWithTribeNode() {
    Settings settings = Settings.builder()
            .put("node.name", "thread_pool_leaking_threads_tribe_node")
            .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir())
            .put("tribe.t1.cluster.name", "non_existing_cluster")
                    //trigger initialization failure of one of the tribes (doesn't require starting the node)
            .put("tribe.t1.plugin.mandatory", "non_existing").build();

    try {
        new Node(settings);
        fail("The node startup is supposed to fail");
    } catch(Exception e) {
        //all good
        assertThat(e.getMessage(), containsString("mandatory plugins [non_existing]"));
    }
}
 
Example 19
Project: elasticsearch_my   File: DedicatedClusterSnapshotRestoreIT.java   Source Code and License 6 votes vote down vote up
public void testRegistrationFailure() {
    logger.info("--> start first node");
    internalCluster().startNode();
    logger.info("--> start second node");
    // Make sure the first node is elected as master
    internalCluster().startNode(Settings.builder().put(Node.NODE_MASTER_SETTING.getKey(), false));
    // Register mock repositories
    for (int i = 0; i < 5; i++) {
        client().admin().cluster().preparePutRepository("test-repo" + i)
                .setType("mock").setSettings(Settings.builder()
                .put("location", randomRepoPath())).setVerify(false).get();
    }
    logger.info("--> make sure that properly setup repository can be registered on all nodes");
    client().admin().cluster().preparePutRepository("test-repo-0")
            .setType("fs").setSettings(Settings.builder()
            .put("location", randomRepoPath())).get();

}
 
Example 20
Project: elasticsearch-indexing-proxy   File: IndexingProxyPluginTest.java   Source Code and License 6 votes vote down vote up
private void waitForNdocs(final Node node, final String index1, final String type, final long num) throws Exception {
    long actual = 0;
    for (int i = 0; i < 30; i++) {
        try (CurlResponse curlResponse = Curl.post(node, "/" + index1 + "/" + type + "/_search")
                .header("Content-Type", "application/json").body("{\"query\":{\"match_all\":{}}}").execute()) {
            final Map<String, Object> map = curlResponse.getContentAsMap();
            @SuppressWarnings("unchecked")
            final Map<String, Object> hits = (Map<String, Object>) map.get("hits");
            actual = ((Number) hits.get("total")).longValue();
            if (actual == num) {
                Thread.sleep(3000L); // wait for bulk requests
                return;
            }
        }
        runner.refresh();
        Thread.sleep(1000L);
    }
    fail(num + " docs are not inserted. " + actual + " docs exist.");
}
 
Example 21
Project: elasticsearch-indexing-proxy   File: IndexingProxyPluginTest.java   Source Code and License 6 votes vote down vote up
private void bulkRequest(final Node node, final String index, final String type, final long id) throws IOException {
    final StringBuilder buf = new StringBuilder();
    long value = id;
    buf.append("{\"create\":{\"_index\":\"" + index + "\",\"_type\":\"" + type + "\",\"_id\":\"" + value + "\"}}\n");
    buf.append("{\"id\":" + value + ",\"msg\":\"test " + value + "\"}\n");
    value++;
    createRequest(node, index, type, value);
    buf.append("{\"update\":{\"_index\":\"" + index + "\",\"_type\":\"" + type + "\",\"_id\":\"" + value + "\"}}\n");
    buf.append("{\"doc\":{\"msg\":\"test " + (value + 300) + "\"}}\n");
    value++;
    createRequest(node, index, type, value);
    buf.append("{\"delete\":{\"_index\":\"" + index + "\",\"_type\":\"" + type + "\",\"_id\":\"" + value + "\"}}\n");

    try (CurlResponse curlResponse =
            Curl.post(node, "/_bulk").header("Content-Type", "application/x-ndjson").body(buf.toString()).execute()) {
        final Map<String, Object> map = curlResponse.getContentAsMap();
        assertNotNull(map);
        assertTrue(map.containsKey("took"));
    }
}
 
Example 22
Project: Elasticsearch   File: TribeService.java   Source Code and License 6 votes vote down vote up
@Override
protected void doStart() {
    for (Node node : nodes) {
        try {
            node.start();
        } catch (Throwable e) {
            // calling close is safe for non started nodes, we can just iterate over all
            for (Node otherNode : nodes) {
                try {
                    otherNode.close();
                } catch (Throwable t) {
                    logger.warn("failed to close node {} on failed start", t, otherNode);
                }
            }
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            }
            throw new ElasticsearchException(e.getMessage(), e);
        }
    }
}
 
Example 23
Project: Elasticsearch   File: NodeModule.java   Source Code and License 6 votes vote down vote up
@Override
protected void configure() {
    if (pageCacheRecyclerImpl == PageCacheRecycler.class) {
        bind(PageCacheRecycler.class).asEagerSingleton();
    } else {
        bind(PageCacheRecycler.class).to(pageCacheRecyclerImpl).asEagerSingleton();
    }
    if (bigArraysImpl == BigArrays.class) {
        bind(BigArrays.class).asEagerSingleton();
    } else {
        bind(BigArrays.class).to(bigArraysImpl).asEagerSingleton();
    }

    bind(Node.class).toInstance(node);
    bind(NodeSettingsService.class).asEagerSingleton();
    bind(NodeService.class).asEagerSingleton();
}
 
Example 24
Project: servicebuilder   File: ElasticsearchAddonMockImpl.java   Source Code and License 6 votes vote down vote up
private static Node elasticSearchTestNode(Path path) throws NodeValidationException {
    return new MyNode(
            Settings.builder()
                    .put("http.enabled", "true")
                    .put("path.home", path.toString())
                    .put("cluster.name", "test-search-api-5-local_junit")
                    .put("http.port", 9211)
                    .put("transport.tcp.port", 9311)
                    .put("http.publish_port", 9211)
                    .put("http.publish_host", "127.0.0.1")
                    .put("transport.bind_host", "127.0.0.1")
                    .put("transport.publish_port", 9311)
                    .put("transport.publish_host", "127.0.0.1")
                    .build(),
            Lists.newArrayList(Netty4Plugin.class)
    ).start();
}
 
Example 25
Project: dremio-oss   File: ElasticsearchCluster.java   Source Code and License 6 votes vote down vote up
/**
 * Starts an embedded elasticsearch cluster.
 * @param sslEnabled only compatible with size == 1
 * @throws IOException
 */
public ElasticsearchCluster(int size, int scrollSize, Random random, boolean scriptsEnabled, boolean showIDColumn, boolean publishHost, boolean sslEnabled, Integer presetSSLPort) throws IOException {
  if (size < 1) {
    throw new IllegalArgumentException("Cluster size must be at least 1");
  }
  if (sslEnabled && size != 1) {
    throw new IllegalArgumentException("only single node cluster supported for ssl. size = " + size);
  }
  this.scrollSize = scrollSize;
  this.random = random;
  logger.info("--> Initializing elasticsearch cluster with {} nodes", size);
  nodes = new Node[size];
  this.scriptsEnabled = scriptsEnabled;
  this.showIDColumn = showIDColumn;
  this.sslEnabled = sslEnabled;
  if(!USE_EXTERNAL_ES5){
    initServer(size, publishHost, presetSSLPort);
  }
  initClient();

}
 
Example 26
Project: elastic-crud   File: NodeTestConfig.java   Source Code and License 6 votes vote down vote up
@Bean(destroyMethod="close")
Node newNode() throws NodeValidationException {
  final Path tempDir = createTempDir().toPath();
  final Settings settings = Settings.builder()
    .put(ClusterName.CLUSTER_NAME_SETTING.getKey(), new ClusterName("single-node-cluster" + System.nanoTime()))
    .put(Environment.PATH_HOME_SETTING.getKey(), tempDir)
    .put(Environment.PATH_REPO_SETTING.getKey(), tempDir.resolve("repo"))
    .put(Environment.PATH_SHARED_DATA_SETTING.getKey(), createTempDir().getParent())
    .put("node.name", "single-node")
    .put("script.inline", "true")
    .put("script.stored", "true")
    .put(ScriptService.SCRIPT_MAX_COMPILATIONS_PER_MINUTE.getKey(), 1000)
    .put(EsExecutors.PROCESSORS_SETTING.getKey(), 1)
    .put(NetworkModule.HTTP_ENABLED.getKey(), false)
    .put("discovery.type", "zen")
    .put("transport.type", "local")
    .put(Node.NODE_DATA_SETTING.getKey(), true)
    .put(NODE_ID_SEED_SETTING.getKey(), System.nanoTime())
    .build();
  return new Node(settings).start(); // NOSONAR
}
 
Example 27
Project: nexus-public   File: NodeProvider.java   Source Code and License 6 votes vote down vote up
@Override
public synchronized Node get() {
  if (node == null) {
    try {
      Node newNode = create();

      // yellow status means that node is up (green will mean that replicas are online but we have only one node)
      log.debug("Waiting for yellow-status");
      newNode.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet();

      this.node = newNode;
    }
    catch (Exception e) {
      // If we can not acquire an ES node reference, give up
      Throwables.throwIfUnchecked(e);
      throw new RuntimeException(e);
    }
  }
  return node;
}
 
Example 28
Project: search-guard   File: IntegrationTests.java   Source Code and License 6 votes vote down vote up
@SuppressWarnings("resource")
@Test
public void testNodeClientAllowedWithServerCertificate() throws Exception {
    setup();
    Assert.assertEquals(3, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getNumberOfNodes());
    Assert.assertEquals(ClusterHealthStatus.GREEN, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getStatus());

    
    final Settings tcSettings = Settings.builder()
            .put(minimumSearchGuardSettings(Settings.EMPTY).get(0))
            .put("cluster.name", clusterInfo.clustername)
            .put("node.data", false)
            .put("node.master", false)
            .put("node.ingest", false)
            .put("path.home", ".")
            .build();

    log.debug("Start node client");
    
    try (Node node = new PluginAwareNode(tcSettings, Netty4Plugin.class, SearchGuardPlugin.class).start()) {
        Thread.sleep(50);
        Assert.assertEquals(4, node.client().admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size());    
    }
}
 
Example 29
Project: search-guard   File: IntegrationTests.java   Source Code and License 6 votes vote down vote up
@SuppressWarnings("resource")
@Test
public void testNodeClientDisallowedWithNonServerCertificate() throws Exception {
    setup();
    Assert.assertEquals(3, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getNumberOfNodes());
    Assert.assertEquals(ClusterHealthStatus.GREEN, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getStatus());

    
    final Settings tcSettings = Settings.builder()
            .put(minimumSearchGuardSettings(Settings.EMPTY).get(0))
            .put("cluster.name", clusterInfo.clustername)
            .put("node.data", false)
            .put("node.master", false)
            .put("node.ingest", false)
            .put("path.home", ".")
            .put("searchguard.ssl.transport.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath("kirk-keystore.jks"))
            .put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_ALIAS,"kirk")
            .build();

    log.debug("Start node client");
    
    try (Node node = new PluginAwareNode(tcSettings, Netty4Plugin.class, SearchGuardPlugin.class).start()) {
        Thread.sleep(50);
        Assert.assertEquals(1, node.client().admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size());    
    }
}
 
Example 30
Project: search-guard   File: IntegrationTests.java   Source Code and License 6 votes vote down vote up
@SuppressWarnings("resource")
@Test
public void testNodeClientDisallowedWithNonServerCertificate2() throws Exception {
    setup();
    Assert.assertEquals(3, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getNumberOfNodes());
    Assert.assertEquals(ClusterHealthStatus.GREEN, clusterHelper.nodeClient().admin().cluster().health(new ClusterHealthRequest().waitForGreenStatus()).actionGet().getStatus());
 
    final Settings tcSettings = Settings.builder()
            .put(minimumSearchGuardSettings(Settings.EMPTY).get(0))
            .put("cluster.name", clusterInfo.clustername)
            .put("node.data", false)
            .put("node.master", false)
            .put("node.ingest", false)
            .put("path.home", ".")
            .put("searchguard.ssl.transport.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath("spock-keystore.jks"))
            .put(SSLConfigConstants.SEARCHGUARD_SSL_TRANSPORT_KEYSTORE_ALIAS,"spock")
            .build();

    log.debug("Start node client");
    
    try (Node node = new PluginAwareNode(tcSettings, Netty4Plugin.class, SearchGuardPlugin.class).start()) {
        Thread.sleep(50);
        Assert.assertEquals(1, node.client().admin().cluster().nodesInfo(new NodesInfoRequest()).actionGet().getNodes().size());    
    }
}
 
Example 31
Project: bts   File: ElasticTest.java   Source Code and License 6 votes vote down vote up
public static void main(String[] args)
{
	Node node = nodeBuilder().node();
	Client client = node.client();

	AdminClient ac = client.admin();

	client = new TransportClient().addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
	ElasticTest t = new ElasticTest();
	t.initRiver(client);
	// t.initRiver(client);
	// t.getContributionsByCandName(client, "aaew_corpus_aaew_full", 0);
	t.search(client);
	// // on shutdown
	// // Configuration
	// ClientConfig clientConfig = new
	// ClientConfig.Builder("http://localhost:9200").multiThreaded(true).build();
	//
	// // Construct a new Jest client according to configuration via factory
	// JestClientFactory factory = new JestClientFactory();
	// factory.setClientConfig(clientConfig);
	// JestClient clientj = factory.getObject();
	// clientj.
	node.close();

}
 
Example 32
Project: heroic   File: NodeClientSetup.java   Source Code and License 6 votes vote down vote up
@Override
public ClientWrapper setup() throws Exception {
    final Settings settings = Settings
        .builder()
        .put("node.name", InetAddress.getLocalHost().getHostName())
        .putArray("discovery.zen.ping.unicast.hosts", seeds)
        .put("cluster.name", clusterName)
        .put("node.data", false)
        .build();

    final Node node = new Node(settings);

    return new ClientWrapper(node.client(), new Runnable() {
        @Override
        public void run() {
            try {
                node.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
 
Example 33
Project: alfresco-apache-storm-demo   File: ElasticSearchConnection.java   Source Code and License 6 votes vote down vote up
public static Client getClient(Map stormConf, String boltType) {
    String host = ConfUtils.getString(stormConf, "es." + boltType
            + ".hostname", "localhost");

    Client client;

    // connection to ES
    if (host.equalsIgnoreCase("localhost")) {
        Node node = org.elasticsearch.node.NodeBuilder.nodeBuilder()
                .clusterName("elasticsearch").client(true).node();
        client = node.client();
    } else {
        Settings settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name", "elasticsearch").build();
        client = new TransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(host,
                        9300));
    }
    return client;
}
 
Example 34
Project: eservice   File: CommonBeans.java   Source Code and License 6 votes vote down vote up
@Bean(name = "appSearchElasticNode", destroyMethod = "close")
public Node getElasticSearchNode(
        @Value("${elastic.hosts}") String hosts,
        @Value("${elastic.cluster.name}") String clusterName,
        @Value("${elastic.node.local}") boolean localMode) {

    NodeBuilder builder = nodeBuilder().clusterName(clusterName);

    if (localMode) {
        builder.local(true);
    } else {
        builder.settings(
                ImmutableSettings.builder()
                        .put("discovery.zen.ping.unicast.hosts", hosts)
                        .put("discovery.zen.ping.multicast.enabled", false)
                        .put("node.client", true)
                        .put("node.master", false)
                        .put("node.data", false));
    }

    return builder.node();
}
 
Example 35
Project: gerrit   File: ElasticTestUtils.java   Source Code and License 6 votes vote down vote up
private static String getHttpPort(Node node) throws InterruptedException, ExecutionException {
  String nodes =
      node.client().admin().cluster().nodesInfo(new NodesInfoRequest("*")).get().toString();
  Gson gson =
      new GsonBuilder()
          .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
          .create();
  Info info = gson.fromJson(nodes, Info.class);
  if (info.nodes == null || info.nodes.size() != 1) {
    throw new RuntimeException("Cannot extract local Elasticsearch http port");
  }
  Iterator<NodeInfo> values = info.nodes.values().iterator();
  String httpAddress = values.next().httpAddress;
  if (Strings.isNullOrEmpty(httpAddress)) {
    throw new RuntimeException("Cannot extract local Elasticsearch http port");
  }
  if (httpAddress.indexOf(':') < 0) {
    throw new RuntimeException("Seems that port is not included in Elasticsearch http_address");
  }
  return httpAddress.substring(httpAddress.indexOf(':') + 1, httpAddress.length());
}
 
Example 36
Project: elasticsearch-tutorial   File: SearchClientServiceMockImpl.java   Source Code and License 6 votes vote down vote up
public Node buildNode(String id, Settings settings) {
    String settingsSource = getClass().getName().replace('.', '/') + ".yml";
    Settings finalSettings = settingsBuilder()
            .loadFromClasspath(settingsSource)
            .put(defaultSettings)
            .put(settings)
            .put("name", id)
            .build();

    if (finalSettings.get("gateway.type") == null) {
        // default to non gateway
        finalSettings = settingsBuilder().put(finalSettings).put("gateway.type", "none").build();
    }
    if (finalSettings.get("cluster.routing.schedule") != null) {
        // decrease the routing schedule so new nodes will be added quickly
        finalSettings = settingsBuilder().put(finalSettings).put("cluster.routing.schedule", "50ms").build();
    }

    Node node = nodeBuilder()
            .settings(finalSettings)
            .build();
    nodes.put(id, node);
    clients.put(id, node.client());
    return node;
}
 
Example 37
Project: elasticsearch-akka   File: SearchClientServiceMockImpl.java   Source Code and License 6 votes vote down vote up
public Node buildNode(final String id, final Settings settings)
{
    final String settingsSource = getClass().getName().replace('.', '/') + ".yml";
    Settings finalSettings = settingsBuilder().loadFromClasspath(settingsSource).put(defaultSettings).put(settings).put("name", id)
            .build();
    if (finalSettings.get("gateway.type") == null)
    {
        // default to non gateway
        finalSettings = settingsBuilder().put(finalSettings).put("gateway.type", "none").build();
    }
    if (finalSettings.get("cluster.routing.schedule") != null)
    {
        // decrease the routing schedule so new nodes will be added quickly
        finalSettings = settingsBuilder().put(finalSettings).put("cluster.routing.schedule", "50ms").build();
    }
    final Node node = nodeBuilder().settings(finalSettings).build();
    nodes.put(id, node);
    clients.put(id, node.client());
    return node;
}
 
Example 38
Project: ElasticParser   File: ESSearchTest.java   Source Code and License 6 votes vote down vote up
private static Node startESNode(String clusterName)
{

    Settings nodeSettings = Settings.settingsBuilder()
    		.put("path.home", "target")
    		.put("transport.tcp.port", "9600-9700")
            .put("http.port", "9500")
            .put("http.max_content_length", "100M").build();

    Node node = NodeBuilder.nodeBuilder()
    		.settings(nodeSettings)
    		.clusterName(clusterName).node();

    node.start();

    return node;
}
 
Example 39
Project: elasticsearch-reindexing   File: ReindexingPluginTest.java   Source Code and License 6 votes vote down vote up
private void test_wait_for_completion(Node node, String index) {
    String newIndex0 = "dataset0", newIndex1 = "dataset1";

    CurlResponse response0 = Curl.post(node, "/" + index + "/_reindex/" + newIndex0)
            .param("wait_for_completion", "true").execute();
    CurlResponse response1 = Curl.post(node, "/" + index + "/_reindex/" + newIndex1)
            .execute();
    Map<String, Object> map0 = response0.getContentAsMap();
    Map<String, Object> map1 = response1.getContentAsMap();
    assertEquals(map0.size(), 1);
    assertTrue(map0.containsKey("acknowledged"));
    assertEquals(map1.size(), 2);
    assertTrue(map1.containsKey("acknowledged"));
    assertTrue(map1.containsKey("name"));

    runner.flush();
    runner.deleteIndex(newIndex0);
    runner.deleteIndex(newIndex1);
}
 
Example 40
Project: elasticsearch-native-script-joiner   File: JoinerScript.java   Source Code and License 6 votes vote down vote up
/**
 * This constructor will be called by guice during initialization
 *
 * @param node injecting the reference to current node to get access to node's client
 */
@SuppressWarnings("unchecked")
@Inject
public Factory(Node node, Settings settings) {
    super(settings);
    // Node is not fully initialized here
    // All we can do is save a reference to it for future use
    this.node = node;

    // Setup lookup cache
    ByteSizeValue size = settings.getAsBytesSize("nativescript.joiner.size", null);
    TimeValue expire = settings.getAsTime("expire", new TimeValue(2000));
    CacheBuilder cacheBuilder = CacheBuilder.newBuilder();
    if (size != null) {
        cacheBuilder.maximumSize(size.bytes());
    }
    if (expire != null) {
        cacheBuilder.expireAfterAccess(expire.nanos(), TimeUnit.SECONDS);
    }
    cache = cacheBuilder.build();
}
 
Example 41
Project: elasticsearch-cluster-runner   File: ElasticsearchClusterRunner.java   Source Code and License 6 votes vote down vote up
/**
 * Start a closed node.
 *
 * @param i
 * @return true if the node is started.
 */
@SuppressWarnings("resource")
public boolean startNode(final int i) {
    if (i >= nodeList.size()) {
        return false;
    }
    if (!nodeList.get(i).isClosed()) {
        return false;
    }
    final Node node = new ClusterRunnerNode(settingsList.get(i), pluginList);
    try {
        node.start();
        nodeList.set(i, node);
        return true;
    } catch (final NodeValidationException e) {
        print(e.getLocalizedMessage());
    }
    return false;
}
 
Example 42
Project: searchanalytics-bigdata   File: SearchClientServiceMockImpl.java   Source Code and License 6 votes vote down vote up
private Node buildNode(final String id, final Settings settings) {
	final String settingsSource = getClass().getName().replace('.', '/')
			+ ".yml";
	Settings finalSettings = settingsBuilder()
			.loadFromClasspath(settingsSource).put(defaultSettings)
			.put(settings).put("name", id).build();
	if (finalSettings.get("gateway.type") == null) {
		// default to non gateway
		finalSettings = settingsBuilder().put(finalSettings)
				.put("gateway.type", "none").build();
	}
	if (finalSettings.get("cluster.routing.schedule") != null) {
		// decrease the routing schedule so new nodes will be added quickly
		finalSettings = settingsBuilder().put(finalSettings)
				.put("cluster.routing.schedule", "50ms").build();
	}
	final Node node = nodeBuilder().settings(finalSettings).build();
	nodes.put(id, node);
	clients.put(id, node.client());
	return node;
}
 
Example 43
Project: elasticsearch-lang-velocity   File: VelocityPluginTest.java   Source Code and License 6 votes vote down vote up
@Test
public void test_render() throws Exception {
    setupEs();

    assertThat(1, is(runner.getNodeSize()));

    final Node node = runner.node();

    try (CurlResponse curlResponse = Curl.post(node, "/_search/script_template/velocity/index_search_query_1").body(
            "{\"template\":\"{\\\"query\\\":{\\\"match\\\":{\\\"${my_field}\\\":\\\"${my_value}\\\"}},\\\"size\\\":\\\"${my_size}\\\"}\"}")
            .execute()) {
        assertThat(200, is(curlResponse.getHttpStatusCode()));
    }

    String query;

    query = "{\"lang\":\"velocity\",\"inline\":\"{\\\"query\\\":{\\\"match\\\":{\\\"$my_field\\\":\\\"$my_value\\\"}},\\\"size\\\":\\\"$my_size\\\"}\","
            + "\"params\":{\"my_field\":\"category\",\"my_value\":\"1\",\"my_size\":\"50\"}}";
    try (CurlResponse curlResponse = Curl.post(node, "/_render/script_template").body(query).execute()) {
        final String content = curlResponse.getContentAsString();
        assertEquals("{\"template_output\":{\"query\":{\"match\":{\"category\":\"1\"}},\"size\":\"50\"}}", content);
    }
}
 
Example 44
Project: ezelastic   File: EzElasticHandler.java   Source Code and License 6 votes vote down vote up
private ElasticClient startLocalNode(String clusterName, boolean refresh, int version) {
    final ImmutableSettings.Builder builder = ImmutableSettings.settingsBuilder()
            .put("node.name", applicationName + "-local-node" + System.currentTimeMillis()).put("node.data", true)
            .put("cluster.name", clusterName).put("index.store.type", "memory")
            .put("index.store.fs.memory.enabled", "true").put("gateway.type", "none")
            .put("path.data", "./local-cluster/" + clusterName + "/data")
            .put("path.work", "./local-cluster/" + clusterName + "/work")
            .put("path.logs", "./local-cluster/" + clusterName + "/logs").put("index.number_of_shards", "1")
            .put("index.number_of_replicas", "0").put("cluster.routing.schedule", "50ms");
    final Node localNode = NodeBuilder.nodeBuilder().settings(builder).node();

    final Client client = localNode.client();
    client.admin().cluster().prepareHealth().setWaitForYellowStatus().setTimeout(TimeValue.timeValueMinutes(1))
            .execute().actionGet();
    return new ElasticClient(client, applicationName, refresh, version);
}
 
Example 45
Project: elasticsearch-gatherer   File: AbstractNodeTest.java   Source Code and License 6 votes vote down vote up
public Node buildNode(String id, Settings settings) {
    String settingsSource = getClass().getName().replace('.', '/') + ".yml";
    Settings finalSettings = settingsBuilder()
            .loadFromClasspath(settingsSource)
            .put(defaultSettings)
            .put(settings)
            .put("name", id)
            .build();
    if (finalSettings.get("gateway.type") == null) {
        finalSettings = settingsBuilder().put(finalSettings).put("gateway.type", "none").build();
    }
    if (finalSettings.get("cluster.routing.schedule") != null) {
        finalSettings = settingsBuilder().put(finalSettings).put("cluster.routing.schedule", "50ms").build();
    }
    Node node = nodeBuilder().settings(finalSettings).build();
    Client client = node.client();
    nodes.put(id, node);
    clients.put(id, client);
    return node;
}
 
Example 46
Project: elasticsearch_my   File: IndexShardTestCase.java   Source Code and License 5 votes vote down vote up
/**
 * Recovers a replica from the give primary, allow the user to supply a custom recovery target. A typical usage of a custom recovery
 * target is to assert things in the various stages of recovery.
 * @param replica                the recovery target shard
 * @param primary                the recovery source shard
 * @param targetSupplier         supplies an instance of {@link RecoveryTarget}
 * @param markAsRecovering       set to {@code false} if the replica is marked as recovering
 */
protected final void recoverReplica(final IndexShard replica,
                                    final IndexShard primary,
                                    final BiFunction<IndexShard, DiscoveryNode, RecoveryTarget> targetSupplier,
                                    final boolean markAsRecovering) throws IOException {
    final DiscoveryNode pNode = getFakeDiscoNode(primary.routingEntry().currentNodeId());
    final DiscoveryNode rNode = getFakeDiscoNode(replica.routingEntry().currentNodeId());
    if (markAsRecovering) {
        replica.markAsRecovering("remote", new RecoveryState(replica.routingEntry(), pNode, rNode));
    } else {
        assertEquals(replica.state(), IndexShardState.RECOVERING);
    }
    replica.prepareForIndexRecovery();
    final RecoveryTarget recoveryTarget = targetSupplier.apply(replica, pNode);

    final Store.MetadataSnapshot snapshot = getMetadataSnapshotOrEmpty(replica);
    final long startingSeqNo;
    if (snapshot.size() > 0) {
        startingSeqNo = PeerRecoveryTargetService.getStartingSeqNo(recoveryTarget);
    } else {
        startingSeqNo = SequenceNumbersService.UNASSIGNED_SEQ_NO;
    }

    final StartRecoveryRequest request =
        new StartRecoveryRequest(replica.shardId(), pNode, rNode, snapshot, false, 0, startingSeqNo);
    final RecoverySourceHandler recovery = new RecoverySourceHandler(
        primary,
        recoveryTarget,
        request,
        () -> 0L,
        e -> () -> {},
        (int) ByteSizeUnit.MB.toBytes(1),
        Settings.builder().put(Node.NODE_NAME_SETTING.getKey(), pNode.getName()).build());
    recovery.recoverToTarget();
    recoveryTarget.markAsDone();
    replica.updateRoutingEntry(ShardRoutingHelper.moveToStarted(replica.routingEntry()));
}
 
Example 47
Project: elasticsearch_my   File: InternalTestCluster.java   Source Code and License 5 votes vote down vote up
private void markNodeDataDirsAsPendingForWipe(Node node) {
    assert Thread.holdsLock(this);
    NodeEnvironment nodeEnv = node.getNodeEnvironment();
    if (nodeEnv.hasNodeFile()) {
        dataDirToClean.addAll(Arrays.asList(nodeEnv.nodeDataPaths()));
    }
}
 
Example 48
Project: elasticsearch_my   File: InternalTestCluster.java   Source Code and License 5 votes vote down vote up
private void markNodeDataDirsAsNotEligableForWipe(Node node) {
    assert Thread.holdsLock(this);
    NodeEnvironment nodeEnv = node.getNodeEnvironment();
    if (nodeEnv.hasNodeFile()) {
        dataDirToClean.removeAll(Arrays.asList(nodeEnv.nodeDataPaths()));
    }
}
 
Example 49
Project: elasticsearch_my   File: InternalTestCluster.java   Source Code and License 5 votes vote down vote up
/**
 * Starts a node with the given settings and returns it's name.
 */
public synchronized String startNode(Settings settings) {
    final int defaultMinMasterNodes = getMinMasterNodes(getMasterNodesCount() + (Node.NODE_MASTER_SETTING.get(settings) ? 1 : 0));
    NodeAndClient buildNode = buildNode(settings, defaultMinMasterNodes);
    startAndPublishNodesAndClients(Collections.singletonList(buildNode));
    return buildNode.name;
}
 
Example 50
Project: elasticsearch_my   File: MockTransportService.java   Source Code and License 5 votes vote down vote up
public static MockTransportService createNewService(Settings settings, Transport transport, Version version, ThreadPool threadPool,
                                                    @Nullable ClusterSettings clusterSettings) {
    return new MockTransportService(settings, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR,
        boundAddress ->
        new DiscoveryNode(Node.NODE_NAME_SETTING.get(settings), UUIDs.randomBase64UUID(), boundAddress.publishAddress(),
            Node.NODE_ATTRIBUTES.get(settings).getAsMap(), DiscoveryNode.getRolesFromSettings(settings), version),
        clusterSettings);
}
 
Example 51
Project: elasticsearch_my   File: MockTransportService.java   Source Code and License 5 votes vote down vote up
/**
 * Build the service.
 *
 * @param clusterSettings if non null the the {@linkplain TransportService} will register with the {@link ClusterSettings} for settings
 *        updates for {@link #TRACE_LOG_EXCLUDE_SETTING} and {@link #TRACE_LOG_INCLUDE_SETTING}.
 */
public MockTransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor interceptor,
        @Nullable ClusterSettings clusterSettings) {
    this(settings, transport, threadPool, interceptor, (boundAddress) ->
        DiscoveryNode.createLocal(settings, boundAddress.publishAddress(), settings.get(Node.NODE_NAME_SETTING.getKey(),
            UUIDs.randomBase64UUID())), clusterSettings);
}
 
Example 52
Project: elasticsearch_my   File: TribeService.java   Source Code and License 5 votes vote down vote up
public TribeService(Settings settings, ClusterService clusterService, final String tribeNodeId,
                    NamedWriteableRegistry namedWriteableRegistry, Function<Settings, Node> clientNodeBuilder) {
    super(settings);
    this.clusterService = clusterService;
    this.namedWriteableRegistry = namedWriteableRegistry;
    Map<String, Settings> nodesSettings = new HashMap<>(settings.getGroups("tribe", true));
    nodesSettings.remove("blocks"); // remove prefix settings that don't indicate a client
    nodesSettings.remove("on_conflict"); // remove prefix settings that don't indicate a client
    for (Map.Entry<String, Settings> entry : nodesSettings.entrySet()) {
        Settings clientSettings = buildClientSettings(entry.getKey(), tribeNodeId, settings, entry.getValue());
        nodes.add(clientNodeBuilder.apply(clientSettings));
    }

    this.blockIndicesMetadata = BLOCKS_METADATA_INDICES_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY);
    this.blockIndicesRead = BLOCKS_READ_INDICES_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY);
    this.blockIndicesWrite = BLOCKS_WRITE_INDICES_SETTING.get(settings).toArray(Strings.EMPTY_ARRAY);

    if (!nodes.isEmpty()) {
        if (BLOCKS_WRITE_SETTING.get(settings)) {
            clusterService.addInitialStateBlock(TRIBE_WRITE_BLOCK);
        }
        if (BLOCKS_METADATA_SETTING.get(settings)) {
            clusterService.addInitialStateBlock(TRIBE_METADATA_BLOCK);
        }
    }

    this.onConflict = ON_CONFLICT_SETTING.get(settings);
}
 
Example 53
Project: elasticsearch_my   File: TribeService.java   Source Code and License 5 votes vote down vote up
/**
 * Builds node settings for a tribe client node from the tribe node's global settings,
 * combined with tribe specific settings.
 */
static Settings buildClientSettings(String tribeName, String parentNodeId, Settings globalSettings, Settings tribeSettings) {
    for (String tribeKey : tribeSettings.getAsMap().keySet()) {
        if (tribeKey.startsWith("path.")) {
            throw new IllegalArgumentException("Setting [" + tribeKey + "] not allowed in tribe client [" + tribeName + "]");
        }
    }
    Settings.Builder sb = Settings.builder().put(tribeSettings);
    sb.put(Node.NODE_NAME_SETTING.getKey(), Node.NODE_NAME_SETTING.get(globalSettings) + "/" + tribeName);
    sb.put(Environment.PATH_HOME_SETTING.getKey(), Environment.PATH_HOME_SETTING.get(globalSettings)); // pass through ES home dir
    if (Environment.PATH_CONF_SETTING.exists(globalSettings)) {
        sb.put(Environment.PATH_CONF_SETTING.getKey(), Environment.PATH_CONF_SETTING.get(globalSettings));
    }
    if (Environment.PATH_LOGS_SETTING.exists(globalSettings)) {
        sb.put(Environment.PATH_LOGS_SETTING.getKey(), Environment.PATH_LOGS_SETTING.get(globalSettings));
    }
    if (Environment.PATH_SCRIPTS_SETTING.exists(globalSettings)) {
        sb.put(Environment.PATH_SCRIPTS_SETTING.getKey(), Environment.PATH_SCRIPTS_SETTING.get(globalSettings));
    }
    for (Setting<?> passthrough : PASS_THROUGH_SETTINGS) {
        if (passthrough.exists(tribeSettings) == false && passthrough.exists(globalSettings)) {
            sb.put(passthrough.getKey(), globalSettings.get(passthrough.getKey()));
        }
    }
    sb.put(TRIBE_NAME_SETTING.getKey(), tribeName);
    if (sb.get(NetworkModule.HTTP_ENABLED.getKey()) == null) {
        sb.put(NetworkModule.HTTP_ENABLED.getKey(), false);
    }
    sb.put(Node.NODE_DATA_SETTING.getKey(), false);
    sb.put(Node.NODE_MASTER_SETTING.getKey(), false);
    sb.put(Node.NODE_INGEST_SETTING.getKey(), false);

    // node id of a tribe client node is determined by node id of parent node and tribe name
    final BytesRef seedAsString = new BytesRef(parentNodeId + "/" + tribeName);
    long nodeIdSeed = MurmurHash3.hash128(seedAsString.bytes, seedAsString.offset, seedAsString.length, 0, new MurmurHash3.Hash128()).h1;
    sb.put(NodeEnvironment.NODE_ID_SEED_SETTING.getKey(), nodeIdSeed);
    sb.put(Node.NODE_LOCAL_STORAGE_SETTING.getKey(), false);
    return sb.build();
}
 
Example 54
Project: elasticsearch_my   File: TribeService.java   Source Code and License 5 votes vote down vote up
@Override
protected void doClose() {
    for (Node node : nodes) {
        try {
            node.close();
        } catch (Exception e) {
            logger.warn((Supplier<?>) () -> new ParameterizedMessage("failed to close node {}", node), e);
        }
    }
}
 
Example 55
Project: elasticsearch_my   File: TribeService.java   Source Code and License 5 votes vote down vote up
private boolean updateCustoms(ClusterState currentState, List<ClusterChangedEvent> tasks, MetaData.Builder metaData) {
    boolean clusterStateChanged = false;
    Set<String> changedCustomMetaDataTypeSet = tasks.stream()
            .map(ClusterChangedEvent::changedCustomMetaDataSet)
            .flatMap(Collection::stream)
            .collect(Collectors.toSet());
    final List<Node> tribeClientNodes = TribeService.this.nodes;
    Map<String, MetaData.Custom> mergedCustomMetaDataMap = mergeChangedCustomMetaData(changedCustomMetaDataTypeSet,
            customMetaDataType -> tribeClientNodes.stream()
                    .map(TribeService::getClusterService).map(ClusterService::state)
                    .map(ClusterState::metaData)
                    .map(clusterMetaData -> ((MetaData.Custom) clusterMetaData.custom(customMetaDataType)))
                    .filter(custom1 -> custom1 != null && custom1 instanceof MergableCustomMetaData)
                    .map(custom2 -> (MergableCustomMetaData) marshal(custom2))
                    .collect(Collectors.toList())
    );
    for (String changedCustomMetaDataType : changedCustomMetaDataTypeSet) {
        MetaData.Custom mergedCustomMetaData = mergedCustomMetaDataMap.get(changedCustomMetaDataType);
        if (mergedCustomMetaData == null) {
            // we ignore merging custom md which doesn't implement MergableCustomMetaData interface
            if (currentState.metaData().custom(changedCustomMetaDataType) instanceof MergableCustomMetaData) {
                // custom md has been removed
                clusterStateChanged = true;
                logger.info("[{}] removing custom meta data type [{}]", tribeName, changedCustomMetaDataType);
                metaData.removeCustom(changedCustomMetaDataType);
            }
        } else {
            // custom md has been changed
            clusterStateChanged = true;
            logger.info("[{}] updating custom meta data type [{}] data [{}]", tribeName, changedCustomMetaDataType, mergedCustomMetaData);
            metaData.putCustom(changedCustomMetaDataType, mergedCustomMetaData);
        }
    }
    return clusterStateChanged;
}
 
Example 56
Project: elasticsearch_my   File: DiscoveryNode.java   Source Code and License 5 votes vote down vote up
public static boolean nodeRequiresLocalStorage(Settings settings) {
    boolean localStorageEnable = Node.NODE_LOCAL_STORAGE_SETTING.get(settings);
    if (localStorageEnable == false &&
        (Node.NODE_DATA_SETTING.get(settings) ||
            Node.NODE_MASTER_SETTING.get(settings))
        ) {
        // TODO: make this a proper setting validation logic, requiring multi-settings validation
        throw new IllegalArgumentException("storage can not be disabled for master and data nodes");
    }
    return localStorageEnable;
}
 
Example 57
Project: elasticsearch_my   File: DiscoveryNode.java   Source Code and License 5 votes vote down vote up
/** Creates a DiscoveryNode representing the local node. */
public static DiscoveryNode createLocal(Settings settings, TransportAddress publishAddress, String nodeId) {
    Map<String, String> attributes = new HashMap<>(Node.NODE_ATTRIBUTES.get(settings).getAsMap());
    Set<Role> roles = getRolesFromSettings(settings);

    return new DiscoveryNode(Node.NODE_NAME_SETTING.get(settings), nodeId, publishAddress, attributes, roles, Version.CURRENT);
}
 
Example 58
Project: elasticsearch_my   File: DiscoveryNode.java   Source Code and License 5 votes vote down vote up
/** extract node roles from the given settings */
public static Set<Role> getRolesFromSettings(Settings settings) {
    Set<Role> roles = new HashSet<>();
    if (Node.NODE_INGEST_SETTING.get(settings)) {
        roles.add(Role.INGEST);
    }
    if (Node.NODE_MASTER_SETTING.get(settings)) {
        roles.add(Role.MASTER);
    }
    if (Node.NODE_DATA_SETTING.get(settings)) {
        roles.add(Role.DATA);
    }
    return roles;
}
 
Example 59
Project: elasticsearch_my   File: EsExecutors.java   Source Code and License 5 votes vote down vote up
public static String threadName(Settings settings, String namePrefix) {
    if (Node.NODE_NAME_SETTING.exists(settings)) {
        return threadName(Node.NODE_NAME_SETTING.get(settings), namePrefix);
    } else {
        return threadName("", namePrefix);
    }
}
 
Example 60
Project: elasticsearch_my   File: ScalingExecutorBuilder.java   Source Code and License 5 votes vote down vote up
@Override
ScalingExecutorSettings getSettings(Settings settings) {
    final String nodeName = Node.NODE_NAME_SETTING.get(settings);
    final int coreThreads = coreSetting.get(settings);
    final int maxThreads = maxSetting.get(settings);
    final TimeValue keepAlive = keepAliveSetting.get(settings);
    return new ScalingExecutorSettings(nodeName, coreThreads, maxThreads, keepAlive);
}