zk

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2017 License: Apache-2.0, MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultSessionTimeout is the time for ZK server to expire client session
	// ZK uses 20 times tickTime, which is set to 200 ms
	// in existing ZK cluster to ensure proper detection
	// of Helix node failures
	DefaultSessionTimeout = 4 * time.Second
	// FlagsZero is the default ZK data node flag
	FlagsZero = int32(0)
	// FlagsEphemeral is the ephemeral ZK data node flag
	FlagsEphemeral = int32(zk.FlagEphemeral)
)
View Source
const EmbeddedZkServer = "localhost:2181"

EmbeddedZkServer is the connect string for embedded ZK server

Variables

View Source
var (
	// ACLPermAll is the mode of ZK nodes where all users have permission to access
	ACLPermAll = zk.WorldACL(zk.PermAll)
)

Functions

func EnsureZookeeperUp

func EnsureZookeeperUp(scriptRelativeDirPath string) error

EnsureZookeeperUp starts the embedded (test) Zookeeper if not running.

func StopZookeeper

func StopZookeeper(scriptRelativeDirPath string) error

StopZookeeper stops the embedded (test) Zookeeper if running.

Types

type BaseZkTestSuite

type BaseZkTestSuite struct {
	suite.Suite

	EmbeddedZkPath  string
	ZkConnectString string
}

BaseZkTestSuite provides utility to test Zookeeper functions without Helix admin

func (*BaseZkTestSuite) CreateAndConnectClient

func (s *BaseZkTestSuite) CreateAndConnectClient() *Client

CreateAndConnectClient creates ZK client and connects to ZK server

func (*BaseZkTestSuite) SetupSuite

func (s *BaseZkTestSuite) SetupSuite()

SetupSuite ensures ZK server is up

type Client

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

Client wraps utils to communicate with ZK

func NewClient

func NewClient(logger *zap.Logger, scope tally.Scope, options ...ClientOption) *Client

NewClient returns new ZK client

func (*Client) AddWatcher

func (c *Client) AddWatcher(w Watcher)

AddWatcher adds a Watcher to zk session event

func (*Client) Children

func (c *Client) Children(path string) ([]string, error)

Children returns children of ZK path

func (*Client) ChildrenW

func (c *Client) ChildrenW(path string) ([]string, <-chan zk.Event, error)

ChildrenW gets children and watches path

func (*Client) ClearWatchers

func (c *Client) ClearWatchers()

ClearWatchers removes all the watchers the client has

func (*Client) Connect

func (c *Client) Connect() error

Connect sets up ZK connection

func (*Client) Create

func (c *Client) Create(path string, data []byte, flags int32, acl []zk.ACL) error

Create creates ZK path with data

func (*Client) CreateDataWithPath

func (c *Client) CreateDataWithPath(p string, data []byte) error

CreateDataWithPath creates a path with a string

func (*Client) CreateEmptyNode

func (c *Client) CreateEmptyNode(path string) error

CreateEmptyNode creates an empty node for future use

func (*Client) Delete

func (c *Client) Delete(path string) error

Delete removes ZK path

func (*Client) DeleteTree

func (c *Client) DeleteTree(path string) error

DeleteTree removes ZK path and its children

func (*Client) Disconnect

func (c *Client) Disconnect()

Disconnect closes ZK connection

func (*Client) Exists

func (c *Client) Exists(path string) (bool, *zk.Stat, error)

Exists checks if a key exists in ZK

func (*Client) ExistsAll

func (c *Client) ExistsAll(paths ...string) (bool, error)

ExistsAll returns if all paths exist

func (*Client) Get

func (c *Client) Get(path string) ([]byte, *zk.Stat, error)

Get returns data in ZK path

func (*Client) GetRecordFromPath

func (c *Client) GetRecordFromPath(path string) (*model.ZNRecord, error)

GetRecordFromPath returns message by ZK path

func (*Client) GetSessionID

func (c *Client) GetSessionID() string

GetSessionID returns current ZK session ID

func (*Client) GetSimpleFieldBool

func (c *Client) GetSimpleFieldBool(path string, key string) (bool, error)

GetSimpleFieldBool checks if value in path is "TRUE"

func (*Client) GetSimpleFieldValueByKey

func (c *Client) GetSimpleFieldValueByKey(path string, key string) (string, error)

GetSimpleFieldValueByKey returns value in simple field by key

func (*Client) GetW

func (c *Client) GetW(path string) ([]byte, <-chan zk.Event, error)

GetW returns data in ZK path and watches path

func (*Client) IsConnected

func (c *Client) IsConnected() bool

IsConnected returns if client is connected to Zookeeper

func (*Client) RemoveMapFieldKey

func (c *Client) RemoveMapFieldKey(path string, key string) error

RemoveMapFieldKey removes a map field by key

func (*Client) Set

func (c *Client) Set(path string, data []byte, version int32) error

Set sets data in ZK path

func (*Client) SetDataForPath

func (c *Client) SetDataForPath(path string, data []byte, version int32) error

SetDataForPath updates data at given ZK path

func (*Client) SetRecordForPath

func (c *Client) SetRecordForPath(path string, r *model.ZNRecord) error

SetRecordForPath sets a record in give ZK path

func (*Client) SetWithDefaultVersion

func (c *Client) SetWithDefaultVersion(path string, data []byte) error

SetWithDefaultVersion sets data with default version, -1

func (*Client) UpdateMapField

func (c *Client) UpdateMapField(path string, key string, property string, value string) error

UpdateMapField updates a map field for path key is the top-level key in the MapFields mapProperty is the inner key

Example:

mapFields":{

"partition_1":{
  "CURRENT_STATE":"OFFLINE",
  "INFO":""
}

To set the CURRENT_STATE to ONLINE, use UpdateMapField(

"/CLUSTER/INSTANCES/{instance}/CURRENT_STATE/{sessionID}/{db}",
"partition_1", "CURRENT_STATE", "ONLINE")

func (*Client) UpdateSimpleField

func (c *Client) UpdateSimpleField(path string, key string, value string) error

UpdateSimpleField updates a simple field

type ClientOption

type ClientOption func(*Client)

ClientOption provides options or ZK client

func WithConnFactory

func WithConnFactory(connFactory ConnFactory) ClientOption

WithConnFactory configures ConnFactory used to make ZK connections

func WithRetryTimeout

func WithRetryTimeout(t time.Duration) ClientOption

WithRetryTimeout configures retryTimeout for ZK operations

func WithSessionTimeout

func WithSessionTimeout(t time.Duration) ClientOption

WithSessionTimeout configures sessionTimeout

func WithZkSvr

func WithZkSvr(zkSvr string) ClientOption

WithZkSvr configures ZK servers for the client

type ConnFactory

type ConnFactory interface {
	NewConn() (Connection, <-chan zk.Event, error)
}

ConnFactory provides interface that creates ZK connections

func NewConnFactory

func NewConnFactory(zkServers []string, sessionTimeout time.Duration) ConnFactory

NewConnFactory creates new connFactory

type Connection

type Connection interface {
	AddAuth(scheme string, auth []byte) error
	Children(path string) ([]string, *zk.Stat, error)
	ChildrenW(path string) ([]string, *zk.Stat, <-chan zk.Event, error)
	Get(path string) ([]byte, *zk.Stat, error)
	GetW(path string) ([]byte, *zk.Stat, <-chan zk.Event, error)
	Exists(path string) (bool, *zk.Stat, error)
	ExistsW(path string) (bool, *zk.Stat, <-chan zk.Event, error)
	Set(path string, data []byte, version int32) (*zk.Stat, error)
	Create(path string, data []byte, flags int32, acl []zk.ACL) (string, error)
	Delete(path string, version int32) error
	Multi(ops ...interface{}) ([]zk.MultiResponse, error)
	SessionID() int64
	SetLogger(zk.Logger)
	State() zk.State
	Close()
}

Connection is the thread safe interface for ZK connection

type FakeZk

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

FakeZk provides utility to make fake connections and manipulate connection states

func NewFakeZk

func NewFakeZk(opts ...FakeZkOption) *FakeZk

NewFakeZk creates new FakeZk test utility

func (*FakeZk) GetConnections

func (z *FakeZk) GetConnections() []*FakeZkConn

GetConnections returns all of the connections FakeZk has made

func (*FakeZk) GetState

func (z *FakeZk) GetState(conn Connection) zk.State

GetState returns state by ZK connection

func (*FakeZk) NewConn

func (z *FakeZk) NewConn() (Connection, <-chan zk.Event, error)

NewConn makes new fake ZK connection

func (*FakeZk) SetState

func (z *FakeZk) SetState(conn Connection, state zk.State)

SetState sets state of ZK connection

type FakeZkConn

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

FakeZkConn is a fake ZK connection for testing

func NewFakeZkConn

func NewFakeZkConn(zk *FakeZk) *FakeZkConn

NewFakeZkConn creates a FakeZkConn

func (*FakeZkConn) AddAuth

func (c *FakeZkConn) AddAuth(scheme string, auth []byte) error

AddAuth addds auth info

func (*FakeZkConn) Children

func (c *FakeZkConn) Children(path string) ([]string, *zk.Stat, error)

Children returns children of a path

func (*FakeZkConn) ChildrenW

func (c *FakeZkConn) ChildrenW(path string) ([]string, *zk.Stat, <-chan zk.Event, error)

ChildrenW returns children and watcher channel of a path

func (*FakeZkConn) Close

func (c *FakeZkConn) Close()

Close closes the connection to ZK

func (*FakeZkConn) Create

func (c *FakeZkConn) Create(path string, data []byte, flags int32, acl []zk.ACL) (string, error)

Create creates new ZK node

func (*FakeZkConn) Delete

func (c *FakeZkConn) Delete(path string, version int32) error

Delete deletes ZK node

func (*FakeZkConn) Exists

func (c *FakeZkConn) Exists(path string) (bool, *zk.Stat, error)

Exists returns if the path exists

func (*FakeZkConn) ExistsW

func (c *FakeZkConn) ExistsW(path string) (bool, *zk.Stat, <-chan zk.Event, error)

ExistsW returns if path exists and watcher chan of path

func (*FakeZkConn) Get

func (c *FakeZkConn) Get(path string) ([]byte, *zk.Stat, error)

Get returns node by path

func (*FakeZkConn) GetHistory

func (c *FakeZkConn) GetHistory() *MethodCallHistory

GetHistory returns history

func (*FakeZkConn) GetW

func (c *FakeZkConn) GetW(path string) ([]byte, *zk.Stat, <-chan zk.Event, error)

GetW returns node and watcher channel of path

func (*FakeZkConn) Multi

func (c *FakeZkConn) Multi(ops ...interface{}) ([]zk.MultiResponse, error)

Multi executes multiple ZK operations

func (*FakeZkConn) SessionID

func (c *FakeZkConn) SessionID() int64

SessionID returns session ID

func (*FakeZkConn) Set

func (c *FakeZkConn) Set(path string, data []byte, version int32) (*zk.Stat, error)

Set sets data for path

func (*FakeZkConn) SetLogger

func (c *FakeZkConn) SetLogger(l zk.Logger)

SetLogger sets loggeer for the client

func (*FakeZkConn) State

func (c *FakeZkConn) State() zk.State

State returns state of the client

type FakeZkOption

type FakeZkOption func(*FakeZk)

FakeZkOption is the optional arg to create a FakeZk

func DefaultConnectionState

func DefaultConnectionState(state zk.State) FakeZkOption

DefaultConnectionState sets the default state when a fake connection is made

type MethodCall

type MethodCall struct {
	MethodName string
	Params     []interface{}
}

MethodCall represents a call record

type MethodCallHistory

type MethodCallHistory struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

MethodCallHistory represents the history of the method called on the connection

func (*MethodCallHistory) GetHistory

func (h *MethodCallHistory) GetHistory() []*MethodCall

GetHistory returns all of the histories

func (*MethodCallHistory) GetHistoryForMethod

func (h *MethodCallHistory) GetHistoryForMethod(method string) []*MethodCall

GetHistoryForMethod returns all of the histories of a method

type Watcher

type Watcher interface {
	Process(e zk.Event)
}

Watcher mirrors org.apache.zookeeper.Watcher

Jump to

Keyboard shortcuts

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