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        throws IOException {
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        throws IOException {
88      this.hTableDescriptor = hTableDescriptor;
89      this.newRegions = newRegions != null ? Lists.newArrayList(newRegions) : null;
90      this.user = env.getRequestUser().getUGI();
91      this.setOwner(this.user.getShortUserName());
92  
93      // used for compatibility with clients without procedures
94      // they need a sync TableExistsException
95      this.syncLatch = syncLatch;
96    }
97  
98    @Override
99    protected Flow executeFromState(final MasterProcedureEnv env, final CreateTableState state) {
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 (InterruptedException|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 (!env.isInitialized() && !getTableName().isSystemTable()) {
273       return false;
274     }
275     return env.getProcedureQueue().tryAcquireTableWrite(getTableName(), "create table");
276   }
277 
278   @Override
279   protected void releaseLock(final MasterProcedureEnv env) {
280     env.getProcedureQueue().releaseTableWrite(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     return true;
305   }
306 
307   private void preCreate(final MasterProcedureEnv env)
308       throws IOException, InterruptedException {
309     if (!getTableName().isSystemTable()) {
310       ProcedureSyncWait.getMasterQuotaManager(env).checkNamespaceTableAndRegionQuota(
311         getTableName(), newRegions.size());
312     }
313     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
314     if (cpHost != null) {
315       final HRegionInfo[] regions = newRegions == null ? null :
316         newRegions.toArray(new HRegionInfo[newRegions.size()]);
317       user.doAs(new PrivilegedExceptionAction<Void>() {
318         @Override
319         public Void run() throws Exception {
320           cpHost.preCreateTableHandler(hTableDescriptor, regions);
321           return null;
322         }
323       });
324     }
325   }
326 
327   private void postCreate(final MasterProcedureEnv env)
328       throws IOException, InterruptedException {
329     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
330     if (cpHost != null) {
331       final HRegionInfo[] regions = (newRegions == null) ? null :
332         newRegions.toArray(new HRegionInfo[newRegions.size()]);
333       user.doAs(new PrivilegedExceptionAction<Void>() {
334         @Override
335         public Void run() throws Exception {
336           cpHost.postCreateTableHandler(hTableDescriptor, regions);
337           return null;
338         }
339       });
340     }
341   }
342 
343   protected interface CreateHdfsRegions {
344     List<HRegionInfo> createHdfsRegions(final MasterProcedureEnv env,
345       final Path tableRootDir, final TableName tableName,
346       final List<HRegionInfo> newRegions) throws IOException;
347   }
348 
349   protected static List<HRegionInfo> createFsLayout(final MasterProcedureEnv env,
350       final HTableDescriptor hTableDescriptor, final List<HRegionInfo> newRegions)
351       throws IOException {
352     return createFsLayout(env, hTableDescriptor, newRegions, new CreateHdfsRegions() {
353       @Override
354       public List<HRegionInfo> createHdfsRegions(final MasterProcedureEnv env,
355           final Path tableRootDir, final TableName tableName,
356           final List<HRegionInfo> newRegions) throws IOException {
357         HRegionInfo[] regions = newRegions != null ?
358           newRegions.toArray(new HRegionInfo[newRegions.size()]) : null;
359         return ModifyRegionUtils.createRegions(env.getMasterConfiguration(),
360             tableRootDir, hTableDescriptor, regions, null);
361       }
362     });
363   }
364 
365   protected static List<HRegionInfo> createFsLayout(final MasterProcedureEnv env,
366       final HTableDescriptor hTableDescriptor, List<HRegionInfo> newRegions,
367       final CreateHdfsRegions hdfsRegionHandler) throws IOException {
368     final MasterFileSystem mfs = env.getMasterServices().getMasterFileSystem();
369     final Path tempdir = mfs.getTempDir();
370 
371     // 1. Create Table Descriptor
372     // using a copy of descriptor, table will be created enabling first
373     final Path tempTableDir = FSUtils.getTableDir(tempdir, hTableDescriptor.getTableName());
374     new FSTableDescriptors(env.getMasterConfiguration()).createTableDescriptorForTableDirectory(
375       tempTableDir, hTableDescriptor, false);
376 
377     // 2. Create Regions
378     newRegions = hdfsRegionHandler.createHdfsRegions(env, tempdir,
379       hTableDescriptor.getTableName(), newRegions);
380 
381     // 3. Move Table temp directory to the hbase root location
382     final Path tableDir = FSUtils.getTableDir(mfs.getRootDir(), hTableDescriptor.getTableName());
383     FileSystem fs = mfs.getFileSystem();
384     if (!fs.delete(tableDir, true) && fs.exists(tableDir)) {
385       throw new IOException("Couldn't delete " + tableDir);
386     }
387     if (!fs.rename(tempTableDir, tableDir)) {
388       throw new IOException("Unable to move table from temp=" + tempTableDir +
389         " to hbase root=" + tableDir);
390     }
391     return newRegions;
392   }
393 
394   protected static List<HRegionInfo> addTableToMeta(final MasterProcedureEnv env,
395       final HTableDescriptor hTableDescriptor,
396       final List<HRegionInfo> regions) throws IOException {
397     if (regions != null && regions.size() > 0) {
398       ProcedureSyncWait.waitMetaRegions(env);
399 
400       // Add regions to META
401       addRegionsToMeta(env, hTableDescriptor, regions);
402       // Add replicas if needed
403       List<HRegionInfo> newRegions = addReplicas(env, hTableDescriptor, regions);
404 
405       // Setup replication for region replicas if needed
406       if (hTableDescriptor.getRegionReplication() > 1) {
407         ServerRegionReplicaUtil.setupRegionReplicaReplication(env.getMasterConfiguration());
408       }
409       return newRegions;
410     }
411     return regions;
412   }
413 
414   /**
415    * Create any replicas for the regions (the default replicas that was
416    * already created is passed to the method)
417    * @param hTableDescriptor descriptor to use
418    * @param regions default replicas
419    * @return the combined list of default and non-default replicas
420    */
421   private static List<HRegionInfo> addReplicas(final MasterProcedureEnv env,
422       final HTableDescriptor hTableDescriptor,
423       final List<HRegionInfo> regions) {
424     int numRegionReplicas = hTableDescriptor.getRegionReplication() - 1;
425     if (numRegionReplicas <= 0) {
426       return regions;
427     }
428     List<HRegionInfo> hRegionInfos =
429         new ArrayList<HRegionInfo>((numRegionReplicas+1)*regions.size());
430     for (int i = 0; i < regions.size(); i++) {
431       for (int j = 1; j <= numRegionReplicas; j++) {
432         hRegionInfos.add(RegionReplicaUtil.getRegionInfoForReplica(regions.get(i), j));
433       }
434     }
435     hRegionInfos.addAll(regions);
436     return hRegionInfos;
437   }
438 
439   protected static void assignRegions(final MasterProcedureEnv env,
440       final TableName tableName, final List<HRegionInfo> regions)
441       throws HBaseException, IOException {
442     ProcedureSyncWait.waitRegionServers(env);
443 
444     final AssignmentManager assignmentManager = env.getMasterServices().getAssignmentManager();
445 
446     // Mark the table as Enabling
447     assignmentManager.getTableStateManager().setTableState(tableName,
448         ZooKeeperProtos.Table.State.ENABLING);
449 
450     // Trigger immediate assignment of the regions in round-robin fashion
451     ModifyRegionUtils.assignRegions(assignmentManager, regions);
452 
453     // Enable table
454     assignmentManager.getTableStateManager()
455       .setTableState(tableName, ZooKeeperProtos.Table.State.ENABLED);
456   }
457 
458   /**
459    * Add the specified set of regions to the hbase:meta table.
460    */
461   protected static void addRegionsToMeta(final MasterProcedureEnv env,
462       final HTableDescriptor hTableDescriptor,
463       final List<HRegionInfo> regionInfos) throws IOException {
464     MetaTableAccessor.addRegionsToMeta(env.getMasterServices().getConnection(),
465       regionInfos, hTableDescriptor.getRegionReplication());
466   }
467 
468   protected static void updateTableDescCache(final MasterProcedureEnv env,
469       final TableName tableName) throws IOException {
470     env.getMasterServices().getTableDescriptors().get(tableName);
471   }
472 }