Skip to content

Commit 1ecb8a4

Browse files
authored
fix: propagate transaction through aggregation pipeline (#20772)
Addresses an issue where transactions in AggregationQuery.Pipeline() may not be properly propagated. This change ensures the transaction is retained, and updates the logic in Execute() as well as updating tests. related: b/577989416
1 parent 4504821 commit 1ecb8a4

4 files changed

Lines changed: 147 additions & 0 deletions

File tree

‎firestore/pipeline.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,20 @@ func (p *Pipeline) Execute(ctx context.Context, opts ...ExecuteOption) *Pipeline
251251
}
252252
}
253253

254+
if newP.tx != nil {
255+
if newP.tx.err != nil {
256+
return &PipelineSnapshot{
257+
iter: &PipelineResultIterator{err: newP.tx.err},
258+
}
259+
}
260+
if len(newP.tx.writes) > 0 {
261+
newP.tx.readAfterWrite = true
262+
return &PipelineSnapshot{
263+
iter: &PipelineResultIterator{err: errReadAfterWrite},
264+
}
265+
}
266+
}
267+
254268
if newP.tx != nil || newP.executeSettings.Atomic {
255269
ctx = withTransactionHeader(ctx)
256270
}

‎firestore/query.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,7 @@ func (q Query) Pipeline() *Pipeline {
22582258
// and an aggregate stage will be added for the aggregations.
22592259
func (aq *AggregationQuery) Pipeline() *Pipeline {
22602260
p := aq.query.toPipeline()
2261+
p.tx = aq.tx
22612262
if p.err != nil {
22622263
return p
22632264
}

‎firestore/query_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package firestore
1616

1717
import (
18+
"bytes"
1819
"context"
1920
"fmt"
2021
"math"
@@ -26,6 +27,7 @@ import (
2627
"cloud.google.com/go/internal/pretty"
2728
"github.com/google/go-cmp/cmp"
2829
"github.com/google/go-cmp/cmp/cmpopts"
30+
"google.golang.org/grpc/metadata"
2931
"google.golang.org/protobuf/reflect/protoreflect"
3032
"google.golang.org/protobuf/testing/protocmp"
3133
tspb "google.golang.org/protobuf/types/known/timestamppb"
@@ -1948,6 +1950,66 @@ func TestAggregationQuery_Pipeline(t *testing.T) {
19481950
})
19491951
}
19501952
}
1953+
1954+
func TestAggregationQuery_Pipeline_Transaction(t *testing.T) {
1955+
t.Parallel()
1956+
client, _, cleanup := newMock(t)
1957+
defer cleanup()
1958+
1959+
coll := client.Collection("C")
1960+
tx := &Transaction{id: []byte("tx-123")}
1961+
1962+
testCases := []struct {
1963+
name string
1964+
pipe *Pipeline
1965+
}{
1966+
{
1967+
name: "with aggregations",
1968+
pipe: coll.NewAggregationQuery().WithCount("total").Transaction(tx).Pipeline(),
1969+
},
1970+
{
1971+
name: "without aggregations",
1972+
pipe: coll.NewAggregationQuery().Transaction(tx).Pipeline(),
1973+
},
1974+
{
1975+
name: "with chained stage after Pipeline",
1976+
pipe: coll.NewAggregationQuery().WithCount("total").Transaction(tx).Pipeline().Limit(1),
1977+
},
1978+
{
1979+
name: "via CreateFromAggregationQuery",
1980+
pipe: client.Pipeline().CreateFromAggregationQuery(coll.NewAggregationQuery().WithCount("total").Transaction(tx)),
1981+
},
1982+
}
1983+
1984+
for _, tc := range testCases {
1985+
t.Run(tc.name, func(t *testing.T) {
1986+
t.Parallel()
1987+
if tc.pipe.err != nil {
1988+
t.Fatalf("Pipeline() got error: %v", tc.pipe.err)
1989+
}
1990+
if tc.pipe.tx != tx {
1991+
t.Fatalf("Pipeline().tx = %v, want %v", tc.pipe.tx, tx)
1992+
}
1993+
req, err := tc.pipe.toExecutePipelineRequest()
1994+
if err != nil {
1995+
t.Fatalf("toExecutePipelineRequest() got error: %v", err)
1996+
}
1997+
if got := req.GetTransaction(); !bytes.Equal(got, tx.id) {
1998+
t.Errorf("req.GetTransaction() = %q, want %q", got, tx.id)
1999+
}
2000+
2001+
snap := tc.pipe.Execute(context.Background())
2002+
streamIter, ok := snap.iter.iter.(*streamPipelineResultIterator)
2003+
if !ok {
2004+
t.Fatalf("expected *streamPipelineResultIterator, got %T (err: %v)", snap.iter.iter, snap.iter.err)
2005+
}
2006+
md, _ := metadata.FromOutgoingContext(streamIter.ctx)
2007+
if got := md.Get(isTransactionHeader); len(got) != 1 || got[0] != "true" {
2008+
t.Errorf("%s header: got %v, want [true]", isTransactionHeader, got)
2009+
}
2010+
})
2011+
}
2012+
}
19512013
func TestFindNearest(t *testing.T) {
19522014
ctx := context.Background()
19532015
c, srv, cleanup := newMock(t)

‎firestore/transaction_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,27 @@ func TestTransactionErrors(t *testing.T) {
371371
}
372372
})
373373

374+
t.Run("Read after write, with AggregationQuery.Transaction.Pipeline", func(t *testing.T) {
375+
srv.reset()
376+
srv.addRPC(beginReq, beginRes)
377+
srv.addRPC(rollbackReq, &emptypb.Empty{})
378+
err := c.RunTransaction(ctx, func(cbCtx context.Context, tx *Transaction) error {
379+
if err := tx.Delete(c.Doc("C/a")); err != nil {
380+
return err
381+
}
382+
p := c.Collection("C").NewAggregationQuery().WithCount("n").Transaction(tx).Pipeline()
383+
it := p.Execute(cbCtx).Results()
384+
it.Stop()
385+
return it.err
386+
})
387+
if err != errReadAfterWrite {
388+
t.Errorf("got <%v>, want <%v>", err, errReadAfterWrite)
389+
}
390+
if !srv.isEmpty() {
391+
t.Errorf("Expected %+v requests but not received. srv.reqItems: %+v", len(srv.reqItems), srv.reqItems)
392+
}
393+
})
394+
374395
t.Run("Read after write fails even if the user ignores the read's error", func(t *testing.T) {
375396
srv.reset()
376397
srv.addRPC(beginReq, beginRes)
@@ -927,3 +948,52 @@ func TestTransaction_WithReadOptions_Empty_AfterTransactionReadTime(t *testing.T
927948
t.Fatal(err)
928949
}
929950
}
951+
952+
func TestRunTransaction_AggregationQueryPipeline(t *testing.T) {
953+
ctx := context.Background()
954+
c, srv, cleanup := newMock(t)
955+
defer cleanup()
956+
957+
const db = "projects/projectID/databases/(default)"
958+
tid := []byte{1}
959+
960+
beginReq := &pb.BeginTransactionRequest{Database: db}
961+
beginRes := &pb.BeginTransactionResponse{Transaction: tid}
962+
commitReq := &pb.CommitRequest{Database: db, Transaction: tid}
963+
964+
wantPipeReq, err := c.Collection("C").NewAggregationQuery().WithCount("n").Transaction(&Transaction{id: tid}).Pipeline().toExecutePipelineRequest()
965+
if err != nil {
966+
t.Fatal(err)
967+
}
968+
969+
srv.addRPC(beginReq, beginRes)
970+
srv.addRPC(wantPipeReq, []interface{}{
971+
&pb.ExecutePipelineResponse{
972+
Results: []*pb.Document{
973+
{
974+
Fields: map[string]*pb.Value{"n": intval(3)},
975+
},
976+
},
977+
ExecutionTime: aTimestamp,
978+
},
979+
})
980+
srv.addRPC(commitReq, &pb.CommitResponse{CommitTime: aTimestamp})
981+
982+
err = c.RunTransaction(ctx, func(cbCtx context.Context, tx *Transaction) error {
983+
aq := c.Collection("C").NewAggregationQuery().WithCount("n").Transaction(tx)
984+
results, err := aq.Pipeline().Execute(cbCtx).Results().GetAll()
985+
if err != nil {
986+
return err
987+
}
988+
if len(results) != 1 {
989+
t.Fatalf("got %d results, want 1", len(results))
990+
}
991+
if got := results[0].Data()["n"]; got != int64(3) {
992+
t.Fatalf("got n = %v, want 3", got)
993+
}
994+
return nil
995+
})
996+
if err != nil {
997+
t.Fatal(err)
998+
}
999+
}

0 commit comments

Comments
 (0)
⌂ 换网址