logfs opened files

This commit is contained in:
royalcat 2024-06-19 00:17:10 +03:00
parent bf2dac5cf1
commit 2d72790c1a
4 changed files with 42 additions and 11 deletions

View file

@ -109,7 +109,10 @@ func run(configPath string) error {
vfs.NewCtxBillyFs("/", ctxbilly.WrapFileSystem(sourceFs)), vfs.NewCtxBillyFs("/", ctxbilly.WrapFileSystem(sourceFs)),
tsrv, ytdlpsrv, tsrv, ytdlpsrv,
) )
sfs = vfs.WrapLogFS(sfs) sfs, err = vfs.WrapLogFS(sfs)
if err != nil {
return err
}
if conf.Mounts.Fuse.Enabled { if conf.Mounts.Fuse.Enabled {
mh := fuse.NewHandler(conf.Mounts.Fuse.AllowOther, conf.Mounts.Fuse.Path) mh := fuse.NewHandler(conf.Mounts.Fuse.AllowOther, conf.Mounts.Fuse.Path)

View file

@ -154,8 +154,7 @@ func (c *CachingHandler) InvalidateHandle(ctx context.Context, fs nfs.Filesystem
return c.activeHandles.Delete(ctx, id) return c.activeHandles.Delete(ctx, id)
} }
// const maxInt = int(^uint(0) >> 1) const maxHandlers = int(^uint(0) >> 1)
const maxHandlers = 8129
// HandleLimit exports how many file handles can be safely stored by this cache. // HandleLimit exports how many file handles can be safely stored by this cache.
func (c *CachingHandler) HandleLimit() int { func (c *CachingHandler) HandleLimit() int {

View file

@ -8,7 +8,6 @@ import (
"time" "time"
"github.com/royalcat/ctxio" "github.com/royalcat/ctxio"
"go.opentelemetry.io/otel"
) )
type File interface { type File interface {
@ -24,8 +23,6 @@ type File interface {
var ErrNotImplemented = errors.New("not implemented") var ErrNotImplemented = errors.New("not implemented")
var tracer = otel.Tracer("git.kmsign.ru/royalcat/tstor/src/vfs")
type Filesystem interface { type Filesystem interface {
// Open opens the named file for reading. If successful, methods on the // Open opens the named file for reading. If successful, methods on the
// returned file can be used for reading; the associated file descriptor has // returned file can be used for reading; the associated file descriptor has

View file

@ -3,19 +3,32 @@ package vfs
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io/fs" "io/fs"
"log/slog" "log/slog"
"reflect" "reflect"
"time" "time"
"git.kmsign.ru/royalcat/tstor/pkg/rlog" "git.kmsign.ru/royalcat/tstor/pkg/rlog"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
var (
meter = otel.Meter("git.kmsign.ru/royalcat/tstor/src/vfs")
tracer = otel.Tracer("git.kmsign.ru/royalcat/tstor/src/vfs")
)
type fsTelemetry struct {
openedFiles metric.Int64UpDownCounter
}
type LogFS struct { type LogFS struct {
fs Filesystem fs Filesystem
log *rlog.Logger log *rlog.Logger
tel *fsTelemetry
timeout time.Duration timeout time.Duration
readTimeout time.Duration readTimeout time.Duration
@ -27,13 +40,19 @@ func isLoggableError(err error) bool {
var _ Filesystem = (*LogFS)(nil) var _ Filesystem = (*LogFS)(nil)
func WrapLogFS(vfs Filesystem) *LogFS { func WrapLogFS(vfs Filesystem) (*LogFS, error) {
openedFiles, err := meter.Int64UpDownCounter("vfs.opened_files")
if err != nil {
return nil, fmt.Errorf("failed to create opened_files metric: %w", err)
}
return &LogFS{ return &LogFS{
fs: vfs, fs: vfs,
log: rlog.Component("logfs"), log: rlog.Component("logfs"),
tel: &fsTelemetry{openedFiles: openedFiles},
timeout: time.Minute * 3, timeout: time.Minute * 3,
readTimeout: time.Minute, readTimeout: time.Minute,
} }, nil
} }
// ModTime implements Filesystem. // ModTime implements Filesystem.
@ -104,7 +123,12 @@ func (fs *LogFS) Open(ctx context.Context, filename string) (file File, err erro
if isLoggableError(err) { if isLoggableError(err) {
fs.log.Error(ctx, "Failed to open file") fs.log.Error(ctx, "Failed to open file")
} }
file = WrapLogFile(file, filename, fs.log, fs.readTimeout) file = wrapLogFile(file, filename, fs.log, fs.readTimeout, fs.tel)
if file != nil {
fs.tel.openedFiles.Add(ctx, 1)
}
return file, err return file, err
} }
@ -178,7 +202,9 @@ type LogFile struct {
filename string filename string
f File f File
log *rlog.Logger log *rlog.Logger
tel *fsTelemetry
timeout time.Duration timeout time.Duration
} }
@ -194,11 +220,12 @@ func (f *LogFile) Type() fs.FileMode {
var _ File = (*LogFile)(nil) var _ File = (*LogFile)(nil)
func WrapLogFile(f File, filename string, log *rlog.Logger, timeout time.Duration) *LogFile { func wrapLogFile(f File, filename string, log *rlog.Logger, timeout time.Duration, tel *fsTelemetry) *LogFile {
return &LogFile{ return &LogFile{
filename: filename, filename: filename,
f: f, f: f,
log: log.With(slog.String("filename", filename)), log: log.With(slog.String("filename", filename)),
tel: tel,
timeout: timeout, timeout: timeout,
} }
} }
@ -221,6 +248,11 @@ func (f *LogFile) Close(ctx context.Context) (err error) {
if isLoggableError(err) { if isLoggableError(err) {
f.log.Error(ctx, "Failed to close", rlog.Error(err)) f.log.Error(ctx, "Failed to close", rlog.Error(err))
} }
if err != nil {
f.tel.openedFiles.Add(ctx, -1)
}
return err return err
} }