config

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2023 License: Apache-2.0 Imports: 25 Imported by: 0

README

配置中心

Root

ARANA_ROOT=/arana

Tenants

# 路径 Key
${ARANA_ROOT}/v1/tenants/<tenant>
/arana/v1/tenants/arana

# 存储内容
- name: arana
  users:
    - username: root
      password: "123456"
    - username: arana
      password: "123456"

Nodes

# 路径 Key
${ARANA_ROOT}/v1/tenants/<tenant>/nodes/<node>
/arana/v1/tenants/arana/nodes/node0
/arana/v1/tenants/arana/nodes/node1

# 存储内容
- name: node0
  host: arana-mysql
  port: 3306
  username: root
  password: "123456"
  database: employees_0000
  weight: r10w10
  parameters:

Data Source Clusters

# 路径 Key
${ARANA_ROOT}/v1/tenants/<tenant>/clusters/<cluster>/groups/<group>/nodes/<node>
/arana/v1/tenants/arana/clusters/employees/groups/employees_0000/nodes/node0
/arana/v1/tenants/arana/clusters/employees/groups/employees_0000/nodes/node0_r_0

# 存储内容
- name: node0
  host: arana-mysql
  port: 3306
  username: root
  password: "123456"
  database: employees_0000
  weight: r10w10
  parameters:


# 路径 Key
${ARANA_ROOT}/v1/tenants/<tenant>/clusters/<cluster>/groups/<group>
/arana/v1/tenants/arana/clusters/employees/groups/employees_0000
/arana/v1/tenants/arana/clusters/employees/groups/employees_0001

# 存储内容
nodes:
  - node0
  - node1

Sharding Rule

# 路径 Key
${ARANA_ROOT}/v1/tenants/<tenant>/clusters/<cluster>/tables/<table>
/arana/v1/tenants/arana/clusters/employees/tables/student
/arana/v1/tenants/arana/clusters/employees/tables/order

# 存储内容
- name: student
  allow_full_scan: true
  sequence:
    type: snowflake
    option:
  db_rules:
    - column: uid
      type: scriptExpr
      expr: parseInt($value % 32 / 8)
  tbl_rules:
    - column: uid
      type: scriptExpr
      expr: $value % 32
      step: 32
  topology:
    db_pattern: employees_${0000..0003}
    tbl_pattern: student_${0000..0031}
  attributes:
    sqlMaxLimit: -1

Documentation

Index

Constants

View Source
const (
	ConfigItemSpec         = "spec"
	ConfigItemSysDB        = "sys_db"
	ConfigItemUsers        = "users"
	ConfigItemClusters     = "clusters"
	ConfigItemShardingRule = "sharding_rule"
	ConfigItemNodes        = "nodes"
	ConfigItemShadowRule   = "shadow_rule"
)

Variables

View Source
var ErrorNoStoreOperate = errors.New("no store operate")
View Source
var ErrorNotImplement = errors.New("not implement")

Functions

func GetConnPropCapacity

func GetConnPropCapacity(connProps map[string]interface{}, defaultValue int) int

GetConnPropCapacity parses the capacity of backend connection pool, return default value if failed.

func GetConnPropIdleTime

func GetConnPropIdleTime(connProps map[string]interface{}, defaultValue time.Duration) time.Duration

GetConnPropIdleTime parses the idle time of backend connection pool, return default value if failed.

func GetConnPropMaxCapacity

func GetConnPropMaxCapacity(connProps map[string]interface{}, defaultValue int) int

GetConnPropMaxCapacity parses the max capacity of backend connection pool, return default value if failed.

func Init

func Init(options Options, version string) error

func InitStoreOperate

func InitStoreOperate(options Options) error

func JSONToYAML

func JSONToYAML(j string) ([]byte, error)

func NewConfigWatcher

func NewConfigWatcher(tenant string, storeOperate StoreOperator, pathInfo *PathInfo) (*configWatcher, error)

func Register

func Register(s StoreOperator) error

Register register store plugin

func Validate

func Validate(cfg *Configuration) error

Validate validates the input configuration.

func WithCacheable

func WithCacheable(open bool) option

WithCacheable set whether to enable the read cache mechanism

func WithReader

func WithReader(open bool) option

WithReader Set whether to enable the read operation of the configuration center

func WithWatcher

func WithWatcher(open bool) option

WithWatcher Set whether to enable the watch operation of the configuration center

func WithWriter

func WithWriter(open bool) option

WithWriter Set whether to enable the write operation of the configuration center

Types

type BootOptions

type BootOptions struct {
	Spec       `yaml:",inline"`
	Config     *Options    `yaml:"config" json:"config"`
	Listeners  []*Listener `validate:"required,dive" yaml:"listeners" json:"listeners"`
	Registry   *Registry   `yaml:"registry" json:"registry"`
	Trace      *Trace      `yaml:"trace" json:"trace"`
	Supervisor *User       `validate:"required,dive" yaml:"supervisor" json:"supervisor"`
	Logging    *log.Config `validate:"required,dive" yaml:"logging" json:"logging"`
}

func LoadBootOptions

func LoadBootOptions(path string) (*BootOptions, error)

LoadBootOptions loads BootOptions from specified file path.

type CacheConfigReaderTest

type CacheConfigReaderTest = cacheConfigReader

type Center

type Center interface {
	io.Closer
	ConfigWriter
	ConfigReader
	ConfigWatcher
	// Tenant tenant info
	Tenant() string
}

Center Configuration center for each tenant, tenant-level isolation

func NewCenter

func NewCenter(tenant string, storeOperate StoreOperator, opts ...option) (Center, error)

type CenterTest

type CenterTest = center

type ClusterEvent

type ClusterEvent struct {
	Name        string
	Type        DataSourceType
	SqlMaxLimit int
	Parameters  ParametersMap
	GroupsEvent *GroupsEvent
}

ClusterEvent cluster event

type Clusters

type Clusters []*DataSourceCluster

func (Clusters) Diff

func (c Clusters) Diff(old Clusters) *ClustersEvent

type ClustersEvent

type ClustersEvent struct {
	AddClusters    Clusters
	DeleteClusters Clusters
	UpdateClusters []*ClusterEvent
}

ClustersEvent clusters event

func (ClustersEvent) Type

func (e ClustersEvent) Type() EventType

type ColumnRule

type ColumnRule struct {
	Name string `validate:"required" yaml:"name" json:"name"`
	Type string `yaml:"type" json:"type"`
	Step int    `yaml:"step" json:"step"`
}

type ConfigItem

type ConfigItem string

ConfigItem

type ConfigReader

type ConfigReader interface {
	io.Closer
	// LoadAll loads the full Tenant configuration, the first time it will be loaded remotely,
	// and then it will be directly assembled from the cache layer
	LoadAll(ctx context.Context) (*Tenant, error)
	// Load loads the full Tenant configuration, the first time it will be loaded remotely,
	// and then it will be directly assembled from the cache layer
	Load(ctx context.Context, item ConfigItem) (*Tenant, error)
}

type ConfigWatcher

type ConfigWatcher interface {
	io.Closer
	// Subscribe subscribes to all changes of an event by EventType
	Subscribe(ctx context.Context, et EventType, c EventCallback) (context.CancelFunc, error)
}

type ConfigWatcherTest

type ConfigWatcherTest configWatcher

type ConfigWriter

type ConfigWriter interface {
	io.Closer
	// Import imports the configuration information of a tenant
	Import(ctx context.Context, cfg *Tenant) error
	// Write imports the configuration information of a tenant
	Write(ctx context.Context, item ConfigItem, cfg *Tenant) error
}

type Configuration

type Configuration struct {
	Spec `yaml:",inline"`
	Data *Data `validate:"required,structonly" yaml:"data" json:"data"`
}

Configuration represents an Arana configuration.

func Load

func Load(path string) (*Configuration, error)

Load loads the configuration from file path.

type Data

type Data struct {
	Tenants []*Tenant `validate:"required,dive" yaml:"tenants" json:"tenants"`
}

type DataRevision

type DataRevision interface {
	Revision() string
}

type DataSourceCluster

type DataSourceCluster struct {
	Name        string         `yaml:"name" json:"name"`
	Type        DataSourceType `yaml:"type" json:"type"`
	SqlMaxLimit int            `default:"-1" yaml:"sql_max_limit" json:"sql_max_limit,omitempty"`
	Parameters  ParametersMap  `yaml:"parameters" json:"parameters"`
	Groups      []*Group       `yaml:"groups" json:"groups"`
}

func (*DataSourceCluster) Diff

type DataSourceType

type DataSourceType string

DataSourceType is the data source type

const (
	DBMySQL      DataSourceType = "mysql"
	DBPostgreSQL DataSourceType = "postgresql"
)

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder decodes configuration.

func NewDecoder

func NewDecoder(reader io.Reader) *Decoder

NewDecoder creates a Decoder from a reader.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

type Event

type Event interface {
	Type() EventType
}

type EventCallback

type EventCallback func(e Event)

type EventType

type EventType int32
const (
	EventTypeTenants EventType
	EventTypeUsers
	EventTypeNodes
	EventTypeClusters
	EventTypeShardingRule
	EventTypeShadowRule
)

type Group

type Group struct {
	Name  string   `yaml:"name" json:"name"`
	Nodes []string `yaml:"nodes" json:"nodes"`
}

type GroupEvent

type GroupEvent struct {
	Name        string
	AddNodes    Nodes
	UpdateNodes Nodes
	DeleteNodes Nodes
}

GroupEvent group event

type Groups

type Groups []*Group

func (Groups) Diff

func (g Groups) Diff(old Groups) *GroupsEvent

type GroupsEvent

type GroupsEvent struct {
	AddGroups    Groups
	UpdateGroups Groups
	DeleteGroups Groups
}

GroupsEvent groups event

type Listener

type Listener struct {
	ProtocolType  string         `yaml:"protocol_type" json:"protocol_type"`
	SocketAddress *SocketAddress `yaml:"socket_address" json:"socket_address"`
	ServerVersion string         `yaml:"server_version" json:"server_version"`
}

func (*Listener) String

func (l *Listener) String() string

type MatchRule

type MatchRule struct {
	Operation  []string         `yaml:"operation" json:"operation"`
	MatchType  string           `yaml:"match_type" json:"match_type"`
	Attributes []*RuleAttribute `yaml:"attributes" json:"attributes"`
}

type Node

type Node struct {
	Name       string                 `validate:"required" yaml:"name" json:"name"`
	Host       string                 `validate:"required" yaml:"host" json:"host"`
	Port       int                    `validate:"required" yaml:"port" json:"port"`
	Username   string                 `validate:"required" yaml:"username" json:"username"`
	Password   string                 `validate:"required" yaml:"password" json:"password"`
	Database   string                 `validate:"required" yaml:"database" json:"database"`
	Parameters ParametersMap          `yaml:"parameters" json:"parameters"`
	ConnProps  map[string]interface{} `yaml:"conn_props" json:"conn_props,omitempty"`
	Weight     string                 `default:"r10w10" yaml:"weight" json:"weight"`
	Labels     map[string]string      `yaml:"labels" json:"labels,omitempty"`
}

func (*Node) Equals

func (nd *Node) Equals(o *Node) bool

func (*Node) GetReadAndWriteWeight

func (nd *Node) GetReadAndWriteWeight() (int, int, error)

func (*Node) String

func (nd *Node) String() string

type Nodes

type Nodes map[string]*Node

func (Nodes) Diff

func (n Nodes) Diff(old Nodes) *NodesEvent

type NodesEvent

type NodesEvent struct {
	AddNodes    []*Node
	UpdateNodes []*Node
	DeleteNodes []*Node
}

NodesEvent nodes event

func (NodesEvent) Type

func (e NodesEvent) Type() EventType

type Options

type Options struct {
	StoreName string                 `yaml:"name"`
	RootPath  string                 `yaml:"root_path"`
	Options   map[string]interface{} `yaml:"options"`
}

type ParametersMap

type ParametersMap map[string]string

func (*ParametersMap) Camel

func (pm *ParametersMap) Camel(name string) string

Camel underline to camel

func (*ParametersMap) LowerCaseFirstLetter

func (pm *ParametersMap) LowerCaseFirstLetter(str string) string

LowerCaseFirstLetter lowercase letter

func (*ParametersMap) Merge

func (pm *ParametersMap) Merge(parametersMap ParametersMap)

func (*ParametersMap) String

func (pm *ParametersMap) String() string

type PathInfo

type PathInfo struct {
	DefaultConfigSpecPath               PathKey
	DefaultTenantBaseConfigPath         PathKey
	DefaultConfigSysDBPath              PathKey
	DefaultConfigDataNodesPath          PathKey
	DefaultConfigDataUsersPath          PathKey
	DefaultConfigDataSourceClustersPath PathKey
	DefaultConfigDataShardingRulePath   PathKey
	DefaultConfigDataShadowRulePath     PathKey

	ConfigKeyMapping   map[PathKey]string
	ConfigEventMapping map[PathKey]EventType
	BuildEventMapping  map[EventType]func(pre, cur *Tenant) Event
	ConfigValSupplier  map[PathKey]func(cfg *Tenant) interface{}
}

func NewPathInfo

func NewPathInfo(tenant string) *PathInfo

type PathKey

type PathKey string

PathKey config path key type

var (
	DefaultRootPath    PathKey
	DefaultTenantsPath PathKey
)

type Prop

type Prop struct {
	Operation string `yaml:"operation" json:"operation"`
	Column    string `yaml:"column" json:"column"`
	Value     string `yaml:"value" json:"value"`
	Regex     string `yaml:"regex" json:"regex"`
}

type ProtocolType

type ProtocolType int32

ProtocolType protocol type enum

const (
	Http ProtocolType = iota
	MySQL
)

func (*ProtocolType) UnmarshalText

func (t *ProtocolType) UnmarshalText(text []byte) error

type Registry

type Registry struct {
	Enable   bool                   `yaml:"enable" json:"enable"`
	Name     string                 `yaml:"name" json:"name"`
	RootPath string                 `yaml:"root_path" json:"root_path"`
	Options  map[string]interface{} `yaml:"options" json:"options"`
}

type Rule

type Rule struct {
	Columns []*ColumnRule `validate:"required" yaml:"columns" json:"columns"`
	Type    string        `validate:"required" yaml:"type" json:"type"`
	Expr    string        `validate:"required" yaml:"expr" json:"expr"`
}

type RuleAttribute

type RuleAttribute struct {
	Column string `yaml:"column" json:"column"`
	Value  string `yaml:"value,omitempty" json:"value,omitempty"`
	Regex  string `yaml:"regex,omitempty" json:"regex,omitempty"`
}

type Rules

type Rules []*Rule

func (Rules) Equals

func (r Rules) Equals(o Rules) bool

type Sequence

type Sequence struct {
	Type   string            `yaml:"type" json:"type,omitempty"`
	Option map[string]string `yaml:"option" json:"option,omitempty"`
}

type ShadowRule

type ShadowRule struct {
	ShadowTables []*ShadowTable `yaml:"tables" json:"tables"`
}

func (*ShadowRule) Diff

func (s *ShadowRule) Diff(old *ShadowRule) *ShadowRuleEvent

type ShadowRuleEvent

type ShadowRuleEvent struct {
	AddTables    []*ShadowTable
	UpdateTables []*ShadowTable
	DeleteTables []*ShadowTable
}

ShadowRuleEvent shadow rule event

func (ShadowRuleEvent) Type

func (e ShadowRuleEvent) Type() EventType

type ShadowTable

type ShadowTable struct {
	Name       string       `yaml:"name" json:"name"`
	Enable     bool         `yaml:"enable" json:"enable"`
	GroupNode  string       `yaml:"group_node" json:"group_node"`
	MatchRules []*MatchRule `yaml:"match_rules" json:"match_rules"`
}

type ShardingRule

type ShardingRule struct {
	Tables []*Table `yaml:"tables" json:"tables"`
}

func (*ShardingRule) Diff

type ShardingRuleEvent

type ShardingRuleEvent struct {
	AddTables    []*Table
	UpdateTables []*Table
	DeleteTables []*Table
}

ShardingRuleEvent sharding rule event

func (ShardingRuleEvent) Type

func (e ShardingRuleEvent) Type() EventType

type SocketAddress

type SocketAddress struct {
	Address string `default:"0.0.0.0" yaml:"address" json:"address"`
	Port    int    `default:"13306" yaml:"port" json:"port"`
}

SocketAddress specify either a logical or physical address and port, which are used to tell server where to bind/listen, connect to upstream and find management servers

type Spec

type Spec struct {
	Kind                       string                 `yaml:"kind" json:"kind,omitempty"`
	APIVersion                 string                 `yaml:"apiVersion" json:"apiVersion,omitempty"`
	LogPath                    string                 `yaml:"log_path" json:"log_path,omitempty"`
	SlowLogPath                string                 `yaml:"slow_log_path" json:"slow_log_path,omitempty"`
	EnableLocalMathComputation bool                   `yaml:"enable_local_math_computation" json:"enable_local_math_computation,omitempty"`
	Metadata                   map[string]interface{} `yaml:"metadata" json:"metadata"`
}

type StoreOperator

type StoreOperator interface {
	io.Closer
	// Init plugin initialization
	Init(options map[string]interface{}) error
	// Save save a configuration data
	Save(key PathKey, val []byte) error
	// Get get a configuration
	Get(key PathKey) ([]byte, error)
	// Watch Monitor changes of the key
	Watch(key PathKey) (<-chan []byte, error)
	// Name plugin name
	Name() string
}

StoreOperator config storage related plugins

func GetStoreOperate

func GetStoreOperate() StoreOperator

type SubscribeResult

type SubscribeResult struct {
	EventChan <-chan Event
	Cancel    context.CancelFunc
}

type Table

type Table struct {
	Name           string            `validate:"required" yaml:"name" json:"name"`
	Sequence       *Sequence         `yaml:"sequence" json:"sequence"`
	DbRules        []*Rule           `yaml:"db_rules" json:"db_rules"`
	TblRules       []*Rule           `yaml:"tbl_rules" json:"tbl_rules"`
	Topology       *Topology         `validate:"required" yaml:"topology" json:"topology"`
	ShadowTopology *Topology         `yaml:"shadow_topology" json:"shadow_topology"`
	Attributes     map[string]string `yaml:"attributes" json:"attributes"`
}

func (*Table) Equals

func (t *Table) Equals(o *Table) bool

type Tenant

type Tenant struct {
	Spec
	Name               string               `validate:"required" yaml:"name" json:"name"`
	Users              []*User              `validate:"required" yaml:"users" json:"users"`
	SysDB              *Node                `validate:"required" yaml:"sys_db" json:"sys_db"`
	DataSourceClusters []*DataSourceCluster `validate:"required,dive" yaml:"clusters" json:"clusters"`
	ShardingRule       *ShardingRule        `validate:"required,dive" yaml:"sharding_rule,omitempty" json:"sharding_rule,omitempty"`
	ShadowRule         *ShadowRule          `yaml:"shadow_rule,omitempty" json:"shadow_rule,omitempty"`
	Nodes              map[string]*Node     `validate:"required" yaml:"nodes" json:"nodes"`
}

func NewEmptyTenant

func NewEmptyTenant() *Tenant

func (*Tenant) Empty

func (t *Tenant) Empty() bool

type TenantOperator

type TenantOperator interface {
	io.Closer

	// ListTenants lists all tenants.
	ListTenants() []string

	// CreateTenant creates tenant.
	CreateTenant(string) error

	// UpdateTenant update tenant.
	UpdateTenant(tenant string, newTenant string) error

	// RemoveTenant removes tenant.
	RemoveTenant(string) error

	// RemoveTenantUser removes a tenant user.
	RemoveTenantUser(tenant, username string) error

	// CreateTenantUser creates a user.
	CreateTenantUser(tenant, username, password string) error

	// UpdateTenantUser update user by username.
	UpdateTenantUser(tenant, newUsername, password, oldUsername string) error

	// UpsertNode creates a node, or updates a node.
	UpsertNode(tenant, node, name, host string, port int, username, password, database, weight string) error

	// RemoveNode removes a node.
	RemoveNode(tenant, name string) error

	// Subscribe subscribes tenants change
	Subscribe(ctx context.Context, c EventCallback) context.CancelFunc
}

TenantOperator actions specific to tenant spaces

func LoadTenantOperator

func LoadTenantOperator(cfg *BootOptions) (TenantOperator, error)

LoadTenantOperator loads tenant operator from boot option.

func LoadTenantOperatorFromPath

func LoadTenantOperatorFromPath(path string) (TenantOperator, error)

LoadTenantOperatorFromPath loads tenant operator from specified file path.

func NewTenantOperator

func NewTenantOperator(op StoreOperator) (TenantOperator, error)

NewTenantOperator create a tenant data operator

type Tenants

type Tenants []string

func (Tenants) Diff

func (t Tenants) Diff(old Tenants) *TenantsEvent

type TenantsEvent

type TenantsEvent struct {
	AddTenants    Tenants
	DeleteTenants Tenants
}

TenantsEvent tenants event

func (TenantsEvent) Type

func (e TenantsEvent) Type() EventType

type Topology

type Topology struct {
	DbPattern  string `validate:"required" yaml:"db_pattern" json:"db_pattern"`
	TblPattern string `validate:"required" yaml:"tbl_pattern" json:"tbl_pattern"`
}

type Trace

type Trace struct {
	Type    string `default:"jaeger" yaml:"type" json:"type"`
	Address string `default:"http://localhost:14268/api/traces" yaml:"address" json:"address"`
}

Trace Distributed tracing configuration, which is used to configure the collector type and address

type User

type User struct {
	Username string `yaml:"username" json:"username"`
	Password string `yaml:"password" json:"password"`
}

func (*User) Equals

func (u *User) Equals(o *User) bool

type Users

type Users []*User

func (Users) Diff

func (u Users) Diff(old Users) *UsersEvent

type UsersEvent

type UsersEvent struct {
	AddUsers    Users
	UpdateUsers Users
	DeleteUsers Users
}

UsersEvent users event

func (UsersEvent) Type

func (e UsersEvent) Type() EventType

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL