1
0
Fork 0
arangodb/js/client/modules/@arangodb/replication.js

247 lines
7.4 KiB
JavaScript

'use strict';
////////////////////////////////////////////////////////////////////////////////
/// @brief Replication management
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2012 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Jan Steemann
/// @author Copyright 2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
var internal = require("internal");
var arangosh = require("@arangodb/arangosh");
var logger = { };
var applier = { };
////////////////////////////////////////////////////////////////////////////////
/// @brief return the replication logger state
////////////////////////////////////////////////////////////////////////////////
logger.state = function () {
var db = internal.db;
var requestResult = db._connection.GET("/_api/replication/logger-state");
arangosh.checkRequestResult(requestResult);
return requestResult;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief return the tick ranges that can be provided by the replication logger
////////////////////////////////////////////////////////////////////////////////
logger.tickRanges = function () {
var db = internal.db;
var requestResult = db._connection.GET("/_api/replication/logger-tick-ranges");
arangosh.checkRequestResult(requestResult);
return requestResult;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief return the first tick that can be provided by the replication logger
////////////////////////////////////////////////////////////////////////////////
logger.firstTick = function () {
var db = internal.db;
var requestResult = db._connection.GET("/_api/replication/logger-first-tick");
arangosh.checkRequestResult(requestResult);
return requestResult.firstTick;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief starts the replication applier
////////////////////////////////////////////////////////////////////////////////
applier.start = function (initialTick) {
var db = internal.db;
var append = "";
if (initialTick !== undefined) {
append = "?from=" + encodeURIComponent(initialTick);
}
var requestResult = db._connection.PUT("/_api/replication/applier-start" + append, "");
arangosh.checkRequestResult(requestResult);
return requestResult;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief stops the replication applier
////////////////////////////////////////////////////////////////////////////////
applier.stop = applier.shutdown = function () {
var db = internal.db;
var requestResult = db._connection.PUT("/_api/replication/applier-stop", "");
arangosh.checkRequestResult(requestResult);
return requestResult;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief return the replication applier state
////////////////////////////////////////////////////////////////////////////////
applier.state = function () {
var db = internal.db;
var requestResult = db._connection.GET("/_api/replication/applier-state");
arangosh.checkRequestResult(requestResult);
return requestResult;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief stop the replication applier state and "forget" all state
////////////////////////////////////////////////////////////////////////////////
applier.forget = function () {
var db = internal.db;
var requestResult = db._connection.DELETE("/_api/replication/applier-state");
arangosh.checkRequestResult(requestResult);
return requestResult;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief configures the replication applier
////////////////////////////////////////////////////////////////////////////////
applier.properties = function (config) {
var db = internal.db;
var requestResult;
if (config === undefined) {
requestResult = db._connection.GET("/_api/replication/applier-config");
}
else {
requestResult = db._connection.PUT("/_api/replication/applier-config",
JSON.stringify(config));
}
arangosh.checkRequestResult(requestResult);
return requestResult;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief performs a one-time synchronization with a remote endpoint
////////////////////////////////////////////////////////////////////////////////
var sync = function (config) {
var db = internal.db;
var body = JSON.stringify(config || { });
var requestResult;
if (config.async) {
var headers = { "X-Arango-Async" : "store" };
requestResult = db._connection.PUT_RAW("/_api/replication/sync", body, headers);
}
else {
requestResult = db._connection.PUT("/_api/replication/sync", body);
}
arangosh.checkRequestResult(requestResult);
if (config.async) {
return requestResult.headers["x-arango-async-id"];
}
return requestResult;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief performs a one-time synchronization with a remote endpoint, for
/// a single collection
////////////////////////////////////////////////////////////////////////////////
var syncCollection = function (collection, config) {
var db = internal.db;
config = config || { };
config.restrictType = "include";
config.restrictCollections = [ collection ];
config.includeSystem = true;
var body = JSON.stringify(config);
var requestResult;
if (config.async) {
var headers = { "X-Arango-Async" : "store" };
requestResult = db._connection.PUT_RAW("/_api/replication/sync", body, headers);
}
else {
requestResult = db._connection.PUT("/_api/replication/sync", body);
}
arangosh.checkRequestResult(requestResult);
if (config.async) {
return requestResult.headers["x-arango-async-id"];
}
return requestResult;
};
var getSyncResult = function (id) {
var db = internal.db;
var requestResult = db._connection.PUT_RAW("/_api/job/" + encodeURIComponent(id), "");
arangosh.checkRequestResult(requestResult);
if (requestResult.headers.hasOwnProperty("x-arango-async-id")) {
return JSON.parse(requestResult.body);
}
return false;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief fetches a server's id
////////////////////////////////////////////////////////////////////////////////
var serverId = function () {
var db = internal.db;
var requestResult = db._connection.GET("/_api/replication/server-id");
arangosh.checkRequestResult(requestResult);
return requestResult.serverId;
};
exports.logger = logger;
exports.applier = applier;
exports.sync = sync;
exports.syncCollection = syncCollection;
exports.getSyncResult = getSyncResult;
exports.serverId = serverId;