001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.master; 020 021import java.io.IOException; 022import java.util.List; 023import java.util.Set; 024import org.apache.hadoop.hbase.HConstants; 025import org.apache.hadoop.hbase.ServerName; 026import org.apache.hadoop.hbase.client.RegionInfo; 027import org.apache.hadoop.hbase.client.RegionInfoBuilder; 028import org.apache.hadoop.hbase.client.RegionReplicaUtil; 029import org.apache.hadoop.hbase.master.assignment.AssignmentManager; 030import org.apache.hadoop.hbase.zookeeper.MetaTableLocator; 031import org.apache.hadoop.hbase.zookeeper.ZKUtil; 032import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 033import org.apache.yetus.audience.InterfaceAudience; 034import org.apache.zookeeper.KeeperException; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038/** 039 * Used by the HMaster on startup to split meta logs and assign the meta table. 040 */ 041@InterfaceAudience.Private 042public class MasterMetaBootstrap { 043 private static final Logger LOG = LoggerFactory.getLogger(MasterMetaBootstrap.class); 044 045 private final HMaster master; 046 047 public MasterMetaBootstrap(HMaster master) { 048 this.master = master; 049 } 050 051 public void recoverMeta() throws InterruptedException, IOException { 052 // This is a blocking call that waits until hbase:meta is deployed. 053 master.recoverMeta(); 054 // Now we can start the TableStateManager. It is backed by hbase:meta. 055 master.getTableStateManager().start(); 056 // Enable server crash procedure handling 057 enableCrashedServerProcessing(); 058 } 059 060 public void processDeadServers() { 061 // get a list for previously failed RS which need log splitting work 062 // we recover hbase:meta region servers inside master initialization and 063 // handle other failed servers in SSH in order to start up master node ASAP 064 Set<ServerName> previouslyFailedServers = 065 master.getMasterWalManager().getFailedServersFromLogFolders(); 066 067 // Master has recovered hbase:meta region server and we put 068 // other failed region servers in a queue to be handled later by SSH 069 for (ServerName tmpServer : previouslyFailedServers) { 070 master.getServerManager().processDeadServer(tmpServer, true); 071 } 072 } 073 074 /** 075 * For assigning hbase:meta replicas only. 076 * TODO: The way this assign runs, nothing but chance to stop all replicas showing up on same 077 * server as the hbase:meta region. 078 */ 079 protected void assignMetaReplicas() 080 throws IOException, InterruptedException, KeeperException { 081 int numReplicas = master.getConfiguration().getInt(HConstants.META_REPLICAS_NUM, 082 HConstants.DEFAULT_META_REPLICA_NUM); 083 if (numReplicas <= 1) { 084 // No replicaas to assign. Return. 085 return; 086 } 087 final AssignmentManager assignmentManager = master.getAssignmentManager(); 088 if (!assignmentManager.isMetaInitialized()) { 089 throw new IllegalStateException("hbase:meta must be initialized first before we can " + 090 "assign out its replicas"); 091 } 092 ServerName metaServername = 093 this.master.getMetaTableLocator().getMetaRegionLocation(this.master.getZooKeeper()); 094 for (int i = 1; i < numReplicas; i++) { 095 // Get current meta state for replica from zk. 096 RegionState metaState = MetaTableLocator.getMetaRegionState(master.getZooKeeper(), i); 097 RegionInfo hri = RegionReplicaUtil.getRegionInfoForReplica( 098 RegionInfoBuilder.FIRST_META_REGIONINFO, i); 099 LOG.debug(hri.getRegionNameAsString() + " replica region state from zookeeper=" + metaState); 100 if (metaServername.equals(metaState.getServerName())) { 101 metaState = null; 102 LOG.info(hri.getRegionNameAsString() + 103 " old location is same as current hbase:meta location; setting location as null..."); 104 } 105 // These assigns run inline. All is blocked till they complete. Only interrupt is shutting 106 // down hosting server which calls AM#stop. 107 if (metaState != null && metaState.getServerName() != null) { 108 // Try to retain old assignment. 109 assignmentManager.assign(hri, metaState.getServerName()); 110 } else { 111 assignmentManager.assign(hri); 112 } 113 } 114 unassignExcessMetaReplica(numReplicas); 115 } 116 117 private void unassignExcessMetaReplica(int numMetaReplicasConfigured) { 118 final ZKWatcher zooKeeper = master.getZooKeeper(); 119 // unassign the unneeded replicas (for e.g., if the previous master was configured 120 // with a replication of 3 and now it is 2, we need to unassign the 1 unneeded replica) 121 try { 122 List<String> metaReplicaZnodes = zooKeeper.getMetaReplicaNodes(); 123 for (String metaReplicaZnode : metaReplicaZnodes) { 124 int replicaId = zooKeeper.znodePaths.getMetaReplicaIdFromZnode(metaReplicaZnode); 125 if (replicaId >= numMetaReplicasConfigured) { 126 RegionState r = MetaTableLocator.getMetaRegionState(zooKeeper, replicaId); 127 LOG.info("Closing excess replica of meta region " + r.getRegion()); 128 // send a close and wait for a max of 30 seconds 129 ServerManager.closeRegionSilentlyAndWait(master.getClusterConnection(), 130 r.getServerName(), r.getRegion(), 30000); 131 ZKUtil.deleteNode(zooKeeper, zooKeeper.znodePaths.getZNodeForReplica(replicaId)); 132 } 133 } 134 } catch (Exception ex) { 135 // ignore the exception since we don't want the master to be wedged due to potential 136 // issues in the cleanup of the extra regions. We can do that cleanup via hbck or manually 137 LOG.warn("Ignoring exception " + ex); 138 } 139 } 140 141 private void enableCrashedServerProcessing() throws InterruptedException { 142 // If crashed server processing is disabled, we enable it and expire those dead but not expired 143 // servers. This is required so that if meta is assigning to a server which dies after 144 // assignMeta starts assignment, ServerCrashProcedure can re-assign it. Otherwise, we will be 145 // stuck here waiting forever if waitForMeta is specified. 146 if (!master.isServerCrashProcessingEnabled()) { 147 master.setServerCrashProcessingEnabled(true); 148 master.getServerManager().processQueuedDeadServers(); 149 } 150 } 151}