|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.cluster.coordination; |
| 20 | + |
| 21 | +import org.apache.logging.log4j.LogManager; |
| 22 | +import org.apache.logging.log4j.Logger; |
| 23 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 24 | +import org.elasticsearch.common.settings.Setting; |
| 25 | +import org.elasticsearch.common.settings.Settings; |
| 26 | +import org.elasticsearch.common.unit.TimeValue; |
| 27 | +import org.elasticsearch.threadpool.ThreadPool; |
| 28 | +import org.elasticsearch.threadpool.ThreadPool.Names; |
| 29 | + |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.concurrent.atomic.AtomicLong; |
| 36 | +import java.util.function.Consumer; |
| 37 | +import java.util.function.Supplier; |
| 38 | +import java.util.stream.Collectors; |
| 39 | + |
| 40 | +import static org.elasticsearch.common.util.concurrent.ConcurrentCollections.newConcurrentMap; |
| 41 | + |
| 42 | +/** |
| 43 | + * A publication can succeed and complete before all nodes have applied the published state and acknowledged it; however we need every node |
| 44 | + * eventually either to apply the published state (or a later state) or be removed from the cluster. This component achieves this by |
| 45 | + * removing any lagging nodes from the cluster after a timeout. |
| 46 | + */ |
| 47 | +public class LagDetector { |
| 48 | + |
| 49 | + private static final Logger logger = LogManager.getLogger(LagDetector.class); |
| 50 | + |
| 51 | + // the timeout for each node to apply a cluster state update after the leader has applied it, before being removed from the cluster |
| 52 | + public static final Setting<TimeValue> CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING = |
| 53 | + Setting.timeSetting("cluster.follower_lag.timeout", |
| 54 | + TimeValue.timeValueMillis(90000), TimeValue.timeValueMillis(1), Setting.Property.NodeScope); |
| 55 | + |
| 56 | + private final TimeValue clusterStateApplicationTimeout; |
| 57 | + private final Consumer<DiscoveryNode> onLagDetected; |
| 58 | + private final Supplier<DiscoveryNode> localNodeSupplier; |
| 59 | + private final ThreadPool threadPool; |
| 60 | + private final Map<DiscoveryNode, NodeAppliedStateTracker> appliedStateTrackersByNode = newConcurrentMap(); |
| 61 | + |
| 62 | + public LagDetector(final Settings settings, final ThreadPool threadPool, final Consumer<DiscoveryNode> onLagDetected, |
| 63 | + final Supplier<DiscoveryNode> localNodeSupplier) { |
| 64 | + this.threadPool = threadPool; |
| 65 | + this.clusterStateApplicationTimeout = CLUSTER_FOLLOWER_LAG_TIMEOUT_SETTING.get(settings); |
| 66 | + this.onLagDetected = onLagDetected; |
| 67 | + this.localNodeSupplier = localNodeSupplier; |
| 68 | + } |
| 69 | + |
| 70 | + public void setTrackedNodes(final Iterable<DiscoveryNode> discoveryNodes) { |
| 71 | + final Set<DiscoveryNode> discoveryNodeSet = new HashSet<>(); |
| 72 | + discoveryNodes.forEach(discoveryNodeSet::add); |
| 73 | + discoveryNodeSet.remove(localNodeSupplier.get()); |
| 74 | + appliedStateTrackersByNode.keySet().retainAll(discoveryNodeSet); |
| 75 | + discoveryNodeSet.forEach(node -> appliedStateTrackersByNode.putIfAbsent(node, new NodeAppliedStateTracker(node))); |
| 76 | + } |
| 77 | + |
| 78 | + public void clearTrackedNodes() { |
| 79 | + appliedStateTrackersByNode.clear(); |
| 80 | + } |
| 81 | + |
| 82 | + public void setAppliedVersion(final DiscoveryNode discoveryNode, final long appliedVersion) { |
| 83 | + final NodeAppliedStateTracker nodeAppliedStateTracker = appliedStateTrackersByNode.get(discoveryNode); |
| 84 | + if (nodeAppliedStateTracker == null) { |
| 85 | + // Received an ack from a node that a later publication has removed (or we are no longer master). No big deal. |
| 86 | + logger.trace("node {} applied version {} but this node's version is not being tracked", discoveryNode, appliedVersion); |
| 87 | + } else { |
| 88 | + nodeAppliedStateTracker.increaseAppliedVersion(appliedVersion); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void startLagDetector(final long version) { |
| 93 | + final List<NodeAppliedStateTracker> laggingTrackers |
| 94 | + = appliedStateTrackersByNode.values().stream().filter(t -> t.appliedVersionLessThan(version)).collect(Collectors.toList()); |
| 95 | + |
| 96 | + if (laggingTrackers.isEmpty()) { |
| 97 | + logger.trace("lag detection for version {} is unnecessary: {}", version, appliedStateTrackersByNode.values()); |
| 98 | + } else { |
| 99 | + logger.debug("starting lag detector for version {}: {}", version, laggingTrackers); |
| 100 | + |
| 101 | + threadPool.scheduleUnlessShuttingDown(clusterStateApplicationTimeout, Names.GENERIC, new Runnable() { |
| 102 | + @Override |
| 103 | + public void run() { |
| 104 | + laggingTrackers.forEach(t -> t.checkForLag(version)); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String toString() { |
| 109 | + return "lag detector for version " + version + " on " + laggingTrackers; |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String toString() { |
| 117 | + return "LagDetector{" + |
| 118 | + "clusterStateApplicationTimeout=" + clusterStateApplicationTimeout + |
| 119 | + ", appliedStateTrackersByNode=" + appliedStateTrackersByNode.values() + |
| 120 | + '}'; |
| 121 | + } |
| 122 | + |
| 123 | + // for assertions |
| 124 | + Set<DiscoveryNode> getTrackedNodes() { |
| 125 | + return Collections.unmodifiableSet(appliedStateTrackersByNode.keySet()); |
| 126 | + } |
| 127 | + |
| 128 | + private class NodeAppliedStateTracker { |
| 129 | + private final DiscoveryNode discoveryNode; |
| 130 | + private final AtomicLong appliedVersion = new AtomicLong(); |
| 131 | + |
| 132 | + NodeAppliedStateTracker(final DiscoveryNode discoveryNode) { |
| 133 | + this.discoveryNode = discoveryNode; |
| 134 | + } |
| 135 | + |
| 136 | + void increaseAppliedVersion(long appliedVersion) { |
| 137 | + long maxAppliedVersion = this.appliedVersion.updateAndGet(v -> Math.max(v, appliedVersion)); |
| 138 | + logger.trace("{} applied version {}, max now {}", this, appliedVersion, maxAppliedVersion); |
| 139 | + } |
| 140 | + |
| 141 | + boolean appliedVersionLessThan(final long version) { |
| 142 | + return appliedVersion.get() < version; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String toString() { |
| 147 | + return "NodeAppliedStateTracker{" + |
| 148 | + "discoveryNode=" + discoveryNode + |
| 149 | + ", appliedVersion=" + appliedVersion + |
| 150 | + '}'; |
| 151 | + } |
| 152 | + |
| 153 | + void checkForLag(final long version) { |
| 154 | + if (appliedStateTrackersByNode.get(discoveryNode) != this) { |
| 155 | + logger.trace("{} no longer active when checking version {}", this, version); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + long appliedVersion = this.appliedVersion.get(); |
| 160 | + if (version <= appliedVersion) { |
| 161 | + logger.trace("{} satisfied when checking version {}, node applied version {}", this, version, appliedVersion); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + logger.debug("{}, detected lag at version {}, node has only applied version {}", this, version, appliedVersion); |
| 166 | + onLagDetected.accept(discoveryNode); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments