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.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.hbase.HColumnDescriptor;
31  import org.apache.hadoop.hbase.HRegionInfo;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.InvalidFamilyOperationException;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.executor.EventType;
37  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
38  import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
39  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
40  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos;
41  import org.apache.hadoop.hbase.protobuf.generated.MasterProcedureProtos.AddColumnFamilyState;
42  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
43  import org.apache.hadoop.security.UserGroupInformation;
44  
45  /**
46   * The procedure to add a column family to an existing table.
47   */
48  @InterfaceAudience.Private
49  public class AddColumnFamilyProcedure
50      extends StateMachineProcedure<MasterProcedureEnv, AddColumnFamilyState>
51      implements TableProcedureInterface {
52    private static final Log LOG = LogFactory.getLog(AddColumnFamilyProcedure.class);
53  
54    private final AtomicBoolean aborted = new AtomicBoolean(false);
55  
56    private TableName tableName;
57    private HTableDescriptor unmodifiedHTableDescriptor;
58    private HColumnDescriptor cfDescriptor;
59    private UserGroupInformation user;
60  
61    private List<HRegionInfo> regionInfoList;
62    private Boolean traceEnabled;
63  
64    public AddColumnFamilyProcedure() {
65      this.unmodifiedHTableDescriptor = null;
66      this.regionInfoList = null;
67      this.traceEnabled = null;
68    }
69  
70    public AddColumnFamilyProcedure(final MasterProcedureEnv env, final TableName tableName,
71        final HColumnDescriptor cfDescriptor) {
72      this.tableName = tableName;
73      this.cfDescriptor = cfDescriptor;
74      this.user = env.getRequestUser().getUGI();
75      this.setOwner(this.user.getShortUserName());
76      this.unmodifiedHTableDescriptor = null;
77      this.regionInfoList = null;
78      this.traceEnabled = null;
79    }
80  
81    @Override
82    protected Flow executeFromState(final MasterProcedureEnv env, final AddColumnFamilyState state) {
83      if (isTraceEnabled()) {
84        LOG.trace(this + " execute state=" + state);
85      }
86  
87      try {
88        switch (state) {
89        case ADD_COLUMN_FAMILY_PREPARE:
90          prepareAdd(env);
91          setNextState(AddColumnFamilyState.ADD_COLUMN_FAMILY_PRE_OPERATION);
92          break;
93        case ADD_COLUMN_FAMILY_PRE_OPERATION:
94          preAdd(env, state);
95          setNextState(AddColumnFamilyState.ADD_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR);
96          break;
97        case ADD_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR:
98          updateTableDescriptor(env);
99          setNextState(AddColumnFamilyState.ADD_COLUMN_FAMILY_POST_OPERATION);
100         break;
101       case ADD_COLUMN_FAMILY_POST_OPERATION:
102         postAdd(env, state);
103         setNextState(AddColumnFamilyState.ADD_COLUMN_FAMILY_REOPEN_ALL_REGIONS);
104         break;
105       case ADD_COLUMN_FAMILY_REOPEN_ALL_REGIONS:
106         reOpenAllRegionsIfTableIsOnline(env);
107         return Flow.NO_MORE_STATE;
108       default:
109         throw new UnsupportedOperationException(this + " unhandled state=" + state);
110       }
111     } catch (InterruptedException|IOException e) {
112       LOG.warn("Error trying to add the column family" + getColumnFamilyName() + " to the table "
113           + tableName + " (in state=" + state + ")", e);
114 
115       setFailure("master-add-columnfamily", e);
116     }
117     return Flow.HAS_MORE_STATE;
118   }
119 
120   @Override
121   protected void rollbackState(final MasterProcedureEnv env, final AddColumnFamilyState state)
122       throws IOException {
123     if (isTraceEnabled()) {
124       LOG.trace(this + " rollback state=" + state);
125     }
126     try {
127       switch (state) {
128       case ADD_COLUMN_FAMILY_REOPEN_ALL_REGIONS:
129         break; // Nothing to undo.
130       case ADD_COLUMN_FAMILY_POST_OPERATION:
131         // TODO-MAYBE: call the coprocessor event to undo?
132         break;
133       case ADD_COLUMN_FAMILY_UPDATE_TABLE_DESCRIPTOR:
134         restoreTableDescriptor(env);
135         break;
136       case ADD_COLUMN_FAMILY_PRE_OPERATION:
137         // TODO-MAYBE: call the coprocessor event to undo?
138         break;
139       case ADD_COLUMN_FAMILY_PREPARE:
140         break; // nothing to do
141       default:
142         throw new UnsupportedOperationException(this + " unhandled state=" + state);
143       }
144     } catch (IOException e) {
145       // This will be retried. Unless there is a bug in the code,
146       // this should be just a "temporary error" (e.g. network down)
147       LOG.warn("Failed rollback attempt step " + state + " for adding the column family"
148           + getColumnFamilyName() + " to the table " + tableName, e);
149       throw e;
150     }
151   }
152 
153   @Override
154   protected AddColumnFamilyState getState(final int stateId) {
155     return AddColumnFamilyState.valueOf(stateId);
156   }
157 
158   @Override
159   protected int getStateId(final AddColumnFamilyState state) {
160     return state.getNumber();
161   }
162 
163   @Override
164   protected AddColumnFamilyState getInitialState() {
165     return AddColumnFamilyState.ADD_COLUMN_FAMILY_PREPARE;
166   }
167 
168   @Override
169   protected void setNextState(AddColumnFamilyState state) {
170     if (aborted.get()) {
171       setAbortFailure("add-columnfamily", "abort requested");
172     } else {
173       super.setNextState(state);
174     }
175   }
176 
177   @Override
178   public boolean abort(final MasterProcedureEnv env) {
179     aborted.set(true);
180     return true;
181   }
182 
183   @Override
184   protected boolean acquireLock(final MasterProcedureEnv env) {
185     if (!env.isInitialized()) return false;
186     return env.getProcedureQueue().tryAcquireTableWrite(
187       tableName,
188       EventType.C_M_ADD_FAMILY.toString());
189   }
190 
191   @Override
192   protected void releaseLock(final MasterProcedureEnv env) {
193     env.getProcedureQueue().releaseTableWrite(tableName);
194   }
195 
196   @Override
197   public void serializeStateData(final OutputStream stream) throws IOException {
198     super.serializeStateData(stream);
199 
200     MasterProcedureProtos.AddColumnFamilyStateData.Builder addCFMsg =
201         MasterProcedureProtos.AddColumnFamilyStateData.newBuilder()
202             .setUserInfo(MasterProcedureUtil.toProtoUserInfo(user))
203             .setTableName(ProtobufUtil.toProtoTableName(tableName))
204             .setColumnfamilySchema(cfDescriptor.convert());
205     if (unmodifiedHTableDescriptor != null) {
206       addCFMsg.setUnmodifiedTableSchema(unmodifiedHTableDescriptor.convert());
207     }
208 
209     addCFMsg.build().writeDelimitedTo(stream);
210   }
211 
212   @Override
213   public void deserializeStateData(final InputStream stream) throws IOException {
214     super.deserializeStateData(stream);
215 
216     MasterProcedureProtos.AddColumnFamilyStateData addCFMsg =
217         MasterProcedureProtos.AddColumnFamilyStateData.parseDelimitedFrom(stream);
218     user = MasterProcedureUtil.toUserInfo(addCFMsg.getUserInfo());
219     tableName = ProtobufUtil.toTableName(addCFMsg.getTableName());
220     cfDescriptor = HColumnDescriptor.convert(addCFMsg.getColumnfamilySchema());
221     if (addCFMsg.hasUnmodifiedTableSchema()) {
222       unmodifiedHTableDescriptor = HTableDescriptor.convert(addCFMsg.getUnmodifiedTableSchema());
223     }
224   }
225 
226   @Override
227   public void toStringClassDetails(StringBuilder sb) {
228     sb.append(getClass().getSimpleName());
229     sb.append(" (table=");
230     sb.append(tableName);
231     sb.append(", columnfamily=");
232     if (cfDescriptor != null) {
233       sb.append(getColumnFamilyName());
234     } else {
235       sb.append("Unknown");
236     }
237     sb.append(")");
238   }
239 
240   @Override
241   public TableName getTableName() {
242     return tableName;
243   }
244 
245   @Override
246   public TableOperationType getTableOperationType() {
247     return TableOperationType.EDIT;
248   }
249 
250   /**
251    * Action before any real action of adding column family.
252    * @param env MasterProcedureEnv
253    * @throws IOException
254    */
255   private void prepareAdd(final MasterProcedureEnv env) throws IOException {
256     // Checks whether the table is allowed to be modified.
257     MasterDDLOperationHelper.checkTableModifiable(env, tableName);
258 
259     // In order to update the descriptor, we need to retrieve the old descriptor for comparison.
260     unmodifiedHTableDescriptor = env.getMasterServices().getTableDescriptors().get(tableName);
261     if (unmodifiedHTableDescriptor == null) {
262       throw new IOException("HTableDescriptor missing for " + tableName);
263     }
264     if (unmodifiedHTableDescriptor.hasFamily(cfDescriptor.getName())) {
265       throw new InvalidFamilyOperationException("Column family '" + getColumnFamilyName()
266           + "' in table '" + tableName + "' already exists so cannot be added");
267     }
268   }
269 
270   /**
271    * Action before adding column family.
272    * @param env MasterProcedureEnv
273    * @param state the procedure state
274    * @throws IOException
275    * @throws InterruptedException
276    */
277   private void preAdd(final MasterProcedureEnv env, final AddColumnFamilyState state)
278       throws IOException, InterruptedException {
279     runCoprocessorAction(env, state);
280   }
281 
282   /**
283    * Add the column family to the file system
284    */
285   private void updateTableDescriptor(final MasterProcedureEnv env) throws IOException {
286     // Update table descriptor
287     LOG.info("AddColumn. Table = " + tableName + " HCD = " + cfDescriptor.toString());
288 
289     HTableDescriptor htd = env.getMasterServices().getTableDescriptors().get(tableName);
290 
291     if (htd.hasFamily(cfDescriptor.getName())) {
292       // It is possible to reach this situation, as we could already add the column family
293       // to table descriptor, but the master failover happens before we complete this state.
294       // We should be able to handle running this function multiple times without causing problem.
295       return;
296     }
297 
298     htd.addFamily(cfDescriptor);
299     env.getMasterServices().getTableDescriptors().add(htd);
300   }
301 
302   /**
303    * Restore the table descriptor back to pre-add
304    * @param env MasterProcedureEnv
305    * @throws IOException
306    **/
307   private void restoreTableDescriptor(final MasterProcedureEnv env) throws IOException {
308     HTableDescriptor htd = env.getMasterServices().getTableDescriptors().get(tableName);
309     if (htd.hasFamily(cfDescriptor.getName())) {
310       // Remove the column family from file system and update the table descriptor to
311       // the before-add-column-family-state
312       MasterDDLOperationHelper.deleteColumnFamilyFromFileSystem(env, tableName,
313         getRegionInfoList(env), cfDescriptor.getName());
314 
315       env.getMasterServices().getTableDescriptors().add(unmodifiedHTableDescriptor);
316 
317       // Make sure regions are opened after table descriptor is updated.
318       reOpenAllRegionsIfTableIsOnline(env);
319     }
320   }
321 
322   /**
323    * Action after adding column family.
324    * @param env MasterProcedureEnv
325    * @param state the procedure state
326    * @throws IOException
327    * @throws InterruptedException
328    */
329   private void postAdd(final MasterProcedureEnv env, final AddColumnFamilyState state)
330       throws IOException, InterruptedException {
331     runCoprocessorAction(env, state);
332   }
333 
334   /**
335    * Last action from the procedure - executed when online schema change is supported.
336    * @param env MasterProcedureEnv
337    * @throws IOException
338    */
339   private void reOpenAllRegionsIfTableIsOnline(final MasterProcedureEnv env) throws IOException {
340     // This operation only run when the table is enabled.
341     if (!env.getMasterServices().getAssignmentManager().getTableStateManager()
342         .isTableState(getTableName(), ZooKeeperProtos.Table.State.ENABLED)) {
343       return;
344     }
345 
346     if (MasterDDLOperationHelper.reOpenAllRegions(env, getTableName(), getRegionInfoList(env))) {
347       LOG.info("Completed add column family operation on table " + getTableName());
348     } else {
349       LOG.warn("Error on reopening the regions on table " + getTableName());
350     }
351   }
352 
353   /**
354    * The procedure could be restarted from a different machine. If the variable is null, we need to
355    * retrieve it.
356    * @return traceEnabled
357    */
358   private Boolean isTraceEnabled() {
359     if (traceEnabled == null) {
360       traceEnabled = LOG.isTraceEnabled();
361     }
362     return traceEnabled;
363   }
364 
365   private String getColumnFamilyName() {
366     return cfDescriptor.getNameAsString();
367   }
368 
369   /**
370    * Coprocessor Action.
371    * @param env MasterProcedureEnv
372    * @param state the procedure state
373    * @throws IOException
374    * @throws InterruptedException
375    */
376   private void runCoprocessorAction(final MasterProcedureEnv env, final AddColumnFamilyState state)
377       throws IOException, InterruptedException {
378     final MasterCoprocessorHost cpHost = env.getMasterCoprocessorHost();
379     if (cpHost != null) {
380       user.doAs(new PrivilegedExceptionAction<Void>() {
381         @Override
382         public Void run() throws Exception {
383           switch (state) {
384           case ADD_COLUMN_FAMILY_PRE_OPERATION:
385             cpHost.preAddColumnHandler(tableName, cfDescriptor);
386             break;
387           case ADD_COLUMN_FAMILY_POST_OPERATION:
388             cpHost.postAddColumnHandler(tableName, cfDescriptor);
389             break;
390           default:
391             throw new UnsupportedOperationException(this + " unhandled state=" + state);
392           }
393           return null;
394         }
395       });
396     }
397   }
398 
399   private List<HRegionInfo> getRegionInfoList(final MasterProcedureEnv env) throws IOException {
400     if (regionInfoList == null) {
401       regionInfoList = ProcedureSyncWait.getRegionsFromMeta(env, getTableName());
402     }
403     return regionInfoList;
404   }
405 }