commandinterpreter.cpp

来自「MySQL源码文件5.X系列, 可自已编译到服务器」· C++ 代码 · 共 1,989 行 · 第 1/5 页

CPP
1,989
字号
/* 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; version 2 of the License.   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, bool interactive=1, int *error= 0);private:  void printError();  int execute_impl(const char *_line, bool interactive=1);  /**   *   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   *   @return: 0 if analyseAfterFirstToken succeeds, otherwise -1    */  int  analyseAfterFirstToken(int processId, char* allAfterFirstTokenCstr);  int  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.   */  int  executeHelp(char* parameters);  int  executeShow(char* parameters);  int  executePurge(char* parameters);  int  executeConnect(char* parameters, bool interactive);  int  executeShutdown(char* parameters);  void executeRun(char* parameters);  void executeInfo(char* parameters);  void executeClusterLog(char* parameters);public:  int  executeStop(int processId, const char* parameters, bool all);  int  executeEnterSingleUser(char* parameters);  int  executeExitSingleUser(char* parameters);  int  executeStart(int processId, const char* parameters, bool all);  int  executeRestart(int processId, const char* parameters, bool all);  int  executeLogLevel(int processId, const char* parameters, bool all);  int  executeError(int processId, const char* parameters, bool all);  int  executeLog(int processId, const char* parameters, bool all);  int  executeLogIn(int processId, const char* parameters, bool all);  int  executeLogOut(int processId, const char* parameters, bool all);  int  executeLogOff(int processId, const char* parameters, bool all);  int  executeTestOn(int processId, const char* parameters, bool all);  int  executeTestOff(int processId, const char* parameters, bool all);  int  executeSet(int processId, const char* parameters, bool all);  int  executeGetStat(int processId, const char* parameters, bool all);  int  executeStatus(int processId, const char* parameters, bool all);  int  executeEventReporting(int processId, const char* parameters, bool all);  int  executeDumpState(int processId, const char* parameters, bool all);  int  executeStartBackup(char * parameters);  int  executeAbortBackup(char * parameters);  int  executeStop(Vector<BaseString> &command_list, unsigned command_pos,                   int *node_ids, int no_of_nodes);  int  executeRestart(Vector<BaseString> &command_list, unsigned command_pos,                      int *node_ids, int no_of_nodes);  int  executeRep(char* parameters);  void executeCpc(char * parameters);public:  bool connect(bool interactive);  bool disconnect();  /**   * A execute function definition   */public:  typedef int (CommandInterpreter::* ExecuteFunction)(int processId, 						       const char * param, 						       bool all);    struct CommandFunctionPair {    const char * command;    ExecuteFunction executeFunction;  };private:  /**   *    */  int  executeForAll(const char * cmd, 		     ExecuteFunction fun,		     const char * param);  NdbMgmHandle m_mgmsrv;  NdbMgmHandle m_mgmsrv2;  const char *m_constr;  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;  NdbMutex *m_print_mutex;};struct event_thread_param {  NdbMgmHandle *m;  NdbMutex **p;};NdbMutex* print_mutex;/* * 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, bool interactive, int *error){  return m_cmd->execute(_line,_try_reconnect,interactive, 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, bool interactive, 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, interactive, error);}/***************************************************************************** * HELP *****************************************************************************/static const char* helpText ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help\n""---------------------------------------------------------------------------\n""HELP                                   Print help text\n""HELP COMMAND                           Print detailed help for COMMAND(e.g. SHOW)\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 data node (started with -n)\n""<id> RESTART [-n] [-i]                 Restart data or management server node\n""<id> STOP                              Stop data or management server node\n""ENTER SINGLE USER MODE <id>            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 Print information about cluster\n\n""SHOW               Print information about cluster.The status reported is from\n""                   the perspective of the data nodes. API and Management Server nodes\n""                   are only reported as connected once the data nodes have started.\n" #if 0"SHOW CONFIG        Print configuration (in initial config file format)\n" "SHOW PARAMETERS    Print information about configuration parameters\n\n"#endif;static const char* helpTextHelp ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for HELP command\n""---------------------------------------------------------------------------\n""HELP List available commands of NDB Cluster Management Client\n\n""HELP               List available commands.\n";static const char* helpTextBackup ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for BACKUP command\n""---------------------------------------------------------------------------\n""BACKUP  A backup is a snapshot of the database at a given time. \n""        The backup consists of three main parts:\n\n""        Metadata: the names and definitions of all database tables. \n""        Table records: the data actually stored in the database tables \n""        at the time that the backup was made.\n""        Transaction log: a sequential record telling how \n""        and when data was stored in the database.\n\n""        Backups are stored on each data node in the cluster that \n""        participates in the backup.\n\n""        The cluster log records backup related events (such as \n""        backup started, aborted, finished).\n";static const char* helpTextStartBackup ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for START BACKUP command\n""---------------------------------------------------------------------------\n""START BACKUP  Start a cluster backup\n\n""START BACKUP [NOWAIT | WAIT STARTED | WAIT COMPLETED]\n""                   Start a backup for the cluster.\n""                   Each backup gets an ID number that is reported to the\n""                   user. This ID number can help you find the backup on the\n""                   file system, or ABORT BACKUP if you wish to cancel a \n""                   running backup.\n\n""                   NOWAIT \n""                     Start a cluster backup and return immediately.\n""                     The management client will return control directly\n""                     to the user without waiting for the backup\n""                     to have started.\n""                     The status of the backup is recorded in the Cluster log.\n""                   WAIT STARTED\n""                     Start a cluster backup and return until the backup has\n""                     started. The management client will wait for the backup \n""                     to have started before returning control to the user.\n""                   WAIT COMPLETED\n""                     Start a cluster backup and return until the backup has\n""                     completed. The management client will wait for the backup\n""                     to complete before returning control to the user.\n";static const char* helpTextAbortBackup ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for ABORT BACKUP command\n""---------------------------------------------------------------------------\n""ABORT BACKUP  Abort a cluster backup\n\n""ABORT BACKUP <backup id>  \n""                   Abort a backup that is already in progress.\n""                   The backup id can be seen in the cluster log or in the\n""                   output of the START BACKUP command.\n";static const char* helpTextShutdown ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for SHUTDOWN command\n""---------------------------------------------------------------------------\n""SHUTDOWN  Shutdown the cluster\n\n""SHUTDOWN           Shutdown the data nodes and management nodes.\n""                   MySQL Servers and NDBAPI nodes are currently not \n""                   shut down by issuing this command.\n";static const char* helpTextClusterlogOn ="---------------------------------------------------------------------------\n"" NDB Cluster -- Management Client -- Help for CLUSTERLOG ON command\n""---------------------------------------------------------------------------\n""CLUSTERLOG ON  Enable Cluster logging\n\n""CLUSTERLOG ON [<severity>] ... \n""                   Turn the cluster log on.\n""                   It tells management server which severity levels\n"

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?