Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pools] Allow adjustment by 1 #34

Merged
merged 1 commit into from
Nov 27, 2023
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
2 changes: 1 addition & 1 deletion src/io/aleph/dirigiste/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private void adjust() {
q.drop();
}
q.cleanup();
} else if (n > 1) {
} else if (n > 0) {
for (int i = 0; i < n; i++) {
upward.add(entry.getKey());
}
Expand Down
27 changes: 27 additions & 0 deletions test/java/io/aleph/dirigiste/PoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ public void testPoolWithSimpleUtilizationExecutor() throws InterruptedException
assertNull(pool._queues.get(KEY));
}

@Test
public void testPoolIncByOne() throws InterruptedException {
Pool<Key,Value> pool = newPool(incByOneController(), generator(), 1 ,10, 100);
pool.acquire(KEY);
// Let's wait a bit it has been adjusted
Thread.sleep(150);
assertEquals(pool._queues.get(KEY).objects.get(), 2);
}

@Test
public void testPoolOnAHighlyConcurrentEnvironment() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(30);
Expand Down Expand Up @@ -329,6 +338,10 @@ private Pool<Key, Value> newPool(Controller<Key> controller, Generator<Key, Valu
return new Pool<>(generator, controller, maxQueueSize, 10, 1000, TimeUnit.MICROSECONDS);
}

private Pool<Key, Value> newPool(Controller<Key> controller, Generator<Key, Value> generator, int maxQueueSize, int samplePeriod, int controlPeriod) {
return new Pool<>(generator, controller, maxQueueSize, samplePeriod, controlPeriod, TimeUnit.MILLISECONDS);
}

private double getUtilization(Pool<Key, Value> pool) {
return pool.getUtilization(pool.queue(KEY).availableObjectsCount(), pool.queue(KEY).getQueueLength(), pool.queue(KEY).objects.get());
}
Expand Down Expand Up @@ -376,6 +389,20 @@ public void destroy(Key key, Value val) {
}

private Controller<Key> noopController() {
return new Controller<Key>() {
@Override
public boolean shouldIncrement(Key key, int objectsForKey, int totalObjects) {
return true;
}

@Override
public Map<Key, Integer> adjustment(Map<Key, Stats> stats) {
return stats.entrySet().stream().collect(toMap(Map.Entry::getKey, __ -> 0));
}
};
}

private Controller<Key> incByOneController() {
return new Controller<Key>() {
@Override
public boolean shouldIncrement(Key key, int objectsForKey, int totalObjects) {
Expand Down