Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-14680: Provide simple interfaces to our cloud classes (only API) #1694

Merged
merged 29 commits into from Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,10 +25,15 @@
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.cloud.sdk.SolrCluster;
import org.apache.solr.common.cloud.sdk.SolrCollection;
import org.apache.solr.common.cloud.sdk.SolrNode;
import org.apache.solr.common.util.SimpleMap;
import org.apache.solr.common.util.Utils;
import org.apache.zookeeper.KeeperException;
import org.noggit.JSONWriter;
Expand All @@ -38,7 +43,7 @@
* {@link ZkStateReader#getClusterState()}.
* @lucene.experimental
*/
public class ClusterState implements JSONWriter.Writable {
public class ClusterState implements JSONWriter.Writable , SolrCluster {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little worried that this design allows for casting of the SolrCluster reference...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have multiple implementations of SolrCluster .The idea of existing classes implementing these interfaces is to have readily available implementations

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename Solr luster to Cluster.


private final Map<String, CollectionRef> collectionStates, immutableCollectionStates;
private Set<String> liveNodes;
Expand All @@ -58,6 +63,49 @@ private static Map<String, CollectionRef> getRefMap(Map<String, DocCollection> c
}
return collRefs;
}
private final SimpleMap<SolrCollection> _collections = new SimpleMap<>() {
@Override
public SolrCollection get(CharSequence key) {
CollectionRef ref = collectionStates.get(key.toString());
return ref ==null? null: ref.get();
}

@Override
public void forEach(BiConsumer<? super CharSequence, ? super SolrCollection> fun) {
collectionStates.forEach((s, collectionRef) -> {
DocCollection coll = collectionRef.get();
if (coll == null) return;
fun.accept(s, coll);
});
}
};

@Override
public SimpleMap<SolrCollection> collections() {
return _collections;
}


@Override
public SimpleMap<SolrNode> nodes() {
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public SolrNode getNode(String node) {
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public String overseerNode() {
throw new UnsupportedOperationException("Not yet implemented");

}

@Override
public String thisNode() {
throw new UnsupportedOperationException("Not yet implemented");
}

/**
* Use this if all the collection states are not readily available and some needs to be lazily loaded
Expand Down
Expand Up @@ -29,6 +29,9 @@
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;

import org.apache.solr.common.cloud.sdk.Shard;
import org.apache.solr.common.cloud.sdk.SolrCollection;
import org.apache.solr.common.util.SimpleMap;
import org.noggit.JSONWriter;

import static org.apache.solr.common.cloud.ZkStateReader.NRT_REPLICAS;
Expand All @@ -41,7 +44,7 @@
/**
* Models a Collection in zookeeper (but that Java name is obviously taken, hence "DocCollection")
*/
public class DocCollection extends ZkNodeProps implements Iterable<Slice> {
public class DocCollection extends ZkNodeProps implements Iterable<Slice>, SolrCollection {

public static final String DOC_ROUTER = "router";
public static final String SHARDS = "shards";
Expand Down Expand Up @@ -389,4 +392,25 @@ public int getExpectedReplicaCount(Replica.Type type, int def) {
if (type == Replica.Type.TLOG) result = numTlogReplicas;
return result == null ? def : result;
}

private SimpleMap<Shard> shards = new SimpleMap<Shard>() {
@Override
public Shard get(CharSequence key) {
return slices.get(key.toString());
}

@Override
public void forEach(BiConsumer<? super CharSequence, ? super Shard> fun) {
slices.forEach(fun::accept);
}
};
@Override
public SimpleMap<Shard> shards() {
return shards;
}

@Override
public String configSet() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
33 changes: 32 additions & 1 deletion solr/solrj/src/java/org/apache/solr/common/cloud/Replica.java
Expand Up @@ -27,13 +27,14 @@
import java.util.function.BiPredicate;

import org.apache.solr.common.MapWriter;
import org.apache.solr.common.cloud.sdk.ShardReplica;
import org.apache.solr.common.util.Utils;
import org.noggit.JSONWriter;

import static org.apache.solr.common.ConditionalMapWriter.NON_NULL_VAL;
import static org.apache.solr.common.ConditionalMapWriter.dedupeKeyPredicate;

public class Replica extends ZkNodeProps implements MapWriter {
public class Replica extends ZkNodeProps implements MapWriter, ShardReplica {

/**
* The replica's state. In general, if the node the replica is hosted on is
Expand Down Expand Up @@ -337,4 +338,34 @@ public void write(JSONWriter jsonWriter) {
public String toString() {
return name + ':' + Utils.toJSONString(propMap); // small enough, keep it on one line (i.e. no indent)
}

@Override
public String name() {
return name;
}

@Override
public String shard() {
return getShard();
}

@Override
public String collection() {
return getCollection();
}

@Override
public String node() {
return getNodeName();
}

@Override
public String core() {
return getCoreName();
}

@Override
public Type type() {
return type;
}
}
42 changes: 41 additions & 1 deletion solr/solrj/src/java/org/apache/solr/common/cloud/Slice.java
Expand Up @@ -25,18 +25,22 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.solr.common.cloud.Replica.Type;
import org.apache.solr.common.cloud.sdk.Shard;
import org.apache.solr.common.cloud.sdk.ShardReplica;
import org.apache.solr.common.util.SimpleMap;
import org.noggit.JSONWriter;

import static org.apache.solr.common.util.Utils.toJSONString;

/**
* A Slice contains immutable information about a logical shard (all replicas that share the same shard id).
*/
public class Slice extends ZkNodeProps implements Iterable<Replica> {
public class Slice extends ZkNodeProps implements Iterable<Replica> , Shard {
public final String collection;

/** Loads multiple slices into a Map from a generic Map that probably came from deserialized JSON. */
Expand Down Expand Up @@ -294,4 +298,40 @@ public void write(JSONWriter jsonWriter) {
jsonWriter.write(propMap);
}

@Override
public String name() {
return name;
}

@Override
public String collection() {
return collection;
}

@Override
public DocRouter.Range range() {
return range;
}

private final SimpleMap<ShardReplica> _replicas = new SimpleMap<ShardReplica>() {
@Override
public ShardReplica get(CharSequence key) {
return replicas.get(key.toString());
}

@Override
public void forEach(BiConsumer<? super CharSequence, ? super ShardReplica> fun) {
replicas.forEach(fun::accept);
}
};
@Override
public SimpleMap<ShardReplica> replicas() {
return _replicas;
}

@Override
public String leader() {
Replica leader = getLeader();
return leader == null ? null : leader.getName();
}
}
44 changes: 44 additions & 0 deletions solr/solrj/src/java/org/apache/solr/common/cloud/sdk/Shard.java
@@ -0,0 +1,44 @@
/*
* 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.common.cloud.sdk;

import org.apache.solr.common.cloud.DocRouter;
import org.apache.solr.common.util.SimpleMap;

/**A shard of a collection */
public interface Shard {

/**name of the shard */
String name();

/**collection this shard belongs to */
String collection();

/**hash range of this shard. null if this is not using hash based router
*/
DocRouter.Range range();

/** replicas of the shard
*/
SimpleMap<ShardReplica> replicas();

/**
* Name of the replica that is acting as the leader at the moment
*/
String leader();
}
@@ -0,0 +1,43 @@
/*
* 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.common.cloud.sdk;

import org.apache.solr.common.cloud.Replica;

/** replica of a shard */
public interface ShardReplica {
/** Name of this replica */
String name();

/**
* The shard which it belongs to
*/
String shard();

/** collection which it belongs to */
String collection();

/** Name of the node where this replica is present */
String node();

/** Name of the core where this is hosted */
String core();

/** type of the replica */
Replica.Type type();
}
@@ -0,0 +1,46 @@
/*
* 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.common.cloud.sdk;

import org.apache.solr.common.util.SimpleMap;

/**
* Represents a Solr cluster
*/
public interface SolrCluster {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have our own package namespace, prepending solr isn't really needed unless we think we might also model non-solr clusters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. Imagine this will also be used outside of Solr as a part of SolrJ. So if you read some client code,

Cluster cluster;

is less readable compared to

SolrCluster solrCluster;

Eventually, I would wish to replace a lot of SolrJ code/API with a standard set of interfaces.

/** collections in the cluster */
SimpleMap<SolrCollection> collections();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also aliases... and the Alias class returned should list the collections provided and routing info if routed. (also law of Demeter etc...)

Copy link
Contributor Author

@noblepaul noblepaul Jul 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wanted to include aliases. Where do you propose it to be there?

I would say , There should be no Alias class/interface.

It should be as simple as
SolrCluster#collections#get(String name , boolean includeAlias)

and

SolrCollection#aliases() returns List<String>


/** nodes in the cluster */
SimpleMap<SolrNode> nodes();

/** Get a {@link SolrNode} by name. returns null if no such node exists */
SolrNode getNode(String node);

/**
* Name of the node in which the overseer is running
*/
String overseerNode();

/**
* The name of the node in which this method is invoked from. returns null, if this is not invoked from a
* Solr node
*/
String thisNode();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've wondered sometimes if clusters should have name or id of some sort but that's probably another topic.

}