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.slowlog;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HConstants;
023import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
024import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
025import org.apache.hadoop.hbase.master.MasterServices;
026import org.apache.hadoop.hbase.slowlog.SlowLogTableAccessor;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031/**
032 * Slowlog Master services - Table creation to be used by HMaster
033 */
034@InterfaceAudience.Private
035public class SlowLogMasterService {
036
037  private static final Logger LOG = LoggerFactory.getLogger(SlowLogMasterService.class);
038
039  private final boolean slowlogTableEnabled;
040  private final MasterServices masterServices;
041
042  private static final TableDescriptorBuilder TABLE_DESCRIPTOR_BUILDER = TableDescriptorBuilder
043    .newBuilder(SlowLogTableAccessor.SLOW_LOG_TABLE_NAME).setRegionReplication(1)
044    .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(HConstants.SLOWLOG_INFO_FAMILY)
045      .setScope(HConstants.REPLICATION_SCOPE_LOCAL).setBlockCacheEnabled(false).setMaxVersions(1)
046      .build());
047
048  public SlowLogMasterService(final Configuration configuration,
049    final MasterServices masterServices) {
050    slowlogTableEnabled = configuration.getBoolean(HConstants.SLOW_LOG_SYS_TABLE_ENABLED_KEY,
051      HConstants.DEFAULT_SLOW_LOG_SYS_TABLE_ENABLED_KEY);
052    this.masterServices = masterServices;
053  }
054
055  public void init() throws IOException {
056    if (!slowlogTableEnabled) {
057      LOG.info("Slow/Large requests logging to system table hbase:slowlog is disabled. Quitting.");
058      return;
059    }
060    if (!masterServices.getTableDescriptors().exists(SlowLogTableAccessor.SLOW_LOG_TABLE_NAME)) {
061      LOG.info("slowlog table not found. Creating.");
062      this.masterServices.createSystemTable(TABLE_DESCRIPTOR_BUILDER.build());
063    }
064  }
065
066}