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.

AppNodesInfoLoaderImpl.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.server.platform.monitoring.cluster;
  21. import com.hazelcast.cluster.Member;
  22. import com.hazelcast.cluster.MemberSelector;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.Optional;
  27. import org.sonar.api.server.ServerSide;
  28. import org.sonar.process.ProcessId;
  29. import org.sonar.process.cluster.hz.DistributedAnswer;
  30. import org.sonar.process.cluster.hz.HazelcastMember;
  31. import org.sonar.process.cluster.hz.HazelcastMemberSelectors;
  32. import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
  33. import static org.sonar.process.cluster.hz.HazelcastMember.Attribute.NODE_NAME;
  34. @ServerSide
  35. public class AppNodesInfoLoaderImpl implements AppNodesInfoLoader {
  36. /**
  37. * Timeout to get information from all nodes
  38. */
  39. private static final long DISTRIBUTED_TIMEOUT_MS = 15_000L;
  40. private final HazelcastMember hzMember;
  41. public AppNodesInfoLoaderImpl(HazelcastMember hzMember) {
  42. this.hzMember = hzMember;
  43. }
  44. public AppNodesInfoLoaderImpl() {
  45. this(null);
  46. }
  47. public Collection<NodeInfo> load() throws InterruptedException {
  48. Map<String, NodeInfo> nodesByName = new HashMap<>();
  49. MemberSelector memberSelector = HazelcastMemberSelectors.selectorForProcessIds(ProcessId.WEB_SERVER, ProcessId.COMPUTE_ENGINE);
  50. DistributedAnswer<ProtobufSystemInfo.SystemInfo> distributedAnswer = hzMember.call(ProcessInfoProvider::provide, memberSelector, DISTRIBUTED_TIMEOUT_MS);
  51. for (Member member : distributedAnswer.getMembers()) {
  52. String nodeName = member.getAttribute(NODE_NAME.getKey());
  53. NodeInfo nodeInfo = nodesByName.computeIfAbsent(nodeName, name -> {
  54. NodeInfo info = new NodeInfo(name);
  55. info.setHost(member.getAddress().getHost());
  56. return info;
  57. });
  58. completeNodeInfo(distributedAnswer, member, nodeInfo);
  59. }
  60. return nodesByName.values();
  61. }
  62. private static void completeNodeInfo(DistributedAnswer<ProtobufSystemInfo.SystemInfo> distributedAnswer, Member member, NodeInfo nodeInfo) {
  63. Optional<ProtobufSystemInfo.SystemInfo> nodeAnswer = distributedAnswer.getAnswer(member);
  64. Optional<Exception> failure = distributedAnswer.getFailed(member);
  65. if (distributedAnswer.hasTimedOut(member)) {
  66. nodeInfo.setErrorMessage("Failed to retrieve information on time");
  67. } else if (failure.isPresent()) {
  68. nodeInfo.setErrorMessage("Failed to retrieve information: " + failure.get().getMessage());
  69. } else if (nodeAnswer.isPresent()) {
  70. nodeAnswer.get().getSectionsList().forEach(nodeInfo::addSection);
  71. }
  72. }
  73. }