Skip to content

Commit

Permalink
Merge remote-tracking branch 'sashayakovtseva/feature/ydb-insert-from…
Browse files Browse the repository at this point in the history
…-as-table' into feature/build-ydb
  • Loading branch information
sashayakovtseva committed Mar 19, 2024
2 parents 6061ddc + 78ea937 commit 4962f73
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 250 deletions.
4 changes: 3 additions & 1 deletion internal/datastore/benchmark/driver_bench_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build ci && docker
// +build ci,docker

package benchmark

Expand All @@ -18,6 +17,7 @@ import (
"github.com/authzed/spicedb/internal/datastore/mysql"
"github.com/authzed/spicedb/internal/datastore/postgres"
"github.com/authzed/spicedb/internal/datastore/spanner"
"github.com/authzed/spicedb/internal/datastore/ydb"
"github.com/authzed/spicedb/internal/testfixtures"
testdatastore "github.com/authzed/spicedb/internal/testserver/datastore"
"github.com/authzed/spicedb/internal/testserver/datastore/config"
Expand Down Expand Up @@ -47,6 +47,8 @@ var drivers = []struct {
{postgres.Engine, "", nil},
{crdb.Engine, "-overlap-static", []dsconfig.ConfigOption{dsconfig.WithOverlapStrategy("static")}},
{crdb.Engine, "-overlap-insecure", []dsconfig.ConfigOption{dsconfig.WithOverlapStrategy("insecure")}},
{ydb.Engine, "-uniqueness-check", []dsconfig.ConfigOption{dsconfig.WithYDBEnableUniquenessCheck(true)}},
{ydb.Engine, "-no-uniqueness-check", []dsconfig.ConfigOption{dsconfig.WithYDBEnableUniquenessCheck(false)}},
{mysql.Engine, "", nil},
}

Expand Down
23 changes: 16 additions & 7 deletions internal/datastore/ydb/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type ydbConfig struct {

bulkLoadBatchSize int

gcEnabled bool
enableGC bool
enablePrometheusStats bool
enableUniquenessCheck bool
}

var defaultConfig = ydbConfig{
Expand All @@ -34,7 +35,8 @@ var defaultConfig = ydbConfig{
gcInterval: 3 * time.Minute,
gcMaxOperationTime: time.Minute,
bulkLoadBatchSize: 1000,
gcEnabled: true,
enableGC: true,
enableUniquenessCheck: true,
}

// Option provides the facility to configure how clients within the YDB
Expand Down Expand Up @@ -80,11 +82,11 @@ func GCInterval(interval time.Duration) Option {
return func(o *ydbConfig) { o.gcInterval = interval }
}

// GCEnabled indicates whether garbage collection is enabled.
// WithEnableGC indicates whether garbage collection is enabled.
//
// GC is enabled by default.
func GCEnabled(isGCEnabled bool) Option {
return func(o *ydbConfig) { o.gcEnabled = isGCEnabled }
func WithEnableGC(isGCEnabled bool) Option {
return func(o *ydbConfig) { o.enableGC = isGCEnabled }
}

// GCMaxOperationTime is the maximum operation time of a garbage collection pass before it times out.
Expand Down Expand Up @@ -143,6 +145,13 @@ func WatchBufferWriteTimeout(watchBufferWriteTimeout time.Duration) Option {
// clients being used by the datastore are enabled.
//
// Prometheus metrics are disabled by default.
func WithEnablePrometheusStats(enablePrometheusStats bool) Option {
return func(o *ydbConfig) { o.enablePrometheusStats = enablePrometheusStats }
func WithEnablePrometheusStats(v bool) Option {
return func(o *ydbConfig) { o.enablePrometheusStats = v }
}

// WithEnableUniquenessCheck marks whether relation tuples will be checked against
// unique index during CREATE operation. YDB doesn't support unique secondary indexes,
// and since this check is quite expensive one may turn it off.
func WithEnableUniquenessCheck(v bool) Option {
return func(o *ydbConfig) { o.enableUniquenessCheck = v }
}
2 changes: 1 addition & 1 deletion internal/datastore/ydb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func queryTuples(
var structuredCtx map[string]any
if caveatCtx != nil {
if err := json.Unmarshal(*caveatCtx, &structuredCtx); err != nil {
return nil, fmt.Errorf("failed to unmarhsla relation tuple caveat context: %w", err)
return nil, fmt.Errorf("failed to unmarshal relation tuple caveat context: %w", err)
}
}

Expand Down
21 changes: 12 additions & 9 deletions internal/datastore/ydb/readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ var _ datastore.ReadWriteTransaction = (*ydbReadWriter)(nil)

type ydbReadWriter struct {
*ydbReader
bulkLoadBatchSize int
newRevision revisions.TimestampRevision
bulkLoadBatchSize int
newRevision revisions.TimestampRevision
enableUniquenessCheck bool
}

// WriteCaveats stores the provided caveats.
Expand Down Expand Up @@ -91,7 +92,7 @@ func (rw *ydbReadWriter) WriteRelationships(ctx context.Context, mutations []*co
}

// Perform SELECT queries first as a part of uniqueness check.
if len(insertionTuples) > 0 {
if len(insertionTuples) > 0 && rw.enableUniquenessCheck {
dups, err := rw.selectTuples(ctx, insertionTuples)
if err != nil {
return fmt.Errorf("failed to ensure CREATE tuples uniqueness: %w", err)
Expand Down Expand Up @@ -284,12 +285,14 @@ func (rw *ydbReadWriter) BulkLoad(ctx context.Context, iter datastore.BulkWriteR
}

if len(insertionTuples) > 0 {
dups, err := rw.selectTuples(ctx, insertionTuples)
if err != nil {
return 0, fmt.Errorf("failed to ensure CREATE tuples uniqueness: %w", err)
}
if len(dups) > 0 {
return 0, datastoreCommon.NewCreateRelationshipExistsError(dups[0])
if rw.enableUniquenessCheck {
dups, err := rw.selectTuples(ctx, insertionTuples)
if err != nil {
return 0, fmt.Errorf("failed to ensure CREATE tuples uniqueness: %w", err)
}
if len(dups) > 0 {
return 0, datastoreCommon.NewCreateRelationshipExistsError(dups[0])
}
}

if err := executeQuery(ctx, rw.tablePathPrefix, rw.executor, insertBuilder); err != nil {
Expand Down
Loading

0 comments on commit 4962f73

Please sign in to comment.