/
TaskMergingManager.java
190 lines (157 loc) · 8.11 KB
/
TaskMergingManager.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
/******************************************************************************
* NOTICE *
* *
* This software (or technical data) was produced for the U.S. Government *
* under contract, and is subject to the Rights in Data-General Clause *
* 52.227-14, Alt. IV (DEC 2007). *
* *
* Copyright 2023 The MITRE Corporation. All Rights Reserved. *
******************************************************************************/
/******************************************************************************
* Copyright 2023 The MITRE Corporation *
* *
* 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 org.mitre.mpf.wfm.service;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.UUID;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.inject.Inject;
import org.apache.camel.Message;
import org.mitre.mpf.rest.api.pipelines.Action;
import org.mitre.mpf.wfm.data.entities.persistent.BatchJob;
import org.mitre.mpf.wfm.data.entities.persistent.Media;
import org.mitre.mpf.wfm.data.entities.transients.Track;
import org.mitre.mpf.wfm.enums.MpfConstants;
import org.mitre.mpf.wfm.util.AggregateJobPropertiesUtil;
import org.springframework.stereotype.Component;
@Component
public class TaskMergingManager {
private static final String BREAD_CRUMB_HEADER = "breadcrumbId";
// Camel will automatically add a bread crumb if one is not included in the message headers.
// Adding a prefix to the bread crumb makes it possible to identify which bread crumbs were
// added by this class.
private static final String CUSTOM_BREAD_CRUMB_PREFIX = "mpf-";
private static final int NUM_CHARS_BEFORE_ALGO
= CUSTOM_BREAD_CRUMB_PREFIX.length() + UUID.randomUUID().toString().length() + 1;
private final AggregateJobPropertiesUtil _aggregateJobPropertiesUtil;
@Inject
TaskMergingManager(
AggregateJobPropertiesUtil aggregateJobPropertiesUtil) {
_aggregateJobPropertiesUtil = aggregateJobPropertiesUtil;
}
public boolean needsBreadCrumb(BatchJob job, Media media, int taskIdx, int actionIdx) {
return getTransitiveMergeTargets(job, media, taskIdx, actionIdx)
.min()
.stream()
.mapToObj(ti -> job.getPipelineElements().getAction(ti, 0))
.map(a -> _aggregateJobPropertiesUtil.getValue(MpfConstants.TRIGGER, job, media, a))
.anyMatch(t -> t != null && !t.isBlank());
}
// When creating detection request messages for an action that uses task merging and the
// transitive merge target uses a trigger, the tracks that are fed in to the action may have
// initially been generated by different algorithms. In those cases, we store name of the
// algorithm that was originally used to generate the feed forward track in the breadcrumbId
// header.
public void addBreadCrumb(
Message message, Track feedForwardTrack) {
message.setHeader(
BREAD_CRUMB_HEADER,
CUSTOM_BREAD_CRUMB_PREFIX + UUID.randomUUID()
+ '-' + feedForwardTrack.getMergedAlgorithm());
}
public String getMergedAlgorithm(
BatchJob job,
Media media,
int taskIdx,
int actionIdx,
Map<String, Object> headers) {
var headerAlgo = Optional.ofNullable(headers.get(BREAD_CRUMB_HEADER))
.filter(String.class::isInstance)
.map(String.class::cast)
.filter(hv -> hv.startsWith(CUSTOM_BREAD_CRUMB_PREFIX)
&& hv.length() > NUM_CHARS_BEFORE_ALGO)
.map(hv -> hv.substring(NUM_CHARS_BEFORE_ALGO));
if (headerAlgo.isPresent()) {
return headerAlgo.get();
}
var pipelineElements = job.getPipelineElements();
return getTransitiveMergeTargets(job, media, taskIdx, actionIdx)
.min()
.stream()
// Task merging is enabled, so use the algorithm at the end of the merge chain.
.mapToObj(ti -> pipelineElements.getAlgorithm(ti, 0))
.findAny()
// Task merging wasn't enabled, so use the actual algorithm.
.orElseGet(() -> pipelineElements.getAlgorithm(taskIdx, actionIdx))
.getName();
}
public boolean isMergeSource(BatchJob job, Media media, int taskIdx, int actionIdx) {
if (taskIdx == 0) {
return false;
}
var action = job.getPipelineElements().getAction(taskIdx, actionIdx);
return isMergeSource(job, media, action);
}
private boolean isMergeSource(BatchJob job, Media media, Action action) {
var mergeProperty = _aggregateJobPropertiesUtil.getValue(
MpfConstants.OUTPUT_MERGE_WITH_PREVIOUS_TASK_PROPERTY, job, media, action);
return Boolean.parseBoolean(mergeProperty);
}
public boolean isMergeTarget(BatchJob job, Media media, int taskIdx) {
var pipelineElements = job.getPipelineElements();
int lastDetectionTaskIdx = pipelineElements.getLastDetectionTaskIdx();
for (int futureTaskIdx = taskIdx + 1;
futureTaskIdx <= lastDetectionTaskIdx;
futureTaskIdx++) {
var futureTask = pipelineElements.getTask(futureTaskIdx);
var taskAppliesToMedia = false;
for (var futureAction : pipelineElements.getActionsInOrder(futureTask)) {
if (_aggregateJobPropertiesUtil.actionAppliesToMedia(job, media, futureAction)) {
taskAppliesToMedia = true;
if (isMergeSource(job, media, futureAction)) {
return true;
}
}
}
if (taskAppliesToMedia) {
return false;
}
}
return false;
}
public IntStream getTransitiveMergeTargets(
BatchJob job, Media media, int srcTaskIdx, int srcActionIdx) {
return Stream.iterate(
getDirectMergeTarget(job, media, srcTaskIdx, srcActionIdx),
OptionalInt::isPresent,
p -> getDirectMergeTarget(job, media, p.getAsInt(), 0))
.mapToInt(OptionalInt::getAsInt);
}
private OptionalInt getDirectMergeTarget(
BatchJob job, Media media, int srcTaskIdx, int srcActionIdx) {
if (!isMergeSource(job, media, srcTaskIdx, srcActionIdx)) {
return OptionalInt.empty();
}
for (int prevTaskIdx = srcTaskIdx - 1; prevTaskIdx >= 0; prevTaskIdx--) {
var prevAction = job.getPipelineElements().getAction(prevTaskIdx, 0);
if (_aggregateJobPropertiesUtil.actionAppliesToMedia(job, media, prevAction)) {
return OptionalInt.of(prevTaskIdx);
}
}
return OptionalInt.of(0);
}
}