ev/driver/disk-store/disk-store.go

403 lines
8.9 KiB
Go
Raw Normal View History

2022-10-25 16:07:46 -06:00
// package diskstore provides a driver that reads and writes events to disk.
2022-10-13 15:32:25 -06:00
2022-08-04 21:07:10 -06:00
package diskstore
2022-08-04 14:37:51 -06:00
import (
"context"
"errors"
2022-08-04 14:37:51 -06:00
"fmt"
"hash/fnv"
2022-08-04 14:37:51 -06:00
"os"
"path/filepath"
"strings"
"github.com/tidwall/wal"
"go.opentelemetry.io/otel/attribute"
2023-07-12 17:35:02 -06:00
"go.opentelemetry.io/otel/metric"
"go.uber.org/multierr"
2022-08-04 14:37:51 -06:00
2023-07-12 17:35:02 -06:00
"go.sour.is/pkg/cache"
"go.sour.is/pkg/lg"
"go.sour.is/pkg/locker"
2023-02-26 22:33:01 -07:00
"go.sour.is/ev"
2023-09-29 10:07:24 -06:00
"go.sour.is/ev/driver"
"go.sour.is/ev/event"
2022-08-04 14:37:51 -06:00
)
const CachSize = 1000
const AppendOnly = ev.AppendOnly
const AllEvents = ev.AllEvents
type lockedWal = locker.Locked[wal.Log]
2022-08-10 21:18:57 -06:00
type openlogs struct {
logs *cache.Cache[string, *lockedWal]
2022-08-10 21:18:57 -06:00
}
2022-08-04 14:37:51 -06:00
type diskStore struct {
2022-08-10 21:18:57 -06:00
path string
openlogs *locker.Locked[openlogs]
2023-07-12 17:35:02 -06:00
m_disk_open metric.Int64Counter
m_disk_evict metric.Int64Counter
m_disk_read metric.Int64Counter
m_disk_write metric.Int64Counter
2022-08-04 14:37:51 -06:00
}
var _ driver.Driver = (*diskStore)(nil)
2022-08-04 14:37:51 -06:00
2022-08-06 09:52:36 -06:00
func Init(ctx context.Context) error {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
2022-09-08 10:32:35 -06:00
d := &diskStore{}
m := lg.Meter(ctx)
var err, errs error
2023-07-12 17:35:02 -06:00
d.m_disk_open, err = m.Int64Counter("disk_open")
errs = multierr.Append(errs, err)
2023-07-12 17:35:02 -06:00
d.m_disk_evict, err = m.Int64Counter("disk_evict")
errs = multierr.Append(errs, err)
2023-07-12 17:35:02 -06:00
d.m_disk_read, err = m.Int64Counter("disk_read")
2022-09-08 10:32:35 -06:00
errs = multierr.Append(errs, err)
2023-07-12 17:35:02 -06:00
d.m_disk_write, err = m.Int64Counter("disk_write")
2022-09-08 10:32:35 -06:00
errs = multierr.Append(errs, err)
ev.Register(ctx, "file", d)
return errs
2022-08-04 14:37:51 -06:00
}
func (d *diskStore) Open(ctx context.Context, dsn string) (driver.Driver, error) {
2022-09-08 10:32:35 -06:00
_, span := lg.Span(ctx)
defer span.End()
span.SetAttributes(
attribute.String("args.dsn", dsn),
)
2022-08-04 14:37:51 -06:00
scheme, path, ok := strings.Cut(dsn, ":")
if !ok {
return nil, fmt.Errorf("expected scheme")
}
if scheme != "file" {
return nil, fmt.Errorf("expeted scheme=file, got=%s", scheme)
}
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, 0700)
if err != nil {
2022-09-08 10:32:35 -06:00
span.RecordError(err)
2022-08-04 14:37:51 -06:00
return nil, err
}
}
c, err := cache.NewWithEvict(CachSize, func(ctx context.Context, s string, l *lockedWal) {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
l.Use(ctx, func(ctx context.Context, w *wal.Log) error {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
2022-09-08 10:32:35 -06:00
d.m_disk_evict.Add(ctx, 1)
err := w.Close()
if err != nil {
2022-08-14 10:04:15 -06:00
span.RecordError(err)
return err
}
return nil
})
})
if err != nil {
2022-08-14 10:04:15 -06:00
span.RecordError(err)
return nil, err
}
logs := &openlogs{logs: c}
return &diskStore{
2022-09-08 10:33:02 -06:00
path: path,
openlogs: locker.New(logs),
2022-09-08 10:32:35 -06:00
m_disk_open: d.m_disk_open,
m_disk_evict: d.m_disk_evict,
2022-09-08 10:33:02 -06:00
m_disk_read: d.m_disk_read,
m_disk_write: d.m_disk_write,
}, nil
2022-08-04 14:37:51 -06:00
}
2022-08-16 16:06:25 -06:00
func (d *diskStore) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
span.SetAttributes(
attribute.String("args.streamID", streamID),
attribute.String("path", d.path),
)
2022-09-08 10:32:35 -06:00
el := &eventLog{streamID: streamID, diskStore: d}
2022-08-10 21:18:57 -06:00
return el, d.openlogs.Use(ctx, func(ctx context.Context, openlogs *openlogs) error {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
if events, ok := openlogs.logs.Get(streamID); ok {
el.events = *events
2022-08-10 21:18:57 -06:00
return nil
}
2022-09-08 10:32:35 -06:00
d.m_disk_open.Add(ctx, 1)
// migrate streams into dir friendly subdirs
hashPart := mkDirName(streamID)
oldPath := filepath.Join(d.path, streamID)
newPath := filepath.Join(d.path, hashPart, streamID)
if _, err := os.Stat(oldPath); !os.IsNotExist(err) {
os.MkdirAll(filepath.Join(d.path, hashPart), 0700)
os.Rename(oldPath, newPath)
}
l, err := wal.Open(newPath, wal.DefaultOptions)
2022-08-10 21:18:57 -06:00
if err != nil {
2022-08-14 10:04:15 -06:00
span.RecordError(err)
2022-08-10 21:18:57 -06:00
return err
}
2022-09-08 10:32:35 -06:00
2022-08-10 21:18:57 -06:00
el.events = locker.New(l)
openlogs.logs.Add(ctx, streamID, el.events)
2022-08-10 21:18:57 -06:00
return nil
})
}
2022-08-04 14:37:51 -06:00
type eventLog struct {
2022-09-08 10:33:02 -06:00
streamID string
events *locker.Locked[wal.Log]
diskStore *diskStore
}
2022-08-04 14:37:51 -06:00
var _ driver.EventLog = (*eventLog)(nil)
2022-08-04 14:37:51 -06:00
2022-08-16 16:06:25 -06:00
func (e *eventLog) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
span.SetAttributes(
attribute.Int("args.events", len(events)),
attribute.Int64("args.version", int64(version)),
attribute.String("streamID", e.streamID),
attribute.String("path", e.diskStore.path),
)
2022-08-16 16:06:25 -06:00
event.SetStreamID(e.streamID, events...)
2022-08-04 14:37:51 -06:00
var count uint64
err := e.events.Use(ctx, func(ctx context.Context, l *wal.Log) error {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
last, err := l.LastIndex()
2022-08-04 14:37:51 -06:00
if err != nil {
2022-08-14 10:04:15 -06:00
span.RecordError(err)
return err
2022-08-04 14:37:51 -06:00
}
if version != AppendOnly && version != last {
err = fmt.Errorf("%w: current version wrong %d != %d", ev.ErrWrongVersion, version, last)
2022-10-25 16:07:46 -06:00
span.RecordError(err)
return err
}
2022-08-04 14:37:51 -06:00
var b []byte
2022-08-04 14:37:51 -06:00
batch := &wal.Batch{}
for i, e := range events {
2022-08-13 18:59:15 -06:00
span.AddEvent(fmt.Sprintf("append event %d of %d", i, len(events)))
2022-08-15 08:05:04 -06:00
b, err = event.MarshalBinary(e)
if err != nil {
2022-08-14 10:04:15 -06:00
span.RecordError(err)
return err
}
pos := last + uint64(i) + 1
event.SetPosition(e, pos)
2022-08-04 14:37:51 -06:00
batch.Write(pos, b)
2022-08-04 14:37:51 -06:00
}
count = uint64(len(events))
2022-09-08 10:32:35 -06:00
e.diskStore.m_disk_write.Add(ctx, int64(len(events)))
return l.WriteBatch(batch)
})
2022-10-25 16:07:46 -06:00
span.RecordError(err)
2022-08-04 14:37:51 -06:00
return count, err
2022-08-04 14:37:51 -06:00
}
func (e *eventLog) ReadN(ctx context.Context, index ...uint64) (event.Events, error) {
ctx, span := lg.Span(ctx)
defer span.End()
lis := make([]int64, len(index))
for i := range index {
lis[i] = int64(index[i])
}
span.SetAttributes(
attribute.Int64Slice("args.index", lis),
attribute.String("streamID", e.streamID),
attribute.String("path", e.diskStore.path),
)
var events event.Events
err := e.events.Use(ctx, func(ctx context.Context, stream *wal.Log) error {
var err error
events, err = readStreamN(ctx, stream, index...)
return err
})
return events, err
}
func (e *eventLog) Read(ctx context.Context, after, count int64) (event.Events, error) {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
span.SetAttributes(
attribute.Int64("args.after", after),
attribute.Int64("args.count", count),
attribute.String("streamID", e.streamID),
attribute.String("path", e.diskStore.path),
)
2022-08-13 18:59:15 -06:00
var events event.Events
err := e.events.Use(ctx, func(ctx context.Context, stream *wal.Log) error {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
first, err := stream.FirstIndex()
2022-08-04 14:37:51 -06:00
if err != nil {
return err
}
last, err := stream.LastIndex()
2022-08-04 14:37:51 -06:00
if err != nil {
return err
}
streamIDs, err := driver.GenerateStreamIDs(first, last, after, count)
if err != nil {
return err
}
events, err = readStreamN(ctx, stream, streamIDs...)
event.SetStreamID(e.streamID, events...)
return err
})
2022-08-07 11:55:49 -06:00
if err != nil {
2022-08-14 10:04:15 -06:00
span.RecordError(err)
return nil, err
2022-08-07 11:55:49 -06:00
}
2022-09-08 10:32:35 -06:00
e.diskStore.m_disk_read.Add(ctx, int64(len(events)))
2022-08-14 10:04:15 -06:00
return events, nil
2022-08-04 14:37:51 -06:00
}
2022-08-16 16:06:25 -06:00
func (e *eventLog) FirstIndex(ctx context.Context) (uint64, error) {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
2022-11-20 10:26:20 -07:00
span.SetAttributes(
attribute.String("streamID", e.streamID),
attribute.String("path", e.diskStore.path),
)
2022-08-13 18:59:15 -06:00
var idx uint64
var err error
err = e.events.Use(ctx, func(ctx context.Context, events *wal.Log) error {
idx, err = events.FirstIndex()
return err
})
return idx, err
2022-08-04 14:37:51 -06:00
}
2022-08-16 16:06:25 -06:00
func (e *eventLog) LastIndex(ctx context.Context) (uint64, error) {
ctx, span := lg.Span(ctx)
2022-08-13 18:59:15 -06:00
defer span.End()
span.SetAttributes(
attribute.String("streamID", e.streamID),
attribute.String("path", e.diskStore.path),
)
var idx uint64
var err error
err = e.events.Use(ctx, func(ctx context.Context, events *wal.Log) error {
idx, err = events.LastIndex()
return err
})
2022-08-04 14:37:51 -06:00
return idx, err
2022-08-04 14:37:51 -06:00
}
func (e *eventLog) Truncate(ctx context.Context, index int64) error {
ctx, span := lg.Span(ctx)
defer span.End()
2022-11-20 10:26:20 -07:00
span.SetAttributes(
attribute.Int64("args.index", index),
attribute.String("streamID", e.streamID),
attribute.String("path", e.diskStore.path),
)
if index == 0 {
return nil
}
return e.events.Use(ctx, func(ctx context.Context, events *wal.Log) error {
if index < 0 {
return events.TruncateBack(uint64(-index))
}
return events.TruncateFront(uint64(index))
})
2022-08-14 10:04:15 -06:00
}
2022-10-30 09:18:08 -06:00
func readStreamN(ctx context.Context, stream *wal.Log, index ...uint64) (event.Events, error) {
ctx, span := lg.Span(ctx)
2022-10-30 09:18:08 -06:00
defer span.End()
lis := make([]int64, len(index))
for i := range index {
2022-11-20 10:26:20 -07:00
lis[i] = int64(index[i])
}
span.SetAttributes(
attribute.Int64Slice("args.index", lis),
)
2022-10-30 09:18:08 -06:00
var b []byte
var err error
events := make(event.Events, len(index))
for i, idx := range index {
b, err = stream.Read(idx)
if err != nil {
if errors.Is(err, wal.ErrNotFound) || errors.Is(err, wal.ErrOutOfRange) {
err = fmt.Errorf("%w: empty", ev.ErrNotFound)
2022-10-30 09:18:08 -06:00
}
span.RecordError(err)
return nil, err
}
events[i], err = event.UnmarshalBinary(ctx, b, idx)
if err != nil {
span.RecordError(err)
return nil, err
}
2023-07-26 16:52:42 -06:00
span.AddEvent(fmt.Sprintf("read event %d of %d - %d", i, len(events), events[i].EventMeta().ActualPosition))
2022-10-30 09:18:08 -06:00
}
return events, err
}
func mkDirName(name string) string {
h := fnv.New32a()
fmt.Fprint(h, name)
return fmt.Sprintf("%x/%x/%x", h.Sum32()>>24&0xff, h.Sum32()>>16&0xff, h.Sum32()&0xffff)
}