Don't start a server for map migration

This commit is contained in:
ShadowNinja 2015-02-27 02:24:07 -05:00
parent 708337dfc2
commit e9eda2b0d0
3 changed files with 65 additions and 75 deletions

View File

@ -46,22 +46,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "quicktune.h" #include "quicktune.h"
#include "httpfetch.h" #include "httpfetch.h"
#include "guiEngine.h" #include "guiEngine.h"
#include "map.h"
#include "mapsector.h" #include "mapsector.h"
#include "fontengine.h" #include "fontengine.h"
#include "gameparams.h" #include "gameparams.h"
#include "database.h"
#ifndef SERVER #ifndef SERVER
#include "client/clientlauncher.h" #include "client/clientlauncher.h"
#endif #endif
#include "database-sqlite3.h"
#ifdef USE_LEVELDB
#include "database-leveldb.h"
#endif
#if USE_REDIS
#include "database-redis.h"
#endif
#ifdef HAVE_TOUCHSCREENGUI #ifdef HAVE_TOUCHSCREENGUI
#include "touchscreengui.h" #include "touchscreengui.h"
#endif #endif
@ -140,8 +133,7 @@ static bool get_game_from_cmdline(GameParams *game_params, const Settings &cmd_a
static bool determine_subgame(GameParams *game_params); static bool determine_subgame(GameParams *game_params);
static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args); static bool run_dedicated_server(const GameParams &game_params, const Settings &cmd_args);
static bool migrate_database(const GameParams &game_params, const Settings &cmd_args, static bool migrate_database(const GameParams &game_params, const Settings &cmd_args);
Server *server);
/**********************************************************************/ /**********************************************************************/
@ -894,14 +886,14 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
return false; return false;
} }
// Database migration
if (cmd_args.exists("migrate"))
return migrate_database(game_params, cmd_args);
// Create server // Create server
Server server(game_params.world_path, Server server(game_params.world_path,
game_params.game_spec, false, bind_addr.isIPv6()); game_params.game_spec, false, bind_addr.isIPv6());
// Database migration
if (cmd_args.exists("migrate"))
return migrate_database(game_params, cmd_args, &server);
server.start(bind_addr); server.start(bind_addr);
// Run server // Run server
@ -911,68 +903,58 @@ static bool run_dedicated_server(const GameParams &game_params, const Settings &
return true; return true;
} }
static bool migrate_database(const GameParams &game_params, const Settings &cmd_args, static bool migrate_database(const GameParams &game_params, const Settings &cmd_args)
Server *server)
{ {
std::string migrate_to = cmd_args.get("migrate"); std::string migrate_to = cmd_args.get("migrate");
Settings world_mt; Settings world_mt;
std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt"; std::string world_mt_path = game_params.world_path + DIR_DELIM + "world.mt";
bool success = world_mt.readConfigFile(world_mt_path.c_str()); if (!world_mt.readConfigFile(world_mt_path.c_str())) {
if (!success) { errorstream << "Cannot read world.mt!" << std::endl;
errorstream << "Cannot read world.mt" << std::endl; return false;
return 1;
} }
if (!world_mt.exists("backend")) { if (!world_mt.exists("backend")) {
errorstream << "Please specify your current backend in world.mt:" errorstream << "Please specify your current backend in world.mt:"
<< std::endl
<< " backend = {sqlite3|leveldb|redis|dummy}"
<< std::endl; << std::endl;
errorstream << " backend = {sqlite3|leveldb|redis|dummy}" return false;
<< std::endl;
return 1;
} }
std::string backend = world_mt.get("backend"); std::string backend = world_mt.get("backend");
Database *new_db;
if (backend == migrate_to) { if (backend == migrate_to) {
errorstream << "Cannot migrate: new backend is same" errorstream << "Cannot migrate: new backend is same"
<<" as the old one" << std::endl; << " as the old one" << std::endl;
return 1; return false;
}
if (migrate_to == "sqlite3")
new_db = new Database_SQLite3(game_params.world_path);
#if USE_LEVELDB
else if (migrate_to == "leveldb")
new_db = new Database_LevelDB(game_params.world_path);
#endif
#if USE_REDIS
else if (migrate_to == "redis")
new_db = new Database_Redis(world_mt);
#endif
else {
errorstream << "Migration to " << migrate_to
<< " is not supported" << std::endl;
return 1;
} }
Database *old_db = ServerMap::createDatabase(backend, game_params.world_path, world_mt),
*new_db = ServerMap::createDatabase(migrate_to, game_params.world_path, world_mt);
u32 count = 0;
time_t last_update_time = 0;
bool &kill = *porting::signal_handler_killstatus();
std::vector<v3s16> blocks; std::vector<v3s16> blocks;
ServerMap &old_map = (ServerMap &) server->getMap(); old_db->listAllLoadableBlocks(blocks);
old_map.listAllLoadableBlocks(blocks);
int count = 0;
new_db->beginSave(); new_db->beginSave();
for (std::vector<v3s16>::iterator i = blocks.begin(); i != blocks.end(); i++) { for (std::vector<v3s16>::const_iterator it = blocks.begin(); it != blocks.end(); ++it) {
MapBlock *block = old_map.loadBlock(*i); if (kill) return false;
if (!block) {
errorstream << "Failed to load block " << PP(*i) << ", skipping it."; const std::string &data = old_db->loadBlock(*it);
if (!data.empty()) {
new_db->saveBlock(*it, data);
} else {
errorstream << "Failed to load block " << PP(*it) << ", skipping it." << std::endl;
} }
else { if (++count % 0xFF == 0 && time(NULL) - last_update_time >= 1) {
old_map.saveBlock(block, new_db); std::cerr << " Migrated " << count << " blocks, "
MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z)); << (100.0 * count / blocks.size()) << "% completed.\r";
sector->deleteBlock(block); new_db->endSave();
new_db->beginSave();
last_update_time = time(NULL);
} }
++count;
if (count % 500 == 0)
actionstream << "Migrated " << count << " blocks "
<< (100.0 * count / blocks.size()) << "% completed" << std::endl;
} }
std::cerr << std::endl;
new_db->endSave(); new_db->endSave();
delete old_db;
delete new_db; delete new_db;
actionstream << "Successfully migrated " << count << " blocks" << std::endl; actionstream << "Successfully migrated " << count << " blocks" << std::endl;
@ -984,3 +966,4 @@ static bool migrate_database(const GameParams &game_params, const Settings &cmd_
return true; return true;
} }

View File

@ -2018,20 +2018,7 @@ ServerMap::ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emer
conf.set("backend", "sqlite3"); conf.set("backend", "sqlite3");
} }
std::string backend = conf.get("backend"); std::string backend = conf.get("backend");
if (backend == "dummy") dbase = createDatabase(backend, savedir, conf);
dbase = new Database_Dummy();
else if (backend == "sqlite3")
dbase = new Database_SQLite3(savedir);
#if USE_LEVELDB
else if (backend == "leveldb")
dbase = new Database_LevelDB(savedir);
#endif
#if USE_REDIS
else if (backend == "redis")
dbase = new Database_Redis(conf);
#endif
else
throw BaseException("Unknown map backend");
if (!conf.updateConfigFile(conf_path.c_str())) if (!conf.updateConfigFile(conf_path.c_str()))
errorstream << "ServerMap::ServerMap(): Failed to update world.mt!" << std::endl; errorstream << "ServerMap::ServerMap(): Failed to update world.mt!" << std::endl;
@ -2813,7 +2800,7 @@ plan_b:
} }
bool ServerMap::loadFromFolders() { bool ServerMap::loadFromFolders() {
if(!dbase->initialized() && if (!dbase->initialized() &&
!fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite")) !fs::PathExists(m_savedir + DIR_DELIM + "map.sqlite"))
return true; return true;
return false; return false;
@ -2989,9 +2976,9 @@ void ServerMap::save(ModifiedState save_level)
void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst) void ServerMap::listAllLoadableBlocks(std::vector<v3s16> &dst)
{ {
if(loadFromFolders()){ if (loadFromFolders()) {
errorstream<<"Map::listAllLoadableBlocks(): Result will be missing " errorstream << "Map::listAllLoadableBlocks(): Result will be missing "
<<"all blocks that are stored in flat files"<<std::endl; << "all blocks that are stored in flat files." << std::endl;
} }
dbase->listAllLoadableBlocks(dst); dbase->listAllLoadableBlocks(dst);
} }
@ -3246,6 +3233,24 @@ bool ServerMap::loadSectorFull(v2s16 p2d)
} }
#endif #endif
Database *ServerMap::createDatabase(const std::string &name, const std::string &savedir, Settings &conf)
{
if (name == "sqlite3")
return new Database_SQLite3(savedir);
if (name == "dummy")
return new Database_Dummy();
#if USE_LEVELDB
else if (name == "leveldb")
return new Database_LevelDB(savedir);
#endif
#if USE_REDIS
else if (name == "redis")
return new Database_Redis(conf);
#endif
else
throw BaseException(std::string("Database backend ") + name + " not supported.");
}
void ServerMap::beginSave() void ServerMap::beginSave()
{ {
dbase->beginSave(); dbase->beginSave();

View File

@ -34,6 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/container.h" #include "util/container.h"
#include "nodetimer.h" #include "nodetimer.h"
class Settings;
class Database; class Database;
class ClientMap; class ClientMap;
class MapSector; class MapSector;
@ -445,6 +446,7 @@ public:
/* /*
Database functions Database functions
*/ */
static Database *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
// Verify we can read/write to the database // Verify we can read/write to the database
void verifyDatabase(); void verifyDatabase();