chord

package
v0.3.16-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2018 License: Apache-2.0, MIT Imports: 21 Imported by: 0

Documentation

Overview

This package is used to provide an implementation of the Chord network protocol.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareId

func CompareId(id1, id2 []byte) int

func CreateNet

func CreateNet() (*Ring, *TCPTransport, error)

Creat the ring

func JoinNet

func JoinNet() (*Ring, *TCPTransport, error)

Join the ring

Types

type BlackholeTransport

type BlackholeTransport struct {
}

BlackholeTransport is used to provide an implemenation of the Transport that does not actually do anything. Any operation will result in an error.

func (*BlackholeTransport) ClearPredecessor

func (*BlackholeTransport) ClearPredecessor(target, self *Vnode) error

func (*BlackholeTransport) FindSuccessors

func (*BlackholeTransport) FindSuccessors(vn *Vnode, n int, key []byte) ([]*Vnode, error)

func (*BlackholeTransport) GetPredecessor

func (*BlackholeTransport) GetPredecessor(vn *Vnode) (*Vnode, error)

func (*BlackholeTransport) ListVnodes

func (*BlackholeTransport) ListVnodes(host string) ([]*Vnode, error)

func (*BlackholeTransport) Notify

func (*BlackholeTransport) Notify(vn, self *Vnode) ([]*Vnode, error)

func (*BlackholeTransport) Ping

func (*BlackholeTransport) Ping(vn *Vnode) (bool, error)

func (*BlackholeTransport) Register

func (*BlackholeTransport) Register(v *Vnode, o VnodeRPC)

func (*BlackholeTransport) SkipSuccessor

func (*BlackholeTransport) SkipSuccessor(target, self *Vnode) error

type Config

type Config struct {
	Hostname      string           // Local host name
	NumVnodes     int              // Number of vnodes per physical node
	HashFunc      func() hash.Hash // Hash function to use
	StabilizeMin  time.Duration    // Minimum stabilization time
	StabilizeMax  time.Duration    // Maximum stabilization time
	NumSuccessors int              // Number of successors to maintain
	Delegate      Delegate         // Invoked to handle ring events

	SeedNodeAddr  string // Join a ring via a seed node
	JoinBlkHeight uint32 // Current BlockHeight when join ring
	// contains filtered or unexported fields
}

Configuration for Chord nodes

func DefaultConfig

func DefaultConfig(hostname string) *Config

Returns the default Ring configuration

type Delegate

type Delegate interface {
	NewPredecessor(local, remoteNew, remotePrev *Vnode)
	Leaving(local, pred, succ *Vnode)
	PredecessorLeaving(local, remote *Vnode)
	SuccessorLeaving(local, remote *Vnode)
	Shutdown()
}

Delegate to notify on ring events

type LocalTransport

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

LocalTransport is used to provides fast routing to Vnodes running locally using direct method calls. For any non-local vnodes, the request is passed on to another transport.

func (*LocalTransport) ClearPredecessor

func (lt *LocalTransport) ClearPredecessor(target, self *Vnode) error

func (*LocalTransport) Deregister

func (lt *LocalTransport) Deregister(v *Vnode)

func (*LocalTransport) FindSuccessors

func (lt *LocalTransport) FindSuccessors(vn *Vnode, n int, key []byte) ([]*Vnode, error)

func (*LocalTransport) GetPredecessor

func (lt *LocalTransport) GetPredecessor(vn *Vnode) (*Vnode, error)

func (*LocalTransport) ListVnodes

func (lt *LocalTransport) ListVnodes(host string) ([]*Vnode, error)

func (*LocalTransport) Notify

func (lt *LocalTransport) Notify(vn, self *Vnode) ([]*Vnode, error)

func (*LocalTransport) Ping

func (lt *LocalTransport) Ping(vn *Vnode) (bool, error)

func (*LocalTransport) Register

func (lt *LocalTransport) Register(v *Vnode, o VnodeRPC)

func (*LocalTransport) SkipSuccessor

func (lt *LocalTransport) SkipSuccessor(target, self *Vnode) error

type Ring

type Ring struct {
	Vnodes []*localVnode
	// contains filtered or unexported fields
}

Stores the state required for a Chord ring

func Create

func Create(conf *Config, trans Transport) (*Ring, error)

Creates a new Chord ring given the config and transport

func GetRing

func GetRing() *Ring

func Join

func Join(conf *Config, trans Transport, existing string) (*Ring, error)

Joins an existing Chord ring

func (*Ring) Distance

func (r *Ring) Distance(fromId, toId []byte) *big.Int

func (*Ring) DumpInfo

func (r *Ring) DumpInfo(finger bool)

func (*Ring) GetFirstVnode

func (r *Ring) GetFirstVnode() (*localVnode, error)

func (*Ring) GetPredecessor

func (r *Ring) GetPredecessor(key []byte) (*Vnode, error)

func (*Ring) Leave

func (r *Ring) Leave() error

Leaves a given Chord ring and shuts down the local vnodes

func (*Ring) Len

func (r *Ring) Len() int

Len is the number of vnodes

func (*Ring) Less

func (r *Ring) Less(i, j int) bool

Less returns whether the vnode with index i should sort before the vnode with index j.

func (*Ring) Lookup

func (r *Ring) Lookup(n int, key []byte) ([]*Vnode, error)

Does a key lookup for up to N successors of a key

func (*Ring) Shutdown

func (r *Ring) Shutdown()

Shutdown shuts down the local processes in a given Chord ring Blocks until all the vnodes terminate.

func (*Ring) Swap

func (r *Ring) Swap(i, j int)

Swap swaps the vnodes with indexes i and j.

func (*Ring) ToData

func (r *Ring) ToData() *RingData

ToData: Extract marshalable data from Ring struct

type RingData

type RingData struct {
	Conf   *configData
	Vnodes []*localVnodeData
}

RingData : Data of Ring for json.Marshal in API

type TCPTransport

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

TCPTransport provides a TCP based Chord transport layer. This allows Chord to be implemented over a network, instead of only using the LocalTransport. It is meant to be a simple implementation, optimizing for simplicity instead of performance. Messages are sent with a header frame, followed by a body frame. All data is encoded using the GOB format for simplicity.

Internally, there is 1 Goroutine listening for inbound connections, 1 Goroutine PER inbound connection.

func InitTCPTransport

func InitTCPTransport(listen string, timeout time.Duration) (*TCPTransport, error)

Creates a new TCP transport on the given listen address with the configured timeout duration.

func (*TCPTransport) ClearPredecessor

func (t *TCPTransport) ClearPredecessor(target, self *Vnode) error

Clears a predecessor if it matches a given vnode. Used to leave.

func (*TCPTransport) FindSuccessors

func (t *TCPTransport) FindSuccessors(vn *Vnode, n int, k []byte) ([]*Vnode, error)

Find a successor

func (*TCPTransport) GetPredecessor

func (t *TCPTransport) GetPredecessor(vn *Vnode) (*Vnode, error)

Request a nodes predecessor

func (*TCPTransport) ListVnodes

func (t *TCPTransport) ListVnodes(host string) ([]*Vnode, error)

Gets a list of the vnodes on the box

func (*TCPTransport) Notify

func (t *TCPTransport) Notify(target, self *Vnode) ([]*Vnode, error)

Notify our successor of ourselves

func (*TCPTransport) Ping

func (t *TCPTransport) Ping(vn *Vnode) (bool, error)

Ping a Vnode, check for liveness

func (*TCPTransport) Register

func (t *TCPTransport) Register(v *Vnode, o VnodeRPC)

Register for an RPC callbacks

func (*TCPTransport) Shutdown

func (t *TCPTransport) Shutdown()

Shutdown the TCP transport

func (*TCPTransport) SkipSuccessor

func (t *TCPTransport) SkipSuccessor(target, self *Vnode) error

Instructs a node to skip a given successor. Used to leave.

type Transport

type Transport interface {
	// Gets a list of the vnodes on the box
	ListVnodes(string) ([]*Vnode, error)

	// Ping a Vnode, check for liveness
	Ping(*Vnode) (bool, error)

	// Request a nodes predecessor
	GetPredecessor(*Vnode) (*Vnode, error)

	// Notify our successor of ourselves
	Notify(target, self *Vnode) ([]*Vnode, error)

	// Find a successor
	FindSuccessors(*Vnode, int, []byte) ([]*Vnode, error)

	// Clears a predecessor if it matches a given vnode. Used to leave.
	ClearPredecessor(target, self *Vnode) error

	// Instructs a node to skip a given successor. Used to leave.
	SkipSuccessor(target, self *Vnode) error

	// Register for an RPC callbacks
	Register(*Vnode, VnodeRPC)
}

Implements the methods needed for a Chord ring

func InitLocalTransport

func InitLocalTransport(remote Transport) Transport

Creates a local transport to wrap a remote transport

type Vnode

type Vnode struct {
	Id         []byte // Virtual ID
	Host       string // Chord Host identifier
	NodePort   uint16 // Node port
	HttpWsPort uint16 // Websocket port
}

Represents an Vnode, local or remote

func (*Vnode) HttpWsAddr

func (vn *Vnode) HttpWsAddr() (string, error)

func (*Vnode) NodeAddr

func (vn *Vnode) NodeAddr() (string, error)

func (*Vnode) String

func (vn *Vnode) String() string

Converts the ID to Hex string

func (*Vnode) ToData

func (vn *Vnode) ToData() *VnodeData

type VnodeData

type VnodeData struct {
	Id         string // Virtual ID of Hex String
	Host       string // Chord Host identifier
	NodePort   uint16 // Node port
	HttpWsPort uint16 // Websocket port
}

type VnodeRPC

type VnodeRPC interface {
	GetPredecessor() (*Vnode, error)
	Notify(*Vnode) ([]*Vnode, error)
	FindSuccessors(int, []byte) ([]*Vnode, error)
	ClearPredecessor(*Vnode) error
	SkipSuccessor(*Vnode) error
}

These are the methods to invoke on the registered vnodes

Jump to

Keyboard shortcuts

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