View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.replication;
20  
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.ServerName;
30  
31  /**
32   * This class is responsible for the parsing logic for a znode representing a queue.
33   * It will extract the peerId if it's recovered as well as the dead region servers
34   * that were part of the queue's history.
35   */
36  @InterfaceAudience.Private
37  public class ReplicationQueueInfo {
38    private static final Log LOG = LogFactory.getLog(ReplicationQueueInfo.class);
39  
40    private final String peerId;
41    private final String peerClusterZnode;
42    private boolean queueRecovered;
43    // List of all the dead region servers that had this queue (if recovered)
44    private List<String> deadRegionServers = new ArrayList<String>();
45  
46    /**
47     * The passed znode will be either the id of the peer cluster or
48     * the handling story of that queue in the form of id-servername-*
49     */
50    public ReplicationQueueInfo(String znode) {
51      this.peerClusterZnode = znode;
52      String[] parts = znode.split("-", 2);
53      this.queueRecovered = parts.length != 1;
54      this.peerId = this.queueRecovered ?
55          parts[0] : peerClusterZnode;
56      if (parts.length >= 2) {
57        // extract dead servers
58        extractDeadServersFromZNodeString(parts[1], this.deadRegionServers);
59      }
60    }
61  
62    /**
63     * Parse dead server names from znode string servername can contain "-" such as
64     * "ip-10-46-221-101.ec2.internal", so we need skip some "-" during parsing for the following
65     * cases: 2-ip-10-46-221-101.ec2.internal,52170,1364333181125-<server name>-...
66     */
67    private static void
68        extractDeadServersFromZNodeString(String deadServerListStr, List<String> result) {
69  
70      if(deadServerListStr == null || result == null || deadServerListStr.isEmpty()) return;
71  
72      // valid server name delimiter "-" has to be after "," in a server name
73      int seenCommaCnt = 0;
74      int startIndex = 0;
75      int len = deadServerListStr.length();
76  
77      for (int i = 0; i < len; i++) {
78        switch (deadServerListStr.charAt(i)) {
79        case ',':
80          seenCommaCnt += 1;
81          break;
82        case '-':
83          if(seenCommaCnt>=2) {
84            if (i > startIndex) {
85              String serverName = deadServerListStr.substring(startIndex, i);
86              if(ServerName.isFullServerName(serverName)){
87                result.add(serverName);
88              } else {
89                LOG.error("Found invalid server name:" + serverName);
90              }
91              startIndex = i + 1;
92            }
93            seenCommaCnt = 0;
94          }
95          break;
96        default:
97          break;
98        }
99      }
100 
101     // add tail
102     if(startIndex < len - 1){
103       String serverName = deadServerListStr.substring(startIndex, len);
104       if(ServerName.isFullServerName(serverName)){
105         result.add(serverName);
106       } else {
107         LOG.error("Found invalid server name at the end:" + serverName);
108       }
109     }
110 
111     LOG.debug("Found dead servers:" + result);
112   }
113 
114   public List<String> getDeadRegionServers() {
115     return Collections.unmodifiableList(this.deadRegionServers);
116   }
117 
118   public String getPeerId() {
119     return this.peerId;
120   }
121 
122   public String getPeerClusterZnode() {
123     return this.peerClusterZnode;
124   }
125 
126   public boolean isQueueRecovered() {
127     return queueRecovered;
128   }
129 }