• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #272

21 Jun 2024 08:17PM UTC coverage: 77.548% (+0.04%) from 77.506%
#272

push

github

web-flow
Resource based tuner (#2110)

275 of 338 new or added lines in 11 files covered. (81.36%)

12 existing lines in 5 files now uncovered.

19522 of 25174 relevant lines covered (77.55%)

0.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.35
/temporal-sdk/src/main/java/io/temporal/worker/tuning/ResourceBasedTuner.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.worker.tuning;
22

23
import io.temporal.common.Experimental;
24
import java.time.Duration;
25
import javax.annotation.Nonnull;
26

27
/** A {@link WorkerTuner} that attempts to allocate slots based on available system resources. */
28
@Experimental
29
public class ResourceBasedTuner implements WorkerTuner {
30
  public static final ResourceBasedSlotOptions DEFAULT_WORKFLOW_SLOT_OPTIONS =
31
      ResourceBasedSlotOptions.newBuilder()
1✔
32
          .setMinimumSlots(5)
1✔
33
          .setMaximumSlots(500)
1✔
34
          .setRampThrottle(Duration.ZERO)
1✔
35
          .build();
1✔
36
  public static final ResourceBasedSlotOptions DEFAULT_ACTIVITY_SLOT_OPTIONS =
1✔
37
      ResourceBasedSlotOptions.newBuilder()
1✔
38
          .setMinimumSlots(1)
1✔
39
          .setMaximumSlots(1000)
1✔
40
          .setRampThrottle(Duration.ofMillis(50))
1✔
41
          .build();
1✔
42

43
  private final ResourceBasedController controller;
44
  private final ResourceBasedSlotOptions workflowSlotOptions;
45
  private final ResourceBasedSlotOptions activitySlotOptions;
46
  private final ResourceBasedSlotOptions localActivitySlotOptions;
47

48
  public static Builder newBuilder() {
49
    return new Builder();
1✔
50
  }
51

52
  public static final class Builder {
53
    private ResourceBasedControllerOptions controllerOptions;
54
    private @Nonnull ResourceBasedSlotOptions workflowSlotOptions = DEFAULT_WORKFLOW_SLOT_OPTIONS;
1✔
55
    private @Nonnull ResourceBasedSlotOptions activitySlotOptions = DEFAULT_ACTIVITY_SLOT_OPTIONS;
1✔
56
    private @Nonnull ResourceBasedSlotOptions localActivitySlotOptions =
1✔
57
        DEFAULT_ACTIVITY_SLOT_OPTIONS;
58

59
    private Builder() {}
1✔
60

61
    public Builder setControllerOptions(ResourceBasedControllerOptions controllerOptions) {
62
      this.controllerOptions = controllerOptions;
1✔
63
      return this;
1✔
64
    }
65

66
    /**
67
     * Set the slot options for workflow tasks. Has no effect after the worker using this tuner
68
     * starts.
69
     *
70
     * <p>Defaults to minimum 5 slots, maximum 500 slots, and no ramp throttle.
71
     */
72
    public Builder setWorkflowSlotOptions(@Nonnull ResourceBasedSlotOptions workflowSlotOptions) {
NEW
73
      this.workflowSlotOptions = workflowSlotOptions;
×
NEW
74
      return this;
×
75
    }
76

77
    /**
78
     * Set the slot options for activity tasks. Has no effect after the worker using this tuner
79
     * starts.
80
     *
81
     * <p>Defaults to minimum 1 slot, maximum 1000 slots, and 50ms ramp throttle.
82
     */
83
    public Builder setActivitySlotOptions(@Nonnull ResourceBasedSlotOptions activitySlotOptions) {
NEW
84
      this.activitySlotOptions = activitySlotOptions;
×
NEW
85
      return this;
×
86
    }
87

88
    /**
89
     * Set the slot options for local activity tasks. Has no effect after the worker using this
90
     * tuner starts.
91
     *
92
     * <p>Defaults to minimum 1 slot, maximum 1000 slots, and 50ms ramp throttle.
93
     */
94
    public Builder setLocalActivitySlotOptions(
95
        @Nonnull ResourceBasedSlotOptions localActivitySlotOptions) {
NEW
96
      this.localActivitySlotOptions = localActivitySlotOptions;
×
NEW
97
      return this;
×
98
    }
99

100
    public ResourceBasedTuner build() {
101
      return new ResourceBasedTuner(
1✔
102
          controllerOptions, workflowSlotOptions, activitySlotOptions, localActivitySlotOptions);
103
    }
104
  }
105

106
  /**
107
   * @param controllerOptions options for the {@link ResourceBasedController} used by this tuner
108
   */
109
  public ResourceBasedTuner(
110
      ResourceBasedControllerOptions controllerOptions,
111
      ResourceBasedSlotOptions workflowSlotOptions,
112
      ResourceBasedSlotOptions activitySlotOptions,
113
      ResourceBasedSlotOptions localActivitySlotOptions) {
1✔
114
    this.controller = ResourceBasedController.newSystemInfoController(controllerOptions);
1✔
115
    this.workflowSlotOptions = workflowSlotOptions;
1✔
116
    this.activitySlotOptions = activitySlotOptions;
1✔
117
    this.localActivitySlotOptions = localActivitySlotOptions;
1✔
118
  }
1✔
119

120
  @Nonnull
121
  @Override
122
  public SlotSupplier<WorkflowSlotInfo> getWorkflowTaskSlotSupplier() {
123
    return ResourceBasedSlotSupplier.createForWorkflow(controller, workflowSlotOptions);
1✔
124
  }
125

126
  @Nonnull
127
  @Override
128
  public SlotSupplier<ActivitySlotInfo> getActivityTaskSlotSupplier() {
129
    return ResourceBasedSlotSupplier.createForActivity(controller, activitySlotOptions);
1✔
130
  }
131

132
  @Nonnull
133
  @Override
134
  public SlotSupplier<LocalActivitySlotInfo> getLocalActivitySlotSupplier() {
135
    return ResourceBasedSlotSupplier.createForLocalActivity(controller, localActivitySlotOptions);
1✔
136
  }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc