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