ichord

package module
v0.0.0-...-17b887b Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2019 License: Apache-2.0 Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ichord

Implement chrod algorithm

Status

Working in process: 40%

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

func Debugf

func Debugf(format string, v ...interface{})

func DefaultConfig

func DefaultConfig()

func Equal

func Equal(a, b UUID) bool

Returns true iff NodeID n == id

func Error

func Error(v ...interface{})

func Errorf

func Errorf(format string, v ...interface{})

func Greater

func Greater(a, b UUID) bool

Returns true iff NodeID n > id

func Less

func Less(a, b UUID) bool

Returns true iff NodeID n < id

func NewError

func NewError(err string) error

func Warn

func Warn(v ...interface{})

func Warnf

func Warnf(format string, v ...interface{})

Types

type Application

type Application interface {
	ID() UUID
	Startup(args *icfg.Config) error
	ShutDown() error
	// When a error occurs in the functionality of the DHT
	OnError(err error)

	// Receive a message intended for the self node
	OnDeliver(msg *Message)

	// Received a message that needs to be routed onwards
	OnForward(msg *Message, node *Node) bool // return False if chord should not forward

	// A new node has joined the network
	OnNodeJoin(node *Node)

	// A node has left the network
	OnNodeExit(node *Node)

	// Received a heartbeat signal from a peer
	OnHeartbeat(node *Node)
}

type Chord

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

func NewChord

func NewChord(self *Node, args map[string]interface{}) *Chord

Create a new Cluster

func (*Chord) CloseDB

func (c *Chord) CloseDB(dbName string)

func (*Chord) Count

func (c *Chord) Count() int

TODO Count calculate total count of nodes in the network

func (*Chord) DBDel

func (c *Chord) DBDel(dbName string, key []byte) error

func (*Chord) DBGet

func (c *Chord) DBGet(dbName string, key []byte) ([]byte, error)

func (*Chord) DBSet

func (c *Chord) DBSet(dbName string, key, value []byte) error

func (*Chord) Decode

func (c *Chord) Decode(dataBuf *bytes.Buffer, v interface{}) error

func (*Chord) Del

func (c *Chord) Del(key []byte) error

func (*Chord) DropDB

func (c *Chord) DropDB(dbName string)

func (*Chord) Encode

func (c *Chord) Encode(dataBuf *bytes.Buffer, v interface{}) error

func (*Chord) FindPredecessor

func (c *Chord) FindPredecessor(key UUID) (*Node, error)

func (*Chord) FindSuccessor

func (c *Chord) FindSuccessor(key UUID) (*Node, error)

func (*Chord) FixFingers

func (c *Chord) FixFingers()

func (*Chord) Get

func (c *Chord) Get(key []byte) ([]byte, error)

Set,Get,Del操作首先令k=hash(key), 生成maxCopies个hash, (k+i*maxHash/maxCopies) mod maxHash, 找出负责节点,去重,并存入 hashCircleCache里,过期时间从配置中读取

func (*Chord) InstallApp

func (c *Chord) InstallApp(cfg *icfg.Config, app Application) error

func (*Chord) Join

func (c *Chord) Join(nodes ...*Node) error

func (*Chord) Leave

func (c *Chord) Leave(nodes ...*Node) error

func (*Chord) List

func (c *Chord) List() ([]*Node, error)

func (*Chord) Listen

func (c *Chord) Listen(ln net.Listener) error

func (*Chord) Maintain

func (c *Chord) Maintain()

func (*Chord) Node

func (c *Chord) Node() Node

func (*Chord) Notify

func (c *Chord) Notify(n *Node) (*Message, error)

func (*Chord) Ping

func (c *Chord) Ping(node *Node) bool

func (*Chord) RegisterDB

func (c *Chord) RegisterDB(dbName string, db KvDb)

func (*Chord) Route

func (c *Chord) Route(key UUID) (*Node, error)

func (*Chord) SendMsg

func (c *Chord) SendMsg(msg *Message) (*Message, error)

SendMsg a message through the network to it's intended Node

func (*Chord) SendTo

func (c *Chord) SendTo(node *Node, data []byte) error

func (*Chord) Set

func (c *Chord) Set(key, value []byte) error

func (*Chord) SetDefaultDB

func (c *Chord) SetDefaultDB(db KvDb)

func (*Chord) ShowFingers

func (c *Chord) ShowFingers() []Finger

func (*Chord) Stabilize

func (c *Chord) Stabilize() error

func (*Chord) Sync

func (c *Chord) Sync()

TODO Sync reserved

func (*Chord) UninstallApp

func (c *Chord) UninstallApp(id UUID) error

type ChordAPI

type ChordAPI interface {
	//Encode and decode the message
	Encode(dataBuf *bytes.Buffer, v interface{}) error
	Decode(dataBuf *bytes.Buffer, v interface{}) error

	//Operate the default db, default is map
	Set(key, value []byte) error
	Get(key []byte) ([]byte, error)
	Del(key []byte) error

	//Set to a specify db
	DBSet(dbName string, key, value []byte) error
	// Get from a specify db
	DBGet(dbName string, key []byte) ([]byte, error)
	//Del from a specify db
	DBDel(dbName string, key []byte) error

	// set default db to replace the default map
	SetDefaultDB(db KvDb)
	//add new kv db
	RegisterDB(dbName string, db KvDb)
	//close a db and remove from list
	CloseDB(dbName string)
	//close a db and remove it, it's dangerous
	DropDB(dbName string)

	//Install an app to handle messages
	InstallApp(cfg *icfg.Config, app Application) error
	//Uninstall an app
	UninstallApp(id UUID) error

	Node() Node
	//Ping is used to detect a node is connectable or not
	Ping(node *Node) bool
	//SendTo a node with []byte format data
	SendTo(node *Node, data []byte) error
	//SendMsg a message and find receiver from the message
	SendMsg(msg *Message) (*Message, error)

	//Listen will hold on a listener and accept connections then handle it.
	Listen(l net.Listener) error
	// maintain will periodically perform maintenance operations
	Maintain()
	//Sync is used to check local data replications number periodically and if not then send that data to other nodes
	Sync()
	//Count statistics how many nodes in the network
	Count() int

	//Join will use self uuid to ask specify nodes or build-in public node to initialize node
	Join(node ...*Node) error
	//Leave tells the specify node or the public nodes I will offline, update network states and re-sync data
	Leave(node ...*Node) error
	//Route find the node that stored this key or should store this key
	Route(key UUID) (*Node, error)
	//FindSuccessor returns the first successor for the given key
	FindSuccessor(key UUID) (*Node, error)
	//FindPredecessor returns the first predecessor for the given key
	FindPredecessor(key UUID) (*Node, error)

	//List returns an array contains all stored nodes' info
	List() ([]*Node, error)
	//Stabilize successor/predecessor pointers
	Stabilize() error
	//Notify a Node of our existence
	Notify(n *Node) (*Message, error)
	//FixFingers table
	FixFingers()
	//ShowFingers table
	ShowFingers() []Finger
}

type Coder

type Coder interface {
	Name() string
	NewEncoder(io.Writer) Encoder
	NewDecoder(io.Reader) Decoder
}

A coder for coding ghord messages

func NewCoder

func NewCoder(t string) Coder

func NewGobCoder

func NewGobCoder() Coder

func NewJsonCoder

func NewJsonCoder() Coder

type ConnPool

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

func (*ConnPool) AddConn

func (cp *ConnPool) AddConn(id UUID, conn net.Conn) bool

func (*ConnPool) Close

func (cp *ConnPool) Close() (err error)

func (*ConnPool) DelConn

func (cp *ConnPool) DelConn(id UUID)

func (*ConnPool) ReadFrom

func (cp *ConnPool) ReadFrom(id UUID) (data []byte, err error)

func (*ConnPool) SRMsg

func (cp *ConnPool) SRMsg(id UUID, req *Message) (resp *Message, err error)

func (*ConnPool) SendTo

func (cp *ConnPool) SendTo(id UUID, data []byte) (n int, err error)

type Decoder

type Decoder interface {
	// Decode into the given interface, or error
	Decode(v interface{}) error
}

Standard decoder interface, usually created from an io.Reader Important: Needs to be stateless, in that each call to Decode() must act the same as any other call

type Encoder

type Encoder interface {
	// Encode given interface, or error
	Encode(v interface{}) error
}

Standard encoder interface, usually created from an io.Writer Important: Needs to be stateless, in that each call to Encode() must act the same as any other call

type Finger

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

type FingerTable

type FingerTable struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*FingerTable) ShowTable

func (ft *FingerTable) ShowTable() []Finger

type GobCoder

type GobCoder struct{}

func (*GobCoder) Name

func (g *GobCoder) Name() string

func (*GobCoder) NewDecoder

func (g *GobCoder) NewDecoder(r io.Reader) Decoder

func (*GobCoder) NewEncoder

func (g *GobCoder) NewEncoder(w io.Writer) Encoder

type JsonCoder

type JsonCoder struct{}

func (*JsonCoder) Name

func (c *JsonCoder) Name() string

func (*JsonCoder) NewDecoder

func (c *JsonCoder) NewDecoder(r io.Reader) Decoder

func (*JsonCoder) NewEncoder

func (c *JsonCoder) NewEncoder(w io.Writer) Encoder

type KvDb

type KvDb interface {
	Name() string
	Set(key, value []byte) error
	Get(key []byte) ([]byte, error)
	Del(key []byte) error
	Close() error
	Drop()
}

type LogApp

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

func NewLogApp

func NewLogApp() *LogApp

func (*LogApp) ID

func (la *LogApp) ID() UUID

func (*LogApp) OnDeliver

func (la *LogApp) OnDeliver(msg *Message)

func (*LogApp) OnError

func (la *LogApp) OnError(err error)

func (*LogApp) OnForward

func (la *LogApp) OnForward(msg *Message, node *Node) bool

func (*LogApp) OnHeartbeat

func (la *LogApp) OnHeartbeat(node *Node)

func (*LogApp) OnNodeExit

func (la *LogApp) OnNodeExit(node *Node)

func (*LogApp) OnNodeJoin

func (la *LogApp) OnNodeJoin(node *Node)

func (*LogApp) ShutDown

func (la *LogApp) ShutDown() error

func (*LogApp) Startup

func (la *LogApp) Startup(cfg *icfg.Config) error

type MemKvDb

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

func NewMemKvDb

func NewMemKvDb(dbName string) (mkd *MemKvDb)

func (*MemKvDb) Close

func (mkd *MemKvDb) Close() error

func (*MemKvDb) Del

func (mkd *MemKvDb) Del(key []byte) error

func (*MemKvDb) Drop

func (mkd *MemKvDb) Drop()

func (*MemKvDb) Get

func (mkd *MemKvDb) Get(key []byte) ([]byte, error)

func (*MemKvDb) Name

func (mkd *MemKvDb) Name() string

func (*MemKvDb) Set

func (mkd *MemKvDb) Set(key, value []byte) error

type Message

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

Represents a message in the DHT network

func NewMessage

func NewMessage(sender *Node, purpose Purpose, key UUID, body []byte) *Message

Create a new message

func (*Message) Body

func (msg *Message) Body() []byte

Ele the message body

func (*Message) DecodeBody

func (msg *Message) DecodeBody(coder Coder, v interface{}) error

Extract the message body into the given value (must be a pointer), using the provided coder

func (*Message) Hops

func (msg *Message) Hops() int

Ele the message hops taken

func (*Message) Key

func (msg *Message) Key() UUID

Ele the message key

func (*Message) Purpose

func (msg *Message) Purpose() Purpose

Ele the message purpose

func (*Message) Sender

func (msg *Message) Sender() Node

Ele the message sender node

func (*Message) String

func (msg *Message) String() string

func (*Message) Target

func (msg *Message) Target() *Node

Ele the message target node

type Node

type Node struct {
	Id   UUID
	Host string
	Port int
	// contains filtered or unexported fields
}

func NewNode

func NewNode(id UUID, host string, port int) *Node

func (*Node) IsResponsible

func (n *Node) IsResponsible(id UUID) bool

Returns true if this node is responsible for the given UUID

func (*Node) String

func (n *Node) String() string

type Purpose

type Purpose int
const (
	NodeJoin    Purpose // A node is joining the network
	NodeLeave           // A node is leaving the network
	HeartBeat           // Heartbeat signal
	NodeNotify          // Notified of node existence
	NodeAnn             // A node has been announced
	SucReq              // A request for a nodes successor
	PreReq              // A request for a nodes predecessor
	StatusError         // Response indicating an error
	StatusOk            // Simple status OK response
)

Message types

type UUID

type UUID = uuid.UUID

func NewID

func NewID() UUID

Directories

Path Synopsis
cmd
ichord command

Jump to

Keyboard shortcuts

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