-
Notifications
You must be signed in to change notification settings - Fork 389
Local pinning fix #255
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
Local pinning fix #255
Changes from 4 commits
e39afef
7edb3d6
3f502f4
9fb5ad3
8392a05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ import ( | |
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "math/rand" | ||
| "os" | ||
|
|
@@ -534,3 +535,193 @@ func TestSetTestHookCollectGarbage(t *testing.T) { | |
| t.Errorf("got hook value %v, want %v", got, original) | ||
| } | ||
| } | ||
|
|
||
| func TestPinAfterMultiGC(t *testing.T) { | ||
| db := newTestDB(t, &Options{ | ||
| Capacity: 100, | ||
| }) | ||
|
|
||
| pinnedChunks := make([]swarm.Address, 0) | ||
|
|
||
| // upload random chunks above db capacity to see if chunks are still pinned | ||
| for i := 0; i < 200; i++ { | ||
| ch := generateTestRandomChunk() | ||
| _, err := db.Put(context.Background(), storage.ModePutUpload, ch) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetSyncPull, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| if len(pinnedChunks) < 10 { | ||
| rch := generateAndPinAChunk(t, db) | ||
| pinnedChunks = append(pinnedChunks, rch.Address()) | ||
| } | ||
| } | ||
| for i := 0; i < 200; i++ { | ||
| ch := generateTestRandomChunk() | ||
| _, err := db.Put(context.Background(), storage.ModePutUpload, ch) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetSyncPull, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| for i := 0; i < 200; i++ { | ||
| ch := generateTestRandomChunk() | ||
| _, err := db.Put(context.Background(), storage.ModePutUpload, ch) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetSyncPull, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| t.Run("pin Index count", newItemsCountTest(db.pinIndex, len(pinnedChunks))) | ||
|
|
||
| // Check if all the pinned chunks are present in the data DB | ||
| for _, addr := range pinnedChunks { | ||
| outItem := shed.Item{ | ||
| Address: addr.Bytes(), | ||
| } | ||
| gotChunk, err := db.Get(context.Background(), storage.ModeGetRequest, swarm.NewAddress(outItem.Address)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !gotChunk.Address().Equal(swarm.NewAddress(addr.Bytes())) { | ||
| t.Fatal("Pinned chunk is not equal to got chunk") | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func generateAndPinAChunk(t *testing.T, db *DB) swarm.Chunk { | ||
| // Create a chunk and pin it | ||
| pinnedChunk := generateTestRandomChunk() | ||
|
|
||
| _, err := db.Put(context.Background(), storage.ModePutUpload, pinnedChunk) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetPin, pinnedChunk.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetSyncPull, pinnedChunk.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return pinnedChunk | ||
| } | ||
|
|
||
| func TestPinSyncAndAccessPutSetChunkMultipleTimes(t *testing.T) { | ||
| var closed chan struct{} | ||
| testHookCollectGarbageChan := make(chan uint64) | ||
| t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) { | ||
| select { | ||
| case testHookCollectGarbageChan <- collectedCount: | ||
| case <-closed: | ||
| } | ||
| })) | ||
| db := newTestDB(t, &Options{ | ||
| Capacity: 10, | ||
| }) | ||
| closed = db.close | ||
|
|
||
| rch := generateTestRandomChunk() | ||
| for i := 0; i < 20; i++ { | ||
| ch := generateTestRandomChunk() | ||
|
|
||
| // pin a chunk in between | ||
| if i == 5 { | ||
| _, err := db.Put(context.Background(), storage.ModePutUpload, rch) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetPin, rch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| _, err := db.Put(context.Background(), storage.ModePutUpload, ch) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetSyncPull, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| err = db.Set(context.Background(), storage.ModeSetAccess, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _, err = db.Get(context.Background(), storage.ModeGetRequest, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| _, err := db.Get(context.Background(), storage.ModeGetRequest, rch.Address()) | ||
| if err != nil { | ||
| fmt.Println("Pinned chunk missing ", rch.Address()) | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| addRandomChunks(t, 20, db) | ||
|
|
||
| _, err = db.Get(context.Background(), storage.ModeGetRequest, rch.Address()) | ||
| if err != nil { | ||
| fmt.Println("Pinned chunk missing ", rch.Address()) | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| addRandomChunks(t, 20, db) | ||
|
|
||
| _, err = db.Get(context.Background(), storage.ModeGetRequest, rch.Address()) | ||
| if err != nil { | ||
| fmt.Println("Pinned chunk missing ", rch.Address()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again and again we ask to remove
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops... my bad.. will remove it |
||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // check if the pinned chunk is present after GC | ||
| gotChunk, err := db.Get(context.Background(), storage.ModeGetRequest, rch.Address()) | ||
| if err != nil { | ||
| t.Fatal("Pinned chunk missing ", err) | ||
| } | ||
| if !gotChunk.Address().Equal(rch.Address()) { | ||
| t.Fatal("Pinned chunk is not equal to got chunk") | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func addRandomChunks(t *testing.T, count int, db *DB) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why don't you use this in all of the tests above?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because other tests dont need this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm seeing a lot of Puts and Sets above this, if this test helper would have a few parameters, it could save you all of the manual Puts and Sets |
||
| for i := 0; i < count; i++ { | ||
| ch := generateTestRandomChunk() | ||
| _, err := db.Put(context.Background(), storage.ModePutUpload, ch) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetSyncPull, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| err = db.Set(context.Background(), storage.ModeSetAccess, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| _, err = db.Get(context.Background(), storage.ModeGetRequest, ch.Address()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.