@@ -245,8 +245,10 @@ private boolean jobRequiresUpgrade(Job job, Set<Store> stores) {
245245 * @param featureSet featureSet {@link FeatureSet} to find jobs and allocate
246246 */
247247 FeatureSet allocateFeatureSetToJobs (FeatureSet featureSet ) {
248- Set <FeatureSetJobStatus > toAdd = new HashSet <>();
249- Set <FeatureSetJobStatus > existing = featureSet .getJobStatuses ();
248+ Map <FeatureSetJobStatus .FeatureSetJobStatusKey , FeatureSetJobStatus > current = new HashMap <>();
249+ Map <FeatureSetJobStatus .FeatureSetJobStatusKey , FeatureSetJobStatus > existing =
250+ featureSet .getJobStatuses ().stream ()
251+ .collect (Collectors .toMap (FeatureSetJobStatus ::getId , s -> s ));
250252
251253 Stream <Pair <Source , Store >> jobArgsStream =
252254 getAllStores ().stream ()
@@ -269,14 +271,17 @@ FeatureSet allocateFeatureSetToJobs(FeatureSet featureSet) {
269271 status .setJob (job );
270272 status .setDeliveryStatus (FeatureSetProto .FeatureSetJobDeliveryStatus .STATUS_IN_PROGRESS );
271273
272- toAdd .add (status );
274+ current .put (
275+ new FeatureSetJobStatus .FeatureSetJobStatusKey (job .getId (), featureSet .getId ()), status );
273276 }
274277
275- Set <FeatureSetJobStatus > toDelete = Sets .difference (existing , toAdd );
276- toAdd = Sets .difference (toAdd , existing );
278+ Set <FeatureSetJobStatus .FeatureSetJobStatusKey > toDelete =
279+ Sets .difference (existing .keySet (), current .keySet ());
280+ Set <FeatureSetJobStatus .FeatureSetJobStatusKey > toAdd =
281+ Sets .difference (current .keySet (), existing .keySet ());
277282
278- jobStatusRepository .deleteAll (toDelete );
279- jobStatusRepository .saveAll (toAdd );
283+ jobStatusRepository .deleteAll (toDelete . stream (). map ( existing :: get ). collect ( Collectors . toSet ()) );
284+ jobStatusRepository .saveAll (toAdd . stream (). map ( current :: get ). collect ( Collectors . toSet ()) );
280285 jobStatusRepository .flush ();
281286 return featureSet ;
282287 }
@@ -382,15 +387,17 @@ public void notifyJobsWhenFeatureSetUpdated() {
382387 // FeatureSet).
383388 // We now set status to IN_PROGRESS, so listenAckFromJobs would be able to
384389 // monitor delivery progress for each new version.
385- fs .getJobStatuses ().stream ()
390+ Set <FeatureSetJobStatus > jobStatuses = fs .getJobStatuses ();
391+ jobStatuses .stream ()
386392 .filter (s -> s .getJob ().isRunning ())
387393 .forEach (
388394 jobStatus -> {
389395 jobStatus .setDeliveryStatus (
390396 FeatureSetProto .FeatureSetJobDeliveryStatus .STATUS_IN_PROGRESS );
391397 jobStatus .setVersion (fs .getVersion ());
392398 });
393- featureSetRepository .saveAndFlush (fs );
399+ jobStatusRepository .saveAll (jobStatuses );
400+ jobStatusRepository .flush ();
394401 });
395402 }
396403
0 commit comments