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