@@ -53,13 +53,14 @@ public class StageCoordinator
53
53
54
54
private final int stageId ;
55
55
private final boolean isQueued ;
56
+ private int downStreamWorkerNum ;
56
57
private final int fixedWorkerNum ;
57
58
private final TaskQueue <Task > taskQueue ;
58
59
private final Map <Long , Worker <CFWorkerInfo >> workerIdToWorkers = new ConcurrentHashMap <>();
59
60
// this.workers is used for dependency checking, no concurrent reads and writes
60
61
private final List <Worker <CFWorkerInfo >> workers = new ArrayList <>();
61
- private final Map <Long , Integer > workerIdToWorkerIndex = new ConcurrentHashMap <>();
62
- private final AtomicInteger workerIndexAssigner = new AtomicInteger ( 0 ) ;
62
+ private final Map <Long , List < Integer > > workerIdToWorkerIndex = new ConcurrentHashMap <>();
63
+ private int workerIndexAssigner ;
63
64
private final Object lock = new Object ();
64
65
65
66
/**
@@ -78,6 +79,8 @@ public StageCoordinator(int stageId, int workerNum)
78
79
this .isQueued = false ;
79
80
this .fixedWorkerNum = workerNum ;
80
81
this .taskQueue = null ;
82
+ this .downStreamWorkerNum = 0 ;
83
+ this .workerIndexAssigner = 0 ;
81
84
}
82
85
83
86
/**
@@ -94,6 +97,8 @@ public StageCoordinator(int stageId, List<Task> pendingTasks)
94
97
this .isQueued = true ;
95
98
this .fixedWorkerNum = 0 ;
96
99
this .taskQueue = new TaskQueue <>(pendingTasks );
100
+ this .downStreamWorkerNum = 0 ;
101
+ this .workerIndexAssigner = 0 ;
97
102
}
98
103
99
104
/**
@@ -105,7 +110,37 @@ public void addWorker(Worker<CFWorkerInfo> worker)
105
110
synchronized (this .lock )
106
111
{
107
112
this .workerIdToWorkers .put (worker .getWorkerId (), worker );
108
- this .workerIdToWorkerIndex .put (worker .getWorkerId (), this .workerIndexAssigner .getAndIncrement ());
113
+ if (fixedWorkerNum > 0 && downStreamWorkerNum > 0 )
114
+ {
115
+ if (downStreamWorkerNum > fixedWorkerNum )
116
+ {
117
+ // one-to-multiple stream
118
+ List <Integer > workerIndexs = new ArrayList <>();
119
+ int num = downStreamWorkerNum / fixedWorkerNum ;
120
+ if (downStreamWorkerNum > fixedWorkerNum *num )
121
+ {
122
+ num ++;
123
+ }
124
+ for (int i = 0 ; i < num ; i ++)
125
+ {
126
+ workerIndexs .add (this .workerIndexAssigner % this .downStreamWorkerNum );
127
+ this .workerIndexAssigner ++;
128
+ }
129
+ } else
130
+ {
131
+ // multiple-to-one stream
132
+ List <Integer > workerIndexes = new ArrayList <>();
133
+ workerIndexes .add (this .workerIndexAssigner % this .downStreamWorkerNum );
134
+ this .workerIndexAssigner ++;
135
+ this .workerIdToWorkerIndex .put (worker .getWorkerId (), workerIndexes );
136
+ }
137
+ } else
138
+ {
139
+ // assume one-to-one stream
140
+ List <Integer > workerIndexs = new ArrayList <>(this .workerIndexAssigner );
141
+ this .workerIndexAssigner ++;
142
+ this .workerIdToWorkerIndex .put (worker .getWorkerId (), workerIndexs );
143
+ }
109
144
this .workers .add (worker );
110
145
if (!this .isQueued && this .workers .size () == this .fixedWorkerNum )
111
146
{
@@ -191,14 +226,14 @@ public Worker<CFWorkerInfo> getWorker(long workerId)
191
226
* @param workerId the (global) id of the worker
192
227
* @return the index of the worker in this stage, or < 0 if the worker is not found
193
228
*/
194
- public int getWorkerIndex (long workerId )
229
+ public List < Integer > getWorkerIndex (long workerId )
195
230
{
196
- Integer index = this .workerIdToWorkerIndex .get (workerId );
231
+ List < Integer > index = this .workerIdToWorkerIndex .get (workerId );
197
232
if (index != null )
198
233
{
199
234
return index ;
200
235
}
201
- return - 1 ;
236
+ return null ;
202
237
}
203
238
204
239
/**
@@ -246,4 +281,20 @@ public List<Worker<CFWorkerInfo>> getWorkers()
246
281
{
247
282
return this .workers ;
248
283
}
284
+
285
+ /**
286
+ * set down stream workers num
287
+ */
288
+ public void setDownStreamWorkerNum (int downStreamWorkerNum )
289
+ {
290
+ this .downStreamWorkerNum = downStreamWorkerNum ;
291
+ }
292
+
293
+ /**
294
+ * get worker num of this stage
295
+ */
296
+ public int getFixedWorkerNum ()
297
+ {
298
+ return this .fixedWorkerNum ;
299
+ }
249
300
}
0 commit comments