/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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 com.mongodb.internal.connection;
import com.mongodb.ServerAddress;
import com.mongodb.TagSet;
import com.mongodb.connection.ClusterDescription;
import com.mongodb.connection.ServerDescription;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
This class is NOT part of the public API.
/**
* This class is NOT part of the public API.
*/
public final class ClusterDescriptionHelper {
Returns the Set of all server descriptions in this cluster, sorted by the String value of the ServerAddress of each one.
Returns: the set of server descriptions
/**
* Returns the Set of all server descriptions in this cluster, sorted by the String value of the ServerAddress of each one.
*
* @return the set of server descriptions
*/
public static Set<ServerDescription> getAll(final ClusterDescription clusterDescription) {
Set<ServerDescription> serverDescriptionSet = new TreeSet<ServerDescription>((o1, o2) -> {
int val = o1.getAddress().getHost().compareTo(o2.getAddress().getHost());
if (val != 0) {
return val;
}
return Integer.compare(o1.getAddress().getPort(), o2.getAddress().getPort());
});
serverDescriptionSet.addAll(clusterDescription.getServerDescriptions());
return Collections.unmodifiableSet(serverDescriptionSet);
}
Returns the ServerDescription for the server at the given address
Params: - serverAddress – the ServerAddress for a server in this cluster
Returns: the ServerDescription for this server
/**
* Returns the ServerDescription for the server at the given address
*
* @param serverAddress the ServerAddress for a server in this cluster
* @return the ServerDescription for this server
*/
public static ServerDescription getByServerAddress(final ClusterDescription clusterDescription, final ServerAddress serverAddress) {
for (final ServerDescription cur : clusterDescription.getServerDescriptions()) {
if (cur.isOk() && cur.getAddress().equals(serverAddress)) {
return cur;
}
}
return null;
}
While it may seem counter-intuitive that a MongoDB cluster can have more than one primary, it can in the case where the client's view
of the cluster is a set of mongos servers, any of which can serve as the primary.
Returns: a list of servers that can act as primaries
/**
* While it may seem counter-intuitive that a MongoDB cluster can have more than one primary, it can in the case where the client's view
* of the cluster is a set of mongos servers, any of which can serve as the primary.
*
* @return a list of servers that can act as primaries
*/
public static List<ServerDescription> getPrimaries(final ClusterDescription clusterDescription) {
return getServersByPredicate(clusterDescription, ServerDescription::isPrimary);
}
Get a list of all the secondaries in this cluster
Returns: a List of ServerDescriptions of all the secondaries this cluster is currently aware of
/**
* Get a list of all the secondaries in this cluster
*
* @return a List of ServerDescriptions of all the secondaries this cluster is currently aware of
*/
public static List<ServerDescription> getSecondaries(final ClusterDescription clusterDescription) {
return getServersByPredicate(clusterDescription, ServerDescription::isSecondary);
}
Get a list of all the secondaries in this cluster that match a given TagSet
Params: - tagSet – a Set of replica set tags
Returns: a List of ServerDescriptions of all the secondaries this cluster that match all of the given tags
/**
* Get a list of all the secondaries in this cluster that match a given TagSet
*
* @param tagSet a Set of replica set tags
* @return a List of ServerDescriptions of all the secondaries this cluster that match all of the given tags
*/
public static List<ServerDescription> getSecondaries(final ClusterDescription clusterDescription, final TagSet tagSet) {
return getServersByPredicate(clusterDescription, serverDescription ->
serverDescription.isSecondary() && serverDescription.hasTags(tagSet));
}
Gets a list of ServerDescriptions for all the servers in this cluster which are currently accessible.
Returns: a List of ServerDescriptions for all servers that have a status of OK
/**
* Gets a list of ServerDescriptions for all the servers in this cluster which are currently accessible.
*
* @return a List of ServerDescriptions for all servers that have a status of OK
*/
public static List<ServerDescription> getAny(final ClusterDescription clusterDescription) {
return getServersByPredicate(clusterDescription, ServerDescription::isOk);
}
Gets a list of all the primaries and secondaries in this cluster.
Returns: a list of ServerDescriptions for all primary and secondary servers
/**
* Gets a list of all the primaries and secondaries in this cluster.
*
* @return a list of ServerDescriptions for all primary and secondary servers
*/
public static List<ServerDescription> getAnyPrimaryOrSecondary(final ClusterDescription clusterDescription) {
return getServersByPredicate(clusterDescription, serverDescription ->
serverDescription.isPrimary() || serverDescription.isSecondary());
}
Gets a list of all the primaries and secondaries in this cluster that match the given replica set tags.
Params: - tagSet – a Set of replica set tags
Returns: a list of ServerDescriptions for all primary and secondary servers that contain all of the given tags
/**
* Gets a list of all the primaries and secondaries in this cluster that match the given replica set tags.
*
* @param tagSet a Set of replica set tags
* @return a list of ServerDescriptions for all primary and secondary servers that contain all of the given tags
*/
public static List<ServerDescription> getAnyPrimaryOrSecondary(final ClusterDescription clusterDescription, final TagSet tagSet) {
return getServersByPredicate(clusterDescription, serverDescription ->
(serverDescription.isPrimary() || serverDescription.isSecondary()) && serverDescription.hasTags(tagSet));
}
public interface Predicate {
boolean apply(ServerDescription serverDescription);
}
public static List<ServerDescription> getServersByPredicate(final ClusterDescription clusterDescription, final Predicate predicate) {
List<ServerDescription> membersByTag = new ArrayList<ServerDescription>();
for (final ServerDescription cur : clusterDescription.getServerDescriptions()) {
if (predicate.apply(cur)) {
membersByTag.add(cur);
}
}
return membersByTag;
}
private ClusterDescriptionHelper() {
}
}