Skip to content

[Go] Fix flyweight DecodeLength() and version-gated groups for messages from other schema versions - #1197

Open
QuantIntellect wants to merge 2 commits into
aeron-io:masterfrom
QuantIntellect:go-flyweight-versioning
Open

QuantIntellect wants to merge 2 commits into
aeron-io:masterfrom
QuantIntellect:go-flyweight-versioning

Conversation

@QuantIntellect

Copy link
Copy Markdown

Two schema-versioning bugs in the Go flyweight codecs, one commit each. Either one makes DecodeLength() wrong when a message was encoded with a different schema version.

DecodeLength() uses the schema's block length. It wraps its skipper with SbeBlockLength() instead of the block length from the message header. Once a field has been added to the root block, groups and var data are read from the wrong offset. C++ had the same code and fixed it in #1045 (#1044). With the versioned-message-v1/v2.xml schemas from that PR, a V1 decoder of a V2 message returns 13 instead of 26, the value the C++ VersionedMessageTest expects. A V2 decoder of a V1 message panics with buffer too short [E100] instead of returning 14. The fix passes m.actingBlockLength and m.actingVersion, as sbeRewind() already does.

Groups ignore their sinceVersion when decoding. A group's WrapForDecode() always reads a group header at the current position. It does this even when the acting version is below the group's sinceVersion and the group is not on the wire. A decoder for the newer schema then reads the following bytes (the next group entry, the var data, or the next message in a batch) as a group header. The generated Skip(), DecodeLength() and String() go through the group accessors, so they walk phantom entries even when the caller checks <Group>InActingVersion() first. Field and var data accessors in the same generator already handle this case.

A group with a sinceVersion now has a notPresent() method like the C++ one. Its WrapForDecode() calls it to return an empty group without moving the position. Java returns a count of 0 here and Rust returns None. C++ does the check in the accessor. Here it is in WrapForDecode(), because a branch in the accessor puts the accessor over Go's inlining budget; this way the accessors still inline as before. The encode path is unchanged.

To reproduce the second bug: gocode/resources/versioned-groups-v2.xml adds two groups with sinceVersion="2" to versioned-groups-v1.xml, both before the var data: a nested group inside Group1 and a top-level Group2. All groups share one groupSizeEncoding. With V1 data and V2 codecs:

  • With an empty Group1, Group2InActingVersion() is false but Group2().Count() is 29537, read from the String1 bytes. String1() then panics with buffer too short [E100], and DecodeLength() with buffer too short for next group index [E108].
  • With two Group1 entries, the first entry's NestedGroup2 header is read from the second entry. The second entry's FieldB1 then comes back as 1935736836 (the String1 length and bytes), and its NestedGroup2() panics with buffer too short for flyweight [E107].

With this change the groups are empty, String1() is "asdf" and DecodeLength() equals the encoded length.

Tests:

  • gocode/flyweight/src/versmsg/VersionedMessage_test.go ports the C++ VersionedMessageTest case (V1 decodes V2) and adds the reverse. A new generateGolangFlyweightCodecTestVersionedMessage task generates the codecs. The versioned-message schemas have no sbe: namespace, so they can't go in the XSD-validated task.
  • gocode/flyweight/src/versgroup/VersionedGroups_test.go decodes V1 data with an empty Group1 and with a two-entry Group1, then decodes V2 data, all with the V2 codecs. Its schemas are added to generateGolangFlyweightCodecsWithXSD.
  • versmsg and versgroup are added to the flyweight loop in gocode/Makefile.

Verified:

  • The versmsg tests fail on master and pass after the first commit.
  • The V1-data versgroup test fails on master and after the first commit, and passes after the second.
  • ./gradlew clean build and ./gradlew assemble && cd gocode && make both pass.
  • The generated code is unchanged under gofmt, and go build -gcflags=-m still reports the group accessors as inlinable.

This overlaps with #1188, which also adds an argument to the group WrapForDecode() template in GolangFlyweightGenerator. The conflict is in that one format call, and I can rebase onto it if it goes in first.

DecodeLength() wrapped its skipper with the schema's SbeBlockLength()
instead of the block length from the message header, so it returned a
wrong length, or panicked with E100, when the message was encoded with
a different schema version. This is the same fix as aeron-io#1045 for C++. The
test ports the C++ VersionedMessageTest case (V1 decodes V2) and adds
the reverse.
A flyweight group's WrapForDecode() always read a group header at the
current position, even when the group's sinceVersion is above the
acting version and the group is not on the wire. A decoder for a newer
schema then read the following bytes as a group header, and the
generated Skip(), DecodeLength() and String() walked phantom entries
even when the caller checked <Group>InActingVersion() first. A group
with a sinceVersion now has a notPresent() method, as in the C++
codecs, and WrapForDecode() calls it to leave the group empty without
moving the position. The check is in WrapForDecode() rather than in
the accessor, where C++ has it, so that the accessor stays inlinable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant