Commit 7cb82e81 authored by Tristan Cavelier's avatar Tristan Cavelier

replicate storage check & repair redesigned

parent 1ad1b46b
...@@ -41,8 +41,8 @@ ...@@ -41,8 +41,8 @@
var Promise = require('rsvp').Promise, var Promise = require('rsvp').Promise,
all = require('rsvp').all, all = require('rsvp').all,
dictUpdate = require('jio').util.dictUpdate, addStorageFunction = require('jio').addStorage,
addStorageFunction = require('jio').addStorage; uniqueJSONStringify = require('jio').util.uniqueJSONStringify;
/** /**
* Test if the a value is a date * Test if the a value is a date
...@@ -436,70 +436,98 @@ ...@@ -436,70 +436,98 @@
ReplicateStorage.prototype.check = function (command, param, option) { ReplicateStorage.prototype.check = function (command, param, option) {
var promise_list = [], index, length = this._storage_list.length; var promise_list = [], index, length = this._storage_list.length;
for (index = 0; index < length; index += 1) { for (index = 0; index < length; index += 1) {
promise_list[index] = success( promise_list[index] =
command.storage(this._storage_list[index]).check(param, option) command.storage(this._storage_list[index]).check(param, option);
);
} }
sequence([function () { return all(promise_list).
return all(promise_list); then(function () { return; }).
}, [command.success, command.error]]); then(command.success, command.error, command.notify);
}; };
ReplicateStorage.prototype.repair = function (command, param) { ReplicateStorage.prototype.repair = function (command, param, option) {
var promise_list = [], index, that, length = this._storage_list.length; var storage_list = this._storage_list, length = storage_list.length;
that = this;
if (typeof param._id !== 'string' || !param._id) { if (typeof param._id !== 'string' || !param._id) {
command.success(); command.error("bad_request");
return; return;
} }
for (index = 0; index < length; index += 1) {
promise_list[index] = storage_list = storage_list.map(function (description) {
success(command.storage(this._storage_list[index]).get(param)); return command.storage(description);
});
function repairSubStorages() {
var promise_list = [], i;
for (i = 0; i < length; i += 1) {
promise_list[i] = storage_list[i].repair(param, option);
}
return all(promise_list);
} }
sequence([function () {
function getSubStoragesDocument() {
var promise_list = [], i;
for (i = 0; i < length; i += 1) {
promise_list[i] = success(storage_list[i].get(param));
}
return all(promise_list); return all(promise_list);
}, function (answers) { }
var i, list = [], winner = null;
function synchronizeDocument(answers) {
var i, tmp, winner, winner_str, promise_list = [],
metadata_dict = {}, not_found_dict = {}, modified_list = [];
for (i = 0; i < answers.length; i += 1) { for (i = 0; i < answers.length; i += 1) {
if (answers[i].result === "success") { if (answers[i].result !== "success") {
if (isDate(answers[i].data.modified)) { not_found_dict[i] = true;
list[i] = answers[i].data; } else {
if (winner === null || metadata_dict[i] = answers[i].data;
new Date(winner.modified) < tmp = metadata_dict[i].modified;
new Date(answers[i].data.modified)) { tmp = new Date(tmp === undefined ? NaN : tmp);
winner = answers[i].data; tmp.index = i;
} modified_list.push(tmp);
}
} else if (answers[i].status === 404) {
list[i] = 0;
} }
} }
for (i = 0; i < list.length; i += 1) { modified_list.sort();
if (list[i] && new Date(list[i].modified) < new Date(winner.modified)) {
list[i] = success(command.storage(that._storage_list[i]).put(winner)); if (modified_list.length === 0) {
} else if (list[i] === 0) { // do nothing because no document was found
list[i] = dictUpdate({}, winner); return [];
delete list[i]._id;
list[i] =
success(command.storage(that._storage_list[i]).post(list[i]));
}
} }
list = list.reduce(function (previous, current) {
if (current) { tmp = modified_list.pop();
previous.push(current); winner = metadata_dict[tmp.index];
winner_str = uniqueJSONStringify(winner);
tmp = tmp.index;
// if no document has valid modified metadata
// just take the first one and replicate to the other one
for (i = 0; i < length; i += 1) {
if (i !== tmp && winner_str !== uniqueJSONStringify(metadata_dict[i])) {
// console.log("Synchronizing document `" + winner_str +
// "` into storage number " + i + " by doing a `" +
// (not_found_dict[i] ? "post" : "put") + "`. ");
promise_list.push(
storage_list[i][not_found_dict[i] ? "post" : "put"](winner)
);
} }
return previous; }
}, []); return all(promise_list);
return all(list); }
}, function (answers) {
function checkAnswers(answers) {
var i; var i;
for (i = 0; i < answers.length; i += 1) { for (i = 0; i < answers.length; i += 1) {
if (answers[i].result !== "success") { if (answers[i].result !== "success") {
return command.error(409); throw answers[i];
} }
} }
command.success(); }
}]);
return repairSubStorages().
then(getSubStoragesDocument).
then(synchronizeDocument).
then(checkAnswers).
then(command.success, command.error, command.notify);
}; };
addStorageFunction('replicate', ReplicateStorage); addStorageFunction('replicate', ReplicateStorage);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment