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 */
018
019package org.apache.hadoop.hbase.util.hbck;
020
021import java.io.IOException;
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.List;
025import java.util.Map;
026import java.util.Map.Entry;
027import java.util.Set;
028
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
031import org.apache.yetus.audience.InterfaceAudience;
032import org.apache.hadoop.hbase.client.ClusterConnection;
033import org.apache.hadoop.hbase.master.cleaner.ReplicationZKNodeCleaner;
034import org.apache.hadoop.hbase.replication.ReplicationQueueInfo;
035import org.apache.hadoop.hbase.util.HBaseFsck;
036import org.apache.hadoop.hbase.util.HBaseFsck.ErrorReporter;
037
038/*
039 * Check and fix undeleted replication queues for removed peerId.
040 */
041@InterfaceAudience.Private
042public class ReplicationChecker {
043  private final ErrorReporter errorReporter;
044  // replicator with its queueIds for removed peers
045  private Map<String, List<String>> undeletedQueueIds = new HashMap<>();
046  // replicator with its undeleted queueIds for removed peers in hfile-refs queue
047  private Set<String> undeletedHFileRefsQueueIds = new HashSet<>();
048  private final ReplicationZKNodeCleaner cleaner;
049
050  public ReplicationChecker(Configuration conf, ZKWatcher zkw, ClusterConnection connection,
051                            ErrorReporter errorReporter) throws IOException {
052    this.cleaner = new ReplicationZKNodeCleaner(conf, zkw, connection);
053    this.errorReporter = errorReporter;
054  }
055
056  public boolean hasUnDeletedQueues() {
057    return errorReporter.getErrorList().contains(
058      HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE);
059  }
060
061  public void checkUnDeletedQueues() throws IOException {
062    undeletedQueueIds = cleaner.getUnDeletedQueues();
063    for (Entry<String, List<String>> replicatorAndQueueIds : undeletedQueueIds.entrySet()) {
064      String replicator = replicatorAndQueueIds.getKey();
065      for (String queueId : replicatorAndQueueIds.getValue()) {
066        ReplicationQueueInfo queueInfo = new ReplicationQueueInfo(queueId);
067        String msg = "Undeleted replication queue for removed peer found: "
068            + String.format("[removedPeerId=%s, replicator=%s, queueId=%s]", queueInfo.getPeerId(),
069              replicator, queueId);
070        errorReporter.reportError(HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE,
071          msg);
072      }
073    }
074
075    checkUnDeletedHFileRefsQueues();
076  }
077
078  private void checkUnDeletedHFileRefsQueues() throws IOException {
079    undeletedHFileRefsQueueIds = cleaner.getUnDeletedHFileRefsQueues();
080    if (undeletedHFileRefsQueueIds != null && !undeletedHFileRefsQueueIds.isEmpty()) {
081      String msg = "Undeleted replication hfile-refs queue for removed peer found: "
082          + undeletedHFileRefsQueueIds + " under hfile-refs node";
083      errorReporter
084          .reportError(HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE, msg);
085    }
086  }
087
088  public void fixUnDeletedQueues() throws IOException {
089    if (!undeletedQueueIds.isEmpty()) {
090      cleaner.removeQueues(undeletedQueueIds);
091    }
092    fixUnDeletedHFileRefsQueue();
093  }
094
095  private void fixUnDeletedHFileRefsQueue() throws IOException {
096    if (undeletedHFileRefsQueueIds != null && !undeletedHFileRefsQueueIds.isEmpty()) {
097      cleaner.removeHFileRefsQueues(undeletedHFileRefsQueueIds);
098    }
099  }
100}