curator

package module
v0.0.0-...-10905f8 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2016 License: MIT Imports: 10 Imported by: 0

README

go-curator

Travis Build Status Coverage Status Go Report Card

The go-curator framework is a high-level API that simplifies using ZooKeeper in GO, by building features on top of go-zookeeper. At this time it is not the goal of go-curator to be in feature parity with the java project of the same namesake (curator)

Documentation

Index

Constants

View Source
const MaxWaitToExistTime = 2 * time.Minute

Variables

View Source
var ErrConnectionTimedOut = errors.New("connection to zookeeper timed out")

ErrConnectionTimedOut occurs when initial connection attempt to zk fails

View Source
var ErrInvalidPath = errors.New("provided path is invalid")

ErrInvalidPath occurs when the provided path in zk is malformed

View Source
var ErrNodeNotInCache = errors.New("node not in cache")
View Source
var ErrWatchStopped = errors.New("the watch was stopped")

ErrWatchStopped occurs when two calls to StopWatching are performed

Functions

func SpewableWrapper

func SpewableWrapper(config *spew.ConfigState, a ...interface{}) string

Types

type ChildCache

type ChildCache struct {
	CreateFlags int32
	Path        string
	// contains filtered or unexported fields
}

ChildCache provides a cached list, whose nodes are the children of a single path within zookeeper. In other words, all the items in the cache would share the same parent.

For example, supposing a zookeeper structure as follows:

/services/birdhouse/work /services/birdhouse/work/123 /services/birdhouse/work/124 /services/birdhouse/work/125 /services/birdhouse/work/126

ChildCache provides operations on /services/birdhouse/work path to manipulate its children: {123,124,125,126}.

The underlying list is stored as a map. Just like a filesystem structure in unix, the children of a znode have unique names.

Some notes:

* Does not observe state changes in zk * Thread safe * Does not require or use channels

func NewChildCache

func NewChildCache(client *Client, path string) *ChildCache

func (*ChildCache) Add

func (l *ChildCache) Add(nodes ...*Znode) (err error)

Add new nodes to underlying store, only if the node does not already exist in cache. If the underlying node exists in Zk, its loaded into the cache (and not considered an error). It's not considered an error to add a node that is already in the cache.

func (*ChildCache) Cache

func (l *ChildCache) Cache() (nodesMap map[string]Znode)

Shallow copy of cache that can be used for RO operations.

func (*ChildCache) Clear

func (l *ChildCache) Clear() (err error)

Clear all nodes from underlying store, but only if the node exists in the cache.

func (*ChildCache) Contains

func (l *ChildCache) Contains(name string) bool

Determine if a node exists by node name.

func (*ChildCache) Get

func (l *ChildCache) Get(name string) (node *Znode)

Retrieves a node from the cache if it exists, nil otherwise.

func (*ChildCache) LoadCache

func (l *ChildCache) LoadCache() (err error)

Wipes local cache and reloads it directly from zk.

func (*ChildCache) Remove

func (l *ChildCache) Remove(nodes ...*Znode) (err error)

Delete nodes from underlying store, only if the node already exists in the cache. If the node does not exist in the cache, an error is returned.

func (*ChildCache) Size

func (l *ChildCache) Size() int

Number of nodes in the cache.

func (*ChildCache) ToSlice

func (l *ChildCache) ToSlice() (nodes []Znode)

List of children nodes for the path provided in the constructor.

type ChildWatch

type ChildWatch struct {
	Path                string
	MaxRetryElapsedTime time.Duration
	RetrieveData        bool
	// contains filtered or unexported fields
}

ChildWatch observes the children of a path in zookeeper for changes and maintains a cached list of those nodes. Only changes in the children are tracked, not changes in the data of the children. Some advantages over the children watch in go-zookeeper include:

* tracks removals and additions for you in an easy to use map * automatically restarts the watch when child watch events occur * xmits events with nodes observed to have been added and/or removed * returns data as proper Znode structures instead of a string * stops a watch somewhat more cleanly than go-zookeeper * can automatically retrieve child data (use carefully as a list of size n would introduce n calls to retrieve data from zookeeper)

The children are stored as a map, with the Znode name as the key, and the Znode as the value.

A ChildWatch is not re-usable and can only be started and stopped once. If you need a to re-use a ChildWatch, create a new one with the same path. Due to limitations in go-zookeeper a created ChildWatch will not free its goroutine until a zookeeper connection or child watch event occurs.

func NewChildWatch

func NewChildWatch(client *Client, path string) *ChildWatch

NewChildWatch creates a watch on the provided path.

func (*ChildWatch) GetChildren

func (w *ChildWatch) GetChildren() map[string]Znode

GetChildren returns a copy of all child Znodes of Path. Note that after you get the list it could immediately change. Using the channel returned by WatchChildren is usually what you want.

func (*ChildWatch) StopWatching

func (w *ChildWatch) StopWatching() (err error)

StopWatching is a bit tricky because go-zookeeper doesn't cleanup child watches nicely. In fact, its for this reason that a ChildWatch can only be started and stopped once. When a ChildWatch is stopped the goroutine can't exit until a watch event from zookeeper is received (again a limitation imposed by go-zookeeper)

func (*ChildWatch) WatchChildren

func (w *ChildWatch) WatchChildren() (chan Event, error)

WatchChildren starts watching the specified path and loads the intial set of items.

type Client

type Client struct {
	*zk.Conn
	// contains filtered or unexported fields
}

Client connects to and interacts with zk.

func NewClient

func NewClient() *Client

NewClient creates a client that can interact with zk

func (*Client) Connect

func (c *Client) Connect(settings *Settings, options ...zk.ConnOption) (evnt <-chan zk.Event, err error)

Connect creates a connection to zookeeper for the client

func (*Client) CreatePath

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

CreatePath will create the full path in zookeeper (emulates 'mkdir -p'). Each node will be assigned the same data and acl permissions. Only non-ephemeral nodes can have children.

The path parameter must begin with '/'

func (*Client) WaitToExist

func (c *Client) WaitToExist(path string, maxWaitTime time.Duration) (err error)

type Curator

type Curator struct {
	Client   *Client
	Settings *Settings
	// contains filtered or unexported fields
}

func NewCurator

func NewCurator(client *Client, settings *Settings, plugins []Plugin) *Curator

func (*Curator) AllPlugins

func (c *Curator) AllPlugins() (plugins []Plugin)

func (*Curator) ClearPlugins

func (c *Curator) ClearPlugins()

func (*Curator) FindPlugin

func (c *Curator) FindPlugin(f func(int, Plugin) bool) (index int, plugin Plugin)

func (*Curator) FireEvent

func (c *Curator) FireEvent(event Event)

func (*Curator) LoadPlugin

func (c *Curator) LoadPlugin(plugin Plugin)

func (*Curator) LogEntry

func (c *Curator) LogEntry(suffix string) *logrus.Entry

func (*Curator) Logger

func (c *Curator) Logger() *logrus.Logger

func (*Curator) Start

func (c *Curator) Start() (err error)

func (*Curator) Stop

func (c *Curator) Stop() (err error)

func (*Curator) UnloadPlugin

func (c *Curator) UnloadPlugin(plugin Plugin)

type Event

type Event struct {
	Type   EventType
	Node   *Znode
	Source *zk.Event
	Error  error
	Data   map[string]interface{}
}

Event represents an event that occurs within the components in curator

func NewEvent

func NewEvent(event EventType, node *Znode, err error) *Event

NewEvent creates a new event for the most common cases

func (Event) DeepCopy

func (e Event) DeepCopy() (ecopy *Event)

DeepCopy makes a safe copy of the event

func (Event) IsConnectedEvent

func (e Event) IsConnectedEvent() (connected bool)

IsConnectedEvent is a shortcut for determing if the event is a ConnectionEvent and represents an underlying zk.StateHasSession

func (Event) IsDisconnectedEvent

func (e Event) IsDisconnectedEvent() (connected bool)

IsDisConnectedEvent is a shortcut for determing if the event is a ConnectionEvent and represents anything except an underlying zk.StateHasSession (which means we are not reliably connected and ready to process typical zk API requests)

func (Event) IsValidSessionEvent

func (e Event) IsValidSessionEvent() (yes bool)

IsValidSessionEvent determines if the event type is a ConnectionEvent and there is a valid and associated zk event of type zk.EventSession.

type EventType

type EventType uint64

EventType describes the type of event that occured

const (
	AnyEvent EventType = 1 << iota
	ConnectionEvent
	ChildrenWatchLoadedEvent
	ChildrenWatchChangedEvent
	ChildrenWatchStoppedEvent
	LeaderEventElected
	LeaderEventCandidate
	LeaderEventResigned
	MemberEventRegistered
	MemberEventUnregistered
	DiscoveryEventActive
	DiscoveryEventInactive
	WorkCollectorEventChangeset
	WorkCollectorEventLoaded
	WorkLeaderChangeset
	WorkLeaderActive
	WorkLeaderInactive
)

Limited to 64 event types

func (EventType) String

func (e EventType) String() (name string)

type NullLogger

type NullLogger struct{}

NullLogger can be used to silence output from the client connection. Only recommended for tests.

func (NullLogger) Printf

func (NullLogger) Printf(format string, a ...interface{})

Printf is the only method that is part of the connection logger interface

type Plugin

type Plugin interface {
	Name() string
	Accepts(eventType EventType) bool
	Notify(event Event)
	OnLoad(curator *Curator)
	OnUnload()
}

type Settings

type Settings struct {
	ZkServers               []string
	ZkLogger                zk.Logger
	ZkSessionTimeout        time.Duration
	ZkWaitForSessionTimeout time.Duration
	ZkWaitForSession        bool
	RootPath                string
	Logger                  *logrus.Logger
	LogComponent            string
}

type Spewable

type Spewable interface {
	Spew() string
}

type Znode

type Znode struct {
	Name string
	Path string
	Data []byte
	Stat *zk.Stat
}

Znode is an in memory only representation of a Zookeeper node.

func NewZnode

func NewZnode(path string) *Znode

NewZnode creates a in-memory representation of a znode given the provided path.

func (Znode) Basename

func (n Znode) Basename() string

Basename returns the leaf name of this Znode

func (Znode) DeepCopy

func (n Znode) DeepCopy() (zcopy *Znode)

DeepCopy creates a deepcopy of n

func (Znode) Parent

func (n Znode) Parent() string

Parent returns the name of this Znodes parent

func (Znode) Spew

func (n Znode) Spew() string

Spew implements the Spewable interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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