Skip to content
New issue

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

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

Already on GitHub? # to your account

Create thread pool that blocks when full #2186

Merged
merged 2 commits into from
Mar 12, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,16 @@ public synchronized ExecutorService get(String name, int maxSize) {
return service;
}

public synchronized ExecutorAndQueue get(String name, int maxSize, int queueSize) {
public synchronized ExecutorAndQueue get(
String name,
int maxSize,
int queueSize,
boolean blockWhenFull
) {
checkState(!stopped.get(), "already stopped");
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(queueSize);
LinkedBlockingQueue<Runnable> queue = blockWhenFull
? new ThreadPoolQueue(queueSize)
: new LinkedBlockingQueue<>(queueSize);
ExecutorService service = new ThreadPoolExecutor(
maxSize,
maxSize,
Expand Down Expand Up @@ -102,4 +109,21 @@ public void stop() throws Exception {
}
}
}

public static final class ThreadPoolQueue extends LinkedBlockingQueue<Runnable> {

public ThreadPoolQueue(int capacity) {
super(capacity);
}

@Override
public boolean offer(Runnable e) {
try {
put(e);
} catch (InterruptedException e1) {
return false;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public SingularityMesosStatusUpdateHandler(
threadPoolFactory.get(
"status-updates",
configuration.getMesosConfiguration().getStatusUpdateConcurrencyLimit(),
configuration.getMesosConfiguration().getMaxStatusUpdateQueueSize()
configuration.getMesosConfiguration().getMaxStatusUpdateQueueSize(),
true
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.hubspot.singularity.helpers;

import com.hubspot.singularity.SingularityManagedThreadPoolFactory;
import com.hubspot.singularity.async.ExecutorAndQueue;
import com.hubspot.singularity.config.SingularityConfiguration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.RejectedExecutionException;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SingularityBlockingThreadPoolTest {

@Test
public void testBoundedQueueBlocksWhenFull() {
SingularityManagedThreadPoolFactory threadPoolFactory = new SingularityManagedThreadPoolFactory(
new SingularityConfiguration()
);
Assertions.assertThrows(
RejectedExecutionException.class,
() -> {
ExecutorAndQueue executorAndQueue = threadPoolFactory.get("test", 2, 5, false);
IntStream
.range(0, 10)
.forEach(
i ->
executorAndQueue
.getExecutorService()
.submit(
() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// didn't see that...
}
}
)
);
}
);

Assertions.assertDoesNotThrow(
() -> {
ExecutorAndQueue executorAndQueue = threadPoolFactory.get("test", 2, 5, true);
IntStream
.range(0, 10)
.forEach(
i ->
executorAndQueue
.getExecutorService()
.submit(
() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// didn't see that...
}
}
)
);
}
);
}

@Test
public void testBoundedQueueBlocksWhenFullForCompletableFutures() {
SingularityManagedThreadPoolFactory threadPoolFactory = new SingularityManagedThreadPoolFactory(
new SingularityConfiguration()
);
Assertions.assertThrows(
RejectedExecutionException.class,
() -> {
ExecutorAndQueue executorAndQueue = threadPoolFactory.get("test", 2, 5, false);
IntStream
.range(0, 10)
.forEach(
i ->
CompletableFuture.runAsync(
() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// didn't see that...
}
},
executorAndQueue.getExecutorService()
)
);
}
);

Assertions.assertDoesNotThrow(
() -> {
ExecutorAndQueue executorAndQueue = threadPoolFactory.get("test", 2, 5, true);
IntStream
.range(0, 10)
.forEach(
i ->
CompletableFuture.runAsync(
() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
// didn't see that...
}
},
executorAndQueue.getExecutorService()
)
);
}
);
}
}