cluster

package
v0.0.0-...-9de1cbb Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2020 License: MIT Imports: 11 Imported by: 3

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/lodastack/store/cluster"
)

func main() {
	opts := cluster.Options{
		// store bind TCP listen
		Bind: "127.0.0.1:9000",
		// store data dir
		DataDir: "/tmp/store",
		// any node in exist cluster
		JoinAddr: "10.0.0.1:9000",
	}

	cs, err := cluster.NewService(opts)
	if err != nil {
		fmt.Printf("new store service failed: %s", err)
	}

	if err := cs.Open(); err != nil {
		fmt.Printf("failed to open cluster service failed: %s", err)
	}

	// If join was specified, make the join request.
	nodes, err := cs.Nodes()
	if err != nil {
		fmt.Printf("get nodes failed: %s", err)
	}

	// if exist a raftdb, or exist a cluster, don't join any leader.
	if opts.JoinAddr != "" && len(nodes) <= 1 {
		if err := cs.JoinCluster(opts.JoinAddr, opts.Bind); err != nil {
			fmt.Printf("failed to join node at %s: %s", opts.JoinAddr, err)
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Bind address to use for the cluster raft service.
	Bind string

	// DataDir is the directory where the data stores.
	DataDir string

	// JoinAddr, which cluster to join.
	// Optional.
	JoinAddr string

	// Logger logs store status.
	// Default write to stdout.
	// Optional.
	Logger log.Logger
}

Options are the options to be used when initializing a cluster service.

type Service

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

Service allows access to the cluster and associated meta data, via distributed consensus.

func NewService

func NewService(opts Options) (*Service, error)

NewService returns a new instance of the cluster service.

func (*Service) Addr

func (s *Service) Addr() string

Addr returns the address the service is listening on.

func (*Service) Backup

func (s *Service) Backup() ([]byte, error)

Backup database.

func (*Service) Batch

func (s *Service) Batch(rows []model.Row) error

Batch update values for given keys in given buckets, via distributed consensus.

func (*Service) Close

func (s *Service) Close() error

Close closes the service.

func (*Service) CreateBucket

func (s *Service) CreateBucket(name []byte) error

CreateBucket will create bucket via the cluster.

func (*Service) CreateBucketIfNotExist

func (s *Service) CreateBucketIfNotExist(name []byte) error

CreateBucketIfNotExist will create bucket via the cluster if not exist.

func (*Service) DelSession

func (s *Service) DelSession(key interface{}) error

DelSession delete the session from given key.

func (*Service) GetSession

func (s *Service) GetSession(key interface{}) interface{}

GetSession returns the session value for the given key.

func (*Service) Join

func (s *Service) Join(addr string) error

Join joins the node, reachable at addr, to the cluster.

func (*Service) JoinCluster

func (s *Service) JoinCluster(server, addr string) error

JoinCluster joins the cluster via TCP, reachable at addr, to the cluster.

func (*Service) Nodes

func (s *Service) Nodes() ([]string, error)

Nodes returns the list of current peers.

func (*Service) Open

func (s *Service) Open() error

Open opens the Service.

func (*Service) Peers

func (s *Service) Peers() (map[string]map[string]string, error)

Peers returns cluster roles.

func (*Service) PublishAPIAddr

func (s *Service) PublishAPIAddr(apiAddr string, delay time.Duration, timeout time.Duration) error

PublishAPIAddr public API addr in cluster

func (*Service) Remove

func (s *Service) Remove(addr string) error

Remove removes a node from the store, specified by addr.

func (*Service) RemoveBucket

func (s *Service) RemoveBucket(name []byte) error

RemoveBucket will remove bucket via the cluster.

func (*Service) RemoveKey

func (s *Service) RemoveKey(bucket, key []byte) error

RemoveKey removes the key from the bucket.

func (*Service) Restore

func (s *Service) Restore(Backupfile string) error

Restore database.

func (*Service) SetPeer

func (s *Service) SetPeer(raftAddr, apiAddr string) error

SetPeer will set the mapping between raftAddr and apiAddr for the entire cluster.

func (*Service) SetSession

func (s *Service) SetSession(key, value interface{}) error

SetSession set the session.

func (*Service) Statistics

func (s *Service) Statistics(tags map[string]string) []model.Statistic

Statistics returns statistics for periodic monitoring.

func (*Service) Update

func (s *Service) Update(bucket []byte, key []byte, value []byte) error

Update will update the value of the given key in bucket via the cluster.

func (*Service) View

func (s *Service) View(bucket, key []byte) ([]byte, error)

View returns the value for the given key.

func (*Service) ViewPrefix

func (s *Service) ViewPrefix(bucket, keyPrefix []byte) (map[string][]byte, error)

ViewPrefix returns the value for the keys has the keyPrefix.

func (*Service) WaitForLeader

func (s *Service) WaitForLeader(timeout time.Duration) (string, error)

WaitForLeader blocks until a leader is detected, or the timeout expires.

type Store

type Store interface {
	// Leader returns the leader of the consensus system.
	Leader() string

	// Join joins the node, reachable at addr, to the cluster.
	Join(addr string) error

	// Remove removes a node from the store, specified by addr.
	Remove(addr string) error

	// UpdateAPIPeers updates the API peers on the store.
	UpdateAPIPeers(peers map[string]string) error

	// Create a bucket, via distributed consensus.
	CreateBucket(name []byte) error

	// Create a bucket via distributed consensus if not exist.
	CreateBucketIfNotExist(name []byte) error

	// Remove a bucket, via distributed consensus.
	RemoveBucket(name []byte) error

	// Get returns the value for the given key.
	View(bucket, key []byte) ([]byte, error)

	// ViewPrefix returns the value for the keys has the keyPrefix.
	ViewPrefix(bucket, keyPrefix []byte) (map[string][]byte, error)

	// Set sets the value for the given key, via distributed consensus.
	Update(bucket []byte, key []byte, value []byte) error

	// RemoveKey removes the key from the bucket.
	RemoveKey(bucket, key []byte) error

	// Batch update values for given keys in given buckets, via distributed consensus.
	Batch(rows []model.Row) error

	// GetSession returns the sression value for the given key.
	GetSession(key interface{}) interface{}

	// SetSession sets the value for the given key, via distributed consensus.
	SetSession(key, value interface{}) error

	// DelSession delete the value for the given key, via distributed consensus.
	DelSession(key interface{}) error

	// Backup database.
	Backup() ([]byte, error)

	// Restore restores backup data file.
	Restore(backupfile string) error

	// APIPeers return the map of Raft addresses to API addresses.
	APIPeers() (map[string]string, error)

	// Nodes returns the list of current peers.
	Nodes() ([]string, error)

	// WaitForLeader blocks until a leader is detected, or the timeout expires.
	WaitForLeader(timeout time.Duration) (string, error)

	// Close closes the store. If wait is true, waits for a graceful shutdown.
	Close(wait bool) error

	// Statistics returns statistics for periodic monitoring.
	Statistics(tags map[string]string) []model.Statistic
}

Store represents a store of information, managed via consensus.

type Transport

type Transport interface {
	net.Listener

	// Dial is used to create a new outgoing connection.
	Dial(address string, timeout time.Duration) (net.Conn, error)
}

Transport is the interface the network service must provide.

Jump to

Keyboard shortcuts

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