2022-09-02 19:23:33 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2022-09-26 18:05:28 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2022-09-21 05:45:23 +00:00
|
|
|
"sync"
|
2022-09-02 19:23:33 +00:00
|
|
|
|
|
|
|
"go.k6.io/k6/js/modules"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RootModule is the global module object type. It is instantiated once per test
|
2022-12-29 14:52:55 +00:00
|
|
|
// run and will be used to create k6/x/frostfs/registry module instances for each VU.
|
2022-09-02 19:23:33 +00:00
|
|
|
type RootModule struct {
|
2022-09-21 05:45:23 +00:00
|
|
|
// Stores object registry by path of database file. We should have only single instance
|
|
|
|
// of registry per each file
|
|
|
|
registries map[string]*ObjRegistry
|
2022-09-26 18:05:28 +00:00
|
|
|
// Stores object selector by name. We may have multiple selectors per database file
|
|
|
|
selectors map[string]*ObjSelector
|
|
|
|
// Mutex to sync access to the maps
|
2022-09-21 05:45:23 +00:00
|
|
|
mu sync.Mutex
|
2022-09-02 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Registry represents an instance of the module for every VU.
|
|
|
|
type Registry struct {
|
|
|
|
vu modules.VU
|
|
|
|
root *RootModule
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the interfaces are implemented correctly.
|
|
|
|
var (
|
|
|
|
_ modules.Instance = &Registry{}
|
|
|
|
_ modules.Module = &RootModule{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-09-26 18:05:28 +00:00
|
|
|
rootModule := &RootModule{
|
|
|
|
registries: make(map[string]*ObjRegistry),
|
|
|
|
selectors: make(map[string]*ObjSelector),
|
|
|
|
}
|
2022-12-29 14:52:55 +00:00
|
|
|
modules.Register("k6/x/frostfs/registry", rootModule)
|
2022-09-02 19:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewModuleInstance implements the modules.Module interface and returns
|
|
|
|
// a new instance for each VU.
|
|
|
|
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
|
|
|
|
mi := &Registry{vu: vu, root: r}
|
|
|
|
return mi
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports implements the modules.Instance interface and returns the exports
|
|
|
|
// of the JS module.
|
|
|
|
func (r *Registry) Exports() modules.Exports {
|
|
|
|
return modules.Exports{Default: r}
|
|
|
|
}
|
|
|
|
|
2022-09-21 05:45:23 +00:00
|
|
|
// Open creates a new instance of object registry that will store information about objects
|
|
|
|
// in the specified file. If repository instance for the file was previously created, then
|
|
|
|
// Open will return the existing instance of repository, because bolt database allows only
|
2022-09-22 16:57:21 +00:00
|
|
|
// one write connection at a time.
|
2022-09-21 05:45:23 +00:00
|
|
|
func (r *Registry) Open(dbFilePath string) *ObjRegistry {
|
|
|
|
r.root.mu.Lock()
|
|
|
|
defer r.root.mu.Unlock()
|
2022-09-26 18:05:28 +00:00
|
|
|
return r.open(dbFilePath)
|
|
|
|
}
|
2022-09-02 19:23:33 +00:00
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
// Implementation of Open without mutex lock, so that it can be re-used in other methods.
|
|
|
|
func (r *Registry) open(dbFilePath string) *ObjRegistry {
|
2022-09-21 05:45:23 +00:00
|
|
|
registry := r.root.registries[dbFilePath]
|
|
|
|
if registry == nil {
|
2022-10-26 19:29:17 +00:00
|
|
|
registry = NewObjRegistry(r.vu.Context(), dbFilePath)
|
2022-09-21 05:45:23 +00:00
|
|
|
r.root.registries[dbFilePath] = registry
|
|
|
|
}
|
|
|
|
return registry
|
2022-09-02 19:23:33 +00:00
|
|
|
}
|
2022-09-26 18:05:28 +00:00
|
|
|
|
2022-11-10 06:23:33 +00:00
|
|
|
func (r *Registry) GetSelector(dbFilePath string, name string, cacheSize int, filter map[string]string) *ObjSelector {
|
2022-09-26 18:05:28 +00:00
|
|
|
objFilter, err := parseFilter(filter)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.root.mu.Lock()
|
|
|
|
defer r.root.mu.Unlock()
|
|
|
|
|
|
|
|
selector := r.root.selectors[name]
|
|
|
|
if selector == nil {
|
|
|
|
registry := r.open(dbFilePath)
|
2022-11-10 06:23:33 +00:00
|
|
|
selector = NewObjSelector(registry, cacheSize, objFilter)
|
2022-09-26 18:05:28 +00:00
|
|
|
r.root.selectors[name] = selector
|
|
|
|
} else if !reflect.DeepEqual(selector.filter, objFilter) {
|
|
|
|
panic(fmt.Sprintf("selector %s already has been created with a different filter", name))
|
|
|
|
}
|
|
|
|
return selector
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:48:09 +00:00
|
|
|
func (r *Registry) GetExporter(selector *ObjSelector) *ObjExporter {
|
|
|
|
return NewObjExporter(selector)
|
|
|
|
}
|
|
|
|
|
2022-09-26 18:05:28 +00:00
|
|
|
func parseFilter(filter map[string]string) (*ObjFilter, error) {
|
|
|
|
objFilter := ObjFilter{}
|
|
|
|
objFilter.Status = filter["status"]
|
|
|
|
|
|
|
|
if ageStr := filter["age"]; ageStr != "" {
|
|
|
|
age, err := strconv.ParseInt(ageStr, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
objFilter.Age = int(age)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &objFilter, nil
|
|
|
|
}
|