-
Notifications
You must be signed in to change notification settings - Fork 707
wasm: Add wasm support #976
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
Open
tmc
wants to merge
3
commits into
etcd-io:main
Choose a base branch
from
tmc:wasm-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| --- | ||
| name: Tests WASM | ||
| permissions: read-all | ||
| on: [push, pull_request] | ||
| jobs: | ||
| test-wasm-js: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
| - id: goversion | ||
| run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT" | ||
| - uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 | ||
| with: | ||
| go-version: ${{ steps.goversion.outputs.goversion }} | ||
| - name: Setup Node.js | ||
| uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 | ||
| with: | ||
| node-version: '20' | ||
| - name: Run WASM JS tests | ||
| run: | | ||
| env -i \ | ||
| PATH="$PATH" \ | ||
| HOME="$HOME" \ | ||
| GOCACHE="$GOCACHE" \ | ||
| GOPATH="$GOPATH" \ | ||
| GOROOT="$(go env GOROOT)" \ | ||
| make test-wasm | ||
|
|
||
| test-wasm-wasip1: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
| - id: goversion | ||
| run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT" | ||
| - uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0 | ||
| with: | ||
| go-version: ${{ steps.goversion.outputs.goversion }} | ||
| - name: Install wazero | ||
| run: go install github.com/tetratelabs/wazero/cmd/wazero@latest | ||
| - name: Run WASM wasip1 tests | ||
| run: | | ||
| make test-wasip1 |
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
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,147 @@ | ||
| //go:build js || wasip1 | ||
|
|
||
| package bbolt | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "time" | ||
| "unsafe" | ||
|
|
||
| berrors "go.etcd.io/bbolt/errors" | ||
| "go.etcd.io/bbolt/internal/common" | ||
| ) | ||
|
|
||
| // mmap memory maps a DB's data file. | ||
| func mmap(db *DB, sz int) error { | ||
| // Check MaxSize constraint for WASM platforms | ||
| if !db.readOnly && db.MaxSize > 0 && sz > db.MaxSize { | ||
| // The max size only limits future writes; however, we don't block opening | ||
| // and mapping the database if it already exceeds the limit. | ||
| fileSize, err := db.fileSize() | ||
| if err != nil { | ||
| return fmt.Errorf("could not check existing db file size: %s", err) | ||
| } | ||
|
|
||
| if sz > fileSize { | ||
| return berrors.ErrMaxSizeReached | ||
| } | ||
| } | ||
|
|
||
| // Truncate and fsync to ensure file size metadata is flushed. | ||
| // https://github.com/boltdb/bolt/issues/284 | ||
| if !db.NoGrowSync && !db.readOnly { | ||
| if err := db.file.Truncate(int64(sz)); err != nil { | ||
| return fmt.Errorf("file resize error: %s", err) | ||
| } | ||
| if err := db.file.Sync(); err != nil { | ||
| return fmt.Errorf("file sync error: %s", err) | ||
| } | ||
| } | ||
|
|
||
| // Map the data file to memory. | ||
| b := make([]byte, sz) | ||
| if sz > 0 { | ||
| // Read the data file. | ||
| if _, err := db.file.ReadAt(b, 0); err != nil && err != io.EOF { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| // Save the original byte slice and convert to a byte array pointer. | ||
| db.dataref = b | ||
| db.datasz = sz | ||
| if sz > 0 { | ||
| db.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(&b[0])) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // munmap unmaps a DB's data file from memory. | ||
| func munmap(db *DB) error { | ||
| // In WASM, we just clear the references | ||
| db.dataref = nil | ||
| db.data = nil | ||
| db.datasz = 0 | ||
| return nil | ||
| } | ||
|
|
||
| // madvise is not supported in WASM. | ||
| func madvise(b []byte, advice int) error { | ||
| // Not implemented - no memory advice in WASM | ||
| return nil | ||
| } | ||
|
|
||
| // mlock is not supported in WASM. | ||
| func mlock(db *DB, fileSize int) error { | ||
| // Not implemented - no memory locking in WASM | ||
| return nil | ||
| } | ||
|
|
||
| // munlock is not supported in WASM. | ||
| func munlock(db *DB, fileSize int) error { | ||
| // Not implemented - no memory unlocking in WASM | ||
| return nil | ||
| } | ||
|
|
||
| // flock acquires an advisory lock on a file descriptor. | ||
| func flock(db *DB, exclusive bool, timeout time.Duration) error { | ||
| // Not implemented - no file locking in WASM | ||
| return nil | ||
| } | ||
|
|
||
| // funlock releases an advisory lock on a file descriptor. | ||
| func funlock(db *DB) error { | ||
| // Not implemented - no file unlocking in WASM | ||
| return nil | ||
| } | ||
|
|
||
| // fdatasync flushes written data to a file descriptor. | ||
| func fdatasync(db *DB) error { | ||
| if db.file == nil { | ||
| return nil | ||
| } | ||
| return db.file.Sync() | ||
| } | ||
|
|
||
| // txInit refreshes the memory buffer from the file for WASM platforms. | ||
| // This is needed because WASM doesn't have real mmap, so we need to manually | ||
| // sync the memory buffer with the file to see changes from previous transactions. | ||
| func (db *DB) txInit() error { | ||
| // For read-only databases or initial state, skip refresh | ||
| if db.file == nil { | ||
| return nil | ||
| } | ||
|
|
||
| // Check if the file has grown | ||
| fileInfo, err := db.file.Stat() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fileSize := int(fileInfo.Size()) | ||
|
|
||
| // If file has grown or we need to initialize, refresh memory | ||
| if fileSize > db.datasz || db.datasz == 0 { | ||
| // Re-mmap with the new size | ||
| if err := mmap(db, fileSize); err != nil { | ||
| return err | ||
| } | ||
| } else if db.datasz > 0 { | ||
| // Refresh the existing buffer | ||
| b := make([]byte, db.datasz) | ||
| if _, err := db.file.ReadAt(b, 0); err != nil && err != io.EOF { | ||
| return err | ||
| } | ||
| db.dataref = b | ||
| db.data = (*[common.MaxMapSize]byte)(unsafe.Pointer(&b[0])) | ||
| } | ||
|
|
||
| // Update meta page pointers | ||
| if db.pageSize > 0 && db.datasz >= db.pageSize*2 { | ||
| db.meta0 = db.page(0).Meta() | ||
| db.meta1 = db.page(1).Meta() | ||
| } | ||
|
|
||
| return nil | ||
| } |
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
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 |
|---|---|---|
|
|
@@ -772,12 +772,28 @@ func (db *DB) Logger() Logger { | |
| return db.logger | ||
| } | ||
|
|
||
| // txIniter is an interface that allows for platform-specific transaction | ||
| // initialization. | ||
| type txIniter interface { | ||
| txInit() error | ||
| } | ||
|
|
||
| func (db *DB) beginTx() (*Tx, error) { | ||
| // Lock the meta pages while we initialize the transaction. We obtain | ||
| // the meta lock before the mmap lock because that's the order that the | ||
| // write transaction will obtain them. | ||
| db.metalock.Lock() | ||
|
|
||
| // Allow WASM-specific transaction initialization | ||
| if runtime.GOARCH == "wasm" { | ||
| if initer, ok := any(db).(txIniter); ok { | ||
| if err := initer.txInit(); err != nil { | ||
| db.metalock.Unlock() | ||
|
Member
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. Any reason not to precede this call with ideally it should be move above, after acquiring the lock |
||
| return nil, err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Obtain a read-only lock on the mmap. When the mmap is remapped it will | ||
| // obtain a write lock so all transactions must finish before it can be | ||
| // remapped. | ||
|
|
@@ -834,6 +850,16 @@ func (db *DB) beginRWTx() (*Tx, error) { | |
| db.metalock.Lock() | ||
| defer db.metalock.Unlock() | ||
|
|
||
| // Allow WASM-specific transaction initialization | ||
| if runtime.GOARCH == "wasm" { | ||
| if initer, ok := any(db).(txIniter); ok { | ||
| if err := initer.txInit(); err != nil { | ||
| db.rwlock.Unlock() | ||
|
Member
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. ditto |
||
| return nil, err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Exit if the database is not open yet. | ||
| if !db.opened { | ||
| db.rwlock.Unlock() | ||
|
|
||
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,9 @@ | ||
| //go:build js || wasip1 | ||
|
|
||
| package common | ||
|
|
||
| // MaxMapSize represents the largest mmap size supported by Bolt. | ||
| const MaxMapSize = 0x10000000 | ||
|
|
||
| // MaxAllocSize is the size used when creating array pointers. | ||
| const MaxAllocSize = 0x10100000 |
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| //go:build !windows | ||
| //go:build !windows && !js && !wasip1 | ||
|
|
||
| package bbolt | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| //go:build !windows | ||
| //go:build !windows && !js && !wasip1 | ||
|
|
||
| package bbolt_test | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tmc for your prompt follow up 🚀 👍🏽
wdyt about this ?
Any reason to wrap
dbwithinany?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @tmc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type assertions must take an interface value and db is *DB which means it can't be used in a type assertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @fuweid @ahrtr