From db2aa960586d4b2e23e00a1c86ce7186b59acc20 Mon Sep 17 00:00:00 2001 From: illia-li Date: Wed, 27 Dec 2023 13:34:50 -0400 Subject: [PATCH] fix(stmtlogger): lets gemini work without 'test-statement-log-file' option --- pkg/stmtlogger/filelogger.go | 30 ++++++++++++++++++++++-------- pkg/store/cqlstore.go | 3 ++- pkg/store/store.go | 8 +------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/pkg/stmtlogger/filelogger.go b/pkg/stmtlogger/filelogger.go index 5087556..b6bc152 100644 --- a/pkg/stmtlogger/filelogger.go +++ b/pkg/stmtlogger/filelogger.go @@ -31,7 +31,13 @@ const ( errorsOnFileLimit = 5 ) -type FileLogger struct { +type StmtToFile interface { + LogStmt(*typedef.Stmt) + LogStmtWithTimeStamp(stmt *typedef.Stmt, ts time.Time) + Close() error +} + +type fileLogger struct { fd *os.File activeChannel atomic.Pointer[loggerChan] channel loggerChan @@ -46,7 +52,7 @@ type logRec struct { ts time.Time } -func (fl *FileLogger) LogStmt(stmt *typedef.Stmt) { +func (fl *fileLogger) LogStmt(stmt *typedef.Stmt) { ch := fl.activeChannel.Load() if ch != nil { *ch <- logRec{ @@ -55,7 +61,7 @@ func (fl *FileLogger) LogStmt(stmt *typedef.Stmt) { } } -func (fl *FileLogger) LogStmtWithTimeStamp(stmt *typedef.Stmt, ts time.Time) { +func (fl *fileLogger) LogStmtWithTimeStamp(stmt *typedef.Stmt, ts time.Time) { ch := fl.activeChannel.Load() if ch != nil { *ch <- logRec{ @@ -65,11 +71,11 @@ func (fl *FileLogger) LogStmtWithTimeStamp(stmt *typedef.Stmt, ts time.Time) { } } -func (fl *FileLogger) Close() error { +func (fl *fileLogger) Close() error { return fl.fd.Close() } -func (fl *FileLogger) committer() { +func (fl *fileLogger) committer() { var err2 error defer func() { @@ -114,16 +120,16 @@ func (fl *FileLogger) committer() { } } -func NewFileLogger(filename string) (*FileLogger, error) { +func NewFileLogger(filename string) (StmtToFile, error) { if filename == "" { - return nil, nil + return &nopFileLogger{}, nil } fd, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) if err != nil { return nil, err } - out := &FileLogger{ + out := &fileLogger{ filename: filename, fd: fd, channel: make(loggerChan, defaultChanSize), @@ -133,3 +139,11 @@ func NewFileLogger(filename string) (*FileLogger, error) { go out.committer() return out, nil } + +type nopFileLogger struct{} + +func (n *nopFileLogger) LogStmtWithTimeStamp(_ *typedef.Stmt, _ time.Time) {} + +func (n *nopFileLogger) Close() error { return nil } + +func (n *nopFileLogger) LogStmt(_ *typedef.Stmt) {} diff --git a/pkg/store/cqlstore.go b/pkg/store/cqlstore.go index c756e07..3370741 100644 --- a/pkg/store/cqlstore.go +++ b/pkg/store/cqlstore.go @@ -27,6 +27,7 @@ import ( "github.com/scylladb/gocqlx/v2/qb" "go.uber.org/zap" + "github.com/scylladb/gemini/pkg/stmtlogger" "github.com/scylladb/gemini/pkg/typedef" ) @@ -39,7 +40,7 @@ type cqlStore struct { //nolint:govet maxRetriesMutate int maxRetriesMutateSleep time.Duration useServerSideTimestamps bool - stmtLogger stmtLogger + stmtLogger stmtlogger.StmtToFile } func (cs *cqlStore) name() string { diff --git a/pkg/store/store.go b/pkg/store/store.go index d9890ad..0d72e2c 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -56,12 +56,6 @@ type storeLoader interface { name() string } -type stmtLogger interface { - LogStmt(*typedef.Stmt) - LogStmtWithTimeStamp(stmt *typedef.Stmt, ts time.Time) - Close() error -} - type Store interface { Create(context.Context, *typedef.Stmt, *typedef.Stmt) error Mutate(context.Context, *typedef.Stmt) error @@ -132,7 +126,7 @@ func (n *noOpStore) close() error { type delegatingStore struct { oracleStore storeLoader testStore storeLoader - statementLogger stmtLogger + statementLogger stmtlogger.StmtToFile logger *zap.Logger validations bool }