Skip to content

Commit c9136c0

Browse files
author
Eric Curtin
committed
objectstorage: register compose intermediates early, test via httptest
Signed-off-by: Eric Curtin <[email protected]>
1 parent cc561e9 commit c9136c0

3 files changed

Lines changed: 84 additions & 40 deletions

File tree

‎pkg/objectstorage/gcs_retry_test.go‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ import (
2525
"testing"
2626
)
2727

28+
// fakeGCSClient returns a production-configured client bound to srv.
29+
func fakeGCSClient(t *testing.T, srv *httptest.Server) *gcsClient {
30+
t.Helper()
31+
t.Setenv("STORAGE_EMULATOR_HOST", srv.URL)
32+
store, err := NewGCSClient(context.Background())
33+
if err != nil {
34+
t.Fatalf("storage client: %v", err)
35+
}
36+
g := store.(*gcsClient)
37+
t.Cleanup(func() { g.client.Close() })
38+
return g
39+
}
40+
2841
// A transient 429 on an upload must be retried, not surfaced: GCS sheds write
2942
// bursts with rateLimitExceeded while it scales a bucket's key ranges, and the
3043
// default client policy (RetryIdempotent) would fail the whole snapshot on the
@@ -68,14 +81,8 @@ func TestPutObjectRetriesTransient429(t *testing.T) {
6881
defer srv.Close()
6982
srvURL = srv.URL
7083

71-
// The storage client routes everything, uploads included, at the emulator.
72-
t.Setenv("STORAGE_EMULATOR_HOST", srv.URL)
84+
store := fakeGCSClient(t, srv)
7385
ctx := context.Background()
74-
store, err := NewGCSClient(ctx)
75-
if err != nil {
76-
t.Fatalf("storage client: %v", err)
77-
}
78-
defer store.(*gcsClient).client.Close()
7986

8087
if err := store.PutObject(ctx, "snapshots", "snap/pages.img.zstd", strings.NewReader("payload")); err != nil {
8188
t.Fatalf("PutObject after a transient 429: %v", err)

‎pkg/objectstorage/gcscompose.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,11 @@ func composeAll(ctx context.Context, bkt *storage.BucketHandle, object string, p
182182
continue
183183
}
184184
mid := bkt.Object(fmt.Sprintf("%s.compose-%s-%d-%04d", object, runID, round, i))
185+
// Before Run: it can fail after GCS committed mid.
186+
mids = append(mids, mid)
185187
if _, err := mid.ComposerFrom(group...).Run(ctx); err != nil {
186188
return fmt.Errorf("while composing parts of %q: %w", object, err)
187189
}
188-
mids = append(mids, mid)
189190
next = append(next, mid)
190191
}
191192
parts = next

‎pkg/objectstorage/gcscompose_test.go‎

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ import (
1818
"bytes"
1919
"context"
2020
"crypto/sha256"
21+
"encoding/json"
2122
"fmt"
2223
"io"
2324
"math/rand/v2"
25+
"net/http"
26+
"net/http/httptest"
27+
"net/url"
2428
"os"
2529
"strings"
30+
"sync"
2631
"testing"
2732

2833
"cloud.google.com/go/storage"
@@ -123,52 +128,83 @@ func TestPutObjectCompositeRoundTrip(t *testing.T) {
123128
}
124129
}
125130

131+
// fakeObjects is an in-memory GCS that serves only what composeAll sends: compose
132+
// (concatenation, capped at maxComposeSources like GCS) and delete.
133+
type fakeObjects struct {
134+
mu sync.Mutex
135+
objs map[string][]byte
136+
}
137+
138+
func (f *fakeObjects) ServeHTTP(w http.ResponseWriter, r *http.Request) {
139+
rest, ok := strings.CutPrefix(r.URL.EscapedPath(), "/storage/v1/b/test/o/")
140+
if !ok {
141+
http.NotFound(w, r)
142+
return
143+
}
144+
f.mu.Lock()
145+
defer f.mu.Unlock()
146+
switch {
147+
case r.Method == http.MethodPost && strings.HasSuffix(rest, "/compose"):
148+
name, _ := url.PathUnescape(strings.TrimSuffix(rest, "/compose"))
149+
var req struct {
150+
SourceObjects []struct{ Name string } `json:"sourceObjects"`
151+
}
152+
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || len(req.SourceObjects) > maxComposeSources {
153+
http.Error(w, "bad compose", http.StatusBadRequest)
154+
return
155+
}
156+
var data []byte
157+
for _, src := range req.SourceObjects {
158+
b, ok := f.objs[src.Name]
159+
if !ok {
160+
http.NotFound(w, r)
161+
return
162+
}
163+
data = append(data, b...)
164+
}
165+
f.objs[name] = data
166+
w.Header().Set("Content-Type", "application/json")
167+
_ = json.NewEncoder(w).Encode(map[string]string{"name": name, "bucket": "test"})
168+
case r.Method == http.MethodDelete:
169+
name, _ := url.PathUnescape(rest)
170+
if _, ok := f.objs[name]; !ok {
171+
http.NotFound(w, r)
172+
return
173+
}
174+
delete(f.objs, name)
175+
w.WriteHeader(http.StatusNoContent)
176+
default:
177+
http.NotFound(w, r)
178+
}
179+
}
180+
126181
// Folding more than maxComposeSources parts goes through intermediate objects, and
127182
// none may outlive the compose, including the ones the final compose consumes.
128183
func TestComposeAllRemovesIntermediates(t *testing.T) {
129-
client, bucket := emulatorClient(t)
130-
ctx := context.Background()
131-
bkt := client.Bucket(bucket)
132-
133184
const partSize = 1 << 10
134185
want := body((maxComposeSources + 8) * partSize)
186+
fake := &fakeObjects{objs: map[string][]byte{}}
187+
srv := httptest.NewServer(fake)
188+
defer srv.Close()
189+
bkt := fakeGCSClient(t, srv).client.Bucket("test")
190+
135191
var parts []*storage.ObjectHandle
136192
for i := 0; i < len(want); i += partSize {
137-
p := bkt.Object(fmt.Sprintf("fold/obj.part-run-%04d", i/partSize))
138-
if err := writeObject(ctx, p, want[i:i+partSize], partSize); err != nil {
139-
t.Fatalf("writing part: %v", err)
140-
}
141-
parts = append(parts, p)
193+
name := fmt.Sprintf("fold/obj.part-run-%04d", i/partSize)
194+
fake.objs[name] = want[i : i+partSize]
195+
parts = append(parts, bkt.Object(name))
142196
}
143197

144-
if err := composeAll(ctx, bkt, "fold/obj", parts, "run"); err != nil {
198+
if err := composeAll(context.Background(), bkt, "fold/obj", parts, "run"); err != nil {
145199
t.Fatalf("composeAll: %v", err)
146200
}
147201

148-
rc, err := bkt.Object("fold/obj").NewReader(ctx)
149-
if err != nil {
150-
t.Fatalf("reading back: %v", err)
151-
}
152-
got, err := io.ReadAll(rc)
153-
rc.Close()
154-
if err != nil {
155-
t.Fatalf("reading back: %v", err)
156-
}
157-
if !bytes.Equal(got, want) {
202+
if got := fake.objs["fold/obj"]; !bytes.Equal(got, want) {
158203
t.Errorf("composed %d bytes differ from the %d uploaded", len(got), len(want))
159204
}
160-
161-
it := bkt.Objects(ctx, &storage.Query{Prefix: "fold/"})
162-
for {
163-
attrs, err := it.Next()
164-
if err == iterator.Done {
165-
break
166-
}
167-
if err != nil {
168-
t.Fatalf("listing: %v", err)
169-
}
170-
if strings.Contains(attrs.Name, ".compose-") {
171-
t.Errorf("composeAll left intermediate %q behind", attrs.Name)
205+
for name := range fake.objs {
206+
if strings.Contains(name, ".compose-") {
207+
t.Errorf("composeAll left intermediate %q behind", name)
172208
}
173209
}
174210
}

0 commit comments

Comments
 (0)