Skip to content

Commit

Permalink
Merge pull request #1603 from ydb-platform/fix-conns-counter
Browse files Browse the repository at this point in the history
fix broken metric `ydb_go_sdk_ydb_database_sql_conns`
  • Loading branch information
asmyasnikov authored Jan 15, 2025
2 parents d157002 + 5047bea commit 6b7be71
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 153 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Fixed broken metric `ydb_go_sdk_ydb_database_sql_conns`

## v3.96.1
* Fixed drop session from pool unnecessary in query service

Expand Down
18 changes: 18 additions & 0 deletions internal/xslices/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package xslices

import (
"cmp"
"slices"
)

func Keys[Key cmp.Ordered, T any](m map[Key]T) []Key {
keys := make([]Key, 0, len(m))

for key := range m {
keys = append(keys, key)
}

slices.Sort(keys)

return keys
}
18 changes: 18 additions & 0 deletions internal/xslices/keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package xslices

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestKeys(t *testing.T) {
require.Equal(t, []string{"1", "2"}, Keys(map[string]any{
"1": nil,
"2": nil,
}))
require.Equal(t, []int{1, 2}, Keys(map[int]any{
1: nil,
2: nil,
}))
}
27 changes: 19 additions & 8 deletions internal/xsql/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/legacy"
Expand Down Expand Up @@ -67,8 +68,8 @@ func (e Engine) String() string {
switch e {
case LEGACY:
return "LEGACY"
case QUERY_SERVICE:
return "QUERY_SERVICE"
case PROPOSE:
return "PROPOSE"
default:
return "UNKNOWN"
}
Expand Down Expand Up @@ -115,26 +116,33 @@ func (c *Connector) Scheme() scheme.Client {
}

const (
QUERY_SERVICE = iota + 1 //nolint:revive,stylecheck
LEGACY //nolint:revive,stylecheck
PROPOSE = iota + 1
LEGACY
)

func (c *Connector) Open(name string) (driver.Conn, error) {
return nil, xerrors.WithStackTrace(driver.ErrSkip)
}

func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, finalErr error) { //nolint:funlen
onDone := trace.DatabaseSQLOnConnectorConnect(c.Trace(), &ctx,
stack.FunctionID("", stack.Package("database/sql")),
)

switch c.processor {
case QUERY_SERVICE:
case PROPOSE:
s, err := query.CreateSession(ctx, c.Query())
defer func() {
onDone(s, finalErr)
}()
if err != nil {
return nil, xerrors.WithStackTrace(err)
}

id := uuid.New()

conn := &Conn{
processor: QUERY_SERVICE,
processor: PROPOSE,
cc: propose.New(ctx, c, s, append(
c.Options,
propose.WithOnClose(func() {
Expand All @@ -152,6 +160,9 @@ func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {

case LEGACY:
s, err := c.Table().CreateSession(ctx) //nolint:staticcheck
defer func() {
onDone(s, finalErr)
}()
if err != nil {
return nil, xerrors.WithStackTrace(err)
}
Expand Down Expand Up @@ -207,7 +218,7 @@ func Open(parent ydbDriver, balancer grpc.ClientConnInterface, opts ...Option) (
balancer: balancer,
processor: func() Engine {
if overQueryService, _ := strconv.ParseBool(os.Getenv("YDB_DATABASE_SQL_OVER_QUERY_SERVICE")); overQueryService {
return QUERY_SERVICE
return PROPOSE
}

return LEGACY
Expand Down
2 changes: 1 addition & 1 deletion internal/xsql/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func WithQueryOptions(opts ...propose.Option) Option {

func WithQueryService(b bool) Option {
if b {
return queryProcessorOption(QUERY_SERVICE)
return queryProcessorOption(PROPOSE)
}

return queryProcessorOption(LEGACY)
Expand Down
Loading

0 comments on commit 6b7be71

Please sign in to comment.