Skip to content
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

fix(pubsub/pstest): make Message.Modacks thread safe #2755

Merged
merged 8 commits into from
Aug 21, 2020
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
12 changes: 7 additions & 5 deletions pubsub/pstest/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,15 @@ type Message struct {
Data []byte
Attributes map[string]string
PublishTime time.Time
Deliveries int // number of times delivery of the message was attempted
Acks int // number of acks received from clients
Deliveries int // number of times delivery of the message was attempted
Acks int // number of acks received from clients
Modacks []Modack // modacks received by server for this message
OrderingKey string

// protected by server mutex
deliveries int
acks int
Modacks []Modack // modacks received by server for this message

modacks []Modack
}

// Modack represents a modack sent to the server.
Expand All @@ -187,6 +187,7 @@ func (s *Server) Messages() []*Message {
for _, m := range s.GServer.msgs {
m.Deliveries = m.deliveries
m.Acks = m.acks
m.Modacks = append([]Modack(nil), m.modacks...)
msgs = append(msgs, m)
}
return msgs
Expand All @@ -202,6 +203,7 @@ func (s *Server) Message(id string) *Message {
if m != nil {
m.Deliveries = m.deliveries
m.Acks = m.acks
m.Modacks = append([]Modack(nil), m.modacks...)
}
return m
}
Expand Down Expand Up @@ -657,7 +659,7 @@ func (s *GServer) ModifyAckDeadline(_ context.Context, req *pb.ModifyAckDeadline
}
now := time.Now()
for _, id := range req.AckIds {
s.msgsByID[id].Modacks = append(s.msgsByID[id].Modacks, Modack{AckID: id, AckDeadline: req.AckDeadlineSeconds, ReceivedAt: now})
s.msgsByID[id].modacks = append(s.msgsByID[id].modacks, Modack{AckID: id, AckDeadline: req.AckDeadlineSeconds, ReceivedAt: now})
}
dur := secsToDur(req.AckDeadlineSeconds)
for _, id := range req.AckIds {
Expand Down
42 changes: 42 additions & 0 deletions pubsub/pstest/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,48 @@ func TestTimeNowFunc(t *testing.T) {
}
}

func TestModAck_Race(t *testing.T) {
ctx := context.Background()
pclient, sclient, server, cleanup := newFake(ctx, t)
defer cleanup()

top := mustCreateTopic(ctx, t, pclient, &pb.Topic{Name: "projects/P/topics/T"})
sub := mustCreateSubscription(ctx, t, sclient, &pb.Subscription{
Name: "projects/P/subscriptions/S",
Topic: top.Name,
AckDeadlineSeconds: 10,
})

publish(t, pclient, top, []*pb.PubsubMessage{
{Data: []byte("d1")},
{Data: []byte("d2")},
{Data: []byte("d3")},
})
msgs := streamingPullN(ctx, t, 3, sclient, sub)
var ackIDs []string
for _, m := range msgs {
ackIDs = append(ackIDs, m.AckId)
}

// Try to access m.Modacks while simultaneously calling ModifyAckDeadline
// so as to try and create a race condition.
// Invoke ModifyAckDeadline from the server rather than the client
// to increase replicability of simultaneous data access.
go func() {
req := &pb.ModifyAckDeadlineRequest{
Subscription: sub.Name,
AckIds: ackIDs,
AckDeadlineSeconds: 0,
}
server.GServer.ModifyAckDeadline(ctx, req)
}()

sm := server.Messages()
for _, m := range sm {
t.Logf("got modacks: %v\n", m.Modacks)
}
}

func TestUpdateDeadLetterPolicy(t *testing.T) {
pclient, sclient, _, cleanup := newFake(context.TODO(), t)
defer cleanup()
Expand Down