-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add statement-log-file option
- Loading branch information
Dmitry Kropachev
committed
Dec 18, 2023
1 parent
16a7413
commit 35c1511
Showing
9 changed files
with
236 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// Copyright 2019 ScyllaDB | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package stmtlogger | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/scylladb/gemini/pkg/typedef" | ||
) | ||
|
||
const ( | ||
defaultChanSize = 1000 | ||
errorsOnFileLimit = 5 | ||
) | ||
|
||
type FileLogger struct { | ||
fd *os.File | ||
filename string | ||
isFileNonOperational bool | ||
mt sync.RWMutex | ||
activeChannel atomic.Pointer[chan *typedef.Stmt] | ||
channel chan *typedef.Stmt | ||
} | ||
|
||
func (fl *FileLogger) LogStmt(stmt *typedef.Stmt) { | ||
ch := fl.activeChannel.Load() | ||
if ch != nil { | ||
*ch <- stmt | ||
} | ||
} | ||
|
||
func (fl *FileLogger) Close() error { | ||
return fl.fd.Close() | ||
} | ||
|
||
func (fl *FileLogger) committer() { | ||
defer func() { | ||
fl.activeChannel.Swap(nil) | ||
close(fl.channel) | ||
}() | ||
|
||
errsAtRow := 0 | ||
|
||
for stmt := range fl.channel { | ||
if fl.isFileNonOperational { | ||
continue | ||
} | ||
|
||
stmtStr := stmt.PrettyCQL() | ||
_, err := fl.fd.Write([]byte(fmt.Sprintf("%s: %s;\n", time.Now().Format(time.RFC1123Z), stmtStr))) | ||
if err == nil { | ||
errsAtRow = 0 | ||
continue | ||
} | ||
|
||
if errors.Is(err, os.ErrClosed) { | ||
fl.isFileNonOperational = true | ||
return | ||
} | ||
|
||
errsAtRow++ | ||
if errsAtRow > errorsOnFileLimit { | ||
fl.isFileNonOperational = true | ||
} | ||
log.Printf("failed to write to file %q: %s", fl.filename, err.Error()) | ||
return | ||
} | ||
} | ||
|
||
func NewFileLogger(filename string) (*FileLogger, error) { | ||
fd, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ch := make(chan *typedef.Stmt, defaultChanSize) | ||
|
||
out := &FileLogger{ | ||
filename: filename, | ||
fd: fd, | ||
channel: ch, | ||
} | ||
out.activeChannel.Store(&ch) | ||
|
||
go out.committer() | ||
return out, nil | ||
} | ||
|
||
func drain(ch chan *typedef.Stmt) { | ||
loop: | ||
for { | ||
select { | ||
case <-ch: | ||
default: | ||
break loop | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.