• This repository has been archived on 22/Dec/2020
  • Stars
    star
    247
  • Rank 163,157 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Raft Consensus for Node.js, backed by LevelDB

Skiff

Skiff

Raft Consensus Algorithm implementation for Node.js.

npm version Build Status

  • Persists to LevelDB (or any database exposing a LevelDown interface).
  • Exposes the cluster as a Levelup or Leveldown-compatible interface, with which you can extend using the Levelup plugins.
  • Encodes messages using Msgpack

Installation

$ npm install skiff --save

Usage

const Skiff = require('skiff')

const options = {
  db: require('memdown'), // in memory database
  peers: [ // peer addresses
    '/ip4/127.0.0.1/tcp/9491',
    '/ip4/127.0.0.1/tcp/9492'
  ]
}
const skiff = Skiff('/ip4/127.0.0.1/tcp/9490', options)

// expose the cluster as a Levelup-compatible database
const db = skiff.levelup()

skiff.start(err => {
  if (err) {
    console.error('Error starting skiff node: ', err.message)
  } else {
    console.log('Skiff node started')

    db.put('key', 'value', (err) => {
      // ...
    })
  }
})

API

Skiff (address, options)

Returns a new skiff node.

Arguments:

  • address (string, mandatory): an address in the multiaddr format (example: "/ip/127.0.0.1/tcp/5398").

  • options (object):

    • network (object): if you want to share the network with other skiff nodes on the same process, create a network using Skiff.createNetwork(options) (see below)
    • server (object):
      • port (integer):Β TCP port. Defaults to the port in address
      • host (string): host name to bind the server to. Defaults to the host name in the address
    • rpcTimeoutMS (integer, defaults to 2000): Timeout for RPC calls.
    • peers (array of strings, defaults to []): The addresses of the peers (also in the multiaddr format). If the database you're using is persisted to disk (which is the default), these peers will be overrridden by whatever is loaded from the latest snapshot once the node starts.
    • levelup (object): options to the internal Levelup database. Defaults to:
    {
      keyEncoding: 'utf8',
      valueEncoding: 'json'
    }
    • location (string): Location of the base directory for the leveldb files. Defaults to the data directory on the root of this package (not recommended)
    • db (function, defaults to Leveldown implementation): Database constructor, should return a Leveldown implementation.

(You can use this to create a in-memory database using Memdown)

  • Advanced options

    • appendEntriesIntervalMS (integer, defaults to 100): The interval (ms) with which a leader sends AppendEntries messages to the followers (ping).
    • electionTimeoutMinMS (integer, defaults to 300): The minimum election timeout (ms) for a node. It's the minimum time a node has to wait until no AppendEntries message triggers an election.
    • electionTimeoutMaxMS (integer, defaults to 600): The maximum election timeout (ms) for a node. It's the maximum time a node has to wait until no AppendEntries message triggers an election.
    • installSnapshotChunkSize (integer, defaults to 10): The maximum number of database records on each InstallSnapshot message.
    • batchEntriesLimit (integer, defaults to 10): The maximum number of log entries in a AppendEntries message.
    • clientRetryRPCTimeout (integer, defaults to 200): The number of miliseconds the internal client has to wait until retrying
    • clientMaxRetries (integer, defaults to 10): The maximum number of times the client is allowed to retry the remote call.

skiff.start (callback)

Starts the node, initializing. Calls back with no argument when started, or with error in the first argument.

skiff.stop (callback)

Stops the node, shutting down server, disconnects from all peers and stops activity. Calls back once all this is done, or when an error is encountered, with an error in the first argument.

skiff.levelup ()

Returns a new Levelup-compatible object for you to interact with the cluster.

skiff.leveldown ()

Returns a new Leveldown-compatible object for you to interact with the cluster.

skiff.join (peerAddress, callback)

Adds a peer to the cluster. Calls back once the cluster reaches consensus, or with an error if no consensus can be reached.

skiff.leave (peerAddress, callback)

Removes a peer from the cluster. Calls back once the cluster reaches consensus, or with an error if no consensus can be reached.

skiff.stats ()

Returns some interesting stats for this node.

skiff.peers (callback)

Invokes the error-first callback function with the cluster peers and some interesting stats from each.

skiff.term ()

Returns the current term (integer).

skiff.weaken (durationMS)

Weakens the node for the duration. During this period, the node transitions to a special weakened state, in which the node does not react to election timeouts. This period ends once it learns a new leader or the period runs out.

skiff.readConsensus(callback)

Asks for read consensus from the cluster. Calls back when there is an error (with the error as the first argument) or succeeded.

Events

A skiff instance emits the following events:

  • started: once the node is started (network server is up and persisted state is loaded)
  • warning (err): if a non-fatal error was encountered
  • connect (peer): once a leader node is connected to a peer
  • disconnect (peer): once a leader node is disconnected from a peer
  • new state (state): once a node changes state (possible states are follower, candidate and leader)
  • leader: once the node becomes the cluster leader
  • joined (peerAddress): when a peer joined the cluster
  • left (peerAddress): whan a peer left the cluster
  • rpc latency (ms): the latency for an RPC call, in milisenconds

Skiff.createNetwork (options)

Creates a network you can share amongst several Skiff nodes in the same process.

Options:

  • active (object):
    • innactivityTimeout (integer, miliseconds, defaults to 5000): The amount of time to wait before a client connection is closed because of innactivity.
  • passive (object):
    • server (object):
      • port (integer, defaults to 9163): the port the server should listen on
      • host (string, defaults to "0.0.0.0"): the interface address the server should listen to
      • exclusive (boolean, defaults to true): if true, the server is not shareable with other processes (see Server#listen() on Node.js docs).

Sponsors

Development of Skiff is sponsored by YLD.

License

MIT

Copyright

Copyright (c) 2016 Pedro Teixeira

More Repositories

1

styled-is

A flag utility for styled-components
JavaScript
211
star
2

pouch-redux-middleware

PouchDB Redux Middleware
JavaScript
184
star
3

graphql-workshop

GraphQL Workshop Material
JavaScript
175
star
4

pouch-websocket-sync-example

Pouch Stream example
JavaScript
147
star
5

normalized-styled-components

normalize.css with a styled-components mindset
JavaScript
50
star
6

pouch-websocket-sync

PouchDB live sync through websockets
JavaScript
46
star
7

react-topology

Create Topology views with SVG and React
JavaScript
45
star
8

joyent-portal

JavaScript
44
star
9

kubernetes-auth

πŸ‘©β€πŸ’»πŸ” Log into k8s clusters using dex with a nice UI
Go
34
star
10

career-framework

Framework for career progression @ YLD
28
star
11

react-lazy-youtube

React Component for lazy loading youtube videos
JavaScript
25
star
12

react-native-offline-first

React Native offline first with pouchdb
JavaScript
23
star
13

react-native-wearables

React Native common interface for iOS and Android wearables
JavaScript
23
star
14

react-example

JavaScript
21
star
15

yld.io

YLD Website
JavaScript
19
star
16

serverpush-serviceworkers-example

HTTP/2 Server Push & Service Workers example
JavaScript
18
star
17

message-queue

a standard interface to access message queues
JavaScript
17
star
18

pouch-clerk

PouchDB worker reacting to document changes
JavaScript
17
star
19

native-iso-relay

JavaScript
16
star
20

asap-hub

The monorepo for the backend, frontend, and infrastructure of the ASAP hub.
TypeScript
15
star
21

standards

The standard way of doing things in YLD
12
star
22

pouch-remote-stream

Remote Stream for PouchDB
JavaScript
12
star
23

cloud-pizza-place

Our pizza oven is serverless, hot and always ready for baking cheesy items from the YLD menu!
JavaScript
11
star
24

pouch-stream-multi-sync

Sync various PouchDBs over a stream
JavaScript
11
star
25

copilot

JavaScript
10
star
26

license-bot

πŸ™πŸ€– A Github bot to help with Open-Source license conformence
Go
10
star
27

borough

Borough
JavaScript
9
star
28

training-node-angular-source-code

Angluar and Node Training Source Code
JavaScript
8
star
29

bpf-nodejs

Using BPF (http://iovisor.github.io/bcc/) with Node.js
Python
8
star
30

redux-replaceable-middleware

Redux Replaceable Middleware
JavaScript
7
star
31

website

Shiny new website
JavaScript
7
star
32

whisper-ui

Helper native app to run whisper on local machines.
Rust
7
star
33

remcalc

Calculate the rem's from px values.
JavaScript
6
star
34

staff-recommendations

Need help? Need ideas? Do you have ideas? Check our guides!
6
star
35

pseudo-yaml-ast

JavaScript
6
star
36

disclosure

Project dependencies overview
JavaScript
6
star
37

vue-styled-is

Flag utility for styled-components in vue
JavaScript
6
star
38

pouch-clerk-example-app

Pouch Clerk Example App
JavaScript
5
star
39

react-grid-debugger

Visual Tool for debugging grid-based layouts built on React
JavaScript
5
star
40

metrics-yall

Make them pretty metrics πŸš€
JavaScript
5
star
41

node-lambda-example

Example of a Node.js Lambda function
Makefile
4
star
42

teg-august-training-node-code

JavaScript
4
star
43

alpine-node-containerpilot

🐳 - πŸ”πŸ’πŸ—ƒπŸ›©
Shell
3
star
44

oss-stats

JavaScript
3
star
45

react-native-wearables-demo

Objective-C
3
star
46

razzle-plugin-stylable

Get stylable running in your razzle installation
JavaScript
3
star
47

website-contentful-data-transfer

Standalone script that fetched up-to-date info from associated services (Meetup, YouTube) and creates new Contentful entries as necessary
JavaScript
3
star
48

eslint-config-joyent-portal

JavaScript
3
star
49

relay-starter

JavaScript
2
star
50

heart-rate-validating-admission-webhook

Capture your heart-rate in Node.js and perform action based on the data!
JavaScript
2
star
51

BackendCSharpHomeCodeChallengeBase

Backend C# Challenge
C#
2
star
52

chartjs-chart-box-plot

πŸ“ˆ A Box-plot implementation for chart.js
JavaScript
2
star
53

deeplinking-poc

Deeplinking proof of concept
Kotlin
2
star
54

kubernetes-usage

Reporting tool of usage of resources on kubernetes
Shell
2
star
55

joyent-react-scripts

JavaScript
1
star
56

docker-compose-api

Python
1
star
57

coding-challenge

CSS
1
star
58

babel-preset-joyent-portal

JavaScript
1
star
59

react-screenshot-renderer

JavaScript
1
star
60

hello-world

JavaScript
1
star
61

Referral-Links

Referral links
1
star
62

bench-node-cluster

JavaScript
1
star
63

unitcalc

JavaScript
1
star
64

ecs-docker-test-app

ECS Docker Test App
JavaScript
1
star
65

conference-template

Template project to create websites for YLD's conferences
TypeScript
1
star
66

rnd-id

JavaScript
1
star