use of com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy in project rest.li by linkedin.
the class TestDarkClusterStrategyFactory method testNoChangeStrategyOnNotification.
@Test
public void testNoChangeStrategyOnNotification() {
DarkClusterConfig darkClusterConfig1 = createRelativeTrafficMultiplierConfig(0.5f);
DarkClusterConfig darkClusterConfig2 = createRelativeTrafficMultiplierConfig(1.0f);
_clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1);
_clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
DarkClusterStrategy strategy = _strategyFactory.get(DARK_CLUSTER_NAME);
Assert.assertTrue(strategy instanceof RelativeTrafficMultiplierDarkClusterStrategy);
Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy).getMultiplier(), 0.5f, "expected 0.5f multiplier");
// update Strategy, then simulating a notification on the dark cluster.
_clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig2);
_clusterInfoProvider.notifyListenersClusterAdded(DARK_CLUSTER_NAME);
// Nothing should have been changed, since we should be ignoring dark cluster changes. (strategy-impacting changes are all captured
// in the source cluster data)
DarkClusterStrategy strategy2 = _strategyFactory.get(DARK_CLUSTER_NAME);
Assert.assertTrue(strategy2 instanceof RelativeTrafficMultiplierDarkClusterStrategy);
Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy2).getMultiplier(), 0.5f, "expected 0.5f multiplier");
}
use of com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy in project rest.li by linkedin.
the class TestDarkClusterStrategyFactory method testStrategyRaceCondition.
@Test
public void testStrategyRaceCondition() {
int noopStrategyCount = 0;
DarkClusterConfig darkClusterConfig1 = createRelativeTrafficMultiplierConfig(0.5f);
_clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig1);
_clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
DarkClusterStrategy strategy = _strategyFactory.get(DARK_CLUSTER_NAME);
Assert.assertTrue(strategy instanceof RelativeTrafficMultiplierDarkClusterStrategy);
Assert.assertEquals(((RelativeTrafficMultiplierDarkClusterStrategy) strategy).getMultiplier(), 0.5f, "expected 0.5f multiplier");
// this was registered after DarkClusterStrategyFactoryImpl registered it's clusterListener.
_clusterInfoProvider.registerClusterListener(new DeletingClusterListener(_clusterInfoProvider));
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
final CountDownLatch latch = new CountDownLatch(1);
try {
scheduledExecutorService.scheduleAtFixedRate(() -> {
_clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
latch.countDown();
}, 0, 1, TimeUnit.MILLISECONDS);
if (!latch.await(30, TimeUnit.SECONDS)) {
fail("unable to execute task on executor");
}
for (int i = 0; i < 100000; i++) {
strategy = _strategyFactory.get(DARK_CLUSTER_NAME);
// verified that this will catch race conditions, saw it happen 9/100k times.
Assert.assertNotNull(strategy, "null at iteration: " + i);
if (strategy instanceof NoOpDarkClusterStrategy) {
noopStrategyCount++;
}
}
System.out.println("noopStrategyCount: " + noopStrategyCount);
} catch (InterruptedException ie) {
fail("got interrupted exception", ie);
} finally {
scheduledExecutorService.shutdown();
}
}
use of com.linkedin.darkcluster.impl.RelativeTrafficMultiplierDarkClusterStrategy in project rest.li by linkedin.
the class TestRelativeTrafficMultiplierDarkClusterStrategy method testStrategy.
@Test(dataProvider = "multiplierKeys")
public void testStrategy(int numIterations, float multiplier, int numSourceInstances, int numDarkInstances) {
DarkClusterDispatcher darkClusterDispatcher = new DefaultDarkClusterDispatcher(new MockClient(false));
BaseDarkClusterDispatcherImpl baseDispatcher = new BaseDarkClusterDispatcherImpl(DARK_CLUSTER_NAME, darkClusterDispatcher, new DoNothingNotifier(), new CountingVerifierManager());
MockClusterInfoProvider mockClusterInfoProvider = new MockClusterInfoProvider();
mockClusterInfoProvider.putHttpsClusterCount(DARK_CLUSTER_NAME, numDarkInstances);
mockClusterInfoProvider.putHttpsClusterCount(SOURCE_CLUSTER_NAME, numSourceInstances);
RelativeTrafficMultiplierDarkClusterStrategy strategy = new RelativeTrafficMultiplierDarkClusterStrategy(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, multiplier, baseDispatcher, new DoNothingNotifier(), mockClusterInfoProvider, new Random(SEED));
for (int i = 0; i < numIterations; i++) {
RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("foo")).build();
strategy.handleRequest(dummyRestRequest, dummyRestRequest, new RequestContext());
}
int expectedCount = (int) (numIterations * multiplier * numDarkInstances) / (numSourceInstances);
int actualCount = baseDispatcher.getRequestCount();
Assert.assertEquals(actualCount, expectedCount, expectedCount * ERR_PCT, "count not within expected range");
}
Aggregations