Skip to content

Commit b3711af

Browse files
nonsensegbalint
authored andcommitted
swarm: ctx propagation; bmt fixes; pss generic notification framework (#17150)
* cmd/swarm: minor cli flag text adjustments * swarm/api/http: sticky footer for swarm landing page using flex * swarm/api/http: sticky footer for error pages and fix for multiple choices * cmd/swarm, swarm/storage, swarm: fix mingw on windows test issues * cmd/swarm: update description of swarm cmd * swarm: added network ID test * cmd/swarm: support for smoke tests on the production swarm cluster * cmd/swarm/swarm-smoke: simplify cluster logic as per suggestion * swarm: propagate ctx to internal apis (bnb-chain#754) * swarm/metrics: collect disk measurements * swarm/bmt: fix io.Writer interface * Write now tolerates arbitrary variable buffers * added variable buffer tests * Write loop and finalise optimisation * refactor / rename * add tests for empty input * swarm/pss: (UPDATE) Generic notifications package (bnb-chain#744) swarm/pss: Generic package for creating pss notification svcs * swarm: Adding context to more functions * swarm/api: change colour of landing page in templates * swarm/api: change landing page to react to enter keypress
1 parent 30bdf81 commit b3711af

49 files changed

Lines changed: 1631 additions & 489 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cmd/swarm/fs_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

17+
// +build linux darwin freebsd
18+
1719
package main
1820

1921
import (

‎cmd/swarm/hash.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package main
1919

2020
import (
21+
"context"
2122
"fmt"
2223
"os"
2324

@@ -39,7 +40,7 @@ func hash(ctx *cli.Context) {
3940

4041
stat, _ := f.Stat()
4142
fileStore := storage.NewFileStore(storage.NewMapChunkStore(), storage.NewFileStoreParams())
42-
addr, _, err := fileStore.Store(f, stat.Size(), false)
43+
addr, _, err := fileStore.Store(context.TODO(), f, stat.Size(), false)
4344
if err != nil {
4445
utils.Fatalf("%v\n", err)
4546
} else {

‎cmd/swarm/main.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ var (
143143
}
144144
SwarmWantManifestFlag = cli.BoolTFlag{
145145
Name: "manifest",
146-
Usage: "Automatic manifest upload",
146+
Usage: "Automatic manifest upload (default true)",
147147
}
148148
SwarmUploadDefaultPath = cli.StringFlag{
149149
Name: "defaultpath",
@@ -155,7 +155,7 @@ var (
155155
}
156156
SwarmUploadMimeType = cli.StringFlag{
157157
Name: "mime",
158-
Usage: "force mime type",
158+
Usage: "Manually specify MIME type",
159159
}
160160
SwarmEncryptedFlag = cli.BoolFlag{
161161
Name: "encrypt",

‎cmd/swarm/swarm-smoke/upload_and_sync.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ import (
3737
)
3838

3939
func generateEndpoints(scheme string, cluster string, from int, to int) {
40+
if cluster == "prod" {
41+
cluster = ""
42+
} else {
43+
cluster = cluster + "."
44+
}
45+
4046
for port := from; port <= to; port++ {
41-
endpoints = append(endpoints, fmt.Sprintf("%s://%v.%s.swarm-gateways.net", scheme, port, cluster))
47+
endpoints = append(endpoints, fmt.Sprintf("%s://%v.%sswarm-gateways.net", scheme, port, cluster))
4248
}
4349

4450
if includeLocalhost {

‎metrics/metrics.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ func CollectProcessMetrics(refresh time.Duration) {
5858
memPauses := GetOrRegisterMeter("system/memory/pauses", DefaultRegistry)
5959

6060
var diskReads, diskReadBytes, diskWrites, diskWriteBytes Meter
61+
var diskReadBytesCounter, diskWriteBytesCounter Counter
6162
if err := ReadDiskStats(diskstats[0]); err == nil {
6263
diskReads = GetOrRegisterMeter("system/disk/readcount", DefaultRegistry)
6364
diskReadBytes = GetOrRegisterMeter("system/disk/readdata", DefaultRegistry)
65+
diskReadBytesCounter = GetOrRegisterCounter("system/disk/readbytes", DefaultRegistry)
6466
diskWrites = GetOrRegisterMeter("system/disk/writecount", DefaultRegistry)
6567
diskWriteBytes = GetOrRegisterMeter("system/disk/writedata", DefaultRegistry)
68+
diskWriteBytesCounter = GetOrRegisterCounter("system/disk/writebytes", DefaultRegistry)
6669
} else {
6770
log.Debug("Failed to read disk metrics", "err", err)
6871
}
@@ -82,6 +85,9 @@ func CollectProcessMetrics(refresh time.Duration) {
8285
diskReadBytes.Mark(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
8386
diskWrites.Mark(diskstats[location1].WriteCount - diskstats[location2].WriteCount)
8487
diskWriteBytes.Mark(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
88+
89+
diskReadBytesCounter.Inc(diskstats[location1].ReadBytes - diskstats[location2].ReadBytes)
90+
diskWriteBytesCounter.Inc(diskstats[location1].WriteBytes - diskstats[location2].WriteBytes)
8591
}
8692
time.Sleep(refresh)
8793
}

‎swarm/api/api.go‎

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -227,28 +227,28 @@ func NewAPI(fileStore *storage.FileStore, dns Resolver, resourceHandler *mru.Han
227227
}
228228

229229
// Upload to be used only in TEST
230-
func (a *API) Upload(uploadDir, index string, toEncrypt bool) (hash string, err error) {
230+
func (a *API) Upload(ctx context.Context, uploadDir, index string, toEncrypt bool) (hash string, err error) {
231231
fs := NewFileSystem(a)
232232
hash, err = fs.Upload(uploadDir, index, toEncrypt)
233233
return hash, err
234234
}
235235

236236
// Retrieve FileStore reader API
237-
func (a *API) Retrieve(addr storage.Address) (reader storage.LazySectionReader, isEncrypted bool) {
238-
return a.fileStore.Retrieve(addr)
237+
func (a *API) Retrieve(ctx context.Context, addr storage.Address) (reader storage.LazySectionReader, isEncrypted bool) {
238+
return a.fileStore.Retrieve(ctx, addr)
239239
}
240240

241241
// Store wraps the Store API call of the embedded FileStore
242-
func (a *API) Store(data io.Reader, size int64, toEncrypt bool) (addr storage.Address, wait func(), err error) {
242+
func (a *API) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr storage.Address, wait func(ctx context.Context) error, err error) {
243243
log.Debug("api.store", "size", size)
244-
return a.fileStore.Store(data, size, toEncrypt)
244+
return a.fileStore.Store(ctx, data, size, toEncrypt)
245245
}
246246

247247
// ErrResolve is returned when an URI cannot be resolved from ENS.
248248
type ErrResolve error
249249

250250
// Resolve resolves a URI to an Address using the MultiResolver.
251-
func (a *API) Resolve(uri *URI) (storage.Address, error) {
251+
func (a *API) Resolve(ctx context.Context, uri *URI) (storage.Address, error) {
252252
apiResolveCount.Inc(1)
253253
log.Trace("resolving", "uri", uri.Addr)
254254

@@ -286,34 +286,37 @@ func (a *API) Resolve(uri *URI) (storage.Address, error) {
286286
}
287287

288288
// Put provides singleton manifest creation on top of FileStore store
289-
func (a *API) Put(content, contentType string, toEncrypt bool) (k storage.Address, wait func(), err error) {
289+
func (a *API) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (k storage.Address, wait func(context.Context) error, err error) {
290290
apiPutCount.Inc(1)
291291
r := strings.NewReader(content)
292-
key, waitContent, err := a.fileStore.Store(r, int64(len(content)), toEncrypt)
292+
key, waitContent, err := a.fileStore.Store(ctx, r, int64(len(content)), toEncrypt)
293293
if err != nil {
294294
apiPutFail.Inc(1)
295295
return nil, nil, err
296296
}
297297
manifest := fmt.Sprintf(`{"entries":[{"hash":"%v","contentType":"%s"}]}`, key, contentType)
298298
r = strings.NewReader(manifest)
299-
key, waitManifest, err := a.fileStore.Store(r, int64(len(manifest)), toEncrypt)
299+
key, waitManifest, err := a.fileStore.Store(ctx, r, int64(len(manifest)), toEncrypt)
300300
if err != nil {
301301
apiPutFail.Inc(1)
302302
return nil, nil, err
303303
}
304-
return key, func() {
305-
waitContent()
306-
waitManifest()
304+
return key, func(ctx context.Context) error {
305+
err := waitContent(ctx)
306+
if err != nil {
307+
return err
308+
}
309+
return waitManifest(ctx)
307310
}, nil
308311
}
309312

310313
// Get uses iterative manifest retrieval and prefix matching
311314
// to resolve basePath to content using FileStore retrieve
312315
// it returns a section reader, mimeType, status, the key of the actual content and an error
313-
func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.LazySectionReader, mimeType string, status int, contentAddr storage.Address, err error) {
316+
func (a *API) Get(ctx context.Context, manifestAddr storage.Address, path string) (reader storage.LazySectionReader, mimeType string, status int, contentAddr storage.Address, err error) {
314317
log.Debug("api.get", "key", manifestAddr, "path", path)
315318
apiGetCount.Inc(1)
316-
trie, err := loadManifest(a.fileStore, manifestAddr, nil)
319+
trie, err := loadManifest(ctx, a.fileStore, manifestAddr, nil)
317320
if err != nil {
318321
apiGetNotFound.Inc(1)
319322
status = http.StatusNotFound
@@ -375,7 +378,7 @@ func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.Laz
375378
log.Trace("resource is multihash", "key", manifestAddr)
376379

377380
// get the manifest the multihash digest points to
378-
trie, err := loadManifest(a.fileStore, manifestAddr, nil)
381+
trie, err := loadManifest(ctx, a.fileStore, manifestAddr, nil)
379382
if err != nil {
380383
apiGetNotFound.Inc(1)
381384
status = http.StatusNotFound
@@ -410,7 +413,7 @@ func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.Laz
410413
}
411414
mimeType = entry.ContentType
412415
log.Debug("content lookup key", "key", contentAddr, "mimetype", mimeType)
413-
reader, _ = a.fileStore.Retrieve(contentAddr)
416+
reader, _ = a.fileStore.Retrieve(ctx, contentAddr)
414417
} else {
415418
// no entry found
416419
status = http.StatusNotFound
@@ -422,10 +425,10 @@ func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.Laz
422425
}
423426

424427
// Modify loads manifest and checks the content hash before recalculating and storing the manifest.
425-
func (a *API) Modify(addr storage.Address, path, contentHash, contentType string) (storage.Address, error) {
428+
func (a *API) Modify(ctx context.Context, addr storage.Address, path, contentHash, contentType string) (storage.Address, error) {
426429
apiModifyCount.Inc(1)
427430
quitC := make(chan bool)
428-
trie, err := loadManifest(a.fileStore, addr, quitC)
431+
trie, err := loadManifest(ctx, a.fileStore, addr, quitC)
429432
if err != nil {
430433
apiModifyFail.Inc(1)
431434
return nil, err
@@ -449,15 +452,15 @@ func (a *API) Modify(addr storage.Address, path, contentHash, contentType string
449452
}
450453

451454
// AddFile creates a new manifest entry, adds it to swarm, then adds a file to swarm.
452-
func (a *API) AddFile(mhash, path, fname string, content []byte, nameresolver bool) (storage.Address, string, error) {
455+
func (a *API) AddFile(ctx context.Context, mhash, path, fname string, content []byte, nameresolver bool) (storage.Address, string, error) {
453456
apiAddFileCount.Inc(1)
454457

455458
uri, err := Parse("bzz:/" + mhash)
456459
if err != nil {
457460
apiAddFileFail.Inc(1)
458461
return nil, "", err
459462
}
460-
mkey, err := a.Resolve(uri)
463+
mkey, err := a.Resolve(ctx, uri)
461464
if err != nil {
462465
apiAddFileFail.Inc(1)
463466
return nil, "", err
@@ -476,13 +479,13 @@ func (a *API) AddFile(mhash, path, fname string, content []byte, nameresolver bo
476479
ModTime: time.Now(),
477480
}
478481

479-
mw, err := a.NewManifestWriter(mkey, nil)
482+
mw, err := a.NewManifestWriter(ctx, mkey, nil)
480483
if err != nil {
481484
apiAddFileFail.Inc(1)
482485
return nil, "", err
483486
}
484487

485-
fkey, err := mw.AddEntry(bytes.NewReader(content), entry)
488+
fkey, err := mw.AddEntry(ctx, bytes.NewReader(content), entry)
486489
if err != nil {
487490
apiAddFileFail.Inc(1)
488491
return nil, "", err
@@ -496,19 +499,18 @@ func (a *API) AddFile(mhash, path, fname string, content []byte, nameresolver bo
496499
}
497500

498501
return fkey, newMkey.String(), nil
499-
500502
}
501503

502504
// RemoveFile removes a file entry in a manifest.
503-
func (a *API) RemoveFile(mhash, path, fname string, nameresolver bool) (string, error) {
505+
func (a *API) RemoveFile(ctx context.Context, mhash string, path string, fname string, nameresolver bool) (string, error) {
504506
apiRmFileCount.Inc(1)
505507

506508
uri, err := Parse("bzz:/" + mhash)
507509
if err != nil {
508510
apiRmFileFail.Inc(1)
509511
return "", err
510512
}
511-
mkey, err := a.Resolve(uri)
513+
mkey, err := a.Resolve(ctx, uri)
512514
if err != nil {
513515
apiRmFileFail.Inc(1)
514516
return "", err
@@ -519,7 +521,7 @@ func (a *API) RemoveFile(mhash, path, fname string, nameresolver bool) (string,
519521
path = path[1:]
520522
}
521523

522-
mw, err := a.NewManifestWriter(mkey, nil)
524+
mw, err := a.NewManifestWriter(ctx, mkey, nil)
523525
if err != nil {
524526
apiRmFileFail.Inc(1)
525527
return "", err
@@ -542,7 +544,7 @@ func (a *API) RemoveFile(mhash, path, fname string, nameresolver bool) (string,
542544
}
543545

544546
// AppendFile removes old manifest, appends file entry to new manifest and adds it to Swarm.
545-
func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content []byte, oldAddr storage.Address, offset int64, addSize int64, nameresolver bool) (storage.Address, string, error) {
547+
func (a *API) AppendFile(ctx context.Context, mhash, path, fname string, existingSize int64, content []byte, oldAddr storage.Address, offset int64, addSize int64, nameresolver bool) (storage.Address, string, error) {
546548
apiAppendFileCount.Inc(1)
547549

548550
buffSize := offset + addSize
@@ -552,7 +554,7 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
552554

553555
buf := make([]byte, buffSize)
554556

555-
oldReader, _ := a.Retrieve(oldAddr)
557+
oldReader, _ := a.Retrieve(ctx, oldAddr)
556558
io.ReadAtLeast(oldReader, buf, int(offset))
557559

558560
newReader := bytes.NewReader(content)
@@ -575,7 +577,7 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
575577
apiAppendFileFail.Inc(1)
576578
return nil, "", err
577579
}
578-
mkey, err := a.Resolve(uri)
580+
mkey, err := a.Resolve(ctx, uri)
579581
if err != nil {
580582
apiAppendFileFail.Inc(1)
581583
return nil, "", err
@@ -586,7 +588,7 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
586588
path = path[1:]
587589
}
588590

589-
mw, err := a.NewManifestWriter(mkey, nil)
591+
mw, err := a.NewManifestWriter(ctx, mkey, nil)
590592
if err != nil {
591593
apiAppendFileFail.Inc(1)
592594
return nil, "", err
@@ -606,7 +608,7 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
606608
ModTime: time.Now(),
607609
}
608610

609-
fkey, err := mw.AddEntry(io.Reader(combinedReader), entry)
611+
fkey, err := mw.AddEntry(ctx, io.Reader(combinedReader), entry)
610612
if err != nil {
611613
apiAppendFileFail.Inc(1)
612614
return nil, "", err
@@ -620,23 +622,22 @@ func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content
620622
}
621623

622624
return fkey, newMkey.String(), nil
623-
624625
}
625626

626627
// BuildDirectoryTree used by swarmfs_unix
627-
func (a *API) BuildDirectoryTree(mhash string, nameresolver bool) (addr storage.Address, manifestEntryMap map[string]*manifestTrieEntry, err error) {
628+
func (a *API) BuildDirectoryTree(ctx context.Context, mhash string, nameresolver bool) (addr storage.Address, manifestEntryMap map[string]*manifestTrieEntry, err error) {
628629

629630
uri, err := Parse("bzz:/" + mhash)
630631
if err != nil {
631632
return nil, nil, err
632633
}
633-
addr, err = a.Resolve(uri)
634+
addr, err = a.Resolve(ctx, uri)
634635
if err != nil {
635636
return nil, nil, err
636637
}
637638

638639
quitC := make(chan bool)
639-
rootTrie, err := loadManifest(a.fileStore, addr, quitC)
640+
rootTrie, err := loadManifest(ctx, a.fileStore, addr, quitC)
640641
if err != nil {
641642
return nil, nil, fmt.Errorf("can't load manifest %v: %v", addr.String(), err)
642643
}
@@ -725,8 +726,8 @@ func (a *API) ResourceIsValidated() bool {
725726
}
726727

727728
// ResolveResourceManifest retrieves the Mutable Resource manifest for the given address, and returns the address of the metadata chunk.
728-
func (a *API) ResolveResourceManifest(addr storage.Address) (storage.Address, error) {
729-
trie, err := loadManifest(a.fileStore, addr, nil)
729+
func (a *API) ResolveResourceManifest(ctx context.Context, addr storage.Address) (storage.Address, error) {
730+
trie, err := loadManifest(ctx, a.fileStore, addr, nil)
730731
if err != nil {
731732
return nil, fmt.Errorf("cannot load resource manifest: %v", err)
732733
}

‎swarm/api/api_test.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func expResponse(content string, mimeType string, status int) *Response {
8585

8686
func testGet(t *testing.T, api *API, bzzhash, path string) *testResponse {
8787
addr := storage.Address(common.Hex2Bytes(bzzhash))
88-
reader, mimeType, status, _, err := api.Get(addr, path)
88+
reader, mimeType, status, _, err := api.Get(context.TODO(), addr, path)
8989
if err != nil {
9090
t.Fatalf("unexpected error: %v", err)
9191
}
@@ -109,12 +109,15 @@ func TestApiPut(t *testing.T) {
109109
testAPI(t, func(api *API, toEncrypt bool) {
110110
content := "hello"
111111
exp := expResponse(content, "text/plain", 0)
112-
// exp := expResponse([]byte(content), "text/plain", 0)
113-
addr, wait, err := api.Put(content, exp.MimeType, toEncrypt)
112+
ctx := context.TODO()
113+
addr, wait, err := api.Put(ctx, content, exp.MimeType, toEncrypt)
114+
if err != nil {
115+
t.Fatalf("unexpected error: %v", err)
116+
}
117+
err = wait(ctx)
114118
if err != nil {
115119
t.Fatalf("unexpected error: %v", err)
116120
}
117-
wait()
118121
resp := testGet(t, api, addr.Hex(), "")
119122
checkResponse(t, resp, exp)
120123
})
@@ -226,7 +229,7 @@ func TestAPIResolve(t *testing.T) {
226229
if x.immutable {
227230
uri.Scheme = "bzz-immutable"
228231
}
229-
res, err := api.Resolve(uri)
232+
res, err := api.Resolve(context.TODO(), uri)
230233
if err == nil {
231234
if x.expectErr != nil {
232235
t.Fatalf("expected error %q, got result %q", x.expectErr, res)

0 commit comments

Comments
 (0)