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 =
030          new CopyOnWriteArrayList<>();
031
032  private final AtomicBoolean running = new AtomicBoolean(false);
033
034  /**
035   * Change the state to 'isRunning',
036   * returns true if the store state was changed,
037   * false if the store was already in that state.
038   * @param isRunning the state to set.
039   * @return true if the store state was changed, otherwise false.
040   */
041  protected boolean setRunning(boolean isRunning) {
042    return running.getAndSet(isRunning) != isRunning;
043  }
044
045  @Override
046  public boolean isRunning() {
047    return running.get();
048  }
049
050  @Override
051  public void registerListener(ProcedureStoreListener listener) {
052    listeners.add(listener);
053  }
054
055  @Override
056  public boolean unregisterListener(ProcedureStoreListener listener) {
057    return listeners.remove(listener);
058  }
059
060  protected final void sendPostSyncSignal() {
061    listeners.forEach(ProcedureStoreListener::postSync);
062  }
063
064  protected final void sendAbortProcessSignal() {
065    listeners.forEach(ProcedureStoreListener::abortProcess);
066  }
067
068  protected final void sendForceUpdateSignal(long[] procIds) {
069    listeners.forEach(l -> l.forceUpdate(procIds));
070  }
071}