forked from unshiftio/liferaft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
121 lines (108 loc) · 2.84 KB
/
log.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'use strict';
var setImmediate = require('immediate');
/**
* The representation of the log of a single node.
*
* Options:
*
* - `engine` The storage engine that should be used.
*
* @constructor
* @param {Node} node Instance of a node.
* @param {Object} options Optional configuration.
* @api public
*/
function Log(node, options) {
if (!(this instanceof Log)) return new Log(node, options);
this.node = node;
this.engine = options.engine || 'memory';
//
// Remark: So we want to use something like leveldb here with a particular engine but
// for now lets just use a silly little array
// The following would all be stored in a leveldb database. Entries would be
// its own namespaced key set for easy stream reading and the other values
// would be stored at their particular key for proper persistence and
// fetching. These could be used as a cache like thing as well if we wanted
// faster lookups by default.
//
this.commitIndex = 0;
this.lastApplied = 0;
this.startIndex = 0;
this.startTerm = 0;
this.entries = [];
}
//
// Add some sugar and spice and everything nice. Oh, and also inheritance.
//
Log.extend = require('extendible');
/**
* Commit a log entry
*
* @param {Object} data Data we receive from ourselves or from LEADER
* @param {function} fn function
* @api public
*/
Log.prototype.commit = function commit(data, fn) {
var entry = this.entry(data);
if (entry) this.append(entry);
return setImmediate(fn.bind(null, null, !!entry));
};
Log.prototype.append = function append(entry) {
this.entries.push(entry);
};
/**
* Return the last entry (this may be async in the future)
*
* @returns {Object}
* @api public
*/
Log.prototype.last = function lastentry() {
var last = this.entries[this.entries.length - 1];
if (last) return last;
return {
index: this.startIndex,
term: this.startTerm
};
};
/**
* Create a log entry that we will append with correct form and attrs
*
* @param {object} Data to compute to a proper entry
* @api public
*/
Log.prototype.entry = function entry(data) {
//
// type of entry, (data/command, or something related to raft itself)
//
var type = data.type
, command = data.command
//
// Remark: Hmm this may have to be async if we are fetching everything from a db,
// lets just keep it in memory for now because we may just preload into cache
// on startup?
//
, index = this.last().index + 1;
//
// Remark: How do we want to store function executions or particular actions
// to be replayed in case necessary?
//
return {
command: command,
index: index,
term: this.node.term,
type: type
};
};
/**
* The raft instance we're attached to is closing.
*
* @returns {Boolean} First time shutdown.
* @api private
*/
Log.prototype.end = function end() {
return true;
};
//
// Expose the log module.
//
module.exports = Log;