-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
223 lines (209 loc) · 5.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"context"
"fmt"
"github.com/ydb-platform/ydb-go-sdk/v3/config"
"google.golang.org/grpc"
"math/rand"
_ "net/http/pprof"
"os"
"path"
"sync"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
ydbZap "github.com/ydb-platform/ydb-go-sdk-zap"
)
var (
log *zap.Logger
)
func init() {
var err error
log, err = zap.NewDevelopment(
zap.IncreaseLevel(
func() (l zapcore.Level) {
l = zapcore.DebugLevel
_ = l.UnmarshalText([]byte(os.Getenv("LOG_LEVEL")))
return l
}(),
),
zap.AddStacktrace(zapcore.InvalidLevel),
)
if err != nil {
panic(err)
}
}
func main() {
ctx := context.Background()
var creds ydb.Option
if token, has := os.LookupEnv("YDB_ACCESS_TOKEN_CREDENTIALS"); has {
creds = ydb.WithAccessTokenCredentials(token)
}
if v, has := os.LookupEnv("YDB_ANONYMOUS_CREDENTIALS"); has && v == "1" {
creds = ydb.WithAnonymousCredentials()
}
db, err := ydb.Open(
ctx,
os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithDialTimeout(5*time.Second),
creds,
ydb.WithSessionPoolSizeLimit(300),
ydb.WithSessionPoolIdleThreshold(time.Second*5),
ydb.With(config.WithGrpcOptions(grpc.WithBlock())),
ydbZap.WithTraces(log, trace.DiscoveryEvents|trace.DriverRepeaterEvents|trace.TableEvents|trace.DriverRepeaterEvents),
)
if err != nil {
panic(err)
}
defer func() {
_ = db.Close(ctx)
}()
wg := &sync.WaitGroup{}
//if concurrency, err := strconv.Atoi(os.Getenv("YDB_PREPARE_BENCH_DATA")); err == nil && concurrency > 0 {
_ = upsertData(ctx, db.Table(), db.Name(), "series", 10)
//}
//concurrency := func() int {
// if concurrency, err := strconv.Atoi(os.Getenv("CONCURRENCY")); err != nil {
// return concurrency
// }
// return 300
//}()
//
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
for {
time.Sleep(time.Duration(rand.Int63n(int64(time.Second))))
start := time.Now()
count, err := scanSelect(
ctx,
db.Table(),
db.Name(),
rand.Int63n(25000),
)
log.Debug("scan select",
zap.Duration("latency", time.Since(start)),
zap.Uint64("count", count),
zap.Error(err),
)
}
}()
}
wg.Wait()
}
func upsertData(ctx context.Context, c table.Client, prefix, tableName string, concurrency int) (err error) {
err = c.Do(ctx,
func(ctx context.Context, s table.Session) (err error) {
return s.DropTable(ctx, path.Join(prefix, tableName))
},
table.WithIdempotent(),
)
if err != nil {
log.Warn("drop table", zap.Error(err))
}
err = c.Do(ctx,
func(ctx context.Context, s table.Session) (err error) {
return s.CreateTable(ctx, path.Join(prefix, tableName),
options.WithColumn("series_id", types.Optional(types.TypeUint64)),
options.WithColumn("title", types.Optional(types.TypeUTF8)),
options.WithColumn("series_info", types.Optional(types.TypeUTF8)),
options.WithColumn("release_date", types.Optional(types.TypeUint64)),
options.WithColumn("comment", types.Optional(types.TypeUTF8)),
options.WithPrimaryKeyColumn("series_id"),
)
},
table.WithIdempotent(),
)
if err != nil {
log.Error("create table", zap.Error(err))
}
rowsLen := 25000000
batchSize := 1000
wg := sync.WaitGroup{}
sema := make(chan struct{}, concurrency)
for shift := 0; shift < rowsLen; shift += batchSize {
wg.Add(1)
sema <- struct{}{}
go func(prefix, tableName string, shift int) {
defer func() {
<-sema
wg.Done()
}()
rows := make([]types.Value, 0, batchSize)
for i := 0; i < batchSize; i++ {
rows = append(rows, types.StructValue(
types.StructFieldValue("series_id", types.Uint64Value(uint64(i+shift+3))),
types.StructFieldValue("title", types.UTF8Value(fmt.Sprintf("series No. %d title", i+shift+3))),
types.StructFieldValue("series_info", types.UTF8Value(fmt.Sprintf("series No. %d info", i+shift+3))),
types.StructFieldValue("release_date", types.Uint64Value(uint64(time.Since(time.Unix(0, 0))/time.Hour/24))),
types.StructFieldValue("comment", types.UTF8Value(fmt.Sprintf("series No. %d comment", i+shift+3))),
))
}
err = c.Do(ctx,
func(ctx context.Context, session table.Session) (err error) {
return session.BulkUpsert(
ctx,
path.Join(prefix, tableName),
types.ListValue(rows...),
)
},
table.WithIdempotent(),
)
if err == nil {
log.Debug("bulk upserted", zap.Int("from", shift), zap.Int("to", shift+batchSize))
} else {
log.Error("bulk upsert failed", zap.Int("from", shift), zap.Int("to", shift+batchSize), zap.Error(err))
}
}(prefix, tableName, shift)
}
wg.Wait()
return nil
}
func scanSelect(ctx context.Context, c table.Client, prefix string, limit int64) (count uint64, err error) {
query := fmt.Sprintf(`
PRAGMA TablePathPrefix("%s");
$format = DateTime::Format("%%Y-%%m-%%d");
SELECT
series_id,
title,
$format(DateTime::FromSeconds(CAST(DateTime::ToSeconds(DateTime::IntervalFromDays(CAST(release_date AS Int16))) AS Uint32))) AS release_date
FROM series LIMIT %d;`,
prefix,
limit,
)
err = c.Do(ctx,
func(ctx context.Context, s table.Session) error {
res, err := s.StreamExecuteScanQuery(
ctx,
query,
table.NewQueryParameters(),
)
if err != nil {
return err
}
var (
id *uint64
title *string
date *[]byte
)
for res.NextResultSet(ctx, "series_id", "title", "release_date") {
for res.NextRow() {
count++
err = res.Scan(&id, &title, &date)
if err != nil {
return err
}
}
}
return res.Err()
},
table.WithIdempotent(),
)
return count, err
}