Commit 30c69d9a authored by Levin Zimmermann's avatar Levin Zimmermann Committed by Kirill Smelkov

go/neo/proto: Fix KnownMasterList nesting

Before this patch, the 'KnownMasterList' field of the 'NotPrimaryMaster'
was expected to be structured in the following way:

	ArrayHeader (KnownMasterList)

	    ArrayHeader (KnownMaster)

		ArrayHeader (Address)

		    Host (string)
		    Port (uint16)

However NEO/py sends the following structure:

	ArrayHeader (KnownMasterList)

	    ArrayHeader (Address)

		Host (string)
		Port (uint16)

This also makes sense, as 'KnownMaster' doesn't need to add another
nesting, because it only includes the address.

This patch amends the NEO/go protocol definition to transparently
represent the nesting as it's send by NEO/py. See also
levin.zimmermann/neoppod@18287612 for a similar issue.

/reviewed-by @kirr
/reviewed-on !6
parent 2cf10114
...@@ -138,7 +138,7 @@ func (node *_MasteredNode) TalkMaster(ctx context.Context, f func(context.Contex ...@@ -138,7 +138,7 @@ func (node *_MasteredNode) TalkMaster(ctx context.Context, f func(context.Contex
} else { } else {
// TODO update masterRegistry from received KnownMasterList // TODO update masterRegistry from received KnownMasterList
primary := notPrimaryMaster.KnownMasterList[p] primary := notPrimaryMaster.KnownMasterList[p]
maddr = primary.Address.String() maddr = primary.String()
log.Info(ctx, "switching to try %s as primary master", maddr) log.Info(ctx, "switching to try %s as primary master", maddr)
} }
} }
......
...@@ -37,7 +37,7 @@ func TestNotPrimaryMasterError(t *testing.T) { ...@@ -37,7 +37,7 @@ func TestNotPrimaryMasterError(t *testing.T) {
"not primary master; primary: ? ; known masters: []", "not primary master; primary: ? ; known masters: []",
}, },
{&NotPrimaryMaster{1, []struct{Address}{{Address{"α",111}}, {Address{"β",222}}, {Address{"γ",333}}}}, {&NotPrimaryMaster{1, []Address{{"α",111}, {"β",222}, {"γ",333}}},
"not primary master; primary: β:222 ; known masters: [α:111 β:222 γ:333]", "not primary master; primary: β:222 ; known masters: [α:111 β:222 γ:333]",
}, },
} }
......
...@@ -456,9 +456,7 @@ type AnswerPrimary struct { ...@@ -456,9 +456,7 @@ type AnswerPrimary struct {
//neo:nodes SM -> * //neo:nodes SM -> *
type NotPrimaryMaster struct { type NotPrimaryMaster struct {
Primary int32 // index of PM in KnownMasterList Primary int32 // index of PM in KnownMasterList
KnownMasterList []struct { KnownMasterList []Address
Address
}
} }
// Notify information about one or more nodes. // Notify information about one or more nodes.
......
...@@ -357,15 +357,15 @@ func TestMsgMarshal(t *testing.T) { ...@@ -357,15 +357,15 @@ func TestMsgMarshal(t *testing.T) {
}, },
// NotPrimaryMaster (.Primary used to have wrong type) // NotPrimaryMaster (.Primary used to have wrong type)
{&NotPrimaryMaster{0x01020304, []struct{Address}{{Address{"m111", 111}}, {Address{"m222", 222}}}}, {&NotPrimaryMaster{0x01020304, []Address{{"m111", 111}, {"m222", 222}}},
// N // N
u32(0x01020304) + u32(2) + u32(4)+"m111"+u16(111) + u32(4)+"m222"+u16(222), u32(0x01020304) + u32(2) + u32(4)+"m111"+u16(111) + u32(4)+"m222"+u16(222),
// M // M
hex("92") + hex("92") +
hex("d2" + "01020304") + hex("d2" + "01020304") +
hex("92") + hex("92") +
hex("9192") + hex("c4")+u8(4)+"m111" + u8(111) + hex("92") + hex("c4")+u8(4)+"m111" + u8(111) +
hex("9192") + hex("c4")+u8(4)+"m222" + hex("ccde"), hex("92") + hex("c4")+u8(4)+"m222" + hex("ccde"),
}, },
// TODO we need tests for: // TODO we need tests for:
......
...@@ -811,7 +811,7 @@ func (p *NotPrimaryMaster) neoMsgDecodeN(data []byte) (int, error) { ...@@ -811,7 +811,7 @@ func (p *NotPrimaryMaster) neoMsgDecodeN(data []byte) (int, error) {
{ {
l := binary.BigEndian.Uint32(data[4 : 4+4]) l := binary.BigEndian.Uint32(data[4 : 4+4])
data = data[8:] data = data[8:]
p.KnownMasterList = make([]struct{ Address }, l) p.KnownMasterList = make([]Address, l)
for i := 0; uint32(i) < l; i++ { for i := 0; uint32(i) < l; i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
{ {
...@@ -834,9 +834,9 @@ func (p *NotPrimaryMaster) neoMsgEncodedLenM() int { ...@@ -834,9 +834,9 @@ func (p *NotPrimaryMaster) neoMsgEncodedLenM() int {
var size int var size int
for i := 0; i < len(p.KnownMasterList); i++ { for i := 0; i < len(p.KnownMasterList); i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
size += msgpack.BinHeadSize(len((*a).Address.Host)) + len((*a).Address.Host) + msgpack.Uint16Size((*a).Address.Port) size += msgpack.BinHeadSize(len((*a).Host)) + len((*a).Host) + msgpack.Uint16Size((*a).Port)
} }
return 1 + msgpack.Int32Size(p.Primary) + msgpack.ArrayHeadSize(len(p.KnownMasterList)) + len(p.KnownMasterList)*2 + size return 1 + msgpack.Int32Size(p.Primary) + msgpack.ArrayHeadSize(len(p.KnownMasterList)) + len(p.KnownMasterList)*1 + size
} }
func (p *NotPrimaryMaster) neoMsgEncodeM(data []byte) { func (p *NotPrimaryMaster) neoMsgEncodeM(data []byte) {
...@@ -851,17 +851,16 @@ func (p *NotPrimaryMaster) neoMsgEncodeM(data []byte) { ...@@ -851,17 +851,16 @@ func (p *NotPrimaryMaster) neoMsgEncodeM(data []byte) {
data = data[0+n:] data = data[0+n:]
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
data[0] = byte(msgpack.FixArray_4 | 1) data[0] = byte(msgpack.FixArray_4 | 2)
data[1] = byte(msgpack.FixArray_4 | 2)
{ {
l := len((*a).Address.Host) l := len((*a).Host)
n := msgpack.PutBinHead(data[2:], l) n := msgpack.PutBinHead(data[1:], l)
data = data[2+n:] data = data[1+n:]
copy(data, (*a).Address.Host) copy(data, (*a).Host)
data = data[l:] data = data[l:]
} }
{ {
n := msgpack.PutUint16(data[0:], (*a).Address.Port) n := msgpack.PutUint16(data[0:], (*a).Port)
data = data[0+n:] data = data[0+n:]
} }
} }
...@@ -893,39 +892,36 @@ func (p *NotPrimaryMaster) neoMsgDecodeM(data []byte) (int, error) { ...@@ -893,39 +892,36 @@ func (p *NotPrimaryMaster) neoMsgDecodeM(data []byte) (int, error) {
} }
nread += uint64(len(data) - len(tail)) nread += uint64(len(data) - len(tail))
data = tail data = tail
p.KnownMasterList = make([]struct{ Address }, l) p.KnownMasterList = make([]Address, l)
for i := 0; uint32(i) < l; i++ { for i := 0; uint32(i) < l; i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
if len(data) < 2 { if len(data) < 1 {
goto overflow goto overflow
} }
if op, opOk := msgpack.Op(data[0]), msgpack.FixArray_4|1; op != opOk { if op, opOk := msgpack.Op(data[0]), msgpack.FixArray_4|2; op != opOk {
return 0, &mstructDecodeError{"NotPrimaryMaster", op, opOk} return 0, &mstructDecodeError{"NotPrimaryMaster", op, opOk}
} }
if op, opOk := msgpack.Op(data[1]), msgpack.FixArray_4|2; op != opOk { data = data[1:]
return 0, &mstructDecodeError{"NotPrimaryMaster.Address", op, opOk}
}
data = data[2:]
{ {
b, tail, err := msgp.ReadBytesZC(data) b, tail, err := msgp.ReadBytesZC(data)
if err != nil { if err != nil {
return 0, mdecodeErr("NotPrimaryMaster.Address.Host", err) return 0, mdecodeErr("NotPrimaryMaster.Host", err)
} }
(*a).Address.Host = string(b) (*a).Host = string(b)
nread += uint64(len(data) - len(tail)) nread += uint64(len(data) - len(tail))
data = tail data = tail
} }
{ {
v, tail, err := msgp.ReadUint16Bytes(data) v, tail, err := msgp.ReadUint16Bytes(data)
if err != nil { if err != nil {
return 0, mdecodeErr("NotPrimaryMaster.Address.Port", err) return 0, mdecodeErr("NotPrimaryMaster.Port", err)
} }
(*a).Address.Port = v (*a).Port = v
nread += uint64(len(data) - len(tail)) nread += uint64(len(data) - len(tail))
data = tail data = tail
} }
} }
nread += uint64(l) * 2 nread += uint64(l) * 1
} }
return 1 + int(nread), nil return 1 + int(nread), nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment