• 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

95.56
/temporal-sdk/src/main/java/io/temporal/internal/client/WorkflowClientRequestFactory.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.client;
22

23
import static io.temporal.internal.common.HeaderUtils.toHeaderGrpc;
24
import static io.temporal.internal.common.SerializerUtils.toRetryPolicy;
25

26
import com.google.common.base.MoreObjects;
27
import com.google.common.base.Strings;
28
import com.google.protobuf.ByteString;
29
import io.temporal.api.common.v1.*;
30
import io.temporal.api.enums.v1.HistoryEventFilterType;
31
import io.temporal.api.taskqueue.v1.TaskQueue;
32
import io.temporal.api.workflowservice.v1.GetWorkflowExecutionHistoryRequest;
33
import io.temporal.api.workflowservice.v1.SignalWithStartWorkflowExecutionRequest;
34
import io.temporal.api.workflowservice.v1.StartWorkflowExecutionRequest;
35
import io.temporal.api.workflowservice.v1.StartWorkflowExecutionRequestOrBuilder;
36
import io.temporal.client.WorkflowClientOptions;
37
import io.temporal.client.WorkflowOptions;
38
import io.temporal.common.RetryOptions;
39
import io.temporal.common.context.ContextPropagator;
40
import io.temporal.internal.common.ProtobufTimeUtils;
41
import io.temporal.internal.common.SearchAttributesUtil;
42
import java.util.*;
43
import javax.annotation.Nonnull;
44
import javax.annotation.Nullable;
45

46
final class WorkflowClientRequestFactory {
47
  private final WorkflowClientOptions clientOptions;
48

49
  public WorkflowClientRequestFactory(WorkflowClientOptions clientOptions) {
1✔
50
    this.clientOptions = clientOptions;
1✔
51
  }
1✔
52

53
  // If you add anything new here, keep newSignalWithStartWorkflowExecutionRequest in sync
54
  @SuppressWarnings("deprecation")
55
  @Nonnull
56
  StartWorkflowExecutionRequest.Builder newStartWorkflowExecutionRequest(
57
      @Nonnull String workflowId,
58
      @Nonnull String workflowTypeName,
59
      @Nonnull io.temporal.common.interceptors.Header header,
60
      @Nonnull WorkflowOptions options,
61
      @Nullable Payloads inputArgs,
62
      @Nullable Memo memo) {
63
    StartWorkflowExecutionRequest.Builder request =
64
        StartWorkflowExecutionRequest.newBuilder()
1✔
65
            .setNamespace(clientOptions.getNamespace())
1✔
66
            .setRequestId(generateUniqueId())
1✔
67
            .setIdentity(clientOptions.getIdentity())
1✔
68
            .setWorkflowId(workflowId)
1✔
69
            .setWorkflowType(WorkflowType.newBuilder().setName(workflowTypeName))
1✔
70
            .setWorkflowRunTimeout(
1✔
71
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowRunTimeout()))
1✔
72
            .setWorkflowExecutionTimeout(
1✔
73
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowExecutionTimeout()))
1✔
74
            .setWorkflowTaskTimeout(
1✔
75
                ProtobufTimeUtils.toProtoDuration(options.getWorkflowTaskTimeout()));
1✔
76

77
    if (inputArgs != null) {
1✔
78
      request.setInput(inputArgs);
1✔
79
    }
80

81
    if (options.getWorkflowIdReusePolicy() != null) {
1✔
82
      request.setWorkflowIdReusePolicy(options.getWorkflowIdReusePolicy());
1✔
83
    }
84

85
    String taskQueue = options.getTaskQueue();
1✔
86
    if (taskQueue != null && !taskQueue.isEmpty()) {
1✔
87
      request.setTaskQueue(TaskQueue.newBuilder().setName(taskQueue).build());
1✔
88
    }
89

90
    RetryOptions retryOptions = options.getRetryOptions();
1✔
91
    if (retryOptions != null) {
1✔
92
      request.setRetryPolicy(toRetryPolicy(retryOptions));
1✔
93
    }
94

95
    if (!Strings.isNullOrEmpty(options.getCronSchedule())) {
1✔
96
      request.setCronSchedule(options.getCronSchedule());
1✔
97
    }
98

99
    if (memo != null) {
1✔
100
      request.setMemo(memo);
1✔
101
    }
102

103
    if (options.getSearchAttributes() != null && !options.getSearchAttributes().isEmpty()) {
1✔
104
      if (options.getTypedSearchAttributes() != null) {
1✔
105
        throw new IllegalArgumentException(
×
106
            "Cannot have search attributes and typed search attributes");
107
      }
108
      request.setSearchAttributes(SearchAttributesUtil.encode(options.getSearchAttributes()));
1✔
109
    } else if (options.getTypedSearchAttributes() != null) {
1✔
110
      request.setSearchAttributes(
1✔
111
          SearchAttributesUtil.encodeTyped(options.getTypedSearchAttributes()));
1✔
112
    }
113

114
    Header grpcHeader =
1✔
115
        toHeaderGrpc(header, extractContextsAndConvertToBytes(options.getContextPropagators()));
1✔
116
    request.setHeader(grpcHeader);
1✔
117

118
    return request;
1✔
119
  }
120

121
  @Nonnull
122
  SignalWithStartWorkflowExecutionRequest.Builder newSignalWithStartWorkflowExecutionRequest(
123
      @Nonnull StartWorkflowExecutionRequestOrBuilder startParameters,
124
      @Nonnull String signalName,
125
      @Nullable Payloads signalInput) {
126
    SignalWithStartWorkflowExecutionRequest.Builder request =
127
        SignalWithStartWorkflowExecutionRequest.newBuilder()
1✔
128
            .setNamespace(clientOptions.getNamespace())
1✔
129
            .setRequestId(generateUniqueId())
1✔
130
            .setIdentity(clientOptions.getIdentity())
1✔
131
            .setSignalName(signalName)
1✔
132
            .setWorkflowRunTimeout(startParameters.getWorkflowRunTimeout())
1✔
133
            .setWorkflowExecutionTimeout(startParameters.getWorkflowExecutionTimeout())
1✔
134
            .setWorkflowTaskTimeout(startParameters.getWorkflowTaskTimeout())
1✔
135
            .setWorkflowType(startParameters.getWorkflowType())
1✔
136
            .setWorkflowIdReusePolicy(startParameters.getWorkflowIdReusePolicy())
1✔
137
            .setCronSchedule(startParameters.getCronSchedule());
1✔
138

139
    String workflowId = startParameters.getWorkflowId();
1✔
140
    if (workflowId.isEmpty()) {
1✔
141
      workflowId = generateUniqueId();
×
142
    }
143
    request.setWorkflowId(workflowId);
1✔
144

145
    if (signalInput != null) {
1✔
146
      request.setSignalInput(signalInput);
1✔
147
    }
148

149
    if (startParameters.hasInput()) {
1✔
150
      request.setInput(startParameters.getInput());
1✔
151
    }
152

153
    if (startParameters.hasTaskQueue()) {
1✔
154
      request.setTaskQueue(startParameters.getTaskQueue());
1✔
155
    }
156

157
    if (startParameters.hasRetryPolicy()) {
1✔
158
      request.setRetryPolicy(startParameters.getRetryPolicy());
×
159
    }
160

161
    if (startParameters.hasMemo()) {
1✔
162
      request.setMemo(startParameters.getMemo());
×
163
    }
164

165
    if (startParameters.hasSearchAttributes()) {
1✔
166
      request.setSearchAttributes(startParameters.getSearchAttributes());
1✔
167
    }
168

169
    if (startParameters.hasHeader()) {
1✔
170
      request.setHeader(startParameters.getHeader());
1✔
171
    }
172

173
    return request;
1✔
174
  }
175

176
  @Nonnull
177
  GetWorkflowExecutionHistoryRequest newHistoryLongPollRequest(
178
      WorkflowExecution workflowExecution, ByteString pageToken) {
179
    return GetWorkflowExecutionHistoryRequest.newBuilder()
1✔
180
        .setNamespace(clientOptions.getNamespace())
1✔
181
        .setExecution(workflowExecution)
1✔
182
        .setHistoryEventFilterType(HistoryEventFilterType.HISTORY_EVENT_FILTER_TYPE_CLOSE_EVENT)
1✔
183
        .setWaitNewEvent(true)
1✔
184
        .setNextPageToken(pageToken)
1✔
185
        .build();
1✔
186
  }
187

188
  private io.temporal.common.interceptors.Header extractContextsAndConvertToBytes(
189
      List<ContextPropagator> workflowOptionsContextPropagators) {
190
    List<ContextPropagator> workflowClientContextPropagators =
1✔
191
        clientOptions.getContextPropagators();
1✔
192
    if ((workflowClientContextPropagators.isEmpty() && workflowOptionsContextPropagators == null)
1✔
193
        || (workflowOptionsContextPropagators != null
194
            && workflowOptionsContextPropagators.isEmpty())) {
1✔
195
      return null;
1✔
196
    }
197

198
    List<ContextPropagator> listToUse =
1✔
199
        MoreObjects.firstNonNull(
1✔
200
            workflowOptionsContextPropagators, workflowClientContextPropagators);
201
    Map<String, Payload> result = new HashMap<>();
1✔
202
    for (ContextPropagator propagator : listToUse) {
1✔
203
      result.putAll(propagator.serializeContext(propagator.getCurrentContext()));
1✔
204
    }
1✔
205
    return new io.temporal.common.interceptors.Header(result);
1✔
206
  }
207

208
  private static String generateUniqueId() {
209
    return UUID.randomUUID().toString();
1✔
210
  }
211
}
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