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

Improve job pool configurations #1746

Merged
merged 2 commits into from
Nov 12, 2024
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 @@ -64,16 +64,20 @@
import com.pinterest.teletraan.worker.MetricsEmitter;
import com.pinterest.teletraan.worker.SimpleAgentJanitor;
import com.pinterest.teletraan.worker.StateTransitioner;
import io.dropwizard.setup.Environment;
import io.dropwizard.util.Duration;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.TimeUnit;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.dbcp.BasicDataSource;
Expand All @@ -98,8 +102,8 @@ public class ConfigHelper {
private static final int DEFAULT_MAX_DAYS_TO_KEEP = 180;
private static final int DEFAULT_MAX_BUILDS_TO_KEEP = 1000;

public static TeletraanServiceContext setupContext(TeletraanServiceConfiguration configuration)
throws Exception {
public static TeletraanServiceContext setupContext(
TeletraanServiceConfiguration configuration, Environment environment) throws Exception {
TeletraanServiceContext context = new TeletraanServiceContext();

BasicDataSource dataSource = configuration.getDataSourceFactory().build();
Expand Down Expand Up @@ -218,20 +222,20 @@ public static TeletraanServiceContext setupContext(TeletraanServiceConfiguration
context.setAccountAllowList(configuration.getAccountAllowList());
}

/*
Lastly, let us create the in-process background job executor, all transient, long
running background jobs can be handled by this executor
Currently we hard coded the parameters as:

corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set
maximumPoolSize - the maximum number of threads to allow in the pool
keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
unit - the time unit for the keepAliveTime argument
workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
*/
// TODO make the thread configurable
int poolSize = Runtime.getRuntime().availableProcessors();
String jobPoolName = "jobPool";
ExecutorService jobPool =
new ThreadPoolExecutor(1, 10, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
environment
.lifecycle()
.executorService(jobPoolName)
.minThreads(poolSize * 4)
.maxThreads(poolSize * 8)
.keepAliveTime(Duration.seconds(30))
.workQueue(new ArrayBlockingQueue<>(poolSize * 5000, false))
.shutdownTime(Duration.seconds(30))
.rejectedExecutionHandler(new AbortPolicy())
.build();
new ExecutorServiceMetrics(jobPool, jobPoolName, null).bindTo(Metrics.globalRegistry);
tylerwowen marked this conversation as resolved.
Show resolved Hide resolved
context.setJobPool(jobPool);

context.setDeployBoardUrlPrefix(configuration.getSystemFactory().getDashboardUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void initialize(Bootstrap<TeletraanServiceConfiguration> bootstrap) {
@Override
public void run(TeletraanServiceConfiguration configuration, Environment environment)
throws Exception {
TeletraanServiceContext context = ConfigHelper.setupContext(configuration);
TeletraanServiceContext context = ConfigHelper.setupContext(configuration, environment);
environment.jersey().register(context);
environment
.jersey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void initialize(Bootstrap<TeletraanServiceConfiguration> bootstrap) {
@Override
public void run(TeletraanServiceConfiguration configuration, Environment environment)
throws Exception {
TeletraanServiceContext context = ConfigHelper.setupContext(configuration);
TeletraanServiceContext context = ConfigHelper.setupContext(configuration, environment);

environment.jersey().register(context);
environment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void initialize(Bootstrap<TeletraanServiceConfiguration> bootstrap) {
@Override
public void run(TeletraanServiceConfiguration configuration, Environment environment)
throws Exception {
TeletraanServiceContext context = ConfigHelper.setupContext(configuration);
TeletraanServiceContext context = ConfigHelper.setupContext(configuration, environment);
ConfigHelper.scheduleWorkers(configuration, context);
environment.healthChecks().register("generic", new WorkerHealthCheck(context));
}
Expand Down
Loading