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.replication.master;
019
020import static org.apache.hadoop.hbase.HConstants.NO_NONCE;
021
022import java.io.IOException;
023import java.util.concurrent.TimeUnit;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
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.util.Bytes;
031import org.apache.yetus.audience.InterfaceAudience;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035/**
036 * This will create {@link #REPLICATION_SINK_TRACKER_TABLE_NAME_STR} table if
037 * hbase.regionserver.replication.sink.tracker.enabled config key is enabled and table not created
038 **/
039@InterfaceAudience.Private
040public final class ReplicationSinkTrackerTableCreator {
041  private static final Logger LOG =
042    LoggerFactory.getLogger(ReplicationSinkTrackerTableCreator.class);
043  private static final long TTL = TimeUnit.DAYS.toSeconds(365); // 1 year in seconds
044
045  public static final byte[] RS_COLUMN = Bytes.toBytes("region_server_name");
046  public static final byte[] WAL_NAME_COLUMN = Bytes.toBytes("wal_name");
047  public static final byte[] TIMESTAMP_COLUMN = Bytes.toBytes("timestamp");
048  public static final byte[] OFFSET_COLUMN = Bytes.toBytes("offset");
049
050  /** Will create {@link #REPLICATION_SINK_TRACKER_TABLE_NAME_STR} table if this conf is enabled **/
051  public static final String REPLICATION_SINK_TRACKER_ENABLED_KEY =
052    "hbase.regionserver.replication.sink.tracker.enabled";
053  public static final boolean REPLICATION_SINK_TRACKER_ENABLED_DEFAULT = false;
054
055  /** The {@link #REPLICATION_SINK_TRACKER_TABLE_NAME_STR} info family as a string */
056  private static final String REPLICATION_SINK_TRACKER_INFO_FAMILY_STR = "info";
057
058  /** The {@link #REPLICATION_SINK_TRACKER_TABLE_NAME_STR} info family in array of bytes */
059  public static final byte[] REPLICATION_SINK_TRACKER_INFO_FAMILY =
060    Bytes.toBytes(REPLICATION_SINK_TRACKER_INFO_FAMILY_STR);
061
062  public static final String REPLICATION_SINK_TRACKER_TABLE_NAME_STR = "REPLICATION.SINK_TRACKER";
063
064  /* Private default constructor */
065  private ReplicationSinkTrackerTableCreator() {
066  }
067
068  /**
069   * {@link #REPLICATION_SINK_TRACKER_TABLE_NAME_STR} table name - can be enabled with config -
070   * hbase.regionserver.replication.sink.tracker.enabled
071   */
072  public static final TableName REPLICATION_SINK_TRACKER_TABLE_NAME =
073    TableName.valueOf(REPLICATION_SINK_TRACKER_TABLE_NAME_STR);
074
075  private static final TableDescriptorBuilder TABLE_DESCRIPTOR_BUILDER = TableDescriptorBuilder
076    .newBuilder(REPLICATION_SINK_TRACKER_TABLE_NAME).setRegionReplication(1)
077    .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(REPLICATION_SINK_TRACKER_INFO_FAMILY)
078      .setScope(HConstants.REPLICATION_SCOPE_LOCAL).setBlockCacheEnabled(false).setMaxVersions(1)
079      .setTimeToLive((int) TTL).build());
080
081  /*
082   * We will create this table only if hbase.regionserver.replication.sink.tracker.enabled is
083   * enabled and table doesn't exists already.
084   */
085  public static void createIfNeededAndNotExists(Configuration conf, MasterServices masterServices)
086    throws IOException {
087    boolean replicationSinkTrackerEnabled = conf.getBoolean(REPLICATION_SINK_TRACKER_ENABLED_KEY,
088      REPLICATION_SINK_TRACKER_ENABLED_DEFAULT);
089    if (!replicationSinkTrackerEnabled) {
090      LOG.info("replication sink tracker requests logging to table {} is disabled." + " Quitting.",
091        REPLICATION_SINK_TRACKER_TABLE_NAME_STR);
092      return;
093    }
094    if (!masterServices.getTableDescriptors().exists(REPLICATION_SINK_TRACKER_TABLE_NAME)) {
095      LOG.info("{} table not found. Creating.", REPLICATION_SINK_TRACKER_TABLE_NAME_STR);
096      masterServices.createTable(TABLE_DESCRIPTOR_BUILDER.build(), null, 0L, NO_NONCE);
097    }
098  }
099}