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

Implement WorkerActivitiesPerSecond #557

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -39,6 +39,8 @@ public interface TaskHandler<TT> {
private final String taskList;
private final TaskHandler<T> handler;

private Throttler taskRateThrottler;

PollTaskExecutor(
String domain, String taskList, SingleWorkerOptions options, TaskHandler<T> handler) {
this.domain = domain;
Expand All @@ -59,6 +61,10 @@ public interface TaskHandler<TT> {
options.getPollerOptions().getPollThreadNamePrefix().replaceFirst("Poller", "Executor"),
options.getPollerOptions().getUncaughtExceptionHandler()));
taskExecutor.setRejectedExecutionHandler(new BlockCallerPolicy());

if (options.getWorkerActivitiesPerSecond() > 0.0) {
taskRateThrottler = new Throttler("PollTaskExecutor", options.getWorkerActivitiesPerSecond());
}
}

@Override
Expand All @@ -68,6 +74,9 @@ public void process(T task) {
MDC.put(LoggerTag.DOMAIN, domain);
MDC.put(LoggerTag.TASK_LIST, taskList);
try {
if (taskRateThrottler != null) {
taskRateThrottler.throttle();
}
handler.handle(task);
} catch (Throwable ee) {
options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public final class SingleWorkerOptions {

public static final class Builder {

private double workerActivitiesPerSecond;
private String identity;
private DataConverter dataConverter;
private int taskExecutorThreadPoolSize = 100;
Expand All @@ -47,6 +48,7 @@ public static final class Builder {
public Builder() {}

public Builder(SingleWorkerOptions options) {
this.workerActivitiesPerSecond = options.getWorkerActivitiesPerSecond();
this.identity = options.getIdentity();
this.dataConverter = options.getDataConverter();
this.pollerOptions = options.getPollerOptions();
Expand All @@ -59,6 +61,11 @@ public Builder(SingleWorkerOptions options) {
this.contextPropagators = options.getContextPropagators();
}

public Builder setWorkerActivitiesPerSecond(double workerActivitiesPerSecond) {
this.workerActivitiesPerSecond = workerActivitiesPerSecond;
return this;
}

public Builder setIdentity(String identity) {
this.identity = identity;
return this;
Expand Down Expand Up @@ -137,6 +144,7 @@ public SingleWorkerOptions build() {
}

return new SingleWorkerOptions(
workerActivitiesPerSecond,
identity,
dataConverter,
taskExecutorThreadPoolSize,
Expand All @@ -150,6 +158,7 @@ public SingleWorkerOptions build() {
}
}

private final double workerActivitiesPerSecond;
private final String identity;
private final DataConverter dataConverter;
private final int taskExecutorThreadPoolSize;
Expand All @@ -162,6 +171,7 @@ public SingleWorkerOptions build() {
private List<ContextPropagator> contextPropagators;

private SingleWorkerOptions(
double workerActivitiesPerSecond,
String identity,
DataConverter dataConverter,
int taskExecutorThreadPoolSize,
Expand All @@ -172,6 +182,7 @@ private SingleWorkerOptions(
Scope metricsScope,
boolean enableLoggingInReplay,
List<ContextPropagator> contextPropagators) {
this.workerActivitiesPerSecond = workerActivitiesPerSecond;
this.identity = identity;
this.dataConverter = dataConverter;
this.taskExecutorThreadPoolSize = taskExecutorThreadPoolSize;
Expand All @@ -184,6 +195,10 @@ private SingleWorkerOptions(
this.contextPropagators = contextPropagators;
}

double getWorkerActivitiesPerSecond() {
return workerActivitiesPerSecond;
}

public String getIdentity() {
return identity;
}
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/com/uber/cadence/internal/worker/Throttler.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,30 @@ final class Throttler {

private final long rateIntervalMilliseconds;

/** Default 1s interval when interval per message is less than 1ms */
private static final long defaultRateIntervalMilliseconds = 1000L;

private long overslept;

private static long calculateIntervalMillisecondsPerMessage(double maxRatePerSecond) {
return (long) (1 / maxRatePerSecond * 1000.0);
}

/**
* Construct throttler.
*
* @param name Human readable name of the resource being throttled. Used for logging only.
* @param maxRatePerSecond maximum rate allowed
*/
public Throttler(String name, double maxRatePerSecond) {
this(
name,
maxRatePerSecond,
calculateIntervalMillisecondsPerMessage(maxRatePerSecond) <= 0L
? defaultRateIntervalMilliseconds
: calculateIntervalMillisecondsPerMessage(maxRatePerSecond));
}

/**
* Construct throttler.
*
Expand Down Expand Up @@ -69,7 +91,7 @@ public synchronized void setMaxRatePerSecond(double maxRatePerSecond) {
int maxMessagesPerRateInterval = (int) (maxRatePerSecond * rateIntervalMilliseconds / 1000);
if (maxMessagesPerRateInterval == 0) {
maxMessagesPerRateInterval = 1;
rateInterval = (long) (1.0 / maxRatePerSecond * 1000.0);
rateInterval = calculateIntervalMillisecondsPerMessage(maxRatePerSecond);
} else {
rateInterval = rateIntervalMilliseconds;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/uber/cadence/worker/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private static SingleWorkerOptions toActivityOptions(
.put(MetricsTag.TASK_LIST, taskList)
.build();
return new SingleWorkerOptions.Builder()
.setWorkerActivitiesPerSecond(options.getWorkerActivitiesPerSecond())
.setDataConverter(options.getDataConverter())
.setIdentity(options.getIdentity())
.setPollerOptions(options.getActivityPollerOptions())
Expand Down