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 org.apache.yetus.audience.InterfaceAudience; 021 022/** 023 * A barrier to guard the execution of {@link ReplicationLogCleaner}. 024 * <p/> 025 * The reason why we introduce this class is because there could be race between 026 * {@link org.apache.hadoop.hbase.master.replication.AddPeerProcedure} and 027 * {@link ReplicationLogCleaner}. See HBASE-27214 for more details. 028 */ 029@InterfaceAudience.Private 030public class ReplicationLogCleanerBarrier { 031 032 private enum State { 033 // the cleaner is not running 034 NOT_RUNNING, 035 // the cleaner is running 036 RUNNING, 037 // the cleaner is disabled 038 DISABLED 039 } 040 041 private State state = State.NOT_RUNNING; 042 043 // we could have multiple AddPeerProcedure running at the same time, so here we need to do 044 // reference counting. 045 private int numberDisabled = 0; 046 047 public synchronized boolean start() { 048 if (state == State.NOT_RUNNING) { 049 state = State.RUNNING; 050 return true; 051 } 052 if (state == State.DISABLED) { 053 return false; 054 } 055 throw new IllegalStateException("Unexpected state " + state); 056 } 057 058 public synchronized void stop() { 059 if (state != State.RUNNING) { 060 throw new IllegalStateException("Unexpected state " + state); 061 } 062 state = State.NOT_RUNNING; 063 } 064 065 public synchronized boolean disable() { 066 if (state == State.RUNNING) { 067 return false; 068 } 069 if (state == State.NOT_RUNNING) { 070 state = State.DISABLED; 071 } 072 numberDisabled++; 073 return true; 074 } 075 076 public synchronized void enable() { 077 if (state != State.DISABLED) { 078 throw new IllegalStateException("Unexpected state " + state); 079 } 080 numberDisabled--; 081 if (numberDisabled == 0) { 082 state = State.NOT_RUNNING; 083 } 084 } 085}