-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform] Check node shutdown before fail (#107358)
Transforms continue to run even when a node is shutting down. This may lead to a transform failing and putting itself into a failed state, which will prevent it from restarting when the node comes back online. The transform will now abort rather than fail, which puts itself into a started state. When the node comes back online, or another node in the cluster starts the transform, then the transform will pick up from its last successful saved state and checkpoint. Close #100891
- Loading branch information
Showing
15 changed files
with
328 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pr: 107358 | ||
summary: Check node shutdown before fail | ||
area: Transform | ||
type: enhancement | ||
issues: | ||
- 100891 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/TransformNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.transform; | ||
|
||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Stateful representation of this node, relevant to the {@link org.elasticsearch.xpack.transform.transforms.TransformTask}. | ||
* For stateless functions, see {@link org.elasticsearch.xpack.transform.transforms.TransformNodes}. | ||
*/ | ||
public class TransformNode { | ||
private final Supplier<Optional<ClusterState>> clusterState; | ||
|
||
public TransformNode(Supplier<Optional<ClusterState>> clusterState) { | ||
this.clusterState = clusterState; | ||
} | ||
|
||
/** | ||
* @return an optional containing true if this node is reported as shutting down in the cluster state metadata, false if it is not | ||
* reported as shutting down, or empty if the cluster state is missing or the local node has not been set yet. | ||
*/ | ||
public Optional<Boolean> isShuttingDown() { | ||
return clusterState.get().map(state -> { | ||
var localId = state.nodes().getLocalNodeId(); | ||
if (localId != null) { | ||
return state.metadata().nodeShutdowns().contains(localId); | ||
} else { | ||
return null; // empty | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return the node id stored in the cluster state, or "null" if the cluster state is missing or the local node has not been set yet. | ||
* This should behave exactly as {@link String#valueOf(Object)}. | ||
*/ | ||
public String nodeId() { | ||
return clusterState.get().map(ClusterState::nodes).map(DiscoveryNodes::getLocalNodeId).orElse("null"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
.../plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformNodeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.transform; | ||
|
||
import org.elasticsearch.cluster.ClusterName; | ||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.metadata.Metadata; | ||
import org.elasticsearch.cluster.metadata.NodesShutdownMetadata; | ||
import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata; | ||
import org.elasticsearch.cluster.node.DiscoveryNodeUtils; | ||
import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class TransformNodeTests extends ESTestCase { | ||
private static final String SHUTTING_DOWN_ID = "shuttingDownNodeId"; | ||
private static final String NOT_SHUTTING_DOWN_ID = "notShuttingDownId"; | ||
|
||
/** | ||
* When the local node is shutting down | ||
* Then we return true | ||
*/ | ||
public void testIsShuttingDown() { | ||
var isShuttingDown = new TransformNode(clusterState(SHUTTING_DOWN_ID)).isShuttingDown(); | ||
assertTrue(isShuttingDown.isPresent()); | ||
assertTrue(isShuttingDown.get()); | ||
} | ||
|
||
/** | ||
* When the local node is not shutting down | ||
* Then we return false | ||
*/ | ||
public void testIsNotShuttingDown() { | ||
var isShuttingDown = new TransformNode(clusterState(NOT_SHUTTING_DOWN_ID)).isShuttingDown(); | ||
assertTrue(isShuttingDown.isPresent()); | ||
assertFalse(isShuttingDown.get()); | ||
} | ||
|
||
/** | ||
* When the local node is null | ||
* Then we return empty | ||
*/ | ||
public void testMissingLocalId() { | ||
var isShuttingDown = new TransformNode(clusterState(null)).isShuttingDown(); | ||
assertFalse(isShuttingDown.isPresent()); | ||
} | ||
|
||
/** | ||
* When the cluster state is empty | ||
* Then we return empty | ||
*/ | ||
public void testClusterStateMissing() { | ||
var isShuttingDown = new TransformNode(Optional::empty).isShuttingDown(); | ||
assertFalse(isShuttingDown.isPresent()); | ||
} | ||
|
||
/** | ||
* When there is a local node | ||
* Then return its id | ||
*/ | ||
public void testNodeId() { | ||
var nodeId = new TransformNode(clusterState(SHUTTING_DOWN_ID)).nodeId(); | ||
assertThat(nodeId, equalTo(SHUTTING_DOWN_ID)); | ||
} | ||
|
||
/** | ||
* When the local node is null | ||
* Then return "null" | ||
*/ | ||
public void testNodeIdMissing() { | ||
var nodeId = new TransformNode(Optional::empty).nodeId(); | ||
assertThat(nodeId, equalTo(String.valueOf((String) null))); | ||
} | ||
|
||
private Supplier<Optional<ClusterState>> clusterState(String nodeId) { | ||
var nodesShutdownMetadata = new NodesShutdownMetadata( | ||
Map.of( | ||
SHUTTING_DOWN_ID, | ||
SingleNodeShutdownMetadata.builder() | ||
.setNodeId(SHUTTING_DOWN_ID) | ||
.setReason("shutdown for a unit test") | ||
.setType(SingleNodeShutdownMetadata.Type.RESTART) | ||
.setStartedAtMillis(randomNonNegativeLong()) | ||
.setGracePeriod(null) | ||
.build() | ||
) | ||
); | ||
|
||
var nodes = DiscoveryNodes.builder().add(DiscoveryNodeUtils.create(SHUTTING_DOWN_ID)).localNodeId(nodeId).masterNodeId(nodeId); | ||
|
||
if (SHUTTING_DOWN_ID.equals(nodeId) == false && nodeId != null) { | ||
nodes.add(DiscoveryNodeUtils.create(nodeId)); | ||
} | ||
|
||
var state = ClusterState.builder(ClusterName.DEFAULT) | ||
.metadata(Metadata.builder().putCustom(NodesShutdownMetadata.TYPE, nodesShutdownMetadata).build()) | ||
.nodes(nodes) | ||
.build(); | ||
|
||
return () -> Optional.of(state); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.