1
0
Fork 0

added server version check

This commit is contained in:
Jan Steemann 2012-11-20 09:27:28 +01:00
parent 3780aafde9
commit 2efa2f32db
10 changed files with 275 additions and 66 deletions

View File

@ -14,6 +14,7 @@ JAVASCRIPT_HEADER = \
js/common/bootstrap/js-print.h \
js/client/js-client.h \
js/server/js-server.h \
js/server/js-version-check.h \
js/server/js-ahuacatl.h
BUILT_SOURCES += $(JAVASCRIPT_HEADER)

View File

@ -467,6 +467,8 @@ void ArangoServer::buildApplicationServer () {
}
#endif
// if we got here, then we are in server mode
// .............................................................................
// sanity checks
// .............................................................................
@ -515,6 +517,7 @@ int ArangoServer::startupServer () {
_applicationV8->setVocbase(_vocbase);
_applicationV8->setConcurrency(_dispatcherThreads);
_applicationV8->enableVersionCheck();
#if TRI_ENABLE_MRUBY
_applicationMR->setVocbase(_vocbase);
@ -646,6 +649,10 @@ int ArangoServer::executeConsole (OperationMode::server_operation_mode_e mode) {
v8::Local<v8::String> name(v8::String::New("(arango)"));
v8::Context::Scope contextScope(context->_context);
context->_context->Global()->Set(v8::String::New("DATABASEPATH"), v8::String::New(_databasePath.c_str()), v8::ReadOnly);
context->_context->Global()->Set(v8::String::New("VALGRIND"), _runningOnValgrind ? v8::True() : v8::False(), v8::ReadOnly);
context->_context->Global()->Set(v8::String::New("VERSION"), v8::String::New(TRIAGENS_VERSION), v8::ReadOnly);
ok = true;
@ -666,8 +673,6 @@ int ArangoServer::executeConsole (OperationMode::server_operation_mode_e mode) {
sysTestFiles->Set((uint32_t) i, v8::String::New(_unitTests[i].c_str()));
}
context->_context->Global()->Set(v8::String::New("DATABASEPATH"), v8::String::New(_databasePath.c_str()), v8::ReadOnly);
context->_context->Global()->Set(v8::String::New("VALGRIND"), _runningOnValgrind ? v8::True() : v8::False(), v8::ReadOnly);
context->_context->Global()->Set(v8::String::New("SYS_UNIT_TESTS"), sysTestFiles);
context->_context->Global()->Set(v8::String::New("SYS_UNIT_TESTS_RESULT"), v8::True());
@ -701,8 +706,6 @@ int ArangoServer::executeConsole (OperationMode::server_operation_mode_e mode) {
sysTestFiles->Set((uint32_t) i, v8::String::New(_jslint[i].c_str()));
}
context->_context->Global()->Set(v8::String::New("DATABASEPATH"), v8::String::New(_databasePath.c_str()), v8::ReadOnly);
context->_context->Global()->Set(v8::String::New("VALGRIND"), _runningOnValgrind ? v8::True() : v8::False(), v8::ReadOnly);
context->_context->Global()->Set(v8::String::New("SYS_UNIT_TESTS"), sysTestFiles);
context->_context->Global()->Set(v8::String::New("SYS_UNIT_TESTS_RESULT"), v8::True());
@ -727,9 +730,6 @@ int ArangoServer::executeConsole (OperationMode::server_operation_mode_e mode) {
case OperationMode::MODE_SCRIPT: {
v8::TryCatch tryCatch;
context->_context->Global()->Set(v8::String::New("DATABASEPATH"), v8::String::New(_databasePath.c_str()), v8::ReadOnly);
context->_context->Global()->Set(v8::String::New("VALGRIND"), _runningOnValgrind ? v8::True() : v8::False(), v8::ReadOnly);
for (size_t i = 0; i < _scriptFile.size(); ++i) {
bool r = TRI_LoadJavaScriptFile(context->_context, _scriptFile[i].c_str());
@ -785,8 +785,6 @@ int ArangoServer::executeConsole (OperationMode::server_operation_mode_e mode) {
// .............................................................................
case OperationMode::MODE_CONSOLE: {
context->_context->Global()->Set(v8::String::New("DATABASEPATH"), v8::String::New(_databasePath.c_str()), v8::ReadOnly);
context->_context->Global()->Set(v8::String::New("VALGRIND"), _runningOnValgrind ? v8::True() : v8::False(), v8::ReadOnly);
V8LineEditor console(context->_context, ".arango");
console.open(true);

View File

@ -48,6 +48,7 @@ using namespace std;
#include "js/common/bootstrap/js-errors.h"
#include "js/server/js-ahuacatl.h"
#include "js/server/js-server.h"
#include "js/server/js-version-check.h"
// -----------------------------------------------------------------------------
// --SECTION-- class V8GcThread
@ -177,6 +178,7 @@ ApplicationV8::ApplicationV8 (string const& binaryPath)
_startupModules(),
_actionPath(),
_useActions(true),
_runVersionCheck(false),
_gcInterval(1000),
_gcFrequency(10.0),
_v8Options(""),
@ -228,6 +230,14 @@ void ApplicationV8::setVocbase (TRI_vocbase_t* vocbase) {
_vocbase = vocbase;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief enable the database version check
////////////////////////////////////////////////////////////////////////////////
void ApplicationV8::enableVersionCheck () {
_runVersionCheck = true;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief enters an context
////////////////////////////////////////////////////////////////////////////////
@ -621,7 +631,7 @@ void ApplicationV8::stop () {
/// @brief prepares a V8 instance
////////////////////////////////////////////////////////////////////////////////
bool ApplicationV8::prepareV8Instance (size_t i) {
bool ApplicationV8::prepareV8Instance (const size_t i) {
static char const* files[] = { "common/bootstrap/modules.js",
"common/bootstrap/print.js",
"common/bootstrap/errors.js",
@ -665,11 +675,11 @@ bool ApplicationV8::prepareV8Instance (size_t i) {
TRI_InitV8Shell(context->_context);
// load all init files
for (i = 0; i < sizeof(files) / sizeof(files[0]); ++i) {
bool ok = _startupLoader.loadScript(context->_context, files[i]);
for (size_t j = 0; j < sizeof(files) / sizeof(files[0]); ++j) {
bool ok = _startupLoader.loadScript(context->_context, files[j]);
if (! ok) {
LOGGER_FATAL << "cannot load JavaScript utilities from file '" << files[i] << "'";
LOGGER_FATAL << "cannot load JavaScript utilities from file '" << files[j] << "'";
context->_context->Exit();
context->_isolate->Exit();
@ -679,6 +689,32 @@ bool ApplicationV8::prepareV8Instance (size_t i) {
}
}
if (i == 0 && _runVersionCheck) {
LOGGER_DEBUG << "running database version check";
const string script = _startupLoader.buildScript(JS_server_version_check);
// special check script to be run just once in first thread (not in all)
v8::HandleScope scope;
v8::Handle<v8::Value> result = TRI_ExecuteJavaScriptString(context->_context,
v8::String::New(script.c_str()),
v8::String::New("version-check.js"),
false);
if (! TRI_ObjectToBoolean(result)) {
LOGGER_FATAL << "Database version check failed. Please run arango-upgrade --database.directory \"" << _vocbase->_path << "\"";
context->_context->Exit();
context->_isolate->Exit();
delete context->_locker;
return false;
}
LOGGER_DEBUG << "database version check passed";
}
// load all actions
if (_useActions) {
bool ok = _actionLoader.executeAllScripts(context->_context);
@ -709,8 +745,6 @@ bool ApplicationV8::prepareV8Instance (size_t i) {
context->_lastGcStamp = TRI_microtime();
context->_lastGcStamp = TRI_microtime();
LOGGER_TRACE << "initialised V8 context #" << i;
_freeContexts.push_back(context);

View File

@ -200,6 +200,12 @@ namespace triagens {
void setVocbase (struct TRI_vocbase_s*);
////////////////////////////////////////////////////////////////////////////////
/// @brief enable the database version check
////////////////////////////////////////////////////////////////////////////////
void enableVersionCheck ();
////////////////////////////////////////////////////////////////////////////////
/// @brief enters an context
////////////////////////////////////////////////////////////////////////////////
@ -362,6 +368,12 @@ namespace triagens {
bool _useActions;
////////////////////////////////////////////////////////////////////////////////
/// @brief run the database version check
////////////////////////////////////////////////////////////////////////////////
bool _runVersionCheck;
////////////////////////////////////////////////////////////////////////////////
/// @brief JavaScript garbage collection interval (each x requests)
///

View File

@ -27,6 +27,8 @@
#include "v8-vocbase.h"
#include "build.h"
#include "Logger/Logger.h"
#include "Ahuacatl/ahuacatl-codegen.h"
#include "Ahuacatl/ahuacatl-context.h"
@ -5876,6 +5878,9 @@ TRI_v8_global_t* TRI_InitV8VocBridge (v8::Handle<v8::Context> context, TRI_vocba
v8::Boolean::New(false),
#endif
v8::ReadOnly);
context->Global()->Set(v8::String::New("VERSION"), v8::String::New(TRIAGENS_VERSION), v8::ReadOnly);
context->Global()->Set(v8::String::New("DATABASEPATH"), v8::String::New(vocbase->_path), v8::ReadOnly);
// .............................................................................
// create the global variables

View File

@ -49,27 +49,43 @@ function main (argv) {
var allTasks = [ ];
var activeTasks = [ ];
// assume we do not yet have a VERSION value
var currentVersion = 0;
var lastVersion = null;
var lastTasks = { };
if (FS_EXISTS(versionFile)) {
// VERSION file exists, read its contents
currentVersion = parseInt(SYS_READ(versionFile));
}
// helper function to define tasks
function addTask (description, maxVersion, code) {
// "description" is a textual description of the task that will be printed out on screen
// "maxVersion" is the maximum version number the task will be applied for
var task = { description: description, maxVersion: maxVersion, code: code };
allTasks.push(task);
// the maximum version number defined for the task is >= than the current VERSION number
// that means we will apply to task
if (currentVersion < parseInt(maxVersion)) {
activeTasks.push(task);
var versionInfo = SYS_READ(versionFile);
if (versionInfo != '') {
var versionValues = JSON.parse(versionInfo);
if (versionValues && versionValues.version && ! isNaN(versionValues.version)) {
lastVersion = parseFloat(versionValues.version);
}
if (versionValues && versionValues.tasks && typeof(versionValues.tasks) === 'object') {
lastTasks = versionValues.tasks;
}
}
}
console.log("Upgrade script " + argv[0] + " started");
console.log("Server version: " + VERSION + ", database directory version: " + (lastVersion || "(not set)"));
var currentServerVersion = VERSION.match(/^(\d+\.\d+).*$/);
if (! currentServerVersion) {
// server version is invalid for some reason
console.error("Unexpected arangodb server version found.");
console.error("Refusing to start.");
return 1;
}
var currentVersion = parseFloat(currentServerVersion[1]);
if (lastVersion != null && lastVersion > currentVersion) {
// downgrade??
console.error("Database directory version is higher than server version. This seems like a downgrade, which is not supported.");
console.error("Refusing to start.");
return 1;
}
function getCollection (name) {
return db._collection(name);
@ -94,6 +110,20 @@ function main (argv) {
return collectionExists(name);
}
// helper function to define tasks
function addTask (name, description, code) {
// "description" is a textual description of the task that will be printed out on screen
// "maxVersion" is the maximum version number the task will be applied for
var task = { name: name, description: description, code: code };
allTasks.push(task);
if (lastTasks[name] === undefined || lastTasks[name] === false) {
// task never executed or previous execution failed
activeTasks.push(task);
}
}
// --------------------------------------------------------------------------
@ -101,12 +131,12 @@ function main (argv) {
// --------------------------------------------------------------------------
// set up the collection _users
addTask("setup _users collection", 1, function () {
addTask("setupUsers", "setup _users collection", function () {
return createSystemCollection("_users", { waitForSync : true });
});
// create a unique index on username attribute in _users
addTask("create index on username attribute in _users collection", 1, function () {
addTask("createUsersIndex", "create index on username attribute in _users collection", function () {
var users = getCollection("_users");
if (! users) {
return false;
@ -118,7 +148,7 @@ function main (argv) {
});
// add a default root user with no passwd
addTask("add default root user", 1, function () {
addTask("addDefaultUser", "add default root user", function () {
var users = getCollection("_users");
if (! users) {
return false;
@ -133,12 +163,12 @@ function main (argv) {
});
// set up the collection _graphs
addTask("setup _graphs collection", 1, function () {
addTask("setupGraphs", "setup _graphs collection", function () {
return createSystemCollection("_graphs", { waitForSync : true });
});
// create a unique index on name attribute in _graphs
addTask("create index on name attribute in _graphs collection", 1, function () {
addTask("createGraphsIndex", "create index on name attribute in _graphs collection", function () {
var graphs = getCollection("_graphs");
if (! graphs) {
@ -151,7 +181,7 @@ function main (argv) {
});
// make distinction between document and edge collections
addTask("set new collection type for edge collections and update collection version", 1, function () {
addTask("addCollectionVersion", "set new collection type for edge collections and update collection version", function () {
var collections = db._collections();
for (var i in collections) {
@ -201,25 +231,29 @@ function main (argv) {
});
// create the _modules collection
addTask("setup _modules collection", 1, function () {
addTask("createModules", "setup _modules collection", function () {
return createSystemCollection("_modules");
});
// create the _routing collection
addTask("setup _routing collection", 1, function () {
addTask("createRouting", "setup _routing collection", function () {
return createSystemCollection("_routing");
});
// create the VERSION file
addTask("create VERSION file", 2, function () {
// save "1" into VERSION file
SYS_SAVE(versionFile, "2");
// insert default route for /
addTask("insertDefaultRoute", "insert default route for the admin interface", function () {
var routing = getCollection("_routing");
if (! routing) {
return false;
}
if (routing.count() == 0) {
// only add route if no other route has been defined
routing.save({ url: "/", action: { "do": "org/arangodb/actions/redirectRequest", options: { permanently: true, destination: "/_admin/html/index.html" } } });
}
return true;
});
console.log("Upgrade script " + argv[0] + " started");
console.log("Server VERSION is: " + currentVersion);
// loop through all tasks and execute them
console.log("Found " + allTasks.length + " defined task(s), " + activeTasks.length + " task(s) to run");
@ -228,18 +262,29 @@ function main (argv) {
for (var i in activeTasks) {
var task = activeTasks[i];
console.log("Executing task #" + (++taskNumber) + ": " + task.description);
console.log("Executing task #" + (++taskNumber) + " (" + task.name + "): " + task.description);
var result = task.code();
// assume failure
var result = false;
try {
// execute task
result = task.code();
}
catch (e) {
}
if (! result) {
if (result) {
// success
lastTasks[task.name] = true;
// save/update version info
SYS_SAVE(versionFile, JSON.stringify({ version: currentVersion, tasks: lastTasks }));
console.log("Task successful");
}
else {
console.error("Task failed. Aborting upgrade script.");
console.error("Please fix the problem and try running the upgrade script again.");
return 1;
}
else {
console.log("Task successful");
}
}
console.log("Upgrade script successfully finished");

View File

@ -152,7 +152,7 @@ function splitUrl (url) {
////////////////////////////////////////////////////////////////////////////////
function lookupUrl (prefix, url) {
if (url === undefined) {
if (url === undefined || url === '') {
return null;
}
@ -428,6 +428,10 @@ function defineRoutePart (route, subwhere, parts, pos, constraint, callback) {
var ok;
part = parts[pos];
if (part == undefined) {
// otherwise we'll get an exception below
part = '';
}
if (typeof part === "string") {
if (! subwhere.hasOwnProperty('exact')) {
@ -438,7 +442,7 @@ function defineRoutePart (route, subwhere, parts, pos, constraint, callback) {
subwhere.exact[part] = {};
}
if (pos + 1< parts.length) {
if (pos + 1 < parts.length) {
defineRoutePart(route, subwhere.exact[part], parts, pos + 1, constraint, callback);
}
else {

View File

@ -0,0 +1,95 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief version check at the start of the server
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2004-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 2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- version check
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup ArangoDB
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief updates the database
////////////////////////////////////////////////////////////////////////////////
(function() {
var console = require("console");
// path to the VERSION file
var versionFile = DATABASEPATH + "/VERSION";
var lastVersion = null;
if (! FS_EXISTS(versionFile)) {
console.error("No version information file found in database directory.");
return false;
}
// VERSION file exists, read its contents
var versionInfo = SYS_READ(versionFile);
if (versionInfo != '') {
var versionValues = JSON.parse(versionInfo);
if (versionValues && versionValues.version && ! isNaN(versionValues.version)) {
lastVersion = parseFloat(versionValues.version);
}
}
if (lastVersion == null) {
console.error("No version information file found in database directory.");
return false;
}
var currentServerVersion = VERSION.match(/^(\d+\.\d+).*$/);
if (! currentServerVersion) {
// server version is invalid for some reason
console.error("Unexpected arangodb server version found.");
return false;
}
var currentVersion = parseFloat(currentServerVersion[1]);
if (lastVersion != null && lastVersion > currentVersion) {
// downgrade??
console.warn("Database directory version is higher than server version. This seems like you are running arangodb on a database directory that was created with a newer version. This is not supported.");
// still, allow the start
}
return true;
})();
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- END-OF-FILE
// -----------------------------------------------------------------------------
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)"
// End:

View File

@ -87,6 +87,24 @@ void ScriptLoader::setDirectory (string const& directory) {
_directory = directory;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief build a script from an array of strings
////////////////////////////////////////////////////////////////////////////////
string ScriptLoader::buildScript (const char** script) {
string scriptString;
while (true) {
string tempStr = string(*script);
if (tempStr == "//__end__") {
break;
}
scriptString += tempStr + "\n";
++script;
}
return scriptString;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief defines a new named script
////////////////////////////////////////////////////////////////////////////////
@ -98,19 +116,10 @@ void ScriptLoader::defineScript (string const& name, string const& script) {
}
void ScriptLoader::defineScript (const string& name, const char** script) {
string scriptString;
string scriptString = buildScript(script);
MUTEX_LOCKER(_lock);
while (true) {
string tempStr = string(*script);
if (tempStr == "//__end__") {
break;
}
scriptString += tempStr + "\n";
++script;
}
_scripts[name] = scriptString;
}

View File

@ -98,6 +98,12 @@ namespace triagens {
void setDirectory (string const&);
////////////////////////////////////////////////////////////////////////////////
/// @brief build a script from an array of strings
////////////////////////////////////////////////////////////////////////////////
string buildScript (const char** script);
////////////////////////////////////////////////////////////////////////////////
/// @brief defines a new named script
////////////////////////////////////////////////////////////////////////////////