2022-08-04 21:07:10 -06:00
|
|
|
package diskstore
|
2022-08-04 14:37:51 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-08-10 21:18:04 -06:00
|
|
|
"log"
|
2022-08-04 14:37:51 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/tidwall/wal"
|
2022-08-13 13:34:13 -06:00
|
|
|
"go.opentelemetry.io/otel/metric/instrument/syncint64"
|
|
|
|
"go.uber.org/multierr"
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
"github.com/sour-is/ev/internal/logz"
|
|
|
|
"github.com/sour-is/ev/pkg/cache"
|
2022-08-04 14:37:51 -06:00
|
|
|
"github.com/sour-is/ev/pkg/es"
|
|
|
|
"github.com/sour-is/ev/pkg/es/driver"
|
|
|
|
"github.com/sour-is/ev/pkg/es/event"
|
2022-08-09 16:23:33 -06:00
|
|
|
"github.com/sour-is/ev/pkg/locker"
|
2022-08-04 14:37:51 -06:00
|
|
|
"github.com/sour-is/ev/pkg/math"
|
|
|
|
)
|
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
const CachSize = 1000
|
|
|
|
|
|
|
|
type lockedWal = locker.Locked[wal.Log]
|
2022-08-10 21:18:57 -06:00
|
|
|
type openlogs struct {
|
2022-08-13 13:34:13 -06:00
|
|
|
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]
|
2022-08-13 13:34:13 -06:00
|
|
|
|
|
|
|
Mdisk_open syncint64.Counter
|
|
|
|
Mdisk_evict syncint64.Counter
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
const AppendOnly = es.AppendOnly
|
|
|
|
const AllEvents = es.AllEvents
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-06 09:52:36 -06:00
|
|
|
func Init(ctx context.Context) error {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
m := logz.Meter(ctx)
|
|
|
|
var err, errs error
|
|
|
|
|
|
|
|
Mdisk_open, err := m.SyncInt64().Counter("disk_open")
|
|
|
|
errs = multierr.Append(errs, err)
|
|
|
|
|
|
|
|
Mdisk_evict, err := m.SyncInt64().Counter("disk_evict")
|
|
|
|
errs = multierr.Append(errs, err)
|
|
|
|
|
|
|
|
es.Register(ctx, "file", &diskStore{
|
|
|
|
Mdisk_open: Mdisk_open,
|
|
|
|
Mdisk_evict: Mdisk_evict,
|
|
|
|
})
|
|
|
|
|
|
|
|
return errs
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
var _ driver.Driver = (*diskStore)(nil)
|
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
func (d *diskStore) Open(ctx context.Context, dsn string) (driver.Driver, error) {
|
|
|
|
ctx, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
d.Mdisk_open.Add(ctx, 1)
|
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2022-08-13 13:34:13 -06:00
|
|
|
c, err := cache.NewWithEvict(CachSize, func(ctx context.Context, s string, l *lockedWal) {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
l.Modify(ctx, func(w *wal.Log) error {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
d.Mdisk_evict.Add(ctx, 1)
|
2022-08-13 13:34:13 -06:00
|
|
|
|
|
|
|
err := w.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logs := &openlogs{logs: c}
|
|
|
|
return &diskStore{
|
|
|
|
path: path,
|
|
|
|
openlogs: locker.New(logs),
|
|
|
|
Mdisk_open: d.Mdisk_open,
|
|
|
|
Mdisk_evict: d.Mdisk_evict,
|
|
|
|
}, nil
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
func (ds *diskStore) EventLog(ctx context.Context, streamID string) (driver.EventLog, error) {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
el := &eventLog{streamID: streamID}
|
2022-08-10 21:18:57 -06:00
|
|
|
|
|
|
|
return el, ds.openlogs.Modify(ctx, func(openlogs *openlogs) error {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-13 13:34:13 -06:00
|
|
|
if events, ok := openlogs.logs.Get(streamID); ok {
|
|
|
|
el.events = *events
|
2022-08-10 21:18:57 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := wal.Open(filepath.Join(ds.path, streamID), wal.DefaultOptions)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
el.events = locker.New(l)
|
2022-08-13 13:34:13 -06:00
|
|
|
openlogs.logs.Add(ctx, streamID, el.events)
|
2022-08-10 21:18:57 -06:00
|
|
|
return nil
|
|
|
|
})
|
2022-08-09 16:23:33 -06:00
|
|
|
}
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
type eventLog struct {
|
|
|
|
streamID string
|
|
|
|
events *locker.Locked[wal.Log]
|
|
|
|
}
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
var _ driver.EventLog = (*eventLog)(nil)
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
func (es *eventLog) Append(ctx context.Context, events event.Events, version uint64) (uint64, error) {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
event.SetStreamID(es.streamID, events...)
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
var count uint64
|
|
|
|
err := es.events.Modify(ctx, func(l *wal.Log) error {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
last, err := l.LastIndex()
|
2022-08-04 14:37:51 -06:00
|
|
|
if err != nil {
|
2022-08-09 16:23:33 -06:00
|
|
|
return err
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
if version != AppendOnly && version != last {
|
|
|
|
return fmt.Errorf("current version wrong %d != %d", version, last)
|
|
|
|
}
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
var b []byte
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -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-09 16:23:33 -06:00
|
|
|
b, err = event.MarshalText(e)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pos := last + uint64(i) + 1
|
|
|
|
event.SetPosition(e, pos)
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
batch.Write(pos, b)
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
count = uint64(len(events))
|
|
|
|
return l.WriteBatch(batch)
|
|
|
|
})
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
return count, err
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
func (es *eventLog) Read(ctx context.Context, pos, count int64) (event.Events, error) {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
var events event.Events
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
err := es.events.Modify(ctx, func(stream *wal.Log) error {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
first, err := stream.FirstIndex()
|
2022-08-04 14:37:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
last, err := stream.LastIndex()
|
2022-08-04 14:37:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
// ---
|
|
|
|
if first == 0 || last == 0 {
|
|
|
|
return nil
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-10 10:09:58 -06:00
|
|
|
start, count := math.PagerBox(first, last, pos, count)
|
2022-08-10 21:18:04 -06:00
|
|
|
log.Println("reading", first, last, pos, count, start)
|
2022-08-10 10:09:58 -06:00
|
|
|
if count == 0 {
|
|
|
|
return nil
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
events = make([]event.Event, math.Abs(count))
|
|
|
|
for i := range events {
|
2022-08-13 18:59:15 -06:00
|
|
|
span.AddEvent(fmt.Sprintf("read event %d of %d", i, len(events)))
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
// ---
|
|
|
|
var b []byte
|
|
|
|
b, err = stream.Read(start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
events[i], err = event.UnmarshalText(ctx, b, start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// ---
|
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
start += 1
|
|
|
|
} else {
|
|
|
|
start -= 1
|
|
|
|
}
|
|
|
|
if start < first || start > last {
|
|
|
|
events = events[:i+1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-08-07 11:55:49 -06:00
|
|
|
if err != nil {
|
2022-08-09 16:23:33 -06:00
|
|
|
return nil, err
|
2022-08-07 11:55:49 -06:00
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
|
|
|
|
event.SetStreamID(es.streamID, events...)
|
|
|
|
|
|
|
|
return events, err
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
func (es *eventLog) FirstIndex(ctx context.Context) (uint64, error) {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
var idx uint64
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = es.events.Modify(ctx, func(events *wal.Log) error {
|
|
|
|
idx, err = events.FirstIndex()
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return idx, err
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|
2022-08-09 16:23:33 -06:00
|
|
|
func (es *eventLog) LastIndex(ctx context.Context) (uint64, error) {
|
2022-08-13 18:59:15 -06:00
|
|
|
_, span := logz.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
var idx uint64
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = es.events.Modify(ctx, func(events *wal.Log) error {
|
|
|
|
idx, err = events.LastIndex()
|
|
|
|
return err
|
|
|
|
})
|
2022-08-04 14:37:51 -06:00
|
|
|
|
2022-08-09 16:23:33 -06:00
|
|
|
return idx, err
|
2022-08-04 14:37:51 -06:00
|
|
|
}
|