|
| 1 | +package redis.clients.jedis.mcf; |
| 2 | + |
| 3 | +import java.util.concurrent.CountDownLatch; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | +import java.util.concurrent.atomic.AtomicReference; |
| 6 | + |
| 7 | +import redis.clients.jedis.exceptions.JedisConnectionException; |
| 8 | +import redis.clients.jedis.exceptions.JedisValidationException; |
| 9 | + |
| 10 | +/** |
| 11 | + * StatusTracker is responsible for tracking and waiting for health status changes for specific endpoints. It provides |
| 12 | + * an event-driven approach to wait for health status transitions from UNKNOWN to either HEALTHY or UNHEALTHY. |
| 13 | + */ |
| 14 | +public class StatusTracker { |
| 15 | + |
| 16 | + private final HealthStatusManager healthStatusManager; |
| 17 | + |
| 18 | + public StatusTracker(HealthStatusManager healthStatusManager) { |
| 19 | + this.healthStatusManager = healthStatusManager; |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Waits for a specific endpoint's health status to be determined (not UNKNOWN). Uses event-driven approach with |
| 24 | + * CountDownLatch to avoid polling. |
| 25 | + * @param endpoint the endpoint to wait for |
| 26 | + * @return the determined health status (HEALTHY or UNHEALTHY) |
| 27 | + * @throws JedisConnectionException if interrupted while waiting |
| 28 | + */ |
| 29 | + public HealthStatus waitForHealthStatus(Endpoint endpoint) { |
| 30 | + // First check if status is already determined |
| 31 | + HealthStatus currentStatus = healthStatusManager.getHealthStatus(endpoint); |
| 32 | + if (currentStatus != HealthStatus.UNKNOWN) { |
| 33 | + return currentStatus; |
| 34 | + } |
| 35 | + |
| 36 | + // Set up event-driven waiting |
| 37 | + final CountDownLatch latch = new CountDownLatch(1); |
| 38 | + final AtomicReference<HealthStatus> resultStatus = new AtomicReference<>(); |
| 39 | + |
| 40 | + // Create a temporary listener for this specific endpoint |
| 41 | + HealthStatusListener tempListener = new HealthStatusListener() { |
| 42 | + @Override |
| 43 | + public void onStatusChange(HealthStatusChangeEvent event) { |
| 44 | + if (event.getEndpoint().equals(endpoint) && event.getNewStatus() != HealthStatus.UNKNOWN) { |
| 45 | + resultStatus.set(event.getNewStatus()); |
| 46 | + latch.countDown(); |
| 47 | + } |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + // Register the temporary listener |
| 52 | + healthStatusManager.registerListener(endpoint, tempListener); |
| 53 | + |
| 54 | + try { |
| 55 | + // Double-check status after registering listener (race condition protection) |
| 56 | + currentStatus = healthStatusManager.getHealthStatus(endpoint); |
| 57 | + if (currentStatus != HealthStatus.UNKNOWN) { |
| 58 | + return currentStatus; |
| 59 | + } |
| 60 | + |
| 61 | + // Wait for the health status change event |
| 62 | + // just for safety to not block indefinitely |
| 63 | + boolean completed = latch.await(60, TimeUnit.SECONDS); |
| 64 | + if (!completed) { |
| 65 | + throw new JedisValidationException("Timeout while waiting for health check result"); |
| 66 | + } |
| 67 | + return resultStatus.get(); |
| 68 | + |
| 69 | + } catch (InterruptedException e) { |
| 70 | + Thread.currentThread().interrupt(); |
| 71 | + throw new JedisConnectionException("Interrupted while waiting for health check result", e); |
| 72 | + } finally { |
| 73 | + // Clean up: unregister the temporary listener |
| 74 | + healthStatusManager.unregisterListener(endpoint, tempListener); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments