@@ -133,24 +133,23 @@ module.exports.create = function(port) {
133133
134134 // For each name, store what instance
135135 // number we think we're on
136- var CURRENT_INSTANCE = 1 ;
136+ var CURRENT_INSTANCE = { } ;
137137
138138 // For each (name, instance) pair, track
139139 // what proposal number we should use next
140140 var PROPOSAL_COUNTER = { } ;
141141
142- var handlePropose = function ( instance , proposal ) {
143-
144- if ( RECEIVED_ACCEPTS . hasOwnProperty ( instance ) ) {
142+ var handlePropose = function ( name , instance , proposal ) {
143+ if ( RECEIVED_ACCEPTS [ name ] . hasOwnProperty ( instance ) ) {
145144 // We have already accepted something for this
146145 // instance
147- var accepted = RECEIVED_ACCEPTS [ instance ] ;
146+ var accepted = RECEIVED_ACCEPTS [ name ] [ instance ] ;
148147 var acceptedProposal = accepted . proposal ;
149148 var acceptedValue = accepted . value ;
150149
151150 // We also need to get our minimum promised proposal
152151 // number
153- var promisedProposal = RECEIVED_PROPOSALS [ instance ] . proposal ;
152+ var promisedProposal = RECEIVED_PROPOSALS [ name ] [ instance ] . proposal ;
154153
155154 log ( " " , "previously accepted:" , promisedProposal , "--" , acceptedValue ) ;
156155
@@ -161,10 +160,11 @@ module.exports.create = function(port) {
161160
162161 // However, we also need to note this new proposal,
163162 // because we promised not to accept anything less than it
164- RECEIVED_PROPOSALS [ instance ] . proposal = proposal ;
163+ RECEIVED_PROPOSALS [ name ] [ instance ] . proposal = proposal ;
165164
166165 log ( " " , "promising (already accepted)" , proposal , " -- " , acceptedValue ) ;
167166 return {
167+ name : name ,
168168 peer : port ,
169169 promise : true ,
170170 highestProposal : promisedProposal ,
@@ -178,27 +178,29 @@ module.exports.create = function(port) {
178178
179179 log ( " " , "rejecting (already accepted)" , promisedProposal , " -- " , acceptedValue ) ;
180180 return {
181+ name : name ,
181182 peer : port ,
182183 promise : false ,
183184 highestProposal : promisedProposal ,
184185 value : acceptedValue
185186 } ;
186187 }
187188 }
188- else if ( RECEIVED_PROPOSALS . hasOwnProperty ( instance ) ) {
189+ else if ( RECEIVED_PROPOSALS [ name ] . hasOwnProperty ( instance ) ) {
189190 // We have received a previous proposal for this
190191 // instance, but we haven't accepted any value yet
191- var promisedProposal = RECEIVED_PROPOSALS [ instance ] . proposal ;
192+ var promisedProposal = RECEIVED_PROPOSALS [ name ] [ instance ] . proposal ;
192193
193194 log ( " " , "previously promised:" , promisedProposal ) ;
194195 if ( greaterThan ( proposal , promisedProposal ) ) {
195196 // The proposal is igher than our previously
196197 // promised proposal, so we promise not to accept
197198 // anything higher than it
198- RECEIVED_PROPOSALS [ instance ] . proposal = proposal ;
199+ RECEIVED_PROPOSALS [ name ] [ instance ] . proposal = proposal ;
199200
200201 log ( " " , "promising" , proposal ) ;
201202 return {
203+ name : name ,
202204 peer : port ,
203205 promise : true ,
204206 highestProposal : proposal ,
@@ -212,6 +214,7 @@ module.exports.create = function(port) {
212214
213215 log ( " " , "rejecting" , proposal ) ;
214216 return {
217+ name : name ,
215218 peer : port ,
216219 promise : false ,
217220 highestProposal : promisedProposal ,
@@ -225,14 +228,15 @@ module.exports.create = function(port) {
225228
226229 // Store that this is the highest proposal number
227230 // we've seen
228- RECEIVED_PROPOSALS [ instance ] = {
231+ RECEIVED_PROPOSALS [ name ] [ instance ] = {
229232 proposal : proposal ,
230233 value : null
231234 } ;
232235
233236 log ( " " , "first proposal, accepting" , proposal ) ;
234237 // And return a promise saying we accept it
235238 return {
239+ name : name ,
236240 peer : port ,
237241 promise : true ,
238242 highestProposal : proposal ,
@@ -241,26 +245,26 @@ module.exports.create = function(port) {
241245 }
242246 } ;
243247
244- var handleAccept = function ( instance , proposal , value ) {
245- var promisedProposal = RECEIVED_PROPOSALS [ instance ] . proposal ;
248+ var handleAccept = function ( name , instance , proposal , value ) {
249+ var promisedProposal = RECEIVED_PROPOSALS [ name ] [ instance ] . proposal ;
246250 if ( greaterThan ( proposal , promisedProposal ) ) {
247251 // We haven't promised a higher proposal,
248252 // so we can still accept this request
249253 log ( " " , "beginning to accept" ) ;
250254
251- if ( RECEIVED_ACCEPTS . hasOwnProperty ( instance ) ) {
255+ if ( RECEIVED_ACCEPTS [ name ] . hasOwnProperty ( instance ) ) {
252256 // We're checking to see if we already accepted
253257 // something previously. This is just for logging
254258 // purposes
255- var acceptedProposal = RECEIVED_ACCEPTS [ instance ] . proposal ;
256- var acceptedValue = RECEIVED_ACCEPTS [ instance ] . value ;
259+ var acceptedProposal = RECEIVED_ACCEPTS [ name ] [ instance ] . proposal ;
260+ var acceptedValue = RECEIVED_ACCEPTS [ name ] [ instance ] . value ;
257261
258262 log ( " " , "previously accepted:" , acceptedProposal , acceptedValue ) ;
259263 }
260264
261265 // Store the accepted proposal and value
262266 // in the accept store
263- RECEIVED_ACCEPTS [ instance ] = {
267+ RECEIVED_ACCEPTS [ name ] [ instance ] = {
264268 proposal : proposal ,
265269 value : value
266270 } ;
@@ -270,6 +274,7 @@ module.exports.create = function(port) {
270274 peer . send (
271275 "/learn" ,
272276 {
277+ name : name ,
273278 instance : instance ,
274279 value : value ,
275280 peer : port
@@ -295,11 +300,11 @@ module.exports.create = function(port) {
295300
296301 } ;
297302
298- var handleLearn = function ( instance , value , peer ) {
299- if ( LEARNT_VALUES . hasOwnProperty ( instance ) ) {
303+ var handleLearn = function ( name , instance , value , peer ) {
304+ if ( LEARNT_VALUES [ name ] . hasOwnProperty ( instance ) ) {
300305 // We've already fully learned a value,
301306 // because we received a quorum for it
302- log ( " " , "ignoring, previously fully learned:" , LEARNT_VALUES [ instance ] ) ;
307+ log ( " " , "ignoring, previously fully learned:" , name , LEARNT_VALUES [ name ] [ instance ] ) ;
303308 return ;
304309 }
305310
@@ -308,7 +313,7 @@ module.exports.create = function(port) {
308313 var numAcceptors = 0 ;
309314
310315 // Get the learnt information for this particular instance (or initialize it)
311- var learned = tentativeLearnStore [ instance ] = ( tentativeLearnStore [ instance ] || { } ) ;
316+ var learned = tentativeLearnStore [ name ] [ instance ] = ( tentativeLearnStore [ name ] [ instance ] || { } ) ;
312317 if ( learned . hasOwnProperty ( value ) ) {
313318 // We've already seen this value for this instance, so
314319 // we just increment the value
@@ -323,30 +328,31 @@ module.exports.create = function(port) {
323328 // More than half the acceptors have accepted
324329 // this particular value, so we have now fully
325330 // learnt it
326- log ( " " , "fully learned: (" , instance , "," , value , ")" ) ;
327- LEARNT_VALUES [ instance ] = value ;
331+ log ( " " , "fully learned: (" , name , "," , instance , "," , value , ")" ) ;
332+ LEARNT_VALUES [ name ] [ instance ] = value ;
328333
329- if ( instance >= CURRENT_INSTANCE ) {
334+ if ( instance >= CURRENT_INSTANCE [ name ] ) {
330335 // The instance number is higher than the one
331336 // we have locally, which could happen if we got
332337 // out of sync. As such, we set our own instance
333338 // number to one higher.
334339 log ( " " , "setting instance:" , instance + 1 ) ;
335- CURRENT_INSTANCE = instance + 1 ;
340+ CURRENT_INSTANCE [ name ] = instance + 1 ;
336341 }
337342 }
338343 } ;
339344
340- var initiateAccept = function ( instance , proposal , value , originalValue , finalResponse ) {
345+ var initiateAccept = function ( name , instance , proposal , value , originalValue , finalResponse ) {
341346 // Create a set of tasks, where each task is sending the ACCEPT
342347 // message to a specific peer
343348 var acceptTasks = { } ;
344349 _ . each ( PEERS , function ( peer ) {
345- log ( "SEND ACCEPT(" , instance , "," , proposal , "," , value , ")" , "from" , port ) ;
350+ log ( "SEND ACCEPT(" , name , "," , instance , "," , proposal , "," , value , ")" , "from" , port ) ;
346351 acceptTasks [ peer . port ] = function ( done ) {
347352 peer . send (
348353 "/accept" ,
349354 {
355+ name : name ,
350356 instance : instance ,
351357 proposal : proposal ,
352358 value : value
@@ -393,12 +399,12 @@ module.exports.create = function(port) {
393399 }
394400 else {
395401 log ( "majority rejected" , accepted ) ;
396- initiateProposal ( instance , originalValue , finalResponse ) ;
402+ initiateProposal ( name , instance , originalValue , finalResponse ) ;
397403 }
398404 } ) ;
399405 } ;
400406
401- var initiateProposal = function ( instance , originalValue , finalResponse ) {
407+ var initiateProposal = function ( name , instance , originalValue , finalResponse ) {
402408 var value = originalValue ;
403409 var number = PROPOSAL_COUNTER [ instance ] = ( PROPOSAL_COUNTER [ instance ] || 0 ) + 1 ;
404410 var proposal = {
@@ -409,14 +415,13 @@ module.exports.create = function(port) {
409415 // Create a set of tasks, where each task is sending the PROPOSE
410416 // message to a specific peer
411417 var proposeTasks = { } ;
412- console . log ( port , PEERS ) ;
413418 _ . each ( PEERS , function ( peer ) {
414- console . log ( "peer" , peer ) ;
415419 proposeTasks [ peer . port ] = function ( done ) {
416- log ( "SEND PROPOSE(" , instance , "," , proposal , ")" , "from" , port ) ;
420+ log ( "SEND PROPOSE(" , name , "," , instance , "," , proposal , ")" , "from" , port ) ;
417421 peer . send (
418422 "/propose" ,
419423 {
424+ name : name ,
420425 instance : instance ,
421426 proposal : proposal
422427 } ,
@@ -491,7 +496,7 @@ module.exports.create = function(port) {
491496 // If we changed values, we still need to try and store
492497 // the original value the user sent us,
493498 // so we simply go again
494- initiateProposal ( CURRENT_INSTANCE ++ , originalValue , finalResponse ) ;
499+ initiateProposal ( name , CURRENT_INSTANCE [ name ] ++ , originalValue , finalResponse ) ;
495500
496501 // We reset the final response in the case where the value changed,
497502 // so that we only respond for the original request coming in from the
@@ -500,7 +505,7 @@ module.exports.create = function(port) {
500505 }
501506
502507 // Initiate the ACCEPT phase, but if we changed
503- initiateAccept ( instance , proposal , value , originalValue , finalResponse ) ;
508+ initiateAccept ( name , instance , proposal , value , originalValue , finalResponse ) ;
504509 }
505510 else {
506511 // We failed to update because somebody beat us in terms
@@ -511,11 +516,24 @@ module.exports.create = function(port) {
511516 log ( "Proposal rejected, setting new proposal number:" , newNumber ) ;
512517 PROPOSAL_COUNTER [ instance ] = newNumber ;
513518
514- initiateProposal ( instance , originalValue , finalResponse ) ;
519+ initiateProposal ( name , instance , originalValue , finalResponse ) ;
515520 }
516521 } ) ;
517522 } ;
518523
524+ var initializeStorageForName = function ( name ) {
525+ // If we haven't seen this name before,
526+ // then we initialize all our storage for it
527+ if ( ! RECEIVED_PROPOSALS . hasOwnProperty ( name ) ) {
528+ log ( "INITIALIZED STORAGE FOR" , name ) ;
529+ RECEIVED_PROPOSALS [ name ] = { } ;
530+ RECEIVED_ACCEPTS [ name ] = { } ;
531+ LEARNT_VALUES [ name ] = { } ;
532+ tentativeLearnStore [ name ] = { } ;
533+ CURRENT_INSTANCE [ name ] = 1 ;
534+ }
535+ } ;
536+
519537 /*******************************/
520538 /*******************************/
521539 /*******************************/
@@ -545,13 +563,17 @@ module.exports.create = function(port) {
545563
546564 app . post ( '/store' , function ( req , res ) {
547565 var data = req . body ;
566+ var name = data . name ;
548567 var value = data . value ;
549568
550- log ( "Received STORE(" , value , ")" ) ;
569+ log ( "Received STORE(" , name , "," , value , ")" ) ;
570+
571+ // Initialize our storage for this name
572+ initializeStorageForName ( name ) ;
551573
552574 // Get the next proposal number and build the proposal
553- var instance = CURRENT_INSTANCE ++ ;
554- initiateProposal ( instance , value , function ( err , result ) {
575+ var instance = CURRENT_INSTANCE [ name ] ++ ;
576+ initiateProposal ( name , instance , value , function ( err , result ) {
555577 if ( err ) {
556578 res . send ( err ) ;
557579 }
@@ -567,37 +589,52 @@ module.exports.create = function(port) {
567589
568590 app . post ( '/propose' , function ( req , res ) {
569591 var data = req . body ;
592+ var name = data . name ;
570593 var instance = data . instance ;
571594 var proposal = data . proposal ;
572595
573- log ( "RECEIVE PROPOSE(" , instance , "," , proposal , ")" ) ;
574- var result = handlePropose ( instance , proposal ) ;
596+ log ( "RECEIVE PROPOSE(" , name , "," , instance , "," , proposal , ")" ) ;
597+
598+ // Initialize our storage for this name
599+ initializeStorageForName ( name ) ;
600+
601+ var result = handlePropose ( name , instance , proposal ) ;
575602
576603 res . json ( result ) ;
577604 log ( "END PROPOSE" ) ;
578605 } ) ;
579606
580607 app . post ( '/accept' , function ( req , res ) {
581608 var data = req . body ;
609+ var name = data . name ;
582610 var instance = data . instance ;
583611 var proposal = data . proposal ;
584612 var value = data . value ;
585613
586- log ( "RECEIVE ACCEPT(" , instance , "," , proposal , "," , value , ")" ) ;
587- var result = handleAccept ( instance , proposal , value ) ;
614+ log ( "RECEIVE ACCEPT(" , name , "," , instance , "," , proposal , "," , value , ")" ) ;
615+
616+ // Initialize our storage for this name
617+ initializeStorageForName ( name ) ;
618+
619+ var result = handleAccept ( name , instance , proposal , value ) ;
588620
589621 res . json ( result ) ;
590622 log ( "END ACCEPT" ) ;
591623 } ) ;
592624
593625 app . post ( '/learn' , function ( req , res ) {
594626 var data = req . body ;
627+ var name = data . name ;
595628 var instance = data . instance ;
596629 var value = data . value ;
597630 var peer = data . peer ;
598631
599- log ( "RECEIVE LEARN(" , instance , "," , value , "," , peer , ")" ) ;
600- var result = handleLearn ( instance , value , peer ) ;
632+ log ( "RECEIVE LEARN(" , name , "," , instance , "," , value , "," , peer , ")" ) ;
633+
634+ // Initialize our storage for this name
635+ initializeStorageForName ( name ) ;
636+
637+ var result = handleLearn ( name , instance , value , peer ) ;
601638
602639 res . json ( result ) ;
603640
0 commit comments