Class QueueCircuitBreakerImpl
- java.lang.Object
-
- org.swisspush.gateleen.queue.queuing.circuitbreaker.impl.QueueCircuitBreakerImpl
-
- All Implemented Interfaces:
Refreshable,QueueCircuitBreaker,RuleProvider.RuleChangesObserver
public class QueueCircuitBreakerImpl extends Object implements QueueCircuitBreaker, RuleProvider.RuleChangesObserver, Refreshable
- Author:
- https://github.com/mcweba [Marc-Andre Weber]
-
-
Field Summary
Fields Modifier and Type Field Description static StringOPEN_TO_HALF_OPEN_TASK_LOCKstatic StringUNLOCK_QUEUES_TASK_LOCKstatic StringUNLOCK_SAMPLE_QUEUES_TASK_LOCK
-
Constructor Summary
Constructors Constructor Description QueueCircuitBreakerImpl(io.vertx.core.Vertx vertx, Lock lock, String redisquesAddress, QueueCircuitBreakerStorage queueCircuitBreakerStorage, RuleProvider ruleProvider, GateleenExceptionFactory exceptionFactory, QueueCircuitBreakerRulePatternToCircuitMapping ruleToCircuitMapping, QueueCircuitBreakerConfigurationResourceManager configResourceManager, io.vertx.core.Handler<io.vertx.core.http.HttpServerRequest> queueCircuitBreakerHttpRequestHandler, int requestHandlerPort)Constructor for the QueueCircuitBreakerImpl.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description io.vertx.core.Future<Void>closeAllCircuits()Closes all non-closed circuits.io.vertx.core.Future<Void>closeCircuit(HttpRequest queuedRequest)Closes the circuit representing the queued request.io.vertx.core.Future<QueueCircuitState>handleQueuedRequest(String queueName, HttpRequest queuedRequest)Handles a queued request by checking the currentQueueCircuitStateof the corresponding circuit.booleanisCircuitCheckEnabled()Check whether the circuit check is enabled.booleanisStatisticsUpdateEnabled()Check whether the statistics update is enabled.io.vertx.core.Future<Void>lockQueue(String queueName, HttpRequest queuedRequest)Locks the queue having the provided queueName by calling the vertx-redisques API.voidrefresh()io.vertx.core.Future<Void>reOpenCircuit(HttpRequest queuedRequest)Re-Opens the (half-open) circuit representing the provided queued request again.voidrulesChanged(List<Rule> rules)io.vertx.core.Future<Long>setOpenCircuitsToHalfOpen()Changes the status of all circuits having a status equalsQueueCircuitState.OPENtoQueueCircuitState.HALF_OPEN.io.vertx.core.Future<String>unlockNextQueue()Unlocks the next queue in-line.io.vertx.core.Future<String>unlockQueue(String queueName)Unlocks the queue having the provided queueName by calling the vertx-redisques API.io.vertx.core.Future<Long>unlockSampleQueues()Unlocks a sample queue of all circuits having a status equalsQueueCircuitState.HALF_OPEN.io.vertx.core.Future<Void>updateStatistics(String queueName, HttpRequest queuedRequest, QueueResponseType queueResponseType)Updates the statistics of the corresponding circuit based on the provided request to execute.
-
-
-
Field Detail
-
OPEN_TO_HALF_OPEN_TASK_LOCK
public static final String OPEN_TO_HALF_OPEN_TASK_LOCK
- See Also:
- Constant Field Values
-
UNLOCK_QUEUES_TASK_LOCK
public static final String UNLOCK_QUEUES_TASK_LOCK
- See Also:
- Constant Field Values
-
UNLOCK_SAMPLE_QUEUES_TASK_LOCK
public static final String UNLOCK_SAMPLE_QUEUES_TASK_LOCK
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
QueueCircuitBreakerImpl
public QueueCircuitBreakerImpl(io.vertx.core.Vertx vertx, Lock lock, String redisquesAddress, QueueCircuitBreakerStorage queueCircuitBreakerStorage, RuleProvider ruleProvider, GateleenExceptionFactory exceptionFactory, QueueCircuitBreakerRulePatternToCircuitMapping ruleToCircuitMapping, QueueCircuitBreakerConfigurationResourceManager configResourceManager, io.vertx.core.Handler<io.vertx.core.http.HttpServerRequest> queueCircuitBreakerHttpRequestHandler, int requestHandlerPort)Constructor for the QueueCircuitBreakerImpl.- Parameters:
vertx- vertxlock- the lock implementationredisquesAddress- the event bus address of redisquesqueueCircuitBreakerStorage- the storageruleProvider- the provider for the rule objectsruleToCircuitMapping- ruleToCircuitMapping helper classconfigResourceManager- the manager for the configuration resourcequeueCircuitBreakerHttpRequestHandler- request handlerrequestHandlerPort- the port to listen to
-
-
Method Detail
-
rulesChanged
public void rulesChanged(List<Rule> rules)
- Specified by:
rulesChangedin interfaceRuleProvider.RuleChangesObserver
-
refresh
public void refresh()
- Specified by:
refreshin interfaceRefreshable
-
isCircuitCheckEnabled
public boolean isCircuitCheckEnabled()
Description copied from interface:QueueCircuitBreakerCheck whether the circuit check is enabled. The circuit check can be enabled/disabled with the 'circuitCheckEnabled' configuration property.- Specified by:
isCircuitCheckEnabledin interfaceQueueCircuitBreaker- Returns:
- returns true when circuit check is enabled, false otherwise
-
isStatisticsUpdateEnabled
public boolean isStatisticsUpdateEnabled()
Description copied from interface:QueueCircuitBreakerCheck whether the statistics update is enabled. The statistics update can be enabled/disabled with the 'statisticsUpdateEnabled' configuration property.- Specified by:
isStatisticsUpdateEnabledin interfaceQueueCircuitBreaker- Returns:
- returns true when statistics update is enabled, false otherwise
-
handleQueuedRequest
public io.vertx.core.Future<QueueCircuitState> handleQueuedRequest(String queueName, HttpRequest queuedRequest)
Description copied from interface:QueueCircuitBreakerHandles a queued request by checking the currentQueueCircuitStateof the corresponding circuit. When theQueueCircuitStateof the corresponding circuit equalsQueueCircuitState.OPEN, the provided queueName will be locked.- Specified by:
handleQueuedRequestin interfaceQueueCircuitBreaker- Parameters:
queueName- the name of the queuequeuedRequest- the queued request- Returns:
- returns a
QueueCircuitStaterepresenting the current status of the circuit
-
updateStatistics
public io.vertx.core.Future<Void> updateStatistics(String queueName, HttpRequest queuedRequest, QueueResponseType queueResponseType)
Description copied from interface:QueueCircuitBreakerUpdates the statistics of the corresponding circuit based on the provided request to execute.
Updating the statistics includes the following steps:
- Record queuedRequest as failed when queueResponseType equals
QueueResponseType.FAILURE - Record queuedRequest as success when queueResponseType equals
QueueResponseType.SUCCESS - Calculate failRatio based on fail/success records
- Change status of corresponding circuit to 'OPEN' when failRatio threshold is reached
- Specified by:
updateStatisticsin interfaceQueueCircuitBreaker- Parameters:
queueName- the name of the queuequeuedRequest- the queued requestqueueResponseType- theQueueResponseTyperepresenting the execution result of the queuedRequest- Returns:
- returns a void future when statistics could be updated successfully.
- Record queuedRequest as failed when queueResponseType equals
-
closeCircuit
public io.vertx.core.Future<Void> closeCircuit(HttpRequest queuedRequest)
Description copied from interface:QueueCircuitBreakerCloses the circuit representing the queued request.
Closing the circuit includes the following steps:
- Clear statistics of this circuit
- Reset failRatio of this circuit to zero
- Set status of this circuit to 'CLOSED'
- Unlock all queues related to this circuit
- Specified by:
closeCircuitin interfaceQueueCircuitBreaker- Parameters:
queuedRequest- the queued request- Returns:
- returns a void future when circuit could be closed successfully.
-
closeAllCircuits
public io.vertx.core.Future<Void> closeAllCircuits()
Description copied from interface:QueueCircuitBreakerCloses all non-closed circuits.
Closing all non-closed circuits includes the following steps:
- Clear statistics of all non-closed circuits
- Reset failRatio of all non-closed circuits to zero
- Set status of all non-closed circuits to 'CLOSED'
- Unlock all queues related to all non-closed circuits
- Specified by:
closeAllCircuitsin interfaceQueueCircuitBreaker- Returns:
- returns a void future when all non-closed circuits could be closed successfully.
-
reOpenCircuit
public io.vertx.core.Future<Void> reOpenCircuit(HttpRequest queuedRequest)
Description copied from interface:QueueCircuitBreakerRe-Opens the (half-open) circuit representing the provided queued request again. Circuits should be re-opened after a sample queue request was not successful.- Specified by:
reOpenCircuitin interfaceQueueCircuitBreaker- Parameters:
queuedRequest- the queued request- Returns:
- returns a void future when the circuit representing the provided queued request could be re-opened successfully
-
lockQueue
public io.vertx.core.Future<Void> lockQueue(String queueName, HttpRequest queuedRequest)
Description copied from interface:QueueCircuitBreakerLocks the queue having the provided queueName by calling the vertx-redisques API. Additionally, marks the queueName as a locked queue of the circuit representing the provided queuedRequest.- Specified by:
lockQueuein interfaceQueueCircuitBreaker- Parameters:
queueName- the name of the queuequeuedRequest- the queued request- Returns:
- returns a void future when the queue could be locked successfully through vertx-redisques and also successfully marked as a locked queue of the circuit.
-
unlockNextQueue
public io.vertx.core.Future<String> unlockNextQueue()
Description copied from interface:QueueCircuitBreakerUnlocks the next queue in-line. Does nothing when there's no next queue to unlock. Returns anullstring in this case.- Specified by:
unlockNextQueuein interfaceQueueCircuitBreaker- Returns:
- returns a string future holding the name of the queue which was successfully unlocked. Also returns the name
of the queue (as failureMessage) when the next queue could not be unlocked. Returns a
nullstring when no queue was available to unlock.
-
setOpenCircuitsToHalfOpen
public io.vertx.core.Future<Long> setOpenCircuitsToHalfOpen()
Description copied from interface:QueueCircuitBreakerChanges the status of all circuits having a status equalsQueueCircuitState.OPENtoQueueCircuitState.HALF_OPEN.- Specified by:
setOpenCircuitsToHalfOpenin interfaceQueueCircuitBreaker- Returns:
- returns a future holding the amount of circuits updated
-
unlockSampleQueues
public io.vertx.core.Future<Long> unlockSampleQueues()
Description copied from interface:QueueCircuitBreakerUnlocks a sample queue of all circuits having a status equalsQueueCircuitState.HALF_OPEN. The sample queues are always the queues which have not been unlocked the longest.- Specified by:
unlockSampleQueuesin interfaceQueueCircuitBreaker- Returns:
- returns a future holding the amount of unlocked sample queues
-
unlockQueue
public io.vertx.core.Future<String> unlockQueue(String queueName)
Description copied from interface:QueueCircuitBreakerUnlocks the queue having the provided queueName by calling the vertx-redisques API.- Specified by:
unlockQueuein interfaceQueueCircuitBreaker- Parameters:
queueName- the name of the queue- Returns:
- returns a string future holding the name of the queue when successfully unlocked. Also returns the name of the queue (as failureMessage) when the unlocking failed.
-
-