View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.util.hbck;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Map.Entry;
27  import java.util.Set;
28  
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.client.HConnection;
32  import org.apache.hadoop.hbase.master.cleaner.ReplicationZKNodeCleaner;
33  import org.apache.hadoop.hbase.replication.ReplicationQueueInfo;
34  import org.apache.hadoop.hbase.util.HBaseFsck;
35  import org.apache.hadoop.hbase.util.HBaseFsck.ErrorReporter;
36  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
37  
38  /*
39   * Check and fix undeleted replication queues for removed peerId.
40   */
41  @InterfaceAudience.Private
42  public class ReplicationChecker {
43    private final ErrorReporter errorReporter;
44    // replicator with its queueIds for removed peers
45    private Map<String, List<String>> undeletedQueueIds = new HashMap<String, List<String>>();
46    // Set of un deleted hfile refs queue Ids
47    private Set<String> undeletedHFileRefsQueueIds = new HashSet<>();
48    private final ReplicationZKNodeCleaner cleaner;
49  
50    public ReplicationChecker(Configuration conf, ZooKeeperWatcher zkw, HConnection connection,
51        ErrorReporter errorReporter) throws IOException {
52      this.cleaner = new ReplicationZKNodeCleaner(conf, zkw, connection);
53      this.errorReporter = errorReporter;
54    }
55  
56    public boolean hasUnDeletedQueues() {
57      return errorReporter.getErrorList().contains(
58        HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE);
59    }
60  
61    public void checkUnDeletedQueues() throws IOException {
62      undeletedQueueIds = cleaner.getUnDeletedQueues();
63      for (Entry<String, List<String>> replicatorAndQueueIds : undeletedQueueIds.entrySet()) {
64        String replicator = replicatorAndQueueIds.getKey();
65        for (String queueId : replicatorAndQueueIds.getValue()) {
66          ReplicationQueueInfo queueInfo = new ReplicationQueueInfo(queueId);
67          String msg = "Undeleted replication queue for removed peer found: "
68              + String.format("[removedPeerId=%s, replicator=%s, queueId=%s]", queueInfo.getPeerId(),
69                replicator, queueId);
70          errorReporter.reportError(HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE,
71            msg);
72        }
73      }
74  
75      checkUnDeletedHFileRefsQueues();
76    }
77  
78    private void checkUnDeletedHFileRefsQueues() throws IOException {
79      undeletedHFileRefsQueueIds = cleaner.getUnDeletedHFileRefsQueues();
80      if (undeletedHFileRefsQueueIds != null && !undeletedHFileRefsQueueIds.isEmpty()) {
81        String msg = "Undeleted replication hfile-refs queue for removed peer found: "
82            + undeletedHFileRefsQueueIds + " under hfile-refs node";
83        errorReporter
84            .reportError(HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE, msg);
85      }
86    }
87  
88    public void fixUnDeletedQueues() throws IOException {
89      if (!undeletedQueueIds.isEmpty()) {
90        cleaner.removeQueues(undeletedQueueIds);
91      }
92      fixUnDeletedHFileRefsQueue();
93    }
94  
95    private void fixUnDeletedHFileRefsQueue() throws IOException {
96      if (undeletedHFileRefsQueueIds != null && !undeletedHFileRefsQueueIds.isEmpty()) {
97        cleaner.removeHFileRefsQueues(undeletedHFileRefsQueueIds);
98      }
99    }
100 }