View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.master;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.RemoteExceptionHandler;
26  import org.apache.hadoop.hbase.TableNotDisabledException;
27  import org.apache.hadoop.hbase.TableNotFoundException;
28  import org.apache.hadoop.hbase.ipc.HRegionInterface;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.apache.hadoop.hbase.util.Sleeper;
31  import org.apache.hadoop.ipc.RemoteException;
32  
33  import java.io.IOException;
34  import java.util.ArrayList;
35  import java.util.List;
36  import java.util.concurrent.Callable;
37  
38  /**
39   * Uses Callable pattern so that operations against meta regions do not need
40   * to duplicate retry logic.
41   */
42  abstract class RetryableMetaOperation<T> implements Callable<T> {
43    protected final Log LOG = LogFactory.getLog(this.getClass());
44    protected final Sleeper sleeper;
45    protected final MetaRegion m;
46    protected final HMaster master;
47  
48    protected HRegionInterface server;
49  
50    protected RetryableMetaOperation(MetaRegion m, HMaster master) {
51      this.m = m;
52      this.master = master;
53      this.sleeper = new Sleeper(this.master.getThreadWakeFrequency(),
54        this.master.getClosed());
55    }
56  
57    protected T doWithRetries()
58    throws IOException, RuntimeException {
59      List<IOException> exceptions = new ArrayList<IOException>();
60      for (int tries = 0; tries < this.master.getNumRetries(); tries++) {
61        if (this.master.isClosed()) {
62          return null;
63        }
64        try {
65          this.server =
66            this.master.getServerConnection().getHRegionConnection(m.getServer());
67          return this.call();
68        } catch (IOException e) {
69          if (e instanceof TableNotFoundException ||
70              e instanceof TableNotDisabledException ||
71              e instanceof InvalidColumnNameException) {
72            throw e;
73          }
74          if (e instanceof RemoteException) {
75            e = RemoteExceptionHandler.decodeRemoteException((RemoteException) e);
76          }
77          if (tries == this.master.getNumRetries() - 1) {
78            if (LOG.isDebugEnabled()) {
79              StringBuilder message = new StringBuilder(
80                  "Trying to contact region server for regionName '" +
81                  Bytes.toString(m.getRegionName()) + "', but failed after " +
82                  (tries + 1) + " attempts.\n");
83              int i = 1;
84              for (IOException e2 : exceptions) {
85                message.append("Exception " + i + ":\n" + e2);
86              }
87              LOG.debug(message);
88            }
89            this.master.checkFileSystem();
90            throw e;
91          }
92          if (LOG.isDebugEnabled()) {
93            exceptions.add(e);
94          }
95        } catch (Exception e) {
96          LOG.debug("Exception in RetryableMetaOperation: ", e);
97          throw new RuntimeException(e);
98        }
99        this.sleeper.sleep();
100     }
101     return null;
102   }
103 }