Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.task.exec

File ParallelLevelSet.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
10% of files have more coverage

Code metrics

2
27
6
1
115
101
8
0.3
4.5
6
1.33

Classes

Class Line # Actions
ParallelLevelSet 18 27 8
0.885714388.6%
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    package io.sunshower.service.task.exec;
2   
3    import io.reactivex.subjects.Subject;
4    import io.sunshower.common.Identifier;
5    import io.sunshower.service.model.task.TaskEvent;
6    import io.sunshower.service.model.task.TaskLogger;
7    import io.sunshower.service.task.TaskContext;
8    import java.util.Iterator;
9    import java.util.LinkedHashMap;
10    import java.util.Map;
11    import java.util.concurrent.CountDownLatch;
12    import java.util.concurrent.ExecutorService;
13    import java.util.logging.Level;
14    import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
15    import org.springframework.context.ApplicationContext;
16   
17    /** Created by haswell on 2/15/17. */
 
18    class ParallelLevelSet implements LevelSet<TaskElement> {
19   
20    int order;
21    CountDownLatch latch;
22    volatile boolean last;
23   
24    private final TaskLogger taskLogger;
25    private final TinkerGraph graph;
26   
27    private final TaskContext context;
28    private final Identifier sequenceId;
29    private final Identifier scheduleId;
30   
31    private final Subject<TaskEvent> subject;
32    private final ExecutorService executorService;
33    private final Map<String, TaskElement> elements;
34    private final ApplicationContext applicationContext;
35    private final ParallelTaskExecutor parallelTaskExecutor;
36   
 
37  73 toggle ParallelLevelSet(
38    final TaskLogger taskLogger,
39    final TaskContext context,
40    final Identifier scheduleId,
41    final TinkerGraph graph,
42    final Subject<TaskEvent> subject,
43    final ExecutorService executorService,
44    final ApplicationContext applicationContext,
45    final ParallelTaskExecutor parallelTaskExecutor) {
46  73 this.taskLogger = taskLogger;
47  73 this.graph = graph;
48  73 this.subject = subject;
49  73 this.context = context;
50  73 this.scheduleId = scheduleId;
51  73 this.sequenceId = Identifier.random();
52  73 this.elements = new LinkedHashMap<>();
53  73 this.executorService = executorService;
54  73 this.applicationContext = applicationContext;
55  73 this.parallelTaskExecutor = parallelTaskExecutor;
56    }
57   
 
58  161 toggle @Override
59    public boolean add(TaskElement element) {
60  161 return elements.put(element.vertex.label(), element) == null;
61    }
62   
 
63  68 toggle @Override
64    public int size() {
65  68 return elements.size();
66    }
67   
 
68  68 toggle void run() {
69  68 ParallelTaskExecutor.log.log(
70    Level.INFO,
71    "Beginning task set {0} on schedule {1}",
72    new Object[] {sequenceId, scheduleId});
73  68 subject.onNext(new TaskEvent(TaskEvent.Type.SequenceBeginning, scheduleId, sequenceId));
74  68 doRun();
75  68 if (last) {
76  0 subject.onNext(new TaskEvent(TaskEvent.Type.SequenceComplete, scheduleId, sequenceId));
77   
78  0 ParallelTaskExecutor.log.log(
79    Level.INFO,
80    "Task set {0} on schedule {1} complete",
81    new Object[] {sequenceId, scheduleId});
82    }
83    }
84   
 
85  68 toggle private void doRun() {
86  68 try {
87  68 CountDownLatch latch = new CountDownLatch(size());
88  68 for (TaskElement e : this) {
89  153 executorService.submit(
90    new TaskStep(
91    taskLogger,
92    context,
93    graph,
94    e,
95    scheduleId,
96    latch,
97    subject,
98    applicationContext,
99    parallelTaskExecutor));
100    }
101  68 try {
102  68 latch.await();
103    } catch (InterruptedException e) {
104  0 e.printStackTrace();
105    }
106    } finally {
107  68 this.latch.countDown();
108    }
109    }
110   
 
111  141 toggle @Override
112    public Iterator<TaskElement> iterator() {
113  141 return elements.values().iterator();
114    }
115    }