kv

package
v1.16.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 19 Imported by: 7

Documentation

Index

Constants

View Source
const (
	// Primary is a role to use KV store primarily.
	Primary = role("primary")
	// Secondary is a role for KV store used by "multi" KV store.
	Secondary = role("secondary")
)

Variables

This section is empty.

Functions

func RegistererWithKVName added in v1.2.0

func RegistererWithKVName(reg prometheus.Registerer, name string) prometheus.Registerer

RegistererWithKVName wraps the provided Registerer with the KV name label. If a nil reg is provided, a nil registry is returned

Types

type Client

type Client interface {
	// List returns a list of keys under the given prefix. Returned keys will
	// include the prefix.
	List(ctx context.Context, prefix string) ([]string, error)

	// Get a specific key.  Will use a codec to deserialise key to appropriate type.
	// If the key does not exist, Get will return nil and no error.
	Get(ctx context.Context, key string) (interface{}, error)

	// Delete a specific key. Deletions are best-effort and no error will
	// be returned if the key does not exist.
	Delete(ctx context.Context, key string) error

	// CAS stands for Compare-And-Swap.  Will call provided callback f with the
	// current value of the key and allow callback to return a different value.
	// Will then attempt to atomically swap the current value for the new value.
	// If that doesn't succeed will try again - callback will be called again
	// with new value etc.  Guarantees that only a single concurrent CAS
	// succeeds.  Callback can return nil to indicate it is happy with existing
	// value.
	CAS(ctx context.Context, key string, f func(in interface{}) (out interface{}, retry bool, err error)) error

	// WatchKey calls f whenever the value stored under key changes.
	WatchKey(ctx context.Context, key string, f func(interface{}) bool)

	// WatchPrefix calls f whenever any value stored under prefix changes.
	WatchPrefix(ctx context.Context, prefix string, f func(string, interface{}) bool)

	// LastUpdateTime returns the time a key was last sync by the kv store
	LastUpdateTime(key string) time.Time
}

Client is a high-level client for key-value stores (such as Etcd and Consul) that exposes operations such as CAS and Watch which take callbacks. It also deals with serialisation by using a Codec and having a instance of the the desired type passed in to methods ala json.Unmarshal.

func NewClient

func NewClient(cfg Config, codec codec.Codec, reg prometheus.Registerer, logger log.Logger) (Client, error)

NewClient creates a new Client (consul, etcd or inmemory) based on the config, encodes and decodes data for storage using the codec.

func PrefixClient

func PrefixClient(client Client, prefix string) Client

PrefixClient takes a KVClient and forces a prefix on all its operations.

type Config

type Config struct {
	Store       string `yaml:"store"`
	Prefix      string `yaml:"prefix"`
	StoreConfig `yaml:",inline"`

	Mock Client `yaml:"-"`
}

Config is config for a KVStore currently used by ring and HA tracker, where store can be consul or inmemory.

func (*Config) RegisterFlagsWithPrefix

func (cfg *Config) RegisterFlagsWithPrefix(flagsPrefix, defaultPrefix string, f *flag.FlagSet)

RegisterFlagsWithPrefix adds the flags required to config this to the given FlagSet. If prefix is an empty string we will register consul flags with no prefix and the store flag with the prefix ring, so ring.store. For everything else we pass the prefix to the Consul flags. If prefix is not an empty string it should end with a period.

type MultiClient added in v0.6.0

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

MultiClient implements kv.Client by forwarding all API calls to primary client. Writes performed via CAS method are also (optionally) forwarded to secondary clients.

func NewMultiClient added in v0.6.0

func NewMultiClient(cfg MultiConfig, clients []kvclient, logger log.Logger, registerer prometheus.Registerer) *MultiClient

NewMultiClient creates new MultiClient with given KV Clients. First client in the slice is the primary client.

func (*MultiClient) CAS added in v0.6.0

func (m *MultiClient) CAS(ctx context.Context, key string, f func(in interface{}) (out interface{}, retry bool, err error)) error

CAS is a part of kv.Client interface.

func (*MultiClient) Delete added in v1.1.0

func (m *MultiClient) Delete(ctx context.Context, key string) error

Delete is a part of the kv.Client interface.

func (*MultiClient) Get added in v0.6.0

func (m *MultiClient) Get(ctx context.Context, key string) (interface{}, error)

Get is a part of kv.Client interface.

func (*MultiClient) LastUpdateTime added in v1.15.0

func (m *MultiClient) LastUpdateTime(key string) time.Time

func (*MultiClient) List added in v1.1.0

func (m *MultiClient) List(ctx context.Context, prefix string) ([]string, error)

List is a part of the kv.Client interface.

func (*MultiClient) WatchKey added in v0.6.0

func (m *MultiClient) WatchKey(ctx context.Context, key string, f func(interface{}) bool)

WatchKey is a part of kv.Client interface.

func (*MultiClient) WatchPrefix added in v0.6.0

func (m *MultiClient) WatchPrefix(ctx context.Context, prefix string, f func(string, interface{}) bool)

WatchPrefix is a part of kv.Client interface.

type MultiConfig added in v0.6.0

type MultiConfig struct {
	Primary   string `yaml:"primary"`
	Secondary string `yaml:"secondary"`

	MirrorEnabled bool          `yaml:"mirror_enabled"`
	MirrorTimeout time.Duration `yaml:"mirror_timeout"`

	// ConfigProvider returns channel with MultiRuntimeConfig updates.
	ConfigProvider func() <-chan MultiRuntimeConfig `yaml:"-"`
}

MultiConfig is a configuration for MultiClient.

func (*MultiConfig) RegisterFlagsWithPrefix added in v0.6.0

func (cfg *MultiConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix string)

RegisterFlagsWithPrefix registers flags with prefix.

type MultiRuntimeConfig added in v0.6.0

type MultiRuntimeConfig struct {
	// Primary store used by MultiClient. Can be updated in runtime to switch to a different store (eg. consul -> etcd,
	// or to gossip). Doing this allows nice migration between stores. Empty values are ignored.
	PrimaryStore string `yaml:"primary"`

	// Mirroring enabled or not. Nil = no change.
	Mirroring *bool `yaml:"mirror_enabled"`
}

MultiRuntimeConfig has values that can change in runtime (via overrides)

type StoreConfig added in v0.6.0

type StoreConfig struct {
	DynamoDB dynamodb.Config `yaml:"dynamodb"`
	Consul   consul.Config   `yaml:"consul"`
	Etcd     etcd.Config     `yaml:"etcd"`
	Multi    MultiConfig     `yaml:"multi"`

	// Function that returns memberlist.KV store to use. By using a function, we can delay
	// initialization of memberlist.KV until it is actually required.
	MemberlistKV func() (*memberlist.KV, error) `yaml:"-"`
}

StoreConfig is a configuration used for building single store client, either Consul, Etcd, Memberlist or MultiClient. It was extracted from Config to keep single-client config separate from final client-config (with all the wrappers)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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