21 #include <mgmapi_internal.h>
23 #include <BaseString.hpp>
24 #include <Properties.hpp>
26 #include <OutputStream.hpp>
27 #include <SocketInputStream2.hpp>
29 #include "../../src/mgmsrv/Config.hpp"
36 unsigned int m_timeout;
37 void error(
const char*
msg, ...) ATTRIBUTE_FORMAT(printf, 2, 3)
50 ndbout_c(
" error: %d, line: %d, desc: %s",
58 m_handle(NULL), m_nodeid(0), m_verbose(
true), m_timeout(0)
60 const char* connect_string= getenv(
"NDB_CONNECTSTRING");
62 m_connect_str.
assign(connect_string);
74 ndb_mgm_disconnect_quiet(m_handle);
84 NDB_SOCKET_TYPE socket(
void)
const {
85 return _ndb_mgm_get_socket(m_handle);
88 NodeId nodeid(
void)
const {
92 const char* getConnectString()
const {
93 return m_connect_str.
c_str();
96 void setConnectString(
const char* connect_str) {
97 m_connect_str.
assign(connect_str);
100 bool set_timeout(
unsigned int timeout) {
105 error(
"set_timeout: failed to set timeout on handle");
111 void verbose(
bool yes =
true){
115 int last_error(
void)
const {
119 const char* last_error_message(
void)
const {
123 bool connect(
const char* connect_string = NULL,
124 int num_retries = 0,
int retry_delay_in_seconds = 0) {
125 assert(m_handle == NULL);
128 error(
"connect: ndb_mgm_create_handle failed");
134 connect_string : getConnectString()) != 0){
135 error(
"connect: ndb_mgm_set_connectstring failed");
141 error(
"connect: ndb_mgm_set_timeout failed");
145 if (
ndb_mgm_connect(m_handle,num_retries,retry_delay_in_seconds,0) != 0){
146 error(
"connect: ndb_mgm_connect failed");
151 int major, minor, build;
154 sizeof(buf), buf) != 1)
156 error(
"connect: ndb_get_version failed");
163 error(
"connect: could not get nodeid of connected mgmd");
170 bool is_connected(
void) {
172 error(
"is_connected: no handle");
176 error(
"is_connected: not connected");
182 bool disconnect(
void) {
184 error(
"disconnect: ndb_mgm_disconnect failed");
194 bool restart(
bool abort =
false) {
195 if (!is_connected()){
196 error(
"restart: not connected");
201 int node_list= m_nodeid;
211 error(
"restart: failed to restart node %d, restarted: %d",
212 m_nodeid, restarted);
218 bool call(
const char* cmd,
const Properties& args,
220 const char* bulk = NULL,
221 bool name_value_pairs =
true){
223 if (!is_connected()){
224 error(
"call: not connected");
230 if (out.println(cmd)){
231 error(
"call: println failed at line %d", __LINE__);
237 while((name = iter.next()) != NULL) {
243 args.getTypeOf(name, &t);
245 case PropertiesType_Uint32:
246 args.get(name, &val_i);
247 if (out.println(
"%s: %d", name, val_i)){
248 error(
"call: println failed at line %d", __LINE__);
252 case PropertiesType_Uint64:
253 args.get(name, &val_64);
254 if (out.println(
"%s: %Ld", name, val_64)){
255 error(
"call: println failed at line %d", __LINE__);
259 case PropertiesType_char:
260 args.get(name, val_s);
261 if (out.println(
"%s: %s", name, val_s.
c_str())){
262 error(
"call: println failed at line %d", __LINE__);
267 case PropertiesType_Properties:
275 if (out.print(
"\n")){
276 error(
"call: print('\n') failed at line %d", __LINE__);
281 if (bulk && out.println(bulk)){
282 error(
"call: print('<bulk>') failed at line %d", __LINE__);
292 error(
"call: could not read reply command");
297 if (buf != cmd_reply){
298 error(
"call: unexpected reply command, expected: '%s', got '%s'",
299 cmd_reply, buf.
c_str());
312 if (name_value_pairs)
317 if (buf.
split(name_value_pair,
":", 2) != 2){
318 error(
"call: illegal name value pair '%s' received", buf.
c_str());
322 reply.
put(name_value_pair[0].trim(
" ").c_str(),
323 name_value_pair[1].trim(
" ").c_str());
329 reply.
put(
"line", line++, buf.
c_str());
333 error(
"call: should never come here");
342 if (!is_connected()){
343 error(
"get_config: not connected");
350 error(
"get_config: ndb_mgm_get_configuration failed");
354 config.m_configValues= conf;
358 bool set_config(
Config& config){
360 if (!is_connected()){
361 error(
"set_config: not connected");
365 if (ndb_mgm_set_configuration(m_handle,
366 config.values()) != 0)
368 error(
"set_config: ndb_mgm_set_configuration failed");
374 bool end_session(
void){
375 if (!is_connected()){
376 error(
"end_session: not connected");
381 error(
"end_session: ndb_mgm_end_session failed");
391 NodeType(Uint32 node_type) {
392 const char* str= NULL;
395 m_str.assfmt(
"%s(%s)", alias, str);
398 const char* c_str() {
return m_str.c_str(); }