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 org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.ServerName;
27  
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.List;
31  
32  /**
33   * This class is responsible for the parsing logic for a znode representing a queue.
34   * It will extract the peerId if it's recovered as well as the dead region servers
35   * that were part of the queue's history.
36   */
37  @InterfaceAudience.Private
38  public class ReplicationQueueInfo {
39    private static final Log LOG = LogFactory.getLog(ReplicationQueueInfo.class);
40  
41    private final String peerId;
42    private final String peerClusterZnode;
43    private boolean queueRecovered;
44    // List of all the dead region servers that had this queue (if recovered)
45    private List<String> deadRegionServers = new ArrayList<String>();
46  
47    /**
48     * The passed znode will be either the id of the peer cluster or
49     * the handling story of that queue in the form of id-servername-*
50     */
51    public ReplicationQueueInfo(String znode) {
52      this.peerClusterZnode = znode;
53      String[] parts = znode.split("-", 2);
54      this.queueRecovered = parts.length != 1;
55      this.peerId = this.queueRecovered ?
56          parts[0] : peerClusterZnode;
57      if (parts.length >= 2) {
58        // extract dead servers
59        extractDeadServersFromZNodeString(parts[1], this.deadRegionServers);
60      }
61    }
62  
63    /**
64     * Parse dead server names from znode string servername can contain "-" such as
65     * "ip-10-46-221-101.ec2.internal", so we need skip some "-" during parsing for the following
66     * cases: 2-ip-10-46-221-101.ec2.internal,52170,1364333181125-<server name>-...
67     */
68    private static void
69        extractDeadServersFromZNodeString(String deadServerListStr, List<String> result) {
70  
71      if(deadServerListStr == null || result == null || deadServerListStr.isEmpty()) return;
72  
73      // valid server name delimiter "-" has to be after "," in a server name
74      int seenCommaCnt = 0;
75      int startIndex = 0;
76      int len = deadServerListStr.length();
77  
78      for (int i = 0; i < len; i++) {
79        switch (deadServerListStr.charAt(i)) {
80        case ',':
81          seenCommaCnt += 1;
82          break;
83        case '-':
84          if(seenCommaCnt>=2) {
85            if (i > startIndex) {
86              String serverName = deadServerListStr.substring(startIndex, i);
87              if(ServerName.isFullServerName(serverName)){
88                result.add(serverName);
89              } else {
90                LOG.error("Found invalid server name:" + serverName);
91              }
92              startIndex = i + 1;
93            }
94            seenCommaCnt = 0;
95          }
96          break;
97        default:
98          break;
99        }
100     }
101 
102     // add tail
103     if(startIndex < len - 1){
104       String serverName = deadServerListStr.substring(startIndex, len);
105       if(ServerName.isFullServerName(serverName)){
106         result.add(serverName);
107       } else {
108         LOG.error("Found invalid server name at the end:" + serverName);
109       }
110     }
111 
112     LOG.debug("Found dead servers:" + result);
113   }
114 
115   public List<String> getDeadRegionServers() {
116     return Collections.unmodifiableList(this.deadRegionServers);
117   }
118 
119   public String getPeerId() {
120     return this.peerId;
121   }
122 
123   public String getPeerClusterZnode() {
124     return this.peerClusterZnode;
125   }
126 
127   public boolean isQueueRecovered() {
128     return queueRecovered;
129   }
130 }