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

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

There is a newer version: 9.7.0
Show newest version
/*
 * 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.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.apache.solr.client.solrj.impl.ClusterStateProvider;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.util.Utils;
import org.noggit.CharArr;
import org.noggit.JSONWriter;

/**
 * Read-only snapshot of another {@link ClusterStateProvider}.
 */
public class SnapshotClusterStateProvider implements ClusterStateProvider {

  final Set liveNodes;
  final ClusterState clusterState;
  final Map clusterProperties;

  public SnapshotClusterStateProvider(ClusterStateProvider other) throws Exception {
    liveNodes = Collections.unmodifiableSet(new HashSet<>(other.getLiveNodes()));
    ClusterState otherState = other.getClusterState();
    clusterState = new ClusterState(otherState.getZNodeVersion(), liveNodes, otherState.getCollectionsMap());
    clusterProperties = new HashMap<>(other.getClusterProperties());
  }

  public SnapshotClusterStateProvider(Map snapshot) {
    Objects.requireNonNull(snapshot);
    liveNodes = Collections.unmodifiableSet(new HashSet<>((Collection)snapshot.getOrDefault("liveNodes", Collections.emptySet())));
    clusterProperties = (Map)snapshot.getOrDefault("clusterProperties", Collections.emptyMap());
    Map stateMap = new HashMap<>((Map)snapshot.getOrDefault("clusterState", Collections.emptyMap()));
    Number version = (Number)stateMap.remove("version");
    clusterState = ClusterState.load(version != null ? version.intValue() : null, stateMap, liveNodes, ZkStateReader.CLUSTER_STATE);
  }

  public Map getSnapshot() {
    Map snapshot = new HashMap<>();
    snapshot.put("liveNodes", liveNodes);
    if (clusterProperties != null) {
      snapshot.put("clusterProperties", clusterProperties);
    }
    Map stateMap = new HashMap<>();
    snapshot.put("clusterState", stateMap);
    stateMap.put("version", clusterState.getZNodeVersion());
    clusterState.forEachCollection(coll -> {
      CharArr out = new CharArr();
      JSONWriter writer = new JSONWriter(out, 2);
      coll.write(writer);
      String json = out.toString();
      try {
        stateMap.put(coll.getName(), Utils.fromJSON(json.getBytes("UTF-8")));
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("should not happen!", e);
      }
    });
    return snapshot;
  }

  @Override
  public ClusterState.CollectionRef getState(String collection) {
    return clusterState.getCollectionRef(collection);
  }

  @Override
  public Set getLiveNodes() {
    return liveNodes;
  }

  @Override
  public List resolveAlias(String alias) {
    throw new UnsupportedOperationException("resolveAlias");
  }

  @Override
  public Map getAliasProperties(String alias) {
    throw new UnsupportedOperationException("getAliasProperties");
  }

  @Override
  public ClusterState getClusterState() throws IOException {
    return clusterState;
  }

  @Override
  public Map getClusterProperties() {
    return clusterProperties;
  }

  @Override
  public String getPolicyNameByCollection(String coll) {
    DocCollection collection = clusterState.getCollectionOrNull(coll);
    return collection == null ? null : (String)collection.getProperties().get("policy");
  }

  @Override
  public void connect() {

  }

  @Override
  public void close() throws IOException {

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy