proton

package module
v0.0.0-...-a20db7a Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2016 License: Apache-2.0 Imports: 16 Imported by: 0

README

proton

Small library for experimenting with etcd's Raft using gRPC as a communication layer.

Usage

See the example folder and example/proton/init.go as well as example/proton/join.go for examples of usage.

TODO

  • Provide a better abstraction
  • Implement Store interface with a boltdb backend

Documentation

Overview

Package proton is a generated protocol buffer package.

It is generated from these files:

proton.proto

It has these top-level messages:

JoinRaftResponse
LeaveRaftResponse
SendResponse
PutObjectRequest
PutObjectResponse
ListObjectsRequest
ListObjectsResponse
ListMembersRequest
ListMembersResponse
NodeInfo
Pair

Index

Constants

View Source
const (
	// MaxRetryTime is the number of time we try to initiate
	// a grpc connection to a remote raft member
	MaxRetryTime = 3
)

Variables

View Source
var (

	// ErrConnectionRefused is thrown when a connection is refused to a node member in the raft
	ErrConnectionRefused = errors.New("connection refused to the node")
	// ErrConfChangeRefused is thrown when there is an issue with the configuration change
	ErrConfChangeRefused = errors.New("propose configuration change refused")
	// ErrApplyNotSpecified is thrown during the creation of a raft node when no apply method was provided
	ErrApplyNotSpecified = errors.New("apply method was not specified")
)
View Source
var (
	ErrInvalidLengthProton = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowProton   = fmt.Errorf("proto: integer overflow")
)

Functions

func DefaultNodeConfig

func DefaultNodeConfig() *raft.Config

DefaultNodeConfig returns the default config for a raft node that can be modified and customized

func EncodePair

func EncodePair(key string, value []byte) ([]byte, error)

EncodePair returns a protobuf encoded key/value pair to be sent through raft

func GenID

func GenID(hostname string) uint64

GenID generate an id for a raft node given a hostname.

FIXME there is a high chance of id collision

func Register

func Register(server *grpc.Server, node *Node)

Register registers the node raft server

func RegisterRaftServer

func RegisterRaftServer(s *grpc.Server, srv RaftServer)

Types

type ApplyCommand

type ApplyCommand func(interface{})

ApplyCommand function can be used and triggered every time there is an append entry event

type Cluster

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

Cluster represents a set of active raft members

func NewCluster

func NewCluster() *Cluster

NewCluster creates a new cluster neighbors list for a raft member

func (*Cluster) AddPeer

func (c *Cluster) AddPeer(peer *Peer)

AddPeer adds a node to our neighbors

func (*Cluster) Peers

func (c *Cluster) Peers() map[uint64]*Peer

Peers returns the list of peers in the cluster

func (*Cluster) RemovePeer

func (c *Cluster) RemovePeer(id uint64)

RemovePeer removes a node from our neighbors

type JoinRaftResponse

type JoinRaftResponse struct {
	Success bool        `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
	Error   string      `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
	Nodes   []*NodeInfo `protobuf:"bytes,3,rep,name=nodes" json:"nodes,omitempty"`
}

func (*JoinRaftResponse) GetNodes

func (m *JoinRaftResponse) GetNodes() []*NodeInfo

func (*JoinRaftResponse) Marshal

func (m *JoinRaftResponse) Marshal() (data []byte, err error)

func (*JoinRaftResponse) MarshalTo

func (m *JoinRaftResponse) MarshalTo(data []byte) (int, error)

func (*JoinRaftResponse) ProtoMessage

func (*JoinRaftResponse) ProtoMessage()

func (*JoinRaftResponse) Reset

func (m *JoinRaftResponse) Reset()

func (*JoinRaftResponse) Size

func (m *JoinRaftResponse) Size() (n int)

func (*JoinRaftResponse) String

func (m *JoinRaftResponse) String() string

func (*JoinRaftResponse) Unmarshal

func (m *JoinRaftResponse) Unmarshal(data []byte) error

type LeaveRaftResponse

type LeaveRaftResponse struct {
	Success bool   `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
	Error   string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
}

func (*LeaveRaftResponse) Marshal

func (m *LeaveRaftResponse) Marshal() (data []byte, err error)

func (*LeaveRaftResponse) MarshalTo

func (m *LeaveRaftResponse) MarshalTo(data []byte) (int, error)

func (*LeaveRaftResponse) ProtoMessage

func (*LeaveRaftResponse) ProtoMessage()

func (*LeaveRaftResponse) Reset

func (m *LeaveRaftResponse) Reset()

func (*LeaveRaftResponse) Size

func (m *LeaveRaftResponse) Size() (n int)

func (*LeaveRaftResponse) String

func (m *LeaveRaftResponse) String() string

func (*LeaveRaftResponse) Unmarshal

func (m *LeaveRaftResponse) Unmarshal(data []byte) error

type ListMembersRequest

type ListMembersRequest struct {
}

func (*ListMembersRequest) Marshal

func (m *ListMembersRequest) Marshal() (data []byte, err error)

func (*ListMembersRequest) MarshalTo

func (m *ListMembersRequest) MarshalTo(data []byte) (int, error)

func (*ListMembersRequest) ProtoMessage

func (*ListMembersRequest) ProtoMessage()

func (*ListMembersRequest) Reset

func (m *ListMembersRequest) Reset()

func (*ListMembersRequest) Size

func (m *ListMembersRequest) Size() (n int)

func (*ListMembersRequest) String

func (m *ListMembersRequest) String() string

func (*ListMembersRequest) Unmarshal

func (m *ListMembersRequest) Unmarshal(data []byte) error

type ListMembersResponse

type ListMembersResponse struct {
	Members []*NodeInfo `protobuf:"bytes,1,rep,name=members" json:"members,omitempty"`
}

func (*ListMembersResponse) GetMembers

func (m *ListMembersResponse) GetMembers() []*NodeInfo

func (*ListMembersResponse) Marshal

func (m *ListMembersResponse) Marshal() (data []byte, err error)

func (*ListMembersResponse) MarshalTo

func (m *ListMembersResponse) MarshalTo(data []byte) (int, error)

func (*ListMembersResponse) ProtoMessage

func (*ListMembersResponse) ProtoMessage()

func (*ListMembersResponse) Reset

func (m *ListMembersResponse) Reset()

func (*ListMembersResponse) Size

func (m *ListMembersResponse) Size() (n int)

func (*ListMembersResponse) String

func (m *ListMembersResponse) String() string

func (*ListMembersResponse) Unmarshal

func (m *ListMembersResponse) Unmarshal(data []byte) error

type ListObjectsRequest

type ListObjectsRequest struct {
}

func (*ListObjectsRequest) Marshal

func (m *ListObjectsRequest) Marshal() (data []byte, err error)

func (*ListObjectsRequest) MarshalTo

func (m *ListObjectsRequest) MarshalTo(data []byte) (int, error)

func (*ListObjectsRequest) ProtoMessage

func (*ListObjectsRequest) ProtoMessage()

func (*ListObjectsRequest) Reset

func (m *ListObjectsRequest) Reset()

func (*ListObjectsRequest) Size

func (m *ListObjectsRequest) Size() (n int)

func (*ListObjectsRequest) String

func (m *ListObjectsRequest) String() string

func (*ListObjectsRequest) Unmarshal

func (m *ListObjectsRequest) Unmarshal(data []byte) error

type ListObjectsResponse

type ListObjectsResponse struct {
	Objects []*Pair `protobuf:"bytes,1,rep,name=objects" json:"objects,omitempty"`
}

func (*ListObjectsResponse) GetObjects

func (m *ListObjectsResponse) GetObjects() []*Pair

func (*ListObjectsResponse) Marshal

func (m *ListObjectsResponse) Marshal() (data []byte, err error)

func (*ListObjectsResponse) MarshalTo

func (m *ListObjectsResponse) MarshalTo(data []byte) (int, error)

func (*ListObjectsResponse) ProtoMessage

func (*ListObjectsResponse) ProtoMessage()

func (*ListObjectsResponse) Reset

func (m *ListObjectsResponse) Reset()

func (*ListObjectsResponse) Size

func (m *ListObjectsResponse) Size() (n int)

func (*ListObjectsResponse) String

func (m *ListObjectsResponse) String() string

func (*ListObjectsResponse) Unmarshal

func (m *ListObjectsResponse) Unmarshal(data []byte) error

type Node

type Node struct {
	raft.Node

	Client   *Raft
	Cluster  *Cluster
	Server   *grpc.Server
	Listener net.Listener
	Ctx      context.Context

	ID      uint64
	Address string
	Port    int
	Error   error

	PStore map[string]string
	Store  *raft.MemoryStorage
	Cfg    *raft.Config
	// contains filtered or unexported fields
}

Node represents the Raft Node useful configuration.

func NewNode

func NewNode(id uint64, addr string, cfg *raft.Config, apply ApplyCommand) (*Node, error)

NewNode generates a new Raft node based on an unique ID, an address and optionally: a handler and receive only channel to send event when an entry is committed to the logs

func (*Node) Get

func (n *Node) Get(key string) string

Get returns a value from the PStore

func (*Node) IsLeader

func (n *Node) IsLeader() bool

IsLeader checks if we are the leader or not

func (*Node) IsPaused

func (n *Node) IsPaused() bool

IsPaused checks if a node is paused or not

func (*Node) JoinRaft

func (n *Node) JoinRaft(ctx context.Context, info *NodeInfo) (*JoinRaftResponse, error)

JoinRaft sends a configuration change to nodes to add a new member to the raft cluster

func (*Node) Leader

func (n *Node) Leader() uint64

Leader returns the id of the leader

func (*Node) LeaveRaft

func (n *Node) LeaveRaft(ctx context.Context, info *NodeInfo) (*LeaveRaftResponse, error)

LeaveRaft sends a configuration change for a node that is willing to abandon its raft cluster membership

func (*Node) ListMembers

func (n *Node) ListMembers(ctx context.Context, req *ListMembersRequest) (*ListMembersResponse, error)

ListMembers lists the members in the raft cluster

func (*Node) ListObjects

func (n *Node) ListObjects(ctx context.Context, req *ListObjectsRequest) (*ListObjectsResponse, error)

ListObjects list the objects in the raft cluster

func (*Node) ListPairs

func (n *Node) ListPairs() []*Pair

List lists the pair in the store

func (*Node) Pause

func (n *Node) Pause()

Pause pauses the raft node

func (*Node) Put

func (n *Node) Put(key string, value string)

Put puts a value in the raft store

func (*Node) PutObject

func (n *Node) PutObject(ctx context.Context, req *PutObjectRequest) (*PutObjectResponse, error)

Put proposes and puts a value in the raft cluster

func (*Node) RegisterNode

func (n *Node) RegisterNode(node *NodeInfo) error

RegisterNode registers a new node on the cluster

func (*Node) RegisterNodes

func (n *Node) RegisterNodes(nodes []*NodeInfo) (err error)

RegisterNodes registers a set of nodes in the cluster

func (*Node) RemoveNode

func (n *Node) RemoveNode(node *Peer) error

RemoveNode removes a node from the raft cluster

func (*Node) Resume

func (n *Node) Resume()

Resume brings back the raft node to activity

func (*Node) Send

func (n *Node) Send(ctx context.Context, msg *raftpb.Message) (*SendResponse, error)

Send calls 'Step' which advances the raft state machine with the received message

func (*Node) SetPaused

func (n *Node) SetPaused(pause bool)

SetPaused sets the switch for the pause mode

func (*Node) Shutdown

func (n *Node) Shutdown()

Shutdown stops the raft node processing loop. Calling Shutdown on an already stopped node will result in a deadlock

func (*Node) Start

func (n *Node) Start()

Start is the main loop for a Raft node, it goes along the state machine, acting on the messages received from other Raft nodes in the cluster

func (*Node) StoreLength

func (n *Node) StoreLength() int

StoreLength returns the length of the store

func (*Node) UnregisterNode

func (n *Node) UnregisterNode(id uint64)

UnregisterNode unregisters a node that has died or has gracefully left the raft subsystem

type NodeInfo

type NodeInfo struct {
	ID    uint64 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"`
	Addr  string `protobuf:"bytes,2,opt,name=Addr,proto3" json:"Addr,omitempty"`
	Port  string `protobuf:"bytes,3,opt,name=Port,proto3" json:"Port,omitempty"`
	Error string `protobuf:"bytes,4,opt,name=Error,proto3" json:"Error,omitempty"`
}

func (*NodeInfo) Marshal

func (m *NodeInfo) Marshal() (data []byte, err error)

func (*NodeInfo) MarshalTo

func (m *NodeInfo) MarshalTo(data []byte) (int, error)

func (*NodeInfo) ProtoMessage

func (*NodeInfo) ProtoMessage()

func (*NodeInfo) Reset

func (m *NodeInfo) Reset()

func (*NodeInfo) Size

func (m *NodeInfo) Size() (n int)

func (*NodeInfo) String

func (m *NodeInfo) String() string

func (*NodeInfo) Unmarshal

func (m *NodeInfo) Unmarshal(data []byte) error

type Pair

type Pair struct {
	Key   string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
	Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
}

func (*Pair) Marshal

func (m *Pair) Marshal() (data []byte, err error)

func (*Pair) MarshalTo

func (m *Pair) MarshalTo(data []byte) (int, error)

func (*Pair) ProtoMessage

func (*Pair) ProtoMessage()

func (*Pair) Reset

func (m *Pair) Reset()

func (*Pair) Size

func (m *Pair) Size() (n int)

func (*Pair) String

func (m *Pair) String() string

func (*Pair) Unmarshal

func (m *Pair) Unmarshal(data []byte) error

type Peer

type Peer struct {
	*NodeInfo

	Client *Raft
}

Peer represents a raft cluster peer

type PutObjectRequest

type PutObjectRequest struct {
	Object *Pair `protobuf:"bytes,1,opt,name=object" json:"object,omitempty"`
}

func (*PutObjectRequest) GetObject

func (m *PutObjectRequest) GetObject() *Pair

func (*PutObjectRequest) Marshal

func (m *PutObjectRequest) Marshal() (data []byte, err error)

func (*PutObjectRequest) MarshalTo

func (m *PutObjectRequest) MarshalTo(data []byte) (int, error)

func (*PutObjectRequest) ProtoMessage

func (*PutObjectRequest) ProtoMessage()

func (*PutObjectRequest) Reset

func (m *PutObjectRequest) Reset()

func (*PutObjectRequest) Size

func (m *PutObjectRequest) Size() (n int)

func (*PutObjectRequest) String

func (m *PutObjectRequest) String() string

func (*PutObjectRequest) Unmarshal

func (m *PutObjectRequest) Unmarshal(data []byte) error

type PutObjectResponse

type PutObjectResponse struct {
	Success bool   `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
	Error   string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
}

func (*PutObjectResponse) Marshal

func (m *PutObjectResponse) Marshal() (data []byte, err error)

func (*PutObjectResponse) MarshalTo

func (m *PutObjectResponse) MarshalTo(data []byte) (int, error)

func (*PutObjectResponse) ProtoMessage

func (*PutObjectResponse) ProtoMessage()

func (*PutObjectResponse) Reset

func (m *PutObjectResponse) Reset()

func (*PutObjectResponse) Size

func (m *PutObjectResponse) Size() (n int)

func (*PutObjectResponse) String

func (m *PutObjectResponse) String() string

func (*PutObjectResponse) Unmarshal

func (m *PutObjectResponse) Unmarshal(data []byte) error

type Raft

type Raft struct {
	RaftClient
	Conn *grpc.ClientConn
}

Raft represents a connection to a raft member

func GetRaftClient

func GetRaftClient(addr string, timeout time.Duration) (*Raft, error)

GetRaftClient returns a raft client object to communicate with other raft members

type RaftClient

type RaftClient interface {
	JoinRaft(ctx context.Context, in *NodeInfo, opts ...grpc.CallOption) (*JoinRaftResponse, error)
	LeaveRaft(ctx context.Context, in *NodeInfo, opts ...grpc.CallOption) (*LeaveRaftResponse, error)
	Send(ctx context.Context, in *raftpb.Message, opts ...grpc.CallOption) (*SendResponse, error)
	PutObject(ctx context.Context, in *PutObjectRequest, opts ...grpc.CallOption) (*PutObjectResponse, error)
	ListObjects(ctx context.Context, in *ListObjectsRequest, opts ...grpc.CallOption) (*ListObjectsResponse, error)
	ListMembers(ctx context.Context, in *ListMembersRequest, opts ...grpc.CallOption) (*ListMembersResponse, error)
}

func NewRaftClient

func NewRaftClient(cc *grpc.ClientConn) RaftClient

type SendResponse

type SendResponse struct {
	Success bool   `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
	Error   string `protobuf:"bytes,2,opt,name=error,proto3" json:"error,omitempty"`
}

func (*SendResponse) Marshal

func (m *SendResponse) Marshal() (data []byte, err error)

func (*SendResponse) MarshalTo

func (m *SendResponse) MarshalTo(data []byte) (int, error)

func (*SendResponse) ProtoMessage

func (*SendResponse) ProtoMessage()

func (*SendResponse) Reset

func (m *SendResponse) Reset()

func (*SendResponse) Size

func (m *SendResponse) Size() (n int)

func (*SendResponse) String

func (m *SendResponse) String() string

func (*SendResponse) Unmarshal

func (m *SendResponse) Unmarshal(data []byte) error

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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