Esempio n. 1
0
func (a *roleAcceptor) stepByMsgAccpt(ins *spaxosInstance, msg pb.Message) (bool, error) {
	assert(nil != ins)
	assert(msg.Index == ins.index)

	rsp := pb.Message{
		Type: pb.MsgAccptResp, Reject: false,
		Index: msg.Index, From: msg.To, To: msg.From,
		Entry: pb.PaxosEntry{PropNum: msg.Entry.PropNum}}

	if msg.Entry.PropNum < a.maxPromisedNum {
		rsp.Reject = true
		ins.append(rsp)
		return true, nil
	}

	a.acceptedCnt += 1
	if msg.Entry.PropNum == a.maxAcceptedNum {
		ins.append(rsp)
		return true, nil // do not repeat produce the same hs
	}

	assert(msg.Entry.PropNum > a.maxAcceptedNum)
	a.maxPromisedNum = msg.Entry.PropNum
	a.maxAcceptedNum = msg.Entry.PropNum
	a.acceptedValue = msg.Entry.Value

	ins.updateAccptHardState(a.maxPromisedNum, a.maxAcceptedNum, a.acceptedValue)
	ins.append(rsp)
	return true, nil
}
Esempio n. 2
0
func TestRunRecvMsg(t *testing.T) {
	groupsid := []uint64{1}
	peers := []string{":16001"}
	s := newSNet(1, groupsid, peers)

	err := s.RunRecvMsg()
	assert(nil == err)

	conn, err := net.Dial("tcp", peers[0])
	if nil != err {
		t.Error(err)
	}

	msg := pb.Message{Type: pb.MsgHup, To: 1, From: 1, Index: 10}
	pkg, err := msg.Marshal()
	if nil != err {
		t.Error(err)
	}

	err = sendMsg(conn, pkg)
	assert(nil == err)

	recvmsg := <-s.recvc
	{
		newpkg, err := recvmsg.Marshal()
		assert(nil == err)
		ret := bytes.Compare(pkg, newpkg)
		assert(0 == ret)
	}
}
Esempio n. 3
0
func (a *roleAcceptor) stepByMsgProp(ins *spaxosInstance, msg pb.Message) (bool, error) {
	assert(msg.Index == ins.index)

	rsp := pb.Message{
		Type: pb.MsgPropResp, Reject: false,
		Index: msg.Index, From: msg.To, To: msg.From,
		Entry: pb.PaxosEntry{PropNum: msg.Entry.PropNum}}
	if msg.Entry.PropNum < a.maxPromisedNum {
		rsp.Reject = true
		ins.append(rsp)
		return true, nil
	}

	if 0 != a.maxAcceptedNum {
		rsp.Entry.AccptNum = a.maxAcceptedNum
		rsp.Entry.Value = a.acceptedValue
	}

	a.promisedCnt += 1
	if msg.Entry.PropNum == a.maxPromisedNum {
		ins.append(rsp)
		return true, nil
	}

	a.maxPromisedNum = msg.Entry.PropNum

	ins.updateAccptHardState(a.maxPromisedNum, a.maxAcceptedNum, a.acceptedValue)
	ins.append(rsp)
	return true, nil
}
Esempio n. 4
0
// simple network wrapper
func handleConnection(
	conn net.Conn, recvc chan pb.Message, groupsid map[uint64]bool) {
	reader := bufio.NewReader(conn)
	for {
		var pkglen int
		{
			var totalLen uint32
			err := binary.Read(reader, binary.BigEndian, &totalLen)
			if nil != err {
				fmt.Println("binary.Read %s", err)
				return
			}
			pkglen = int(totalLen)
			assert(0 <= pkglen)
		}

		var pkg []byte
		for len(pkg) < pkglen {
			value := make([]byte, pkglen-len(pkg))
			readlen, err := reader.Read(value)
			if nil != err {
				return
			}

			assert(0 < readlen)
			pkg = append(pkg, value...)
		}

		assert(pkglen == len(pkg))
		msg := pb.Message{}
		err := msg.Unmarshal(pkg)
		if nil != err {
			return
		}

		if _, ok := groupsid[msg.From]; !ok {
			// invalid msg.From
			continue
		}

		// feed msg into node
		select {
		case recvc <- msg:
		}
	}
}