You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AppNodesClusterHostsConsistency.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.application.cluster;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.hazelcast.cluster.Address;
  23. import com.hazelcast.cluster.Member;
  24. import com.hazelcast.cluster.MemberSelector;
  25. import com.hazelcast.cluster.memberselector.MemberSelectors;
  26. import java.util.Arrays;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.concurrent.RejectedExecutionException;
  31. import java.util.concurrent.atomic.AtomicReference;
  32. import java.util.function.Consumer;
  33. import javax.annotation.CheckForNull;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import org.sonar.application.config.AppSettings;
  37. import org.sonar.process.ProcessId;
  38. import org.sonar.process.cluster.hz.DistributedCallback;
  39. import org.sonar.process.cluster.hz.HazelcastMember;
  40. import org.sonar.process.cluster.hz.HazelcastMemberSelectors;
  41. import static com.google.common.base.Preconditions.checkState;
  42. import static com.hazelcast.cluster.memberselector.MemberSelectors.NON_LOCAL_MEMBER_SELECTOR;
  43. import static org.sonar.process.ProcessProperties.Property.CLUSTER_HZ_HOSTS;
  44. public class AppNodesClusterHostsConsistency {
  45. private static final Logger LOG = LoggerFactory.getLogger(AppNodesClusterHostsConsistency.class);
  46. private static final AtomicReference<AppNodesClusterHostsConsistency> INSTANCE = new AtomicReference<>();
  47. private final AppSettings settings;
  48. private final HazelcastMember hzMember;
  49. private final Consumer<String> logger;
  50. private AppNodesClusterHostsConsistency(HazelcastMember hzMember, AppSettings settings, Consumer<String> logger) {
  51. this.hzMember = hzMember;
  52. this.settings = settings;
  53. this.logger = logger;
  54. }
  55. public static AppNodesClusterHostsConsistency setInstance(HazelcastMember hzMember, AppSettings settings) {
  56. return setInstance(hzMember, settings, LOG::warn);
  57. }
  58. @VisibleForTesting
  59. public static AppNodesClusterHostsConsistency setInstance(HazelcastMember hzMember, AppSettings settings, Consumer<String> logger) {
  60. AppNodesClusterHostsConsistency instance = new AppNodesClusterHostsConsistency(hzMember, settings, logger);
  61. checkState(INSTANCE.compareAndSet(null, instance), "Instance is already set");
  62. return instance;
  63. }
  64. @VisibleForTesting
  65. @CheckForNull
  66. protected static AppNodesClusterHostsConsistency clearInstance() {
  67. return INSTANCE.getAndSet(null);
  68. }
  69. public void check() {
  70. try {
  71. MemberSelector selector = MemberSelectors.and(NON_LOCAL_MEMBER_SELECTOR, HazelcastMemberSelectors.selectorForProcessIds(ProcessId.APP));
  72. hzMember.callAsync(AppNodesClusterHostsConsistency::getConfiguredClusterHosts, selector, new Callback());
  73. } catch (RejectedExecutionException e) {
  74. // no other node in the cluster yet, ignore
  75. }
  76. }
  77. private class Callback implements DistributedCallback<List<String>> {
  78. @Override
  79. public void onComplete(Map<Member, List<String>> hostsPerMember) {
  80. List<String> currentConfiguredHosts = getConfiguredClusterHosts();
  81. boolean anyDifference = hostsPerMember.values().stream()
  82. .filter(v -> !v.isEmpty())
  83. .anyMatch(hosts -> currentConfiguredHosts.size() != hosts.size() || !currentConfiguredHosts.containsAll(hosts));
  84. if (anyDifference) {
  85. StringBuilder builder = new StringBuilder().append("The configuration of the current node doesn't match the list of hosts configured in "
  86. + "the application nodes that have already joined the cluster:\n");
  87. logMemberSetting(builder, hzMember.getCluster().getLocalMember(), currentConfiguredHosts);
  88. for (Map.Entry<Member, List<String>> e : hostsPerMember.entrySet()) {
  89. if (e.getValue().isEmpty()) {
  90. continue;
  91. }
  92. logMemberSetting(builder, e.getKey(), e.getValue());
  93. }
  94. builder.append("Make sure the configuration is consistent among all application nodes before you restart any node");
  95. logger.accept(builder.toString());
  96. }
  97. }
  98. private String toString(Address address) {
  99. return address.getHost() + ":" + address.getPort();
  100. }
  101. private void logMemberSetting(StringBuilder builder, Member member, List<String> configuredHosts) {
  102. builder.append(toString(member.getAddress()));
  103. builder.append(" : ");
  104. builder.append(configuredHosts);
  105. if (member.localMember()) {
  106. builder.append(" (current)");
  107. }
  108. builder.append("\n");
  109. }
  110. }
  111. private static List<String> getConfiguredClusterHosts() {
  112. try {
  113. AppNodesClusterHostsConsistency instance = INSTANCE.get();
  114. if (instance != null) {
  115. return Arrays.asList(instance.settings.getProps().nonNullValue(CLUSTER_HZ_HOSTS.getKey()).split(","));
  116. }
  117. return Collections.emptyList();
  118. } catch (Exception e) {
  119. LOG.error("Failed to get configured cluster nodes", e);
  120. return Collections.emptyList();
  121. }
  122. }
  123. }