001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.PrintWriter;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.fs.FileSystem;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.hbase.HealthChecker.HealthCheckerExitStatus;
032import org.apache.hadoop.hbase.testclassification.MiscTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.hadoop.util.Shell;
035import org.junit.After;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042@Category({ MiscTests.class, SmallTests.class })
043public class TestNodeHealthCheckChore {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestNodeHealthCheckChore.class);
048
049  private static final Logger LOG = LoggerFactory.getLogger(TestNodeHealthCheckChore.class);
050  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
051  private static final int SCRIPT_TIMEOUT = 5000;
052  private File healthScriptFile;
053  private String eol = System.getProperty("line.separator");
054
055  @After
056  public void cleanUp() throws IOException {
057    // delete and recreate the test directory, ensuring a clean test dir between tests
058    Path testDir = UTIL.getDataTestDir();
059    FileSystem fs = UTIL.getTestFileSystem();
060    fs.delete(testDir, true);
061    if (!fs.mkdirs(testDir)) throw new IOException("Failed mkdir " + testDir);
062  }
063
064  @Test
065  public void testHealthCheckerSuccess() throws Exception {
066    String normalScript = "echo \"I am all fine\"";
067    healthCheckerTest(normalScript, HealthCheckerExitStatus.SUCCESS);
068  }
069
070  @Test
071  public void testHealthCheckerFail() throws Exception {
072    String errorScript = "echo ERROR" + eol + "echo \"Node not healthy\"";
073    healthCheckerTest(errorScript, HealthCheckerExitStatus.FAILED);
074  }
075
076  @Test
077  public void testHealthCheckerTimeout() throws Exception {
078    String timeOutScript = "sleep 10" + eol + "echo \"I am fine\"";
079    healthCheckerTest(timeOutScript, HealthCheckerExitStatus.TIMED_OUT);
080  }
081
082  public void healthCheckerTest(String script, HealthCheckerExitStatus expectedStatus)
083    throws Exception {
084    Configuration config = getConfForNodeHealthScript();
085    config.addResource(healthScriptFile.getName());
086    String location = healthScriptFile.getAbsolutePath();
087    long timeout = config.getLong(HConstants.HEALTH_SCRIPT_TIMEOUT, SCRIPT_TIMEOUT);
088
089    HealthChecker checker = new HealthChecker();
090    checker.init(location, timeout);
091
092    createScript(script, true);
093    HealthReport report = checker.checkHealth();
094    assertEquals(expectedStatus, report.getStatus());
095
096    LOG.info("Health Status:" + report.getHealthReport());
097
098    this.healthScriptFile.delete();
099  }
100
101  @Test
102  public void testRSHealthChore() throws Exception {
103    Stoppable stop = new StoppableImplementation();
104    Configuration conf = getConfForNodeHealthScript();
105    String errorScript = "echo ERROR" + eol + " echo \"Server not healthy\"";
106    createScript(errorScript, true);
107    HealthCheckChore rsChore = new HealthCheckChore(100, stop, conf);
108    try {
109      // Default threshold is three.
110      rsChore.chore();
111      rsChore.chore();
112      assertFalse("Stoppable must not be stopped.", stop.isStopped());
113      rsChore.chore();
114      assertTrue("Stoppable must have been stopped.", stop.isStopped());
115    } finally {
116      stop.stop("Finished w/ test");
117    }
118  }
119
120  private void createScript(String scriptStr, boolean setExecutable) throws Exception {
121    if (!this.healthScriptFile.exists()) {
122      if (!healthScriptFile.createNewFile()) {
123        throw new IOException("Failed create of " + this.healthScriptFile);
124      }
125    }
126    PrintWriter pw = new PrintWriter(new FileOutputStream(healthScriptFile));
127    try {
128      pw.println(scriptStr);
129      pw.flush();
130    } finally {
131      pw.close();
132    }
133    healthScriptFile.setExecutable(setExecutable);
134    LOG.info("Created " + this.healthScriptFile + ", executable=" + setExecutable);
135  }
136
137  private Configuration getConfForNodeHealthScript() throws IOException {
138    Configuration conf = UTIL.getConfiguration();
139    File tempDir = new File(UTIL.getDataTestDir().toString());
140    if (!tempDir.exists()) {
141      if (!tempDir.mkdirs()) {
142        throw new IOException("Failed mkdirs " + tempDir);
143      }
144    }
145    String scriptName =
146      "HealthScript" + UTIL.getRandomUUID().toString() + (Shell.WINDOWS ? ".cmd" : ".sh");
147    healthScriptFile = new File(tempDir.getAbsolutePath(), scriptName);
148    conf.set(HConstants.HEALTH_SCRIPT_LOC, healthScriptFile.getAbsolutePath());
149    conf.setLong(HConstants.HEALTH_FAILURE_THRESHOLD, 3);
150    conf.setLong(HConstants.HEALTH_SCRIPT_TIMEOUT, SCRIPT_TIMEOUT);
151    return conf;
152  }
153
154  /**
155   * Simple helper class that just keeps track of whether or not its stopped.
156   */
157  private static class StoppableImplementation implements Stoppable {
158    private volatile boolean stop = false;
159
160    @Override
161    public void stop(String why) {
162      this.stop = true;
163    }
164
165    @Override
166    public boolean isStopped() {
167      return this.stop;
168    }
169
170  }
171}