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.chaos.actions;
019
020import java.util.Arrays;
021import org.apache.commons.lang3.StringUtils;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
025import org.apache.hadoop.hbase.net.Address;
026import org.apache.hadoop.hdfs.DFSUtil;
027import org.apache.hadoop.hdfs.DistributedFileSystem;
028import org.apache.hadoop.hdfs.HAUtil;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032public class RestartRandomJournalNodeAction extends RestartActionBaseAction {
033  private static final Logger LOG = LoggerFactory.getLogger(RestartRandomJournalNodeAction.class);
034
035  public RestartRandomJournalNodeAction(long sleepTime) {
036    super(sleepTime);
037  }
038
039  @Override
040  protected Logger getLogger() {
041    return LOG;
042  }
043
044  @Override
045  public void perform() throws Exception {
046    getLogger().info("Performing action: Restart random JournalNode");
047
048    final String qjournal;
049    try (final DistributedFileSystem dfs = HdfsActionUtils.createDfs(getConf())) {
050      final Configuration conf = dfs.getConf();
051      final String nameServiceID = DFSUtil.getNamenodeNameServiceId(conf);
052      if (!HAUtil.isHAEnabled(conf, nameServiceID)) {
053        getLogger().info("HA for HDFS is not enabled; skipping");
054        return;
055      }
056
057      qjournal = conf.get("dfs.namenode.shared.edits.dir");
058      if (StringUtils.isEmpty(qjournal)) {
059        getLogger().info("Empty qjournals!");
060        return;
061      }
062    }
063
064    final ServerName journalNode =
065      PolicyBasedChaosMonkey.selectRandomItem(getJournalNodes(qjournal));
066    restartJournalNode(journalNode, sleepTime);
067  }
068
069  private static ServerName[] getJournalNodes(final String qjournal) {
070    // WARNING: HDFS internals. qjournal looks like this:
071    // qjournal://journalnode-0.example.com:8485;...;journalnode-N.example.com:8485/hk8se
072    // When done, we have an array of journalnodes+ports: e.g.journalnode-0.example.com:8485
073    final String[] journalNodes =
074      qjournal.toLowerCase().replaceAll("qjournal:\\/\\/", "").replaceAll("\\/.*$", "").split(";");
075    return Arrays.stream(journalNodes).map(Address::fromString)
076      .map(addr -> ServerName.valueOf(addr.getHostName(), addr.getPort()))
077      .toArray(ServerName[]::new);
078  }
079}