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

org.apache.solr.cluster.placement.impl.CollectionMetricsBuilder 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.cluster.placement.impl;

import java.lang.invoke.MethodHandles;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import org.apache.solr.cluster.placement.CollectionMetrics;
import org.apache.solr.cluster.placement.ReplicaMetric;
import org.apache.solr.cluster.placement.ReplicaMetrics;
import org.apache.solr.cluster.placement.ShardMetrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Builder class for constructing instances of {@link CollectionMetrics}. */
public class CollectionMetricsBuilder {

  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

  final Map shardMetricsBuilders = new HashMap<>();

  public Map getShardMetricsBuilders() {
    return shardMetricsBuilders;
  }

  public CollectionMetrics build() {
    final Map metricsMap = new HashMap<>();
    shardMetricsBuilders.forEach((shard, builder) -> metricsMap.put(shard, builder.build()));
    return new CollectionMetrics() {
      @Override
      public Optional getShardMetrics(String shardName) {
        return Optional.ofNullable(metricsMap.get(shardName));
      }

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

  public static class ShardMetricsBuilder {
    final Map replicaMetricsBuilders = new HashMap<>();
    final String shardName;
    ReplicaMetricsBuilder leaderMetricsBuilder;

    public ShardMetricsBuilder(String shardName) {
      this.shardName = shardName;
    }

    public Map getReplicaMetricsBuilders() {
      return replicaMetricsBuilders;
    }

    public ShardMetricsBuilder setLeaderMetrics(ReplicaMetricsBuilder replicaMetricsBuilder) {
      leaderMetricsBuilder = replicaMetricsBuilder;
      if (leaderMetricsBuilder != null) {
        replicaMetricsBuilders.put(leaderMetricsBuilder.replicaName, leaderMetricsBuilder);
      }
      return this;
    }

    public ShardMetrics build() {
      final Map metricsMap = new HashMap<>();
      replicaMetricsBuilders.forEach(
          (name, replicaBuilder) -> {
            ReplicaMetrics metrics = replicaBuilder.build();
            metricsMap.put(name, metrics);
            if (replicaBuilder.leader) {
              if (leaderMetricsBuilder != null
                  && !leaderMetricsBuilder.replicaName.equals(replicaBuilder.replicaName)) {
                log.warn(
                    "Multiple replicas claim to be shard leader, selecting the latest candidate ({}) for metrics purposes",
                    replicaBuilder.replicaName);
              }
              leaderMetricsBuilder = replicaBuilder;
            }
          });
      final ReplicaMetrics finalLeaderMetrics =
          leaderMetricsBuilder != null ? leaderMetricsBuilder.build() : null;
      return new ShardMetrics() {
        @Override
        public String getShardName() {
          return shardName;
        }

        @Override
        public Optional getLeaderMetrics() {
          return Optional.ofNullable(finalLeaderMetrics);
        }

        @Override
        public Optional getReplicaMetrics(String replicaName) {
          return Optional.ofNullable(metricsMap.get(replicaName));
        }

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

  public static class ReplicaMetricsBuilder {
    final Map, Object> metrics = new HashMap<>();
    final String replicaName;
    boolean leader;

    public ReplicaMetricsBuilder(String replicaName) {
      this.replicaName = replicaName;
    }

    public ReplicaMetricsBuilder setLeader(boolean leader) {
      this.leader = leader;
      return this;
    }

    /**
     * Add unconverted (raw) values here, this method internally calls {@link
     * ReplicaMetric#convert(Object)}.
     *
     * @param metric metric to add
     * @param value raw (unconverted) metric value
     */
    public ReplicaMetricsBuilder addMetric(ReplicaMetric metric, Object value) {
      value = metric.convert(value);
      if (value != null) {
        metrics.put(metric, value);
      }
      return this;
    }

    public ReplicaMetrics build() {
      return new ReplicaMetrics() {
        @Override
        public String getReplicaName() {
          return replicaName;
        }

        @Override
        @SuppressWarnings("unchecked")
        public  Optional getReplicaMetric(ReplicaMetric metric) {
          return Optional.ofNullable((T) metrics.get(metric));
        }

        @Override
        public Iterator, Object>> iterator() {
          return metrics.entrySet().iterator();
        }
      };
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy