Commits

sherwinyu committed ff620f1

fixed compile time errors, added some nOld stuff

  • Participants
  • Parent commits 807cc58

Comments (0)

Files changed (1)

File server/consensus/classicpaxos.go

 	parts := strings.Split(msg, " ")
 
 	/*
-	  Messages have the following format:
-	  COMMANDNAME  proposal_num_n  v_as_a_string 
+			  Messages have the following format:
+		          PREPARE n
+			  PROMISE n nOld vOld
+		          ACCEPT n v
+		          ACCEPTED n v
+
 	*/
 
 	// if we don't have at least 2 (command and n), it's an error
 	if len(parts) < 2 {
 		log.Fatal("paxos received message with only one component")
 	}
-
 	n, err := strconv.Atoi(parts[1])
 
 	if err != nil {
 	// if we have 3 (command n v), v becomes non-empty. otherwise, v = ""
 	// this case neesd to be considered for PROMISE messages
 	var v string = ""
-	if len(parts) == 3 {
-		v = parts[3]
-	}
 
 	switch parts[0] {
 	case "PREPARE":
 		paxos.OnPrepare(from, n)
 	case "PROMISE":
-		paxos.OnPromise(from, n, v)
+		if len(parts) != 4 {
+			log.Fatal("paxos PROMISE incorrect message args")
+		}
+		nOld, err := strconv.Atoi(parts[2])
+		if err != nil {
+			log.Fatal("paxos PROMISE failed nOld conversion: ", err)
+		}
+
+		// if the sender has already promised someting
+		if nOld != 0 {
+			v = parts[3]
+		}
+		paxos.OnPromise(from, n, nOld, v)
 	case "ACCEPT":
+		if len(parts) != 3 {
+			log.Fatal("paxos ACCEPTK incorrect message args")
+		}
+		v = parts[2]
 		paxos.OnAccept(from, n, v)
 	case "ACCEPTED":
+		if len(parts) != 3 {
+			log.Fatal("paxos ACCEPTK incorrect message args")
+		}
+		v = parts[2]
 		paxos.OnAccepted(from, n, v)
 	}
 
 }
 
 func (paxos *ClassicPaxos) hasPromiseMajority() bool {
-	return (paxos.currentProposal.numPromised > paxos.numPeers / 2)
+	return (paxos.currentProposal.numPromised > paxos.numPeers/2)
 }
 
 func (paxos *ClassicPaxos) hasAcceptMajority() bool {
-	return (paxos.currentProposal.numAccepted > paxos.numPeers / 2)
+	return (paxos.currentProposal.numAccepted > paxos.numPeers/2)
 }
 
 func (paxos *ClassicPaxos) proposalExpired() bool {
 		return true
 	}
 
-	return time.Now().Unix() - paxos.currentProposal.time > ProposalTTL
+	return time.Now().Unix()-paxos.currentProposal.time > ProposalTTL
 }
 
 func (paxos *ClassicPaxos) resetCurrentProposal() {
 	}
 
 	paxos.lastPromisedN = n
-	paxos.sendPromise(from, n, paxos.acceptedV)
+	// CHANGED(syu): added nOld as third argument:
+	// paxos.sendPromise(from, n, paxos.acceptedV)
+  nOld := paxos.currentProposal.highestN
+	paxos.sendPromise(from, n, nOld, paxos.acceptedV)
 }
 
 /*
 	}
 
 	paxos.currentProposal.numPromised++
-	if nold > paxos.currentProposal.highestN {
+	if nOld > paxos.currentProposal.highestN {
 		paxos.currentProposal.highestN = nOld
 		paxos.currentProposal.highestV = v
 	}
 	paxos.acceptedN = 0
 	paxos.acceptedV = ""
 }
-