Skip to content

Commit

Permalink
Merge pull request #383 from scylladb/dk/log-warnings-if-present
Browse files Browse the repository at this point in the history
Make driver print warnings returned by server
  • Loading branch information
dkropachev authored Jan 16, 2025
2 parents 5712671 + f496ba2 commit fe68ec3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ type ClusterConfig struct {
// Default: nil
Authenticator Authenticator

WarningsHandlerBuilder WarningHandlerBuilder

// An Authenticator factory. Can be used to create alternative authenticators.
// Default: nil
AuthProvider func(h *HostInfo) (Authenticator, error)
Expand Down Expand Up @@ -322,6 +324,7 @@ func NewCluster(hosts ...string) *ClusterConfig {
WriteCoalesceWaitTime: 200 * time.Microsecond,
MetadataSchemaRequestTimeout: 60 * time.Second,
DisableSkipMetadata: true,
WarningsHandlerBuilder: DefaultWarningHandlerBuilder,
}

return cfg
Expand Down
29 changes: 27 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ type Authenticator interface {
Success(data []byte) error
}

type WarningHandlerBuilder func(session *Session) WarningHandler

type WarningHandler interface {
HandleWarnings(qry ExecutableQuery, host *HostInfo, warnings []string)
}

type PasswordAuthenticator struct {
Username string
Password string
Expand Down Expand Up @@ -1404,7 +1410,16 @@ func marshalQueryValue(typ TypeInfo, value interface{}, dst *queryValues) error
return nil
}

func (c *Conn) executeQuery(ctx context.Context, qry *Query) *Iter {
func (c *Conn) executeQuery(ctx context.Context, qry *Query) (iter *Iter) {
defer func() {
if iter == nil || c.session == nil {
return
}
warnings := iter.Warnings()
if len(warnings) > 0 && c.session.warningHandler != nil {
c.session.warningHandler.HandleWarnings(qry, iter.host, warnings)
}
}()
params := queryParams{
consistency: qry.cons,
}
Expand Down Expand Up @@ -1670,7 +1685,17 @@ func (c *Conn) UseKeyspace(keyspace string) error {
return nil
}

func (c *Conn) executeBatch(ctx context.Context, batch *Batch) *Iter {
func (c *Conn) executeBatch(ctx context.Context, batch *Batch) (iter *Iter) {
defer func() {
if iter == nil || c.session == nil {
return
}
warnings := iter.Warnings()
if len(warnings) > 0 && c.session.warningHandler != nil {
c.session.warningHandler.HandleWarnings(batch, iter.host, warnings)
}
}()

if c.version == protoVersion1 {
return &Iter{err: ErrUnsupported}
}
Expand Down
5 changes: 4 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Session struct {
tabletsRoutingV1 bool

usingTimeoutClause string
warningHandler WarningHandler
}

var queryPool = &sync.Pool{
Expand Down Expand Up @@ -182,7 +183,9 @@ func newSessionCommon(cfg ClusterConfig) (*Session, error) {
return nil, fmt.Errorf("gocql: unable to create session: %v", err)
}
s.connCfg = connCfg

if cfg.WarningsHandlerBuilder != nil {
s.warningHandler = cfg.WarningsHandlerBuilder(s)
}
return s, nil
}

Expand Down
28 changes: 28 additions & 0 deletions warning_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gocql

type DefaultWarningHandler struct {
logger StdLogger
}

func DefaultWarningHandlerBuilder(session *Session) WarningHandler {
return DefaultWarningHandler{
logger: session.logger,
}
}

func (d DefaultWarningHandler) HandleWarnings(qry ExecutableQuery, host *HostInfo, warnings []string) {
if d.logger == nil {
return
}
if host != nil && len(host.hostId) > 0 {
d.logger.Printf("[%s] warnings: %v", host.hostId, warnings)
} else {
d.logger.Printf("Cluster warnings: %v", warnings)
}
}

var _ WarningHandler = DefaultWarningHandler{}

func NoopWarningHandlerBuilder(session *Session) WarningHandler {
return nil
}

0 comments on commit fe68ec3

Please sign in to comment.