001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.master.slowlog;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.MetaTableAccessor;
026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
027import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
028import org.apache.hadoop.hbase.master.MasterServices;
029import org.apache.hadoop.hbase.slowlog.SlowLogTableAccessor;
030import org.apache.yetus.audience.InterfaceAudience;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034/**
035 * Slowlog Master services - Table creation to be used by HMaster
036 */
037@InterfaceAudience.Private
038public class SlowLogMasterService {
039
040  private static final Logger LOG = LoggerFactory.getLogger(SlowLogMasterService.class);
041
042  private final boolean slowlogTableEnabled;
043  private final MasterServices masterServices;
044
045  private static final TableDescriptorBuilder TABLE_DESCRIPTOR_BUILDER =
046    TableDescriptorBuilder.newBuilder(SlowLogTableAccessor.SLOW_LOG_TABLE_NAME)
047      .setRegionReplication(1)
048      .setColumnFamily(
049        ColumnFamilyDescriptorBuilder.newBuilder(HConstants.SLOWLOG_INFO_FAMILY)
050          .setScope(HConstants.REPLICATION_SCOPE_LOCAL)
051          .setBlockCacheEnabled(false)
052          .setMaxVersions(1).build());
053
054  public SlowLogMasterService(final Configuration configuration,
055      final MasterServices masterServices) {
056    slowlogTableEnabled = configuration.getBoolean(HConstants.SLOW_LOG_SYS_TABLE_ENABLED_KEY,
057      HConstants.DEFAULT_SLOW_LOG_SYS_TABLE_ENABLED_KEY);
058    this.masterServices = masterServices;
059  }
060
061  public void init() throws IOException {
062    if (!slowlogTableEnabled) {
063      LOG.info("Slow/Large requests logging to system table hbase:slowlog is disabled. Quitting.");
064      return;
065    }
066    if (!MetaTableAccessor.tableExists(masterServices.getConnection(),
067        SlowLogTableAccessor.SLOW_LOG_TABLE_NAME)) {
068      LOG.info("slowlog table not found. Creating.");
069      this.masterServices.createSystemTable(TABLE_DESCRIPTOR_BUILDER.build());
070    }
071  }
072
073}