Skip to content

Commit 7e25eee

Browse files
committed
PR-suggested changes
1 parent ae78138 commit 7e25eee

File tree

13 files changed

+76
-30
lines changed

13 files changed

+76
-30
lines changed

temporal-sdk/src/main/java/io/temporal/common/SearchAttributes.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ private Builder(SearchAttributes copyFrom) {
5555
untypedValues = new TreeMap<>(copyFrom.untypedValues);
5656
}
5757

58-
/** Set a search attribute key and typed value. */
58+
/** Set a search attribute key and typed value, replacing previous if present. */
5959
public <T> Builder set(SearchAttributeKey<T> key, @Nonnull T value) {
6060
Objects.requireNonNull(untypedValues, "Collection already built");
6161
Objects.requireNonNull(value, "Value cannot be null");
62-
// TODO(cretz): Prevent duplicate key name?
6362
untypedValues.put(key, value);
6463
return this;
6564
}

temporal-sdk/src/main/java/io/temporal/internal/common/SearchAttributePayloadConverter.java

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ private static Class<?> indexValueTypeToJavaType(@Nullable IndexedValueType inde
308308
switch (indexedValueType) {
309309
case INDEXED_VALUE_TYPE_TEXT:
310310
case INDEXED_VALUE_TYPE_KEYWORD:
311+
case INDEXED_VALUE_TYPE_KEYWORD_LIST:
311312
return String.class;
312313
case INDEXED_VALUE_TYPE_INT:
313314
return Long.class;

temporal-sdk/src/main/java/io/temporal/internal/statemachines/ActivityStateMachine.puml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
`limitations under the License.
1818

1919
` PlantUML <plantuml.com> State Diagram.
20-
` Generated from src\main\java\io\temporal\internal\statemachines\ActivityStateMachine.java
20+
` Generated from src/main/java/io/temporal/internal/statemachines/ActivityStateMachine.java
2121
` by io.temporal.internal.statemachines.CommandsGeneratePlantUMLStateDiagrams.
2222

2323

temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflowContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ private StartChildWorkflowExecutionParameters createChildWorkflowParameters(
727727
if (searchAttributes != null && !searchAttributes.isEmpty()) {
728728
if (options.getTypedSearchAttributes() != null) {
729729
throw new IllegalArgumentException(
730-
"Cannot have typed search attributes and search attributes");
730+
"Cannot have both typed search attributes and search attributes");
731731
}
732732
attributes.setSearchAttributes(SearchAttributesUtil.encode(searchAttributes));
733733
} else if (options.getTypedSearchAttributes() != null) {

temporal-sdk/src/test/java/io/temporal/workflow/searchattributes/TypedSearchAttributesTest.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@
4747
import io.temporal.workflow.shared.TestWorkflows.NoArgsWorkflow;
4848
import io.temporal.workflow.shared.TestWorkflows.TestSignaledWorkflow;
4949
import java.time.OffsetDateTime;
50+
import java.util.Arrays;
51+
import java.util.List;
5052
import org.junit.Rule;
5153
import org.junit.Test;
5254

5355
/** Typed attribute translation of {@link SearchAttributesTest} */
5456
public class TypedSearchAttributesTest {
55-
private static final SearchAttributeKey<String> TEST_NEW_KEY =
56-
SearchAttributeKey.forText("NewKey");
57-
private static final String TEST_NEW_VALUE = "NewVal";
57+
private static final SearchAttributeKey<List<String>> TEST_NEW_KEY =
58+
SearchAttributeKey.forKeywordList("NewKey");
59+
private static final List<String> TEST_NEW_VALUE = Arrays.asList("foo", "bar");
5860
private static final SearchAttributeKey<String> TEST_UNKNOWN_KEY =
5961
SearchAttributeKey.forText("UnknownKey");
6062

@@ -71,7 +73,7 @@ public class TypedSearchAttributesTest {
7173
public SDKTestWorkflowRule testWorkflowRule =
7274
SDKTestWorkflowRule.newBuilder()
7375
.setWorkflowTypes(TestWorkflowImpl.class, TestParentWorkflow.class, TestChild.class)
74-
.registerSearchAttribute(TEST_NEW_KEY.getName(), TEST_NEW_KEY.getValueType())
76+
.registerSearchAttribute(TEST_NEW_KEY)
7577
.build();
7678

7779
@Test

temporal-sdk/src/test/java/io/temporal/workflow/searchattributes/UpsertTypedSearchAttributeTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import io.temporal.testing.internal.TracingWorkerInterceptor;
3434
import io.temporal.workflow.Workflow;
3535
import io.temporal.workflow.shared.TestWorkflows.TestWorkflowStringArg;
36+
import java.util.Arrays;
37+
import java.util.List;
3638
import java.util.concurrent.atomic.AtomicBoolean;
3739
import org.junit.Rule;
3840
import org.junit.Test;
@@ -46,6 +48,7 @@ public class UpsertTypedSearchAttributeTest {
4648
public SDKTestWorkflowRule testWorkflowRule =
4749
SDKTestWorkflowRule.newBuilder()
4850
.setWorkflowTypes(TestUpsertSearchAttributesImpl.class)
51+
.registerSearchAttribute(TestUpsertSearchAttributesImpl.MY_KEYWORD_LIST_ATTR)
4952
.build();
5053

5154
@Test
@@ -80,6 +83,8 @@ public static class TestUpsertSearchAttributesImpl implements TestWorkflowString
8083

8184
public static final SearchAttributeKey<String> CUSTOM_KEYWORD_ATTR =
8285
SearchAttributeKey.forKeyword("CustomKeywordField");
86+
private static final SearchAttributeKey<List<String>> MY_KEYWORD_LIST_ATTR =
87+
SearchAttributeKey.forKeywordList("MyKeywordListField");
8388

8489
private static final AtomicBoolean FAILED = new AtomicBoolean();
8590

@@ -114,6 +119,27 @@ public void execute(String taskQueue) {
114119
if (FAILED.compareAndSet(false, true)) {
115120
throw new IllegalStateException("force replay");
116121
}
122+
123+
// Also check keyword list
124+
Workflow.upsertTypedSearchAttributes(
125+
MY_KEYWORD_LIST_ATTR.valueSet(Arrays.asList("foo", "bar")));
126+
newAttributes = Workflow.getTypedSearchAttributes();
127+
assertEquals(2, newAttributes.size());
128+
assertEquals(
129+
Arrays.asList("foo", "bar"),
130+
Workflow.getTypedSearchAttributes().get(MY_KEYWORD_LIST_ATTR));
131+
132+
// Change and confirm completely replaced
133+
Workflow.upsertTypedSearchAttributes(
134+
MY_KEYWORD_LIST_ATTR.valueSet(Arrays.asList("baz", "qux")));
135+
assertEquals(
136+
Arrays.asList("baz", "qux"),
137+
Workflow.getTypedSearchAttributes().get(MY_KEYWORD_LIST_ATTR));
138+
139+
// Unset
140+
Workflow.upsertTypedSearchAttributes(MY_KEYWORD_LIST_ATTR.valueUnset());
141+
newAttributes = Workflow.getTypedSearchAttributes();
142+
assertEquals(1, newAttributes.size());
117143
}
118144
}
119145
}

temporal-test-server/src/test/java/io/temporal/testserver/functional/DescribeWorkflowAsserter.java

-8
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,4 @@ public DescribeWorkflowAsserter assertPendingChildrenCount(int expected) {
178178
"child workflow count should match", expected, actual.getPendingChildrenCount());
179179
return this;
180180
}
181-
182-
public DescribeWorkflowAsserter assertSearchAttributes(Map<String, Object> expected) {
183-
Assert.assertEquals(
184-
"search attributes should match",
185-
expected,
186-
toSimpleMap(actual.getWorkflowExecutionInfo().getSearchAttributes().getIndexedFieldsMap()));
187-
return this;
188-
}
189181
}

temporal-test-server/src/test/java/io/temporal/testserver/functional/DescribeWorkflowExecutionTest.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import io.temporal.client.WorkflowOptions;
3939
import io.temporal.client.WorkflowStub;
4040
import io.temporal.common.RetryOptions;
41+
import io.temporal.common.SearchAttributeKey;
42+
import io.temporal.common.SearchAttributes;
4143
import io.temporal.serviceclient.CheckedExceptionWrapper;
4244
import io.temporal.testing.internal.SDKTestWorkflowRule;
4345
import io.temporal.workflow.ChildWorkflowOptions;
@@ -109,9 +111,12 @@ public void testWorkflowDoesNotExist() {
109111
// We need to use them here in order for this test to work against docker-compose.
110112
// Sadly, they don't fit our hitchhiker's guide narrative.
111113
private static class SearchAttributeFields {
112-
public static final String QUESTION = "CustomStringField";
113-
public static final String ASKER = "CustomTextField";
114-
public static final String ANSWER = "CustomKeywordField";
114+
public static final SearchAttributeKey<String> QUESTION =
115+
SearchAttributeKey.forText("CustomStringField");
116+
public static final SearchAttributeKey<String> ASKER =
117+
SearchAttributeKey.forText("CustomTextField");
118+
public static final SearchAttributeKey<String> ANSWER =
119+
SearchAttributeKey.forKeyword("CustomKeywordField");
115120
}
116121

117122
private WorkflowOptions options() {
@@ -123,12 +128,11 @@ private WorkflowOptions options() {
123128
.setWorkflowRunTimeout(Duration.ofMinutes(2))
124129
.setWorkflowTaskTimeout(Duration.ofMinutes(1))
125130
.setMemo(ImmutableMap.of("memo", "random"))
126-
.setSearchAttributes(
127-
ImmutableMap.of(
128-
SearchAttributeFields.QUESTION,
129-
"How many roads must a man walk down?",
130-
SearchAttributeFields.ASKER,
131-
"Mice"))
131+
.setTypedSearchAttributes(
132+
SearchAttributes.newBuilder()
133+
.set(SearchAttributeFields.QUESTION, "How many roads must a man walk down?")
134+
.set(SearchAttributeFields.ASKER, "Mice")
135+
.build())
132136
.build();
133137
}
134138

@@ -488,13 +492,11 @@ public static class TestDescribeWorkflowImpl implements TestDescribeWorkflow {
488492
@Override
489493
public void run(
490494
String myToken, String childToken, boolean heartbeat, int failAttemptsEarlierThan) {
491-
Workflow.upsertSearchAttributes(
495+
Workflow.upsertTypedSearchAttributes(
492496
// vs. the attributes present at start, this will add one, update one, and leave one
493497
// unchanged
494-
ImmutableMap.of(
495-
SearchAttributeFields.ANSWER,
496-
"42",
497-
SearchAttributeFields.QUESTION,
498+
SearchAttributeFields.ASKER.valueSet("42"),
499+
SearchAttributeFields.QUESTION.valueSet(
498500
"What do you get when you multiply six by nine?"));
499501

500502
if (childToken != null) {

temporal-test-server/src/test/java/io/temporal/testserver/functional/RepeatedWorkflowTaskFailuresTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public void repeatedFailure() {
7171

7272
public static class TestWorkflow implements TestWorkflows.PrimitiveWorkflow {
7373

74+
@SuppressWarnings("deprecation")
7475
@Override
7576
public void execute() {
7677
if (++retryCount < 4) {

temporal-test-server/src/test/java/io/temporal/testserver/functional/searchattributes/IncorrectStartWorkflowSearchAttributesTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class IncorrectStartWorkflowSearchAttributesTest {
5353
public SDKTestWorkflowRule testWorkflowRule =
5454
SDKTestWorkflowRule.newBuilder().setWorkflowTypes(DummyWorkflow.class).build();
5555

56+
@SuppressWarnings("deprecation")
5657
@Test
5758
public void searchAttributeIsNotRegistered() {
5859
final String WORKFLOW_ID = "workflow-with-non-existing-sa";
@@ -91,6 +92,7 @@ public void searchAttributeIsNotRegistered() {
9192
historyException.getStatus().getCode());
9293
}
9394

95+
@SuppressWarnings("deprecation")
9496
@Test
9597
public void searchAttributeIsIncorrectValueType() {
9698
final String WORKFLOW_ID = "workflow-with-sa-incorrect-value-type";

temporal-test-server/src/test/java/io/temporal/testserver/functional/searchattributes/IncorrectUpsertSearchAttributesTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public void searchAttributeIsIncorrectValueType() {
132132
}
133133

134134
public static class UpsertingWorkflow implements TestWorkflows.PrimitiveWorkflow {
135+
@SuppressWarnings("deprecation")
135136
@Override
136137
public void execute() {
137138
// try to set incorrect search attributes on the first attempt

temporal-testing/src/main/java/io/temporal/testing/TestWorkflowRule.java

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.temporal.client.WorkflowClientOptions;
3030
import io.temporal.client.WorkflowOptions;
3131
import io.temporal.client.WorkflowStub;
32+
import io.temporal.common.SearchAttributeKey;
3233
import io.temporal.common.interceptors.WorkerInterceptor;
3334
import io.temporal.internal.common.env.DebugModeUtils;
3435
import io.temporal.internal.docker.RegisterTestNamespace;
@@ -327,6 +328,19 @@ public Builder registerSearchAttribute(String name, IndexedValueType type) {
327328
return this;
328329
}
329330

331+
/**
332+
* Add a search attribute to be registered on the Temporal Server.
333+
*
334+
* @param key key to register
335+
* @return {@code this}
336+
* @see <a
337+
* href="https://docs.temporal.io/docs/tctl/how-to-add-a-custom-search-attribute-to-a-cluster-using-tctl">Add
338+
* a Custom Search Attribute Using tctl</a>
339+
*/
340+
public Builder registerSearchAttribute(SearchAttributeKey<?> key) {
341+
return this.registerSearchAttribute(key.getName(), key.getValueType());
342+
}
343+
330344
/**
331345
* Sets the scope to be used for metrics reporting. Optional. Default is to not report metrics.
332346
*

temporal-testing/src/main/java/io/temporal/testing/internal/SDKTestWorkflowRule.java

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.temporal.client.WorkflowClientOptions;
3737
import io.temporal.client.WorkflowQueryException;
3838
import io.temporal.client.WorkflowStub;
39+
import io.temporal.common.SearchAttributeKey;
3940
import io.temporal.common.WorkflowExecutionHistory;
4041
import io.temporal.common.interceptors.WorkerInterceptor;
4142
import io.temporal.internal.common.env.DebugModeUtils;
@@ -208,6 +209,11 @@ public Builder registerSearchAttribute(String name, IndexedValueType type) {
208209
return this;
209210
}
210211

212+
public Builder registerSearchAttribute(SearchAttributeKey<?> key) {
213+
testWorkflowRuleBuilder.registerSearchAttribute(key);
214+
return this;
215+
}
216+
211217
public Builder setMetricsScope(Scope scope) {
212218
testWorkflowRuleBuilder.setMetricsScope(scope);
213219
return this;

0 commit comments

Comments
 (0)