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 HBaseTestingUtility UTIL = new HBaseTestingUtility();
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)
121      throws Exception {
122    if (!this.healthScriptFile.exists()) {
123      if (!healthScriptFile.createNewFile()) {
124        throw new IOException("Failed create of " + this.healthScriptFile);
125      }
126    }
127    PrintWriter pw = new PrintWriter(new FileOutputStream(healthScriptFile));
128    try {
129      pw.println(scriptStr);
130      pw.flush();
131    } finally {
132      pw.close();
133    }
134    healthScriptFile.setExecutable(setExecutable);
135    LOG.info("Created " + this.healthScriptFile + ", executable=" + setExecutable);
136  }
137
138  private Configuration getConfForNodeHealthScript() throws IOException {
139    Configuration conf = UTIL.getConfiguration();
140    File tempDir = new File(UTIL.getDataTestDir().toString());
141    if (!tempDir.exists()) {
142      if (!tempDir.mkdirs()) {
143        throw new IOException("Failed mkdirs " + tempDir);
144      }
145    }
146    String scriptName = "HealthScript" + UTIL.getRandomUUID().toString()
147        + (Shell.WINDOWS ? ".cmd" : ".sh");
148    healthScriptFile = new File(tempDir.getAbsolutePath(), scriptName);
149    conf.set(HConstants.HEALTH_SCRIPT_LOC, healthScriptFile.getAbsolutePath());
150    conf.setLong(HConstants.HEALTH_FAILURE_THRESHOLD, 3);
151    conf.setLong(HConstants.HEALTH_SCRIPT_TIMEOUT, SCRIPT_TIMEOUT);
152    return conf;
153  }
154
155  /**
156   * Simple helper class that just keeps track of whether or not its stopped.
157   */
158  private static class StoppableImplementation implements Stoppable {
159    private volatile boolean stop = false;
160
161    @Override
162    public void stop(String why) {
163      this.stop = true;
164    }
165
166    @Override
167    public boolean isStopped() {
168      return this.stop;
169    }
170
171  }
172}