-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1479 Write message in transaction
- Loading branch information
Showing
29 changed files
with
702 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package topicwriter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/query" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicreader" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicwriter" | ||
) | ||
|
||
func CopyMessagesBetweenTopicsTxWriter( | ||
ctx context.Context, | ||
db *ydb.Driver, | ||
reader *topicreader.Reader, | ||
topic string, | ||
) error { | ||
return db.Query().DoTx(ctx, func(ctx context.Context, tx query.TxActor) error { | ||
writer, err := db.Topic().StartTransactionalWriter(tx, topic) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
batch, err := reader.PopMessagesBatchTx(ctx, tx) | ||
if err != nil { | ||
return err | ||
} | ||
for _, mess := range batch.Messages { | ||
if err = writer.Write(ctx, topicwriter.Message{Data: mess}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}, query.WithIdempotent()) | ||
} | ||
|
||
func TableAndTopicWithinTransaction( | ||
ctx context.Context, | ||
db *ydb.Driver, | ||
topicPath string, | ||
id int64, | ||
) error { | ||
return db.Query().DoTx(ctx, func(ctx context.Context, t query.TxActor) error { | ||
row, err := t.QueryRow(ctx, "SELECT val FROM table WHERE id=$id", query.WithParameters( | ||
ydb.ParamsBuilder(). | ||
Param("$id").Int64(id). | ||
Build())) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var val int64 | ||
if err = row.Scan(&val); err != nil { | ||
return err | ||
} | ||
|
||
// the writer is dedicated for the transaction, it can't be used outside the transaction | ||
// it is no needs to close or flush the messages - it happened internally on transaction commit | ||
writer, err := db.Topic().StartTransactionalWriter(t, topicPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = writer.Write(ctx, topicwriter.Message{ | ||
Data: strings.NewReader(fmt.Sprintf("val: %v processed", val)), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
} |
19 changes: 19 additions & 0 deletions
19
internal/grpcwrapper/rawtopic/rawtopiccommon/transaction.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package rawtopiccommon | ||
|
||
import "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Topic" | ||
|
||
type TransactionIdentity struct { | ||
ID string | ||
Session string | ||
} | ||
|
||
func (t TransactionIdentity) ToProto() *Ydb_Topic.TransactionIdentity { | ||
if t.ID == "" && t.Session == "" { | ||
return nil | ||
} | ||
|
||
return &Ydb_Topic.TransactionIdentity{ | ||
Id: t.ID, | ||
Session: t.Session, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.