/**
	 * Indices deleted.
	 *
	 * @return the list
	 */
	public List<String> indicesDeleted() {
		if (previousState == null) {
			return ImmutableList.of();
		}
		if (!metaDataChanged()) {
			return ImmutableList.of();
		}
		List<String> deleted = null;
		for (String index : previousState.metaData().indices().keySet()) {
			if (!state.metaData().hasIndex(index)) {
				if (deleted == null) {
					deleted = Lists.newArrayList();
				}
				deleted.add(index);
			}
		}
		return deleted == null ? ImmutableList.<String> of() : deleted;
	}
	/**
	 * Indices created.
	 *
	 * @return the list
	 */
	public List<String> indicesCreated() {
		if (previousState == null) {
			return Lists.newArrayList(state.metaData().indices().keySet());
		}
		if (!metaDataChanged()) {
			return ImmutableList.of();
		}
		List<String> created = null;
		for (String index : state.metaData().indices().keySet()) {
			if (!previousState.metaData().hasIndex(index)) {
				if (created == null) {
					created = Lists.newArrayList();
				}
				created.add(index);
			}
		}
		return created == null ? ImmutableList.<String> of() : created;
	}
Exemplo n.º 3
0
  protected List<String> buildShardList(CloudSolrClient cloudSolrServer) {
    ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader();

    ClusterState clusterState = zkStateReader.getClusterState();

    String[] collections = null;
    if (clusterState.hasCollection(collection)) {
      collections = new String[] {collection};
    } else {
      // might be a collection alias?
      Aliases aliases = zkStateReader.getAliases();
      String aliasedCollections = aliases.getCollectionAlias(collection);
      if (aliasedCollections == null)
        throw new IllegalArgumentException("Collection " + collection + " not found!");
      collections = aliasedCollections.split(",");
    }

    Set<String> liveNodes = clusterState.getLiveNodes();
    Random random = new Random(5150);

    List<String> shards = new ArrayList<String>();
    for (String coll : collections) {
      for (Slice slice : clusterState.getSlices(coll)) {
        List<String> replicas = new ArrayList<String>();
        for (Replica r : slice.getReplicas()) {
          ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(r);
          if (liveNodes.contains(replicaCoreProps.getNodeName()))
            replicas.add(replicaCoreProps.getCoreUrl());
        }
        int numReplicas = replicas.size();
        if (numReplicas == 0)
          throw new IllegalStateException(
              "Shard " + slice.getName() + " does not have any active replicas!");

        String replicaUrl =
            (numReplicas == 1) ? replicas.get(0) : replicas.get(random.nextInt(replicas.size()));
        shards.add(replicaUrl);
      }
    }
    return shards;
  }
	/**
	 * Index meta data changed.
	 *
	 * @param current the current
	 * @return true, if successful
	 */
	public boolean indexMetaDataChanged(IndexMetaData current) {
		MetaData previousMetaData = previousState.metaData();
		if (previousMetaData == null) {
			return true;
		}
		IndexMetaData previousIndexMetaData = previousMetaData.index(current.index());

		if (previousIndexMetaData == current) {
			return false;
		}
		return true;
	}
	/**
	 * Index routing table changed.
	 *
	 * @param index the index
	 * @return true, if successful
	 */
	public boolean indexRoutingTableChanged(String index) {
		if (!state.routingTable().hasIndex(index) && !previousState.routingTable().hasIndex(index)) {
			return false;
		}
		if (state.routingTable().hasIndex(index) && previousState.routingTable().hasIndex(index)) {
			return state.routingTable().index(index) != previousState.routingTable().index(index);
		}
		return true;
	}
	/**
	 * Routing table changed.
	 *
	 * @return true, if successful
	 */
	public boolean routingTableChanged() {
		return state.routingTable() != previousState.routingTable();
	}
	/**
	 * Instantiates a new cluster changed event.
	 *
	 * @param source the source
	 * @param state the state
	 * @param previousState the previous state
	 */
	public ClusterChangedEvent(String source, ClusterState state, ClusterState previousState) {
		this.source = source;
		this.state = state;
		this.previousState = previousState;
		this.nodesDelta = state.nodes().delta(previousState.nodes());
	}
	/**
	 * Local node master.
	 *
	 * @return true, if successful
	 */
	public boolean localNodeMaster() {
		return state.nodes().localNodeMaster();
	}
	/**
	 * Blocks changed.
	 *
	 * @return true, if successful
	 */
	public boolean blocksChanged() {
		return state.blocks() != previousState.blocks();
	}
	/**
	 * Meta data changed.
	 *
	 * @return true, if successful
	 */
	public boolean metaDataChanged() {
		return state.metaData() != previousState.metaData();
	}