Documentation
¶
Overview ¶
Package blobbridge moves blob bytes between a local engine and the origin server it syncs with.
Rows that hold a syncplane.v1.BlobRef sync through the mutation log, but the bytes they name do not. A Bridge closes that gap in both directions:
- Uploads: a blob committed in the local engine and named by a live row of a synced entity is uploaded to the origin, in the same workspace.
- Read-through: a read of a blob that the local engine does not hold, but that a live local row names, fetches it from the origin and commits it in the local engine.
- Prefetch (optional): blobs that newly written rows name are fetched before a reader asks for them.
A Bridge is a blob.Store that wraps the local engine's store. Pass it to server.WithBlobStore, then call Run with the server:
br := blobbridge.New(fsStore, originClient.BlobService()) srv, err := server.New(store, server.WithBlobStore(br)) ... go br.Run(ctx, srv)
The local workspace id is the origin workspace id. The bridge authenticates to the origin with the BlobServiceClient it is given, so the origin applies its own upload policy and read rule to that identity.
Example ¶
A local engine whose blobs reach the origin, and whose readers get the origin's blobs.
package main
import (
"context"
"log"
"github.com/levelfourab/syncplane/engine/blob/fsstore"
"github.com/levelfourab/syncplane/engine/client"
testdatav1 "github.com/levelfourab/syncplane/engine/gen/syncplane/testdata/v1"
"github.com/levelfourab/syncplane/engine/server"
"github.com/levelfourab/syncplane/engine/storage/sqlitestore"
"github.com/levelfourab/syncplane/engine/upstream/blobbridge"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
origin, err := client.New("https://sync.example.com")
if err != nil {
log.Fatal(err)
}
blobs, err := fsstore.Open("/var/lib/app/blobs")
if err != nil {
log.Fatal(err)
}
bridge := blobbridge.New(blobs, origin.BlobService(),
blobbridge.WithEntities("blob_holder"),
)
defer bridge.Close()
store, err := sqlitestore.Open("/var/lib/app/local.db")
if err != nil {
log.Fatal(err)
}
defer store.Close()
local, err := server.New(store,
server.WithEntities(&testdatav1.BlobHolder{}),
server.WithBlobStore(bridge),
)
if err != nil {
log.Fatal(err)
}
defer local.Close()
go func() {
if err := bridge.Run(ctx, local); err != nil {
log.Print(err)
}
}()
// Serve local.HTTPHandler() to the app's clients.
}
Output:
Index ¶
- Constants
- Variables
- type Bridge
- func (b *Bridge) Abort(ctx context.Context, workspaceID string, digest blob.Digest) error
- func (b *Bridge) Begin(ctx context.Context, workspaceID string, m blob.Manifest) error
- func (b *Bridge) Close() error
- func (b *Bridge) Complete(ctx context.Context, workspaceID string, digest blob.Digest) error
- func (b *Bridge) Delete(ctx context.Context, workspaceID string, digest blob.Digest) error
- func (b *Bridge) DeleteWorkspace(ctx context.Context, workspaceID string) error
- func (b *Bridge) List(ctx context.Context, workspaceID string) iter.Seq2[blob.Digest, error]
- func (b *Bridge) ListStaged(ctx context.Context, workspaceID string) iter.Seq2[blob.Upload, error]
- func (b *Bridge) ListWorkspaces(ctx context.Context) iter.Seq2[string, error]
- func (b *Bridge) Open(ctx context.Context, workspaceID string, digest blob.Digest) (io.ReadSeekCloser, error)
- func (b *Bridge) PutChunk(ctx context.Context, workspaceID string, digest blob.Digest, index int, ...) error
- func (b *Bridge) Run(ctx context.Context, srv *server.Server) error
- func (b *Bridge) SetUnreferencedSince(ctx context.Context, workspaceID string, digest blob.Digest, t time.Time) error
- func (b *Bridge) Staged(ctx context.Context, workspaceID string, digest blob.Digest) (blob.Upload, []int, error)
- func (b *Bridge) Stat(ctx context.Context, workspaceID string, digest blob.Digest) (blob.Info, error)
- type Option
- func WithBackoff(minWait, maxWait time.Duration) Option
- func WithConcurrency(n int) Option
- func WithConsumerName(name string) Option
- func WithEntities(names ...string) Option
- func WithFetchPatience(d time.Duration) Option
- func WithLogger(l *slog.Logger) Option
- func WithPrefetch() Option
- func WithReadWait(d time.Duration) Option
- func WithSweepInterval(d time.Duration) Option
Examples ¶
Constants ¶
const ( DefaultConcurrency = 4 DefaultMinBackoff = time.Second DefaultMaxBackoff = time.Minute DefaultSweepInterval = time.Minute DefaultFetchPatience = 10 * time.Minute DefaultConsumerName = "blobbridge" )
Default values of the options.
const StageWait = 10 * time.Second
StageWait is how long a read of a blob that the local engine does not hold waits, at most, for the fetch from the origin to stage the blob locally.
Variables ¶
var ErrRunning = errors.New("blobbridge: already running")
ErrRunning is returned by Run when another Run of the same Bridge has not returned yet.
Functions ¶
This section is empty.
Types ¶
type Bridge ¶
type Bridge struct {
// contains filtered or unexported fields
}
Bridge is a blob.Store that wraps the local engine's store and moves blobs between that engine and an origin server. Build one with New, pass it to server.WithBlobStore, and call Run with the server.
Without a Run in progress, the Bridge is the wrapped store and nothing more. A Bridge is safe for concurrent use.
func New ¶
func New(inner blob.Store, origin syncplanev1connect.BlobServiceClient, opts ...Option) *Bridge
New returns a Bridge over inner, the local engine's blob store, that transfers blobs with origin. The Bridge owns inner: Close closes it.
The local server must use the Bridge as its only blob store, and must not be hosted by a server.Manager: the Bridge reads the names in inner as workspace ids.
func (*Bridge) Complete ¶
Complete commits a staged upload in the wrapped store. While Run runs, a commit queues the blob for upload to the origin.
func (*Bridge) DeleteWorkspace ¶
DeleteWorkspace removes a workspace from the wrapped store.
func (*Bridge) ListStaged ¶
ListStaged lists the staged uploads of the wrapped store.
func (*Bridge) ListWorkspaces ¶
ListWorkspaces lists the workspaces of the wrapped store.
func (*Bridge) Open ¶
func (b *Bridge) Open(ctx context.Context, workspaceID string, digest blob.Digest) (io.ReadSeekCloser, error)
Open opens a committed blob of the wrapped store.
func (*Bridge) PutChunk ¶
func (b *Bridge) PutChunk(ctx context.Context, workspaceID string, digest blob.Digest, index int, r io.Reader) error
PutChunk stores a chunk in the wrapped store.
func (*Bridge) Run ¶
Run moves blobs between srv and the origin until ctx ends, then waits for the transfers in progress to stop and returns nil. srv must be the server that uses the Bridge as its blob store.
Uploads: Run uploads a blob when it is committed locally, and when a row that names it is committed. It follows the change feed of each local workspace from a position it keeps in the local engine (see WithConsumerName), so its work follows the workspaces being written. At start it also checks every blob committed in the local store, a bounded number at a time, and uploads those that a live row names and the origin does not report complete. An interrupted upload resumes from the chunks the origin holds. Failed transfers are retried with backoff; a refusal by the origin (invalid_argument, resource_exhausted, permission_denied) is logged and retried at the next Run.
Reads: while Run runs, a read of a blob that the local store does not hold starts a fetch from the origin when a live row of the local workspace names the blob (see Stat). Concurrent reads of one blob share one fetch. Each chunk is checked against its hash before it is stored.
Run returns ErrRunning when another Run of the Bridge is in progress, and the server's error when the consumer name is not valid.
func (*Bridge) SetUnreferencedSince ¶
func (b *Bridge) SetUnreferencedSince(ctx context.Context, workspaceID string, digest blob.Digest, t time.Time) error
SetUnreferencedSince sets the mark of a committed blob in the wrapped store.
func (*Bridge) Staged ¶
func (b *Bridge) Staged(ctx context.Context, workspaceID string, digest blob.Digest) (blob.Upload, []int, error)
Staged reads a staged upload from the wrapped store.
func (*Bridge) Stat ¶
func (b *Bridge) Stat(ctx context.Context, workspaceID string, digest blob.Digest) (blob.Info, error)
Stat returns a committed blob's Info from the wrapped store, or blob.ErrNotFound.
While Run runs, a blob the wrapped store does not hold starts a fetch from the origin. Stat then waits up to the read wait (see WithReadWait) for the fetch to commit the blob, and after that up to StageWait, or until ctx ends, for the fetch to stage it. A staged blob reads as being uploaded (Staged reports it), and the local engine notifies the workspace when it commits.
type Option ¶
type Option func(*config)
Option configures New.
func WithBackoff ¶
WithBackoff sets the wait before the first retry of a failed transfer or check, and the largest wait; the wait doubles after each failure, with jitter. Non-positive values keep the defaults, DefaultMinBackoff and DefaultMaxBackoff.
func WithConcurrency ¶
WithConcurrency sets how many transfers and checks run at the same time. Values below 1 mean 1. The default is DefaultConcurrency.
func WithConsumerName ¶
WithConsumerName sets the name under which Run keeps its position in the change feed of each local workspace (see server.AdvanceConsumer). The name must not be used by any other consumer of the local engine. The default is DefaultConsumerName.
func WithEntities ¶
WithEntities limits the bridge to blobs that rows of the named entities name. A blob that only rows of other entities name is not uploaded and not fetched. Without this option every entity counts. Call it with the entities that sync with the origin.
func WithFetchPatience ¶
WithFetchPatience sets how long the bridge keeps trying to fetch a blob after the last read or prefetch that asked for it, while the origin does not have it or cannot be reached. Non-positive values keep the default, DefaultFetchPatience.
func WithLogger ¶
WithLogger sets the logger for transfer failures. The default is slog.Default().
func WithPrefetch ¶
func WithPrefetch() Option
WithPrefetch makes the bridge fetch a blob from the origin as soon as a row that names it is committed in the local engine, without waiting for a read. It applies to rows committed while Run runs. Prefetch is off by default.
func WithReadWait ¶
WithReadWait sets how long a read of a blob that the local engine does not hold waits for the fetch from the origin to commit it (see Bridge.Stat). A read that the fetch does not finish in time sees the blob as being uploaded once the fetch has staged it, and the local engine notifies the workspace when the blob commits. The default, 0, waits only for the fetch to stage the blob.
func WithSweepInterval ¶
WithSweepInterval sets how often Run lists the local workspaces whose change feed has rows it has not read, to find writes that no activity notification reported. Non-positive values keep the default, DefaultSweepInterval.