From ec3f6b70cb9d197c37eb3c905e0cbc1d634afc25 Mon Sep 17 00:00:00 2001 From: Vitaly Isaev Date: Mon, 14 Oct 2024 12:42:54 +0300 Subject: [PATCH] Reproducing YDB SDK bug (#203) * Reproducing YDB SDK bug * Reproducing YDB SDK bug - no success * Linter complaining --- tools/{ydbinit => ydb/big_insert}/main.go | 0 .../query_service_negative/docker-compose.yml | 15 +++ .../query_service_negative/init/01_basic.sh | 15 +++ .../ydb/query_service_negative/init/init_ydb | 7 ++ tools/ydb/query_service_negative/main.go | 96 +++++++++++++++++++ 5 files changed, 133 insertions(+) rename tools/{ydbinit => ydb/big_insert}/main.go (100%) create mode 100644 tools/ydb/query_service_negative/docker-compose.yml create mode 100644 tools/ydb/query_service_negative/init/01_basic.sh create mode 100755 tools/ydb/query_service_negative/init/init_ydb create mode 100644 tools/ydb/query_service_negative/main.go diff --git a/tools/ydbinit/main.go b/tools/ydb/big_insert/main.go similarity index 100% rename from tools/ydbinit/main.go rename to tools/ydb/big_insert/main.go diff --git a/tools/ydb/query_service_negative/docker-compose.yml b/tools/ydb/query_service_negative/docker-compose.yml new file mode 100644 index 00000000..60e08fe9 --- /dev/null +++ b/tools/ydb/query_service_negative/docker-compose.yml @@ -0,0 +1,15 @@ +services: + ydb: + image: ghcr.io/ydb-platform/local-ydb:24.2.8 + container_name: ${USER}-fq-connector-go-tests-ydb + hostname: localhost + ports: + - '2136:2136' + - '8765:8765' + environment: + YDB_DEFAULT_LOG_LEVEL: ERROR + POSTGRES_USER: admin + POSTGRES_PASSWORD: password + volumes: + - ./init/init_ydb:/init_ydb + - ./init/01_basic.sh:/01_basic.sh diff --git a/tools/ydb/query_service_negative/init/01_basic.sh b/tools/ydb/query_service_negative/init/01_basic.sh new file mode 100644 index 00000000..28f49a21 --- /dev/null +++ b/tools/ydb/query_service_negative/init/01_basic.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +/ydb -p tests-ydb-client yql -s ' + + CREATE TABLE simple (id Int32 NOT NULL, col1 String NOT NULL, col2 Int32 NOT NULL, PRIMARY KEY (id)); + COMMIT; + INSERT INTO simple (id, col1, col2) VALUES + (1, "ydb_a", 10), + (2, "ydb_b", 20), + (3, "ydb_c", 30), + (4, "ydb_d", 40), + (5, "ydb_e", 50); + COMMIT; + ' + \ No newline at end of file diff --git a/tools/ydb/query_service_negative/init/init_ydb b/tools/ydb/query_service_negative/init/init_ydb new file mode 100755 index 00000000..051aa0ab --- /dev/null +++ b/tools/ydb/query_service_negative/init/init_ydb @@ -0,0 +1,7 @@ +#!/bin/bash + +set -ex + +/ydb config profile create tests-ydb-client --endpoint grpc://localhost:2136 --database /local + +/bin/bash ./01_basic.sh diff --git a/tools/ydb/query_service_negative/main.go b/tools/ydb/query_service_negative/main.go new file mode 100644 index 00000000..86634903 --- /dev/null +++ b/tools/ydb/query_service_negative/main.go @@ -0,0 +1,96 @@ +package main + +import ( + "context" + "fmt" + "log" + "path" + "time" + + "github.com/ydb-platform/ydb-go-sdk/v3" + "github.com/ydb-platform/ydb-go-sdk/v3/balancers" + "github.com/ydb-platform/ydb-go-sdk/v3/config" + "github.com/ydb-platform/ydb-go-sdk/v3/sugar" + "github.com/ydb-platform/ydb-go-sdk/v3/table" + "github.com/ydb-platform/ydb-go-sdk/v3/table/options" + "google.golang.org/grpc" +) + +const ( + dbEndpoint = "localhost:2136" + dbName = "/local" + tableName = "simple" +) + +func main() { + log.Println("Correct credentials") + obtainTableDesciption(dbEndpoint, "admin", "password") + + log.Println("Invalid credentials") + obtainTableDesciption(dbEndpoint, "admin2", "password") +} + +func obtainTableDesciption(endpoint, login, password string) { + ydbDriver, err := makeDriver(endpoint, login, password) + if err != nil { + log.Fatal(err) + } + + defer func() { + if closeErr := ydbDriver.Close(context.Background()); closeErr != nil { + log.Fatal(closeErr) + } + }() + + desc, err := getTableDescription(ydbDriver) + if err != nil { + log.Fatal(err) + } + + log.Printf("Table description: %+v", desc) +} + +func makeDriver(endpoint, login, password string) (*ydb.Driver, error) { + ydbOptions := []ydb.Option{ + ydb.WithStaticCredentials(login, password), + ydb.WithDialTimeout(5 * time.Second), + ydb.WithBalancer(balancers.SingleConn()), // see YQ-3089 + ydb.With(config.WithGrpcOptions(grpc.WithDisableServiceConfig())), + } + + dsn := sugar.DSN(endpoint, dbName, false) + + ydbDriver, err := ydb.Open(context.Background(), dsn, ydbOptions...) + if err != nil { + return nil, fmt.Errorf("open driver error: %w", err) + } + + return ydbDriver, nil +} + +func getTableDescription(ydbDriver *ydb.Driver) (*options.Description, error) { + desc := options.Description{} + filePath := path.Join(dbName, tableName) + + log.Println("Getting table description for table", filePath) + + err := ydbDriver.Table().Do( + context.Background(), + func(ctx context.Context, s table.Session) error { + var errInner error + desc, errInner = s.DescribeTable(ctx, filePath) + if errInner != nil { + return fmt.Errorf("describe table: %w", errInner) + } + + return nil + }, + table.WithIdempotent(), + ) + + if err != nil { + return nil, fmt.Errorf("get table description: %w", err) + } + + return &desc, nil +}