Skip to content

Commit dd0e5af

Browse files
authored
feat: Allow lens runtime selection via config (#2684)
## Relevant issue(s) Resolves #2683 ## Description Allows lens runtime selection via config/cli param. Also adds CI jobs to the matrix to test wasmer and wazero.
1 parent 25fc07d commit dd0e5af

28 files changed

+315
-98
lines changed

.github/workflows/test-and-upload-coverage.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,29 @@ jobs:
3434
client-type: [go, http, cli]
3535
database-type: [badger-file, badger-memory]
3636
mutation-type: [gql, collection-named, collection-save]
37+
lens-type: [wasm-time]
3738
detect-changes: [false]
3839
database-encryption: [false]
3940
include:
4041
- os: ubuntu-latest
4142
client-type: go
4243
database-type: badger-memory
4344
mutation-type: collection-save
45+
lens-type: wasm-time
4446
detect-changes: true
4547
database-encryption: false
4648
- os: ubuntu-latest
4749
client-type: go
4850
database-type: badger-memory
4951
mutation-type: collection-save
52+
lens-type: wasm-time
5053
detect-changes: false
5154
database-encryption: true
5255
- os: macos-latest
5356
client-type: go
5457
database-type: badger-memory
5558
mutation-type: collection-save
59+
lens-type: wasm-time
5660
detect-changes: false
5761
database-encryption: false
5862
## TODO: https://github.com/sourcenetwork/defradb/issues/2080
@@ -63,6 +67,20 @@ jobs:
6367
## mutation-type: collection-save
6468
## detect-changes: false
6569
## database-encryption: false
70+
- os: ubuntu-latest
71+
client-type: go
72+
database-type: badger-memory
73+
mutation-type: collection-save
74+
lens-type: wazero
75+
detect-changes: false
76+
database-encryption: false
77+
- os: ubuntu-latest
78+
client-type: go
79+
database-type: badger-memory
80+
mutation-type: collection-save
81+
lens-type: wasmer
82+
detect-changes: false
83+
database-encryption: false
6684

6785
runs-on: ${{ matrix.os }}
6886

@@ -80,6 +98,7 @@ jobs:
8098
DEFRA_BADGER_FILE: ${{ matrix.database-type == 'badger-file' }}
8199
DEFRA_BADGER_ENCRYPTION: ${{ matrix.database-encryption }}
82100
DEFRA_MUTATION_TYPE: ${{ matrix.mutation-type }}
101+
DEFRA_LENS_TYPE: ${{ matrix.lens-type }}
83102

84103
steps:
85104
- name: Checkout code into the directory

cli/server_dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func MakeServerDumpCmd() *cobra.Command {
3737
if err != nil {
3838
return err
3939
}
40-
db, err := db.NewDB(cmd.Context(), rootstore, acp.NoACP)
40+
db, err := db.NewDB(cmd.Context(), rootstore, acp.NoACP, nil)
4141
if err != nil {
4242
return errors.Wrap("failed to initialize database", err)
4343
}

cli/start.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func MakeStartCommand() *cobra.Command {
126126
http.WithAllowedOrigins(cfg.GetStringSlice("api.allowed-origins")...),
127127
http.WithTLSCertPath(cfg.GetString("api.pubKeyPath")),
128128
http.WithTLSKeyPath(cfg.GetString("api.privKeyPath")),
129+
node.WithLensRuntime(node.LensRuntimeType(cfg.GetString("lens.runtime"))),
129130
}
130131

131132
if cfg.GetString("datastore.store") != configStoreMemory {

client/lens.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515

1616
"github.com/lens-vm/lens/host-go/config/model"
1717
"github.com/sourcenetwork/immutable/enumerable"
18+
19+
"github.com/sourcenetwork/defradb/datastore"
1820
)
1921

2022
// LensConfig represents the configuration of a Lens migration in Defra.
@@ -38,9 +40,18 @@ type LensConfig struct {
3840
model.Lens
3941
}
4042

43+
// TxnSource represents an object capable of constructing the transactions that
44+
// implicit-transaction registries need internally.
45+
type TxnSource interface {
46+
NewTxn(context.Context, bool) (datastore.Txn, error)
47+
}
48+
4149
// LensRegistry exposes several useful thread-safe migration related functions which may
4250
// be used to manage migrations.
4351
type LensRegistry interface {
52+
// Init initializes the registry with the provided transaction source.
53+
Init(TxnSource)
54+
4455
// SetMigration caches the migration for the given collection ID. It does not persist the migration in long
4556
// term storage, for that one should call [Store.SetMigration(ctx, cfg)].
4657
//

docs/config.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,12 @@ Keyring backend to use. Defaults to `file`.
111111

112112
- `file` Stores keys in encrypted files
113113
- `system` Stores keys in the OS managed keyring
114+
115+
## `lens.runtime`
116+
117+
The LensVM wasm runtime to run lens modules in.
118+
119+
Possible values:
120+
- `wasm-time` (default): https://github.com/bytecodealliance/wasmtime-go
121+
- `wasmer` (windows not supported): https://github.com/wasmerio/wasmer-go
122+
- `wazero`: https://github.com/tetratelabs/wazero

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,10 @@ require (
282282
github.com/tendermint/go-amino v0.16.0 // indirect
283283
github.com/tendermint/tm-db v0.6.7 // indirect
284284
github.com/teserakt-io/golang-ed25519 v0.0.0-20210104091850-3888c087a4c8 // indirect
285+
github.com/tetratelabs/wazero v1.5.0 // indirect
285286
github.com/textileio/go-log/v2 v2.1.3-gke-2 // indirect
286287
github.com/ugorji/go/codec v1.2.12 // indirect
288+
github.com/wasmerio/wasmer-go v1.0.4 // indirect
287289
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
288290
github.com/x448/float16 v0.8.4 // indirect
289291
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,8 @@ github.com/tendermint/tm-db v0.6.7 h1:fE00Cbl0jayAoqlExN6oyQJ7fR/ZtoVOmvPJ//+shu
11391139
github.com/tendermint/tm-db v0.6.7/go.mod h1:byQDzFkZV1syXr/ReXS808NxA2xvyuuVgXOJ/088L6I=
11401140
github.com/teserakt-io/golang-ed25519 v0.0.0-20210104091850-3888c087a4c8 h1:RBkacARv7qY5laaXGlF4wFB/tk5rnthhPb8oIBGoagY=
11411141
github.com/teserakt-io/golang-ed25519 v0.0.0-20210104091850-3888c087a4c8/go.mod h1:9PdLyPiZIiW3UopXyRnPYyjUXSpiQNHRLu8fOsR3o8M=
1142+
github.com/tetratelabs/wazero v1.5.0 h1:Yz3fZHivfDiZFUXnWMPUoiW7s8tC1sjdBtlJn08qYa0=
1143+
github.com/tetratelabs/wazero v1.5.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
11421144
github.com/textileio/go-datastore-extensions v1.0.1 h1:qIJGqJaigQ1wD4TdwS/hf73u0HChhXvvUSJuxBEKS+c=
11431145
github.com/textileio/go-datastore-extensions v1.0.1/go.mod h1:Pzj9FDRkb55910dr/FX8M7WywvnS26gBgEDez1ZBuLE=
11441146
github.com/textileio/go-ds-badger3 v0.1.0 h1:q0kBuBmAcRUR3ClMSYlyw0224XeuzjjGinU53Qz1uXI=
@@ -1168,6 +1170,8 @@ github.com/warpfork/go-testmark v0.12.1 h1:rMgCpJfwy1sJ50x0M0NgyphxYYPMOODIJHhsX
11681170
github.com/warpfork/go-testmark v0.12.1/go.mod h1:kHwy7wfvGSPh1rQJYKayD4AbtNaeyZdcGi9tNJTaa5Y=
11691171
github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ=
11701172
github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw=
1173+
github.com/wasmerio/wasmer-go v1.0.4 h1:MnqHoOGfiQ8MMq2RF6wyCeebKOe84G88h5yv+vmxJgs=
1174+
github.com/wasmerio/wasmer-go v1.0.4/go.mod h1:0gzVdSfg6pysA6QVp6iVRPTagC6Wq9pOE8J86WKb2Fk=
11711175
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc h1:BCPnHtcboadS0DvysUuJXZ4lWVv5Bh5i7+tbIyi+ck4=
11721176
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc/go.mod h1:r45hJU7yEoA81k6MWNhpMj/kms0n14dkzkxYHoB96UM=
11731177
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f h1:jQa4QT2UP9WYv2nzyawpKMOCl+Z/jW7djv2/J50lj9E=

http/client_lens.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type setMigrationRequest struct {
3535
Config model.Lens
3636
}
3737

38+
func (w *LensRegistry) Init(txnSource client.TxnSource) {}
39+
3840
func (c *LensRegistry) SetMigration(ctx context.Context, collectionID uint32, config model.Lens) error {
3941
methodURL := c.http.baseURL.JoinPath("lens", "registry")
4042

http/handler_ccip_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestCCIPPost_WithInvalidBody(t *testing.T) {
193193
func setupDatabase(t *testing.T) client.DB {
194194
ctx := context.Background()
195195

196-
cdb, err := db.NewDB(ctx, memory.NewDatastore(ctx), acp.NoACP, db.WithUpdateEvents())
196+
cdb, err := db.NewDB(ctx, memory.NewDatastore(ctx), acp.NoACP, nil, db.WithUpdateEvents())
197197
require.NoError(t, err)
198198

199199
_, err = cdb.AddSchema(ctx, `type User {

internal/db/config.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
package db
1212

1313
import (
14-
"github.com/lens-vm/lens/host-go/engine/module"
1514
"github.com/sourcenetwork/immutable"
1615

1716
"github.com/sourcenetwork/defradb/events"
@@ -40,19 +39,3 @@ func WithMaxRetries(num int) Option {
4039
db.maxTxnRetries = immutable.Some(num)
4140
}
4241
}
43-
44-
// WithLensPoolSize sets the maximum number of cached migrations instances to preserve per schema version.
45-
//
46-
// Will default to `5` if not set.
47-
func WithLensPoolSize(size int) Option {
48-
return func(db *db) {
49-
db.lensPoolSize = immutable.Some(size)
50-
}
51-
}
52-
53-
// WithLensRuntime returns an option that sets the lens registry runtime.
54-
func WithLensRuntime(runtime module.Runtime) Option {
55-
return func(db *db) {
56-
db.lensRuntime = immutable.Some(runtime)
57-
}
58-
}

0 commit comments

Comments
 (0)