public class HostAvailabilityListener extends java.lang.Object implements QueryFailureListener, WriteFailureListener
HostAvailabilityListener is automatically registered with all QueryBatcher
and WriteBatcher instances to monitor for failover scenarios. When
HostAvailabilityListener detects that a host is unavailable (matches one of
getHostUnavailableExceptions()
), it blacklists the MarkLogic host for a
period of time equal to getSuspendTimeForHostUnavailable()
. After
that time, it calls DataMovementManager.readForestConfig()
then
passes that updated ForestConfiguration to batcher.withForestConfig() so the
batcher will fall back to using the hosts the server says are available.
Directly after black-listing (and before updating the ForestConfiguration)
this calls batcher.retry with the failed WriteBatch or QueryBatchException
so the batch can succeed if possible. The main objective here is to
gracefully handle a failover scenario by temporarily routing requests to
hosts other than the failed host(s), retry-ing requests that failed, and
eventually reverting to utilizing the full cluster that's available.
Nevertheless, there will definitely be failure scenarios not addressed by
HostAvailabilityListener and therefore we recommend that production
installations of Data Movement SDK use HostAvailabilityListener as an
example and install their own failure-handling listeners complete with
retry and updates to the batcher's ForestConfiguration as appropriate.
If you would like to change the default settings, you can change them
on the pre-registered HostAvailabilityListener which you can access via
WriteBatcher.getBatchFailureListeners()
or QueryBatcher.getQueryFailureListeners()
.
Constructor and Description |
---|
HostAvailabilityListener(DataMovementManager moveMgr)
Manages refreshing the forests and hosts and retrying events after a host
becomes unavailable.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Throwable[] |
getHostUnavailableExceptions() |
static HostAvailabilityListener |
getInstance(Batcher batcher)
Returns the first HostAvailabilityListener instance registered with the
Batcher.
|
int |
getMinHosts() |
java.time.Duration |
getSuspendTimeForHostUnavailable() |
BatchFailureListener<QueryBatch> |
initializeRetryListener(QueryBatchListener queryBatchListener)
Initializes the RetryListener for the given QueryBatchListener.
|
protected boolean |
isHostUnavailableException(java.lang.Throwable throwable,
java.util.Set<java.lang.Throwable> path) |
void |
processFailure(QueryBatchException queryBatch)
This implements the QueryFailureListener interface
|
void |
processFailure(WriteBatch batch,
java.lang.Throwable throwable)
This implements the WriteFailureListener interface
|
HostAvailabilityListener |
withHostUnavailableExceptions(java.lang.Class<java.lang.Throwable>... exceptionTypes)
Overwrites the list of exceptions for which a request can be retried and
a MarkLogic host can be blacklisted
|
HostAvailabilityListener |
withMinHosts(int numHosts)
If less than minHosts are left, calls stopJob.
|
HostAvailabilityListener |
withSuspendTimeForHostUnavailable(java.time.Duration duration)
If a host becomes unavailable (SocketException, SSLException,
UnknownHostException), adds it to the blacklist
|
public HostAvailabilityListener(DataMovementManager moveMgr)
moveMgr
- the DataMovementManager (used to call readForestConfig to reset after black-listing an unavailable host)public HostAvailabilityListener withSuspendTimeForHostUnavailable(java.time.Duration duration)
duration
- the amount of time an unavailable host will be suspendedpublic HostAvailabilityListener withMinHosts(int numHosts)
numHosts
- the minimum number of hosts before this will call dataMovementMangaer.stopJob(batcher)public HostAvailabilityListener withHostUnavailableExceptions(java.lang.Class<java.lang.Throwable>... exceptionTypes)
exceptionTypes
- the list of types of Throwable, any of which constitute a host that's unavailablepublic java.lang.Throwable[] getHostUnavailableExceptions()
public java.time.Duration getSuspendTimeForHostUnavailable()
public int getMinHosts()
public void processFailure(WriteBatch batch, java.lang.Throwable throwable)
processFailure
in interface BatchFailureListener<WriteBatch>
processFailure
in interface WriteFailureListener
batch
- the batch of WriteEventsthrowable
- the exceptionpublic void processFailure(QueryBatchException queryBatch)
processFailure
in interface FailureListener<QueryBatchException>
processFailure
in interface QueryFailureListener
queryBatch
- the exception with information about the failed query attemptprotected boolean isHostUnavailableException(java.lang.Throwable throwable, java.util.Set<java.lang.Throwable> path)
public BatchFailureListener<QueryBatch> initializeRetryListener(QueryBatchListener queryBatchListener)
queryBatchListener
- the QueryBatchListener for which the RetryListener
has to be initialized.public static HostAvailabilityListener getInstance(Batcher batcher)
Returns the first HostAvailabilityListener instance registered with the Batcher.
You can customize the HostAvailabilityListener instance registered with the Batcher like:
HostAvailabilityListener.getInstance(Batcher)
.withSuspendTimeForHostUnavailable(Duration.ofMinutes(60))
.withMinHosts(2)
batcher
- the Batcher instance for which the registered
HostAvailabilityListener is returnedjava.lang.IllegalStateException
- if the passed Batcher is neither a
QueryBatcher nor a WriteBatcherCopyright © 2013-2018 MarkLogic Corporation.