|
| 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.Version; |
| 24 | +import org.elasticsearch.cluster.ClusterState; |
| 25 | +import org.elasticsearch.cluster.coordination.CoordinationMetaData.VotingConfiguration; |
| 26 | +import org.elasticsearch.cluster.coordination.CoordinationState.VoteCollection; |
| 27 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 28 | +import org.elasticsearch.common.Nullable; |
| 29 | +import org.elasticsearch.common.settings.Setting; |
| 30 | +import org.elasticsearch.common.settings.Settings; |
| 31 | +import org.elasticsearch.common.transport.TransportAddress; |
| 32 | +import org.elasticsearch.common.unit.TimeValue; |
| 33 | +import org.elasticsearch.common.util.concurrent.AbstractRunnable; |
| 34 | +import org.elasticsearch.threadpool.ThreadPool; |
| 35 | +import org.elasticsearch.threadpool.ThreadPool.Names; |
| 36 | + |
| 37 | +import java.util.List; |
| 38 | +import java.util.Locale; |
| 39 | +import java.util.Set; |
| 40 | +import java.util.function.Supplier; |
| 41 | +import java.util.stream.Collectors; |
| 42 | +import java.util.stream.StreamSupport; |
| 43 | + |
| 44 | +import static org.elasticsearch.cluster.coordination.ClusterBootstrapService.INITIAL_MASTER_NODES_SETTING; |
| 45 | +import static org.elasticsearch.cluster.coordination.ClusterBootstrapService.INITIAL_MASTER_NODE_COUNT_SETTING; |
| 46 | + |
| 47 | +public class ClusterFormationFailureHelper { |
| 48 | + private static final Logger logger = LogManager.getLogger(ClusterFormationFailureHelper.class); |
| 49 | + |
| 50 | + public static final Setting<TimeValue> DISCOVERY_CLUSTER_FORMATION_WARNING_TIMEOUT_SETTING = |
| 51 | + Setting.timeSetting("discovery.cluster_formation_warning_timeout", |
| 52 | + TimeValue.timeValueMillis(10000), TimeValue.timeValueMillis(1), Setting.Property.NodeScope); |
| 53 | + |
| 54 | + private final Supplier<ClusterFormationState> clusterFormationStateSupplier; |
| 55 | + private final ThreadPool threadPool; |
| 56 | + private final TimeValue clusterFormationWarningTimeout; |
| 57 | + @Nullable // if no warning is scheduled |
| 58 | + private volatile WarningScheduler warningScheduler; |
| 59 | + |
| 60 | + public ClusterFormationFailureHelper(Settings settings, Supplier<ClusterFormationState> clusterFormationStateSupplier, |
| 61 | + ThreadPool threadPool) { |
| 62 | + this.clusterFormationStateSupplier = clusterFormationStateSupplier; |
| 63 | + this.threadPool = threadPool; |
| 64 | + this.clusterFormationWarningTimeout = DISCOVERY_CLUSTER_FORMATION_WARNING_TIMEOUT_SETTING.get(settings); |
| 65 | + } |
| 66 | + |
| 67 | + public boolean isRunning() { |
| 68 | + return warningScheduler != null; |
| 69 | + } |
| 70 | + |
| 71 | + public void start() { |
| 72 | + assert warningScheduler == null; |
| 73 | + warningScheduler = new WarningScheduler(); |
| 74 | + warningScheduler.scheduleNextWarning(); |
| 75 | + } |
| 76 | + |
| 77 | + public void stop() { |
| 78 | + warningScheduler = null; |
| 79 | + } |
| 80 | + |
| 81 | + private class WarningScheduler { |
| 82 | + |
| 83 | + private boolean isActive() { |
| 84 | + return warningScheduler == this; |
| 85 | + } |
| 86 | + |
| 87 | + void scheduleNextWarning() { |
| 88 | + threadPool.scheduleUnlessShuttingDown(clusterFormationWarningTimeout, Names.GENERIC, new AbstractRunnable() { |
| 89 | + @Override |
| 90 | + public void onFailure(Exception e) { |
| 91 | + logger.debug("unexpected exception scheduling cluster formation warning", e); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected void doRun() { |
| 96 | + if (isActive()) { |
| 97 | + logger.warn(clusterFormationStateSupplier.get().getDescription()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void onAfter() { |
| 103 | + if (isActive()) { |
| 104 | + scheduleNextWarning(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String toString() { |
| 110 | + return "emit warning if cluster not formed"; |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + static class ClusterFormationState { |
| 117 | + private final Settings settings; |
| 118 | + private final ClusterState clusterState; |
| 119 | + private final List<TransportAddress> resolvedAddresses; |
| 120 | + private final List<DiscoveryNode> foundPeers; |
| 121 | + |
| 122 | + ClusterFormationState(Settings settings, ClusterState clusterState, List<TransportAddress> resolvedAddresses, |
| 123 | + List<DiscoveryNode> foundPeers) { |
| 124 | + this.settings = settings; |
| 125 | + this.clusterState = clusterState; |
| 126 | + this.resolvedAddresses = resolvedAddresses; |
| 127 | + this.foundPeers = foundPeers; |
| 128 | + } |
| 129 | + |
| 130 | + String getDescription() { |
| 131 | + final List<String> clusterStateNodes |
| 132 | + = StreamSupport.stream(clusterState.nodes().spliterator(), false).map(DiscoveryNode::toString).collect(Collectors.toList()); |
| 133 | + |
| 134 | + final String discoveryWillContinueDescription = String.format(Locale.ROOT, |
| 135 | + "discovery will continue using %s from hosts providers and %s from last-known cluster state", |
| 136 | + resolvedAddresses, clusterStateNodes); |
| 137 | + |
| 138 | + final String discoveryStateIgnoringQuorum = String.format(Locale.ROOT, "have discovered %s; %s", |
| 139 | + foundPeers, discoveryWillContinueDescription); |
| 140 | + |
| 141 | + if (clusterState.nodes().getLocalNode().isMasterNode() == false) { |
| 142 | + return String.format(Locale.ROOT, "master not discovered yet: %s", discoveryStateIgnoringQuorum); |
| 143 | + } |
| 144 | + |
| 145 | + if (clusterState.getLastAcceptedConfiguration().isEmpty()) { |
| 146 | + |
| 147 | + // TODO handle the case that there is a 6.x node around here, when rolling upgrades are supported |
| 148 | + |
| 149 | + final String bootstrappingDescription; |
| 150 | + |
| 151 | + if (INITIAL_MASTER_NODE_COUNT_SETTING.get(Settings.EMPTY).equals(INITIAL_MASTER_NODE_COUNT_SETTING.get(settings)) |
| 152 | + && INITIAL_MASTER_NODES_SETTING.get(Settings.EMPTY).equals(INITIAL_MASTER_NODES_SETTING.get(settings))) { |
| 153 | + bootstrappingDescription = "cluster bootstrapping is disabled on this node"; |
| 154 | + } else if (INITIAL_MASTER_NODES_SETTING.get(Settings.EMPTY).equals(INITIAL_MASTER_NODES_SETTING.get(settings))) { |
| 155 | + bootstrappingDescription = String.format(Locale.ROOT, |
| 156 | + "this node must discover at least [%d] master-eligible nodes to bootstrap a cluster", |
| 157 | + INITIAL_MASTER_NODE_COUNT_SETTING.get(settings)); |
| 158 | + } else if (INITIAL_MASTER_NODE_COUNT_SETTING.get(settings) <= INITIAL_MASTER_NODES_SETTING.get(settings).size()) { |
| 159 | + // TODO update this when we can bootstrap on only a quorum of the initial nodes |
| 160 | + bootstrappingDescription = String.format(Locale.ROOT, |
| 161 | + "this node must discover master-eligible nodes %s to bootstrap a cluster", |
| 162 | + INITIAL_MASTER_NODES_SETTING.get(settings)); |
| 163 | + } else { |
| 164 | + // TODO update this when we can bootstrap on only a quorum of the initial nodes |
| 165 | + bootstrappingDescription = String.format(Locale.ROOT, |
| 166 | + "this node must discover at least [%d] master-eligible nodes, including %s, to bootstrap a cluster", |
| 167 | + INITIAL_MASTER_NODE_COUNT_SETTING.get(settings), INITIAL_MASTER_NODES_SETTING.get(settings)); |
| 168 | + } |
| 169 | + |
| 170 | + return String.format(Locale.ROOT, |
| 171 | + "master not discovered yet, this node has not previously joined a bootstrapped (v%d+) cluster, and %s: %s", |
| 172 | + Version.V_6_6_0.major + 1, bootstrappingDescription, discoveryStateIgnoringQuorum); |
| 173 | + } |
| 174 | + |
| 175 | + assert clusterState.getLastCommittedConfiguration().isEmpty() == false; |
| 176 | + |
| 177 | + final String quorumDescription; |
| 178 | + if (clusterState.getLastAcceptedConfiguration().equals(clusterState.getLastCommittedConfiguration())) { |
| 179 | + quorumDescription = describeQuorum(clusterState.getLastAcceptedConfiguration()); |
| 180 | + } else { |
| 181 | + quorumDescription = describeQuorum(clusterState.getLastAcceptedConfiguration()) |
| 182 | + + " and " |
| 183 | + + describeQuorum(clusterState.getLastCommittedConfiguration()); |
| 184 | + } |
| 185 | + |
| 186 | + final VoteCollection voteCollection = new VoteCollection(); |
| 187 | + foundPeers.forEach(voteCollection::addVote); |
| 188 | + final String isQuorumOrNot |
| 189 | + = CoordinationState.isElectionQuorum(voteCollection, clusterState) ? "is a quorum" : "is not a quorum"; |
| 190 | + |
| 191 | + return String.format(Locale.ROOT, |
| 192 | + "master not discovered or elected yet, an election requires %s, have discovered %s which %s; %s", |
| 193 | + quorumDescription, foundPeers, isQuorumOrNot, discoveryWillContinueDescription); |
| 194 | + } |
| 195 | + |
| 196 | + private String describeQuorum(VotingConfiguration votingConfiguration) { |
| 197 | + final Set<String> nodeIds = votingConfiguration.getNodeIds(); |
| 198 | + assert nodeIds.isEmpty() == false; |
| 199 | + |
| 200 | + if (nodeIds.size() == 1) { |
| 201 | + return "a node with id " + nodeIds; |
| 202 | + } else if (nodeIds.size() == 2) { |
| 203 | + return "two nodes with ids " + nodeIds; |
| 204 | + } else { |
| 205 | + return "at least " + (nodeIds.size() / 2 + 1) + " nodes with ids from " + nodeIds; |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | +} |
0 commit comments