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.systeminfo.protobuf.ProtobufSystemInfo;
34 import static org.sonar.process.cluster.hz.HazelcastMember.Attribute.NODE_NAME;
35 import static org.sonar.process.cluster.hz.HazelcastMember.Attribute.PROCESS_KEY;
38 public class AppNodesInfoLoaderImpl implements AppNodesInfoLoader {
40 private final HazelcastMember hzMember;
42 public AppNodesInfoLoaderImpl(HazelcastMember hzMember) {
43 this.hzMember = hzMember;
46 public Collection<NodeInfo> load() {
48 Map<String, NodeInfo> nodesByName = new HashMap<>();
49 DistributedAnswer<ProtobufSystemInfo.SystemInfo> distributedAnswer = hzMember.call(ProcessInfoProvider::provide, new CeWebMemberSelector(), 15_000L);
50 for (Member member : distributedAnswer.getMembers()) {
51 String nodeName = member.getStringAttribute(NODE_NAME.getKey());
52 NodeInfo nodeInfo = nodesByName.get(nodeName);
53 if (nodeInfo == null) {
54 nodeInfo = new NodeInfo(nodeName);
55 nodesByName.put(nodeName, nodeInfo);
57 completeNodeInfo(distributedAnswer, member, nodeInfo);
59 return nodesByName.values();
61 } catch (InterruptedException e) {
62 Thread.currentThread().interrupt();
63 throw new IllegalStateException(e);
67 private static void completeNodeInfo(DistributedAnswer<ProtobufSystemInfo.SystemInfo> distributedAnswer, Member member, NodeInfo nodeInfo) {
68 Optional<ProtobufSystemInfo.SystemInfo> nodeAnswer = distributedAnswer.getAnswer(member);
69 Optional<Exception> failure = distributedAnswer.getFailed(member);
70 if (distributedAnswer.hasTimedOut(member)) {
71 nodeInfo.setErrorMessage("Failed to retrieve information on time");
72 } else if (failure.isPresent()) {
73 nodeInfo.setErrorMessage("Failed to retrieve information: " + failure.get().getMessage());
74 } else if (nodeAnswer.isPresent()) {
75 nodeAnswer.get().getSectionsList().forEach(nodeInfo::addSection);
79 private static class CeWebMemberSelector implements MemberSelector {
81 public boolean select(Member member) {
82 String processKey = member.getStringAttribute(PROCESS_KEY.getKey());
83 return processKey.equals(ProcessId.WEB_SERVER.getKey()) || processKey.equals(ProcessId.COMPUTE_ENGINE.getKey());