Class CloudSolrClient
- java.lang.Object
-
- org.apache.solr.client.solrj.SolrClient
-
- org.apache.solr.client.solrj.impl.CloudSolrClient
-
- All Implemented Interfaces:
Closeable
,Serializable
,AutoCloseable
- Direct Known Subclasses:
CloudHttp2SolrClient
,CloudLegacySolrClient
public abstract class CloudSolrClient extends SolrClient
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CloudSolrClient.Builder
ConstructsCloudSolrClient
instances from provided configuration.static class
CloudSolrClient.RouteException
static class
CloudSolrClient.RouteResponse<T extends LBSolrClient.Req>
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.solr.client.solrj.impl.CloudSolrClient.StateCache
collectionStateCache
static String
STATE_VERSION
-
Constructor Summary
Constructors Modifier Constructor Description protected
CloudSolrClient(boolean updatesToLeaders, boolean parallelUpdates, boolean directUpdatesToLeadersOnly)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
CloudSolrClient.RouteResponse
condenseResponse(NamedList<?> response, int timeMillis)
protected <T extends CloudSolrClient.RouteResponse<?>>
TcondenseResponse(NamedList<?> response, int timeMillis, Supplier<T> supplier)
void
connect()
Connect to the zookeeper ensemble.void
connect(long duration, TimeUnit timeUnit)
Connect to a cluster.protected Map<String,? extends LBSolrClient.Req>
createRoutes(UpdateRequest updateRequest, ModifiableSolrParams routableParams, DocCollection col, DocRouter router, Map<String,List<String>> urlMap, String routeField)
ClusterState
getClusterState()
abstract ClusterStateProvider
getClusterStateProvider()
String
getDefaultCollection()
Gets the default collection for requestprotected DocCollection
getDocCollection(String collection, Integer expectedVersion)
protected abstract LBSolrClient
getLbClient()
int
getMinAchievedReplicationFactor(String collection, NamedList<?> resp)
Useful for determining the minimum achieved replication factor across all shards involved in processing an update request, typically useful for gauging the replication factor of a batch.ResponseParser
getParser()
RequestWriter
getRequestWriter()
protected CloudSolrClient.RouteException
getRouteException(SolrException.ErrorCode serverError, NamedList<Throwable> exceptions, Map<String,? extends LBSolrClient.Req> routes)
Map<String,Integer>
getShardReplicationFactor(String collection, NamedList<?> resp)
Walks the NamedList response after performing an update request looking for the replication factor that was achieved in each shard involved in the request.boolean
isDirectUpdatesToLeadersOnly()
boolean
isParallelUpdates()
Gets whether direct updates are sent in parallelboolean
isUpdatesToLeaders()
protected static ArrayList<Object>
objectList(int n)
void
registerCollectionStateWatcher(String collection, CollectionStateWatcher watcher)
Register a CollectionStateWatcher to be called when the cluster state for a collection changes or the set of live nodes changes.void
registerDocCollectionWatcher(String collection, DocCollectionWatcher watcher)
Register a DocCollectionWatcher to be called when the cluster state for a collection changes.NamedList<Object>
request(SolrRequest<?> request, String collection)
Execute a request against a Solr server for a given collectionprotected NamedList<Object>
requestWithRetryOnStaleState(SolrRequest<?> request, int retryCount, List<String> inputCollections)
As this class doesn't watch external collections on the client side, there's a chance that the request will fail due to cached stale state, which means the state must be refreshed from ZK and retried.protected NamedList<Object>
sendRequest(SolrRequest<?> request, List<String> inputCollections)
void
setCollectionCacheTTl(int seconds)
Sets the cache ttl for DocCollection Objects cached.void
setDefaultCollection(String collection)
Sets the default collection for requestvoid
setParallelCacheRefreshes(int n)
If caches are expired they are refreshed after acquiring a lock.void
setParser(ResponseParser processor)
Note: This setter method is not thread-safe.void
setRequestWriter(RequestWriter requestWriter)
void
setRetryExpiryTime(int secs)
This is the time to wait to refetch the state after getting the same state version from ZKvoid
waitForState(String collection, long wait, TimeUnit unit, Predicate<DocCollection> predicate)
Block until a Predicate returns true, or the wait times outvoid
waitForState(String collection, long wait, TimeUnit unit, CollectionStatePredicate predicate)
Block until a CollectionStatePredicate returns true, or the wait times outprotected abstract boolean
wasCommError(Throwable t)
-
Methods inherited from class org.apache.solr.client.solrj.SolrClient
add, add, add, add, add, add, add, add, add, add, addBean, addBean, addBean, addBean, addBeans, addBeans, addBeans, addBeans, addBeans, addBeans, commit, commit, commit, commit, commit, commit, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteById, deleteByQuery, deleteByQuery, deleteByQuery, deleteByQuery, getBinder, getById, getById, getById, getById, getById, getById, getById, getById, getContext, optimize, optimize, optimize, optimize, optimize, optimize, ping, ping, query, query, query, query, queryAndStreamResponse, queryAndStreamResponse, queryAndStreamResponse, request, rollback, rollback
-
-
-
-
Field Detail
-
STATE_VERSION
public static final String STATE_VERSION
- See Also:
- Constant Field Values
-
collectionStateCache
protected final org.apache.solr.client.solrj.impl.CloudSolrClient.StateCache collectionStateCache
-
-
Method Detail
-
setRetryExpiryTime
public void setRetryExpiryTime(int secs)
This is the time to wait to refetch the state after getting the same state version from ZKsecs
-
setCollectionCacheTTl
public void setCollectionCacheTTl(int seconds)
Sets the cache ttl for DocCollection Objects cached.- Parameters:
seconds
- ttl value in seconds
-
getLbClient
protected abstract LBSolrClient getLbClient()
-
getClusterStateProvider
public abstract ClusterStateProvider getClusterStateProvider()
-
getClusterState
public ClusterState getClusterState()
-
wasCommError
protected abstract boolean wasCommError(Throwable t)
-
close
public void close() throws IOException
- Throws:
IOException
-
getParser
public ResponseParser getParser()
-
setParser
public void setParser(ResponseParser processor)
Note: This setter method is not thread-safe.- Parameters:
processor
- Default Response Parser chosen to parse the response if the parser were not specified as part of the request.- See Also:
SolrRequest.getResponseParser()
-
getRequestWriter
public RequestWriter getRequestWriter()
-
setRequestWriter
public void setRequestWriter(RequestWriter requestWriter)
-
setDefaultCollection
public void setDefaultCollection(String collection)
Sets the default collection for request
-
getDefaultCollection
public String getDefaultCollection()
Gets the default collection for request
-
isParallelUpdates
public boolean isParallelUpdates()
Gets whether direct updates are sent in parallel
-
connect
public void connect()
Connect to the zookeeper ensemble. This is an optional method that may be used to force a connect before any other requests are sent.
-
connect
public void connect(long duration, TimeUnit timeUnit) throws TimeoutException, InterruptedException
Connect to a cluster. If the cluster is not ready, retry connection up to a given timeout.- Parameters:
duration
- the timeouttimeUnit
- the units of the timeout- Throws:
TimeoutException
- if the cluster is not ready after the timeoutInterruptedException
- if the wait is interrupted
-
waitForState
public void waitForState(String collection, long wait, TimeUnit unit, CollectionStatePredicate predicate) throws InterruptedException, TimeoutException
Block until a CollectionStatePredicate returns true, or the wait times outNote that the predicate may be called again even after it has returned true, so implementors should avoid changing state within the predicate call itself.
This implementation utilizes
CollectionStateWatcher
internally. Callers that don't care about liveNodes are encouraged to use aDocCollection
Predicate
instead- Parameters:
collection
- the collection to watchwait
- how long to waitunit
- the units of the wait parameterpredicate
- aCollectionStatePredicate
to check the collection state- Throws:
InterruptedException
- on interruptTimeoutException
- on timeout- See Also:
waitForState(String, long, TimeUnit, Predicate)
,registerCollectionStateWatcher(java.lang.String, org.apache.solr.common.cloud.CollectionStateWatcher)
-
waitForState
public void waitForState(String collection, long wait, TimeUnit unit, Predicate<DocCollection> predicate) throws InterruptedException, TimeoutException
Block until a Predicate returns true, or the wait times outNote that the predicate may be called again even after it has returned true, so implementors should avoid changing state within the predicate call itself.
- Parameters:
collection
- the collection to watchwait
- how long to waitunit
- the units of the wait parameterpredicate
- aPredicate
to test against theDocCollection
- Throws:
InterruptedException
- on interruptTimeoutException
- on timeout- See Also:
registerDocCollectionWatcher(java.lang.String, org.apache.solr.common.cloud.DocCollectionWatcher)
-
registerCollectionStateWatcher
public void registerCollectionStateWatcher(String collection, CollectionStateWatcher watcher)
Register a CollectionStateWatcher to be called when the cluster state for a collection changes or the set of live nodes changes.The Watcher will automatically be removed when it's
onStateChanged
returnstrue
This implementation utilizes
ZkStateReader.registerCollectionStateWatcher(java.lang.String, org.apache.solr.common.cloud.CollectionStateWatcher)
internally. Callers that don't care about liveNodes are encouraged to use aDocCollectionWatcher
instead- Parameters:
collection
- the collection to watchwatcher
- a watcher that will be called when the state changes- See Also:
registerDocCollectionWatcher(String, DocCollectionWatcher)
,ZkStateReader.registerCollectionStateWatcher(java.lang.String, org.apache.solr.common.cloud.CollectionStateWatcher)
-
registerDocCollectionWatcher
public void registerDocCollectionWatcher(String collection, DocCollectionWatcher watcher)
Register a DocCollectionWatcher to be called when the cluster state for a collection changes.The Watcher will automatically be removed when it's
onStateChanged
returnstrue
- Parameters:
collection
- the collection to watchwatcher
- a watcher that will be called when the state changes- See Also:
ZkStateReader.registerDocCollectionWatcher(java.lang.String, org.apache.solr.common.cloud.DocCollectionWatcher)
-
getRouteException
protected CloudSolrClient.RouteException getRouteException(SolrException.ErrorCode serverError, NamedList<Throwable> exceptions, Map<String,? extends LBSolrClient.Req> routes)
-
createRoutes
protected Map<String,? extends LBSolrClient.Req> createRoutes(UpdateRequest updateRequest, ModifiableSolrParams routableParams, DocCollection col, DocRouter router, Map<String,List<String>> urlMap, String routeField)
-
condenseResponse
protected <T extends CloudSolrClient.RouteResponse<?>> T condenseResponse(NamedList<?> response, int timeMillis, Supplier<T> supplier)
-
condenseResponse
public CloudSolrClient.RouteResponse condenseResponse(NamedList<?> response, int timeMillis)
-
request
public NamedList<Object> request(SolrRequest<?> request, String collection) throws SolrServerException, IOException
Description copied from class:SolrClient
Execute a request against a Solr server for a given collection- Specified by:
request
in classSolrClient
- Parameters:
request
- the request to executecollection
- the collection to execute the request against- Returns:
- a
NamedList
containing the response from the server - Throws:
SolrServerException
- if there is an error on the serverIOException
- If there is a low-level I/O error.
-
requestWithRetryOnStaleState
protected NamedList<Object> requestWithRetryOnStaleState(SolrRequest<?> request, int retryCount, List<String> inputCollections) throws SolrServerException, IOException
As this class doesn't watch external collections on the client side, there's a chance that the request will fail due to cached stale state, which means the state must be refreshed from ZK and retried.- Throws:
SolrServerException
IOException
-
sendRequest
protected NamedList<Object> sendRequest(SolrRequest<?> request, List<String> inputCollections) throws SolrServerException, IOException
- Throws:
SolrServerException
IOException
-
isUpdatesToLeaders
public boolean isUpdatesToLeaders()
-
isDirectUpdatesToLeadersOnly
public boolean isDirectUpdatesToLeadersOnly()
- Returns:
- true if direct updates are sent to shard leaders only
-
setParallelCacheRefreshes
public void setParallelCacheRefreshes(int n)
If caches are expired they are refreshed after acquiring a lock. use this to set the number of locks
-
getDocCollection
protected DocCollection getDocCollection(String collection, Integer expectedVersion) throws SolrException
- Throws:
SolrException
-
getMinAchievedReplicationFactor
public int getMinAchievedReplicationFactor(String collection, NamedList<?> resp)
Useful for determining the minimum achieved replication factor across all shards involved in processing an update request, typically useful for gauging the replication factor of a batch.
-
getShardReplicationFactor
public Map<String,Integer> getShardReplicationFactor(String collection, NamedList<?> resp)
Walks the NamedList response after performing an update request looking for the replication factor that was achieved in each shard involved in the request. For single doc updates, there will be only one shard in the return value.
-
-