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

org.apache.solr.cloud.autoscaling.sim.SnapshotNodeStateProvider 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.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.apache.solr.client.solrj.cloud.NodeStateProvider;
import org.apache.solr.client.solrj.cloud.SolrCloudManager;
import org.apache.solr.client.solrj.cloud.autoscaling.AutoScalingConfig;
import org.apache.solr.client.solrj.cloud.autoscaling.ReplicaInfo;
import org.apache.solr.client.solrj.cloud.autoscaling.Variable;

/**
 * Read-only snapshot of another {@link NodeStateProvider}.
 */
public class SnapshotNodeStateProvider implements NodeStateProvider {
  private Map> nodeValues = new LinkedHashMap<>();
  private Map>>> replicaInfos = new LinkedHashMap<>();

  private static double GB = 1024.0d * 1024.0d * 1024.0d;

  /**
   * Populate this instance from another instance of {@link SolrCloudManager}.
   * @param other another instance
   * @param config optional {@link AutoScalingConfig}, which will be used to determine what node and
   *               replica tags to retrieve. If this is null then the other instance's config will be used.
   */
  public SnapshotNodeStateProvider(SolrCloudManager other, AutoScalingConfig config) throws Exception {
    if (config == null) {
      config = other.getDistribStateManager().getAutoScalingConfig();
    }
    Set nodeTags = new HashSet<>(SimUtils.COMMON_NODE_TAGS);
    nodeTags.addAll(config.getPolicy().getParams());
    Set replicaTags = new HashSet<>(SimUtils.COMMON_REPLICA_TAGS);
    replicaTags.addAll(config.getPolicy().getPerReplicaAttributes());
    for (String node : other.getClusterStateProvider().getLiveNodes()) {
      nodeValues.put(node, new LinkedHashMap<>(other.getNodeStateProvider().getNodeValues(node, nodeTags)));
      Map>> infos = other.getNodeStateProvider().getReplicaInfo(node, replicaTags);
      infos.forEach((collection, shards) -> {
        shards.forEach((shard, replicas) -> {
          replicas.forEach(r -> {
            List myReplicas = replicaInfos
                .computeIfAbsent(node, n -> new LinkedHashMap<>())
                .computeIfAbsent(collection, c -> new LinkedHashMap<>())
                .computeIfAbsent(shard, s -> new ArrayList<>());
            Map rMap = new LinkedHashMap<>();
            r.toMap(rMap);
            if (r.isLeader) { // ReplicaInfo.toMap doesn't write this!!!
              ((Map)rMap.values().iterator().next()).put("leader", "true");
            }
            ReplicaInfo ri = new ReplicaInfo(rMap);
            // put in "leader" again if present
            if (r.isLeader) {
              ri.getVariables().put("leader", "true");
            }
            // externally produced snapshots may not include the right units
            if (ri.getVariable(Variable.Type.CORE_IDX.metricsAttribute) == null) {
              if (ri.getVariable(Variable.Type.CORE_IDX.tagName) != null) {
                Number indexSizeGB = (Number) ri.getVariable(Variable.Type.CORE_IDX.tagName);
                ri.getVariables().put(Variable.Type.CORE_IDX.metricsAttribute, indexSizeGB.doubleValue() * GB);
              } else {
                throw new RuntimeException("Missing size information for replica: " + ri);
              }
            }
            myReplicas.add(ri);
          });
        });
      });
    }
  }

  /**
   * Populate this instance from a previously generated snapshot.
   * @param snapshot previous snapshot created using this class.
   */
  public SnapshotNodeStateProvider(Map snapshot) {
    Objects.requireNonNull(snapshot);
    nodeValues = (Map>)snapshot.getOrDefault("nodeValues", Collections.emptyMap());
    ((Map)snapshot.getOrDefault("replicaInfos", Collections.emptyMap())).forEach((node, v) -> {
      Map>> perNode = replicaInfos.computeIfAbsent(node, n -> new LinkedHashMap<>());
      ((Map)v).forEach((collection, shards) -> {
        Map> perColl = perNode.computeIfAbsent(collection, c -> new LinkedHashMap<>());
        ((Map)shards).forEach((shard, replicas) -> {
          List infos = perColl.computeIfAbsent(shard, s -> new ArrayList<>());
          ((List>)replicas).forEach(replicaMap -> {
            ReplicaInfo ri = new ReplicaInfo(new LinkedHashMap<>(replicaMap)); // constructor modifies this map
            if (ri.isLeader) {
              ri.getVariables().put("leader", "true");
            }
            // externally produced snapshots may not include the right units
            if (ri.getVariable(Variable.Type.CORE_IDX.metricsAttribute) == null) {
                if (ri.getVariable(Variable.Type.CORE_IDX.tagName) != null) {
                  Number indexSizeGB = (Number) ri.getVariable(Variable.Type.CORE_IDX.tagName);
                  ri.getVariables().put(Variable.Type.CORE_IDX.metricsAttribute, indexSizeGB.doubleValue() * GB);
                } else {
                  throw new RuntimeException("Missing size information for replica: " + ri);
              }
            }
            infos.add(ri);
          });
        });
      });
    });
  }

  /**
   * Create a snapshot of all node and replica tag values available from the original source, per the original
   * autoscaling configuration. Note:
   */
  public Map getSnapshot() {
    Map snapshot = new LinkedHashMap<>();
    snapshot.put("nodeValues", nodeValues);
    Map>>>> replicaInfosMap = new LinkedHashMap<>();
    snapshot.put("replicaInfos", replicaInfosMap);
    replicaInfos.forEach((node, perNode) -> {
      perNode.forEach((collection, shards) -> {
        shards.forEach((shard, replicas) -> {
          replicas.forEach(r -> {
            List> myReplicas = replicaInfosMap
                .computeIfAbsent(node, n -> new LinkedHashMap<>())
                .computeIfAbsent(collection, c -> new LinkedHashMap<>())
                .computeIfAbsent(shard, s -> new ArrayList<>());
            Map rMap = new LinkedHashMap<>();
            r.toMap(rMap);
            if (r.isLeader) { // ReplicaInfo.toMap doesn't write this!!!
              ((Map)rMap.values().iterator().next()).put("leader", "true");
            }
            myReplicas.add(rMap);
          });
        });
      });
    });
    return snapshot;
  }

  @Override
  public Map getNodeValues(String node, Collection tags) {
    return new LinkedHashMap<>(nodeValues.getOrDefault(node, Collections.emptyMap()));
  }

  @Override
  public Map>> getReplicaInfo(String node, Collection keys) {
    Map>> result = new LinkedHashMap<>();
    Map>> infos = replicaInfos.getOrDefault(node, Collections.emptyMap());
    // deep copy
    infos.forEach((coll, shards) -> {
      shards.forEach((shard, replicas) -> {
        replicas.forEach(ri -> {
          List myReplicas = result
              .computeIfAbsent(coll, c -> new LinkedHashMap<>())
              .computeIfAbsent(shard, s -> new ArrayList<>());
          ReplicaInfo myReplica = (ReplicaInfo)ri.clone();
          myReplicas.add(myReplica);
        });
      });
    });
    return result;
  }

  public ReplicaInfo getReplicaInfo(String collection, String coreNode) {
    for (Map>> perNode : replicaInfos.values()) {
      for (List perShard : perNode.getOrDefault(collection, Collections.emptyMap()).values()) {
        for (ReplicaInfo ri : perShard) {
          if (ri.getName().equals(coreNode)) {
            return (ReplicaInfo)ri.clone();
          }
        }
      }
    }
    return null;
  }

  @Override
  public void close() throws IOException {

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy