Search in sources :

Example 36 with DiscoveryNode

use of org.elasticsearch.cluster.node.DiscoveryNode in project elasticsearch by elastic.

the class StartRecoveryRequestTests method testSerialization.

public void testSerialization() throws Exception {
    final Version targetNodeVersion = randomVersion(random());
    final StartRecoveryRequest outRequest = new StartRecoveryRequest(new ShardId("test", "_na_", 0), new DiscoveryNode("a", buildNewFakeTransportAddress(), emptyMap(), emptySet(), targetNodeVersion), new DiscoveryNode("b", buildNewFakeTransportAddress(), emptyMap(), emptySet(), targetNodeVersion), Store.MetadataSnapshot.EMPTY, randomBoolean(), randomNonNegativeLong(), randomBoolean() ? SequenceNumbersService.UNASSIGNED_SEQ_NO : randomNonNegativeLong());
    final ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    final OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer);
    out.setVersion(targetNodeVersion);
    outRequest.writeTo(out);
    final ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
    InputStreamStreamInput in = new InputStreamStreamInput(inBuffer);
    in.setVersion(targetNodeVersion);
    final StartRecoveryRequest inRequest = new StartRecoveryRequest();
    inRequest.readFrom(in);
    assertThat(outRequest.shardId(), equalTo(inRequest.shardId()));
    assertThat(outRequest.sourceNode(), equalTo(inRequest.sourceNode()));
    assertThat(outRequest.targetNode(), equalTo(inRequest.targetNode()));
    assertThat(outRequest.metadataSnapshot().asMap(), equalTo(inRequest.metadataSnapshot().asMap()));
    assertThat(outRequest.isPrimaryRelocation(), equalTo(inRequest.isPrimaryRelocation()));
    assertThat(outRequest.recoveryId(), equalTo(inRequest.recoveryId()));
    if (targetNodeVersion.onOrAfter(Version.V_6_0_0_alpha1_UNRELEASED)) {
        assertThat(outRequest.startingSeqNo(), equalTo(inRequest.startingSeqNo()));
    } else {
        assertThat(SequenceNumbersService.UNASSIGNED_SEQ_NO, equalTo(inRequest.startingSeqNo()));
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) OutputStreamStreamOutput(org.elasticsearch.common.io.stream.OutputStreamStreamOutput) Version(org.elasticsearch.Version) VersionUtils.randomVersion(org.elasticsearch.test.VersionUtils.randomVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputStreamStreamInput(org.elasticsearch.common.io.stream.InputStreamStreamInput)

Example 37 with DiscoveryNode

use of org.elasticsearch.cluster.node.DiscoveryNode in project elasticsearch by elastic.

the class RestRecoveryActionTests method testRestRecoveryAction.

public void testRestRecoveryAction() {
    final Settings settings = Settings.EMPTY;
    final RestController restController = new RestController(settings, Collections.emptySet(), null, null, null);
    final RestRecoveryAction action = new RestRecoveryAction(settings, restController);
    final int totalShards = randomIntBetween(1, 32);
    final int successfulShards = Math.max(0, totalShards - randomIntBetween(1, 2));
    final int failedShards = totalShards - successfulShards;
    final boolean detailed = randomBoolean();
    final Map<String, List<RecoveryState>> shardRecoveryStates = new HashMap<>();
    final List<RecoveryState> recoveryStates = new ArrayList<>();
    for (int i = 0; i < successfulShards; i++) {
        final RecoveryState state = mock(RecoveryState.class);
        when(state.getShardId()).thenReturn(new ShardId(new Index("index", "_na_"), i));
        final RecoveryState.Timer timer = mock(RecoveryState.Timer.class);
        when(timer.time()).thenReturn((long) randomIntBetween(1000000, 10 * 1000000));
        when(state.getTimer()).thenReturn(timer);
        when(state.getRecoverySource()).thenReturn(TestShardRouting.randomRecoverySource());
        when(state.getStage()).thenReturn(randomFrom(RecoveryState.Stage.values()));
        final DiscoveryNode sourceNode = randomBoolean() ? mock(DiscoveryNode.class) : null;
        if (sourceNode != null) {
            when(sourceNode.getHostName()).thenReturn(randomAsciiOfLength(8));
        }
        when(state.getSourceNode()).thenReturn(sourceNode);
        final DiscoveryNode targetNode = mock(DiscoveryNode.class);
        when(targetNode.getHostName()).thenReturn(randomAsciiOfLength(8));
        when(state.getTargetNode()).thenReturn(targetNode);
        RecoveryState.Index index = mock(RecoveryState.Index.class);
        final int totalRecoveredFiles = randomIntBetween(1, 64);
        when(index.totalRecoverFiles()).thenReturn(totalRecoveredFiles);
        final int recoveredFileCount = randomIntBetween(0, totalRecoveredFiles);
        when(index.recoveredFileCount()).thenReturn(recoveredFileCount);
        when(index.recoveredFilesPercent()).thenReturn((100f * recoveredFileCount) / totalRecoveredFiles);
        when(index.totalFileCount()).thenReturn(randomIntBetween(totalRecoveredFiles, 2 * totalRecoveredFiles));
        final int totalRecoveredBytes = randomIntBetween(1, 1 << 24);
        when(index.totalRecoverBytes()).thenReturn((long) totalRecoveredBytes);
        final int recoveredBytes = randomIntBetween(0, totalRecoveredBytes);
        when(index.recoveredBytes()).thenReturn((long) recoveredBytes);
        when(index.recoveredBytesPercent()).thenReturn((100f * recoveredBytes) / totalRecoveredBytes);
        when(index.totalRecoverBytes()).thenReturn((long) randomIntBetween(totalRecoveredBytes, 2 * totalRecoveredBytes));
        when(state.getIndex()).thenReturn(index);
        final RecoveryState.Translog translog = mock(RecoveryState.Translog.class);
        final int translogOps = randomIntBetween(0, 1 << 18);
        when(translog.totalOperations()).thenReturn(translogOps);
        final int translogOpsRecovered = randomIntBetween(0, translogOps);
        when(translog.recoveredOperations()).thenReturn(translogOpsRecovered);
        when(translog.recoveredPercent()).thenReturn(translogOps == 0 ? 100f : (100f * translogOpsRecovered / translogOps));
        when(state.getTranslog()).thenReturn(translog);
        recoveryStates.add(state);
    }
    final List<RecoveryState> shuffle = new ArrayList<>(recoveryStates);
    Randomness.shuffle(shuffle);
    shardRecoveryStates.put("index", shuffle);
    final List<ShardOperationFailedException> shardFailures = new ArrayList<>();
    final RecoveryResponse response = new RecoveryResponse(totalShards, successfulShards, failedShards, detailed, shardRecoveryStates, shardFailures);
    final Table table = action.buildRecoveryTable(null, response);
    assertNotNull(table);
    List<Table.Cell> headers = table.getHeaders();
    assertThat(headers.get(0).value, equalTo("index"));
    assertThat(headers.get(1).value, equalTo("shard"));
    assertThat(headers.get(2).value, equalTo("time"));
    assertThat(headers.get(3).value, equalTo("type"));
    assertThat(headers.get(4).value, equalTo("stage"));
    assertThat(headers.get(5).value, equalTo("source_host"));
    assertThat(headers.get(6).value, equalTo("source_node"));
    assertThat(headers.get(7).value, equalTo("target_host"));
    assertThat(headers.get(8).value, equalTo("target_node"));
    assertThat(headers.get(9).value, equalTo("repository"));
    assertThat(headers.get(10).value, equalTo("snapshot"));
    assertThat(headers.get(11).value, equalTo("files"));
    assertThat(headers.get(12).value, equalTo("files_recovered"));
    assertThat(headers.get(13).value, equalTo("files_percent"));
    assertThat(headers.get(14).value, equalTo("files_total"));
    assertThat(headers.get(15).value, equalTo("bytes"));
    assertThat(headers.get(16).value, equalTo("bytes_recovered"));
    assertThat(headers.get(17).value, equalTo("bytes_percent"));
    assertThat(headers.get(18).value, equalTo("bytes_total"));
    assertThat(headers.get(19).value, equalTo("translog_ops"));
    assertThat(headers.get(20).value, equalTo("translog_ops_recovered"));
    assertThat(headers.get(21).value, equalTo("translog_ops_percent"));
    assertThat(table.getRows().size(), equalTo(successfulShards));
    for (int i = 0; i < successfulShards; i++) {
        final RecoveryState state = recoveryStates.get(i);
        List<Table.Cell> cells = table.getRows().get(i);
        assertThat(cells.get(0).value, equalTo("index"));
        assertThat(cells.get(1).value, equalTo(i));
        assertThat(cells.get(2).value, equalTo(new TimeValue(state.getTimer().time())));
        assertThat(cells.get(3).value, equalTo(state.getRecoverySource().getType().name().toLowerCase(Locale.ROOT)));
        assertThat(cells.get(4).value, equalTo(state.getStage().name().toLowerCase(Locale.ROOT)));
        assertThat(cells.get(5).value, equalTo(state.getSourceNode() == null ? "n/a" : state.getSourceNode().getHostName()));
        assertThat(cells.get(6).value, equalTo(state.getSourceNode() == null ? "n/a" : state.getSourceNode().getName()));
        assertThat(cells.get(7).value, equalTo(state.getTargetNode().getHostName()));
        assertThat(cells.get(8).value, equalTo(state.getTargetNode().getName()));
        assertThat(cells.get(9).value, equalTo(state.getRecoverySource() == null || state.getRecoverySource().getType() != RecoverySource.Type.SNAPSHOT ? "n/a" : ((SnapshotRecoverySource) state.getRecoverySource()).snapshot().getRepository()));
        assertThat(cells.get(10).value, equalTo(state.getRecoverySource() == null || state.getRecoverySource().getType() != RecoverySource.Type.SNAPSHOT ? "n/a" : ((SnapshotRecoverySource) state.getRecoverySource()).snapshot().getSnapshotId().getName()));
        assertThat(cells.get(11).value, equalTo(state.getIndex().totalRecoverFiles()));
        assertThat(cells.get(12).value, equalTo(state.getIndex().recoveredFileCount()));
        assertThat(cells.get(13).value, equalTo(percent(state.getIndex().recoveredFilesPercent())));
        assertThat(cells.get(14).value, equalTo(state.getIndex().totalFileCount()));
        assertThat(cells.get(15).value, equalTo(state.getIndex().totalRecoverBytes()));
        assertThat(cells.get(16).value, equalTo(state.getIndex().recoveredBytes()));
        assertThat(cells.get(17).value, equalTo(percent(state.getIndex().recoveredBytesPercent())));
        assertThat(cells.get(18).value, equalTo(state.getIndex().totalBytes()));
        assertThat(cells.get(19).value, equalTo(state.getTranslog().totalOperations()));
        assertThat(cells.get(20).value, equalTo(state.getTranslog().recoveredOperations()));
        assertThat(cells.get(21).value, equalTo(percent(state.getTranslog().recoveredPercent())));
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RestController(org.elasticsearch.rest.RestController) Index(org.elasticsearch.index.Index) RecoveryResponse(org.elasticsearch.action.admin.indices.recovery.RecoveryResponse) ShardId(org.elasticsearch.index.shard.ShardId) ArrayList(java.util.ArrayList) List(java.util.List) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) Table(org.elasticsearch.common.Table) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource)

Example 38 with DiscoveryNode

use of org.elasticsearch.cluster.node.DiscoveryNode in project elasticsearch by elastic.

the class RecoveriesCollectionTests method startRecovery.

long startRecovery(RecoveriesCollection collection, DiscoveryNode sourceNode, IndexShard indexShard, PeerRecoveryTargetService.RecoveryListener listener, TimeValue timeValue) {
    final DiscoveryNode rNode = getDiscoveryNode(indexShard.routingEntry().currentNodeId());
    indexShard.markAsRecovering("remote", new RecoveryState(indexShard.routingEntry(), sourceNode, rNode));
    indexShard.prepareForIndexRecovery();
    return collection.startRecovery(indexShard, sourceNode, listener, timeValue);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState)

Example 39 with DiscoveryNode

use of org.elasticsearch.cluster.node.DiscoveryNode in project elasticsearch by elastic.

the class ListTasksResponse method toXContentGroupedByNode.

/**
     * Convert this task response to XContent grouping by executing nodes.
     */
public XContentBuilder toXContentGroupedByNode(XContentBuilder builder, Params params, DiscoveryNodes discoveryNodes) throws IOException {
    toXContentCommon(builder, params);
    builder.startObject("nodes");
    for (Map.Entry<String, List<TaskInfo>> entry : getPerNodeTasks().entrySet()) {
        DiscoveryNode node = discoveryNodes.get(entry.getKey());
        builder.startObject(entry.getKey());
        if (node != null) {
            // If the node is no longer part of the cluster, oh well, we'll just skip it's useful information.
            builder.field("name", node.getName());
            builder.field("transport_address", node.getAddress().toString());
            builder.field("host", node.getHostName());
            builder.field("ip", node.getAddress());
            builder.startArray("roles");
            for (DiscoveryNode.Role role : node.getRoles()) {
                builder.value(role.getRoleName());
            }
            builder.endArray();
            if (!node.getAttributes().isEmpty()) {
                builder.startObject("attributes");
                for (Map.Entry<String, String> attrEntry : node.getAttributes().entrySet()) {
                    builder.field(attrEntry.getKey(), attrEntry.getValue());
                }
                builder.endObject();
            }
        }
        builder.startObject("tasks");
        for (TaskInfo task : entry.getValue()) {
            builder.startObject(task.getTaskId().toString());
            task.toXContent(builder, params);
            builder.endObject();
        }
        builder.endObject();
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
Also used : TaskInfo(org.elasticsearch.tasks.TaskInfo) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 40 with DiscoveryNode

use of org.elasticsearch.cluster.node.DiscoveryNode in project elasticsearch by elastic.

the class VerifyRepositoryResponse method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    clusterName.writeTo(out);
    out.writeVInt(nodes.length);
    for (DiscoveryNode node : nodes) {
        node.writeTo(out);
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode)

Aggregations

DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)352 ClusterState (org.elasticsearch.cluster.ClusterState)83 ArrayList (java.util.ArrayList)82 Settings (org.elasticsearch.common.settings.Settings)79 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)74 IOException (java.io.IOException)69 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)52 HashMap (java.util.HashMap)45 ShardId (org.elasticsearch.index.shard.ShardId)45 HashSet (java.util.HashSet)43 List (java.util.List)41 TransportAddress (org.elasticsearch.common.transport.TransportAddress)41 CountDownLatch (java.util.concurrent.CountDownLatch)39 MockTransportService (org.elasticsearch.test.transport.MockTransportService)39 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)37 Map (java.util.Map)35 ExecutionException (java.util.concurrent.ExecutionException)35 Version (org.elasticsearch.Version)35 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)31 ClusterName (org.elasticsearch.cluster.ClusterName)30