View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.master.handler;
20  
21  import java.io.IOException;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.TableDescriptor;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.HRegionInfo;
33  import org.apache.hadoop.hbase.HTableDescriptor;
34  import org.apache.hadoop.hbase.Server;
35  import org.apache.hadoop.hbase.MetaTableAccessor;
36  import org.apache.hadoop.hbase.client.HTable;
37  import org.apache.hadoop.hbase.client.Result;
38  import org.apache.hadoop.hbase.client.ResultScanner;
39  import org.apache.hadoop.hbase.client.Scan;
40  import org.apache.hadoop.hbase.client.Table;
41  import org.apache.hadoop.hbase.client.TableState;
42  import org.apache.hadoop.hbase.executor.EventType;
43  import org.apache.hadoop.hbase.master.HMaster;
44  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
45  import org.apache.hadoop.hbase.master.MasterFileSystem;
46  import org.apache.hadoop.hbase.master.MasterServices;
47  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
48  import org.apache.hadoop.hbase.util.Bytes;
49  
50  @InterfaceAudience.Private
51  public class ModifyTableHandler extends TableEventHandler {
52    private static final Log LOG = LogFactory.getLog(ModifyTableHandler.class);
53  
54    private final HTableDescriptor htd;
55  
56    public ModifyTableHandler(final TableName tableName,
57        final HTableDescriptor htd, final Server server,
58        final MasterServices masterServices) {
59      super(EventType.C_M_MODIFY_TABLE, tableName, server, masterServices);
60      // This is the new schema we are going to write out as this modification.
61      this.htd = htd;
62    }
63  
64    @Override
65    protected void prepareWithTableLock() throws IOException {
66      super.prepareWithTableLock();
67      // Check operation is possible on the table in its current state
68      // Also checks whether the table exists
69      if (masterServices.getAssignmentManager().getTableStateManager()
70          .isTableState(this.htd.getTableName(), TableState.State.ENABLED)
71          && this.htd.getRegionReplication() != getTableDescriptor()
72          .getHTableDescriptor().getRegionReplication()) {
73        throw new IOException("REGION_REPLICATION change is not supported for enabled tables");
74      }
75    }
76  
77    @Override
78    protected void handleTableOperation(List<HRegionInfo> hris)
79    throws IOException {
80      MasterCoprocessorHost cpHost = ((HMaster) this.server).getMasterCoprocessorHost();
81      if (cpHost != null) {
82        cpHost.preModifyTableHandler(this.tableName, this.htd);
83      }
84      // Update descriptor
85      HTableDescriptor oldDescriptor =
86          this.masterServices.getTableDescriptors().get(this.tableName);
87      this.masterServices.getTableDescriptors().add(htd);
88      deleteFamilyFromFS(hris, oldDescriptor.getFamiliesKeys());
89      removeReplicaColumnsIfNeeded(
90          this.htd.getRegionReplication(),
91          oldDescriptor.getRegionReplication(),
92          this.htd.getTableName());
93      if (cpHost != null) {
94        cpHost.postModifyTableHandler(this.tableName, this.htd);
95      }
96    }
97  
98    private void removeReplicaColumnsIfNeeded(int newReplicaCount, int oldReplicaCount,
99        TableName table) throws IOException {
100     if (newReplicaCount >= oldReplicaCount) return;
101     Set<byte[]> tableRows = new HashSet<byte[]>();
102     Scan scan = MetaTableAccessor.getScanForTableName(table);
103     scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
104     Table htable = null;
105     try {
106       htable = new HTable(masterServices.getConfiguration(), TableName.META_TABLE_NAME);
107       ResultScanner resScanner = htable.getScanner(scan);
108       for (Result result : resScanner) {
109         tableRows.add(result.getRow());
110       }
111       MetaTableAccessor.removeRegionReplicasFromMeta(tableRows, newReplicaCount,
112           oldReplicaCount - newReplicaCount, masterServices.getShortCircuitConnection());
113     } finally {
114       if (htable != null) {
115         htable.close();
116       }
117     }
118   }
119 
120   /**
121    * Removes from hdfs the families that are not longer present in the new table descriptor.
122    */
123   private void deleteFamilyFromFS(final List<HRegionInfo> hris, final Set<byte[]> oldFamilies) {
124     try {
125       Set<byte[]> newFamilies = this.htd.getFamiliesKeys();
126       MasterFileSystem mfs = this.masterServices.getMasterFileSystem();
127       for (byte[] familyName: oldFamilies) {
128         if (!newFamilies.contains(familyName)) {
129           LOG.debug("Removing family=" + Bytes.toString(familyName) +
130                     " from table=" + this.tableName);
131           for (HRegionInfo hri: hris) {
132             // Delete the family directory in FS for all the regions one by one
133             mfs.deleteFamilyFromFS(hri, familyName);
134           }
135         }
136       }
137     } catch (IOException e) {
138       LOG.warn("Unable to remove on-disk directories for the removed families", e);
139     }
140   }
141 
142   @Override
143   public String toString() {
144     String name = "UnknownServerName";
145     if(server != null && server.getServerName() != null) {
146       name = server.getServerName().toString();
147     }
148     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
149       tableName;
150   }
151 }