Skip to content

Commit

Permalink
Use file extensions as extra metadata
Browse files Browse the repository at this point in the history
Compression algorithm and encryption are derived from the file name.

This is a breaking change for people already using encryption.
  • Loading branch information
hifi committed Dec 28, 2023
1 parent e71e685 commit 1822096
Show file tree
Hide file tree
Showing 14 changed files with 568 additions and 478 deletions.
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

0 comments on commit 1822096

Please sign in to comment.