001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.concurrent.ConcurrentHashMap;
023import java.util.concurrent.ConcurrentMap;
024import java.util.stream.Collectors;
025import org.apache.commons.lang3.StringUtils;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellUtil;
028import org.apache.hadoop.hbase.DoNotRetryIOException;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.MetaTableAccessor;
031import org.apache.hadoop.hbase.NamespaceDescriptor;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.BufferedMutator;
034import org.apache.hadoop.hbase.client.Connection;
035import org.apache.hadoop.hbase.client.Delete;
036import org.apache.hadoop.hbase.client.Put;
037import org.apache.hadoop.hbase.client.Result;
038import org.apache.hadoop.hbase.client.ResultScanner;
039import org.apache.hadoop.hbase.client.Scan;
040import org.apache.hadoop.hbase.client.Table;
041import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
042import org.apache.hadoop.hbase.client.TableState;
043import org.apache.hadoop.hbase.constraint.ConstraintException;
044import org.apache.hadoop.hbase.master.procedure.DisableTableProcedure;
045import org.apache.hadoop.hbase.util.Bytes;
046import org.apache.yetus.audience.InterfaceAudience;
047
048import org.apache.hbase.thirdparty.com.google.protobuf.CodedInputStream;
049
050import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
051import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
052
053/**
054 * This is a helper class used internally to manage the namespace metadata that is stored in the ns
055 * family in meta table.
056 */
057@InterfaceAudience.Private
058public class TableNamespaceManager {
059
060  public static final String KEY_MAX_REGIONS = "hbase.namespace.quota.maxregions";
061  public static final String KEY_MAX_TABLES = "hbase.namespace.quota.maxtables";
062  static final String NS_INIT_TIMEOUT = "hbase.master.namespace.init.timeout";
063  static final int DEFAULT_NS_INIT_TIMEOUT = 300000;
064
065  private final ConcurrentMap<String, NamespaceDescriptor> cache = new ConcurrentHashMap<>();
066
067  private final MasterServices masterServices;
068
069  TableNamespaceManager(MasterServices masterServices) {
070    this.masterServices = masterServices;
071  }
072
073  private void migrateNamespaceTable() throws IOException {
074    try (Table nsTable = masterServices.getConnection().getTable(TableName.NAMESPACE_TABLE_NAME);
075      ResultScanner scanner = nsTable.getScanner(
076        new Scan().addFamily(TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES).readAllVersions());
077      BufferedMutator mutator =
078        masterServices.getConnection().getBufferedMutator(TableName.META_TABLE_NAME)) {
079      for (Result result;;) {
080        result = scanner.next();
081        if (result == null) {
082          break;
083        }
084        Put put = new Put(result.getRow());
085        result
086          .getColumnCells(TableDescriptorBuilder.NAMESPACE_FAMILY_INFO_BYTES,
087            TableDescriptorBuilder.NAMESPACE_COL_DESC_BYTES)
088          .forEach(c -> put.addColumn(HConstants.NAMESPACE_FAMILY,
089            HConstants.NAMESPACE_COL_DESC_QUALIFIER, c.getTimestamp(), CellUtil.cloneValue(c)));
090        mutator.mutate(put);
091      }
092    }
093    // schedule a disable procedure instead of block waiting here, as when disabling a table we will
094    // wait until master is initialized, but we are part of the initialization...
095    masterServices.getMasterProcedureExecutor().submitProcedure(
096      new DisableTableProcedure(masterServices.getMasterProcedureExecutor().getEnvironment(),
097        TableName.NAMESPACE_TABLE_NAME, false));
098  }
099
100  private void loadNamespaceIntoCache() throws IOException {
101    try (Table table = masterServices.getConnection().getTable(TableName.META_TABLE_NAME);
102      ResultScanner scanner = table.getScanner(HConstants.NAMESPACE_FAMILY)) {
103      for (Result result;;) {
104        result = scanner.next();
105        if (result == null) {
106          break;
107        }
108        Cell cell = result.getColumnLatestCell(HConstants.NAMESPACE_FAMILY,
109          HConstants.NAMESPACE_COL_DESC_QUALIFIER);
110        NamespaceDescriptor ns = ProtobufUtil
111          .toNamespaceDescriptor(HBaseProtos.NamespaceDescriptor.parseFrom(CodedInputStream
112            .newInstance(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength())));
113        cache.put(ns.getName(), ns);
114      }
115    }
116  }
117
118  public void start() throws IOException {
119    TableState nsTableState = MetaTableAccessor.getTableState(masterServices.getConnection(),
120      TableName.NAMESPACE_TABLE_NAME);
121    if (nsTableState != null && nsTableState.isEnabled()) {
122      migrateNamespaceTable();
123    }
124    loadNamespaceIntoCache();
125  }
126
127  /**
128   * check whether a namespace has already existed.
129   */
130  public boolean doesNamespaceExist(String namespaceName) throws IOException {
131    return cache.containsKey(namespaceName);
132  }
133
134  public NamespaceDescriptor get(String name) throws IOException {
135    return cache.get(name);
136  }
137
138  public void addOrUpdateNamespace(NamespaceDescriptor ns) throws IOException {
139    insertNamespaceToMeta(masterServices.getConnection(), ns);
140    cache.put(ns.getName(), ns);
141  }
142
143  public static void insertNamespaceToMeta(Connection conn, NamespaceDescriptor ns)
144    throws IOException {
145    byte[] row = Bytes.toBytes(ns.getName());
146    Put put = new Put(row, true).addColumn(HConstants.NAMESPACE_FAMILY,
147      HConstants.NAMESPACE_COL_DESC_QUALIFIER,
148      ProtobufUtil.toProtoNamespaceDescriptor(ns).toByteArray());
149    try (Table table = conn.getTable(TableName.META_TABLE_NAME)) {
150      table.put(put);
151    }
152  }
153
154  public void deleteNamespace(String namespaceName) throws IOException {
155    Delete d = new Delete(Bytes.toBytes(namespaceName));
156    try (Table table = masterServices.getConnection().getTable(TableName.META_TABLE_NAME)) {
157      table.delete(d);
158    }
159    cache.remove(namespaceName);
160  }
161
162  public List<NamespaceDescriptor> list() throws IOException {
163    return cache.values().stream().collect(Collectors.toList());
164  }
165
166  public void validateTableAndRegionCount(NamespaceDescriptor desc) throws IOException {
167    if (getMaxRegions(desc) <= 0) {
168      throw new ConstraintException(
169        "The max region quota for " + desc.getName() + " is less than or equal to zero.");
170    }
171    if (getMaxTables(desc) <= 0) {
172      throw new ConstraintException(
173        "The max tables quota for " + desc.getName() + " is less than or equal to zero.");
174    }
175  }
176
177  public static long getMaxTables(NamespaceDescriptor ns) throws IOException {
178    String value = ns.getConfigurationValue(KEY_MAX_TABLES);
179    long maxTables = 0;
180    if (StringUtils.isNotEmpty(value)) {
181      try {
182        maxTables = Long.parseLong(value);
183      } catch (NumberFormatException exp) {
184        throw new DoNotRetryIOException("NumberFormatException while getting max tables.", exp);
185      }
186    } else {
187      // The property is not set, so assume its the max long value.
188      maxTables = Long.MAX_VALUE;
189    }
190    return maxTables;
191  }
192
193  public static long getMaxRegions(NamespaceDescriptor ns) throws IOException {
194    String value = ns.getConfigurationValue(KEY_MAX_REGIONS);
195    long maxRegions = 0;
196    if (StringUtils.isNotEmpty(value)) {
197      try {
198        maxRegions = Long.parseLong(value);
199      } catch (NumberFormatException exp) {
200        throw new DoNotRetryIOException("NumberFormatException while getting max regions.", exp);
201      }
202    } else {
203      // The property is not set, so assume its the max long value.
204      maxRegions = Long.MAX_VALUE;
205    }
206    return maxRegions;
207  }
208}