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.assignment;
019
020import java.util.Optional;
021import org.apache.hadoop.hbase.client.RegionInfo;
022import org.apache.hadoop.hbase.coprocessor.ObserverContext;
023import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
024import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
025import org.apache.hadoop.hbase.coprocessor.RegionObserver;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * This coprocessor is used to slow down opening of the replica regions.
031 */
032public class RegionServerHostingReplicaSlowOpenCoprocessor
033  implements RegionCoprocessor, RegionObserver {
034
035  private static final Logger LOG =
036    LoggerFactory.getLogger(RegionServerHostingReplicaSlowOpenCoprocessor.class);
037
038  static volatile boolean slowDownReplicaOpen = false;
039
040  @Override
041  public Optional<RegionObserver> getRegionObserver() {
042    return Optional.of(this);
043  }
044
045  @Override
046  public void preOpen(ObserverContext<? extends RegionCoprocessorEnvironment> c) {
047    int replicaId = c.getEnvironment().getRegion().getRegionInfo().getReplicaId();
048    if (replicaId != RegionInfo.DEFAULT_REPLICA_ID) {
049      while (slowDownReplicaOpen) {
050        LOG.info("Slow down replica region open a bit");
051        try {
052          Thread.sleep(250);
053        } catch (InterruptedException ignored) {
054          return;
055        }
056      }
057    }
058  }
059}