planb

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

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

Go to latest
Published: Jan 24, 2018 License: Apache-2.0 Imports: 22 Imported by: 0

README

Plan B

GoDoc Build Status Go Report Card License

Plan B is a toolkit for building distributed, low-latency services that speak RESP (REdis Serialization Protocol). Under the hood, it is wrapping Redeo and Raft to create a concise interface for custom commands.

Examples

A simple server example:

package main

import (
  "fmt"

  "github.com/bsm/planb"
  "github.com/hashicorp/raft"

)

func main() {
	// Open a store
	store := planb.NewInmemStore()

	// Init config
	conf := planb.NewConfig()
	conf.Sentinel.MasterName = "mymaster"	// handle SENTINEL commands

	// Init server
	srv, err := planb.NewServer("10.0.0.1:7230", ".", store, raft.NewInmemStore(), raft.NewInmemStore(), conf)
	if err != nil {
		panic(err)
	}

	// Setup SET handler
	srv.HandleRW("SET", nil, redeo.WrapperFunc(func(cmd *resp.Command) interface{} {
		if len(cmd.Args) != 2 {
			return redeo.ErrWrongNumberOfArgs(cmd.Name)
		}

		if err := store.Put(cmd.Args[0], cmd.Args[1]); err != nil {
			return err
		}
		return "OK"
	}))

	// Setup GET handler
	srv.HandleRO("GET", nil, redeo.WrapperFunc(func(cmd *resp.Command) interface{} {
		if len(cmd.Args) != 1 {
			return redeo.ErrWrongNumberOfArgs(cmd.Name)
		}

		val, err := store.Get(cmd.Args[0])
		if err != nil {
			return err
		}
		return val
	}))

	// Start serving
	if err := srv.ListenAndServe(); err != nil {
		panic(err)
	}
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Raft configuration options
	Raft *raft.Config

	// Transport configuration options
	Transport *redeoraft.Config

	// Sentinel configuration
	Sentinel struct {
		// MasterName must be set to enable sentinel support
		MasterName string
	}
}

Config contains server config directives

func NewConfig

func NewConfig() *Config

NewConfig inits a default configuration

type HandlerOpts

type HandlerOpts struct {
	// Timeout is an optional timeout for mutating commands. It indicates
	// the maximum duration the server is willing to wait for the application
	// of the command. Minimum: 1s, default: 10s.
	Timeout time.Duration
}

HandlerOpts contain options for handler execution.

type InmemStore

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

func NewInmemStore

func NewInmemStore() *InmemStore

NewInmemStore opens a new simplistic, non-transactional in-memory KVStore

func (*InmemStore) Delete

func (s *InmemStore) Delete(key []byte) error

Delete deletes a key

func (*InmemStore) Get

func (s *InmemStore) Get(key []byte) ([]byte, error)

Get retrieves a key

func (*InmemStore) Put

func (s *InmemStore) Put(key, val []byte) error

Put sets a key

func (*InmemStore) Restore

func (s *InmemStore) Restore(r io.Reader) error

Restore implements Store

func (*InmemStore) Snapshot

func (s *InmemStore) Snapshot(w io.Writer) error

Snapshot implements Store

type RaftCtrl

type RaftCtrl interface {
	// AppliedIndex returns the last index applied to the FSM.
	AppliedIndex() uint64
	// GetConfiguration returns the latest configuration and its associated index currently in use.
	GetConfiguration() raft.ConfigurationFuture
	// LastContact returns the time of last contact by a leader.
	LastContact() time.Time
	// LastIndex returns the last index in stable storage, either from the last log or from the last snapshot.
	LastIndex() uint64
	// Leader is used to return the current leader of the cluster. It may return empty string if there is no current leader or the leader is unknown.
	Leader() raft.ServerAddress
	// State is used to return the current raft state.
	State() raft.RaftState
	// Stats is used to return a map of various internal stats.
	Stats() map[string]string
	// String returns a string representation of this Raft node.
	String() string
}

RaftCtrl is an interface to the underlying raft node controller

type Server

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

Server implements a peer

Example
package main

import (
	"github.com/bsm/planb"
	"github.com/bsm/redeo"
	"github.com/bsm/redeo/resp"
	"github.com/hashicorp/raft"
)

func main() {
	// Open a store
	store := planb.NewInmemStore()

	// Init config
	conf := planb.NewConfig()
	conf.Sentinel.MasterName = "mymaster" // handle SENTINEL commands

	// Init server
	srv, err := planb.NewServer("10.0.0.1:7230", ".", store, raft.NewInmemStore(), raft.NewInmemStore(), conf)
	if err != nil {
		panic(err)
	}

	// Setup SET handler
	srv.HandleRW("SET", nil, redeo.WrapperFunc(func(cmd *resp.Command) interface{} {
		if len(cmd.Args) != 2 {
			return redeo.ErrWrongNumberOfArgs(cmd.Name)
		}

		if err := store.Put(cmd.Args[0], cmd.Args[1]); err != nil {
			return err
		}
		return "OK"
	}))

	// Setup GET handler
	srv.HandleRO("GET", nil, redeo.WrapperFunc(func(cmd *resp.Command) interface{} {
		if len(cmd.Args) != 1 {
			return redeo.ErrWrongNumberOfArgs(cmd.Name)
		}

		val, err := store.Get(cmd.Args[0])
		if err != nil {
			return err
		}
		return val
	}))

	// Start serving
	if err := srv.ListenAndServe(); err != nil {
		panic(err)
	}
}
Output:

func NewServer

func NewServer(advertise raft.ServerAddress, dir string, store Store, logs raft.LogStore, stable raft.StableStore, conf *Config) (*Server, error)

NewServer initializes a new server instance. Each server must advertise an address and use a local dir location and a key-value store for persistence. It also accepts a log and a stable store.

func (*Server) Close

func (s *Server) Close() error

Close closes the server

func (*Server) HandleRO

func (s *Server) HandleRO(name string, opt *HandlerOpts, h redeo.Handler)

HandleRO handles readonly commands

func (*Server) HandleRW

func (s *Server) HandleRW(name string, opt *HandlerOpts, h redeo.Handler)

HandleRW handles commands that may result in modifications. These can only be applied to the master node and are then replicated to slaves.

func (*Server) Info

func (s *Server) Info() *redeo.ServerInfo

Info server info

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts listening and serving on the advertised address.

func (*Server) Raft

func (s *Server) Raft() RaftCtrl

Raft exposes the underlying raft node controller

func (*Server) Serve

func (s *Server) Serve(lis net.Listener) error

Serve starts serving in the given listener

type Store

type Store interface {
	// Restore restores the store from a data stream.
	Restore(r io.Reader) error
	// Snapshot writes a snapshot to the provided writer.
	Snapshot(w io.Writer) error
}

Store is an abstraction of an underlying store implementation. It must have snapshot and restore capabilities.

Jump to

Keyboard shortcuts

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