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

temporalio / sdk-java / #284

23 Jul 2024 10:09PM UTC coverage: 77.304% (-0.06%) from 77.364%
#284

push

github

web-flow
Reintroduce slot supplier & add many tests (#2143)

593 of 752 new or added lines in 37 files covered. (78.86%)

22 existing lines in 10 files now uncovered.

19554 of 25295 relevant lines covered (77.3%)

0.77 hits per line

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

75.47
/temporal-sdk/src/main/java/io/temporal/internal/worker/ActivityPollTask.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.internal.worker;
22

23
import static io.temporal.serviceclient.MetricsTag.METRICS_TAGS_CALL_OPTIONS_KEY;
24

25
import com.google.protobuf.DoubleValue;
26
import com.uber.m3.tally.Scope;
27
import io.temporal.api.common.v1.WorkerVersionCapabilities;
28
import io.temporal.api.taskqueue.v1.TaskQueue;
29
import io.temporal.api.taskqueue.v1.TaskQueueMetadata;
30
import io.temporal.api.workflowservice.v1.GetSystemInfoResponse;
31
import io.temporal.api.workflowservice.v1.PollActivityTaskQueueRequest;
32
import io.temporal.api.workflowservice.v1.PollActivityTaskQueueResponse;
33
import io.temporal.internal.common.ProtobufTimeUtils;
34
import io.temporal.serviceclient.WorkflowServiceStubs;
35
import io.temporal.worker.MetricsType;
36
import io.temporal.worker.tuning.*;
37
import java.util.Objects;
38
import java.util.function.Supplier;
39
import javax.annotation.Nonnull;
40
import javax.annotation.Nullable;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

44
final class ActivityPollTask implements Poller.PollTask<ActivityTask> {
45
  private static final Logger log = LoggerFactory.getLogger(ActivityPollTask.class);
1✔
46

47
  private final WorkflowServiceStubs service;
48
  private final TrackingSlotSupplier<ActivitySlotInfo> slotSupplier;
49
  private final Scope metricsScope;
50
  private final PollActivityTaskQueueRequest pollRequest;
51

52
  public ActivityPollTask(
53
      @Nonnull WorkflowServiceStubs service,
54
      @Nonnull String namespace,
55
      @Nonnull String taskQueue,
56
      @Nonnull String identity,
57
      @Nullable String buildId,
58
      boolean useBuildIdForVersioning,
59
      double activitiesPerSecond,
60
      @Nonnull TrackingSlotSupplier<ActivitySlotInfo> slotSupplier,
61
      @Nonnull Scope metricsScope,
62
      @Nonnull Supplier<GetSystemInfoResponse.Capabilities> serverCapabilities) {
1✔
63
    this.service = Objects.requireNonNull(service);
1✔
64
    this.slotSupplier = slotSupplier;
1✔
65
    this.metricsScope = Objects.requireNonNull(metricsScope);
1✔
66

67
    PollActivityTaskQueueRequest.Builder pollRequest =
68
        PollActivityTaskQueueRequest.newBuilder()
1✔
69
            .setNamespace(namespace)
1✔
70
            .setIdentity(identity)
1✔
71
            .setTaskQueue(TaskQueue.newBuilder().setName(taskQueue));
1✔
72
    if (activitiesPerSecond > 0) {
1✔
73
      pollRequest.setTaskQueueMetadata(
×
74
          TaskQueueMetadata.newBuilder()
×
75
              .setMaxTasksPerSecond(DoubleValue.newBuilder().setValue(activitiesPerSecond).build())
×
76
              .build());
×
77
    }
78

79
    if (serverCapabilities.get().getBuildIdBasedVersioning()) {
1✔
80
      pollRequest.setWorkerVersionCapabilities(
×
81
          WorkerVersionCapabilities.newBuilder()
×
82
              .setBuildId(buildId)
×
83
              .setUseVersioning(useBuildIdForVersioning)
×
84
              .build());
×
85
    }
86
    this.pollRequest = pollRequest.build();
1✔
87
  }
1✔
88

89
  @Override
90
  public ActivityTask poll() {
91
    if (log.isTraceEnabled()) {
1✔
92
      log.trace("poll request begin: " + pollRequest);
×
93
    }
94
    PollActivityTaskQueueResponse response;
95
    SlotPermit permit;
96
    boolean isSuccessful = false;
1✔
97

98
    try {
99
      permit =
1✔
100
          slotSupplier.reserveSlot(
1✔
101
              new SlotReservationData(
102
                  pollRequest.getTaskQueue().getName(),
1✔
103
                  pollRequest.getIdentity(),
1✔
104
                  pollRequest.getWorkerVersionCapabilities().getBuildId()));
1✔
105
    } catch (InterruptedException e) {
1✔
106
      Thread.currentThread().interrupt();
1✔
107
      return null;
1✔
NEW
108
    } catch (Exception e) {
×
NEW
109
      log.warn("Error while trying to reserve a slot for an activity", e.getCause());
×
NEW
110
      return null;
×
111
    }
1✔
112

113
    try {
114
      response =
1✔
115
          service
116
              .blockingStub()
1✔
117
              .withOption(METRICS_TAGS_CALL_OPTIONS_KEY, metricsScope)
1✔
118
              .pollActivityTaskQueue(pollRequest);
1✔
119

120
      if (response == null || response.getTaskToken().isEmpty()) {
1✔
121
        metricsScope.counter(MetricsType.ACTIVITY_POLL_NO_TASK_COUNTER).inc(1);
1✔
122
        return null;
1✔
123
      }
124
      metricsScope
1✔
125
          .timer(MetricsType.ACTIVITY_SCHEDULE_TO_START_LATENCY)
1✔
126
          .record(
1✔
127
              ProtobufTimeUtils.toM3Duration(
1✔
128
                  response.getStartedTime(), response.getCurrentAttemptScheduledTime()));
1✔
129
      isSuccessful = true;
1✔
130
      return new ActivityTask(
1✔
131
          response,
132
          permit,
133
          () -> slotSupplier.releaseSlot(SlotReleaseReason.taskComplete(), permit));
1✔
134
    } finally {
135
      if (!isSuccessful) slotSupplier.releaseSlot(SlotReleaseReason.neverUsed(), permit);
1✔
136
    }
137
  }
138
}
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

© 2026 Coveralls, Inc