This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Tests for unsharding PR #99
Merged
aschmahmann
merged 22 commits into
schomatis/directory/unsharding
from
schomatis/directory/unsharding-tests
Oct 22, 2021
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
d60be37
unsharding tests
schomatis c5e0a00
go fmt
schomatis 2713c85
go static check
schomatis 8a80e74
TestProductionLinkSize
schomatis 94a6082
fix dep listing
schomatis 2f9afe9
TestProductionLinkSize: fix context
schomatis cdcb270
Concurrent walk test (#106)
schomatis 2c49dc3
fixmes
schomatis 8176abf
reduce use of DefaultShardWidth to production
schomatis 9795f4e
rework TestHAMTEnumerationWhenComputingSize documentation
schomatis 805db3b
clean up dead code
schomatis 32ccacd
go fmt
schomatis 87d3898
create internal and private packages (#108)
schomatis dc7e405
comments
schomatis ba5e036
fix TestHAMTEnumerationWhenComputingSize numbers
schomatis ed5213a
unexport shard.IsValueNode and remove shard.EnumAll
aschmahmann 860cb96
move utilities for directory testing into the test file
aschmahmann 0e831ed
remove EnumAllAsync
aschmahmann ba2d000
revert switch to sharding to use default
schomatis f7a8e99
remove leftover code
schomatis 20d45d6
remove commented out code
aschmahmann 64502b0
move completehamt code into testing
aschmahmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,18 +25,24 @@ import ( | |
"fmt" | ||
"os" | ||
|
||
format "github.com/ipfs/go-unixfs" | ||
"github.com/ipfs/go-unixfs/internal" | ||
|
||
bitfield "github.com/ipfs/go-bitfield" | ||
cid "github.com/ipfs/go-cid" | ||
ipld "github.com/ipfs/go-ipld-format" | ||
dag "github.com/ipfs/go-merkledag" | ||
format "github.com/ipfs/go-unixfs" | ||
) | ||
|
||
const ( | ||
// HashMurmur3 is the multiformats identifier for Murmur3 | ||
HashMurmur3 uint64 = 0x22 | ||
) | ||
|
||
func init() { | ||
internal.HAMTHashFunction = murmur3Hash | ||
} | ||
|
||
func (ds *Shard) isValueNode() bool { | ||
return ds.key != "" && ds.val != nil | ||
} | ||
|
@@ -45,17 +51,29 @@ func (ds *Shard) isValueNode() bool { | |
type Shard struct { | ||
childer *childer | ||
|
||
tableSize int | ||
// Entries per node (number of possible childs indexed by the partial key). | ||
tableSize int | ||
// Bits needed to encode child indexes (log2 of number of entries). This is | ||
// the number of bits taken from the hash key on each level of the tree. | ||
tableSizeLg2 int | ||
|
||
builder cid.Builder | ||
hashFunc uint64 | ||
|
||
// String format with number of zeros that will be present in the hexadecimal | ||
// encoding of the child index to always reach the fixed maxpadlen chars. | ||
// Example: maxpadlen = 4 => prefixPadStr: "%04X" (print number in hexadecimal | ||
// format padding with zeros to always reach 4 characters). | ||
prefixPadStr string | ||
maxpadlen int | ||
// Length in chars of string that encodes child indexes. We encode indexes | ||
// as hexadecimal strings to this is log4 of number of entries. | ||
maxpadlen int | ||
|
||
dserv ipld.DAGService | ||
|
||
// FIXME: Remove. We don't actually store "value nodes". This confusing | ||
// abstraction just removes the maxpadlen from the link names to extract | ||
// the actual value link the trie is storing. | ||
Comment on lines
+74
to
+76
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. My understanding of your comment here is that Also, can you clarify if this a FIXME intended for this set of PRs? |
||
// leaf node | ||
key string | ||
val *ipld.Link | ||
|
@@ -68,12 +86,13 @@ func NewShard(dserv ipld.DAGService, size int) (*Shard, error) { | |
return nil, err | ||
} | ||
|
||
// FIXME: Make this at least a static configuration for testing. | ||
ds.hashFunc = HashMurmur3 | ||
return ds, nil | ||
} | ||
|
||
func makeShard(ds ipld.DAGService, size int) (*Shard, error) { | ||
lg2s, err := logtwo(size) | ||
lg2s, err := Logtwo(size) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -211,7 +230,7 @@ func (ds *Shard) Set(ctx context.Context, name string, nd ipld.Node) error { | |
// name key in this Shard or its children. It also returns the previous link | ||
// under that name key (if any). | ||
func (ds *Shard) SetAndPrevious(ctx context.Context, name string, node ipld.Node) (*ipld.Link, error) { | ||
hv := &hashBits{b: hash([]byte(name))} | ||
hv := newHashBits(name) | ||
err := ds.dserv.Add(ctx, node) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -221,6 +240,9 @@ func (ds *Shard) SetAndPrevious(ctx context.Context, name string, node ipld.Node | |
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// FIXME: We don't need to set the name here, it will get overwritten. | ||
// This is confusing, confirm and remove this line. | ||
lnk.Name = ds.linkNamePrefix(0) + name | ||
|
||
return ds.setValue(ctx, hv, name, lnk) | ||
|
@@ -236,13 +258,13 @@ func (ds *Shard) Remove(ctx context.Context, name string) error { | |
// RemoveAndPrevious is similar to the public Remove but also returns the | ||
// old removed link (if it exists). | ||
func (ds *Shard) RemoveAndPrevious(ctx context.Context, name string) (*ipld.Link, error) { | ||
hv := &hashBits{b: hash([]byte(name))} | ||
hv := newHashBits(name) | ||
return ds.setValue(ctx, hv, name, nil) | ||
} | ||
|
||
// Find searches for a child node by 'name' within this hamt | ||
func (ds *Shard) Find(ctx context.Context, name string) (*ipld.Link, error) { | ||
hv := &hashBits{b: hash([]byte(name))} | ||
hv := newHashBits(name) | ||
|
||
var out *ipld.Link | ||
err := ds.getValue(ctx, hv, name, func(sv *Shard) error { | ||
|
@@ -489,10 +511,7 @@ func (ds *Shard) setValue(ctx context.Context, hv *hashBits, key string, value * | |
return nil, err | ||
} | ||
child.builder = ds.builder | ||
chhv := &hashBits{ | ||
b: hash([]byte(grandChild.key)), | ||
consumed: hv.consumed, | ||
} | ||
chhv := newConsumedHashBits(grandChild.key, hv.consumed) | ||
|
||
// We explicitly ignore the oldValue returned by the next two insertions | ||
// (which will be nil) to highlight there is no overwrite here: they are | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package internal | ||
|
||
var HAMTHashFunction func(val []byte) []byte |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package io | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"fmt" | ||
"math" | ||
"testing" | ||
|
||
mdtest "github.com/ipfs/go-merkledag/test" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/ipfs/go-unixfs" | ||
"github.com/ipfs/go-unixfs/hamt" | ||
|
||
ipld "github.com/ipfs/go-ipld-format" | ||
) | ||
|
||
// CreateCompleteHAMT creates a HAMT the following properties: | ||
// * its height (distance/edges from root to deepest node) is specified by treeHeight. | ||
// * all leaf Shard nodes have the same depth (and have only 'value' links). | ||
// * all internal Shard nodes point only to other Shards (and hence have zero 'value' links). | ||
// * the total number of 'value' links (directory entries) is: | ||
// io.DefaultShardWidth ^ (treeHeight + 1). | ||
// FIXME: HAMTHashFunction needs to be set to idHash by the caller. We depend on | ||
// this simplification for the current logic to work. (HAMTHashFunction is a | ||
// global setting of the package, it is hard-coded in the serialized Shard node | ||
// and not allowed to be changed on a per HAMT/Shard basis.) | ||
// (If we didn't rehash inside setValue then we could just generate | ||
// the fake hash as in io.SetAndPrevious through `newHashBits()` and pass | ||
// it as an argument making the hash independent of tree manipulation; that | ||
// sounds as the correct way to go in general and we wouldn't need this.) | ||
func CreateCompleteHAMT(ds ipld.DAGService, treeHeight int, childsPerNode int) (ipld.Node, error) { | ||
if treeHeight < 1 { | ||
panic("treeHeight < 1") | ||
} | ||
if treeHeight > 8 { | ||
panic("treeHeight > 8: we don't allow a key larger than what can be encoded in a 64-bit word") | ||
} | ||
|
||
rootShard, err := hamt.NewShard(ds, childsPerNode) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Assuming we are using the ID hash function we can just insert all | ||
// the combinations of a byte slice that will reach the desired height. | ||
totalChildren := int(math.Pow(float64(childsPerNode), float64(treeHeight))) | ||
log2ofChilds, err := hamt.Logtwo(childsPerNode) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if log2ofChilds*treeHeight%8 != 0 { | ||
return nil, fmt.Errorf("childsPerNode * treeHeight should be multiple of 8") | ||
} | ||
bytesInKey := log2ofChilds * treeHeight / 8 | ||
for i := 0; i < totalChildren; i++ { | ||
var hashbuf [8]byte | ||
binary.LittleEndian.PutUint64(hashbuf[:], uint64(i)) | ||
var oldLink *ipld.Link | ||
oldLink, err = rootShard.SetAndPrevious(context.Background(), string(hashbuf[:bytesInKey]), unixfs.EmptyFileNode()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if oldLink != nil { | ||
// We shouldn't be overwriting any value, otherwise the tree | ||
// won't be complete. | ||
return nil, fmt.Errorf("we have overwritten entry %s", | ||
oldLink.Cid) | ||
} | ||
} | ||
|
||
return rootShard.Node() | ||
} | ||
|
||
// Return the same value as the hash. | ||
func idHash(val []byte) []byte { | ||
return val | ||
} | ||
|
||
func TestCreateCompleteShard(t *testing.T) { | ||
ds := mdtest.Mock() | ||
childsPerNode := 16 | ||
treeHeight := 2 | ||
node, err := CreateCompleteHAMT(ds, treeHeight, childsPerNode) | ||
assert.NoError(t, err) | ||
|
||
shard, err := hamt.NewHamtFromDag(ds, node) | ||
assert.NoError(t, err) | ||
links, err := shard.EnumLinks(context.Background()) | ||
assert.NoError(t, err) | ||
|
||
childNodes := int(math.Pow(float64(childsPerNode), float64(treeHeight))) | ||
assert.Equal(t, childNodes, len(links)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.