yldio / skiff

Raft Consensus for Node.js, backed by LevelDB
MIT License
247 stars 29 forks source link

levelup events not firing #41

Open bhoriuchi opened 7 years ago

bhoriuchi commented 7 years ago

Please close After reviewing the skiff code I see that a call to skiff.levelup() creates a new levelup instance so my event listener was listening on a different instance

Playing around with skiff (using memdown) and I am attempting to get levelup put/del events from a 3 node skiff cluster. i have 1 node making a put request after the leader has been elected and i can see that the data is written. All 3 nodes run the same code on different ports in separate processes with the exception of node 3 calling a put after 2 seconds

pertinent code is

const db = skiff.levelup()
db.on('put', console.log)

I get no console output on any of the 3 nodes. Is there a way to achieve this?

Full code below

peer-list.js

export default [
  '/ip4/127.0.0.1/tcp/9490',
  '/ip4/127.0.0.1/tcp/9491',
  '/ip4/127.0.0.1/tcp/9492'
]

node.js

import Skiff from 'skiff'
import memdown from 'memdown'
import _ from 'lodash'
import peerList from './peer-list'

export default function node (id) {
  let state = null
  let loc = _.first(_.filter(peerList, (peer) => peer.match(new RegExp(`${id}$`)) !== null))
  let peers = _.filter(peerList, (peer) => peer.match(new RegExp(`${id}$`)) === null)

  const options = { db: memdown, peers }
  const skiff = Skiff(loc, options)
  const db = skiff.levelup()

  db.on('put', console.log)

  skiff.start((err) => {
    if (err) return console.error(err)
    console.log('Skiff server started')
  })

  skiff.on('new state', (s) => {
    if (s === state) return
    state = s
    console.log(`Became a ${s}`)
  })

  return skiff
}

node0.js

require('babel-register')
var node = require('./node').default
var skiff = node(0)
var db = skiff.levelup()

node1.js

require('babel-register')
var node = require('./node').default
var skiff = node(1)
var db = skiff.levelup()

node2.js

require('babel-register')
var node = require('./node').default
var skiff = node(2)
var db = skiff.levelup()

setTimeout(() => {
  db.put('1', 'test', (err) => {
    if (err) return console.log('put error')
    console.log('put success')
  })
}, 2000)
pgte commented 7 years ago

Yes, this looks like a bug. There needs to be a notification of changes bubbling up from the internal db into this interface.