raftrpc

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

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

Go to latest
Published: Mar 11, 2016 License: Apache-2.0 Imports: 23 Imported by: 0

README

RaftRPC: Simple RPC Key Value Server using etcd/Raft in Golang

GoDoc Build Status

What is Raft RPC Server/Client

This is a simple KV Server (Key/Value) server using raft consensus algorithm. (implement by CoreOS/etcd).

It provide a basic RPC Client/Server for K/V(Key Value) storage service.

What is Raft

Raft is a consensus algorithm that is designed to be easy to understand. It's equivalent to Paxos in fault-tolerance and performance. The difference is that it's decomposed into relatively independent subproblems, and it cleanly addresses all major pieces needed for practical systems. We hope Raft will make consensus available to a wider audience, and that this wider audience will be able to develop a variety of higher quality consensus-based systems than are available today. (quote from here)

How to use etcd/raft in your project

  1. Refer code from raftexample
  2. Get file listener.go, kvstore.go, raft.go.
  3. Do your modification for your usage.
note
  • raft.transport need an extra http port for raft message exchange. MUST add this in your code. (which is peer info in example code)

Installation and Usage

Install

go get github.com/kkdai/raftrpc

Usage

Server Example(1) Single Server:
package main
    
import (
	"fmt"
    
	. "github.com/kkdai/raftrpc"
)
    
func main() {
	forever := make(chan int)

	//RPC addr
	rpcAddr := "127.0.0.1:1234"
	srv := StartServer(rpcAddr, 1)

	<-forever
}
Server Example(2) Cluster Server:
package main
    
import (
	"fmt"
    
	. "github.com/kkdai/raftrpc"
)
    
func main() {
	forever := make(chan int)
	
	//Note there are two address and port.
	//
	// "127.0.0.1:1234" is RPC access point
	// "http://127.0.0.1:12379" is raft message access point which use http
	
	var raftMsgSrvList []string
	raftMsgSrvList = append(raftMsgSrvList, "http://127.0.0.1:12379")
	raftMsgSrvList = append(raftMsgSrvList, "http://127.0.0.1:22379")

	srv1 := StartClusterServers("127.0.0.1:1234", 1, raftMsgSrvList)
	srv2 := StartClusterServers("127.0.0.1:1235", 2, raftMsgSrvList)
	
	<-forever
}
Client Example

Assume a server exist on 127.0.0.1:1234.

package main
    
import (
	"fmt"
    
	. "github.com/kkdai/raftrpc"
)
    
func main() {
	client := MakeClerk("127.0.0.1:1234")
	client.Put("t1", "v1")
	log.Println("got:", ret)
	if ret != "v1" {
		log.Println("Client get error:", ret)
	}
}	

Inspired By

Project52

It is one of my project 52.

License

etcd is under the Apache 2.0 license. See the LICENSE file for details.

Documentation

Overview

Package raftrpc provides simple rpc server with K/V value with diskqueue.

Index

Constants

View Source
const (
	OK           = "OK"
	ErrNoKey     = "ErrNoKey"
	InvalidParam = "Invalid Parameter"
)
View Source
const Debug = 1

Variables

This section is empty.

Functions

func DPrintf

func DPrintf(format string, a ...interface{}) (n int, err error)

Types

type Clerk

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

func MakeClerk

func MakeClerk(server string) *Clerk

func (*Clerk) Get

func (ck *Clerk) Get(key string) string

Get fetch the current value for a key.

func (*Clerk) Put

func (ck *Clerk) Put(key string, value string)

Put

type Err

type Err string

type GetArgs

type GetArgs struct {
	Key string
}

type GetReply

type GetReply struct {
	Err   Err
	Value string
}

type KVRaft

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

func StarServerJoinCluster

func StarServerJoinCluster(rpcPort string, me int) *KVRaft

func StartClusterServers

func StartClusterServers(rpcPort string, me int, cluster []string) *KVRaft

func StartServer

func StartServer(rpcPort string, me int) *KVRaft

func (*KVRaft) Get

func (kv *KVRaft) Get(args *GetArgs, reply *GetReply) error

func (*KVRaft) Put

func (kv *KVRaft) Put(args *PutArgs, reply *PutReply) error

type PutArgs

type PutArgs struct {
	Key   string
	Value string
}

type PutReply

type PutReply struct {
	Err           Err
	PreviousValue string
}

Jump to

Keyboard shortcuts

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