All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.cloud.autoscaling.sim.LiveNodesSet Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.solr.cloud.autoscaling.sim;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.solr.common.cloud.LiveNodesListener;

/**
 * This class represents a set of live nodes and allows adding listeners to track their state.
 */
public class LiveNodesSet implements Iterable {

  private final Set set = ConcurrentHashMap.newKeySet();
  private final Set listeners = ConcurrentHashMap.newKeySet();

  public Set get() {
    return Collections.unmodifiableSet(set);
  }

  public int size() {
    return set.size();
  }

  public void registerLiveNodesListener(LiveNodesListener listener) {
    listeners.add(listener);
  }

  public void removeLiveNodesListener(LiveNodesListener listener) {
    listeners.remove(listener);
  }
  
  public void removeAllLiveNodesListeners() {
    listeners.clear();
  }

  private void fireListeners(SortedSet oldNodes, SortedSet newNodes) {
    for (LiveNodesListener listener : listeners) {
      listener.onChange(oldNodes, newNodes);
    }
  }

  public boolean isEmpty() {
    return set.isEmpty();
  }

  public boolean contains(String id) {
    return set.contains(id);
  }

  public synchronized boolean add(String id) {
    if (set.contains(id)) {
      return false;
    }
    TreeSet oldNodes = new TreeSet<>(set);
    set.add(id);
    TreeSet newNodes = new TreeSet<>(set);
    fireListeners(oldNodes, newNodes);
    return true;
  }

  public synchronized boolean addAll(Collection nodes) {
    TreeSet oldNodes = new TreeSet<>(set);
    boolean changed = set.addAll(nodes);
    TreeSet newNodes = new TreeSet<>(set);
    if (changed) {
      fireListeners(oldNodes, newNodes);
    }
    return changed;
  }

  public synchronized boolean remove(String id) {
    if (!set.contains(id)) {
      return false;
    }
    TreeSet oldNodes = new TreeSet<>(set);
    set.remove(id);
    TreeSet newNodes = new TreeSet<>(set);
    fireListeners(oldNodes, newNodes);
    return true;
  }

  public synchronized void clear() {
    TreeSet oldNodes = new TreeSet<>(set);
    set.clear();
    fireListeners(oldNodes, Collections.emptySortedSet());
  }

  @Override
  public Iterator iterator() {
    return set.iterator();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy