-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
187 lines (175 loc) · 4.93 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
package main
import (
"context"
"fmt"
"math/rand"
_ "net/http/pprof"
"os"
"path"
"sync"
"time"
"github.com/rs/zerolog"
"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"
ydbZerolog "github.com/ydb-platform/ydb-go-sdk-zerolog"
)
var (
log = zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Logger()
)
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),
ydbZerolog.WithTraces(&log, trace.DetailsAll),
)
if err != nil {
panic(err)
}
defer func() {
_ = db.Close(ctx)
}()
wg := &sync.WaitGroup{}
_ = upsertData(ctx, db.Table(), db.Name(), "series", 10)
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),
)
if err == nil {
log.Debug().Caller().Timestamp().
Dur("latency", time.Since(start)).
Uint64("count", count).
Msg("scan select done")
} else {
log.Error().Caller().Timestamp().
Dur("latency", time.Since(start)).
Uint64("count", count).
Err(err).
Msg("scan select failed")
}
}
}()
}
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.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"),
)
},
)
if err != nil {
log.Error().Caller().Timestamp().Err(err).Msg("create table")
}
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...),
)
},
)
if err == nil {
log.Debug().Caller().Timestamp().Int("from", shift).Int("to", shift+batchSize).Msg("bulk upserted")
} else {
log.Debug().Caller().Timestamp().Int("from", shift).Int("to", shift+batchSize).Err(err).Msg("bulk upsert failed")
}
}(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()
},
)
return count, err
}