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.

SharedHealthStateImpl.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.process.cluster.health;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.UUID;
  26. import java.util.function.Predicate;
  27. import java.util.stream.Collectors;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.sonar.process.cluster.hz.HazelcastMember;
  31. import org.sonar.process.cluster.hz.HazelcastObjects;
  32. import static java.util.Objects.requireNonNull;
  33. public class SharedHealthStateImpl implements SharedHealthState {
  34. private static final Logger LOG = LoggerFactory.getLogger(SharedHealthStateImpl.class);
  35. private static final int TIMEOUT_30_SECONDS = 30 * 1000;
  36. private final HazelcastMember hzMember;
  37. public SharedHealthStateImpl(HazelcastMember hzMember) {
  38. this.hzMember = hzMember;
  39. }
  40. public SharedHealthStateImpl() {
  41. this(null);
  42. }
  43. @Override
  44. public void writeMine(NodeHealth nodeHealth) {
  45. requireNonNull(nodeHealth, "nodeHealth can't be null");
  46. Map<UUID, TimestampedNodeHealth> sqHealthState = readReplicatedMap();
  47. if (LOG.isTraceEnabled()) {
  48. LOG.trace("Reading {} and adding {}", new HashMap<>(sqHealthState), nodeHealth);
  49. }
  50. sqHealthState.put(hzMember.getUuid(), new TimestampedNodeHealth(nodeHealth, hzMember.getClusterTime()));
  51. }
  52. @Override
  53. public void clearMine() {
  54. Map<UUID, TimestampedNodeHealth> sqHealthState = readReplicatedMap();
  55. UUID clientUUID = hzMember.getUuid();
  56. if (LOG.isTraceEnabled()) {
  57. LOG.trace("Reading {} and clearing for {}", new HashMap<>(sqHealthState), clientUUID);
  58. }
  59. sqHealthState.remove(clientUUID);
  60. }
  61. @Override
  62. public Set<NodeHealth> readAll() {
  63. long clusterTime = hzMember.getClusterTime();
  64. long timeout = clusterTime - TIMEOUT_30_SECONDS;
  65. Map<UUID, TimestampedNodeHealth> sqHealthState = readReplicatedMap();
  66. Set<UUID> hzMemberUUIDs = hzMember.getMemberUuids();
  67. Set<NodeHealth> existingNodeHealths = sqHealthState.entrySet().stream()
  68. .filter(outOfDate(timeout))
  69. .filter(ofNonExistentMember(hzMemberUUIDs))
  70. .map(entry -> entry.getValue().getNodeHealth())
  71. .collect(Collectors.toSet());
  72. if (LOG.isTraceEnabled()) {
  73. LOG.trace("Reading {} and keeping {}", new HashMap<>(sqHealthState), existingNodeHealths);
  74. }
  75. return ImmutableSet.copyOf(existingNodeHealths);
  76. }
  77. private static Predicate<Map.Entry<UUID, TimestampedNodeHealth>> outOfDate(long timeout) {
  78. return entry -> {
  79. boolean res = entry.getValue().getTimestamp() > timeout;
  80. if (!res) {
  81. LOG.trace("Ignoring NodeHealth of member {} because it is too old", entry.getKey());
  82. }
  83. return res;
  84. };
  85. }
  86. private static Predicate<Map.Entry<UUID, TimestampedNodeHealth>> ofNonExistentMember(Set<UUID> hzMemberUUIDs) {
  87. return entry -> {
  88. boolean res = hzMemberUUIDs.contains(entry.getKey());
  89. if (!res) {
  90. LOG.trace("Ignoring NodeHealth of member {} because it is not part of the cluster at the moment", entry.getKey());
  91. }
  92. return res;
  93. };
  94. }
  95. private Map<UUID, TimestampedNodeHealth> readReplicatedMap() {
  96. return hzMember.getReplicatedMap(HazelcastObjects.SQ_HEALTH_STATE);
  97. }
  98. }