Search in sources :

Example 61 with ClusterStateResponse

use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse in project crate by crate.

the class GatewayIndexStateIT method testJustMasterNode.

/**
 * Creating a table without any data node will take very long as internally at CrateDB, a table creation
 * is waiting for all shards to acknowledge until it times out if no data node is available.
 * So this will run under the @Slow annotation.
 */
@Slow
@Test
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");
    execute("create table test (id int) with (number_of_replicas = 0, \"write.wait_for_active_shards\" = 0)", null, new TimeValue(90, TimeUnit.SECONDS));
    var tableName = getFqn("test");
    logger.info("--> restarting master node");
    internalCluster().fullRestart(new RestartCallback() {

        @Override
        public Settings onNodeStopped(String nodeName) {
            return 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(tableName).execute().actionGet(REQUEST_TIMEOUT);
    assertThat(health.isTimedOut(), equalTo(false));
    logger.info("--> verify we have an index");
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().setIndices(tableName).execute().actionGet(REQUEST_TIMEOUT);
    assertThat(clusterStateResponse.getState().metadata().hasIndex(tableName), equalTo(true));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) RestartCallback(org.elasticsearch.test.InternalTestCluster.RestartCallback) TimeValue(io.crate.common.unit.TimeValue) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test)

Example 62 with ClusterStateResponse

use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse in project crate by crate.

the class IndexRecoveryIT method testDisconnectsWhileRecovering.

@Test
public void testDisconnectsWhileRecovering() throws Exception {
    final String indexName = "test";
    final Settings nodeSettings = Settings.builder().put(RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_NETWORK_SETTING.getKey(), "100ms").put(RecoverySettings.INDICES_RECOVERY_INTERNAL_ACTION_TIMEOUT_SETTING.getKey(), "1s").put(NodeConnectionsService.CLUSTER_NODE_RECONNECT_INTERVAL_SETTING.getKey(), "1s").build();
    // start a master node
    internalCluster().startNode(nodeSettings);
    final String blueNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "blue").put(nodeSettings).build());
    final String redNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "red").put(nodeSettings).build());
    ClusterHealthResponse response = client().admin().cluster().prepareHealth().setWaitForNodes(">=3").get();
    assertThat(response.isTimedOut(), is(false));
    execute("CREATE TABLE doc." + indexName + " (id int) CLUSTERED INTO 1 SHARDS " + "WITH (" + " number_of_replicas=0," + " \"routing.allocation.include.color\" = 'blue'" + ")");
    int numDocs = scaledRandomIntBetween(25, 250);
    var args = new Object[numDocs][];
    for (int i = 0; i < numDocs; i++) {
        args[i] = new Object[] { i };
    }
    execute("INSERT INTO doc." + indexName + " (id) VALUES (?)", args);
    ensureGreen();
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    final String blueNodeId = internalCluster().getInstance(ClusterService.class, blueNodeName).localNode().getId();
    assertFalse(stateResponse.getState().getRoutingNodes().node(blueNodeId).isEmpty());
    refresh();
    var searchResponse = execute("SELECT COUNT(*) FROM doc." + indexName);
    assertThat((long) searchResponse.rows()[0][0], is((long) numDocs));
    String[] recoveryActions = new String[] { PeerRecoverySourceService.Actions.START_RECOVERY, PeerRecoveryTargetService.Actions.FILES_INFO, PeerRecoveryTargetService.Actions.FILE_CHUNK, PeerRecoveryTargetService.Actions.CLEAN_FILES, // RecoveryTarget.Actions.TRANSLOG_OPS, <-- may not be sent if already flushed
    PeerRecoveryTargetService.Actions.PREPARE_TRANSLOG, PeerRecoveryTargetService.Actions.FINALIZE };
    final String recoveryActionToBlock = randomFrom(recoveryActions);
    final boolean dropRequests = randomBoolean();
    logger.info("--> will {} between blue & red on [{}]", dropRequests ? "drop requests" : "break connection", recoveryActionToBlock);
    MockTransportService blueMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, blueNodeName);
    MockTransportService redMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, redNodeName);
    TransportService redTransportService = internalCluster().getInstance(TransportService.class, redNodeName);
    TransportService blueTransportService = internalCluster().getInstance(TransportService.class, blueNodeName);
    final CountDownLatch requestFailed = new CountDownLatch(1);
    if (randomBoolean()) {
        // Fail on the sending side
        blueMockTransportService.addSendBehavior(redTransportService, new RecoveryActionBlocker(dropRequests, recoveryActionToBlock, requestFailed));
        redMockTransportService.addSendBehavior(blueTransportService, new RecoveryActionBlocker(dropRequests, recoveryActionToBlock, requestFailed));
    } else {
        // Fail on the receiving side.
        blueMockTransportService.addRequestHandlingBehavior(recoveryActionToBlock, (handler, request, channel) -> {
            logger.info("--> preventing {} response by closing response channel", recoveryActionToBlock);
            requestFailed.countDown();
            redMockTransportService.disconnectFromNode(blueMockTransportService.getLocalNode());
            handler.messageReceived(request, channel);
        });
        redMockTransportService.addRequestHandlingBehavior(recoveryActionToBlock, (handler, request, channel) -> {
            logger.info("--> preventing {} response by closing response channel", recoveryActionToBlock);
            requestFailed.countDown();
            blueMockTransportService.disconnectFromNode(redMockTransportService.getLocalNode());
            handler.messageReceived(request, channel);
        });
    }
    logger.info("--> starting recovery from blue to red");
    execute("ALTER TABLE doc." + indexName + " SET (" + " number_of_replicas=1," + " \"routing.allocation.include.color\" = 'red,blue'" + ")");
    requestFailed.await();
    logger.info("--> clearing rules to allow recovery to proceed");
    blueMockTransportService.clearAllRules();
    redMockTransportService.clearAllRules();
    ensureGreen();
    // TODO: ES will use the shard routing preference here to prefer `_local` shards on that node
    var nodeRedExecutor = executor(redNodeName);
    searchResponse = nodeRedExecutor.exec("SELECT COUNT(*) FROM doc." + indexName);
    assertThat((long) searchResponse.rows()[0][0], is((long) numDocs));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) CountDownLatch(java.util.concurrent.CountDownLatch) TransportService(org.elasticsearch.transport.TransportService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) Test(org.junit.Test)

Example 63 with ClusterStateResponse

use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse in project crate by crate.

the class AbstractSnapshotIntegTestCase method waitForCompletion.

public SnapshotInfo waitForCompletion(String repository, String snapshotName, TimeValue timeout) throws InterruptedException {
    long start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < timeout.millis()) {
        List<SnapshotInfo> snapshotInfos = client().admin().cluster().prepareGetSnapshots(repository).setSnapshots(snapshotName).get().getSnapshots();
        assertThat(snapshotInfos.size(), equalTo(1));
        if (snapshotInfos.get(0).state().completed()) {
            // Make sure that snapshot clean up operations are finished
            ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
            SnapshotsInProgress snapshotsInProgress = stateResponse.getState().custom(SnapshotsInProgress.TYPE);
            if (snapshotsInProgress == null) {
                return snapshotInfos.get(0);
            } else {
                boolean found = false;
                for (SnapshotsInProgress.Entry entry : snapshotsInProgress.entries()) {
                    final Snapshot curr = entry.snapshot();
                    if (curr.getRepository().equals(repository) && curr.getSnapshotId().getName().equals(snapshotName)) {
                        found = true;
                        break;
                    }
                }
                if (found == false) {
                    return snapshotInfos.get(0);
                }
            }
        }
        Thread.sleep(100);
    }
    fail("Timeout!!!");
    return null;
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress)

Example 64 with ClusterStateResponse

use of org.elasticsearch.action.admin.cluster.state.ClusterStateResponse in project crate by crate.

the class SQLIntegrationTestCase method getIndexSettings.

/**
 * Get the IndexSettings as JSON String
 *
 * @param index the name of the index
 * @return the IndexSettings as JSON String
 * @throws IOException
 */
protected String getIndexSettings(String index) throws IOException {
    ClusterStateRequest request = Requests.clusterStateRequest().routingTable(false).nodes(false).metadata(true).indices(index);
    ClusterStateResponse response = client().admin().cluster().state(request).actionGet();
    Metadata metadata = response.getState().metadata();
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
    for (IndexMetadata indexMetadata : metadata) {
        builder.startObject(indexMetadata.getIndex().getName());
        builder.startObject("settings");
        Settings settings = indexMetadata.getSettings();
        for (String settingName : settings.keySet()) {
            builder.field(settingName, settings.get(settingName));
        }
        builder.endObject();
        builder.endObject();
    }
    builder.endObject();
    return Strings.toString(builder);
}
Also used : ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.elasticsearch.action.admin.cluster.state.ClusterStateRequest) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SessionSettings(io.crate.metadata.settings.SessionSettings) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)64 Settings (org.elasticsearch.common.settings.Settings)25 ClusterStateRequest (org.elasticsearch.action.admin.cluster.state.ClusterStateRequest)21 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)13 NodeClient (org.elasticsearch.client.node.NodeClient)12 RestController (org.elasticsearch.rest.RestController)12 RestRequest (org.elasticsearch.rest.RestRequest)12 RestResponse (org.elasticsearch.rest.RestResponse)12 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)11 ClusterState (org.elasticsearch.cluster.ClusterState)10 Table (org.elasticsearch.common.Table)10 GET (org.elasticsearch.rest.RestRequest.Method.GET)10 RestResponseListener (org.elasticsearch.rest.action.RestResponseListener)10 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)9 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)8 RestActionListener (org.elasticsearch.rest.action.RestActionListener)8 Strings (org.elasticsearch.common.Strings)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 MetaData (org.elasticsearch.cluster.metadata.MetaData)6