Skip to content

Commit

Permalink
load extensions during compile time using go build tags (#350)
Browse files Browse the repository at this point in the history
* load extensions during compile time using go build tags

* remove scalar values and Extension struct for local extensions
  • Loading branch information
charithabandi authored and brennanjl committed Feb 26, 2024
1 parent 11193f1 commit ab25cfb
Show file tree
Hide file tree
Showing 15 changed files with 518 additions and 229 deletions.
8 changes: 6 additions & 2 deletions cmd/kwild/server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,13 @@ func buildDatasetsModule(d *coreDependencies, eng datasets.Engine, accs datasets
}

func buildEngine(d *coreDependencies, a *sessions.AtomicCommitter) *engine.Engine {
extensions, err := connectExtensions(d.ctx, d.cfg.AppCfg.ExtensionEndpoints)
extensions, err := getExtensions(d.ctx, d.cfg.AppCfg.ExtensionEndpoints)
if err != nil {
failBuild(err, "failed to connect to extensions")
failBuild(err, "failed to get extensions")
}

for _, ext := range extensions {
d.log.Debug("registered extension", zap.String("name", ext.Name()))
}

sqlCommitRegister := &sqlCommittableRegister{
Expand Down
26 changes: 19 additions & 7 deletions cmd/kwild/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"github.com/kwilteam/kwil-db/core/log"
types "github.com/kwilteam/kwil-db/core/types/admin"
"github.com/kwilteam/kwil-db/core/types/transactions"
extensions "github.com/kwilteam/kwil-db/extensions/actions"
extActions "github.com/kwilteam/kwil-db/extensions/actions"
"github.com/kwilteam/kwil-db/internal/abci"
"github.com/kwilteam/kwil-db/internal/abci/cometbft/privval"
"github.com/kwilteam/kwil-db/internal/engine"
"github.com/kwilteam/kwil-db/internal/extensions"
"github.com/kwilteam/kwil-db/internal/kv"
"github.com/kwilteam/kwil-db/internal/sessions"
sqlSessions "github.com/kwilteam/kwil-db/internal/sessions/sql-session"
Expand All @@ -28,9 +29,18 @@ import (
cmttypes "github.com/cometbft/cometbft/types"
)

// connectExtensions connects to the provided extension urls.
func connectExtensions(ctx context.Context, urls []string) (map[string]*extensions.Extension, error) {
exts := make(map[string]*extensions.Extension, len(urls))
// getExtensions returns both the local and remote extensions. Remote extensions are identified by
// connecting to the specified extension URLs.
func getExtensions(ctx context.Context, urls []string) (map[string]extActions.Extension, error) {
exts := make(map[string]extActions.Extension)

for name, ext := range extActions.RegisteredExtensions() {
_, ok := exts[name]
if ok {
return nil, fmt.Errorf("duplicate extension name: %s", name)
}
exts[name] = ext
}

for _, url := range urls {
ext := extensions.New(url)
Expand All @@ -46,15 +56,17 @@ func connectExtensions(ctx context.Context, urls []string) (map[string]*extensio

exts[ext.Name()] = ext
}

return exts, nil
}

func adaptExtensions(exts map[string]*extensions.Extension) map[string]engine.ExtensionInitializer {
func adaptExtensions(exts map[string]extActions.Extension) map[string]engine.ExtensionInitializer {
adapted := make(map[string]engine.ExtensionInitializer, len(exts))

for name, ext := range exts {
adapted[name] = extensionInitializeFunc(ext.CreateInstance)
initializer := &extensions.ExtensionInitializer{
Extension: ext,
}
adapted[name] = extensionInitializeFunc(initializer.CreateInstance)
}

return adapted
Expand Down
72 changes: 0 additions & 72 deletions extensions/actions/extension.go

This file was deleted.

28 changes: 28 additions & 0 deletions extensions/actions/extension_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package extensions

import (
"context"
"strings"
)

type Extension interface {
Name() string
Initialize(ctx context.Context, metadata map[string]string) (map[string]string, error)
Execute(ctx context.Context, metadata map[string]string, method string, args ...any) ([]any, error)
}

var registeredExtensions = make(map[string]Extension)

func RegisterExtension(name string, ext Extension) error {
name = strings.ToLower(name)
if _, ok := registeredExtensions[name]; ok {
panic("extension of same name already registered: " + name)
}

registeredExtensions[name] = ext
return nil
}

func RegisteredExtensions() map[string]Extension {
return registeredExtensions
}
37 changes: 0 additions & 37 deletions extensions/actions/extension_test.go

This file was deleted.

53 changes: 0 additions & 53 deletions extensions/actions/instance.go

This file was deleted.

Loading

0 comments on commit ab25cfb

Please sign in to comment.