Skip to content

Commit

Permalink
[pools] Allow adjustment by 1
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-yakushev committed Nov 27, 2023
1 parent f59164e commit ddac712
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 @@ -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

0 comments on commit ddac712

Please sign in to comment.