Skip to content

queue: fix flaky Example_connectionPool #222

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

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
- Datetime location after encode + decode is unequal (#217)
- Wrong interval arithmetic with timezones (#221)
- Invalid MsgPack if STREAM_ID > 127 (#224)
- queue.Take() returns an invalid task (#222)

## [1.8.0] - 2022-08-17

Expand Down
2 changes: 1 addition & 1 deletion connection_pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func TestConnectionHandlerOpenUpdateClose(t *testing.T) {
for i := 0; i < 100; i++ {
// Wait for read_only update, it should report about close connection
// with old role.
if h.deactivated >= 1 {
if h.discovered >= 3 {
break
}
time.Sleep(poolOpts.CheckTimeout)
Expand Down
18 changes: 17 additions & 1 deletion queue/example_connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package queue_test
import (
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/google/uuid"
Expand All @@ -22,6 +23,7 @@ type QueueConnectionHandler struct {
err error
mutex sync.Mutex
masterUpdated chan struct{}
masterCnt int32
}

// QueueConnectionHandler implements the ConnectionHandler interface.
Expand Down Expand Up @@ -87,6 +89,7 @@ func (h *QueueConnectionHandler) Discovered(conn *tarantool.Connection,
}
}

atomic.AddInt32(&h.masterCnt, 1)
fmt.Printf("Master %s is ready to work!\n", conn.Addr())

return nil
Expand All @@ -95,6 +98,9 @@ func (h *QueueConnectionHandler) Discovered(conn *tarantool.Connection,
// Deactivated doesn't do anything useful for the example.
func (h *QueueConnectionHandler) Deactivated(conn *tarantool.Connection,
role connection_pool.Role) error {
if role == connection_pool.MasterRole {
atomic.AddInt32(&h.masterCnt, -1)
}
return nil
}

Expand Down Expand Up @@ -184,8 +190,18 @@ func Example_connectionPool() {
return
}

for i := 0; i < 2 && atomic.LoadInt32(&h.masterCnt) != 1; i++ {
// The pool does not immediately detect role switching. It may happen
// that requests will be sent to RO instances. In that case q.Take()
// method will return a nil value.
//
// We need to make the example test output deterministic so we need to
// avoid it here. But in real life, you need to take this into account.
time.Sleep(poolOpts.CheckTimeout)
}

// Take a data from the new master instance.
task, err := q.TakeTimeout(1 * time.Second)
task, err := q.Take()
if err != nil {
fmt.Println("Unable to got task:", err)
} else if task == nil {
Expand Down
10 changes: 9 additions & 1 deletion queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,14 @@ func (qd *queueData) DecodeMsgpack(d *decoder) error {
}

qd.task = &Task{data: qd.result, q: qd.q}
d.Decode(&qd.task)
if err = d.Decode(&qd.task); err != nil {
return err
}

if qd.task.Data() == nil {
// It may happen if the decoder has a code.Nil value inside. As a
// result, the task will not be decoded.
qd.task = nil
}
return nil
}
10 changes: 3 additions & 7 deletions queue/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ func (t *Task) DecodeMsgpack(d *decoder) error {
return err
}
if t.data != nil {
if err = d.Decode(t.data); err != nil {
return fmt.Errorf("fffuuuu: %s", err)
}
} else {
if t.data, err = d.DecodeInterface(); err != nil {
return err
}
d.Decode(t.data)
} else if t.data, err = d.DecodeInterface(); err != nil {
return err
}
return nil
}
Expand Down