Skip to content

Commit

Permalink
[pools] Allow adjustment by 1
Browse files Browse the repository at this point in the history
I assume this is not intentional – pool will not adjust upwards if the adjustment value for a key is exactly 1. Not critical, but slightly annoying.

Co-authored-by: alexander-yakushev <alex@bytopia.org>
  • Loading branch information
arnaudgeiser and alexander-yakushev committed Aug 30, 2022
1 parent 82da876 commit ea58a68
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/io/aleph/dirigiste/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,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 @@ -228,6 +228,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 testPoolOnAConcurrentEnvironment() throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(30);
Expand Down Expand Up @@ -296,6 +305,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 @@ -343,6 +356,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

0 comments on commit ea58a68

Please sign in to comment.