Ejemplo n.º 1
0
//write a sequence of commands to stable storage
func (r *Replica) recordCommand(cmd *state.Command) {
	if !r.Durable {
		return
	}

	if cmd == nil {
		return
	}
	cmd.Marshal(io.Writer(r.StableStore))
}
Ejemplo n.º 2
0
func (r *Replica) send1b(msg *gpaxosproto.M_1b, w *bufio.Writer) {
	w.WriteByte(gpaxosproto.M1B)
	msg.Marshal(w)
	dummy := state.Command{0, 0, 0}
	for _, cid := range msg.Cstruct {
		if cmd, present := r.commands[cid]; present {
			cmd.Marshal(w)
		} else {
			dummy.Marshal(w)
		}
	}
	w.Flush()
}
Ejemplo n.º 3
0
func (r *Replica) handleReplicaConnection(rid int, reader *bufio.Reader) error {
	var msgType byte
	var err error
	for !r.Shutdown && err == nil {

		if msgType, err = reader.ReadByte(); err != nil {
			break
		}

		switch uint8(msgType) {

		case gpaxosproto.PREPARE:
			prep := new(gpaxosproto.Prepare)
			if err = prep.Unmarshal(reader); err != nil {
				break
			}
			r.prepareChan <- prep
			break

			/*        case gpaxosproto.PREPARE_REPLY:
			          preply := new(gpaxosproto.PrepareReply)
			          if err = preply.Unmarshal(reader); err != nil {
			             break
			          }
			          r.prepareReplyChan <- preply
			          break*/

		case gpaxosproto.M1A:
			msg := new(gpaxosproto.M_1a)
			if err = msg.Unmarshal(reader); err != nil {
				break
			}
			r.m1aChan <- msg
			break

		case gpaxosproto.M1B:
			msg := new(gpaxosproto.M_1b)
			if err = msg.Unmarshal(reader); err != nil {
				break
			}

			//HACK
			for _, cid := range msg.Cstruct {
				cmd := new(state.Command)
				cmd.Unmarshal(reader)
				r.commandsMutex.Lock()
				if _, present := r.commands[cid]; !present {
					if cmd.Op != 0 || cmd.K != 0 || cmd.V != 0 {
						r.commands[cid] = cmd
					}
				}
				r.commandsMutex.Unlock()
			}

			r.m1bChan <- msg
			break

		case gpaxosproto.M2A:
			msg := new(gpaxosproto.M_2a)
			if err = msg.Unmarshal(reader); err != nil {
				break
			}
			r.m2aChan <- msg
			break

		case gpaxosproto.M2B:
			msg := new(gpaxosproto.M_2b)
			if err = msg.Unmarshal(reader); err != nil {
				break
			}
			//HACK
			for _, cid := range msg.Cids {
				cmd := new(state.Command)
				cmd.Unmarshal(reader)
				r.commandsMutex.Lock()
				if _, present := r.commands[cid]; !present {
					r.commands[cid] = cmd
				}
				r.commandsMutex.Unlock()
			}
			r.m2bChan <- msg
			break

		case gpaxosproto.COMMIT:
			commit := new(gpaxosproto.Commit)
			if err = commit.Unmarshal(reader); err != nil {
				break
			}
			r.commitChan <- commit
			break

		}
	}
	if err != nil {
		log.Println("Error when reading from the connection with replica", rid, err)
		r.Alive[rid] = false
		return err
	}
	return nil
}