-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathInternalUtils.java
210 lines (186 loc) · 6.9 KB
/
InternalUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.uber.cadence.internal.common;
import com.google.common.base.Defaults;
import com.uber.cadence.DataBlob;
import com.uber.cadence.History;
import com.uber.cadence.HistoryEvent;
import com.uber.cadence.HistoryEventFilterType;
import com.uber.cadence.SearchAttributes;
import com.uber.cadence.TaskList;
import com.uber.cadence.TaskListKind;
import com.uber.cadence.converter.DataConverter;
import com.uber.cadence.converter.JsonDataConverter;
import com.uber.cadence.internal.worker.Shutdownable;
import com.uber.cadence.workflow.WorkflowMethod;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
/** Utility functions shared by the implementation code. */
public final class InternalUtils {
/**
* Used to construct default name of an activity or workflow type from a method it implements.
*
* @return "Simple class name"::"methodName"
*/
public static String getSimpleName(Method method) {
return getSimpleName(method.getDeclaringClass(), method);
}
public static String getSimpleName(Class<?> type, Method method) {
return type.getSimpleName() + "::" + method.getName();
}
public static String getWorkflowType(Method method, WorkflowMethod workflowMethod) {
String workflowName = workflowMethod.name();
if (workflowName.isEmpty()) {
return InternalUtils.getSimpleName(method);
} else {
return workflowName;
}
}
public static Method getWorkflowMethod(Class<?> workflowInterface) {
Method result = null;
for (Method m : workflowInterface.getMethods()) {
if (m.getAnnotation(WorkflowMethod.class) != null) {
if (result != null) {
throw new IllegalArgumentException(
"Workflow interface must have exactly one method "
+ "annotated with @WorkflowMethod. Found \""
+ result
+ "\" and \""
+ m
+ "\"");
}
result = m;
}
}
if (result == null) {
throw new IllegalArgumentException(
"Method annotated with @WorkflowMethod is not " + "found at " + workflowInterface);
}
return result;
}
public static TaskList createStickyTaskList(String taskListName) {
TaskList tl = new TaskList();
tl.setName(taskListName);
tl.setKind(TaskListKind.STICKY);
return tl;
}
public static TaskList createNormalTaskList(String taskListName) {
TaskList tl = new TaskList();
tl.setName(taskListName);
tl.setKind(TaskListKind.NORMAL);
return tl;
}
public static long awaitTermination(Shutdownable s, long timeoutMillis) {
if (s == null) {
return timeoutMillis;
}
return awaitTermination(
timeoutMillis,
() -> {
s.awaitTermination(timeoutMillis, TimeUnit.MILLISECONDS);
});
}
public static long awaitTermination(ExecutorService s, long timeoutMillis) {
if (s == null) {
return timeoutMillis;
}
return awaitTermination(
timeoutMillis,
() -> {
try {
s.awaitTermination(timeoutMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
}
});
}
public static long awaitTermination(long timeoutMillis, Runnable toTerminate) {
long started = System.currentTimeMillis();
toTerminate.run();
long remainingTimeout = timeoutMillis - (System.currentTimeMillis() - started);
if (remainingTimeout < 0) {
remainingTimeout = 0;
}
return remainingTimeout;
}
public static Object getValueOrDefault(Object value, Class<?> valueClass) {
if (value != null) {
return value;
}
return Defaults.defaultValue(valueClass);
}
public static SearchAttributes convertMapToSearchAttributes(
Map<String, Object> searchAttributes) {
DataConverter converter = JsonDataConverter.getInstance();
Map<String, ByteBuffer> mapOfByteBuffer = new HashMap<>();
searchAttributes.forEach(
(key, value) -> {
mapOfByteBuffer.put(key, ByteBuffer.wrap(converter.toData(value)));
});
return new SearchAttributes().setIndexedFields(mapOfByteBuffer);
}
// This method deserialize the DataBlob data to the HistoriyEvent data
public static History DeserializeFromBlobToHistoryEvents(
List<DataBlob> blobData, HistoryEventFilterType historyEventFilterType) throws TException {
List<HistoryEvent> events = new ArrayList<HistoryEvent>();
for (DataBlob data : blobData) {
History history = new History();
try {
byte[] dataByte = data.getData();
dataByte = Arrays.copyOfRange(dataByte, 1, dataByte.length);
deSerializer.deserialize(history, dataByte);
if (history == null || history.getEvents() == null || history.getEvents().size() == 0) {
return null;
}
} catch (org.apache.thrift.TException err) {
throw new TException("Deserialize blob data to history event failed with unknown error");
}
events.addAll(history.getEvents());
}
if (events.size() > 0 && historyEventFilterType == HistoryEventFilterType.CLOSE_EVENT) {
events = events.subList(events.size() - 1, events.size());
}
return new History().setEvents(events);
}
// This method serializes history event to blob data
public static List<DataBlob> SerializeFromHistoryEventToBlobData(List<HistoryEvent> events) {
List<DataBlob> blobs = new ArrayList<>(events.size());
for (HistoryEvent event : events) {
DataBlob blob = new DataBlob();
try {
blob.setData(serializer.serialize(event));
} catch (org.apache.thrift.TException err) {
throw new RuntimeException("Serialize to blob data failed", err);
}
blobs.add(blob);
}
return blobs;
}
private static final TDeserializer deSerializer = new TDeserializer();
private static final TSerializer serializer = new TSerializer();
/** Prohibit instantiation */
private InternalUtils() {}
}