3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.platform.monitoring.cluster;
22 import com.hazelcast.core.Member;
23 import com.hazelcast.core.MemberSelector;
24 import java.util.Collection;
25 import java.util.HashMap;
27 import java.util.Optional;
28 import org.sonar.api.server.ServerSide;
29 import org.sonar.process.ProcessId;
30 import org.sonar.process.cluster.hz.DistributedAnswer;
31 import org.sonar.process.cluster.hz.HazelcastMember;
32 import org.sonar.process.cluster.hz.HazelcastMemberSelectors;
33 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
35 import static org.sonar.process.cluster.hz.HazelcastMember.Attribute.NODE_NAME;
38 public class AppNodesInfoLoaderImpl implements AppNodesInfoLoader {
41 * Timeout to get information from all nodes
43 private static final long DISTRIBUTED_TIMEOUT_MS = 15_000L;
45 private final HazelcastMember hzMember;
47 public AppNodesInfoLoaderImpl(HazelcastMember hzMember) {
48 this.hzMember = hzMember;
51 public Collection<NodeInfo> load() throws InterruptedException {
52 Map<String, NodeInfo> nodesByName = new HashMap<>();
53 MemberSelector memberSelector = HazelcastMemberSelectors.selectorForProcessIds(ProcessId.WEB_SERVER, ProcessId.COMPUTE_ENGINE);
54 DistributedAnswer<ProtobufSystemInfo.SystemInfo> distributedAnswer = hzMember.call(ProcessInfoProvider::provide, memberSelector, DISTRIBUTED_TIMEOUT_MS);
55 for (Member member : distributedAnswer.getMembers()) {
56 String nodeName = member.getStringAttribute(NODE_NAME.getKey());
57 NodeInfo nodeInfo = nodesByName.computeIfAbsent(nodeName, NodeInfo::new);
58 completeNodeInfo(distributedAnswer, member, nodeInfo);
60 return nodesByName.values();
63 private static void completeNodeInfo(DistributedAnswer<ProtobufSystemInfo.SystemInfo> distributedAnswer, Member member, NodeInfo nodeInfo) {
64 Optional<ProtobufSystemInfo.SystemInfo> nodeAnswer = distributedAnswer.getAnswer(member);
65 Optional<Exception> failure = distributedAnswer.getFailed(member);
66 if (distributedAnswer.hasTimedOut(member)) {
67 nodeInfo.setErrorMessage("Failed to retrieve information on time");
68 } else if (failure.isPresent()) {
69 nodeInfo.setErrorMessage("Failed to retrieve information: " + failure.get().getMessage());
70 } else if (nodeAnswer.isPresent()) {
71 nodeAnswer.get().getSectionsList().forEach(nodeInfo::addSection);