|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('debug')('elector') |
| 4 | +const zookeeper = require('node-zookeeper-client') |
| 5 | +const async = require('async') |
| 6 | +const _ = require('lodash') |
| 7 | +const EventEmitter = require('events') |
| 8 | + |
| 9 | +class Elector extends EventEmitter { |
| 10 | + constructor (zkOptions, electionPath = '/election') { |
| 11 | + super() |
| 12 | + this.id = null |
| 13 | + this.electionPath = electionPath |
| 14 | + this.client = zookeeper.createClient(zkOptions.host) |
| 15 | + this.client.once('connected', () => this._onConnect()) |
| 16 | + } |
| 17 | + |
| 18 | + connect () { |
| 19 | + this.client.connect() |
| 20 | + } |
| 21 | + |
| 22 | + _onConnect () { |
| 23 | + debug('connected!') |
| 24 | + async.waterfall( |
| 25 | + [ |
| 26 | + callback => { |
| 27 | + debug('creating election path "%s"', this.electionPath) |
| 28 | + this.client.mkdirp(this.electionPath, callback) |
| 29 | + }, |
| 30 | + |
| 31 | + (path, callback) => { |
| 32 | + debug('created path: "%s"', path) |
| 33 | + debug('creating EPHEMERAL_SEQUENTIAL node') |
| 34 | + this.client.create( |
| 35 | + `${this.electionPath}/p_`, |
| 36 | + zookeeper.CreateMode.EPHEMERAL_SEQUENTIAL, |
| 37 | + callback |
| 38 | + ) |
| 39 | + }, |
| 40 | + |
| 41 | + (path, callback) => { |
| 42 | + debug('newly created znode is "%s"', path) |
| 43 | + this.id = _.last(path.split('/')) |
| 44 | + debug('my candidateId is', this.id) |
| 45 | + this.emit('candidateId', this.id) |
| 46 | + this._listCandidates(callback) |
| 47 | + } |
| 48 | + ], |
| 49 | + (error, candidates) => { |
| 50 | + if (error) { |
| 51 | + return console.error(error) |
| 52 | + } |
| 53 | + debug('received candidates', candidates) |
| 54 | + this._pickLeader(candidates) |
| 55 | + } |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + _pickLeader (candidates) { |
| 60 | + const previousLeadershipState = this.isLeader |
| 61 | + this.isLeader = _.first(candidates.sort()) === this.id |
| 62 | + |
| 63 | + if (previousLeadershipState !== this.isLeader) { |
| 64 | + if (this.isLeader) { |
| 65 | + debug('%s I am the leader!', this.id) |
| 66 | + this.emit('leader') |
| 67 | + } else { |
| 68 | + this.emit('follower') |
| 69 | + debug('%s I am a follower', this.id) |
| 70 | + } |
| 71 | + } else { |
| 72 | + debug('%s state is the same', this.id) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + _onCandidateChange (event) { |
| 77 | + if (this.disconnecting) { |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + this._listCandidates((error, candidates) => { |
| 82 | + if (error) { |
| 83 | + debug(error) |
| 84 | + this.emit('error', error) |
| 85 | + return |
| 86 | + } |
| 87 | + debug('new candidates', candidates) |
| 88 | + this._pickLeader(candidates) |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + _listCandidates (callback) { |
| 93 | + this.client.getChildren( |
| 94 | + this.electionPath, |
| 95 | + event => this._onCandidateChange(event), |
| 96 | + (error, candidates, stats) => { |
| 97 | + if (error) { |
| 98 | + return callback(error) |
| 99 | + } |
| 100 | + callback(null, candidates) |
| 101 | + } |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + disconnect () { |
| 106 | + debug('disconnecting') |
| 107 | + this.disconnecting = true |
| 108 | + this.client.remove(`${this.electionPath}/${this.id}`, error => { |
| 109 | + this.client.close() |
| 110 | + if (error) { |
| 111 | + return debug(error) |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +module.exports = Elector |
0 commit comments