@@ -16,6 +16,8 @@ export default class MapReduceMasterJob implements Job {
1616 private currentUsedMapWorkerIndex : number ;
1717 private mapWorkersToReach : number ;
1818 private mapWorkers : Array < MasterP2PConnection > ;
19+ private mapWorkersJobAcks : Map < string , boolean > ;
20+ private mapWorkersJobGuard : boolean ;
1921 private mapWorkersPerRegion : number ;
2022 private reduceWorkersToReach : number ;
2123 private reduceWorkers : Array < MasterP2PConnection > ;
@@ -35,6 +37,8 @@ export default class MapReduceMasterJob implements Job {
3537 Math . random ( ) * this . mapWorkersToReach
3638 ) ;
3739 this . mapWorkers = new Array < MasterP2PConnection > ( ) ;
40+ this . mapWorkersJobAcks = new Map ( ) ;
41+ this . mapWorkersJobGuard = false ;
3842 this . reduceWorkersToReach = params . reduceWorkers ;
3943 this . reduceWorkers = new Array < MasterP2PConnection > ( ) ;
4044 this . mapFunction = params . mapFunction ;
@@ -65,8 +69,10 @@ export default class MapReduceMasterJob implements Job {
6569 masterP2PConnection : MasterP2PConnection
6670 ) : Promise < void > {
6771 this . mapWorkers . push ( masterP2PConnection ) ;
72+ this . mapWorkersJobAcks . set ( masterP2PConnection . slaveId , false ) ;
6873 if ( this . mapWorkers . length === this . mapWorkersToReach ) {
6974 this . status = Status . REDUCE_WORKERS_RECRUITMENT ;
75+ console . log ( 'MAP WORKERS RECRUITED' ) ;
7076 const switchToReduceWorkersRecruitmentInterval = setInterval ( ( ) => {
7177 if ( this . mapWorkers . every ( ( mw ) => mw . remoteDescription !== undefined ) ) {
7278 this . brokerServiceSocket . emit (
@@ -78,6 +84,12 @@ export default class MapReduceMasterJob implements Job {
7884 masterRole : 'NODE' ,
7985 }
8086 ) ;
87+ while ( this . enqueuedTasks . length > 0 ) {
88+ const poppedTask = this . enqueuedTasks . pop ( ) ;
89+ if ( poppedTask !== undefined ) {
90+ this . executeSplit ( poppedTask ) ;
91+ }
92+ }
8193 clearInterval ( switchToReduceWorkersRecruitmentInterval ) ;
8294 }
8395 } , 100 ) ;
@@ -105,12 +117,6 @@ export default class MapReduceMasterJob implements Job {
105117 if ( this . mapWorkers . length === this . mapWorkersToReach ) {
106118 this . status = Status . RECRUITMENT_COMPLETED ;
107119 console . log ( 'RECRUITMENT COMPLETED' ) ;
108- while ( this . enqueuedTasks . length > 0 ) {
109- const poppedTask = this . enqueuedTasks . pop ( ) ;
110- if ( poppedTask !== undefined ) {
111- this . executeSplit ( poppedTask ) ;
112- }
113- }
114120 }
115121 return new Promise < void > ( ( resolve ) => {
116122 masterP2PConnection . sendMessage (
@@ -154,14 +160,20 @@ export default class MapReduceMasterJob implements Job {
154160 const mwUsed = new Array < MasterP2PConnection > ( ) ;
155161 const remainingMWsFromIndex =
156162 this . mapWorkers . length - this . currentUsedMapWorkerIndex ;
163+ console . log ( 'length ' + this . mapWorkers . length . toString ( ) ) ;
164+ console . log ( 'index ' + this . currentUsedMapWorkerIndex . toString ( ) ) ;
165+ console . log ( 'remaining ' + remainingMWsFromIndex . toString ( ) ) ;
166+ console . log ( 'per region ' + this . mapWorkersPerRegion ) ;
157167 if ( remainingMWsFromIndex >= this . mapWorkersPerRegion ) {
168+ console . log ( 'A' ) ;
158169 mwUsed . push (
159170 ...this . mapWorkers . slice (
160171 this . currentUsedMapWorkerIndex ,
161172 this . mapWorkersPerRegion
162173 )
163174 ) ;
164175 } else {
176+ console . log ( 'B' ) ;
165177 mwUsed . push (
166178 ...this . mapWorkers . slice (
167179 this . currentUsedMapWorkerIndex ,
@@ -175,28 +187,58 @@ export default class MapReduceMasterJob implements Job {
175187 )
176188 ) ;
177189 }
190+ console . log ( mwUsed . length . toString ( ) ) ;
178191 this . currentUsedMapWorkerIndex += this . mapWorkersPerRegion ;
179192 if ( this . currentUsedMapWorkerIndex >= this . mapWorkers . length ) {
180193 this . currentUsedMapWorkerIndex =
181194 this . mapWorkersPerRegion - remainingMWsFromIndex ;
182195 }
183- task . execute (
184- { mapWorkers : mwUsed } ,
185- ( ) => {
186- console . log ( 'MASTER COMPLETED SLICE TASK' ) ;
187- } ,
188- ( ) => {
189- 'MASTER HAD AN ERROR COMPLETING SLICE TASK' ;
196+ const interval = setInterval ( ( ) => {
197+ if ( this . mapWorkersJobGuard === true ) {
198+ clearInterval ( interval ) ;
199+ task . execute (
200+ { mapWorkers : mwUsed } ,
201+ ( ) => {
202+ console . log ( 'MASTER COMPLETED SLICE TASK' ) ;
203+ } ,
204+ ( ) => {
205+ 'MASTER HAD AN ERROR COMPLETING SLICE TASK' ;
206+ }
207+ ) ;
190208 }
191- ) ;
209+ } , 100 ) ;
192210 }
193211
194212 enqueueTask ( task : Task ) : Promise < boolean > {
195213 if ( this . status === Status . MAP_WORKERS_RECRUITMENT ) {
214+ console . log ( 'RECEIVED TASK, BUT ENQUEUED IT' ) ;
196215 this . enqueuedTasks . push ( task ) ;
197216 } else {
198217 this . executeSplit ( task ) ;
199218 }
200219 return new Promise < boolean > ( ( resolve ) => resolve ( true ) ) ;
201220 }
221+
222+ notifyNewMessage (
223+ masterP2PConnection : MasterP2PConnection ,
224+ msg : any
225+ ) : Promise < void > {
226+ const parsedMsg = JSON . parse ( msg ) ;
227+ if (
228+ parsedMsg . channel === 'START_JOB' &&
229+ parsedMsg . payload . name === 'MAP_WORKER' &&
230+ parsedMsg . payload . result === 'ACK'
231+ ) {
232+ this . mapWorkersJobAcks . set ( masterP2PConnection . slaveId , true ) ;
233+ if (
234+ this . mapWorkersJobAcks . size === this . mapWorkersToReach &&
235+ Array . from ( this . mapWorkersJobAcks . values ( ) ) . every ( ( s ) => {
236+ return s === true ;
237+ } )
238+ ) {
239+ this . mapWorkersJobGuard = true ;
240+ }
241+ }
242+ return new Promise < void > ( ( resolve ) => resolve ( ) ) ;
243+ }
202244}
0 commit comments