-
Notifications
You must be signed in to change notification settings - Fork 348
feat(go): Add PollMessagesInto #3499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b6a8be8
c5532ea
e743d85
f6d5717
bea9495
b63e969
3843204
9c666ce
1bbe539
98eb5ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package tcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/binary" | ||
| "testing" | ||
| "unsafe" | ||
|
|
||
| iggcon "github.com/apache/iggy/foreign/go/contracts" | ||
| ) | ||
|
|
||
| // buildPolledMessageResponse returns a valid PollMessages response body | ||
| // containing a single message with the given payload. | ||
| func buildPolledMessageResponse(payload []byte) []byte { | ||
| hdr := iggcon.MessageHeader{PayloadLength: uint32(len(payload))} | ||
| hdrBytes := hdr.ToBytes() | ||
|
|
||
| // [partitionId:4][currentOffset:8][messageCount:4][header:64][payload:N] | ||
| buf := make([]byte, 4+8+4+len(hdrBytes)+len(payload)) | ||
| binary.LittleEndian.PutUint32(buf[0:4], 1) | ||
| binary.LittleEndian.PutUint64(buf[4:12], 0) | ||
| binary.LittleEndian.PutUint32(buf[12:16], 1) | ||
| copy(buf[16:], hdrBytes) | ||
| copy(buf[16+len(hdrBytes):], payload) | ||
| return buf | ||
| } | ||
|
|
||
| func basePtr(b []byte) uintptr { | ||
| if len(b) == 0 { | ||
| return 0 | ||
| } | ||
| return uintptr(unsafe.Pointer(&b[0])) | ||
| } | ||
|
|
||
| func TestPollMessagesInto_given_repeated_polls_when_payload_size_is_constant_should_reuse_buffer(t *testing.T) { | ||
| c, serverConn := newTestClient(t) | ||
|
|
||
| payload := []byte("hello iggy") | ||
| responseBody := buildPolledMessageResponse(payload) | ||
|
|
||
| streamId, _ := iggcon.NewIdentifier(uint32(1)) | ||
| topicId, _ := iggcon.NewIdentifier(uint32(1)) | ||
| consumerId, _ := iggcon.NewIdentifier(uint32(1)) | ||
|
|
||
| var buf []byte | ||
| var firstPtr uintptr | ||
|
|
||
| for i := range 3 { | ||
| go serverRespond(t, serverConn, 0, responseBody) | ||
|
|
||
| var polled *iggcon.PolledMessage | ||
| var err error | ||
| polled, buf, err = c.PollMessagesInto( | ||
| context.Background(), | ||
| streamId, | ||
| topicId, | ||
| iggcon.NewSingleConsumer(consumerId), | ||
| iggcon.NextPollingStrategy(), | ||
| 1, | ||
| false, | ||
| nil, | ||
| buf, | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("poll %d: unexpected error: %v", i, err) | ||
| } | ||
| if len(polled.Messages) != 1 { | ||
| t.Fatalf("poll %d: expected 1 message, got %d", i, len(polled.Messages)) | ||
| } | ||
| if string(polled.Messages[0].Payload) != string(payload) { | ||
| t.Errorf("poll %d: got payload %q, want %q", i, polled.Messages[0].Payload, payload) | ||
| } | ||
|
|
||
| ptr := basePtr(buf) | ||
| if i == 0 { | ||
| firstPtr = ptr | ||
| } else if ptr != firstPtr { | ||
| t.Errorf("poll %d: buffer reallocated (addr changed from %x to %x)", i, firstPtr, ptr) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,20 @@ type Client interface { | |
| partitionId *uint32, | ||
| ) (*PolledMessage, error) | ||
|
|
||
| // Payload and UserHeaders in the returned messages alias buf; copy out bytes | ||
| // you need before calling PollMessagesInto again. | ||
| PollMessagesInto( | ||
| ctx context.Context, | ||
| streamId Identifier, | ||
| topicId Identifier, | ||
| consumer Consumer, | ||
| strategy PollingStrategy, | ||
| count uint32, | ||
| autoCommit bool, | ||
| partitionId *uint32, | ||
| buf []byte, | ||
| ) (*PolledMessage, []byte, error) | ||
|
Comment on lines
+119
to
+129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding |
||
|
|
||
| // StoreConsumerOffset store the consumer offset for a specific consumer or consumer group for the given stream and topic by unique IDs or names. | ||
| // Authentication is required, and the permission to poll the messages. | ||
| StoreConsumerOffset( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the server returns an empty body (length <= 1),
sendLockedIntoreturnsbufunchanged -- still holding data from the previous call.DeserializeFetchMessagesResponsethen re-parses that stale data and returns ghost messages. The old code returned[]byte{}here. I think you needreturn buf[:0], nilto preserve the reusable backing array without leaking old contents.I reproduced this locally: first poll returns 1 message, second poll has an empty server response, but caller still sees 1 stale message from the first call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, added regression test