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.procedure2.store;
019
020import java.util.concurrent.CopyOnWriteArrayList;
021import java.util.concurrent.atomic.AtomicBoolean;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Base class for {@link ProcedureStore}s.
026 */
027@InterfaceAudience.Private
028public abstract class ProcedureStoreBase implements ProcedureStore {
029  private final CopyOnWriteArrayList<ProcedureStoreListener> listeners = new CopyOnWriteArrayList<>();
030
031  private final AtomicBoolean running = new AtomicBoolean(false);
032
033  /**
034   * Change the state to 'isRunning',
035   * returns true if the store state was changed,
036   * false if the store was already in that state.
037   * @param isRunning the state to set.
038   * @return true if the store state was changed, otherwise false.
039   */
040  protected boolean setRunning(boolean isRunning) {
041    return running.getAndSet(isRunning) != isRunning;
042  }
043
044  @Override
045  public boolean isRunning() {
046    return running.get();
047  }
048
049  @Override
050  public void registerListener(ProcedureStoreListener listener) {
051    listeners.add(listener);
052  }
053
054  @Override
055  public boolean unregisterListener(ProcedureStoreListener listener) {
056    return listeners.remove(listener);
057  }
058
059  protected final void sendPostSyncSignal() {
060    listeners.forEach(ProcedureStoreListener::postSync);
061  }
062
063  protected final void sendAbortProcessSignal() {
064    listeners.forEach(ProcedureStoreListener::abortProcess);
065  }
066
067  protected final void sendForceUpdateSignal(long[] procIds) {
068    listeners.forEach(l -> l.forceUpdate(procIds));
069  }
070}