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.waleventtracker;
019
020import static org.apache.hadoop.hbase.HConstants.NO_NONCE;
021import static org.apache.hadoop.hbase.namequeues.WALEventTrackerTableAccessor.WAL_EVENT_TRACKER_TABLE_NAME_STR;
022
023import java.io.IOException;
024import java.util.concurrent.TimeUnit;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
029import org.apache.hadoop.hbase.master.MasterServices;
030import org.apache.hadoop.hbase.namequeues.WALEventTrackerTableAccessor;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.apache.yetus.audience.InterfaceAudience;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * WALEventTracker Table creation to be used by HMaster
038 */
039@InterfaceAudience.Private
040public final class WALEventTrackerTableCreator {
041  private static final Logger LOG = LoggerFactory.getLogger(WALEventTrackerTableCreator.class);
042
043  public static final String WAL_EVENT_TRACKER_ENABLED_KEY =
044    "hbase.regionserver.wal.event.tracker.enabled";
045  public static final boolean WAL_EVENT_TRACKER_ENABLED_DEFAULT = false;
046
047  /** The walEventTracker info family as a string */
048  private static final String WAL_EVENT_TRACKER_INFO_FAMILY_STR = "info";
049
050  /** The walEventTracker info family in array of bytes */
051  public static final byte[] WAL_EVENT_TRACKER_INFO_FAMILY =
052    Bytes.toBytes(WAL_EVENT_TRACKER_INFO_FAMILY_STR);
053
054  private static final long TTL = TimeUnit.DAYS.toSeconds(365); // 1 year in seconds
055
056  private static final TableDescriptorBuilder TABLE_DESCRIPTOR_BUILDER = TableDescriptorBuilder
057    .newBuilder(WALEventTrackerTableAccessor.WAL_EVENT_TRACKER_TABLE_NAME).setRegionReplication(1)
058    .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(WAL_EVENT_TRACKER_INFO_FAMILY)
059      .setScope(HConstants.REPLICATION_SCOPE_LOCAL).setBlockCacheEnabled(false).setMaxVersions(1)
060      .setTimeToLive((int) TTL).build());
061
062  /* Private default constructor */
063  private WALEventTrackerTableCreator() {
064  }
065
066  /*
067   * We will create this table only if hbase.regionserver.wal.event.tracker.enabled is enabled and
068   * table doesn't exists already.
069   */
070  public static void createIfNeededAndNotExists(Configuration conf, MasterServices masterServices)
071    throws IOException {
072    boolean walEventTrackerEnabled =
073      conf.getBoolean(WAL_EVENT_TRACKER_ENABLED_KEY, WAL_EVENT_TRACKER_ENABLED_DEFAULT);
074    if (!walEventTrackerEnabled) {
075      LOG.info("wal event tracker requests logging to table " + WAL_EVENT_TRACKER_TABLE_NAME_STR
076        + " is disabled. Quitting.");
077      return;
078    }
079    if (
080      !masterServices.getTableDescriptors()
081        .exists(WALEventTrackerTableAccessor.WAL_EVENT_TRACKER_TABLE_NAME)
082    ) {
083      LOG.info(WAL_EVENT_TRACKER_TABLE_NAME_STR + " table not found. Creating.");
084      masterServices.createTable(TABLE_DESCRIPTOR_BUILDER.build(), null, 0L, NO_NONCE);
085    }
086  }
087}