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

temporalio / sdk-java / #175

pending completion
#175

push

github-actions

web-flow
Worker / Build Id versioning (#1786)

Implement new worker build id based versioning feature

236 of 236 new or added lines in 24 files covered. (100.0%)

18343 of 23697 relevant lines covered (77.41%)

0.81 hits per line

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

77.78
/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 java.util.Objects;
37
import java.util.concurrent.Semaphore;
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 Semaphore pollSemaphore;
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
      Semaphore pollSemaphore,
61
      @Nonnull Scope metricsScope,
62
      @Nonnull Supplier<GetSystemInfoResponse.Capabilities> serverCapabilities) {
1✔
63
    this.service = Objects.requireNonNull(service);
1✔
64
    this.pollSemaphore = pollSemaphore;
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
    boolean isSuccessful = false;
1✔
96

97
    try {
98
      pollSemaphore.acquire();
1✔
99
    } catch (InterruptedException e) {
1✔
100
      Thread.currentThread().interrupt();
1✔
101
      return null;
1✔
102
    }
1✔
103

104
    try {
105
      response =
1✔
106
          service
107
              .blockingStub()
1✔
108
              .withOption(METRICS_TAGS_CALL_OPTIONS_KEY, metricsScope)
1✔
109
              .pollActivityTaskQueue(pollRequest);
1✔
110

111
      if (response == null || response.getTaskToken().isEmpty()) {
1✔
112
        metricsScope.counter(MetricsType.ACTIVITY_POLL_NO_TASK_COUNTER).inc(1);
1✔
113
        return null;
1✔
114
      }
115
      metricsScope
1✔
116
          .timer(MetricsType.ACTIVITY_SCHEDULE_TO_START_LATENCY)
1✔
117
          .record(
1✔
118
              ProtobufTimeUtils.toM3Duration(
1✔
119
                  response.getStartedTime(), response.getCurrentAttemptScheduledTime()));
1✔
120
      isSuccessful = true;
1✔
121
      return new ActivityTask(response, pollSemaphore::release);
1✔
122
    } finally {
123
      if (!isSuccessful) pollSemaphore.release();
1✔
124
    }
125
  }
126
}
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