-
Notifications
You must be signed in to change notification settings - Fork 188
/
AbstractSearchIterativeTaskHandler.java
357 lines (304 loc) · 16.4 KB
/
AbstractSearchIterativeTaskHandler.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright (c) 2010-2013 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.evolveum.midpoint.model.impl.util;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.evolveum.midpoint.prism.PrismProperty;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.prism.xml.ns._public.query_3.QueryType;
import org.springframework.beans.factory.annotation.Autowired;
import com.evolveum.midpoint.model.impl.ModelObjectResolver;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskHandler;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.task.api.TaskRunResult;
import com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
/**
* @author semancik
*
*/
public abstract class AbstractSearchIterativeTaskHandler<O extends ObjectType, H extends AbstractSearchIterativeResultHandler<O>> implements TaskHandler {
// WARNING! This task handler is efficiently singleton!
// It is a spring bean and it is supposed to handle all search task instances
// Therefore it must not have task-specific fields. It can only contain fields specific to
// all tasks of a specified type
private String taskName;
private String taskOperationPrefix;
private Class<O> type;
private boolean logFinishInfo = false;
private boolean countObjectsOnStart = true; // todo make configurable per task instance (if necessary)
// If you need to store fields specific to task instance or task run the ResultHandler is a good place to do that.
// This is not ideal, TODO: refactor
private Map<Task, H> handlers = Collections.synchronizedMap(new HashMap<Task, H>());
@Autowired(required=true)
protected TaskManager taskManager;
@Autowired(required=true)
protected ModelObjectResolver modelObjectResolver;
@Autowired(required = true)
protected PrismContext prismContext;
private static final transient Trace LOGGER = TraceManager.getTrace(AbstractSearchIterativeTaskHandler.class);
protected AbstractSearchIterativeTaskHandler(Class<O> type, String taskName, String taskOperationPrefix) {
super();
this.type = type;
this.taskName = taskName;
this.taskOperationPrefix = taskOperationPrefix;
}
public boolean isLogFinishInfo() {
return logFinishInfo;
}
public void setLogFinishInfo(boolean logFinishInfo) {
this.logFinishInfo = logFinishInfo;
}
@Override
public TaskRunResult run(Task coordinatorTask) {
LOGGER.trace("{} run starting (coordinator task {})", taskName, coordinatorTask);
OperationResult opResult = new OperationResult(taskOperationPrefix + ".run");
opResult.setStatus(OperationResultStatus.IN_PROGRESS);
TaskRunResult runResult = new TaskRunResult();
runResult.setOperationResult(opResult);
H resultHandler = createHandler(runResult, coordinatorTask, opResult);
if (resultHandler == null) {
// the error should already be in the runResult
return runResult;
}
boolean cont = initializeRun(resultHandler, runResult, coordinatorTask, opResult);
if (!cont) {
return runResult;
}
// TODO: error checking - already running
handlers.put(coordinatorTask, resultHandler);
ObjectQuery query;
try {
query = createQuery(resultHandler, runResult, coordinatorTask, opResult);
} catch (SchemaException ex) {
LOGGER.error("{}: Schema error while creating a search filter: {}", new Object[]{taskName, ex.getMessage(), ex});
opResult.recordFatalError("Schema error while creating a search filter: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
runResult.setProgress(resultHandler.getProgress());
return runResult;
}
if (query == null) {
// the error should already be in the runResult
return runResult;
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("{}: searching using query:\n{}", taskName, query.debugDump());
}
try {
// counting objects can be within try-catch block, because the handling is similar to handling errors within searchIterative
Long expectedTotal = null;
if (countObjectsOnStart) {
Integer expectedTotalInt = modelObjectResolver.countObjects(type, query, opResult);
LOGGER.trace("{}: expecting {} objects to be processed", taskName, expectedTotal);
if (expectedTotalInt != null) {
expectedTotal = (long) expectedTotalInt; // conversion would fail on null
}
}
runResult.setProgress(0);
coordinatorTask.setProgress(0);
coordinatorTask.setExpectedTotal(expectedTotal);
try {
coordinatorTask.savePendingModifications(opResult);
} catch (ObjectAlreadyExistsException e) { // other exceptions are handled in the outer try block
throw new IllegalStateException("Unexpected ObjectAlreadyExistsException when updating task progress/expectedTotal", e);
}
resultHandler.createWorkerThreads(coordinatorTask, opResult);
modelObjectResolver.searchIterative(type, query, null, resultHandler, opResult);
resultHandler.completeProcessing(opResult);
} catch (ObjectNotFoundException ex) {
LOGGER.error("{}: Object not found: {}", new Object[]{taskName, ex.getMessage(), ex});
// This is bad. The resource does not exist. Permanent problem.
opResult.recordFatalError("Object not found " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
runResult.setProgress(resultHandler.getProgress());
return runResult;
} catch (CommunicationException ex) {
LOGGER.error("{}: Communication error: {}", new Object[]{taskName, ex.getMessage(), ex});
// Error, but not critical. Just try later.
opResult.recordPartialError("Communication error: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
runResult.setProgress(resultHandler.getProgress());
return runResult;
} catch (SchemaException ex) {
LOGGER.error("{}: Error dealing with schema: {}", new Object[]{taskName, ex.getMessage(), ex});
// Not sure about this. But most likely it is a misconfigured resource or connector
// It may be worth to retry. Error is fatal, but may not be permanent.
opResult.recordFatalError("Error dealing with schema: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
runResult.setProgress(resultHandler.getProgress());
return runResult;
} catch (RuntimeException ex) {
LOGGER.error("{}: Internal Error: {}", new Object[]{taskName, ex.getMessage(), ex});
// Can be anything ... but we can't recover from that.
// It is most likely a programming error. Does not make much sense to retry.
opResult.recordFatalError("Internal Error: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
runResult.setProgress(resultHandler.getProgress());
return runResult;
} catch (ConfigurationException ex) {
LOGGER.error("{}: Configuration error: {}", new Object[]{taskName, ex.getMessage(), ex});
// Not sure about this. But most likely it is a misconfigured resource or connector
// It may be worth to retry. Error is fatal, but may not be permanent.
opResult.recordFatalError("Configuration error: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
runResult.setProgress(resultHandler.getProgress());
return runResult;
} catch (SecurityViolationException ex) {
LOGGER.error("{}: Security violation: {}", new Object[]{taskName, ex.getMessage(), ex});
opResult.recordFatalError("Security violation: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
runResult.setProgress(resultHandler.getProgress());
return runResult;
}
// TODO: check last handler status
handlers.remove(coordinatorTask);
runResult.setProgress(resultHandler.getProgress());
runResult.setRunResultStatus(TaskRunResultStatus.FINISHED);
if (logFinishInfo) {
String finishMessage = "Finished " + taskName + " (" + coordinatorTask + "). ";
String statistics = "Processed " + resultHandler.getProgress() + " objects, got " + resultHandler.getErrors() + " errors.";
if (resultHandler.getProgress() > 0) {
statistics += " Average time for one object: " + resultHandler.getAverageTime() + " milliseconds.";
}
opResult.createSubresult(taskOperationPrefix + ".statistics").recordStatus(OperationResultStatus.SUCCESS, statistics);
LOGGER.info(finishMessage + statistics);
}
try {
finish(resultHandler, runResult, coordinatorTask, opResult);
} catch (SchemaException ex) {
LOGGER.error("{}: Schema error while finishing the run: {}", new Object[]{taskName, ex.getMessage(), ex});
opResult.recordFatalError("Schema error while finishing the run: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
runResult.setProgress(resultHandler.getProgress());
return runResult;
}
LOGGER.trace("{} run finished (task {}, run result {})", new Object[]{taskName, coordinatorTask, runResult});
return runResult;
}
protected void finish(H handler, TaskRunResult runResult, Task task, OperationResult opResult) throws SchemaException {
}
private H getHandler(Task task) {
return handlers.get(task);
}
@Override
public Long heartbeat(Task task) {
// Delegate heartbeat to the result handler
if (getHandler(task) != null) {
return getHandler(task).heartbeat();
} else {
// most likely a race condition.
return null;
}
}
protected <T extends ObjectType> T resolveObjectRef(Class<T> type, TaskRunResult runResult, Task task, OperationResult opResult) {
String typeName = type.getClass().getSimpleName();
String objectOid = task.getObjectOid();
if (objectOid == null) {
LOGGER.error("Import: No {} OID specified in the task", typeName);
opResult.recordFatalError("No "+typeName+" OID specified in the task");
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
return null;
}
T objectType;
try {
objectType = modelObjectResolver.getObject(type, objectOid, null, task, opResult);
} catch (ObjectNotFoundException ex) {
LOGGER.error("Import: {} {} not found: {}", new Object[]{typeName, objectOid, ex.getMessage(), ex});
// This is bad. The resource does not exist. Permanent problem.
opResult.recordFatalError(typeName+" not found " + objectOid, ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
return null;
} catch (SchemaException ex) {
LOGGER.error("Import: Error dealing with schema: {}", ex.getMessage(), ex);
// Not sure about this. But most likely it is a misconfigured resource or connector
// It may be worth to retry. Error is fatal, but may not be permanent.
opResult.recordFatalError("Error dealing with schema: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
return null;
} catch (RuntimeException ex) {
LOGGER.error("Import: Internal Error: {}", ex.getMessage(), ex);
// Can be anything ... but we can't recover from that.
// It is most likely a programming error. Does not make much sense to retry.
opResult.recordFatalError("Internal Error: " + ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
return null;
} catch (CommunicationException ex) {
LOGGER.error("Import: Error getting {} {}: {}", new Object[]{typeName, objectOid, ex.getMessage(), ex});
opResult.recordFatalError("Error getting "+typeName+" " + objectOid+": "+ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.TEMPORARY_ERROR);
return null;
} catch (ConfigurationException ex) {
LOGGER.error("Import: Error getting {} {}: {}", new Object[]{typeName, objectOid, ex.getMessage(), ex});
opResult.recordFatalError("Error getting "+typeName+" " + objectOid+": "+ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
return null;
} catch (SecurityViolationException ex) {
LOGGER.error("Import: Error getting {} {}: {}", new Object[]{typeName, objectOid, ex.getMessage(), ex});
opResult.recordFatalError("Error getting "+typeName+" " + objectOid+": "+ex.getMessage(), ex);
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
return null;
}
if (objectType == null) {
LOGGER.error("Import: No "+typeName+" specified");
opResult.recordFatalError("No "+typeName+" specified");
runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR);
return null;
}
return objectType;
}
@Override
public void refreshStatus(Task task) {
// Local task. No refresh needed. The Task instance has always fresh data.
}
// meant to be used in specific createQuery methods; it is not used here directly, because QueryType->ObjectQuery conversion
// requires information on object class, and this is specific to individual subclasses (handlers)
protected QueryType getObjectQueryTypeFromTask(Task task) {
PrismProperty<QueryType> objectQueryPrismProperty = task.getExtensionProperty(SchemaConstants.MODEL_EXTENSION_OBJECT_QUERY);
if (objectQueryPrismProperty != null && objectQueryPrismProperty.getRealValue() != null) {
return objectQueryPrismProperty.getRealValue();
} else {
return null;
}
}
/**
* Handler parameter may be used to pass task instance state between the calls.
*/
protected abstract ObjectQuery createQuery(H handler, TaskRunResult runResult, Task task, OperationResult opResult) throws SchemaException;
protected abstract H createHandler(TaskRunResult runResult, Task coordinatorTask,
OperationResult opResult);
/**
* Used to properly initialize the "run", which is kind of task instance. The result handler is already created at this stage.
* Therefore this method may be used to "enrich" the result handler with some instance-specific data.
*/
protected boolean initializeRun(H handler, TaskRunResult runResult, Task task, OperationResult opResult) {
// Nothing to do by default
return true;
}
}