Skip to content

Commit

Permalink
Merge pull request #416 from illia-li/il/fix-stmtlogger-add-nop-filel…
Browse files Browse the repository at this point in the history
…ogger

fix(stmtlogger): lets gemini work with empty 'test-statement-log-file' option
  • Loading branch information
dkropachev authored Dec 27, 2023
2 parents ac15fbb + db2aa96 commit 916bff7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
30 changes: 22 additions & 8 deletions pkg/stmtlogger/filelogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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() {
Expand Down Expand Up @@ -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),
Expand All @@ -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) {}
3 changes: 2 additions & 1 deletion pkg/store/cqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 916bff7

Please sign in to comment.