use of org.elasticsearch.cluster.node.DiscoveryNodes in project elasticsearch by elastic.
the class ClusterService method patchVersionsAndNoMasterBlocks.
private ClusterState patchVersionsAndNoMasterBlocks(ClusterState previousClusterState, ClusterTasksResult<Object> executionResult) {
ClusterState newClusterState = executionResult.resultingState;
if (executionResult.noMaster) {
assert newClusterState == previousClusterState : "state can only be changed by ClusterService when noMaster = true";
if (previousClusterState.nodes().getMasterNodeId() != null) {
// remove block if it already exists before adding new one
assert previousClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id()) == false : "NO_MASTER_BLOCK should only be added by ClusterService";
ClusterBlocks clusterBlocks = ClusterBlocks.builder().blocks(previousClusterState.blocks()).addGlobalBlock(discoverySettings.getNoMasterBlock()).build();
DiscoveryNodes discoveryNodes = new DiscoveryNodes.Builder(previousClusterState.nodes()).masterNodeId(null).build();
newClusterState = ClusterState.builder(previousClusterState).blocks(clusterBlocks).nodes(discoveryNodes).build();
}
} else if (newClusterState.nodes().isLocalNodeElectedMaster() && previousClusterState != newClusterState) {
// only the master controls the version numbers
Builder builder = ClusterState.builder(newClusterState).incrementVersion();
if (previousClusterState.routingTable() != newClusterState.routingTable()) {
builder.routingTable(RoutingTable.builder(newClusterState.routingTable()).version(newClusterState.routingTable().version() + 1).build());
}
if (previousClusterState.metaData() != newClusterState.metaData()) {
builder.metaData(MetaData.builder(newClusterState.metaData()).version(newClusterState.metaData().version() + 1));
}
// remove the no master block, if it exists
if (newClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id())) {
builder.blocks(ClusterBlocks.builder().blocks(newClusterState.blocks()).removeGlobalBlock(discoverySettings.getNoMasterBlock().id()));
}
newClusterState = builder.build();
}
assert newClusterState.nodes().getMasterNodeId() == null || newClusterState.blocks().hasGlobalBlock(discoverySettings.getNoMasterBlock().id()) == false : "cluster state with master node must not have NO_MASTER_BLOCK";
return newClusterState;
}
use of org.elasticsearch.cluster.node.DiscoveryNodes in project elasticsearch by elastic.
the class RestMasterAction method buildTable.
private Table buildTable(RestRequest request, ClusterStateResponse state) {
Table table = getTableWithHeader(request);
DiscoveryNodes nodes = state.getState().nodes();
table.startRow();
DiscoveryNode master = nodes.get(nodes.getMasterNodeId());
if (master == null) {
table.addCell("-");
table.addCell("-");
table.addCell("-");
table.addCell("-");
} else {
table.addCell(master.getId());
table.addCell(master.getHostName());
table.addCell(master.getHostAddress());
table.addCell(master.getName());
}
table.endRow();
return table;
}
use of org.elasticsearch.cluster.node.DiscoveryNodes in project elasticsearch by elastic.
the class RestSegmentsAction method buildTable.
private Table buildTable(final RestRequest request, ClusterStateResponse state, Map<String, IndexSegments> indicesSegments) {
Table table = getTableWithHeader(request);
DiscoveryNodes nodes = state.getState().nodes();
for (IndexSegments indexSegments : indicesSegments.values()) {
Map<Integer, IndexShardSegments> shards = indexSegments.getShards();
for (IndexShardSegments indexShardSegments : shards.values()) {
ShardSegments[] shardSegments = indexShardSegments.getShards();
for (ShardSegments shardSegment : shardSegments) {
List<Segment> segments = shardSegment.getSegments();
for (Segment segment : segments) {
table.startRow();
table.addCell(shardSegment.getShardRouting().getIndexName());
table.addCell(shardSegment.getShardRouting().getId());
table.addCell(shardSegment.getShardRouting().primary() ? "p" : "r");
table.addCell(nodes.get(shardSegment.getShardRouting().currentNodeId()).getHostAddress());
table.addCell(shardSegment.getShardRouting().currentNodeId());
table.addCell(segment.getName());
table.addCell(segment.getGeneration());
table.addCell(segment.getNumDocs());
table.addCell(segment.getDeletedDocs());
table.addCell(segment.getSize());
table.addCell(segment.getMemoryInBytes());
table.addCell(segment.isCommitted());
table.addCell(segment.isSearch());
table.addCell(segment.getVersion());
table.addCell(segment.isCompound());
table.endRow();
}
}
}
}
return table;
}
use of org.elasticsearch.cluster.node.DiscoveryNodes in project elasticsearch by elastic.
the class SnapshotsService method processSnapshotsOnRemovedNodes.
/**
* Cleans up shard snapshots that were running on removed nodes
*
* @param event cluster changed event
*/
private void processSnapshotsOnRemovedNodes(ClusterChangedEvent event) {
if (removedNodesCleanupNeeded(event)) {
// Check if we just became the master
final boolean newMaster = !event.previousState().nodes().isLocalNodeElectedMaster();
clusterService.submitStateUpdateTask("update snapshot state after node removal", new ClusterStateUpdateTask() {
@Override
public ClusterState execute(ClusterState currentState) throws Exception {
DiscoveryNodes nodes = currentState.nodes();
SnapshotsInProgress snapshots = currentState.custom(SnapshotsInProgress.TYPE);
if (snapshots == null) {
return currentState;
}
boolean changed = false;
ArrayList<SnapshotsInProgress.Entry> entries = new ArrayList<>();
for (final SnapshotsInProgress.Entry snapshot : snapshots.entries()) {
SnapshotsInProgress.Entry updatedSnapshot = snapshot;
boolean snapshotChanged = false;
if (snapshot.state() == State.STARTED || snapshot.state() == State.ABORTED) {
ImmutableOpenMap.Builder<ShardId, ShardSnapshotStatus> shards = ImmutableOpenMap.builder();
for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardEntry : snapshot.shards()) {
ShardSnapshotStatus shardStatus = shardEntry.value;
if (!shardStatus.state().completed() && shardStatus.nodeId() != null) {
if (nodes.nodeExists(shardStatus.nodeId())) {
shards.put(shardEntry.key, shardEntry.value);
} else {
// TODO: Restart snapshot on another node?
snapshotChanged = true;
logger.warn("failing snapshot of shard [{}] on closed node [{}]", shardEntry.key, shardStatus.nodeId());
shards.put(shardEntry.key, new ShardSnapshotStatus(shardStatus.nodeId(), State.FAILED, "node shutdown"));
}
}
}
if (snapshotChanged) {
changed = true;
ImmutableOpenMap<ShardId, ShardSnapshotStatus> shardsMap = shards.build();
if (!snapshot.state().completed() && completed(shardsMap.values())) {
updatedSnapshot = new SnapshotsInProgress.Entry(snapshot, State.SUCCESS, shardsMap);
endSnapshot(updatedSnapshot);
} else {
updatedSnapshot = new SnapshotsInProgress.Entry(snapshot, snapshot.state(), shardsMap);
}
}
entries.add(updatedSnapshot);
} else if (snapshot.state() == State.INIT && newMaster) {
// Clean up the snapshot that failed to start from the old master
deleteSnapshot(snapshot.snapshot(), new DeleteSnapshotListener() {
@Override
public void onResponse() {
logger.debug("cleaned up abandoned snapshot {} in INIT state", snapshot.snapshot());
}
@Override
public void onFailure(Exception e) {
logger.warn("failed to clean up abandoned snapshot {} in INIT state", snapshot.snapshot());
}
}, updatedSnapshot.getRepositoryStateId(), false);
} else if (snapshot.state() == State.SUCCESS && newMaster) {
// Finalize the snapshot
endSnapshot(snapshot);
}
}
if (changed) {
snapshots = new SnapshotsInProgress(entries.toArray(new SnapshotsInProgress.Entry[entries.size()]));
return ClusterState.builder(currentState).putCustom(SnapshotsInProgress.TYPE, snapshots).build();
}
return currentState;
}
@Override
public void onFailure(String source, Exception e) {
logger.warn("failed to update snapshot state after node removal");
}
});
}
}
use of org.elasticsearch.cluster.node.DiscoveryNodes in project elasticsearch by elastic.
the class UnicastZenPing method ping.
/**
* a variant of {@link #ping(Consumer, TimeValue)}, but allows separating the scheduling duration
* from the duration used for request level time outs. This is useful for testing
*/
protected void ping(final Consumer<PingCollection> resultsConsumer, final TimeValue scheduleDuration, final TimeValue requestDuration) {
final List<DiscoveryNode> seedNodes;
try {
seedNodes = resolveHostsLists(unicastZenPingExecutorService, logger, configuredHosts, limitPortCounts, transportService, UNICAST_NODE_PREFIX, resolveTimeout);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
seedNodes.addAll(hostsProvider.buildDynamicNodes());
final DiscoveryNodes nodes = contextProvider.nodes();
// add all possible master nodes that were active in the last known cluster configuration
for (ObjectCursor<DiscoveryNode> masterNode : nodes.getMasterNodes().values()) {
seedNodes.add(masterNode.value);
}
final ConnectionProfile connectionProfile = ConnectionProfile.buildSingleChannelProfile(TransportRequestOptions.Type.REG, requestDuration, requestDuration);
final PingingRound pingingRound = new PingingRound(pingingRoundIdGenerator.incrementAndGet(), seedNodes, resultsConsumer, nodes.getLocalNode(), connectionProfile);
activePingingRounds.put(pingingRound.id(), pingingRound);
final AbstractRunnable pingSender = new AbstractRunnable() {
@Override
public void onFailure(Exception e) {
if (e instanceof AlreadyClosedException == false) {
logger.warn("unexpected error while pinging", e);
}
}
@Override
protected void doRun() throws Exception {
sendPings(requestDuration, pingingRound);
}
};
threadPool.generic().execute(pingSender);
threadPool.schedule(TimeValue.timeValueMillis(scheduleDuration.millis() / 3), ThreadPool.Names.GENERIC, pingSender);
threadPool.schedule(TimeValue.timeValueMillis(scheduleDuration.millis() / 3 * 2), ThreadPool.Names.GENERIC, pingSender);
threadPool.schedule(scheduleDuration, ThreadPool.Names.GENERIC, new AbstractRunnable() {
@Override
protected void doRun() throws Exception {
finishPingingRound(pingingRound);
}
@Override
public void onFailure(Exception e) {
logger.warn("unexpected error while finishing pinging round", e);
}
});
}
Aggregations