Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. // +build !solution
  2.  
  3. package singlepaxos
  4.  
  5. // Learner represents a learner as defined by the single-decree Paxos
  6. // algorithm.
  7. type Learner struct {
  8. //TODO(student): Task 2 and 3 - algorithm and distributed implementation
  9. // Add needed fields
  10. id int
  11. nrOfNodes int
  12. greatestRound Round
  13.  
  14. mv map[int]bool
  15.  
  16. // Count frequency of words
  17. freq map[Value]int
  18. }
  19.  
  20. // NewLearner returns a new single-decree Paxos learner. It takes the
  21. // following arguments:
  22. //
  23. // id: The id of the node running this instance of a Paxos learner.
  24. //
  25. // nrOfNodes: The total number of Paxos nodes.
  26. //
  27. // valueOut: A send only channel used to send values that has been learned,
  28. // i.e. decided by the Paxos nodes.
  29. func NewLearner(id int, nrOfNodes int, valueOut chan<- Value) *Learner {
  30. //TODO(student): Task 2 and 3 - algorithm and distributed implementation
  31. return &Learner{id: id, nrOfNodes: nrOfNodes, greatestRound: Round(-1)}
  32. }
  33.  
  34. // Start starts l's main run loop as a separate goroutine. The main run loop
  35. // handles incoming learn messages.
  36. func (l *Learner) Start() {
  37. go func() {
  38. for {
  39. //TODO(student): Task 3 - distributed implementation
  40. }
  41. }()
  42. }
  43.  
  44. // Stop stops l's main run loop.
  45. func (l *Learner) Stop() {
  46. //TODO(student): Task 3 - distributed implementation
  47. }
  48.  
  49. // DeliverLearn delivers learn lrn to learner l.
  50. func (l *Learner) DeliverLearn(lrn Learn) {
  51. //TODO(student): Task 3 - distributed implementation
  52. }
  53.  
  54. // Internal: handleLearn processes learn lrn according to the single-decree
  55. // Paxos algorithm. If handling the learn results in learner l emitting a
  56. // corresponding decided value, then output will be true and val contain the
  57. // decided value. If handleLearn returns false as output, then val will have
  58. // its zero value.
  59. func (l *Learner) handleLearn(learn Learn) (val Value, output bool) {
  60. //TODO(student): Task 2 - algorithm implementation
  61.  
  62. // Ignores smaller rounds
  63. if learn.Rnd < l.greatestRound {
  64. return ZeroValue, false
  65. }
  66.  
  67. if _, ok := l.mv[learn.From]; ok {
  68. return ZeroValue, false
  69. }
  70.  
  71. // Clears map and increases greates round if a
  72. // higher round arrives
  73. if learn.Rnd > l.greatestRound {
  74. l.greatestRound = learn.Rnd
  75. l.mv = make(map[int]bool)
  76. l.freq = make(map[Value]int)
  77. }
  78.  
  79. // Add valid node
  80. l.mv[learn.From] = true
  81.  
  82. if _, ok := l.freq[learn.Val]; ok {
  83. l.freq[learn.Val] = l.freq[learn.Val] + 1
  84. } else {
  85. l.freq[learn.Val] = 1
  86. }
  87.  
  88. if len(l.mv) > (l.nrOfNodes / 2) {
  89. val := l.findVal()
  90. if val == "" {
  91. return "", false
  92. }
  93. return val, true
  94. }
  95.  
  96. return ZeroValue, false
  97. }
  98.  
  99. //TODO(student): Add any other unexported methods needed.
  100. func (l *Learner) findVal() Value {
  101.  
  102. for val, freq := range l.freq {
  103. if freq > l.nrOfNodes/2 {
  104. return val
  105. }
  106. }
  107. return ""
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement