View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.master.procedure;
20  
21  import java.io.IOException;
22  import java.io.InterruptedIOException;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.CoordinatedStateException;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.MetaTableAccessor;
32  import org.apache.hadoop.hbase.NotAllMetaRegionsOnlineException;
33  import org.apache.hadoop.hbase.ServerName;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.classification.InterfaceStability;
37  import org.apache.hadoop.hbase.exceptions.TimeoutIOException;
38  import org.apache.hadoop.hbase.master.AssignmentManager;
39  import org.apache.hadoop.hbase.master.RegionStates;
40  import org.apache.hadoop.hbase.master.RegionState.State;
41  import org.apache.hadoop.hbase.master.ServerManager;
42  import org.apache.hadoop.hbase.procedure2.Procedure;
43  import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
44  import org.apache.hadoop.hbase.procedure2.ProcedureResult;
45  import org.apache.hadoop.hbase.quotas.MasterQuotaManager;
46  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
47  import org.apache.hadoop.hbase.util.Threads;
48  import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
49  
50  /**
51   * Helper to synchronously wait on conditions.
52   * This will be removed in the future (mainly when the AssignmentManager will be
53   * replaced with a Procedure version) by using ProcedureYieldException,
54   * and the queue will handle waiting and scheduling based on events.
55   */
56  @InterfaceAudience.Private
57  @InterfaceStability.Evolving
58  public final class ProcedureSyncWait {
59    private static final Log LOG = LogFactory.getLog(ProcedureSyncWait.class);
60  
61    private ProcedureSyncWait() {}
62  
63    @InterfaceAudience.Private
64    public interface Predicate<T> {
65      T evaluate() throws IOException;
66    }
67  
68    public static byte[] submitAndWaitProcedure(ProcedureExecutor<MasterProcedureEnv> procExec,
69        final Procedure proc) throws IOException {
70      long procId = procExec.submitProcedure(proc);
71      return waitForProcedureToComplete(procExec, procId);
72    }
73  
74    public static byte[] waitForProcedureToComplete(ProcedureExecutor<MasterProcedureEnv> procExec,
75        final long procId) throws IOException {
76      while (!procExec.isFinished(procId) && procExec.isRunning()) {
77        // TODO: add a config to make it tunable
78        // Dev Consideration: are we waiting forever, or we can set up some timeout value?
79        Threads.sleepWithoutInterrupt(250);
80      }
81      ProcedureResult result = procExec.getResult(procId);
82      if (result != null) {
83        if (result.isFailed()) {
84          // If the procedure fails, we should always have an exception captured. Throw it.
85          throw result.getException().unwrapRemoteException();
86        }
87        return result.getResult();
88      } else {
89        if (procExec.isRunning()) {
90          throw new IOException("Procedure " + procId + "not found");
91        } else {
92          throw new IOException("The Master is Aborting");
93        }
94      }
95    }
96  
97    public static <T> T waitFor(MasterProcedureEnv env, String purpose, Predicate<T> predicate)
98        throws IOException {
99      final Configuration conf = env.getMasterConfiguration();
100     final long waitTime = conf.getLong("hbase.master.wait.on.region", 5 * 60 * 1000);
101     final long waitingTimeForEvents = conf.getInt("hbase.master.event.waiting.time", 1000);
102     return waitFor(env, waitTime, waitingTimeForEvents, purpose, predicate);
103   }
104 
105   public static <T> T waitFor(MasterProcedureEnv env, long waitTime, long waitingTimeForEvents,
106       String purpose, Predicate<T> predicate) throws IOException {
107     final long done = EnvironmentEdgeManager.currentTime() + waitTime;
108     do {
109       T result = predicate.evaluate();
110       if (result != null && !result.equals(Boolean.FALSE)) {
111         return result;
112       }
113       try {
114         Thread.sleep(waitingTimeForEvents);
115       } catch (InterruptedException e) {
116         LOG.warn("Interrupted while sleeping, waiting on " + purpose);
117         throw (InterruptedIOException)new InterruptedIOException().initCause(e);
118       }
119       LOG.debug("Waiting on " + purpose);
120     } while (EnvironmentEdgeManager.currentTime() < done && env.isRunning());
121 
122     throw new TimeoutIOException("Timed out while waiting on " + purpose);
123   }
124 
125   protected static void waitMetaRegions(final MasterProcedureEnv env) throws IOException {
126     int timeout = env.getMasterConfiguration().getInt("hbase.client.catalog.timeout", 10000);
127     try {
128       if (env.getMasterServices().getMetaTableLocator().waitMetaRegionLocation(
129             env.getMasterServices().getZooKeeper(), timeout) == null) {
130         throw new NotAllMetaRegionsOnlineException();
131       }
132     } catch (InterruptedException e) {
133       throw (InterruptedIOException)new InterruptedIOException().initCause(e);
134     }
135   }
136 
137   protected static void waitRegionServers(final MasterProcedureEnv env) throws IOException {
138     final ServerManager sm = env.getMasterServices().getServerManager();
139     ProcedureSyncWait.waitFor(env, "server to assign region(s)",
140         new ProcedureSyncWait.Predicate<Boolean>() {
141       @Override
142       public Boolean evaluate() throws IOException {
143         List<ServerName> servers = sm.createDestinationServersList();
144         return servers != null && !servers.isEmpty();
145       }
146     });
147   }
148 
149   protected static List<HRegionInfo> getRegionsFromMeta(final MasterProcedureEnv env,
150       final TableName tableName) throws IOException {
151     return ProcedureSyncWait.waitFor(env, "regions of table=" + tableName + " from meta",
152         new ProcedureSyncWait.Predicate<List<HRegionInfo>>() {
153       @Override
154       public List<HRegionInfo> evaluate() throws IOException {
155         if (TableName.META_TABLE_NAME.equals(tableName)) {
156           return new MetaTableLocator().getMetaRegions(env.getMasterServices().getZooKeeper());
157         }
158         return MetaTableAccessor.getTableRegions(env.getMasterServices().getZooKeeper(),
159             env.getMasterServices().getConnection(), tableName);
160       }
161     });
162   }
163 
164   protected static void waitRegionInTransition(final MasterProcedureEnv env,
165       final List<HRegionInfo> regions) throws IOException, CoordinatedStateException {
166     final AssignmentManager am = env.getMasterServices().getAssignmentManager();
167     final RegionStates states = am.getRegionStates();
168     for (final HRegionInfo region : regions) {
169       ProcedureSyncWait.waitFor(env, "regions " + region.getRegionNameAsString() + " in transition",
170           new ProcedureSyncWait.Predicate<Boolean>() {
171         @Override
172         public Boolean evaluate() throws IOException {
173           if (states.isRegionInState(region, State.FAILED_OPEN)) {
174             am.regionOffline(region);
175           }
176           return !states.isRegionInTransition(region);
177         }
178       });
179     }
180   }
181 
182   protected static MasterQuotaManager getMasterQuotaManager(final MasterProcedureEnv env)
183       throws IOException {
184     return ProcedureSyncWait.waitFor(env, "quota manager to be available",
185       new ProcedureSyncWait.Predicate<MasterQuotaManager>() {
186         @Override
187         public MasterQuotaManager evaluate() throws IOException {
188           return env.getMasterServices().getMasterQuotaManager();
189         }
190       });
191   }
192 }