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.hbase.HConstants;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.MetaTableAccessor;
32  import org.apache.hadoop.hbase.Server;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.classification.InterfaceAudience;
35  import org.apache.hadoop.hbase.client.Connection;
36  import org.apache.hadoop.hbase.client.Result;
37  import org.apache.hadoop.hbase.client.ResultScanner;
38  import org.apache.hadoop.hbase.client.Scan;
39  import org.apache.hadoop.hbase.client.Table;
40  import org.apache.hadoop.hbase.client.TableState;
41  import org.apache.hadoop.hbase.executor.EventType;
42  import org.apache.hadoop.hbase.master.HMaster;
43  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
44  import org.apache.hadoop.hbase.master.MasterFileSystem;
45  import org.apache.hadoop.hbase.master.MasterServices;
46  import org.apache.hadoop.hbase.util.Bytes;
47  import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil;
48  
49  @InterfaceAudience.Private
50  public class ModifyTableHandler extends TableEventHandler {
51    private static final Log LOG = LogFactory.getLog(ModifyTableHandler.class);
52  
53    private final HTableDescriptor htd;
54  
55    public ModifyTableHandler(final TableName tableName,
56        final HTableDescriptor htd, final Server server,
57        final MasterServices masterServices) {
58      super(EventType.C_M_MODIFY_TABLE, tableName, server, masterServices);
59      // This is the new schema we are going to write out as this modification.
60      this.htd = htd;
61    }
62  
63    @Override
64    protected void prepareWithTableLock() throws IOException {
65      super.prepareWithTableLock();
66      // Check operation is possible on the table in its current state
67      // Also checks whether the table exists
68      if (masterServices.getAssignmentManager().getTableStateManager()
69          .isTableState(this.htd.getTableName(), TableState.State.ENABLED)
70          && this.htd.getRegionReplication() != getTableDescriptor()
71          .getHTableDescriptor().getRegionReplication()) {
72        throw new IOException("REGION_REPLICATION change is not supported for enabled tables");
73      }
74    }
75  
76    @Override
77    protected void handleTableOperation(List<HRegionInfo> hris)
78    throws IOException {
79      MasterCoprocessorHost cpHost = ((HMaster) this.server).getMasterCoprocessorHost();
80      if (cpHost != null) {
81        cpHost.preModifyTableHandler(this.tableName, this.htd);
82      }
83      // Update descriptor
84      HTableDescriptor oldDescriptor =
85          this.masterServices.getTableDescriptors().get(this.tableName);
86      this.masterServices.getTableDescriptors().add(htd);
87      deleteFamilyFromFS(hris, oldDescriptor.getFamiliesKeys());
88      removeReplicaColumnsIfNeeded(
89          this.htd.getRegionReplication(),
90          oldDescriptor.getRegionReplication(),
91          this.htd.getTableName());
92      // Setup replication for region replicas if needed
93      if (htd.getRegionReplication() > 1 && oldDescriptor.getRegionReplication() <= 1) {
94        ServerRegionReplicaUtil.setupRegionReplicaReplication(server.getConfiguration());
95      }
96      if (cpHost != null) {
97        cpHost.postModifyTableHandler(this.tableName, this.htd);
98      }
99    }
100 
101   private void removeReplicaColumnsIfNeeded(int newReplicaCount, int oldReplicaCount,
102       TableName table) throws IOException {
103     if (newReplicaCount >= oldReplicaCount) return;
104     Set<byte[]> tableRows = new HashSet<byte[]>();
105     Connection connection = this.masterServices.getConnection();
106     Scan scan = MetaTableAccessor.getScanForTableName(connection, table);
107     scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
108     try (Table metaTable = connection.getTable(TableName.META_TABLE_NAME)) {
109       ResultScanner resScanner = metaTable.getScanner(scan);
110       for (Result result : resScanner) {
111         tableRows.add(result.getRow());
112       }
113       MetaTableAccessor.removeRegionReplicasFromMeta(tableRows, newReplicaCount,
114         oldReplicaCount - newReplicaCount, masterServices.getConnection());
115     }
116   }
117 
118   /**
119    * Removes from hdfs the families that are not longer present in the new table descriptor.
120    */
121   private void deleteFamilyFromFS(final List<HRegionInfo> hris, final Set<byte[]> oldFamilies) {
122     try {
123       Set<byte[]> newFamilies = this.htd.getFamiliesKeys();
124       MasterFileSystem mfs = this.masterServices.getMasterFileSystem();
125       for (byte[] familyName: oldFamilies) {
126         if (!newFamilies.contains(familyName)) {
127           LOG.debug("Removing family=" + Bytes.toString(familyName) +
128                     " from table=" + this.tableName);
129           for (HRegionInfo hri: hris) {
130             // Delete the family directory in FS for all the regions one by one
131             mfs.deleteFamilyFromFS(hri, familyName);
132           }
133         }
134       }
135     } catch (IOException e) {
136       LOG.warn("Unable to remove on-disk directories for the removed families", e);
137     }
138   }
139 
140   @Override
141   public String toString() {
142     String name = "UnknownServerName";
143     if(server != null && server.getServerName() != null) {
144       name = server.getServerName().toString();
145     }
146     return getClass().getSimpleName() + "-" + name + "-" + getSeqid() + "-" +
147       tableName;
148   }
149 }