RepApiService.cpp 8.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
/* 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 <Parser.hpp>
#include <NdbOut.hpp>
#include <Properties.hpp>
#include <socket_io.h>
#include "RepApiService.hpp"
#include "RepApiInterpreter.hpp"
#include "repapi/repapi.h"
#include <NdbMutex.h>
#include <OutputStream.hpp>

/**
   const char * name;
   const char * realName;
   const Type type;
   const ArgType argType;
   const ArgRequired argRequired;
   const ArgMinMax argMinMax;
   const int minVal;
   const int maxVal;
   void (T::* function)(const class Properties & args);
   const char * description;
*/

#define REP_CMD(name, fun, desc) \
 { name, \
   0, \
   ParserRow<RepApiSession>::Cmd, \
   ParserRow<RepApiSession>::String, \
   ParserRow<RepApiSession>::Optional, \
   ParserRow<RepApiSession>::IgnoreMinMax, \
   0, 0, \
   fun, \
   desc }

#define REP_ARG(name, type, opt, desc) \
 { name, \
   0, \
   ParserRow<RepApiSession>::Arg, \
   ParserRow<RepApiSession>::type, \
   ParserRow<RepApiSession>::opt, \
   ParserRow<RepApiSession>::IgnoreMinMax, \
   0, 0, \
   0, \
  desc }

#define REP_ARG2(name, type, opt, min, max, desc) \
 { name, \
   0, \
   ParserRow<RepApiSession>::Arg, \
   ParserRow<RepApiSession>::type, \
   ParserRow<RepApiSession>::opt, \
   ParserRow<RepApiSession>::IgnoreMinMax, \
   min, max, \
   0, \
  desc }

#define REP_END() \
 { 0, \
   0, \
   ParserRow<RepApiSession>::Arg, \
   ParserRow<RepApiSession>::Int, \
   ParserRow<RepApiSession>::Optional, \
   ParserRow<RepApiSession>::IgnoreMinMax, \
   0, 0, \
   0, \
   0 }

#define REP_CMD_ALIAS(name, realName, fun) \
 { name, \
   realName, \
   ParserRow<RepApiSession>::CmdAlias, \
   ParserRow<RepApiSession>::Int, \
   ParserRow<RepApiSession>::Optional, \
   ParserRow<RepApiSession>::IgnoreMinMax, \
   0, 0, \
   0, \
   0 }

#define REP_ARG_ALIAS(name, realName, fun) \
 { name, \
   realName, \
   ParserRow<RepApiSession>::ArgAlias, \
   ParserRow<RepApiSession>::Int, \
   ParserRow<RepApiSession>::Optional, \
   ParserRow<RepApiSession>::IgnoreMinMax, \
   0, 0, \
   0, \
   0 }


const
ParserRow<RepApiSession> commands[] = 
{
  
  REP_CMD("rep" , &RepApiSession::execCommand, ""),
    REP_ARG("request",     Int, Mandatory,  "Grep::Request."),
    REP_ARG("id",     Int, Mandatory,  "Replication id "),
    REP_ARG("epoch",     Int, Optional,  "Epoch. Used by stop epoch ..."),

  REP_CMD("rep status" , &RepApiSession::getStatus, ""),
    REP_ARG("request",     Int, Optional,  "Grep::Request."),

  REP_CMD("rep query" , &RepApiSession::query, ""),
  REP_ARG("id",     Int, Mandatory,  "Replication Id"),
  REP_ARG("counter",     Int, Mandatory,  "QueryCounter."),
  REP_ARG("request",     Int, Mandatory,  "Grep::Request."),
  
  REP_END()
};
RepApiSession::RepApiSession(NDB_SOCKET_TYPE sock,
			       class RepApiInterpreter & rep)
  : SocketServer::Session(sock)
  , m_rep(rep)
{
  m_input = new SocketInputStream(sock);
  m_output = new SocketOutputStream(sock);
  m_parser = new Parser<RepApiSession>(commands, *m_input, true, true, true);
}

RepApiSession::RepApiSession(FILE * f, class RepApiInterpreter & rep)
  : SocketServer::Session(1)
  , m_rep(rep)
{
  m_input = new FileInputStream(f);
  m_parser = new Parser<RepApiSession>(commands, *m_input, true, true, true);
}
  
RepApiSession::~RepApiSession() 
{
  delete m_input;
  delete m_parser;
}

void
RepApiSession::runSession()
{
  Parser_t::Context ctx;
  while(!m_stop){
    m_parser->run(ctx, * this); 
    if(ctx.m_currentToken == 0)
      break;

    switch(ctx.m_status){
    case Parser_t::Ok:
      for(size_t i = 0; i<ctx.m_aliasUsed.size(); i++)
	ndbout_c("Used alias: %s -> %s", 
		 ctx.m_aliasUsed[i]->name, ctx.m_aliasUsed[i]->realName);
      break;
    case Parser_t::NoLine:
    case Parser_t::EmptyLine:
      break;
    default:
      break;
    }
  }
  NDB_CLOSE_SOCKET(m_socket);
}

void
RepApiSession::execCommand(Parser_t::Context & /* unused */, 
			   const class Properties & args)
{
  Uint32  err;
  Uint32  replicationId;
  args.get("id", &replicationId);
  Properties * result  = m_rep.execCommand(args);
  if(result == NULL) {
    m_output->println("global replication reply");
    m_output->println("result: %d", -1);
    m_output->println("id: %d",replicationId);
    m_output->println("");
    return;
  }
  result->get("err", &err);
  m_output->println("global replication reply");
  m_output->println("result: %d", err);
  m_output->println("id: %d", 0);
  m_output->println("");
  delete result;
}


void
RepApiSession::getStatus(Parser_t::Context & /* unused */, 
			 const class Properties & args)
{
  Uint32  err;
  Properties * result  = m_rep.getStatus();
  result->get("err", &err);
  Uint32 subId;
  result->get("subid", &subId);
  Uint32 subKey;
  result->get("subkey", &subKey);
  Uint32 connected_rep;
  result->get("connected_rep", &connected_rep);
  Uint32 connected_db;
  result->get("connected_db", &connected_db);
  Uint32 state;
  result->get("state", &state);
  Uint32 state_sub;
  result->get("state", &state_sub);

  m_output->println("global replication status reply");
  m_output->println("result: %d",0);
  m_output->println("id: %d",0);
  m_output->println("subid: %d", subId);
  m_output->println("subkey: %d", subKey);
  m_output->println("connected_rep: %d", connected_rep);
  m_output->println("connected_db: %d", connected_db);
  m_output->println("state_sub: %d", state_sub);
  m_output->println("state: %d", state);
  m_output->println("");
  delete result;
}


void
RepApiSession::query(Parser_t::Context & /* unused */, 
			const class Properties & args)
{
  Uint32  err;
  Uint32 counter, replicationId;
  args.get("counter", &counter);
  args.get("id", &replicationId);
  Properties * result  = m_rep.query(counter, replicationId);
  if(result == NULL) {
    m_output->println("global replication query reply");
    m_output->println("result: %s","Failed");
    m_output->println("id: %d",replicationId);
    m_output->println("");
    return;
  }
    
  BaseString first;
  BaseString last;
  Uint32 subid = 0, subkey = 0, no_of_nodegroups = 0;
  Uint32 connected_rep = 0, connected_db = 0;
  Uint32 state = 0 , state_sub = 0;
  result->get("err", &err);
  result->get("no_of_nodegroups", &no_of_nodegroups);
  result->get("subid", &subid);
  result->get("subkey", &subkey);
  result->get("connected_rep", &connected_rep);
  result->get("connected_db", &connected_db);
  result->get("first", first);
  result->get("last", last);
  result->get("state", &state);
  result->get("state_sub", &state_sub);
  m_output->println("global replication query reply");
  m_output->println("result: %s","Ok");
  m_output->println("id: %d",replicationId);
  m_output->println("no_of_nodegroups: %d",no_of_nodegroups);
  m_output->println("subid: %d", subid);
  m_output->println("subkey: %d", subkey);
  m_output->println("connected_rep: %d", connected_rep);
  m_output->println("connected_db: %d", connected_db);
  m_output->println("state_sub: %d", state_sub);
  m_output->println("state: %d", state);
  m_output->println("first: %s", first.c_str());
  m_output->println("last: %s", last.c_str());
  m_output->println("");
  delete result;
}



static const char *
propToString(Properties *prop, const char *key) {
  static char buf[32];
  const char *retval = NULL;
  PropertiesType pt;

  prop->getTypeOf(key, &pt);
  switch(pt) {
  case PropertiesType_Uint32:
    Uint32 val;
    prop->get(key, &val);
    snprintf(buf, sizeof buf, "%d", val);
    retval = buf;
    break;
  case PropertiesType_char:
    const char *str;
    prop->get(key, &str);
    retval = str;
    break;
  default:
    snprintf(buf, sizeof buf, "(unknown)");
    retval = buf;
  }
  return retval;
}

void
RepApiSession::printProperty(Properties *prop, const char *key) {
  m_output->println("%s: %s", key, propToString(prop, key));
}

void
RepApiSession::stopSession(){

}