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.FileNotFoundException;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.LinkedList;
25  import java.util.List;
26  import java.util.NavigableMap;
27  import java.util.TreeMap;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.classification.InterfaceAudience;
32  import org.apache.hadoop.hbase.CoordinatedStateException;
33  import org.apache.hadoop.hbase.TableDescriptor;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.HRegionInfo;
36  import org.apache.hadoop.hbase.HTableDescriptor;
37  import org.apache.hadoop.hbase.InvalidFamilyOperationException;
38  import org.apache.hadoop.hbase.Server;
39  import org.apache.hadoop.hbase.ServerName;
40  import org.apache.hadoop.hbase.TableExistsException;
41  import org.apache.hadoop.hbase.TableNotDisabledException;
42  import org.apache.hadoop.hbase.MetaTableAccessor;
43  import org.apache.hadoop.hbase.client.HTable;
44  import org.apache.hadoop.hbase.client.TableState;
45  import org.apache.hadoop.hbase.executor.EventHandler;
46  import org.apache.hadoop.hbase.executor.EventType;
47  import org.apache.hadoop.hbase.master.BulkReOpen;
48  import org.apache.hadoop.hbase.master.MasterServices;
49  import org.apache.hadoop.hbase.master.TableLockManager.TableLock;
50  import org.apache.hadoop.hbase.util.Bytes;
51  
52  import com.google.common.collect.Lists;
53  import com.google.common.collect.Maps;
54  
55  /**
56   * Base class for performing operations against tables.
57   * Checks on whether the process can go forward are done in constructor rather
58   * than later on in {@link #process()}.  The idea is to fail fast rather than
59   * later down in an async invocation of {@link #process()} (which currently has
60   * no means of reporting back issues once started).
61   */
62  @InterfaceAudience.Private
63  public abstract class TableEventHandler extends EventHandler {
64    private static final Log LOG = LogFactory.getLog(TableEventHandler.class);
65    protected final MasterServices masterServices;
66    protected final TableName tableName;
67    protected TableLock tableLock;
68    private boolean isPrepareCalled = false;
69  
70    public TableEventHandler(EventType eventType, TableName tableName, Server server,
71        MasterServices masterServices) {
72      super(server, eventType);
73      this.masterServices = masterServices;
74      this.tableName = tableName;
75    }
76  
77    public TableEventHandler prepare() throws IOException {
78      //acquire the table write lock, blocking
79      this.tableLock = masterServices.getTableLockManager()
80          .writeLock(tableName, eventType.toString());
81      this.tableLock.acquire();
82      boolean success = false;
83      try {
84        try {
85          this.masterServices.checkTableModifiable(tableName);
86        } catch (TableNotDisabledException ex)  {
87          if (isOnlineSchemaChangeAllowed()
88              && eventType.isOnlineSchemaChangeSupported()) {
89            LOG.debug("Ignoring table not disabled exception " +
90                "for supporting online schema changes.");
91          } else {
92            throw ex;
93          }
94        }
95        prepareWithTableLock();
96        success = true;
97      } finally {
98        if (!success ) {
99          releaseTableLock();
100       }
101     }
102     this.isPrepareCalled = true;
103     return this;
104   }
105 
106   /** Called from prepare() while holding the table lock. Subclasses
107    * can do extra initialization, and not worry about the releasing
108    * the table lock. */
109   protected void prepareWithTableLock() throws IOException {
110   }
111 
112   private boolean isOnlineSchemaChangeAllowed() {
113     return this.server.getConfiguration().getBoolean(
114       "hbase.online.schema.update.enable", false);
115   }
116 
117   @Override
118   public void process() {
119     if (!isPrepareCalled) {
120       //For proper table locking semantics, the implementor should ensure to call
121       //TableEventHandler.prepare() before calling process()
122       throw new RuntimeException("Implementation should have called prepare() first");
123     }
124     try {
125       LOG.info("Handling table operation " + eventType + " on table " +
126           tableName);
127 
128       List<HRegionInfo> hris =
129         MetaTableAccessor.getTableRegions(this.server.getZooKeeper(),
130           this.server.getShortCircuitConnection(), tableName);
131       handleTableOperation(hris);
132       if (eventType.isOnlineSchemaChangeSupported() && this.masterServices.
133           getAssignmentManager().getTableStateManager().isTableState(
134           tableName, TableState.State.ENABLED)) {
135         if (reOpenAllRegions(hris)) {
136           LOG.info("Completed table operation " + eventType + " on table " +
137               tableName);
138         } else {
139           LOG.warn("Error on reopening the regions");
140         }
141       }
142       completed(null);
143     } catch (IOException e) {
144       LOG.error("Error manipulating table " + tableName, e);
145       completed(e);
146     } catch (CoordinatedStateException e) {
147       LOG.error("Error manipulating table " + tableName, e);
148       completed(e);
149     } finally {
150       releaseTableLock();
151     }
152   }
153 
154   protected void releaseTableLock() {
155     if (this.tableLock != null) {
156       try {
157         this.tableLock.release();
158       } catch (IOException ex) {
159         LOG.warn("Could not release the table lock", ex);
160       }
161     }
162   }
163 
164   /**
165    * Called after that process() is completed.
166    * @param exception null if process() is successful or not null if something has failed.
167    */
168   protected void completed(final Throwable exception) {
169   }
170 
171   public boolean reOpenAllRegions(List<HRegionInfo> regions) throws IOException {
172     boolean done = false;
173     LOG.info("Bucketing regions by region server...");
174     HTable table = new HTable(masterServices.getConfiguration(), tableName);
175     TreeMap<ServerName, List<HRegionInfo>> serverToRegions = Maps
176         .newTreeMap();
177     NavigableMap<HRegionInfo, ServerName> hriHserverMapping;
178     try {
179       hriHserverMapping = table.getRegionLocations();
180     } finally {
181       table.close();
182     }
183 
184     List<HRegionInfo> reRegions = new ArrayList<HRegionInfo>();
185     for (HRegionInfo hri : regions) {
186       ServerName rsLocation = hriHserverMapping.get(hri);
187 
188       // Skip the offlined split parent region
189       // See HBASE-4578 for more information.
190       if (null == rsLocation) {
191         LOG.info("Skip " + hri);
192         continue;
193       }
194       if (!serverToRegions.containsKey(rsLocation)) {
195         LinkedList<HRegionInfo> hriList = Lists.newLinkedList();
196         serverToRegions.put(rsLocation, hriList);
197       }
198       reRegions.add(hri);
199       serverToRegions.get(rsLocation).add(hri);
200     }
201 
202     LOG.info("Reopening " + reRegions.size() + " regions on "
203         + serverToRegions.size() + " region servers.");
204     this.masterServices.getAssignmentManager().setRegionsToReopen(reRegions);
205     BulkReOpen bulkReopen = new BulkReOpen(this.server, serverToRegions,
206         this.masterServices.getAssignmentManager());
207     while (true) {
208       try {
209         if (bulkReopen.bulkReOpen()) {
210           done = true;
211           break;
212         } else {
213           LOG.warn("Timeout before reopening all regions");
214         }
215       } catch (InterruptedException e) {
216         LOG.warn("Reopen was interrupted");
217         // Preserve the interrupt.
218         Thread.currentThread().interrupt();
219         break;
220       }
221     }
222     return done;
223   }
224 
225 
226   /**
227    * Gets a TableDescriptor from the masterServices.  Can Throw exceptions.
228    *
229    * @return Table descriptor for this table
230    * @throws TableExistsException
231    * @throws FileNotFoundException
232    * @throws IOException
233    */
234   public TableDescriptor getTableDescriptor()
235   throws FileNotFoundException, IOException {
236     TableDescriptor htd =
237       this.masterServices.getTableDescriptors().getDescriptor(tableName);
238     if (htd == null) {
239       throw new IOException("HTableDescriptor missing for " + tableName);
240     }
241     return htd;
242   }
243 
244   byte [] hasColumnFamily(final HTableDescriptor htd, final byte [] cf)
245   throws InvalidFamilyOperationException {
246     if (!htd.hasFamily(cf)) {
247       throw new InvalidFamilyOperationException("Column family '" +
248         Bytes.toString(cf) + "' does not exist");
249     }
250     return cf;
251   }
252 
253   protected abstract void handleTableOperation(List<HRegionInfo> regions)
254     throws IOException, CoordinatedStateException;
255 }