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.namequeues;
019
020import org.apache.hadoop.hbase.ScheduledChore;
021import org.apache.hadoop.hbase.Stoppable;
022import org.apache.hadoop.hbase.client.Connection;
023import org.apache.yetus.audience.InterfaceAudience;
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * Chore to insert multiple accumulated slow/large logs to hbase:slowlog system table
029 */
030@InterfaceAudience.Private
031public class NamedQueueServiceChore extends ScheduledChore {
032
033  private static final Logger LOG = LoggerFactory.getLogger(NamedQueueServiceChore.class);
034  public static final String NAMED_QUEUE_CHORE_DURATION_KEY =
035    "hbase.regionserver.named.queue.chore.duration";
036  // 10 mins default.
037  public static final int NAMED_QUEUE_CHORE_DURATION_DEFAULT = 10 * 60 * 1000;
038
039  private final NamedQueueRecorder namedQueueRecorder;
040  private final Connection connection;
041
042  /**
043   * Chore Constructor
044   * @param stopper            The stopper - When {@link Stoppable#isStopped()} is true, this chore
045   *                           will cancel and cleanup
046   * @param period             Period in millis with which this Chore repeats execution when
047   *                           scheduled
048   * @param namedQueueRecorder {@link NamedQueueRecorder} instance
049   */
050  public NamedQueueServiceChore(final Stoppable stopper, final int period,
051    final NamedQueueRecorder namedQueueRecorder, Connection connection) {
052    super("NamedQueueServiceChore", stopper, period);
053    this.namedQueueRecorder = namedQueueRecorder;
054    this.connection = connection;
055  }
056
057  @Override
058  protected void chore() {
059    for (NamedQueuePayload.NamedQueueEvent event : NamedQueuePayload.NamedQueueEvent.values()) {
060      if (LOG.isDebugEnabled()) {
061        LOG.debug(String.format("Starting chore for event %s", event.name()));
062      }
063      namedQueueRecorder.persistAll(event, connection);
064      if (LOG.isDebugEnabled()) {
065        LOG.debug(String.format("Stopping chore for event %s", event.name()));
066      }
067    }
068  }
069}