Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file extensions as extra metadata #554

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 37 additions & 47 deletions abs/replica_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ func (c *ReplicaClient) Snapshots(ctx context.Context, generation string) (lites
}

// WriteSnapshot writes LZ4 compressed data from rd to the object storage.
func (c *ReplicaClient) WriteSnapshot(ctx context.Context, generation string, index int, rd io.Reader) (info litestream.SnapshotInfo, err error) {
func (c *ReplicaClient) WriteSnapshot(ctx context.Context, info *litestream.SnapshotInfo, rd io.Reader) error {
if err := c.Init(ctx); err != nil {
return info, err
return err
}

key, err := litestream.SnapshotPath(c.Path, generation, index)
key, err := litestream.SnapshotPath(c.Path, *info)
if err != nil {
return info, fmt.Errorf("cannot determine snapshot path: %w", err)
return fmt.Errorf("cannot determine snapshot path: %w", err)
}
startTime := time.Now()

Expand All @@ -186,29 +186,26 @@ func (c *ReplicaClient) WriteSnapshot(ctx context.Context, generation string, in
BlobHTTPHeaders: azblob.BlobHTTPHeaders{ContentType: "application/octet-stream"},
BlobAccessTier: azblob.DefaultAccessTier,
}); err != nil {
return info, err
return err
}

internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc()
internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(rc.N()))

// log.Printf("%s(%s): snapshot: creating %s/%08x t=%s", r.db.Path(), r.Name(), generation, index, time.Since(startTime).Truncate(time.Millisecond))

return litestream.SnapshotInfo{
Generation: generation,
Index: index,
Size: rc.N(),
CreatedAt: startTime.UTC(),
}, nil
info.Size = rc.N()
info.CreatedAt = startTime.UTC()
return nil
}

// SnapshotReader returns a reader for snapshot data at the given generation/index.
func (c *ReplicaClient) SnapshotReader(ctx context.Context, generation string, index int) (io.ReadCloser, error) {
func (c *ReplicaClient) SnapshotReader(ctx context.Context, info litestream.SnapshotInfo) (io.ReadCloser, error) {
if err := c.Init(ctx); err != nil {
return nil, err
}

key, err := litestream.SnapshotPath(c.Path, generation, index)
key, err := litestream.SnapshotPath(c.Path, info)
if err != nil {
return nil, fmt.Errorf("cannot determine snapshot path: %w", err)
}
Expand All @@ -228,12 +225,12 @@ func (c *ReplicaClient) SnapshotReader(ctx context.Context, generation string, i
}

// DeleteSnapshot deletes a snapshot with the given generation & index.
func (c *ReplicaClient) DeleteSnapshot(ctx context.Context, generation string, index int) error {
func (c *ReplicaClient) DeleteSnapshot(ctx context.Context, info litestream.SnapshotInfo) error {
if err := c.Init(ctx); err != nil {
return err
}

key, err := litestream.SnapshotPath(c.Path, generation, index)
key, err := litestream.SnapshotPath(c.Path, info)
if err != nil {
return fmt.Errorf("cannot determine snapshot path: %w", err)
}
Expand All @@ -258,14 +255,14 @@ func (c *ReplicaClient) WALSegments(ctx context.Context, generation string) (lit
}

// WriteWALSegment writes LZ4 compressed data from rd into a file on disk.
func (c *ReplicaClient) WriteWALSegment(ctx context.Context, pos litestream.Pos, rd io.Reader) (info litestream.WALSegmentInfo, err error) {
func (c *ReplicaClient) WriteWALSegment(ctx context.Context, info *litestream.WALSegmentInfo, rd io.Reader) error {
if err := c.Init(ctx); err != nil {
return info, err
return err
}

key, err := litestream.WALSegmentPath(c.Path, pos.Generation, pos.Index, pos.Offset)
key, err := litestream.WALSegmentPath(c.Path, *info)
if err != nil {
return info, fmt.Errorf("cannot determine wal segment path: %w", err)
return fmt.Errorf("cannot determine wal segment path: %w", err)
}
startTime := time.Now()

Expand All @@ -276,29 +273,25 @@ func (c *ReplicaClient) WriteWALSegment(ctx context.Context, pos litestream.Pos,
BlobHTTPHeaders: azblob.BlobHTTPHeaders{ContentType: "application/octet-stream"},
BlobAccessTier: azblob.DefaultAccessTier,
}); err != nil {
return info, err
return err
}

internal.OperationTotalCounterVec.WithLabelValues(ReplicaClientType, "PUT").Inc()
internal.OperationBytesCounterVec.WithLabelValues(ReplicaClientType, "PUT").Add(float64(rc.N()))

return litestream.WALSegmentInfo{
Generation: pos.Generation,
Index: pos.Index,
Offset: pos.Offset,
Size: rc.N(),
CreatedAt: startTime.UTC(),
}, nil
info.Size = rc.N()
info.CreatedAt = startTime.UTC()
return nil
}

// WALSegmentReader returns a reader for a section of WAL data at the given index.
// Returns os.ErrNotExist if no matching index/offset is found.
func (c *ReplicaClient) WALSegmentReader(ctx context.Context, pos litestream.Pos) (io.ReadCloser, error) {
func (c *ReplicaClient) WALSegmentReader(ctx context.Context, info litestream.WALSegmentInfo) (io.ReadCloser, error) {
if err := c.Init(ctx); err != nil {
return nil, err
}

key, err := litestream.WALSegmentPath(c.Path, pos.Generation, pos.Index, pos.Offset)
key, err := litestream.WALSegmentPath(c.Path, info)
if err != nil {
return nil, fmt.Errorf("cannot determine wal segment path: %w", err)
}
Expand All @@ -318,13 +311,13 @@ func (c *ReplicaClient) WALSegmentReader(ctx context.Context, pos litestream.Pos
}

// DeleteWALSegments deletes WAL segments with at the given positions.
func (c *ReplicaClient) DeleteWALSegments(ctx context.Context, a []litestream.Pos) error {
func (c *ReplicaClient) DeleteWALSegments(ctx context.Context, a []litestream.WALSegmentInfo) error {
if err := c.Init(ctx); err != nil {
return err
}

for _, pos := range a {
key, err := litestream.WALSegmentPath(c.Path, pos.Generation, pos.Index, pos.Offset)
for _, info := range a {
key, err := litestream.WALSegmentPath(c.Path, info)
if err != nil {
return fmt.Errorf("cannot determine wal segment path: %w", err)
}
Expand Down Expand Up @@ -388,19 +381,18 @@ func (itr *snapshotIterator) fetch() error {
marker = resp.NextMarker

for _, item := range resp.Segment.BlobItems {
key := path.Base(item.Name)
index, err := litestream.ParseSnapshotPath(key)
if err != nil {
continue
}

info := litestream.SnapshotInfo{
Generation: itr.generation,
Index: index,
Size: *item.Properties.ContentLength,
CreatedAt: item.Properties.CreationTime.UTC(),
}

key := path.Base(item.Name)
err := info.ParsePath(key)
if err != nil {
continue
}

select {
case <-itr.ctx.Done():
case itr.ch <- info:
Expand Down Expand Up @@ -494,20 +486,18 @@ func (itr *walSegmentIterator) fetch() error {
marker = resp.NextMarker

for _, item := range resp.Segment.BlobItems {
key := path.Base(item.Name)
index, offset, err := litestream.ParseWALSegmentPath(key)
if err != nil {
continue
}

info := litestream.WALSegmentInfo{
Generation: itr.generation,
Index: index,
Offset: offset,
Size: *item.Properties.ContentLength,
CreatedAt: item.Properties.CreationTime.UTC(),
}

key := path.Base(item.Name)
err := info.ParsePath(key)
if err != nil {
continue
}

select {
case <-itr.ctx.Done():
case itr.ch <- info:
Expand Down
1 change: 1 addition & 0 deletions cmd/litestream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"filippo.io/age"
"github.com/benbjohnson/litestream"

"github.com/benbjohnson/litestream/abs"
"github.com/benbjohnson/litestream/file"
"github.com/benbjohnson/litestream/gcs"
Expand Down
Loading