Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,88 @@

package com.yashdotdev.distributed_traffic_control.config;

import com.yashdotdev.distributed_traffic_control.lease.GlobalCapacityKey;
import com.yashdotdev.distributed_traffic_control.lease.LeaseCoordinator;
import com.yashdotdev.distributed_traffic_control.policy.TrafficPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class GlobalCapacityBootstrap implements ApplicationRunner {

private static final Logger logger =
LoggerFactory.getLogger(GlobalCapacityBootstrap.class);

private static final String DEFAULT_RESOURCE = "/api/orders";

private final LeaseCoordinator leaseCoordinator;
private final TrafficPolicy defaultTrafficPolicy;
private final int maxAttempts;
private final long retryDelayMs;

public GlobalCapacityBootstrap(
LeaseCoordinator leaseCoordinator,
TrafficPolicy defaultTrafficPolicy,
@Value("${allocation.bootstrap.max-attempts:5}") int maxAttempts,
@Value("${allocation.bootstrap.retry-delay-ms:1000}") long retryDelayMs) {

if (maxAttempts < 1) {
throw new IllegalArgumentException(
"allocation.bootstrap.max-attempts must be at least 1");
}
if (retryDelayMs < 0) {
throw new IllegalArgumentException(
"allocation.bootstrap.retry-delay-ms must not be negative");
}

public GlobalCapacityBootstrap(LeaseCoordinator leaseCoordinator, TrafficPolicy defaultTrafficPolicy) {
this.leaseCoordinator = leaseCoordinator;
this.defaultTrafficPolicy = defaultTrafficPolicy;
this.maxAttempts = maxAttempts;
this.retryDelayMs = retryDelayMs;
}

@Override
public void run(ApplicationArguments args) {
GlobalCapacityKey capacityKey = new GlobalCapacityKey(defaultTrafficPolicy.getPolicyId(), DEFAULT_RESOURCE);
leaseCoordinator.registerCapacity(capacityKey, defaultTrafficPolicy.getCapacity());
GlobalCapacityKey capacityKey = new GlobalCapacityKey(
defaultTrafficPolicy.getPolicyId(), DEFAULT_RESOURCE);

for (int attempt = 1; attempt <= maxAttempts; attempt++) {
try {
leaseCoordinator.registerCapacity(
capacityKey, defaultTrafficPolicy.getCapacity());

logger.info(
"Registered global capacity for {} on attempt {}",
DEFAULT_RESOURCE, attempt);
return;
}
catch (RuntimeException exception) {
if (attempt == maxAttempts) {
throw new IllegalStateException(
"Failed to register global capacity after "
+ maxAttempts + " attempts",
exception);
}

logger.warn(
"Failed to register global capacity (attempt {}/{}). "
+ "Retrying in {} ms",
attempt, maxAttempts, retryDelayMs, exception);

try {
Thread.sleep(retryDelayMs);
}
catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
throw new IllegalStateException(
"Interrupted while retrying global capacity registration",
interruptedException);
}
}
}
}
}
}
10 changes: 10 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ management.tracing.sampling.probability=${TRACING_SAMPLING_PROBABILITY:1.0}
# Do not export application metrics to Jaeger
management.otlp.metrics.export.enabled=false


# Redis-aware readiness
management.endpoint.health.probes.enabled=true
management.endpoint.health.group.readiness.include=readinessState,redis
management.endpoint.health.show-details=never


# Global capacity bootstrap resilience
allocation.bootstrap.max-attempts=${ALLOCATION_BOOTSTRAP_MAX_ATTEMPTS:5}
allocation.bootstrap.retry-delay-ms=${ALLOCATION_BOOTSTRAP_RETRY_DELAY_MS:1000}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.yashdotdev.distributed_traffic_control.config;

import com.yashdotdev.distributed_traffic_control.lease.LeaseCoordinator;
import com.yashdotdev.distributed_traffic_control.policy.TrafficPolicy;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

class GlobalCapacityBootstrapTest {

@Test
void shouldRegisterCapacityOnFirstAttempt() {
LeaseCoordinator coordinator = mock(LeaseCoordinator.class);
TrafficPolicy policy = mock(TrafficPolicy.class);

when(policy.getPolicyId()).thenReturn("default-policy");
when(policy.getCapacity()).thenReturn(100L);

GlobalCapacityBootstrap bootstrap =
new GlobalCapacityBootstrap(coordinator, policy, 3, 0);

assertDoesNotThrow(() -> bootstrap.run(null));

verify(coordinator, times(1)).registerCapacity(any(), eq(100L));
}

@Test
void shouldRetryWhenRegistrationInitiallyFails() {
LeaseCoordinator coordinator = mock(LeaseCoordinator.class);
TrafficPolicy policy = mock(TrafficPolicy.class);

when(policy.getPolicyId()).thenReturn("default-policy");
when(policy.getCapacity()).thenReturn(100L);

doThrow(new IllegalStateException("Redis unavailable"))
.doNothing()
.when(coordinator)
.registerCapacity(any(), eq(100L));

GlobalCapacityBootstrap bootstrap =
new GlobalCapacityBootstrap(coordinator, policy, 3, 0);

assertDoesNotThrow(() -> bootstrap.run(null));

verify(coordinator, times(2)).registerCapacity(any(), eq(100L));
}

@Test
void shouldFailAfterExhaustingRetries() {
LeaseCoordinator coordinator = mock(LeaseCoordinator.class);
TrafficPolicy policy = mock(TrafficPolicy.class);

when(policy.getPolicyId()).thenReturn("default-policy");
when(policy.getCapacity()).thenReturn(100L);

doThrow(new IllegalStateException("Redis unavailable"))
.when(coordinator)
.registerCapacity(any(), eq(100L));

GlobalCapacityBootstrap bootstrap =
new GlobalCapacityBootstrap(coordinator, policy, 3, 0);

assertThrows(
IllegalStateException.class,
() -> bootstrap.run(null));

verify(coordinator, times(3)).registerCapacity(any(), eq(100L));
}

@Test
void shouldRejectInvalidRetryConfiguration() {
LeaseCoordinator coordinator = mock(LeaseCoordinator.class);
TrafficPolicy policy = mock(TrafficPolicy.class);

assertThrows(
IllegalArgumentException.class,
() -> new GlobalCapacityBootstrap(coordinator, policy, 0, 0));

assertThrows(
IllegalArgumentException.class,
() -> new GlobalCapacityBootstrap(coordinator, policy, 3, -1));
}
}
Loading