
org.apache.solr.common.cloud.Slice Maven / Gradle / Ivy
package org.apache.solr.common.cloud;
/*
* 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.
*/
import org.noggit.JSONUtil;
import org.noggit.JSONWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* A Slice contains immutable information about a logical shard (all replicas that share the same shard id).
*/
public class Slice extends ZkNodeProps {
public static String REPLICAS = "replicas";
public static String RANGE = "range";
public static String STATE = "state";
public static String LEADER = "leader"; // FUTURE: do we want to record the leader as a slice property in the JSON (as opposed to isLeader as a replica property?)
public static String ACTIVE = "active";
public static String INACTIVE = "inactive";
public static String CONSTRUCTION = "construction";
public static String RECOVERY = "recovery";
public static String PARENT = "parent";
private final String name;
private final DocRouter.Range range;
private final Integer replicationFactor; // FUTURE: optional per-slice override of the collection replicationFactor
private final Map replicas;
private final Replica leader;
private final String state;
private final String parent;
/**
* @param name The name of the slice
* @param replicas The replicas of the slice. This is used directly and a copy is not made. If null, replicas will be constructed from props.
* @param props The properties of the slice - a shallow copy will always be made.
*/
public Slice(String name, Map replicas, Map props) {
super( props==null ? new LinkedHashMap(2) : new LinkedHashMap(props));
this.name = name;
Object rangeObj = propMap.get(RANGE);
if (propMap.containsKey(STATE) && propMap.get(STATE) != null)
this.state = (String) propMap.get(STATE);
else {
this.state = ACTIVE; //Default to ACTIVE
propMap.put(STATE, this.state);
}
DocRouter.Range tmpRange = null;
if (rangeObj instanceof DocRouter.Range) {
tmpRange = (DocRouter.Range)rangeObj;
} else if (rangeObj != null) {
// Doesn't support custom implementations of Range, but currently not needed.
tmpRange = DocRouter.DEFAULT.fromString(rangeObj.toString());
}
range = tmpRange;
/** debugging. this isn't an error condition for custom sharding.
if (range == null) {
System.out.println("###### NO RANGE for " + name + " props=" + props);
}
**/
if (propMap.containsKey(PARENT) && propMap.get(PARENT) != null)
this.parent = (String) propMap.get(PARENT);
else
this.parent = null;
replicationFactor = null; // future
// add the replicas *after* the other properties (for aesthetics, so it's easy to find slice properties in the JSON output)
this.replicas = replicas != null ? replicas : makeReplicas((Map)propMap.get(REPLICAS));
propMap.put(REPLICAS, this.replicas);
leader = findLeader();
}
private Map makeReplicas(Map genericReplicas) {
if (genericReplicas == null) return new HashMap(1);
Map result = new LinkedHashMap(genericReplicas.size());
for (Map.Entry entry : genericReplicas.entrySet()) {
String name = entry.getKey();
Object val = entry.getValue();
Replica r;
if (val instanceof Replica) {
r = (Replica)val;
} else {
r = new Replica(name, (Map)val);
}
result.put(name, r);
}
return result;
}
private Replica findLeader() {
for (Replica replica : replicas.values()) {
if (replica.getStr(LEADER) != null) return replica;
}
return null;
}
/**
* Return slice name (shard id).
*/
public String getName() {
return name;
}
/**
* Gets the list of replicas for this slice.
*/
public Collection getReplicas() {
return replicas.values();
}
/**
* Get the map of coreNodeName to replicas for this slice.
*/
public Map getReplicasMap() {
return replicas;
}
public Map getReplicasCopy() {
return new LinkedHashMap(replicas);
}
public Replica getLeader() {
return leader;
}
public Replica getReplica(String replicaName) {
return replicas.get(replicaName);
}
public DocRouter.Range getRange() {
return range;
}
public String getState() {
return state;
}
public String getParent() {
return parent;
}
@Override
public String toString() {
return name + ':' + JSONUtil.toJSON(propMap);
}
@Override
public void write(JSONWriter jsonWriter) {
jsonWriter.write(propMap);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy