9
9
import lombok .EqualsAndHashCode ;
10
10
import lombok .Getter ;
11
11
import lombok .Setter ;
12
+ import org .opensearch .Version ;
12
13
import org .opensearch .core .common .io .stream .StreamInput ;
13
14
import org .opensearch .core .common .io .stream .StreamOutput ;
14
15
import org .opensearch .core .common .io .stream .Writeable ;
17
18
import org .opensearch .core .xcontent .XContentBuilder ;
18
19
import org .opensearch .core .xcontent .XContentParser ;
19
20
import org .opensearch .ml .common .dataset .MLInputDataType ;
21
+ import org .opensearch .ml .common .transport .register .MLRegisterModelInput ;
20
22
21
23
import java .io .IOException ;
24
+ import java .security .AccessController ;
25
+ import java .security .PrivilegedActionException ;
26
+ import java .security .PrivilegedExceptionAction ;
22
27
import java .time .Instant ;
23
28
import java .util .ArrayList ;
24
29
import java .util .Arrays ;
25
30
import java .util .List ;
31
+ import java .util .Map ;
26
32
27
33
import static org .opensearch .core .xcontent .XContentParserUtils .ensureExpectedToken ;
28
34
import static org .opensearch .ml .common .CommonValue .USER ;
35
+ import static org .opensearch .ml .common .utils .StringUtils .getParameterMap ;
36
+ import static org .opensearch .ml .common .utils .StringUtils .gson ;
29
37
30
38
@ Getter
31
39
@ EqualsAndHashCode
@@ -44,6 +52,8 @@ public class MLTask implements ToXContentObject, Writeable {
44
52
public static final String LAST_UPDATE_TIME_FIELD = "last_update_time" ;
45
53
public static final String ERROR_FIELD = "error" ;
46
54
public static final String IS_ASYNC_TASK_FIELD = "is_async" ;
55
+ public static final String TRANSFORM_JOB_FIELD = "transform_job" ;
56
+ public static final Version MINIMAL_SUPPORTED_VERSION_FOR_BATCH_TRANSFORM_JOB = CommonValue .VERSION_2_16_0 ;
47
57
48
58
@ Setter
49
59
private String taskId ;
@@ -65,6 +75,8 @@ public class MLTask implements ToXContentObject, Writeable {
65
75
private String error ;
66
76
private User user ; // TODO: support document level access control later
67
77
private boolean async ;
78
+ @ Setter
79
+ private Map <String , Object > transformJob ;
68
80
69
81
@ Builder (toBuilder = true )
70
82
public MLTask (
@@ -81,7 +93,8 @@ public MLTask(
81
93
Instant lastUpdateTime ,
82
94
String error ,
83
95
User user ,
84
- boolean async
96
+ boolean async ,
97
+ Map <String , Object > transformJob
85
98
) {
86
99
this .taskId = taskId ;
87
100
this .modelId = modelId ;
@@ -97,9 +110,11 @@ public MLTask(
97
110
this .error = error ;
98
111
this .user = user ;
99
112
this .async = async ;
113
+ this .transformJob = transformJob ;
100
114
}
101
115
102
116
public MLTask (StreamInput input ) throws IOException {
117
+ Version streamInputVersion = input .getVersion ();
103
118
this .taskId = input .readOptionalString ();
104
119
this .modelId = input .readOptionalString ();
105
120
this .taskType = input .readEnum (MLTaskType .class );
@@ -122,10 +137,17 @@ public MLTask(StreamInput input) throws IOException {
122
137
this .user = null ;
123
138
}
124
139
this .async = input .readBoolean ();
140
+ if (streamInputVersion .onOrAfter (MLTask .MINIMAL_SUPPORTED_VERSION_FOR_BATCH_TRANSFORM_JOB )) {
141
+ if (input .readBoolean ()) {
142
+ String mapStr = input .readString ();
143
+ this .transformJob = gson .fromJson (mapStr , Map .class );
144
+ }
145
+ }
125
146
}
126
147
127
148
@ Override
128
149
public void writeTo (StreamOutput out ) throws IOException {
150
+ Version streamOutputVersion = out .getVersion ();
129
151
out .writeOptionalString (taskId );
130
152
out .writeOptionalString (modelId );
131
153
out .writeEnum (taskType );
@@ -149,6 +171,21 @@ public void writeTo(StreamOutput out) throws IOException {
149
171
out .writeBoolean (false );
150
172
}
151
173
out .writeBoolean (async );
174
+ if (streamOutputVersion .onOrAfter (MLTask .MINIMAL_SUPPORTED_VERSION_FOR_BATCH_TRANSFORM_JOB )) {
175
+ if (transformJob != null ) {
176
+ out .writeBoolean (true );
177
+ try {
178
+ AccessController .doPrivileged ((PrivilegedExceptionAction <Void >) () -> {
179
+ out .writeString (gson .toJson (transformJob ));
180
+ return null ;
181
+ });
182
+ } catch (PrivilegedActionException e ) {
183
+ throw new RuntimeException (e );
184
+ }
185
+ } else {
186
+ out .writeBoolean (false );
187
+ }
188
+ }
152
189
}
153
190
154
191
@ Override
@@ -194,6 +231,9 @@ public XContentBuilder toXContent(XContentBuilder xContentBuilder, Params params
194
231
builder .field (USER , user );
195
232
}
196
233
builder .field (IS_ASYNC_TASK_FIELD , async );
234
+ if (transformJob != null ) {
235
+ builder .field (TRANSFORM_JOB_FIELD , transformJob );
236
+ }
197
237
return builder .endObject ();
198
238
}
199
239
@@ -217,6 +257,7 @@ public static MLTask parse(XContentParser parser) throws IOException {
217
257
String error = null ;
218
258
User user = null ;
219
259
boolean async = false ;
260
+ Map <String , Object > transformJob = null ;
220
261
221
262
ensureExpectedToken (XContentParser .Token .START_OBJECT , parser .currentToken (), parser );
222
263
while (parser .nextToken () != XContentParser .Token .END_OBJECT ) {
@@ -274,6 +315,9 @@ public static MLTask parse(XContentParser parser) throws IOException {
274
315
case IS_ASYNC_TASK_FIELD :
275
316
async = parser .booleanValue ();
276
317
break ;
318
+ case TRANSFORM_JOB_FIELD :
319
+ transformJob = parser .map ();
320
+ break ;
277
321
default :
278
322
parser .skipChildren ();
279
323
break ;
@@ -294,6 +338,7 @@ public static MLTask parse(XContentParser parser) throws IOException {
294
338
.error (error )
295
339
.user (user )
296
340
.async (async )
341
+ .transformJob (transformJob )
297
342
.build ();
298
343
}
299
344
}
0 commit comments