-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
333 lines (297 loc) · 7.93 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
var domain = require('./lib/domain'),
tracker = require('./lib/tracker'),
utils = require('./lib/utils'),
querystring = require('querystring'),
net = require('net'),
fs = require('fs'),
noop = function() {};
/**
* Constructor
*
* @param {Array} trackers An array of mogile trackers
* @param {Number} retries Number of times to retry an operation
*/
var Mogile = function(trackers, retries)
{
// A log of all commands that took place during a transaction
this.transaction_log = [];
// Whether we're inside of a transaction or not
this.is_transaction = false;
// List of local files that need to be deleted at the end of of the transaction
this.transaction_files = [];
// The tracker hosts
this.trackers = [];
for(var i = 0; i < trackers.length; i++) {
this.trackers.push(new tracker.factory(trackers[i]));
}
// The current tracker being used
this.current_tracker = null;
// The number of times to retry an operation
this.retries = (typeof retries != 'undefined') ? retries : 1;
// The default encoding for connections
this.encoding = 'ascii';
}
/**
* Factory class
*
* Returns a new instance of Mogile.
*
* @param {Array} trackers An array of mogile trackers
* @param {Number} retries Number of times to retry an operation
* @return {Mogile}
*/
Mogile.createClient = function(trackers, retries)
{
return new Mogile(trackers, retries);
}
/**
* Domain factory method
*
* Returns a new instance of Domain.
*
* @return {Domain}
*/
Mogile.domain = Mogile.prototype.domain = function(name)
{
return domain.factory(this, name);
}
/**
* Begins a mogile transaction
*/
Mogile.prototype.begin = function()
{
if (this.is_transaction) {
return false;
}
this.commit();
this.is_transaction = true;
return true;
}
/**
* Commits all the changes in a transaction
*/
Mogile.prototype.commit = function()
{
this.transactionCleanFiles();
this.is_transaction = false;
this.transaction_log = [];
this.transaction_files = [];
return true;
}
/**
* Rolls back all the changes made during a transaction
*/
Mogile.prototype.rollback = function(callback)
{
callback = callback || noop;
// We have to stop the transaction now, or else subsequent calls
// to send() will keep recording transaction logs.
this.is_transaction = false;
var logs = this.transaction_log.reverse();
var action = null;
var i = -1;
var $this = this;
var rollbackAction = function() {
if (++i >= logs.length) {
$this.commit();
return callback();
}
action = logs[i];
switch(action.cmd) {
case 'DELETE':
$this.domain(action.domain)
.storeFile(action.args.key, action.args.class, action.args.temp_file, function(err, bytes) {
if (err) {
return callback(err);
}
return rollbackAction();
});
break;
case 'RENAME':
$this.domain(action.domain)
.rename(action.args.to_key, action.args.from_key, function(err) {
if (err) {
return callback(err);
}
return rollbackAction();
});
break;
case 'CREATE_CLOSE':
$this.domain(action.domain)
.del(action.args.key, action.args.class, function(err) {
if (err) {
return callback(err);
}
return rollbackAction();
});
break;
default:
// Not all commands have changes to make
return rollbackAction();
break;
}
};
return rollbackAction();
}
/**
* Deletes an temp files that were created during a transaction
*/
Mogile.prototype.transactionCleanFiles = function()
{
for(var i = 0; i < this.transaction_files; i++) {
fs.unlink(this.transaction_files[i]);
}
}
/**
* Gets a list of all the domains in the file system
*
* @param {Function} callback Function to call with an array of all domains
* @return {Boolean}
*/
Mogile.prototype.getDomains = function(callback)
{
callback = callback || noop;
this.send('default', 'GET_DOMAINS', {}, function(err, results) {
if (err) {
return callback(err);
}
var domains = [];
for(var i = 1; i <= results['domains']; i++) {
var dom = 'domain' + i;
var classes = {};
for(var j = 1; j <= results[dom + 'classes']; j++) {
classes[results[dom + 'class' + j + 'name']] = results[dom + 'class' + j + 'mindevcount'] - 0;
}
domains.push({
name: results[dom],
classes: classes
});
}
callback(null, domains);
});
return true;
}
/**
* Sends a command to mogile
*
* @param {String} domain The storage domain
* @param {String} cmd The command to send
* @param {Object} args The command arguments
* @param {Function} callback Function to call when the operation is complete
* @return {Boolean}
*/
Mogile.prototype.send = function(domain, cmd, args, callback)
{
args = args || {};
callback = callback || noop;
args.domain = domain;
var command = cmd + ' ' + querystring.stringify(args) + "\n";
var tries = 0;
var $this = this;
var sendf = function() {
$this.sendCommand(command, function(err, results) {
if (err) {
if (++tries > $this.retries) {
// Mark the tracker dead
return callback(err);
} else {
return sendf();
}
}
// All responses should start with OK or ERR, followed by a space, and then some kind
// of message. The message will be formatted as a URL query string, without any spaces.
var parts = results.split(' ');
// Having fewer than 2 parts is some kind of communications error, since the tracker
// will always return 2 string separated by a space.
if (parts.length != 2) {
return callback('Got invalid response from tracker: ' + results);
}
// Responses starting with ERR are errors returned by the tracker. For instance
// if the key is unknown.
if (parts[0] == 'ERR') {
return callback(parts[1]);
}
return callback(null, querystring.parse(parts[1].replace("\r\n", "")));
});
}
if (this.is_transaction) {
if (cmd == 'DELETE') {
// When deleting, we need to store a temp copy of the file. That way
// we can re-store the file if the transaction is rolled back. That's
// why we *need* a storage class for the delete command when inside
// transactions... There's no way to re-store the file without the name
// of the storage class.
if (typeof args.class == 'undefined') {
return callback("A class name must be specified when deleting within a transaction");
}
args.temp_file = utils.tempnam('/tmp', 'mogile');
if (!args.temp_file) {
return callback('Unable to create temp file in /tmp');
}
$this.domain(args.domain)
.getFile(args.key, args.temp_file, function(err, bytes) {
if (err) {
return callback(err);
}
$this.transaction_files.push(args.temp_file);
$this.transaction_log.push({"domain": domain, "cmd": cmd, "args": args });
sendf();
});
} else {
$this.transaction_log.push({"domain": domain, "cmd": cmd, "args": args });
sendf();
}
} else {
sendf();
}
return true;
}
Mogile.prototype.sendCommand = function(cmd, callback)
{
var trackers = this.getLiveTrackers();
if (trackers.length == 0) {
callback('No live trackers found');
}
var i = 0;
var $this = this;
var sendf = function() {
$this.current_tracker = trackers[i];
var connection = net.createConnection($this.current_tracker.getPort(), $this.current_tracker.getHost());
connection.setEncoding($this.encoding);
connection.on('error', function(err) {
i++;
if (i == $this.trackers.length) {
callback(err);
} else {
sendf();
}
});
connection.on('connect', function() {
connection.write(cmd, $this.encoding, function() {
connection.on('data', function(response) {
connection.end();
callback(null, response);
});
});
});
}
sendf();
return true;
}
/**
* Returns all the trackers in the alive state
*
* @return {Array}
*/
Mogile.prototype.getLiveTrackers = function()
{
var live_trackers = [];
for(var i = 0; i < this.trackers.length; i++) {
if (this.trackers[i].isAlive()) {
live_trackers.push(this.trackers[i]);
}
}
return live_trackers;
}
// Export the Mogile class
module.exports = Mogile;