Skip to content

Commit 5e7f8b4

Browse files
authored
Merge pull request #246 from ydb-platform/keep-in-cache
Enabled by default keep-in-cache
2 parents cd48f7f + 0c47915 commit 5e7f8b4

File tree

6 files changed

+71
-30
lines changed

6 files changed

+71
-30
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Marked as deprecated `session.Prepare` callback
2+
* Marked as deprecated `options.WithQueryCachePolicyKeepInCache` and `options.WithQueryCachePolicy` options
3+
* Added `options.WithKeepInCache` option
4+
* Enabled by default keep-in-cache policy for data queries
15
* Fixed bug with convertation `time.Duration` from/to YDB value
26
* Removed from `ydb.Connection` embedding of `grpc.ClientConnInterface`
37
* Fixed stopping of repeater

internal/table/keep_in_cache_test.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/stretchr/testify/require"
78
"google.golang.org/protobuf/proto"
89

910
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
@@ -15,18 +16,28 @@ import (
1516

1617
func TestQueryCachePolicyKeepInCache(t *testing.T) {
1718
for _, test := range [...]struct {
18-
name string
19-
queryCachePolicyOption []options.QueryCachePolicyOption
19+
name string
20+
executeDataQueryOptions []options.ExecuteDataQueryOption
21+
keepInCache bool
2022
}{
23+
{
24+
name: "no options",
25+
executeDataQueryOptions: nil,
26+
keepInCache: true,
27+
},
2128
{
2229
name: "with server cache",
23-
queryCachePolicyOption: []options.QueryCachePolicyOption{
24-
options.WithQueryCachePolicyKeepInCache(),
30+
executeDataQueryOptions: []options.ExecuteDataQueryOption{
31+
options.WithKeepInCache(true),
2532
},
33+
keepInCache: true,
2634
},
2735
{
28-
name: "no server cache",
29-
queryCachePolicyOption: []options.QueryCachePolicyOption{},
36+
name: "no server cache",
37+
executeDataQueryOptions: []options.ExecuteDataQueryOption{
38+
options.WithKeepInCache(false),
39+
},
40+
keepInCache: false,
3041
},
3142
} {
3243
t.Run(test.name, func(t *testing.T) {
@@ -41,15 +52,7 @@ func TestQueryCachePolicyKeepInCache(t *testing.T) {
4152
if !ok {
4253
t.Fatalf("cannot cast request '%T' to *Ydb_Table.ExecuteDataQueryRequest", request)
4354
}
44-
if len(test.queryCachePolicyOption) > 0 {
45-
if !r.QueryCachePolicy.GetKeepInCache() {
46-
t.Fatalf("keep-in-cache policy must be true, got: %v", r.QueryCachePolicy.GetKeepInCache())
47-
}
48-
} else {
49-
if r.QueryCachePolicy.GetKeepInCache() {
50-
t.Fatalf("keep-in-cache policy must be false, got: %v", r.QueryCachePolicy.GetKeepInCache())
51-
}
52-
}
55+
require.Equal(t, test.keepInCache, r.QueryCachePolicy.GetKeepInCache())
5356
return &Ydb_Table.ExecuteQueryResult{
5457
TxMeta: &Ydb_Table.TransactionMeta{
5558
Id: "",
@@ -79,7 +82,7 @@ func TestQueryCachePolicyKeepInCache(t *testing.T) {
7982
),
8083
"SELECT 1",
8184
table.NewQueryParameters(),
82-
options.WithQueryCachePolicy(test.queryCachePolicyOption...),
85+
test.executeDataQueryOptions...,
8386
)
8487
if err != nil {
8588
t.Fatal(err)

internal/table/session.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ func (s *session) executeDataQuery(
685685
TxControl: tx.Desc(),
686686
Parameters: params.Params(),
687687
Query: &query.query,
688+
QueryCachePolicy: &Ydb_Table.QueryCachePolicy{
689+
KeepInCache: true,
690+
},
688691
OperationParams: operation.Params(
689692
ctx,
690693
s.config.OperationTimeout(),

table/options/options.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,16 +511,42 @@ type (
511511
QueryCachePolicyOption func(*queryCachePolicy)
512512
)
513513

514+
// WithKeepInCache manages keep-in-cache flag in query cache policy
515+
//
516+
// By default all data queries executes with keep-in-cache policy
517+
func WithKeepInCache(keepInCache bool) ExecuteDataQueryOption {
518+
return withQueryCachePolicy(
519+
withQueryCachePolicyKeepInCache(keepInCache),
520+
)
521+
}
522+
523+
// WithQueryCachePolicyKeepInCache manages keep-in-cache policy
524+
//
525+
// Deprecated: data queries always executes with enabled keep-in-cache policy.
526+
// Use WithKeepInCache for disabling keep-in-cache policy
514527
func WithQueryCachePolicyKeepInCache() QueryCachePolicyOption {
528+
return withQueryCachePolicyKeepInCache(true)
529+
}
530+
531+
func withQueryCachePolicyKeepInCache(keepInCache bool) QueryCachePolicyOption {
515532
return func(p *queryCachePolicy) {
516-
p.KeepInCache = true
533+
p.KeepInCache = keepInCache
517534
}
518535
}
519536

537+
// WithQueryCachePolicy manages query cache policy
538+
//
539+
// Deprecated: use WithKeepInCache for disabling keep-in-cache policy
520540
func WithQueryCachePolicy(opts ...QueryCachePolicyOption) ExecuteDataQueryOption {
541+
return withQueryCachePolicy(opts...)
542+
}
543+
544+
func withQueryCachePolicy(opts ...QueryCachePolicyOption) ExecuteDataQueryOption {
521545
return func(d *ExecuteDataQueryDesc) {
522546
if d.QueryCachePolicy == nil {
523-
d.QueryCachePolicy = new(Ydb_Table.QueryCachePolicy)
547+
d.QueryCachePolicy = &Ydb_Table.QueryCachePolicy{
548+
KeepInCache: true,
549+
}
524550
}
525551
for _, opt := range opts {
526552
opt((*queryCachePolicy)(d.QueryCachePolicy))

table/table.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,69 +69,86 @@ type Session interface {
6969
path string,
7070
opts ...options.CreateTableOption,
7171
) (err error)
72+
7273
DescribeTable(
7374
ctx context.Context,
7475
path string,
7576
opts ...options.DescribeTableOption,
7677
) (desc options.Description, err error)
78+
7779
DropTable(
7880
ctx context.Context,
7981
path string,
8082
opts ...options.DropTableOption,
8183
) (err error)
84+
8285
AlterTable(
8386
ctx context.Context,
8487
path string,
8588
opts ...options.AlterTableOption,
8689
) (err error)
90+
8791
CopyTable(
8892
ctx context.Context,
8993
dst, src string,
9094
opts ...options.CopyTableOption,
9195
) (err error)
96+
9297
Explain(
9398
ctx context.Context,
9499
query string,
95100
) (exp DataQueryExplanation, err error)
101+
102+
// Prepare prepares query for executing in the future
103+
//
104+
// Deprecated: use Execute with KeepInCache policy option
96105
Prepare(
97106
ctx context.Context,
98107
query string,
99108
) (stmt Statement, err error)
109+
100110
Execute(
101111
ctx context.Context,
102112
tx *TransactionControl,
103113
query string,
104114
params *QueryParameters,
105115
opts ...options.ExecuteDataQueryOption,
106116
) (txr Transaction, r result.Result, err error)
117+
107118
ExecuteSchemeQuery(
108119
ctx context.Context,
109120
query string,
110121
opts ...options.ExecuteSchemeQueryOption,
111122
) (err error)
123+
112124
DescribeTableOptions(
113125
ctx context.Context,
114126
) (desc options.TableOptionsDescription, err error)
127+
115128
StreamReadTable(
116129
ctx context.Context,
117130
path string,
118131
opts ...options.ReadTableOption,
119132
) (r result.StreamResult, err error)
133+
120134
StreamExecuteScanQuery(
121135
ctx context.Context,
122136
query string,
123137
params *QueryParameters,
124138
opts ...options.ExecuteScanQueryOption,
125139
) (_ result.StreamResult, err error)
140+
126141
BulkUpsert(
127142
ctx context.Context,
128143
table string,
129144
rows types.Value,
130145
) (err error)
146+
131147
BeginTransaction(
132148
ctx context.Context,
133149
tx *TransactionSettings,
134150
) (x Transaction, err error)
151+
135152
KeepAlive(
136153
ctx context.Context,
137154
) error

table/table_e2e_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,6 @@ func TestTable(t *testing.T) {
380380
table.ValueParam("$seasonID", types.Uint64Value(1)),
381381
table.ValueParam("$episodeID", types.Uint64Value(1)),
382382
),
383-
options.WithQueryCachePolicy(
384-
options.WithQueryCachePolicyKeepInCache(),
385-
),
386383
)
387384
if err != nil {
388385
return err
@@ -419,9 +416,6 @@ func TestTable(t *testing.T) {
419416
table.ValueParam("$episodeID", types.Uint64Value(1)),
420417
table.ValueParam("$views", types.Uint64Value(views+1)), // increment views
421418
),
422-
options.WithQueryCachePolicy(
423-
options.WithQueryCachePolicyKeepInCache(),
424-
),
425419
)
426420
if err != nil {
427421
return err
@@ -467,9 +461,6 @@ func TestTable(t *testing.T) {
467461
table.ValueParam("$seasonID", types.Uint64Value(1)),
468462
table.ValueParam("$episodeID", types.Uint64Value(1)),
469463
),
470-
options.WithQueryCachePolicy(
471-
options.WithQueryCachePolicyKeepInCache(),
472-
),
473464
)
474465
if err != nil {
475466
return err
@@ -777,9 +768,6 @@ func executeDataQuery(ctx context.Context, t *testing.T, c table.Client, folderA
777768
table.NewQueryParameters(
778769
table.ValueParam("$seriesID", types.Uint64Value(1)),
779770
),
780-
options.WithQueryCachePolicy(
781-
options.WithQueryCachePolicyKeepInCache(),
782-
),
783771
options.WithCollectStatsModeBasic(),
784772
)
785773
if err != nil {

0 commit comments

Comments
 (0)