modulepackage
0.0.0-20201010192612-3c7917683fe0
Repository: https://github.com/struckoff/sfcframework.git
Documentation: pkg.go.dev
# README
Library provides a hashing mechanism that uses the space-filling curve as a core algorithm for locating data between multiple nodes. This approach provides high data locality and distribution based on data and nodes features.
Architecture
Configuration
Balancer is configurable by those parameters:
- curve type
- amount of dimensions
- size of each dimension
- transform function
- optimizer
Curves
Library provides an implementation of Hilbert and Morton curves. A curve can encode an arbitrary number of dimensions. The number of dimensions to work with should be configured on curve creation.
Hilbert curve
Morton curve
Transform
TransformFunc is an adapter which purpose to convert values into encodable format.
type TransformFunc func(values []interface{}, sfc curve.Curve) ([]uint64, error)
Optimizer
The optimizer is a function responsible for dividing the curve into cell groups. This function should contain the realization of an algorithm of distribution cell ranges per node.
type OptimizerFunc func(s *Space) ([]*CellGroup, error)
Example
package main
import (
"fmt"
balancer "github.com/struckoff/sfcframework"
"github.com/struckoff/sfcframework/curve"
"github.com/struckoff/sfcframework/node"
"github.com/struckoff/sfcframework/optimizer"
"github.com/struckoff/sfcframework/transform"
)
func main() {
n := MyNode("node-0")
d := MyDataItem("di-id")
b, err := balancer.NewBalancer(curve.Morton, 2, 64, transform.SpaceTransform, optimizer.PowerRangeOptimizer, nil)
if err != nil {
panic(err)
}
err = b.AddNode(&n, true)
if err != nil {
panic(err)
}
nn, cID, err := b.AddData(&d)
if err != nil {
panic(err)
}
fmt.Println(nn.ID(), cID)
}
type P float64
func (p *P) Get() float64 {
return float64(*p)
}
type C float64
func (c *C) Get() (float64, error) {
return float64(*c), nil
}
type MyNode string
func (n *MyNode) ID() string {
return string(*n)
}
func (n *MyNode) Hash() uint64 {
return 1
}
func (n *MyNode) Capacity() node.Capacity {
c := C(1000.0)
return &c
}
func (n *MyNode) Power() node.Power {
p := P(42.42)
return &p
}
type MyDataItem string
func (d *MyDataItem) Values() []interface{} {
return []interface{}{1.0, 2.0}
}
func (d *MyDataItem) ID() string {
return string(*d)
}
func (d *MyDataItem) Size() uint64 {
return 42
}
Example of usage
# Packages
The internal implementation of the space-filling curve algorithm responsible for encoding and decoding of dimensions.
No description provided by the author
Interfaces connected with node.
Default optimizers provided by the library.
Default transform functions are provided by the library.
# Functions
NewBalancer creates a new instance of balancerusing curve type, amount of dimension to work with,size of the each dimension,function which transform DataItem into SFC-readable format,optimizer function which distributes cells into groupsand list of nodes in space(could be nil).
NewCell - allocates new instances of cell and attaches it to the cell group.
NewCellGroup builds a new CellGroup.
NewRange - creates a new range by specified limits.
NewSpace creates new spaceusing space-filling curve instance,function which transform DataItem into SFC-readable format,and list of nodes in space(could be nil).
# Structs
Balancer is responsible for distributing load between nodes of the cluster.It stores meta-information about the current load in the cells of the space-filling curve.Cells are stored in the slice of cell groups, one cell group for every node of the cluster.To balance the load between nodes, the balancer analyzes the power and capacity of nodesand distributes cells between cell groups in such a way that all nodes would be equally.
CellGroup represents a segment of SFC attached to the node.It contains information about the range of cells attached to this group and node.
Range - range of cells IDs attached to the cell group.
Space is a container for allocated cells and their relations to groups.It contains instances of the space-filling curve and transforms function.
# Interfaces
DataItem is an interface describing data that is loaded into the system and need to be placed at some node within cluster.
# Type aliases
OptimizerFunc is a function which responsible for dividing curve into cell groups.This function should contain realisation of an algorithm of distribution cell ranges per node.
TransformFunc is an adapter which purpose to convert values into encodable format.