Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-9741 return nodes in response of api/system/health
- Loading branch information
Showing
17 changed files
with
786 additions
and
55 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
server/sonar-server/src/main/java/org/sonar/server/health/ClusterHealth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,71 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2017 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.health; | |||
|
|||
import java.util.Objects; | |||
import java.util.Set; | |||
import org.sonar.cluster.health.NodeHealth; | |||
|
|||
import static com.google.common.collect.ImmutableSet.copyOf; | |||
import static java.util.Objects.requireNonNull; | |||
|
|||
public class ClusterHealth { | |||
private final Health health; | |||
private final Set<NodeHealth> nodes; | |||
|
|||
public ClusterHealth(Health health, Set<NodeHealth> nodes) { | |||
this.health = requireNonNull(health, "health can't be null"); | |||
this.nodes = copyOf(requireNonNull(nodes, "nodes can't be null")); | |||
} | |||
|
|||
public Health getHealth() { | |||
return health; | |||
} | |||
|
|||
public Set<NodeHealth> getNodes() { | |||
return nodes; | |||
} | |||
|
|||
@Override | |||
public boolean equals(Object o) { | |||
if (this == o) { | |||
return true; | |||
} | |||
if (o == null || getClass() != o.getClass()) { | |||
return false; | |||
} | |||
ClusterHealth that = (ClusterHealth) o; | |||
return Objects.equals(health, that.health) && | |||
Objects.equals(nodes, that.nodes); | |||
} | |||
|
|||
@Override | |||
public int hashCode() { | |||
return Objects.hash(health, nodes); | |||
} | |||
|
|||
@Override | |||
public String toString() { | |||
return "ClusterHealth{" + | |||
"health=" + health + | |||
", nodes=" + nodes + | |||
'}'; | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
server/sonar-server/src/main/java/org/sonar/server/platform/ws/HealthActionSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,113 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2017 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.platform.ws; | |||
|
|||
import com.google.common.io.Resources; | |||
import java.util.Comparator; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.cluster.health.NodeDetails; | |||
import org.sonar.cluster.health.NodeHealth; | |||
import org.sonar.server.health.ClusterHealth; | |||
import org.sonar.server.health.Health; | |||
import org.sonar.server.health.HealthChecker; | |||
import org.sonarqube.ws.WsSystem; | |||
|
|||
import static java.lang.String.valueOf; | |||
import static org.sonar.api.utils.DateUtils.formatDateTime; | |||
|
|||
public class HealthActionSupport { | |||
private static final Comparator<NodeHealth> NODE_HEALTH_COMPARATOR = Comparator.<NodeHealth>comparingInt(s -> s.getDetails().getType().ordinal()) | |||
.thenComparing(a -> a.getDetails().getName()) | |||
.thenComparing(a -> a.getDetails().getHost()) | |||
.thenComparing(a -> a.getDetails().getPort()); | |||
private final HealthChecker healthChecker; | |||
|
|||
public HealthActionSupport(HealthChecker healthChecker) { | |||
this.healthChecker = healthChecker; | |||
} | |||
|
|||
void define(WebService.NewController controller, SystemWsAction handler) { | |||
controller.createAction("health") | |||
.setDescription("Provide health status of SonarQube." + | |||
"<p>Require 'Administer System' permission or authentication with passcode</p>" + | |||
"<p> " + | |||
" <ul>" + | |||
" <li>GREEN: SonarQube is fully operational</li>" + | |||
" <li>YELLOW: SonarQube is usable, but it needs attention in order to be fully operational</li>" + | |||
" <li>RED: SonarQube is not operational</li>" + | |||
" </ul>" + | |||
"</p>") | |||
.setSince("6.6") | |||
.setResponseExample(Resources.getResource(this.getClass(), "example-health.json")) | |||
.setHandler(handler); | |||
} | |||
|
|||
WsSystem.HealthResponse checkNodeHealth() { | |||
Health check = healthChecker.checkNode(); | |||
WsSystem.HealthResponse.Builder responseBuilder = WsSystem.HealthResponse.newBuilder() | |||
.setHealth(WsSystem.Health.valueOf(check.getStatus().name())); | |||
WsSystem.Cause.Builder causeBuilder = WsSystem.Cause.newBuilder(); | |||
check.getCauses().forEach(str -> responseBuilder.addCauses(causeBuilder.clear().setMessage(str).build())); | |||
|
|||
return responseBuilder.build(); | |||
} | |||
|
|||
WsSystem.HealthResponse checkClusterHealth() { | |||
ClusterHealth check = healthChecker.checkCluster(); | |||
return toResponse(check); | |||
} | |||
|
|||
private static WsSystem.HealthResponse toResponse(ClusterHealth check) { | |||
WsSystem.HealthResponse.Builder responseBuilder = WsSystem.HealthResponse.newBuilder(); | |||
WsSystem.Node.Builder nodeBuilder = WsSystem.Node.newBuilder(); | |||
WsSystem.Cause.Builder causeBuilder = WsSystem.Cause.newBuilder(); | |||
|
|||
Health health = check.getHealth(); | |||
responseBuilder.setHealth(WsSystem.Health.valueOf(health.getStatus().name())); | |||
health.getCauses().forEach(str -> responseBuilder.addCauses(toCause(str, causeBuilder))); | |||
|
|||
WsSystem.Nodes.Builder nodesBuilder = WsSystem.Nodes.newBuilder(); | |||
check.getNodes().stream() | |||
.sorted(NODE_HEALTH_COMPARATOR) | |||
.map(node -> toNode(node, nodeBuilder, causeBuilder)) | |||
.forEach(nodesBuilder::addNodes); | |||
responseBuilder.setNodes(nodesBuilder.build()); | |||
|
|||
return responseBuilder.build(); | |||
} | |||
|
|||
private static WsSystem.Node toNode(NodeHealth nodeHealth, WsSystem.Node.Builder nodeBuilder, WsSystem.Cause.Builder causeBuilder) { | |||
nodeBuilder.clear(); | |||
nodeBuilder.setHealth(WsSystem.Health.valueOf(nodeHealth.getStatus().name())); | |||
nodeHealth.getCauses().forEach(str -> nodeBuilder.addCauses(toCause(str, causeBuilder))); | |||
NodeDetails details = nodeHealth.getDetails(); | |||
nodeBuilder | |||
.setType(WsSystem.NodeType.valueOf(details.getType().name())) | |||
.setName(details.getName()) | |||
.setHost(details.getHost()) | |||
.setPort(valueOf(details.getPort())) | |||
.setStarted(formatDateTime(details.getStarted())); | |||
return nodeBuilder.build(); | |||
} | |||
|
|||
private static WsSystem.Cause toCause(String str, WsSystem.Cause.Builder causeBuilder) { | |||
return causeBuilder.clear().setMessage(str).build(); | |||
} | |||
} |
43 changes: 43 additions & 0 deletions
43
server/sonar-server/src/main/java/org/sonar/server/platform/ws/SafeModeHealthAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,43 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2017 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.platform.ws; | |||
|
|||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.server.ws.WsUtils; | |||
|
|||
public class SafeModeHealthAction implements SystemWsAction { | |||
private final HealthActionSupport support; | |||
|
|||
public SafeModeHealthAction(HealthActionSupport support) { | |||
this.support = support; | |||
} | |||
|
|||
@Override | |||
public void define(WebService.NewController controller) { | |||
support.define(controller, this); | |||
} | |||
|
|||
@Override | |||
public void handle(Request request, Response response) throws Exception { | |||
WsUtils.writeProtobuf(support.checkNodeHealth(), request, response); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.