-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[Zen2] Add lag detector #35685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Zen2] Add lag detector #35685
Changes from 1 commit
c8ddb4f
fa3f3fc
e6f3291
b39750b
4fbd9a8
4cc00ac
dea2640
213436f
286eddd
409bf3f
97578a5
31218bc
3102f8c
cdcc190
7150336
f956db5
63b21ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.elasticsearch.cluster.coordination; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| import org.elasticsearch.common.settings.Setting; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.unit.TimeValue; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.threadpool.ThreadPool.Names; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import static org.elasticsearch.common.util.concurrent.ConcurrentCollections.newConcurrentMap; | ||
|
|
||
| /** | ||
| * A publication can succeed and complete before all nodes have applied the published state and acknowledged it; however we need every node | ||
| * eventually either to apply the published state (or a later state) or be removed from the cluster. This component achieves this by | ||
| * removing any lagging nodes from the cluster after a timeout. | ||
| */ | ||
| public class LagDetector { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(LagDetector.class); | ||
|
|
||
| // the timeout for each node to apply a value after the end of publication | ||
| public static final Setting<TimeValue> CLUSTER_STATE_APPLICATION_TIMEOUT_SETTING = | ||
ywelsch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Setting.timeSetting("cluster.applier.timeout", | ||
| TimeValue.timeValueMillis(60000), TimeValue.timeValueMillis(1), Setting.Property.NodeScope); | ||
|
|
||
| private final TimeValue clusterStateApplicationTimeout; | ||
| private final Consumer<DiscoveryNode> onLagDetected; | ||
| private final ThreadPool threadPool; | ||
| private final Map<DiscoveryNode, NodeAppliedStateTracker> appliedStateTrackersByNode = newConcurrentMap(); | ||
|
|
||
| public LagDetector(final Settings settings, final ThreadPool threadPool, final Consumer<DiscoveryNode> onLagDetected) { | ||
| this.threadPool = threadPool; | ||
| this.clusterStateApplicationTimeout = CLUSTER_STATE_APPLICATION_TIMEOUT_SETTING.get(settings); | ||
| this.onLagDetected = onLagDetected; | ||
| } | ||
|
|
||
| public void setTrackedNodes(final Iterable<DiscoveryNode> discoveryNodes) { | ||
| final Set<DiscoveryNode> discoveryNodeSet = new HashSet<>(); | ||
| discoveryNodes.forEach(discoveryNodeSet::add); | ||
ywelsch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| appliedStateTrackersByNode.keySet().retainAll(discoveryNodeSet); | ||
| discoveryNodeSet.forEach(node -> appliedStateTrackersByNode.putIfAbsent(node, new NodeAppliedStateTracker(node))); | ||
| } | ||
|
|
||
| public void clearTrackedNodes() { | ||
| appliedStateTrackersByNode.clear(); | ||
| } | ||
|
|
||
| public void setAppliedVersion(final DiscoveryNode discoveryNode, final long appliedVersion) { | ||
| final NodeAppliedStateTracker nodeAppliedStateTracker = appliedStateTrackersByNode.get(discoveryNode); | ||
| if (nodeAppliedStateTracker == null) { | ||
| logger.trace("node {} applied version {} but this node's version is not being tracked", discoveryNode, appliedVersion); | ||
ywelsch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } else { | ||
| nodeAppliedStateTracker.increaseAppliedVersion(appliedVersion); | ||
| } | ||
| } | ||
|
|
||
| public void startLagDetector(final long version) { | ||
| appliedStateTrackersByNode.values().forEach(nodeAppliedStateTracker -> nodeAppliedStateTracker.startLagDetector(version)); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "LagDetector{" + | ||
| "clusterStateApplicationTimeout=" + clusterStateApplicationTimeout + | ||
| ", appliedStateTrackersByNode=" + appliedStateTrackersByNode.values() + | ||
| '}'; | ||
| } | ||
|
|
||
| // for assertions | ||
| Set<DiscoveryNode> getTrackedNodes() { | ||
| return Collections.unmodifiableSet(appliedStateTrackersByNode.keySet()); | ||
| } | ||
|
|
||
| private class NodeAppliedStateTracker { | ||
| private final DiscoveryNode discoveryNode; | ||
| private final AtomicLong appliedVersion = new AtomicLong(); | ||
|
|
||
| NodeAppliedStateTracker(final DiscoveryNode discoveryNode) { | ||
| this.discoveryNode = discoveryNode; | ||
| } | ||
|
|
||
| void increaseAppliedVersion(long appliedVersion) { | ||
| long maxAppliedVersion = this.appliedVersion.updateAndGet(v -> Math.max(v, appliedVersion)); | ||
| logger.trace("{} applied version {}, max now {}", this, appliedVersion, maxAppliedVersion); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "NodeAppliedStateTracker{" + | ||
| "discoveryNode=" + discoveryNode + | ||
| ", appliedVersion=" + appliedVersion + | ||
| '}'; | ||
| } | ||
|
|
||
| void startLagDetector(final long version) { | ||
| final long appliedVersionWhenStarted = appliedVersion.get(); | ||
| if (version <= appliedVersionWhenStarted) { | ||
| logger.trace("lag detection for {} for version {} unnecessary, node has already applied version {}", | ||
| discoveryNode, version, appliedVersionWhenStarted); | ||
| return; | ||
| } | ||
|
|
||
| threadPool.schedule(clusterStateApplicationTimeout, Names.GENERIC, new Runnable() { | ||
ywelsch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Override | ||
| public void run() { | ||
| if (appliedStateTrackersByNode.get(discoveryNode) != NodeAppliedStateTracker.this) { | ||
| logger.trace("{}, no longer active", this); | ||
| return; | ||
| } | ||
|
|
||
| long appliedVersion = NodeAppliedStateTracker.this.appliedVersion.get(); | ||
| if (version <= appliedVersion) { | ||
| logger.trace("{}, satisfied, node applied version {}", this, appliedVersion); | ||
| return; | ||
| } | ||
|
|
||
| logger.debug("{}, detected lag, node has only applied version {}", this, appliedVersion); | ||
| onLagDetected.accept(discoveryNode); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "lag detection for " + discoveryNode + " started at version " + appliedVersionWhenStarted | ||
| + ", expected version " + version; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.function.LongSupplier; | ||
| import java.util.function.ObjLongConsumer; | ||
|
|
||
| public abstract class Publication { | ||
|
|
||
|
|
@@ -46,16 +47,19 @@ public abstract class Publication { | |
| private final AckListener ackListener; | ||
| private final LongSupplier currentTimeSupplier; | ||
| private final long startTime; | ||
| private final ObjLongConsumer<DiscoveryNode> onNodeApplicationAck; | ||
|
|
||
| private Optional<ApplyCommitRequest> applyCommitRequest; // set when state is committed | ||
| private boolean isCompleted; // set when publication is completed | ||
| private boolean timedOut; // set when publication timed out | ||
|
|
||
| public Publication(PublishRequest publishRequest, AckListener ackListener, LongSupplier currentTimeSupplier) { | ||
| public Publication(PublishRequest publishRequest, AckListener ackListener, LongSupplier currentTimeSupplier, | ||
| ObjLongConsumer<DiscoveryNode> onNodeApplicationAck) { | ||
| this.publishRequest = publishRequest; | ||
| this.ackListener = ackListener; | ||
| this.currentTimeSupplier = currentTimeSupplier; | ||
| startTime = currentTimeSupplier.getAsLong(); | ||
| this.onNodeApplicationAck = onNodeApplicationAck; | ||
| applyCommitRequest = Optional.empty(); | ||
| publicationTargets = new ArrayList<>(publishRequest.getAcceptedState().getNodes().getNodes().size()); | ||
| publishRequest.getAcceptedState().getNodes().iterator().forEachRemaining(n -> publicationTargets.add(new PublicationTarget(n))); | ||
|
|
@@ -251,6 +255,7 @@ void sendApplyCommit() { | |
| void setAppliedCommit() { | ||
| assert state == PublicationTargetState.SENT_APPLY_COMMIT : state + " -> " + PublicationTargetState.APPLIED_COMMIT; | ||
| state = PublicationTargetState.APPLIED_COMMIT; | ||
| onNodeApplicationAck.accept(discoveryNode, publishRequest.getAcceptedState().version()); | ||
|
||
| ackOnce(null); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.