use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget in project titus-control-plane by Netflix.
the class CassandraLoadBalancerStoreTest method testUpdateTarget.
@Test(timeout = TIMEOUT_MS)
public void testUpdateTarget() throws Exception {
Session session = cassandraCQLUnit.getSession();
BoundStatement countStmt = session.prepare("SELECT COUNT(*) FROM load_balancer_targets;").bind();
PreparedStatement stateStmt = session.prepare("SELECT state FROM load_balancer_targets WHERE load_balancer_id = ? AND ip_address = ?;");
assertThat(session.execute(countStmt).one().getLong(0)).isEqualTo(0);
LoadBalancerTarget target = new LoadBalancerTarget("lb-1", "task-1", "1.1.1.1");
CassandraLoadBalancerStore store = getInitdStore();
store.addOrUpdateTargets(target.withState(LoadBalancerTarget.State.REGISTERED)).block();
assertThat(session.execute(countStmt).one().getLong(0)).isEqualTo(1);
Row registered = session.execute(stateStmt.bind("lb-1", "1.1.1.1")).one();
assertThat(registered.getString("state")).isEqualTo("REGISTERED");
store.addOrUpdateTargets(target.withState(LoadBalancerTarget.State.DEREGISTERED)).block();
assertThat(session.execute(countStmt).one().getLong(0)).isEqualTo(1);
Row deregistered = session.execute(stateStmt.bind("lb-1", "1.1.1.1")).one();
assertThat(deregistered.getString("state")).isEqualTo("DEREGISTERED");
}
use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget in project titus-control-plane by Netflix.
the class CassandraLoadBalancerStoreTest method testAddTargets.
@Test(timeout = TIMEOUT_MS)
public void testAddTargets() throws Exception {
Map<LoadBalancerTarget, LoadBalancerTarget.State> testData = generateTestData(10, 20, 1).getTargets();
Map<String, List<LoadBalancerTargetState>> expectedTargetsByLoadBalancer = testData.entrySet().stream().map(entry -> new LoadBalancerTargetState(entry.getKey(), entry.getValue())).collect(Collectors.groupingBy(t -> t.getLoadBalancerTarget().getLoadBalancerId()));
CassandraLoadBalancerStore store = getInitdStore();
store.addOrUpdateTargets(testData.entrySet().stream().map(LoadBalancerTargetState::from).collect(Collectors.toList())).block();
expectedTargetsByLoadBalancer.forEach((loadBalancerId, expectedTargets) -> assertThat(store.getLoadBalancerTargets(loadBalancerId).collectList().block()).containsExactlyInAnyOrder(IterableUtil.toArray(expectedTargets)));
int totalCount = expectedTargetsByLoadBalancer.values().stream().mapToInt(List::size).sum();
Session session = cassandraCQLUnit.getSession();
ResultSet resultSet = session.execute("SELECT COUNT(*) FROM load_balancer_targets;");
assertThat(resultSet.one().getLong(0)).isEqualTo(totalCount);
}
use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget in project titus-control-plane by Netflix.
the class DefaultLoadBalancerReconcilerTest method deregisterExtraTargetsPreviouslyRegisteredByUs.
@Test(timeout = TEST_TIMEOUT_MS)
public void deregisterExtraTargetsPreviouslyRegisteredByUs() {
List<Task> tasks = LoadBalancerTests.buildTasksStarted(3, jobId);
JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, loadBalancerId);
JobLoadBalancerState association = new JobLoadBalancerState(jobLoadBalancer, State.ASSOCIATED);
when(v3JobOperations.getTasks(jobId)).thenReturn(tasks);
reset(connector);
when(connector.getLoadBalancer(loadBalancerId)).thenReturn(Single.just(new LoadBalancer(loadBalancerId, LoadBalancer.State.ACTIVE, CollectionsExt.asSet("1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4", "5.5.5.5", "6.6.6.6"))));
store.addOrUpdateLoadBalancer(association.getJobLoadBalancer(), association.getState()).await();
store.addOrUpdateTargets(// 3 running tasks were previously registered by us and are in the load balancer
new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(0).getId(), "1.1.1.1"), LoadBalancerTarget.State.REGISTERED), new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(1).getId(), "2.2.2.2"), LoadBalancerTarget.State.REGISTERED), new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(2).getId(), "3.3.3.3"), LoadBalancerTarget.State.REGISTERED), // Next two ips were previously registered by us, but their tasks do not exist anymore
new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "some-dead-task", "4.4.4.4"), LoadBalancerTarget.State.REGISTERED), new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "another-dead-task", "5.5.5.5"), LoadBalancerTarget.State.DEREGISTERED)).block();
testScheduler.triggerActions();
subscriber.assertNotCompleted().assertNoValues();
awaitReconciliationRuns(1);
subscriber.assertNotCompleted().assertValueCount(2);
subscriber.getOnNextEvents().forEach(update -> {
assertThat(update.getState()).isEqualTo(LoadBalancerTarget.State.DEREGISTERED);
assertThat(update.getPriority()).isEqualTo(Priority.LOW);
assertThat(update.getLoadBalancerId()).isEqualTo(loadBalancerId);
assertThat(update.getIdentifier().getTaskId()).isIn("some-dead-task", "another-dead-task");
assertThat(update.getIdentifier().getIpAddress()).isIn("4.4.4.4", "5.5.5.5");
});
}
use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget in project titus-control-plane by Netflix.
the class DefaultLoadBalancerReconcilerTest method deregisteredTargetsAreCleanedUp.
@Test(timeout = TEST_TIMEOUT_MS)
public void deregisteredTargetsAreCleanedUp() {
List<Task> tasks = LoadBalancerTests.buildTasksStarted(1, jobId);
JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, loadBalancerId);
JobLoadBalancerState association = new JobLoadBalancerState(jobLoadBalancer, State.ASSOCIATED);
when(v3JobOperations.getTasks(jobId)).thenReturn(tasks);
reset(connector);
when(connector.getLoadBalancer(loadBalancerId)).thenReturn(Single.just(new LoadBalancer(loadBalancerId, LoadBalancer.State.ACTIVE, CollectionsExt.asSet("1.1.1.1", "10.10.10.10"))));
store.addOrUpdateLoadBalancer(association.getJobLoadBalancer(), association.getState()).await();
store.addOrUpdateTargets(// running tasks was previously registered by us and are in the load balancer
new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(0).getId(), "1.1.1.1"), LoadBalancerTarget.State.REGISTERED), // Next three ips were previously registered by us, but their tasks do not exist anymore and are not in the load balancer anymore
new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "target-inconsistent", "2.2.2.2"), LoadBalancerTarget.State.REGISTERED), new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "target-not-in-lb", "3.3.3.3"), LoadBalancerTarget.State.DEREGISTERED)).block();
// no reconciliation ran yet
testScheduler.triggerActions();
subscriber.assertNotCompleted().assertNoValues();
assertThat(store.getLoadBalancerTargets(loadBalancerId).collectList().block()).hasSize(3);
// first pass, the one stored as DEREGISTERED is cleaned up, the other in an inconsistent state is fixed
awaitReconciliationRuns(1);
subscriber.assertNotCompleted().assertValueCount(1);
TargetStateBatchable inconsistencyFix = subscriber.getOnNextEvents().get(0);
assertThat(inconsistencyFix.getState()).isEqualTo(LoadBalancerTarget.State.DEREGISTERED);
assertThat(inconsistencyFix.getLoadBalancerId()).isEqualTo(loadBalancerId);
assertThat(inconsistencyFix.getIpAddress()).isEqualTo("2.2.2.2");
List<LoadBalancerTargetState> storedTargets = store.getLoadBalancerTargets(loadBalancerId).collectList().block();
assertThat(storedTargets).hasSize(2);
assertThat(storedTargets).doesNotContain(new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "target-not-in-lb", "3.3.3.3"), LoadBalancerTarget.State.DEREGISTERED));
// update with fix not applied yet, keep trying
awaitReconciliationRuns(1);
subscriber.assertNotCompleted().assertValueCount(2);
TargetStateBatchable update2 = subscriber.getOnNextEvents().get(0);
assertThat(update2.getState()).isEqualTo(LoadBalancerTarget.State.DEREGISTERED);
assertThat(update2.getLoadBalancerId()).isEqualTo(loadBalancerId);
assertThat(update2.getIpAddress()).isEqualTo("2.2.2.2");
assertThat(store.getLoadBalancerTargets(loadBalancerId).collectList().block()).hasSize(2);
// simulate the update with the fix above being applied
store.addOrUpdateTargets(new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, "target-inconsistent", "2.2.2.2"), LoadBalancerTarget.State.DEREGISTERED)).block();
// finally, corrected record is now cleaned up
awaitReconciliationRuns(1);
// no changes needed
subscriber.assertNotCompleted().assertValueCount(2);
assertThat(store.getLoadBalancerTargets(loadBalancerId).collectList().block()).containsOnly(new LoadBalancerTargetState(new LoadBalancerTarget(loadBalancerId, tasks.get(0).getId(), "1.1.1.1"), LoadBalancerTarget.State.REGISTERED));
}
use of com.netflix.titus.api.loadbalancer.model.LoadBalancerTarget in project titus-control-plane by Netflix.
the class DefaultLoadBalancerReconcilerTest method updatesAreIgnoredWhileCooldownIsActive.
@Test(timeout = TEST_TIMEOUT_MS)
public void updatesAreIgnoredWhileCooldownIsActive() {
long cooldownPeriodMs = 5 * delayMs;
List<Task> tasks = LoadBalancerTests.buildTasksStarted(5, jobId);
JobLoadBalancer jobLoadBalancer = new JobLoadBalancer(jobId, loadBalancerId);
JobLoadBalancerState association = new JobLoadBalancerState(jobLoadBalancer, State.ASSOCIATED);
when(v3JobOperations.getTasks(jobId)).thenReturn(tasks);
store.addOrUpdateLoadBalancer(association.getJobLoadBalancer(), association.getState()).await();
for (Task task : tasks) {
String ipAddress = task.getTaskContext().get(TaskAttributes.TASK_ATTRIBUTES_CONTAINER_IP);
LoadBalancerTarget target = new LoadBalancerTarget(loadBalancerId, task.getId(), ipAddress);
reconciler.activateCooldownFor(target, cooldownPeriodMs, TimeUnit.MILLISECONDS);
}
testScheduler.triggerActions();
subscriber.assertNotCompleted().assertNoValues();
// no updates while cooldown is active in the first iteration
awaitReconciliationRuns(1);
subscriber.assertNotCompleted().assertNoValues();
awaitReconciliationRuns(4);
subscriber.assertNotCompleted().assertValueCount(5);
subscriber.getOnNextEvents().forEach(update -> {
assertThat(update.getState()).isEqualTo(LoadBalancerTarget.State.REGISTERED);
assertThat(update.getPriority()).isEqualTo(Priority.LOW);
assertThat(update.getLoadBalancerId()).isEqualTo(loadBalancerId);
});
// try again since it still can't see updates applied on the connector
awaitReconciliationRuns(1);
subscriber.assertNotCompleted().assertValueCount(10);
}
Aggregations