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.master.handler;
20  
21  import java.io.IOException;
22  import java.io.InterruptedIOException;
23  import java.util.ArrayList;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.NavigableMap;
28  import java.util.Set;
29  import java.util.concurrent.locks.Lock;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.hadoop.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.HConstants;
35  import org.apache.hadoop.hbase.HRegionInfo;
36  import org.apache.hadoop.hbase.Server;
37  import org.apache.hadoop.hbase.ServerName;
38  import org.apache.hadoop.hbase.catalog.CatalogTracker;
39  import org.apache.hadoop.hbase.catalog.MetaReader;
40  import org.apache.hadoop.hbase.client.Result;
41  import org.apache.hadoop.hbase.executor.EventHandler;
42  import org.apache.hadoop.hbase.executor.EventType;
43  import org.apache.hadoop.hbase.master.AssignmentManager;
44  import org.apache.hadoop.hbase.master.DeadServer;
45  import org.apache.hadoop.hbase.master.MasterServices;
46  import org.apache.hadoop.hbase.master.RegionState;
47  import org.apache.hadoop.hbase.master.RegionState.State;
48  import org.apache.hadoop.hbase.master.RegionStates;
49  import org.apache.hadoop.hbase.master.ServerManager;
50  import org.apache.hadoop.hbase.regionserver.wal.HLogSplitter;
51  import org.apache.hadoop.hbase.zookeeper.ZKAssign;
52  import org.apache.zookeeper.KeeperException;
53  
54  /**
55   * Process server shutdown.
56   * Server-to-handle must be already in the deadservers lists.  See
57   * {@link ServerManager#expireServer(ServerName)}
58   */
59  @InterfaceAudience.Private
60  public class ServerShutdownHandler extends EventHandler {
61    private static final Log LOG = LogFactory.getLog(ServerShutdownHandler.class);
62    protected final ServerName serverName;
63    protected final MasterServices services;
64    protected final DeadServer deadServers;
65    protected final boolean shouldSplitHlog; // whether to split HLog or not
66    protected final boolean distributedLogReplay;
67    protected final int regionAssignmentWaitTimeout;
68  
69    public ServerShutdownHandler(final Server server, final MasterServices services,
70        final DeadServer deadServers, final ServerName serverName,
71        final boolean shouldSplitHlog) {
72      this(server, services, deadServers, serverName, EventType.M_SERVER_SHUTDOWN,
73          shouldSplitHlog);
74    }
75  
76    ServerShutdownHandler(final Server server, final MasterServices services,
77        final DeadServer deadServers, final ServerName serverName, EventType type,
78        final boolean shouldSplitHlog) {
79      super(server, type);
80      this.serverName = serverName;
81      this.server = server;
82      this.services = services;
83      this.deadServers = deadServers;
84      if (!this.deadServers.isDeadServer(this.serverName)) {
85        LOG.warn(this.serverName + " is NOT in deadservers; it should be!");
86      }
87      this.shouldSplitHlog = shouldSplitHlog;
88      this.distributedLogReplay = HLogSplitter.isDistributedLogReplay(server.getConfiguration());
89      this.regionAssignmentWaitTimeout = server.getConfiguration().getInt(
90        HConstants.LOG_REPLAY_WAIT_REGION_TIMEOUT, 15000);
91    }
92  
93    @Override
94    public String getInformativeName() {
95      if (serverName != null) {
96        return this.getClass().getSimpleName() + " for " + serverName;
97      } else {
98        return super.getInformativeName();
99      }
100   }
101 
102   /**
103    * @return True if the server we are processing was carrying <code>hbase:meta</code>
104    */
105   boolean isCarryingMeta() {
106     return false;
107   }
108 
109   @Override
110   public String toString() {
111     String name = "UnknownServerName";
112     if(server != null && server.getServerName() != null) {
113       name = server.getServerName().toString();
114     }
115     return getClass().getSimpleName() + "-" + name + "-" + getSeqid();
116   }
117 
118   @Override
119   public void process() throws IOException {
120     boolean hasLogReplayWork = false;
121     final ServerName serverName = this.serverName;
122     try {
123 
124       // We don't want worker thread in the MetaServerShutdownHandler
125       // executor pool to block by waiting availability of hbase:meta
126       // Otherwise, it could run into the following issue:
127       // 1. The current MetaServerShutdownHandler instance For RS1 waits for the hbase:meta
128       //    to come online.
129       // 2. The newly assigned hbase:meta region server RS2 was shutdown right after
130       //    it opens the hbase:meta region. So the MetaServerShutdownHandler
131       //    instance For RS1 will still be blocked.
132       // 3. The new instance of MetaServerShutdownHandler for RS2 is queued.
133       // 4. The newly assigned hbase:meta region server RS3 was shutdown right after
134       //    it opens the hbase:meta region. So the MetaServerShutdownHandler
135       //    instance For RS1 and RS2 will still be blocked.
136       // 5. The new instance of MetaServerShutdownHandler for RS3 is queued.
137       // 6. Repeat until we run out of MetaServerShutdownHandler worker threads
138       // The solution here is to resubmit a ServerShutdownHandler request to process
139       // user regions on that server so that MetaServerShutdownHandler
140       // executor pool is always available.
141       //
142       // If AssignmentManager hasn't finished rebuilding user regions,
143       // we are not ready to assign dead regions either. So we re-queue up
144       // the dead server for further processing too.
145       AssignmentManager am = services.getAssignmentManager();
146       if (isCarryingMeta() // hbase:meta
147           || !am.isFailoverCleanupDone()) {
148         this.services.getServerManager().processDeadServer(serverName, this.shouldSplitHlog);
149         return;
150       }
151 
152       // Wait on meta to come online; we need it to progress.
153       // TODO: Best way to hold strictly here?  We should build this retry logic
154       // into the MetaReader operations themselves.
155       // TODO: Is the reading of hbase:meta necessary when the Master has state of
156       // cluster in its head?  It should be possible to do without reading hbase:meta
157       // in all but one case. On split, the RS updates the hbase:meta
158       // table and THEN informs the master of the split via zk nodes in
159       // 'unassigned' dir.  Currently the RS puts ephemeral nodes into zk so if
160       // the regionserver dies, these nodes do not stick around and this server
161       // shutdown processing does fixup (see the fixupDaughters method below).
162       // If we wanted to skip the hbase:meta scan, we'd have to change at least the
163       // final SPLIT message to be permanent in zk so in here we'd know a SPLIT
164       // completed (zk is updated after edits to hbase:meta have gone in).  See
165       // {@link SplitTransaction}.  We'd also have to be figure another way for
166       // doing the below hbase:meta daughters fixup.
167       NavigableMap<HRegionInfo, Result> hris = null;
168       while (!this.server.isStopped()) {
169         try {
170           this.server.getCatalogTracker().waitForMeta();
171           hris = MetaReader.getServerUserRegions(this.server.getCatalogTracker(),
172             this.serverName);
173           break;
174         } catch (InterruptedException e) {
175           Thread.currentThread().interrupt();
176           throw (InterruptedIOException)new InterruptedIOException().initCause(e);
177         } catch (IOException ioe) {
178           LOG.info("Received exception accessing hbase:meta during server shutdown of " +
179             serverName + ", retrying hbase:meta read", ioe);
180         }
181       }
182       if (this.server.isStopped()) {
183         throw new IOException("Server is stopped");
184       }
185 
186       try {
187         if (this.shouldSplitHlog) {
188           LOG.info("Splitting logs for " + serverName + " before assignment.");
189           if (this.distributedLogReplay) {
190             LOG.info("Mark regions in recovery before assignment.");
191             Set<ServerName> serverNames = new HashSet<ServerName>();
192             serverNames.add(serverName);
193             this.services.getMasterFileSystem().prepareLogReplay(serverNames);
194           } else {
195             this.services.getMasterFileSystem().splitLog(serverName);
196           }
197           am.getRegionStates().logSplit(serverName);
198         } else {
199           LOG.info("Skipping log splitting for " + serverName);
200         }
201       } catch (IOException ioe) {
202         resubmit(serverName, ioe);
203       }
204 
205       // Clean out anything in regions in transition.  Being conservative and
206       // doing after log splitting.  Could do some states before -- OPENING?
207       // OFFLINE? -- and then others after like CLOSING that depend on log
208       // splitting.
209       List<HRegionInfo> regionsInTransition = am.processServerShutdown(serverName);
210       LOG.info("Reassigning " + ((hris == null)? 0: hris.size()) +
211         " region(s) that " + (serverName == null? "null": serverName)  +
212         " was carrying (and " + regionsInTransition.size() +
213         " regions(s) that were opening on this server)");
214 
215       List<HRegionInfo> toAssignRegions = new ArrayList<HRegionInfo>();
216       toAssignRegions.addAll(regionsInTransition);
217 
218       // Iterate regions that were on this server and assign them
219       if (hris != null) {
220         RegionStates regionStates = am.getRegionStates();
221         for (Map.Entry<HRegionInfo, Result> e: hris.entrySet()) {
222           HRegionInfo hri = e.getKey();
223           if (regionsInTransition.contains(hri)) {
224             continue;
225           }
226           String encodedName = hri.getEncodedName();
227           Lock lock = am.acquireRegionLock(encodedName);
228           try {
229             RegionState rit = regionStates.getRegionTransitionState(hri);
230             if (processDeadRegion(hri, e.getValue(), am, server.getCatalogTracker())) {
231               ServerName addressFromAM = regionStates.getRegionServerOfRegion(hri);
232               if (addressFromAM != null && !addressFromAM.equals(this.serverName)) {
233                 // If this region is in transition on the dead server, it must be
234                 // opening or pending_open, which should have been covered by AM#processServerShutdown
235                 LOG.info("Skip assigning region " + hri.getRegionNameAsString()
236                   + " because it has been opened in " + addressFromAM.getServerName());
237                 continue;
238               }
239               if (rit != null) {
240                 if (rit.getServerName() != null && !rit.isOnServer(serverName)) {
241                   // Skip regions that are in transition on other server
242                   LOG.info("Skip assigning region in transition on other server" + rit);
243                   continue;
244                 }
245                 try{
246                   //clean zk node
247                   LOG.info("Reassigning region with rs = " + rit + " and deleting zk node if exists");
248                   ZKAssign.deleteNodeFailSilent(services.getZooKeeper(), hri);
249                   regionStates.updateRegionState(hri, State.OFFLINE);
250                 } catch (KeeperException ke) {
251                   this.server.abort("Unexpected ZK exception deleting unassigned node " + hri, ke);
252                   return;
253                 }
254               } else if (regionStates.isRegionInState(
255                   hri, State.SPLITTING_NEW, State.MERGING_NEW)) {
256                 regionStates.regionOffline(hri);
257               }
258               toAssignRegions.add(hri);
259             } else if (rit != null) {
260               if (rit.isPendingCloseOrClosing()
261                   && am.getZKTable().isDisablingOrDisabledTable(hri.getTable())) {
262                 // If the table was partially disabled and the RS went down, we should clear the RIT
263                 // and remove the node for the region.
264                 // The rit that we use may be stale in case the table was in DISABLING state
265                 // but though we did assign we will not be clearing the znode in CLOSING state.
266                 // Doing this will have no harm. See HBASE-5927
267                 regionStates.updateRegionState(hri, State.OFFLINE);
268                 am.deleteClosingOrClosedNode(hri, rit.getServerName());
269                 am.offlineDisabledRegion(hri);
270               } else {
271                 LOG.warn("THIS SHOULD NOT HAPPEN: unexpected region in transition "
272                   + rit + " not to be assigned by SSH of server " + serverName);
273               }
274             }
275           } finally {
276             lock.unlock();
277           }
278         }
279       }
280 
281       try {
282         am.assign(toAssignRegions);
283       } catch (InterruptedException ie) {
284         LOG.error("Caught " + ie + " during round-robin assignment");
285         throw (InterruptedIOException)new InterruptedIOException().initCause(ie);
286       }
287 
288       if (this.shouldSplitHlog && this.distributedLogReplay) {
289         // wait for region assignment completes
290         for (HRegionInfo hri : toAssignRegions) {
291           try {
292             if (!am.waitOnRegionToClearRegionsInTransition(hri, regionAssignmentWaitTimeout)) {
293               // Wait here is to avoid log replay hits current dead server and incur a RPC timeout
294               // when replay happens before region assignment completes.
295               LOG.warn("Region " + hri.getEncodedName()
296                   + " didn't complete assignment in time");
297             }
298           } catch (InterruptedException ie) {
299             throw new InterruptedIOException("Caught " + ie
300                 + " during waitOnRegionToClearRegionsInTransition");
301           }
302         }
303         // submit logReplay work
304         this.services.getExecutorService().submit(
305           new LogReplayHandler(this.server, this.services, this.deadServers, this.serverName));
306         hasLogReplayWork = true;
307       }
308     } finally {
309       this.deadServers.finish(serverName);
310     }
311 
312     if (!hasLogReplayWork) {
313       LOG.info("Finished processing of shutdown of " + serverName);
314     }
315   }
316 
317   private void resubmit(final ServerName serverName, IOException ex) throws IOException {
318     // typecast to SSH so that we make sure that it is the SSH instance that
319     // gets submitted as opposed to MSSH or some other derived instance of SSH
320     this.services.getExecutorService().submit((ServerShutdownHandler) this);
321     this.deadServers.add(serverName);
322     throw new IOException("failed log splitting for " + serverName + ", will retry", ex);
323   }
324 
325   /**
326    * Process a dead region from a dead RS. Checks if the region is disabled or
327    * disabling or if the region has a partially completed split.
328    * @param hri
329    * @param result
330    * @param assignmentManager
331    * @param catalogTracker
332    * @return Returns true if specified region should be assigned, false if not.
333    * @throws IOException
334    */
335   public static boolean processDeadRegion(HRegionInfo hri, Result result,
336       AssignmentManager assignmentManager, CatalogTracker catalogTracker)
337   throws IOException {
338     boolean tablePresent = assignmentManager.getZKTable().isTablePresent(hri.getTable());
339     if (!tablePresent) {
340       LOG.info("The table " + hri.getTable()
341           + " was deleted.  Hence not proceeding.");
342       return false;
343     }
344     // If table is not disabled but the region is offlined,
345     boolean disabled = assignmentManager.getZKTable().isDisabledTable(hri.getTable());
346     if (disabled){
347       LOG.info("The table " + hri.getTable()
348           + " was disabled.  Hence not proceeding.");
349       return false;
350     }
351     if (hri.isOffline() && hri.isSplit()) {
352       //HBASE-7721: Split parent and daughters are inserted into hbase:meta as an atomic operation.
353       //If the meta scanner saw the parent split, then it should see the daughters as assigned
354       //to the dead server. We don't have to do anything.
355       return false;
356     }
357     boolean disabling = assignmentManager.getZKTable().isDisablingTable(hri.getTable());
358     if (disabling) {
359       LOG.info("The table " + hri.getTable()
360           + " is disabled.  Hence not assigning region" + hri.getEncodedName());
361       return false;
362     }
363     return true;
364   }
365 }