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.InputStream;
23  import java.io.OutputStream;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.fs.FileSystem;
31  import org.apache.hadoop.fs.Path;
32  import org.apache.hadoop.hbase.DoNotRetryIOException;
33  import org.apache.hadoop.hbase.HRegionInfo;
34  import org.apache.hadoop.hbase.HTableDescriptor;
35  import org.apache.hadoop.hbase.MetaTableAccessor;
36  import org.apache.hadoop.hbase.TableExistsException;
37  import org.apache.hadoop.hbase.TableName;
38  import org.apache.hadoop.hbase.TableStateManager;
39  import org.apache.hadoop.hbase.classification.InterfaceAudience;
40  import org.apache.hadoop.hbase.client.RegionReplicaUtil;
41  import org.apache.hadoop.hbase.exceptions.HBaseException;
42  import org.apache.hadoop.hbase.master.AssignmentManager;
43  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
44  import org.apache.hadoop.hbase.master.MasterFileSystem;
45  import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
46  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
47  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos;
48  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.CreateTableState;
49  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
50  import org.apache.hadoop.hbase.security.User;
51  import org.apache.hadoop.hbase.util.FSTableDescriptors;
52  import org.apache.hadoop.hbase.util.FSUtils;
53  import org.apache.hadoop.hbase.util.ModifyRegionUtils;
54  import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil;
55  
56  import com.google.common.collect.Lists;
57  
58  @InterfaceAudience.Private
59  public class CreateTableProcedure
60      extends StateMachineProcedure<MasterProcedureEnv, CreateTableState>
61      implements TableProcedureInterface {
62    private static final Log LOG = LogFactory.getLog(CreateTableProcedure.class);
63  
64    private final AtomicBoolean aborted = new AtomicBoolean(false);
65  
66    // used for compatibility with old clients
67    private final ProcedurePrepareLatch syncLatch;
68  
69    private HTableDescriptor hTableDescriptor;
70    private List<HRegionInfo> newRegions;
71    private User user;
72  
73    public CreateTableProcedure() {
74      // Required by the Procedure framework to create the procedure on replay
75      syncLatch = null;
76    }
77  
78    public CreateTableProcedure(final MasterProcedureEnv env,
79        final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions) {
80      this(env, hTableDescriptor, newRegions, null);
81    }
82  
83    public CreateTableProcedure(final MasterProcedureEnv env,
84        final HTableDescriptor hTableDescriptor, final HRegionInfo[] newRegions,
85        final ProcedurePrepareLatch syncLatch) {
86      this.hTableDescriptor = hTableDescriptor;
87      this.newRegions = newRegions != null ? Lists.newArrayList(newRegions) : null;
88      this.user = env.getRequestUser();
89      this.setOwner(this.user.getShortName());
90  
91      // used for compatibility with clients without procedures
92      // they need a sync TableExistsException
93      this.syncLatch = syncLatch;
94    }
95  
96    @Override
97    protected Flow executeFromState(final MasterProcedureEnv env, final CreateTableState state)
98        throws InterruptedException {
99      if (LOG.isTraceEnabled()) {
100       LOG.trace(this + " execute state=" + state);
101     }
102     try {
103       switch (state) {
104         case CREATE_TABLE_PRE_OPERATION:
105           // Verify if we can create the table
106           boolean exists = !prepareCreate(env);
107           ProcedurePrepareLatch.releaseLatch(syncLatch, this);
108 
109           if (exists) {
110             assert isFailed() : "the delete should have an exception here";
111             return Flow.NO_MORE_STATE;
112           }
113 
114           preCreate(env);
115           setNextState(CreateTableState.CREATE_TABLE_WRITE_FS_LAYOUT);
116           break;
117         case CREATE_TABLE_WRITE_FS_LAYOUT:
118           newRegions = createFsLayout(env, hTableDescriptor, newRegions);
119           setNextState(CreateTableState.CREATE_TABLE_ADD_TO_META);
120           break;
121         case CREATE_TABLE_ADD_TO_META:
122           newRegions = addTableToMeta(env, hTableDescriptor, newRegions);
123           setNextState(CreateTableState.CREATE_TABLE_ASSIGN_REGIONS);
124           break;
125         case CREATE_TABLE_ASSIGN_REGIONS:
126           assignRegions(env, getTableName(), newRegions);
127           setNextState(CreateTableState.CREATE_TABLE_UPDATE_DESC_CACHE);
128           break;
129         case CREATE_TABLE_UPDATE_DESC_CACHE:
130           updateTableDescCache(env, getTableName());
131           setNextState(CreateTableState.CREATE_TABLE_POST_OPERATION);
132           break;
133         case CREATE_TABLE_POST_OPERATION:
134           postCreate(env);
135           return Flow.NO_MORE_STATE;
136         default:
137           throw new UnsupportedOperationException("unhandled state=" + state);
138       }
139     } catch (HBaseException|IOException e) {
140       LOG.error("Error trying to create table=" + getTableName() + " state=" + state, e);
141       setFailure("master-create-table", e);
142     }
143     return Flow.HAS_MORE_STATE;
144   }
145 
146   @Override
147   protected void rollbackState(final MasterProcedureEnv env, final CreateTableState state)
148       throws IOException {
149     if (LOG.isTraceEnabled()) {
150       LOG.trace(this + " rollback state=" + state);
151     }
152     try {
153       switch (state) {
154         case CREATE_TABLE_POST_OPERATION:
155           break;
156         case CREATE_TABLE_UPDATE_DESC_CACHE:
157           DeleteTableProcedure.deleteTableDescriptorCache(env, getTableName());
158           break;
159         case CREATE_TABLE_ASSIGN_REGIONS:
160           DeleteTableProcedure.deleteAssignmentState(env, getTableName());
161           break;
162         case CREATE_TABLE_ADD_TO_META:
163           DeleteTableProcedure.deleteFromMeta(env, getTableName(), newRegions);
164           break;
165         case CREATE_TABLE_WRITE_FS_LAYOUT:
166           DeleteTableProcedure.deleteFromFs(env, getTableName(), newRegions, false);
167           break;
168         case CREATE_TABLE_PRE_OPERATION:
169           if (hasException() /* avoid NPE */ &&
170               getException().getCause().getClass() != TableExistsException.class) {
171             DeleteTableProcedure.deleteTableStates(env, getTableName());
172             // TODO-MAYBE: call the deleteTable coprocessor event?
173             final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
174             if (cpHost != null) {
175               cpHost.postDeleteTable(getTableName());
176             }
177           }
178           ProcedurePrepareLatch.releaseLatch(syncLatch, this);
179           break;
180         default:
181           throw new UnsupportedOperationException("unhandled state=" + state);
182       }
183     } catch (HBaseException e) {
184       LOG.warn("Failed rollback attempt step=" + state + " table=" + getTableName(), e);
185       throw new IOException(e);
186     } catch (IOException e) {
187       // This will be retried. Unless there is a bug in the code,
188       // this should be just a "temporary error" (e.g. network down)
189       LOG.warn("Failed rollback attempt step=" + state + " table=" + getTableName(), e);
190       throw e;
191     }
192   }
193 
194   @Override
195   protected CreateTableState getState(final int stateId) {
196     return CreateTableState.valueOf(stateId);
197   }
198 
199   @Override
200   protected int getStateId(final CreateTableState state) {
201     return state.getNumber();
202   }
203 
204   @Override
205   protected CreateTableState getInitialState() {
206     return CreateTableState.CREATE_TABLE_PRE_OPERATION;
207   }
208 
209   @Override
210   protected void setNextState(final CreateTableState state) {
211     if (aborted.get()) {
212       setAbortFailure("create-table", "abort requested");
213     } else {
214       super.setNextState(state);
215     }
216   }
217 
218   @Override
219   public TableName getTableName() {
220     return hTableDescriptor.getTableName();
221   }
222 
223   @Override
224   public TableOperationType getTableOperationType() {
225     return TableOperationType.CREATE;
226   }
227 
228   @Override
229   public boolean abort(final MasterProcedureEnv env) {
230     aborted.set(true);
231     return true;
232   }
233 
234   @Override
235   public void toStringClassDetails(StringBuilder sb) {
236     sb.append(getClass().getSimpleName());
237     sb.append(" (table=");
238     sb.append(getTableName());
239     sb.append(")");
240   }
241 
242   @Override
243   public void serializeStateData(final OutputStream stream) throws IOException {
244     super.serializeStateData(stream);
245 
246     MasterProcedureProtos.CreateTableStateData.Builder state =
247       MasterProcedureProtos.CreateTableStateData.newBuilder()
248         .setUserInfo(MasterProcedureUtil.toProtoUserInfo(this.user))
249         .setTableSchema(hTableDescriptor.convert());
250     if (newRegions != null) {
251       for (HRegionInfo hri: newRegions) {
252         state.addRegionInfo(HRegionInfo.convert(hri));
253       }
254     }
255     state.build().writeDelimitedTo(stream);
256   }
257 
258   @Override
259   public void deserializeStateData(final InputStream stream) throws IOException {
260     super.deserializeStateData(stream);
261 
262     MasterProcedureProtos.CreateTableStateData state =
263       MasterProcedureProtos.CreateTableStateData.parseDelimitedFrom(stream);
264     user = MasterProcedureUtil.toUserInfo(state.getUserInfo());
265     hTableDescriptor = HTableDescriptor.convert(state.getTableSchema());
266     if (state.getRegionInfoCount() == 0) {
267       newRegions = null;
268     } else {
269       newRegions = new ArrayList<HRegionInfo>(state.getRegionInfoCount());
270       for (HBaseProtos.RegionInfo hri: state.getRegionInfoList()) {
271         newRegions.add(HRegionInfo.convert(hri));
272       }
273     }
274   }
275 
276   @Override
277   protected boolean acquireLock(final MasterProcedureEnv env) {
278     if (!getTableName().isSystemTable() && env.waitInitialized(this)) {
279       return false;
280     }
281     return env.getProcedureQueue().tryAcquireTableExclusiveLock(this, getTableName());
282   }
283 
284   @Override
285   protected void releaseLock(final MasterProcedureEnv env) {
286     env.getProcedureQueue().releaseTableExclusiveLock(this, getTableName());
287   }
288 
289   private boolean prepareCreate(final MasterProcedureEnv env) throws IOException {
290     final TableName tableName = getTableName();
291     if (MetaTableAccessor.tableExists(env.getMasterServices().getConnection(), tableName)) {
292       setFailure("master-create-table", new TableExistsException(getTableName()));
293       return false;
294     }
295     // During master initialization, the ZK state could be inconsistent from failed DDL
296     // in the past. If we fail here, it would prevent master to start.  We should force
297     // setting the system table state regardless the table state.
298     boolean skipTableStateCheck =
299         !(env.getMasterServices().isInitialized()) && tableName.isSystemTable();
300     if (!skipTableStateCheck) {
301       TableStateManager tsm = env.getMasterServices().getAssignmentManager().getTableStateManager();
302       if (tsm.isTableState(tableName, true, ZooKeeperProtos.Table.State.ENABLING,
303           ZooKeeperProtos.Table.State.ENABLED)) {
304         LOG.warn("The table " + tableName + " does not exist in meta but has a znode. " +
305                "run hbck to fix inconsistencies.");
306         setFailure("master-create-table", new TableExistsException(getTableName()));
307         return false;
308       }
309     }
310 
311     // check that we have at least 1 CF
312     if (hTableDescriptor.getColumnFamilies().length == 0) {
313       setFailure("master-create-table", new DoNotRetryIOException("Table " +
314           getTableName().toString() + " should have at least one column family."));
315       return false;
316     }
317 
318     return true;
319   }
320 
321   private void preCreate(final MasterProcedureEnv env)
322       throws IOException, InterruptedException {
323     if (!getTableName().isSystemTable()) {
324       ProcedureSyncWait.getMasterQuotaManager(env)
325         .checkNamespaceTableAndRegionQuota(getTableName(), newRegions.size());
326     }
327 
328     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
329     if (cpHost != null) {
330       final HRegionInfo[] regions = newRegions == null ? null :
331         newRegions.toArray(new HRegionInfo[newRegions.size()]);
332       cpHost.preCreateTableHandler(hTableDescriptor, regions, user);
333     }
334   }
335 
336   private void postCreate(final MasterProcedureEnv env)
337       throws IOException, InterruptedException {
338     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
339     if (cpHost != null) {
340       final HRegionInfo[] regions = (newRegions == null) ? null :
341         newRegions.toArray(new HRegionInfo[newRegions.size()]);
342       cpHost.postCreateTableHandler(hTableDescriptor, regions, user);
343     }
344   }
345 
346   protected interface CreateHdfsRegions {
347     List<HRegionInfo> createHdfsRegions(final MasterProcedureEnv env,
348       final Path tableRootDir, final TableName tableName,
349       final List<HRegionInfo> newRegions) throws IOException;
350   }
351 
352   protected static List<HRegionInfo> createFsLayout(final MasterProcedureEnv env,
353       final HTableDescriptor hTableDescriptor, final List<HRegionInfo> newRegions)
354       throws IOException {
355     return createFsLayout(env, hTableDescriptor, newRegions, new CreateHdfsRegions() {
356       @Override
357       public List<HRegionInfo> createHdfsRegions(final MasterProcedureEnv env,
358           final Path tableRootDir, final TableName tableName,
359           final List<HRegionInfo> newRegions) throws IOException {
360         HRegionInfo[] regions = newRegions != null ?
361           newRegions.toArray(new HRegionInfo[newRegions.size()]) : null;
362         return ModifyRegionUtils.createRegions(env.getMasterConfiguration(),
363             tableRootDir, hTableDescriptor, regions, null);
364       }
365     });
366   }
367 
368   protected static List<HRegionInfo> createFsLayout(final MasterProcedureEnv env,
369       final HTableDescriptor hTableDescriptor, List<HRegionInfo> newRegions,
370       final CreateHdfsRegions hdfsRegionHandler) throws IOException {
371     final MasterFileSystem mfs = env.getMasterServices().getMasterFileSystem();
372     final Path tempdir = mfs.getTempDir();
373 
374     // 1. Create Table Descriptor
375     // using a copy of descriptor, table will be created enabling first
376     final Path tempTableDir = FSUtils.getTableDir(tempdir, hTableDescriptor.getTableName());
377     new FSTableDescriptors(env.getMasterConfiguration()).createTableDescriptorForTableDirectory(
378       tempTableDir, hTableDescriptor, false);
379 
380     // 2. Create Regions
381     newRegions = hdfsRegionHandler.createHdfsRegions(env, tempdir,
382       hTableDescriptor.getTableName(), newRegions);
383 
384     // 3. Move Table temp directory to the hbase root location
385     final Path tableDir = FSUtils.getTableDir(mfs.getRootDir(), hTableDescriptor.getTableName());
386     FileSystem fs = mfs.getFileSystem();
387     if (!fs.delete(tableDir, true) && fs.exists(tableDir)) {
388       throw new IOException("Couldn't delete " + tableDir);
389     }
390     if (!fs.rename(tempTableDir, tableDir)) {
391       throw new IOException("Unable to move table from temp=" + tempTableDir +
392         " to hbase root=" + tableDir);
393     }
394     return newRegions;
395   }
396 
397   protected static List<HRegionInfo> addTableToMeta(final MasterProcedureEnv env,
398       final HTableDescriptor hTableDescriptor,
399       final List<HRegionInfo> regions) throws IOException {
400     if (regions != null && regions.size() > 0) {
401       ProcedureSyncWait.waitMetaRegions(env);
402 
403       // Add regions to META
404       addRegionsToMeta(env, hTableDescriptor, regions);
405       // Add replicas if needed
406       List<HRegionInfo> newRegions = addReplicas(env, hTableDescriptor, regions);
407 
408       // Setup replication for region replicas if needed
409       if (hTableDescriptor.getRegionReplication() > 1) {
410         ServerRegionReplicaUtil.setupRegionReplicaReplication(env.getMasterConfiguration());
411       }
412       return newRegions;
413     }
414     return regions;
415   }
416 
417   /**
418    * Create any replicas for the regions (the default replicas that was
419    * already created is passed to the method)
420    * @param hTableDescriptor descriptor to use
421    * @param regions default replicas
422    * @return the combined list of default and non-default replicas
423    */
424   private static List<HRegionInfo> addReplicas(final MasterProcedureEnv env,
425       final HTableDescriptor hTableDescriptor,
426       final List<HRegionInfo> regions) {
427     int numRegionReplicas = hTableDescriptor.getRegionReplication() - 1;
428     if (numRegionReplicas <= 0) {
429       return regions;
430     }
431     List<HRegionInfo> hRegionInfos =
432         new ArrayList<HRegionInfo>((numRegionReplicas+1)*regions.size());
433     for (int i = 0; i < regions.size(); i++) {
434       for (int j = 1; j <= numRegionReplicas; j++) {
435         hRegionInfos.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(i), j));
436       }
437     }
438     hRegionInfos.addAll(regions);
439     return hRegionInfos;
440   }
441 
442   protected static void assignRegions(final MasterProcedureEnv env,
443       final TableName tableName, final List<HRegionInfo> regions)
444       throws HBaseException, IOException {
445     ProcedureSyncWait.waitRegionServers(env);
446 
447     final AssignmentManager assignmentManager = env.getMasterServices().getAssignmentManager();
448 
449     // Mark the table as Enabling
450     assignmentManager.getTableStateManager().setTableState(tableName,
451         ZooKeeperProtos.Table.State.ENABLING);
452 
453     // Trigger immediate assignment of the regions in round-robin fashion
454     ModifyRegionUtils.assignRegions(assignmentManager, regions);
455 
456     // Enable table
457     assignmentManager.getTableStateManager()
458       .setTableState(tableName, ZooKeeperProtos.Table.State.ENABLED);
459   }
460 
461   /**
462    * Add the specified set of regions to the hbase:meta table.
463    */
464   protected static void addRegionsToMeta(final MasterProcedureEnv env,
465       final HTableDescriptor hTableDescriptor,
466       final List<HRegionInfo> regionInfos) throws IOException {
467     MetaTableAccessor.addRegionsToMeta(env.getMasterServices().getConnection(),
468       regionInfos, hTableDescriptor.getRegionReplication());
469   }
470 
471   protected static void updateTableDescCache(final MasterProcedureEnv env,
472       final TableName tableName) throws IOException {
473     env.getMasterServices().getTableDescriptors().get(tableName);
474   }
475 
476   @Override
477   protected boolean shouldWaitClientAck(MasterProcedureEnv env) {
478     // system tables are created on bootstrap internally by the system
479     // the client does not know about this procedures.
480     return !getTableName().isSystemTable();
481   }
482 }