You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by dm...@apache.org on 2016/04/26 09:09:37 UTC
ambari git commit: AMBARI-16005. Enhance Health Check for the Cluster
before upgrading. (dlysnichenko)
Repository: ambari
Updated Branches:
refs/heads/trunk f62a92770 -> 784c5414d
AMBARI-16005. Enhance Health Check for the Cluster before upgrading. (dlysnichenko)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/784c5414
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/784c5414
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/784c5414
Branch: refs/heads/trunk
Commit: 784c5414d4ee0e1cb475a91ea2e854b3695986ec
Parents: f62a927
Author: Lisnichenko Dmitro <dl...@hortonworks.com>
Authored: Tue Apr 26 10:09:35 2016 +0300
Committer: Lisnichenko Dmitro <dl...@hortonworks.com>
Committed: Tue Apr 26 10:09:35 2016 +0300
----------------------------------------------------------------------
.../ambari/server/checks/CheckDescription.java | 7 ++
.../ambari/server/checks/HealthCheck.java | 113 ++++++++++++++++++
.../ambari/server/checks/HealthCheckTest.java | 116 +++++++++++++++++++
3 files changed, 236 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/784c5414/ambari-server/src/main/java/org/apache/ambari/server/checks/CheckDescription.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/CheckDescription.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/CheckDescription.java
index 4967834..d701bdc 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/checks/CheckDescription.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/CheckDescription.java
@@ -47,6 +47,13 @@ public enum CheckDescription {
"There are hosts which are not communicating with Ambari.");
}}),
+ HEALTH(PrereqCheckType.CLUSTER,
+ "Cluster Health",
+ new HashMap<String, String>() {{
+ put(AbstractCheckDescriptor.DEFAULT,
+ "The following issues have been detected on this cluster and should be addressed before upgrading: %s");
+ }}),
+
HOSTS_MAINTENANCE_MODE(PrereqCheckType.HOST,
"Hosts in Maintenance Mode will be excluded from the upgrade.",
new HashMap<String, String>() {{
http://git-wip-us.apache.org/repos/asf/ambari/blob/784c5414/ambari-server/src/main/java/org/apache/ambari/server/checks/HealthCheck.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/checks/HealthCheck.java b/ambari-server/src/main/java/org/apache/ambari/server/checks/HealthCheck.java
new file mode 100644
index 0000000..a1ab1eb
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/checks/HealthCheck.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.server.checks;
+
+
+import static java.util.Arrays.asList;
+import static org.apache.ambari.server.state.AlertState.CRITICAL;
+import static org.apache.ambari.server.state.AlertState.WARNING;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.controller.PrereqCheckRequest;
+import org.apache.ambari.server.orm.dao.AlertsDAO;
+import org.apache.ambari.server.orm.entities.AlertCurrentEntity;
+import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
+import org.apache.ambari.server.state.AlertState;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.MaintenanceState;
+import org.apache.ambari.server.state.stack.PrereqCheckStatus;
+import org.apache.ambari.server.state.stack.PrerequisiteCheck;
+import org.apache.commons.lang.StringUtils;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.Singleton;
+
+/**
+ * Checks that there are no WARNING/CRITICAL alerts on current cluster.
+ * That is a potential problem when doing stack update.
+ */
+@Singleton
+@UpgradeCheck(group = UpgradeCheckGroup.DEFAULT, required = true)
+public class HealthCheck extends AbstractCheckDescriptor {
+
+ private static final List<AlertState> ALERT_STATES = asList(WARNING, CRITICAL);
+
+ @Inject
+ Provider<AlertsDAO> alertsDAOProvider;
+
+ /**
+ * Constructor.
+ */
+ public HealthCheck() {
+ super(CheckDescription.HEALTH);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request)
+ throws AmbariException {
+
+ AlertsDAO alertsDAO = alertsDAOProvider.get();
+ final String clusterName = request.getClusterName();
+ final Cluster cluster = clustersProvider.get().getCluster(clusterName);
+ List<AlertCurrentEntity> alerts = alertsDAO.findCurrentByCluster(cluster.getClusterId());
+
+ List<String> errorMessages = new ArrayList<String>();
+
+ for (AlertCurrentEntity alert : alerts) {
+ AlertHistoryEntity alertHistory = alert.getAlertHistory();
+ AlertState alertState = alertHistory.getAlertState();
+ if (ALERT_STATES.contains(alertState) && !alert.getMaintenanceState().equals(MaintenanceState.ON)) {
+ String state = alertState.name();
+ String label = alertHistory.getAlertDefinition().getLabel();
+ String hostName = alertHistory.getHostName();
+
+ errorMessages.add(state + ": " + label + ": " + hostName);
+ prerequisiteCheck.getFailedDetail().add(new AlertDetail(state, label, hostName));
+ }
+ }
+
+ if (!errorMessages.isEmpty()) {
+ prerequisiteCheck.getFailedOn().add(clusterName);
+ prerequisiteCheck.setStatus(PrereqCheckStatus.WARNING);
+ String failReason = getFailReason(prerequisiteCheck, request);
+ prerequisiteCheck.setFailReason(String.format(failReason, StringUtils.join(errorMessages, "\n")));
+ }
+ }
+
+ /**
+ * Used to represent specific detail about alert.
+ */
+ private static class AlertDetail {
+ public String state;
+ public String label;
+ public String hostName;
+
+ AlertDetail(String state, String label, String hostName) {
+ this.state = state;
+ this.label = label;
+ this.hostName = hostName;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/ambari/blob/784c5414/ambari-server/src/test/java/org/apache/ambari/server/checks/HealthCheckTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/checks/HealthCheckTest.java b/ambari-server/src/test/java/org/apache/ambari/server/checks/HealthCheckTest.java
new file mode 100644
index 0000000..332c4f1
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/checks/HealthCheckTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.server.checks;
+
+import static java.util.Arrays.asList;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+
+import org.apache.ambari.server.AmbariException;
+import org.apache.ambari.server.controller.PrereqCheckRequest;
+import org.apache.ambari.server.orm.dao.AlertsDAO;
+import org.apache.ambari.server.orm.entities.AlertCurrentEntity;
+import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
+import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
+import org.apache.ambari.server.state.AlertState;
+import org.apache.ambari.server.state.Cluster;
+import org.apache.ambari.server.state.Clusters;
+import org.apache.ambari.server.state.stack.PrereqCheckStatus;
+import org.apache.ambari.server.state.stack.PrerequisiteCheck;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.inject.Provider;
+
+
+public class HealthCheckTest {
+ private static final String CLUSTER_NAME = "cluster1";
+ private static final long CLUSTER_ID = 1L;
+ private static final String ALERT_HOSTNAME = "some hostname 1";
+ private static final String ALERT_DEFINITION_LABEL = "label 1";
+ private HealthCheck healthCheck;
+ private AlertsDAO alertsDAO = mock(AlertsDAO.class);
+
+ @Before
+ public void setUp() throws Exception {
+ final Clusters clusters = mock(Clusters.class);
+
+ healthCheck = new HealthCheck();
+ healthCheck.alertsDAOProvider = new Provider<AlertsDAO>() {
+ @Override
+ public AlertsDAO get() {
+ return alertsDAO;
+ }
+ };
+
+ healthCheck.clustersProvider = new Provider<Clusters>() {
+ @Override
+ public Clusters get() {
+ return clusters;
+ }
+ };
+
+ Cluster cluster = mock(Cluster.class);
+ when(clusters.getCluster(CLUSTER_NAME)).thenReturn(cluster);
+ when(cluster.getClusterId()).thenReturn(CLUSTER_ID);
+
+ }
+
+ @Test
+ public void testWarningWhenNoAlertsExist() throws AmbariException {
+ when(alertsDAO.findCurrentByCluster(eq(CLUSTER_ID))).thenReturn(Collections.<AlertCurrentEntity>emptyList());
+
+ PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME);
+ healthCheck.perform(check, new PrereqCheckRequest(CLUSTER_NAME));
+ Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus());
+ }
+
+ @Test
+ public void testWarningWhenCriticalAlertExists() throws AmbariException {
+ expectWarning(AlertState.CRITICAL);
+ }
+
+ @Test
+ public void testWarningWhenWarningAlertExists() throws AmbariException {
+ expectWarning(AlertState.WARNING);
+ }
+
+ private void expectWarning(AlertState alertState) throws AmbariException {
+ AlertCurrentEntity alertCurrentEntity = new AlertCurrentEntity();
+ AlertHistoryEntity criticalAlert = new AlertHistoryEntity();
+ AlertDefinitionEntity alertDefinition = new AlertDefinitionEntity();
+
+ criticalAlert.setAlertDefinition(alertDefinition);
+ criticalAlert.setHostName(ALERT_HOSTNAME);
+ criticalAlert.setAlertState(alertState);
+
+ alertDefinition.setLabel(ALERT_DEFINITION_LABEL);
+
+ alertCurrentEntity.setAlertHistory(criticalAlert);
+
+ when(alertsDAO.findCurrentByCluster(eq(CLUSTER_ID))).thenReturn(asList(alertCurrentEntity));
+
+ PrerequisiteCheck check = new PrerequisiteCheck(null, CLUSTER_NAME);
+ healthCheck.perform(check, new PrereqCheckRequest(CLUSTER_NAME));
+ Assert.assertEquals(PrereqCheckStatus.WARNING, check.getStatus());
+ }
+}
\ No newline at end of file