Skip to content

Commit 61dc11d

Browse files
authored
feat: modify tsbs to support using in ceresdb integration test (#6)
* support creating partitioned table, you can make by setting `--parition-keys xxx`. * support setting access mode of ceresdb client(--access-mode direct/proxy). * support output query responses to defined file. * remove useless prints, and refactor create sql's building. * fix naming style. * fix naming style again... * Extract client's build to a function for eliminating duplicated codes. * make queries result printing more pretty. * Support setting update mode. * support setting partition num.
1 parent 4d5fcf4 commit 61dc11d

File tree

6 files changed

+116
-21
lines changed

6 files changed

+116
-21
lines changed

cmd/tsbs_load_ceresdb/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ func initProgramOptions() (*ceresdb.SpecificConfig, load.BenchmarkRunner, *load.
3333
storageFormat := viper.GetString("storage-format")
3434
primaryKeys := viper.GetString("primary-keys")
3535
rowGroupSize := viper.GetInt64("row-group-size")
36+
partitionKeys := viper.GetString("partition-keys")
37+
partitionNum := viper.GetUint32("partition-num")
38+
accessMode := viper.GetString("access-mode")
39+
updateMode := viper.GetString("update-mode")
3640
loader := load.GetBenchmarkRunner(loaderConf)
3741
return &ceresdb.SpecificConfig{
3842
CeresdbAddr: ceresdbAddr,
3943
StorageFormat: storageFormat,
4044
RowGroupSize: rowGroupSize,
4145
PrimaryKeys: primaryKeys,
46+
PartitionKeys: partitionKeys,
47+
PartitionNum: partitionNum,
48+
AccessMode: accessMode,
49+
UpdateMode: updateMode,
4250
}, loader, &loaderConf
4351
}
4452

cmd/tsbs_run_queries_ceresdb/main.go

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@ package main
77
import (
88
"context"
99
"fmt"
10+
"os"
11+
"strings"
1012
"time"
1113

12-
"github.com/CeresDB/ceresdb-client-go/ceresdb"
14+
ceresdbSdk "github.com/CeresDB/ceresdb-client-go/ceresdb"
1315
"github.com/blagojts/viper"
1416
"github.com/spf13/pflag"
1517
"github.com/timescale/tsbs/internal/utils"
1618
"github.com/timescale/tsbs/pkg/query"
19+
"github.com/timescale/tsbs/pkg/targets/ceresdb"
1720
)
1821

1922
// Program option vars:
2023
var (
2124
ceresdbAddr string
2225

2326
showExplain bool
27+
28+
accessMode string
29+
30+
responsesFile string
2431
)
2532

2633
// Global vars:
@@ -39,6 +46,8 @@ func init() {
3946
"ceresdb gRPC endpoint",
4047
)
4148
pflag.Bool("show-explain", false, "Print out the EXPLAIN output for sample query")
49+
pflag.String("access-mode", "direct", "Access mode of ceresdb client")
50+
pflag.String("responses-file", "", "Write responses to this file if enable responses printing")
4251
pflag.Parse()
4352

4453
err := utils.SetupConfigFile()
@@ -53,7 +62,8 @@ func init() {
5362

5463
ceresdbAddr = viper.GetString("ceresdb-addr")
5564
showExplain = viper.GetBool("show-explain")
56-
65+
accessMode = viper.GetString("access-mode")
66+
responsesFile = viper.GetString("responses-file")
5767
runner = query.NewBenchmarkRunner(config)
5868
}
5969

@@ -69,8 +79,9 @@ type queryExecutorOptions struct {
6979

7080
// query.Processor interface implementation
7181
type processor struct {
72-
db ceresdb.Client
73-
opts *queryExecutorOptions
82+
db ceresdbSdk.Client
83+
opts *queryExecutorOptions
84+
queryResultsFile *os.File
7485
}
7586

7687
// query.Processor interface implementation
@@ -80,11 +91,20 @@ func newProcessor() query.Processor {
8091

8192
// query.Processor interface implementation
8293
func (p *processor) Init(workerNumber int) {
83-
client, err := ceresdb.NewClient(ceresdbAddr, ceresdb.Direct, ceresdb.WithDefaultDatabase("public"))
94+
client, err := ceresdb.NewClient(ceresdbAddr, accessMode, ceresdbSdk.WithDefaultDatabase("public"))
8495
if err != nil {
8596
panic(err)
8697
}
8798
p.db = client
99+
100+
if responsesFile != "" {
101+
queryResultsFile, err := os.Create(responsesFile)
102+
if err != nil {
103+
panic(err)
104+
}
105+
p.queryResultsFile = queryResultsFile
106+
}
107+
88108
p.opts = &queryExecutorOptions{
89109
showExplain: false,
90110
debug: runner.DebugLevel() > 0,
@@ -110,7 +130,7 @@ func (p *processor) ProcessQuery(q query.Query, isWarm bool) ([]*query.Stat, err
110130
}
111131

112132
// Main action - run the query
113-
rows, err := p.db.SQLQuery(context.TODO(), ceresdb.SQLQueryRequest{
133+
rows, err := p.db.SQLQuery(context.TODO(), ceresdbSdk.SQLQueryRequest{
114134
Tables: []string{string(ceresdbQuery.Table)},
115135
SQL: sql,
116136
})
@@ -123,7 +143,13 @@ func (p *processor) ProcessQuery(q query.Query, isWarm bool) ([]*query.Stat, err
123143
fmt.Println(sql)
124144
}
125145
if p.opts.printResponse {
126-
fmt.Printf("request = %v\n", rows)
146+
rowsStr := RowsToStr(rows.Rows)
147+
query_res := fmt.Sprintf("###query\n sql: %v\naffected: %v\nrows: \n%s\n\n", rows.SQL, rows.AffectedRows, rowsStr)
148+
if p.queryResultsFile != nil {
149+
p.queryResultsFile.WriteString(query_res)
150+
} else {
151+
fmt.Print(query_res)
152+
}
127153
}
128154

129155
took := float64(time.Since(start).Nanoseconds()) / 1e6
@@ -133,3 +159,17 @@ func (p *processor) ProcessQuery(q query.Query, isWarm bool) ([]*query.Stat, err
133159

134160
return []*query.Stat{stat}, err
135161
}
162+
163+
func RowsToStr(rows []ceresdbSdk.Row) string {
164+
rowLen := len(rows)
165+
if rowLen == 0 {
166+
return ""
167+
}
168+
169+
rowStrs := make([]string, 0, len(rows))
170+
for _, row := range rows {
171+
rowStrs = append(rowStrs, fmt.Sprintf("%v", row))
172+
}
173+
174+
return strings.Join(rowStrs, "\n")
175+
}

pkg/targets/ceresdb/benchmark.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ type SpecificConfig struct {
1818
StorageFormat string `yaml:"storageFormat" mapstructure:"storageFormat"`
1919
RowGroupSize int64 `yaml:"rowGroupSize" mapstructure:"rowGroupSize"`
2020
PrimaryKeys string `yaml:"primaryKeys" mapstructure:"primaryKeys"`
21+
PartitionKeys string `yaml:"partitionKeys" mapstructure:"partitionKeys"`
22+
PartitionNum uint32 `yaml:"partitionKeys" mapstructure:"partitionNum"`
23+
AccessMode string `yaml:"accessMode" mapstructure:"accessMode"`
24+
UpdateMode string `yaml:"updateMode" mapstructure:"updateMode"`
2125
}
2226

2327
func parseSpecificConfig(v *viper.Viper) (*SpecificConfig, error) {
@@ -44,7 +48,8 @@ func NewBenchmark(config *SpecificConfig, dataSourceConfig *source.DataSourceCon
4448
dataSource := &fileDataSource{
4549
scanner: bufio.NewScanner(br),
4650
}
47-
client, err := ceresdb.NewClient(config.CeresdbAddr, ceresdb.Direct, ceresdb.WithDefaultDatabase("public"))
51+
52+
client, err := NewClient(config.CeresdbAddr, config.AccessMode, ceresdb.WithDefaultDatabase("public"))
4853
if err != nil {
4954
panic(err)
5055
}

pkg/targets/ceresdb/creator.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (d *dbCreator) DBExists(dbName string) bool { return true }
4949

5050
// loader.DBCreator interface implementation
5151
func (d *dbCreator) CreateDB(dbName string) error {
52-
client, err := ceresdb.NewClient(d.config.CeresdbAddr, ceresdb.Direct, ceresdb.WithDefaultDatabase("public"))
52+
client, err := NewClient(d.config.CeresdbAddr, d.config.AccessMode, ceresdb.WithDefaultDatabase("public"))
5353
if err != nil {
5454
return err
5555
}
@@ -78,19 +78,30 @@ func (d *dbCreator) createTable(client ceresdb.Client, tableName string,
7878
columnDefs = append(columnDefs, fmt.Sprintf("`%s` double", field))
7979
}
8080

81-
tmpl := `
82-
create table if not exists %s (
83-
%s,
84-
primary key(%s)
85-
) with (
86-
enable_ttl = 'false',
87-
num_rows_per_row_group='%d',
88-
storage_format = '%s'
89-
);
81+
// The sql can be divided into three parts:
82+
// + Create part
83+
// + Partition part
84+
// + With part
85+
crTmpl := `create table if not exists %s (
86+
%s,
87+
primary key(%s)
88+
)`
89+
partTmpl := `partition by key (%s) partitions %v`
90+
withTmpl := `with (
91+
enable_ttl = 'false',
92+
num_rows_per_row_group='%d',
93+
storage_format = '%s',
94+
update_mode='%s'
95+
);`
9096

91-
`
92-
sql := fmt.Sprintf(tmpl, tableName, strings.Join(columnDefs, ","), d.config.PrimaryKeys, d.config.RowGroupSize, d.config.StorageFormat)
93-
// fmt.Printf("sql = %s\n", sql)
97+
// Make sql
98+
sql := fmt.Sprintf(crTmpl, tableName, strings.Join(columnDefs, ","), d.config.PrimaryKeys) + "\n"
99+
if d.config.PartitionKeys != "" {
100+
sql = sql + fmt.Sprintf(partTmpl, d.config.PartitionKeys, d.config.PartitionNum) + "\n"
101+
}
102+
sql = sql + fmt.Sprintf(withTmpl, d.config.RowGroupSize, d.config.StorageFormat, d.config.UpdateMode)
103+
104+
// Execute
94105
_, err := client.SQLQuery(context.TODO(), ceresdb.SQLQueryRequest{
95106
Tables: []string{tableName},
96107
SQL: sql,

pkg/targets/ceresdb/implemented_target.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ func (vm vmTarget) TargetSpecificFlags(flagPrefix string, flagSet *pflag.FlagSet
4747
"tsid,timestamp",
4848
"Primary keys used when create table",
4949
)
50+
flagSet.String(
51+
flagPrefix+"partition-keys",
52+
"",
53+
"Partition keys used when create partitioned table",
54+
)
55+
flagSet.Uint32(
56+
flagPrefix+"partition-num",
57+
4,
58+
"Partition keys used when create partitioned table",
59+
)
60+
flagSet.String(
61+
flagPrefix+"access-mode",
62+
"direct",
63+
"Access mode of ceresdb client",
64+
)
65+
flagSet.String(
66+
flagPrefix+"update-mode",
67+
"OVERWRITE",
68+
"Update mode when insert",
69+
)
5070
}
5171

5272
func (vm vmTarget) TargetName() string {

pkg/targets/ceresdb/util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ceresdb
2+
3+
import "github.com/CeresDB/ceresdb-client-go/ceresdb"
4+
5+
func NewClient(endpoint string, accessMode string, opts ...ceresdb.Option) (ceresdb.Client, error) {
6+
mode := ceresdb.Direct
7+
if accessMode == "proxy" {
8+
mode = ceresdb.Proxy
9+
}
10+
return ceresdb.NewClient(endpoint, mode, opts...)
11+
}

0 commit comments

Comments
 (0)