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

temporalio / sdk-java / #333

16 Oct 2024 07:28PM UTC coverage: 78.65% (+0.6%) from 78.085%
#333

push

github

web-flow
Fix code coverage (#2275)

22670 of 28824 relevant lines covered (78.65%)

0.79 hits per line

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

81.82
/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 =
37
      ResourceBasedSlotOptions.newBuilder()
1✔
38
          .setMinimumSlots(1)
1✔
39
          .setMaximumSlots(1000)
1✔
40
          .setRampThrottle(Duration.ofMillis(50))
1✔
41
          .build();
1✔
42
  public static final ResourceBasedSlotOptions DEFAULT_NEXUS_SLOT_OPTIONS =
1✔
43
      ResourceBasedSlotOptions.newBuilder()
1✔
44
          .setMinimumSlots(1)
1✔
45
          .setMaximumSlots(1000)
1✔
46
          .setRampThrottle(Duration.ofMillis(50))
1✔
47
          .build();
1✔
48

49
  private final ResourceBasedController controller;
50
  private final ResourceBasedSlotOptions workflowSlotOptions;
51
  private final ResourceBasedSlotOptions activitySlotOptions;
52
  private final ResourceBasedSlotOptions localActivitySlotOptions;
53
  private final ResourceBasedSlotOptions nexusSlotOptions;
54

55
  public static Builder newBuilder() {
56
    return new Builder();
1✔
57
  }
58

59
  public static final class Builder {
60
    private ResourceBasedControllerOptions controllerOptions;
61
    private @Nonnull ResourceBasedSlotOptions workflowSlotOptions = DEFAULT_WORKFLOW_SLOT_OPTIONS;
1✔
62
    private @Nonnull ResourceBasedSlotOptions activitySlotOptions = DEFAULT_ACTIVITY_SLOT_OPTIONS;
1✔
63
    private @Nonnull ResourceBasedSlotOptions localActivitySlotOptions =
1✔
64
        DEFAULT_ACTIVITY_SLOT_OPTIONS;
65
    private @Nonnull ResourceBasedSlotOptions nexusSlotOptions = DEFAULT_NEXUS_SLOT_OPTIONS;
1✔
66

67
    private Builder() {}
1✔
68

69
    public Builder setControllerOptions(ResourceBasedControllerOptions controllerOptions) {
70
      this.controllerOptions = controllerOptions;
1✔
71
      return this;
1✔
72
    }
73

74
    /**
75
     * Set the slot options for workflow tasks. Has no effect after the worker using this tuner
76
     * starts.
77
     *
78
     * <p>Defaults to minimum 5 slots, maximum 500 slots, and no ramp throttle.
79
     */
80
    public Builder setWorkflowSlotOptions(@Nonnull ResourceBasedSlotOptions workflowSlotOptions) {
81
      this.workflowSlotOptions = workflowSlotOptions;
×
82
      return this;
×
83
    }
84

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

96
    /**
97
     * Set the slot options for local activity tasks. Has no effect after the worker using this
98
     * tuner starts.
99
     *
100
     * <p>Defaults to minimum 1 slot, maximum 1000 slots, and 50ms ramp throttle.
101
     */
102
    public Builder setLocalActivitySlotOptions(
103
        @Nonnull ResourceBasedSlotOptions localActivitySlotOptions) {
104
      this.localActivitySlotOptions = localActivitySlotOptions;
×
105
      return this;
×
106
    }
107

108
    /**
109
     * Set the slot options for nexus tasks. Has no effect after the worker using this tuner starts.
110
     *
111
     * <p>Defaults to minimum 1 slot, maximum 1000 slots, and 50ms ramp throttle.
112
     */
113
    public Builder setNexusSlotOptions(@Nonnull ResourceBasedSlotOptions nexusSlotOptions) {
114
      this.nexusSlotOptions = nexusSlotOptions;
×
115
      return this;
×
116
    }
117

118
    public ResourceBasedTuner build() {
119
      return new ResourceBasedTuner(
1✔
120
          controllerOptions,
121
          workflowSlotOptions,
122
          activitySlotOptions,
123
          localActivitySlotOptions,
124
          nexusSlotOptions);
125
    }
126
  }
127

128
  /**
129
   * @param controllerOptions options for the {@link ResourceBasedController} used by this tuner
130
   */
131
  public ResourceBasedTuner(
132
      ResourceBasedControllerOptions controllerOptions,
133
      ResourceBasedSlotOptions workflowSlotOptions,
134
      ResourceBasedSlotOptions activitySlotOptions,
135
      ResourceBasedSlotOptions localActivitySlotOptions,
136
      ResourceBasedSlotOptions nexusSlotOptions) {
1✔
137
    this.controller = ResourceBasedController.newSystemInfoController(controllerOptions);
1✔
138
    this.workflowSlotOptions = workflowSlotOptions;
1✔
139
    this.activitySlotOptions = activitySlotOptions;
1✔
140
    this.localActivitySlotOptions = localActivitySlotOptions;
1✔
141
    this.nexusSlotOptions = nexusSlotOptions;
1✔
142
  }
1✔
143

144
  @Nonnull
145
  @Override
146
  public SlotSupplier<WorkflowSlotInfo> getWorkflowTaskSlotSupplier() {
147
    return ResourceBasedSlotSupplier.createForWorkflow(controller, workflowSlotOptions);
1✔
148
  }
149

150
  @Nonnull
151
  @Override
152
  public SlotSupplier<ActivitySlotInfo> getActivityTaskSlotSupplier() {
153
    return ResourceBasedSlotSupplier.createForActivity(controller, activitySlotOptions);
1✔
154
  }
155

156
  @Nonnull
157
  @Override
158
  public SlotSupplier<LocalActivitySlotInfo> getLocalActivitySlotSupplier() {
159
    return ResourceBasedSlotSupplier.createForLocalActivity(controller, localActivitySlotOptions);
1✔
160
  }
161

162
  @Nonnull
163
  @Override
164
  public SlotSupplier<NexusSlotInfo> getNexusSlotSupplier() {
165
    return ResourceBasedSlotSupplier.createForNexus(controller, nexusSlotOptions);
1✔
166
  }
167
}
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