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

Conditionally write tiles. #70

Merged
merged 6 commits into from
Jan 9, 2024
Merged
Changes from 3 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
48 changes: 47 additions & 1 deletion experimental/gcp-log/internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package storage

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -376,14 +377,45 @@ func (c *Client) Sequence(ctx context.Context, leafhash []byte, leaf []byte) (ui
return 0, fmt.Errorf("couldn't create leafhash object: %w", err)
}
if err := wLeaf.Close(); err != nil {
return 0, fmt.Errorf("couldn't close writer for object %q", leafPath)
return 0, fmt.Errorf("couldn't close writer for object %q, %w", leafPath, err)
}

// All done!
return seq, nil
}
}

// assertContent checks that the content at `gcsPath` matches the passed in `data`.
func (c *Client) assertContent(ctx context.Context, gcsPath string, data []byte) (equal bool, err error) {
bkt := c.gcsClient.Bucket(c.bucket)

obj := bkt.Object(gcsPath)
r, err := obj.NewReader(ctx)
if err != nil {
if errors.Is(err, gcs.ErrObjectNotExist) {
// Return the generic NotExist error so that tileCache.Visit can differentiate
// between this and other errors.
return false, err
jiggoha marked this conversation as resolved.
Show resolved Hide resolved
}

klog.V(2).Infof("GetTile: failed to create reader for object %q in bucket %q: %v",
gcsPath, c.bucket, err)
return false, err
}
defer r.Close()

var gcsData []byte
gcsData, err = io.ReadAll(r)
jiggoha marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return false, err
}

if bytes.Equal(gcsData, data) {
return true, nil
}
return false, nil
}

// StoreTile writes a tile out to GCS.
// Fully populated tiles are stored at the path corresponding to the level &
// index parameters, partially populated (i.e. right-hand edge) tiles are
Expand All @@ -403,6 +435,20 @@ func (c *Client) StoreTile(ctx context.Context, level, index uint64, tile *api.T

// Pass an empty rootDir since we don't need this concept in GCS.
tPath := filepath.Join(layout.TilePath("", level, index, tileSize%256))

// Check that either we are writing the tile for the first time, or that the
jiggoha marked this conversation as resolved.
Show resolved Hide resolved
// write would be a no-op since the content is the same.
//
// gcs.ErrObjectNotExist is okay, we will just write the tile below.
if equal, err := c.assertContent(ctx, tPath, t); err != nil && !errors.Is(err, gcs.ErrObjectNotExist) {
return fmt.Errorf("failed to read content of %q: %w", tPath, err)
} else if err == nil && !equal {
return fmt.Errorf("assertion that tile content for %q has not changed failed", tPath)
} else if err == nil && equal {
klog.V(2).Infof("StoreTile: Tile already exists for level %d index %x ts: %x", level, index, tileSize)
return nil
}

obj := bkt.Object(tPath)

w := obj.NewWriter(ctx)
Expand Down
Loading