chord

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound represents node not found error
	ErrNotFound = errors.New("NotFound")
	// ErrStabilizeNotCompleted represents stabilize process not completed error
	ErrStabilizeNotCompleted = errors.New("StabilizeNotCompleted")
	// ErrNodeUnavailable represents no node available error
	ErrNodeUnavailable = errors.New("NodeUnavailable")
	// ErrNoSuccessorAlive represents no successor available error
	ErrNoSuccessorAlive = errors.New("ErrNoSuccessorAlive")
)

Functions

This section is empty.

Types

type AliveStabilizer

type AliveStabilizer struct {
	Node *LocalNode
}

AliveStabilizer checks successor status. If this stabilizer detects successors dead, remove them from a successor list of a local node.

func NewAliveStabilizer

func NewAliveStabilizer(node *LocalNode) AliveStabilizer

NewAliveStabilizer creates an alive stabilizer

func (AliveStabilizer) Stabilize

func (a AliveStabilizer) Stabilize(ctx context.Context)

Stabilize is implemented for Stabilizer interface.

type Finger

type Finger struct {
	Index int
	ID    model.HashID
	Node  RingNode
}

Finger represents an element of routing table

func NewFinger

func NewFinger(id model.HashID, index int, successor RingNode) *Finger

NewFinger creates a finger. index is an order of finger table. node is this finger table's owner.

func NewFingerTable

func NewFingerTable(id model.HashID) []*Finger

NewFingerTable creates a finger table.

type FingerTableStabilizer

type FingerTableStabilizer struct {
	Node *LocalNode
	// contains filtered or unexported fields
}

FingerTableStabilizer maintains a finger table of a local node.

func NewFingerTableStabilizer

func NewFingerTableStabilizer(node *LocalNode) *FingerTableStabilizer

NewFingerTableStabilizer creates a finger table stabilizer.

func (*FingerTableStabilizer) Stabilize

func (s *FingerTableStabilizer) Stabilize(ctx context.Context)

Stabilize is implemented for Stabilizer interface.

type LocalNode

type LocalNode struct {
	*model.NodeRef
	// contains filtered or unexported fields
}

LocalNode represents local host node.

func NewLocalNode

func NewLocalNode(hostAndPort string) *LocalNode

NewLocalNode creates a local node.

func (*LocalNode) CreateRing

func (l *LocalNode) CreateRing()

func (*LocalNode) DeleteValue

func (l *LocalNode) DeleteValue(ctx context.Context, key *string) (bool, error)

func (*LocalNode) FindClosestPrecedingNode

func (l *LocalNode) FindClosestPrecedingNode(_ context.Context, id model.HashID) (RingNode, error)

func (*LocalNode) FindSuccessorByList

func (l *LocalNode) FindSuccessorByList(ctx context.Context, id model.HashID) (RingNode, error)

func (*LocalNode) FindSuccessorByTable

func (l *LocalNode) FindSuccessorByTable(ctx context.Context, id model.HashID) (RingNode, error)

func (*LocalNode) GetPredecessor

func (l *LocalNode) GetPredecessor(_ context.Context) (RingNode, error)

func (*LocalNode) GetSuccessors

func (l *LocalNode) GetSuccessors(_ context.Context) ([]RingNode, error)

func (*LocalNode) GetValue

func (l *LocalNode) GetValue(ctx context.Context, key *string) (*string, bool, error)

func (*LocalNode) JoinRing

func (l *LocalNode) JoinRing(ctx context.Context, existNode RingNode) error

func (*LocalNode) JoinSuccessors

func (l *LocalNode) JoinSuccessors(offset int, successors []RingNode)

func (*LocalNode) Notify

func (l *LocalNode) Notify(_ context.Context, node RingNode) error

func (*LocalNode) Ping

func (l *LocalNode) Ping(_ context.Context) error

func (*LocalNode) PutSuccessor

func (l *LocalNode) PutSuccessor(suc RingNode)

func (*LocalNode) PutValue

func (l *LocalNode) PutValue(_ context.Context, key *string, value *string) (bool, error)

func (*LocalNode) Reference

func (l *LocalNode) Reference() *model.NodeRef

func (*LocalNode) Shutdown

func (l *LocalNode) Shutdown()

type MockTransport

type MockTransport struct{}

MockTransport does nothing

func (*MockTransport) DeleteValueInnerRPC

func (m *MockTransport) DeleteValueInnerRPC(ctx context.Context, to *model.NodeRef, key *string) (bool, error)

func (*MockTransport) FindClosestPrecedingNodeRPC

func (m *MockTransport) FindClosestPrecedingNodeRPC(ctx context.Context, to *model.NodeRef, id model.HashID) (RingNode, error)

FindClosestPrecedingNodeRPC does nothing

func (*MockTransport) FindSuccessorByListRPC

func (m *MockTransport) FindSuccessorByListRPC(ctx context.Context, to *model.NodeRef, id model.HashID) (RingNode, error)

FindSuccessorByListRPC does nothing

func (*MockTransport) FindSuccessorByTableRPC

func (m *MockTransport) FindSuccessorByTableRPC(ctx context.Context, to *model.NodeRef, id model.HashID) (RingNode, error)

FindSuccessorByTableRPC does nothing

func (*MockTransport) GetValueInnerRPC

func (m *MockTransport) GetValueInnerRPC(ctx context.Context, to *model.NodeRef, key *string) (*string, bool, error)

func (*MockTransport) NotifyRPC

func (m *MockTransport) NotifyRPC(ctx context.Context, to *model.NodeRef, node *model.NodeRef) error

NotifyRPC does nothing

func (*MockTransport) PingRPC

func (m *MockTransport) PingRPC(ctx context.Context, to *model.NodeRef) error

PingRPC does nothing

func (*MockTransport) PredecessorRPC

func (m *MockTransport) PredecessorRPC(ctx context.Context, to *model.NodeRef) (RingNode, error)

PredecessorRPC does nothing

func (*MockTransport) PutValueInnerRPC

func (m *MockTransport) PutValueInnerRPC(ctx context.Context, to *model.NodeRef, key *string, value *string) (bool, error)

func (*MockTransport) Shutdown

func (m *MockTransport) Shutdown()

Shutdown does nothing

func (*MockTransport) SuccessorsRPC

func (m *MockTransport) SuccessorsRPC(ctx context.Context, to *model.NodeRef) ([]RingNode, error)

SuccessorsRPC does nothing

type Process

type Process struct {
	*LocalNode
	AliveStabilizer       Stabilizer
	SuccessorStabilizer   Stabilizer
	FingerTableStabilizer Stabilizer
	Transport             Transport
	IsShutdown            bool
	// contains filtered or unexported fields
}

Process represents chord process. Process manages a local node and some stabilizers.

func NewProcess

func NewProcess(localNode *LocalNode, transport Transport) *Process

NewProcess creates a process.

func (*Process) Shutdown

func (p *Process) Shutdown()

Shutdown stops process

func (*Process) Start

func (p *Process) Start(ctx context.Context, opts ...ProcessOptionFunc) error

Start starts a process. Creates or joins in chord ring and starts some stabilizers of a process.

type ProcessOptionFunc

type ProcessOptionFunc func(option *processOption)

ProcessOptionFunc is function to apply options to a process

func WithExistNode

func WithExistNode(node RingNode) ProcessOptionFunc

func WithStabilizeInterval

func WithStabilizeInterval(duration time.Duration) ProcessOptionFunc

type RemoteNode

type RemoteNode struct {
	*model.NodeRef
	Transport
}

RemoteNode represents remote nodes. To communicate with remote nodes, need to call rpc.

func (*RemoteNode) DeleteValue

func (r *RemoteNode) DeleteValue(ctx context.Context, key *string) (bool, error)

func (*RemoteNode) FindClosestPrecedingNode

func (r *RemoteNode) FindClosestPrecedingNode(ctx context.Context, id model.HashID) (RingNode, error)

func (*RemoteNode) FindSuccessorByList

func (r *RemoteNode) FindSuccessorByList(ctx context.Context, id model.HashID) (RingNode, error)

func (*RemoteNode) FindSuccessorByTable

func (r *RemoteNode) FindSuccessorByTable(ctx context.Context, id model.HashID) (RingNode, error)

func (*RemoteNode) GetPredecessor

func (r *RemoteNode) GetPredecessor(ctx context.Context) (RingNode, error)

func (*RemoteNode) GetSuccessors

func (r *RemoteNode) GetSuccessors(ctx context.Context) ([]RingNode, error)

func (*RemoteNode) GetValue

func (r *RemoteNode) GetValue(ctx context.Context, key *string) (*string, bool, error)

func (*RemoteNode) Notify

func (r *RemoteNode) Notify(ctx context.Context, node RingNode) error

func (*RemoteNode) Ping

func (r *RemoteNode) Ping(ctx context.Context) error

func (*RemoteNode) PutValue

func (r *RemoteNode) PutValue(ctx context.Context, key *string, value *string) (bool, error)

func (*RemoteNode) Reference

func (r *RemoteNode) Reference() *model.NodeRef

type RingNode

type RingNode interface {
	Ping(ctx context.Context) error
	Reference() *model.NodeRef
	GetSuccessors(ctx context.Context) ([]RingNode, error)
	GetPredecessor(ctx context.Context) (RingNode, error)
	FindSuccessorByTable(ctx context.Context, id model.HashID) (RingNode, error)
	FindSuccessorByList(ctx context.Context, id model.HashID) (RingNode, error)
	FindClosestPrecedingNode(ctx context.Context, id model.HashID) (RingNode, error)
	Notify(ctx context.Context, node RingNode) error
	PutValue(ctx context.Context, key *string, value *string) (bool, error)
	GetValue(ctx context.Context, key *string) (*string, bool, error)
	DeleteValue(ctx context.Context, key *string) (bool, error)
}

RingNode represents a node of Chord Ring

func NewRemoteNode

func NewRemoteNode(hostAndPort string, transport Transport) RingNode

type Stabilizer

type Stabilizer interface {
	Stabilize(ctx context.Context)
}

Stabilizer is a process that runs asynchronously in a single goroutine

type SuccessorStabilizer

type SuccessorStabilizer struct {
	Node *LocalNode
}

SuccessorStabilizer checks new successors. If this stabilizer finds new successor, adds a new one to a successor list of a local node. In addition, this notify a successor to check its predecessor.

func NewSuccessorStabilizer

func NewSuccessorStabilizer(node *LocalNode) SuccessorStabilizer

NewSuccessorStabilizer creates a successor stabilizer.

func (SuccessorStabilizer) Stabilize

func (s SuccessorStabilizer) Stabilize(ctx context.Context)

Stabilize is implemented for Stabilizer interface.

type Transport

type Transport interface {
	PingRPC(ctx context.Context, to *model.NodeRef) error
	SuccessorsRPC(ctx context.Context, to *model.NodeRef) ([]RingNode, error)
	PredecessorRPC(ctx context.Context, to *model.NodeRef) (RingNode, error)
	FindSuccessorByTableRPC(ctx context.Context, to *model.NodeRef, id model.HashID) (RingNode, error)
	FindSuccessorByListRPC(ctx context.Context, to *model.NodeRef, id model.HashID) (RingNode, error)
	FindClosestPrecedingNodeRPC(ctx context.Context, to *model.NodeRef, id model.HashID) (RingNode, error)
	NotifyRPC(ctx context.Context, to *model.NodeRef, node *model.NodeRef) error
	Shutdown()
	PutValueInnerRPC(ctx context.Context, to *model.NodeRef, key *string, value *string) (bool, error)
	GetValueInnerRPC(ctx context.Context, to *model.NodeRef, key *string) (*string, bool, error)
	DeleteValueInnerRPC(ctx context.Context, to *model.NodeRef, key *string) (bool, error)
}

Transport represents rpc to remote node

Jump to

Keyboard shortcuts

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