📄 commandinterpreter.cpp
字号:
/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <ndb_global.h>#include <my_sys.h>//#define HAVE_GLOBAL_REPLICATION#include <Vector.hpp>#ifdef HAVE_GLOBAL_REPLICATION#include "../rep/repapi/repapi.h"#endif#include <mgmapi.h>#include <util/BaseString.hpp>class MgmtSrvr;/** * @class CommandInterpreter * @brief Reads command line in management client * * This class has one public method which reads a command line * from a stream. It then interpret that commmand line and calls a suitable * method in the MgmtSrvr class which executes the command. * * For command syntax, see the HELP command. */ class CommandInterpreter {public: /** * Constructor * @param mgmtSrvr: Management server to use when executing commands */ CommandInterpreter(const char *, int verbose); ~CommandInterpreter(); /** * Reads one line from the stream, parse the line to find * a command and then calls a suitable method which executes * the command. * * @return true until quit/bye/exit has been typed */ int execute(const char *_line, int _try_reconnect=-1, int *error= 0);private: void printError(); int execute_impl(const char *_line); /** * Analyse the command line, after the first token. * * @param processId: DB process id to send command to or -1 if * command will be sent to all DB processes. * @param allAfterFirstToken: What the client gave after the * first token on the command line */ void analyseAfterFirstToken(int processId, char* allAfterFirstTokenCstr); void executeCommand(Vector<BaseString> &command_list, unsigned command_pos, int *node_ids, int no_of_nodes); /** * Parse the block specification part of the LOG* commands, * things after LOG*: [BLOCK = {ALL|<blockName>+}] * * @param allAfterLog: What the client gave after the second token * (LOG*) on the command line * @param blocks, OUT: ALL or name of all the blocks * @return: true if correct syntax, otherwise false */ bool parseBlockSpecification(const char* allAfterLog, Vector<const char*>& blocks); /** * A bunch of execute functions: Executes one of the commands * * @param processId: DB process id to send command to * @param parameters: What the client gave after the command name * on the command line. * For example if complete input from user is: "1 LOGLEVEL 22" then the * parameters argument is the string with everything after LOGLEVEL, in * this case "22". Each function is responsible to check the parameters * argument. */ void executeHelp(char* parameters); void executeShow(char* parameters); void executeConnect(char* parameters); void executePurge(char* parameters); int executeShutdown(char* parameters); void executeRun(char* parameters); void executeInfo(char* parameters); void executeClusterLog(char* parameters);public: void executeStop(int processId, const char* parameters, bool all); void executeStop(Vector<BaseString> &command_list, unsigned command_pos, int *node_ids, int no_of_nodes); void executeEnterSingleUser(char* parameters); void executeExitSingleUser(char* parameters); void executeStart(int processId, const char* parameters, bool all); void executeRestart(int processId, const char* parameters, bool all); void executeRestart(Vector<BaseString> &command_list, unsigned command_pos, int *node_ids, int no_of_nodes); void executeLogLevel(int processId, const char* parameters, bool all); void executeError(int processId, const char* parameters, bool all); void executeLog(int processId, const char* parameters, bool all); void executeLogIn(int processId, const char* parameters, bool all); void executeLogOut(int processId, const char* parameters, bool all); void executeLogOff(int processId, const char* parameters, bool all); void executeTestOn(int processId, const char* parameters, bool all); void executeTestOff(int processId, const char* parameters, bool all); void executeSet(int processId, const char* parameters, bool all); void executeGetStat(int processId, const char* parameters, bool all); void executeStatus(int processId, const char* parameters, bool all); void executeEventReporting(int processId, const char* parameters, bool all); void executeDumpState(int processId, const char* parameters, bool all); int executeStartBackup(char * parameters); void executeAbortBackup(char * parameters); void executeRep(char* parameters); void executeCpc(char * parameters);public: bool connect(); bool disconnect(); /** * A execute function definition */public: typedef void (CommandInterpreter::* ExecuteFunction)(int processId, const char * param, bool all); struct CommandFunctionPair { const char * command; ExecuteFunction executeFunction; };private: /** * */ void executeForAll(const char * cmd, ExecuteFunction fun, const char * param); NdbMgmHandle m_mgmsrv; NdbMgmHandle m_mgmsrv2; bool m_connected; int m_verbose; int try_reconnect; int m_error;#ifdef HAVE_GLOBAL_REPLICATION NdbRepHandle m_repserver; const char *rep_host; bool rep_connected;#endif struct NdbThread* m_event_thread;};/* * Facade object for CommandInterpreter */#include "ndb_mgmclient.hpp"#include "ndb_mgmclient.h"Ndb_mgmclient::Ndb_mgmclient(const char *host,int verbose){ m_cmd= new CommandInterpreter(host,verbose);}Ndb_mgmclient::~Ndb_mgmclient(){ delete m_cmd;}int Ndb_mgmclient::execute(const char *_line, int _try_reconnect, int *error){ return m_cmd->execute(_line,_try_reconnect,error);}intNdb_mgmclient::disconnect(){ return m_cmd->disconnect();}extern "C" { Ndb_mgmclient_handle ndb_mgmclient_handle_create(const char *connect_string) { return (Ndb_mgmclient_handle) new Ndb_mgmclient(connect_string); } int ndb_mgmclient_execute(Ndb_mgmclient_handle h, int argc, char** argv) { return ((Ndb_mgmclient*)h)->execute(argc, argv, 1); } int ndb_mgmclient_handle_destroy(Ndb_mgmclient_handle h) { delete (Ndb_mgmclient*)h; return 0; }}/* * The CommandInterpreter */#include <mgmapi.h>#include <mgmapi_debug.h>#include <version.h>#include <NdbAutoPtr.hpp>#include <NdbOut.hpp>#include <NdbSleep.h>#include <NdbMem.h>#include <EventLogger.hpp>#include <signaldata/SetLogLevelOrd.hpp>#include <signaldata/GrepImpl.hpp>#ifdef HAVE_GLOBAL_REPLICATION#endif // HAVE_GLOBAL_REPLICATION#include "MgmtErrorReporter.hpp"#include <Parser.hpp>#include <SocketServer.hpp>#include <util/InputStream.hpp>#include <util/OutputStream.hpp>int Ndb_mgmclient::execute(int argc, char** argv, int _try_reconnect, int *error){ if (argc <= 0) return 0; BaseString _line(argv[0]); for (int i= 1; i < argc; i++) { _line.appfmt(" %s", argv[i]); } return m_cmd->execute(_line.c_str(),_try_reconnect, error);}/***************************************************************************** * HELP *****************************************************************************/static const char* helpText ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help\n""---------------------------------------------------------------------------\n""HELP Print help text\n""HELP SHOW Help for SHOW command\n"#ifdef HAVE_GLOBAL_REPLICATION"HELP REPLICATION Help for global replication\n"#endif // HAVE_GLOBAL_REPLICATION#ifdef VM_TRACE // DEBUG ONLY"HELP DEBUG Help for debug compiled version\n"#endif"SHOW Print information about cluster\n"#if 0"SHOW CONFIG Print configuration\n""SHOW PARAMETERS Print configuration parameters\n"#endif"START BACKUP [NOWAIT | WAIT STARTED | WAIT COMPLETED]\n"" Start backup (default WAIT COMPLETED)\n""ABORT BACKUP <backup id> Abort backup\n""SHUTDOWN Shutdown all processes in cluster\n""CLUSTERLOG ON [<severity>] ... Enable Cluster logging\n""CLUSTERLOG OFF [<severity>] ... Disable Cluster logging\n""CLUSTERLOG TOGGLE [<severity>] ... Toggle severity filter on/off\n""CLUSTERLOG INFO Print cluster log information\n""<id> START Start DB node (started with -n)\n""<id> RESTART [-n] [-i] Restart DB node\n""<id> STOP Stop DB node\n""ENTER SINGLE USER MODE <api-node> Enter single user mode\n""EXIT SINGLE USER MODE Exit single user mode\n""<id> STATUS Print status\n""<id> CLUSTERLOG {<category>=<level>}+ Set log level for cluster log\n"#ifdef HAVE_GLOBAL_REPLICATION"REP CONNECT <host:port> Connect to REP server on host:port\n"#endif"PURGE STALE SESSIONS Reset reserved nodeid's in the mgmt server\n""CONNECT [<connectstring>] Connect to management server (reconnect if already connected)\n""QUIT Quit management client\n";static const char* helpTextShow ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for SHOW command\n""---------------------------------------------------------------------------\n""SHOW prints NDB Cluster information\n\n""SHOW Print information about cluster\n" #if 0"SHOW CONFIG Print configuration (in initial config file format)\n" "SHOW PARAMETERS Print information about configuration parameters\n\n"#endif;#ifdef HAVE_GLOBAL_REPLICATIONstatic const char* helpTextRep ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for Global Replication\n""---------------------------------------------------------------------------\n""Commands should be executed on the standby NDB Cluster\n""These features are in an experimental release state.\n""\n""Simple Commands:\n""REP START Start Global Replication\n" "REP START REQUESTOR Start Global Replication Requestor\n" "REP STATUS Show Global Replication status\n" "REP STOP Stop Global Replication\n""REP STOP REQUESTOR Stop Global Replication Requestor\n""\n" "Advanced Commands:\n""REP START <protocol> Starts protocol\n""REP STOP <protocol> Stops protocol\n""<protocol> = TRANSFER | APPLY | DELETE\n""\n"#ifdef VM_TRACE // DEBUG ONLY"Debugging commands:\n""REP DELETE Removes epochs stored in primary and standy systems\n""REP DROP <tableid> Drop a table in SS identified by table id\n""REP SLOWSTOP Stop Replication (Tries to synchonize with primary)\n" "REP FASTSTOP Stop Replication (Stops in consistent state)\n" "<component> = SUBSCRIPTION\n"" METALOG | METASCAN | DATALOG | DATASCAN\n"" REQUESTOR | TRANSFER | APPLY | DELETE\n"#endif;#endif // HAVE_GLOBAL_REPLICATION#ifdef VM_TRACE // DEBUG ONLYstatic const char* helpTextDebug ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for Debugging (Internal use only)\n""---------------------------------------------------------------------------\n""SHOW PROPERTIES Print config properties object\n""<id> LOGLEVEL {<category>=<level>}+ Set log level\n"#ifdef ERROR_INSERT"<id> ERROR <errorNo> Inject error into NDB node\n"#endif"<id> LOG [BLOCK = {ALL|<block>+}] Set logging on in & out signals\n""<id> LOGIN [BLOCK = {ALL|<block>+}] Set logging on in signals\n""<id> LOGOUT [BLOCK = {ALL|<block>+}] Set logging on out signals\n""<id> LOGOFF [BLOCK = {ALL|<block>+}] Unset signal logging\n""<id> TESTON Start signal logging\n""<id> TESTOFF Stop signal logging\n""<id> SET <configParamName> <value> Update configuration variable\n""<id> DUMP <arg> Dump system state to cluster.log\n""<id> GETSTAT Print statistics\n""\n""<id> = ALL | Any database node id\n";#endifstatic boolconvert(const char* s, int& val) { if (s == NULL) return false; if (strlen(s) == 0) return false; errno = 0; char* p; long v = strtol(s, &p, 10); if (errno != 0) return false; if (p != &s[strlen(s)]) return false; val = v; return true;}/* * Constructor */CommandInterpreter::CommandInterpreter(const char *_host,int verbose) : m_verbose(verbose){ m_mgmsrv = ndb_mgm_create_handle(); if(m_mgmsrv == NULL) { ndbout_c("Cannot create handle to management server."); exit(-1); } m_mgmsrv2 = ndb_mgm_create_handle(); if(m_mgmsrv2 == NULL) { ndbout_c("Cannot create 2:nd handle to management server."); exit(-1); } if (ndb_mgm_set_connectstring(m_mgmsrv, _host)) { printError(); exit(-1); } m_connected= false; m_event_thread= 0; try_reconnect = 0;#ifdef HAVE_GLOBAL_REPLICATION rep_host = NULL; m_repserver = NULL; rep_connected = false;#endif}/* * Destructor */CommandInterpreter::~CommandInterpreter() { disconnect(); ndb_mgm_destroy_handle(&m_mgmsrv); ndb_mgm_destroy_handle(&m_mgmsrv2);}static bool emptyString(const char* s) { if (s == NULL) { return true; } for (unsigned int i = 0; i < strlen(s); ++i) { if (! isspace(s[i])) { return false; } } return true;}voidCommandInterpreter::printError() { if (ndb_mgm_check_connection(m_mgmsrv)) { m_connected= false; disconnect(); } ndbout_c("* %5d: %s", ndb_mgm_get_latest_error(m_mgmsrv), ndb_mgm_get_latest_error_msg(m_mgmsrv)); ndbout_c("* %s", ndb_mgm_get_latest_error_desc(m_mgmsrv));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -