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