forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServices.cpp
2525 lines (2137 loc) · 66.2 KB
/
Services.cpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2003, 2021, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
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, version 2.0, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ndb_global.h>
#include <uucode.h>
#include <socket_io.h>
#include <util/version.h>
#include <mgmapi.h>
#include <EventLogger.hpp>
#include <signaldata/SetLogLevelOrd.hpp>
#include <LogLevel.hpp>
#include <BaseString.hpp>
#include <ConfigValues.hpp>
#include <mgmapi_configuration.hpp>
#include <Vector.hpp>
#include "Services.hpp"
#include "../mgmapi/ndb_logevent.hpp"
#include "ndb_mgmd_error.h"
#include <ndb_base64.h>
#include <ndberror.h>
extern bool g_StopServer;
extern bool g_RestartServer;
extern EventLogger * g_eventLogger;
/**
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 MGM_CMD(name, fun, desc) \
{ name, \
0, \
ParserRow<MgmApiSession>::Cmd, \
ParserRow<MgmApiSession>::String, \
ParserRow<MgmApiSession>::Optional, \
ParserRow<MgmApiSession>::IgnoreMinMax, \
0, 0, \
fun, \
desc, 0 }
#define MGM_ARG(name, type, opt, desc) \
{ name, \
0, \
ParserRow<MgmApiSession>::Arg, \
ParserRow<MgmApiSession>::type, \
ParserRow<MgmApiSession>::opt, \
ParserRow<MgmApiSession>::IgnoreMinMax, \
0, 0, \
0, \
desc, 0 }
#define MGM_ARG2(name, type, opt, min, max, desc) \
{ name, \
0, \
ParserRow<MgmApiSession>::Arg, \
ParserRow<MgmApiSession>::type, \
ParserRow<MgmApiSession>::opt, \
ParserRow<MgmApiSession>::IgnoreMinMax, \
min, max, \
0, \
desc, 0 }
#define MGM_END() \
{ 0, \
0, \
ParserRow<MgmApiSession>::End, \
ParserRow<MgmApiSession>::Int, \
ParserRow<MgmApiSession>::Optional, \
ParserRow<MgmApiSession>::IgnoreMinMax, \
0, 0, \
0, \
0, 0 }
#define MGM_CMD_ALIAS(name, realName, fun) \
{ name, \
realName, \
ParserRow<MgmApiSession>::CmdAlias, \
ParserRow<MgmApiSession>::Int, \
ParserRow<MgmApiSession>::Optional, \
ParserRow<MgmApiSession>::IgnoreMinMax, \
0, 0, \
0, \
0, 0 }
#define MGM_ARG_ALIAS(name, realName, fun) \
{ name, \
realName, \
ParserRow<MgmApiSession>::ArgAlias, \
ParserRow<MgmApiSession>::Int, \
ParserRow<MgmApiSession>::Optional, \
ParserRow<MgmApiSession>::IgnoreMinMax, \
0, 0, \
0, \
0, 0 }
const
ParserRow<MgmApiSession> commands[] = {
MGM_CMD("get config", &MgmApiSession::getConfig, ""),
MGM_ARG("version", Int, Mandatory, "Configuration version number"),
MGM_ARG("node", Int, Optional, "Node ID"),
MGM_ARG("nodetype", Int, Optional, "Type of requesting node"),
MGM_ARG("from_node", Int, Optional, "Node to get config from"),
MGM_CMD("get nodeid", &MgmApiSession::get_nodeid, ""),
MGM_ARG("version", Int, Mandatory, "Configuration version number"),
MGM_ARG("nodetype", Int, Mandatory, "Node type"),
MGM_ARG("transporter", String, Optional, "Transporter type"),
MGM_ARG("nodeid", Int, Optional, "Node ID"),
MGM_ARG("user", String, Mandatory, "Password"),
MGM_ARG("password", String, Mandatory, "Password"),
MGM_ARG("public key", String, Mandatory, "Public key"),
MGM_ARG("endian", String, Optional, "Endianness"),
MGM_ARG("name", String, Optional, "Name of connection"),
MGM_ARG("timeout", Int, Optional, "Timeout in seconds"),
MGM_ARG("log_event", Int, Optional, "Log failure in cluster log"),
MGM_CMD("get version", &MgmApiSession::getVersion, ""),
MGM_CMD("get status", &MgmApiSession::getStatus, ""),
MGM_ARG("types", String, Optional, "Types"),
MGM_CMD("get info clusterlog", &MgmApiSession::getInfoClusterLog, ""),
MGM_CMD("get cluster loglevel", &MgmApiSession::getClusterLogLevel, ""),
MGM_CMD("restart node", &MgmApiSession::restart_v1, ""),
MGM_ARG("node", String, Mandatory, "Nodes to restart"),
MGM_ARG("initialstart", Int, Optional, "Initial start"),
MGM_ARG("nostart", Int, Optional, "No start"),
MGM_ARG("abort", Int, Optional, "Abort"),
MGM_CMD("restart node v2", &MgmApiSession::restart_v2, ""),
MGM_ARG("node", String, Mandatory, "Nodes to restart"),
MGM_ARG("initialstart", Int, Optional, "Initial start"),
MGM_ARG("nostart", Int, Optional, "No start"),
MGM_ARG("abort", Int, Optional, "Abort"),
MGM_ARG("force", Int, Optional, "Force"),
MGM_CMD("restart all", &MgmApiSession::restartAll, ""),
MGM_ARG("initialstart", Int, Optional, "Initial start"),
MGM_ARG("nostart", Int, Optional, "No start"),
MGM_ARG("abort", Int, Optional, "Abort"),
MGM_CMD("insert error", &MgmApiSession::insertError, ""),
MGM_ARG("node", Int, Mandatory, "Node to receive error"),
MGM_ARG("error", Int, Mandatory, "Errorcode to insert"),
MGM_ARG("extra", Int, Optional, "Extra info to error insert"),
MGM_CMD("set trace", &MgmApiSession::setTrace, ""),
MGM_ARG("node", Int, Mandatory, "Node"),
MGM_ARG("trace", Int, Mandatory, "Trace number"),
MGM_CMD("log signals", &MgmApiSession::logSignals, ""),
MGM_ARG("node", Int, Mandatory, "Node"),
MGM_ARG("blocks", String, Mandatory, "Blocks (space separated)"),
MGM_ARG("in", Int, Mandatory, "Log input signals"),
MGM_ARG("out", Int, Mandatory, "Log output signals"),
MGM_CMD("start signallog", &MgmApiSession::startSignalLog, ""),
MGM_ARG("node", Int, Mandatory, "Node"),
MGM_CMD("stop signallog", &MgmApiSession::stopSignalLog, ""),
MGM_ARG("node", Int, Mandatory, "Node"),
MGM_CMD("dump state", &MgmApiSession::dumpState, ""),
MGM_ARG("node", Int, Mandatory ,"Node"),
MGM_ARG("args", String, Mandatory, "Args(space separated int's)"),
MGM_CMD("start backup", &MgmApiSession::startBackup, ""),
MGM_ARG("completed", Int, Optional ,"Wait until completed"),
MGM_ARG("backupid", Int, Optional ,"User input backup id"),
MGM_ARG("backuppoint", Int, Optional ,"backup snapshot at start time or complete time"),
MGM_CMD("abort backup", &MgmApiSession::abortBackup, ""),
MGM_ARG("id", Int, Mandatory, "Backup id"),
MGM_CMD("stop", &MgmApiSession::stop_v1, ""),
MGM_ARG("node", String, Mandatory, "Node"),
MGM_ARG("abort", Int, Mandatory, "Node"),
MGM_CMD("stop v2", &MgmApiSession::stop_v2, ""),
MGM_ARG("node", String, Mandatory, "Node"),
MGM_ARG("abort", Int, Mandatory, "Node"),
MGM_ARG("force", Int, Optional, "Force"),
MGM_CMD("stop all", &MgmApiSession::stopAll, ""),
MGM_ARG("abort", Int, Mandatory, "Node"),
MGM_ARG("stop", String, Optional, "MGM/DB or both"),
MGM_CMD("enter single user", &MgmApiSession::enterSingleUser, ""),
MGM_ARG("nodeId", Int, Mandatory, "Node"),
MGM_CMD("exit single user", &MgmApiSession::exitSingleUser, ""),
MGM_CMD("start", &MgmApiSession::start, ""),
MGM_ARG("node", Int, Mandatory, "Node"),
MGM_CMD("start all", &MgmApiSession::startAll, ""),
MGM_CMD("bye", &MgmApiSession::bye, ""),
MGM_CMD("end session", &MgmApiSession::endSession, ""),
MGM_CMD("set loglevel", &MgmApiSession::setLogLevel, ""),
MGM_ARG("node", Int, Mandatory, "Node"),
MGM_ARG("category", Int, Mandatory, "Event category"),
MGM_ARG("level", Int, Mandatory, "Log level (0-15)"),
MGM_CMD("set cluster loglevel", &MgmApiSession::setClusterLogLevel, ""),
MGM_ARG("node", Int, Mandatory, "Node"),
MGM_ARG("category", Int, Mandatory, "Event category"),
MGM_ARG("level", Int, Mandatory, "Log level (0-15)"),
MGM_CMD("set logfilter", &MgmApiSession::setLogFilter, ""),
MGM_ARG("level", Int, Mandatory, "Severety level"),
MGM_ARG("enable", Int, Mandatory, "1=disable, 0=enable, -1=toggle"),
MGM_CMD("set parameter", &MgmApiSession::setParameter, ""),
MGM_ARG("node", Int, Mandatory, "Node"),
MGM_ARG("parameter", Int, Mandatory, "Parameter"),
MGM_ARG("value", String, Mandatory, "Value"),
MGM_CMD("set connection parameter",
&MgmApiSession::setConnectionParameter, ""),
MGM_ARG("node1", Int, Mandatory, "Node1 ID"),
MGM_ARG("node2", Int, Mandatory, "Node2 ID"),
MGM_ARG("param", Int, Mandatory, "Parameter"),
MGM_ARG("value", Int, Mandatory, "Value"),
MGM_CMD("get connection parameter",
&MgmApiSession::getConnectionParameter, ""),
MGM_ARG("node1", Int, Mandatory, "Node1 ID"),
MGM_ARG("node2", Int, Mandatory, "Node2 ID"),
MGM_ARG("param", Int, Mandatory, "Parameter"),
MGM_CMD("listen event", &MgmApiSession::listen_event, ""),
MGM_ARG("node", Int, Optional, "Node"),
MGM_ARG("parsable", Int, Optional, "Parsable"),
MGM_ARG("filter", String, Mandatory, "Event category"),
MGM_CMD("purge stale sessions", &MgmApiSession::purge_stale_sessions, ""),
MGM_CMD("check connection", &MgmApiSession::check_connection, ""),
MGM_CMD("transporter connect", &MgmApiSession::transporter_connect, ""),
MGM_CMD("get mgmd nodeid", &MgmApiSession::get_mgmd_nodeid, ""),
MGM_CMD("report event", &MgmApiSession::report_event, ""),
MGM_ARG("length", Int, Mandatory, "Length"),
MGM_ARG("data", String, Mandatory, "Data"),
MGM_CMD("list sessions", &MgmApiSession::listSessions, ""),
MGM_CMD("get session id", &MgmApiSession::getSessionId, ""),
MGM_CMD("get session", &MgmApiSession::getSession, ""),
MGM_ARG("id", Int, Mandatory, "SessionID"),
MGM_CMD("set config", &MgmApiSession::setConfig, ""),
MGM_ARG("Content-Length", Int, Mandatory, "Length of config"),
MGM_ARG("Content-Type", String, Mandatory, "Type of config"),
MGM_ARG("Content-Transfer-Encoding", String, Mandatory, "encoding"),
MGM_CMD("create nodegroup", &MgmApiSession::create_nodegroup, ""),
MGM_ARG("nodes", String, Mandatory, "Nodes"),
MGM_CMD("drop nodegroup", &MgmApiSession::drop_nodegroup, ""),
MGM_ARG("ng", Int, Mandatory, "Nodegroup"),
MGM_CMD("show config", &MgmApiSession::showConfig, ""),
MGM_ARG("Section", String, Optional, "Section name"),
MGM_ARG("NodeId", Int, Optional, "Nodeid"),
MGM_ARG("Name", String, Optional, "Parameter name"),
MGM_CMD("reload config", &MgmApiSession::reloadConfig, ""),
MGM_ARG("config_filename", String, Optional, "Reload from path"),
MGM_ARG("mycnf", Int, Optional, "Reload from my.cnf"),
MGM_ARG("force", Int, Optional, "Force reload"),
MGM_CMD("show variables", &MgmApiSession::show_variables, ""),
MGM_CMD("dump events", &MgmApiSession::dump_events, ""),
MGM_ARG("type", Int, Mandatory, "Type of event"),
MGM_ARG("nodes", String, Optional, "Nodes to include"),
MGM_CMD("set ports", &MgmApiSession::set_ports, ""),
MGM_ARG("node", Int, Mandatory, "Node which port list concerns"),
MGM_ARG("num_ports", Int, Mandatory, "Number of ports being set"),
MGM_END()
};
extern int g_errorInsert;
#define ERROR_INSERTED(x) (g_errorInsert == x || m_errorInsert == x)
#define SLEEP_ERROR_INSERTED(x) if(ERROR_INSERTED(x)){NdbSleep_SecSleep(10);}
MgmApiSession::MgmApiSession(class MgmtSrvr & mgm, NDB_SOCKET_TYPE sock, Uint64 session_id)
: SocketServer::Session(sock), m_mgmsrv(mgm),
m_session_id(session_id), m_name("unknown:0")
{
DBUG_ENTER("MgmApiSession::MgmApiSession");
m_input = new SocketInputStream(sock, SOCKET_TIMEOUT);
m_output = new BufferedSockOutputStream(sock, SOCKET_TIMEOUT);
m_parser = new Parser_t(commands, *m_input);
m_stopSelf= 0;
m_ctx= NULL;
m_mutex= NdbMutex_Create();
m_errorInsert= 0;
struct sockaddr_in addr;
SOCKET_SIZE_TYPE addrlen= sizeof(addr);
if (my_getpeername(sock, (struct sockaddr*)&addr, &addrlen) == 0)
{
char addr_buf[NDB_ADDR_STRLEN];
char *addr_str = Ndb_inet_ntop(AF_INET,
static_cast<void*>(&addr.sin_addr),
addr_buf,
(socklen_t)sizeof(addr_buf));
m_name.assfmt("%s:%d", addr_str, ntohs(addr.sin_port));
}
DBUG_PRINT("info", ("new connection from: %s", m_name.c_str()));
DBUG_VOID_RETURN;
}
MgmApiSession::~MgmApiSession()
{
DBUG_ENTER("MgmApiSession::~MgmApiSession");
if (m_input)
delete m_input;
if (m_output)
delete m_output;
if (m_parser)
delete m_parser;
if(my_socket_valid(m_socket))
{
NDB_CLOSE_SOCKET(m_socket);
my_socket_invalidate(&m_socket);
}
if(m_stopSelf < 0)
g_RestartServer= true;
if(m_stopSelf)
g_StopServer= true;
NdbMutex_Destroy(m_mutex);
DBUG_VOID_RETURN;
}
void
MgmApiSession::runSession()
{
DBUG_ENTER("MgmApiSession::runSession");
g_eventLogger->debug("%s: Connected!", name());
Parser_t::Context ctx;
ctx.m_mutex= m_mutex;
m_ctx= &ctx;
bool stop= false;
while(!stop) {
NdbMutex_Lock(m_mutex);
m_input->reset_timeout();
m_output->reset_timeout();
if (m_parser->run(ctx, *this))
{
stop= m_stop; // Has session been stopped
assert(ctx.m_status == Parser_t::Ok);
}
else
{
stop= m_stop; // Has session been stopped
const char* msg= NULL;
switch(ctx.m_status) {
case Parser_t::Eof: // Client disconnected
stop= true;
g_eventLogger->debug("%s: Eof!", name());
break;
case Parser_t::ExternalStop: // Stopped by other thread
stop= true;
g_eventLogger->debug("%s: ExternalStop!", name());
break;
case Parser_t::NoLine: // Normal read timeout
case Parser_t::EmptyLine:
break;
case Parser_t::UnknownCommand: msg= "Unknown command"; break;
case Parser_t::UnknownArgument: msg= "Unknown argument"; break;
case Parser_t::TypeMismatch: msg= "Type mismatch"; break;
case Parser_t::InvalidArgumentFormat: msg= "Invalid arg. format"; break;
case Parser_t::UnknownArgumentType: msg= "Unknown argument type"; break;
case Parser_t::ArgumentGivenTwice: msg= "Argument given twice"; break;
case Parser_t::MissingMandatoryArgument: msg= "Missing arg."; break;
case Parser_t::Ok: // Should never happen here
case Parser_t::CommandWithoutFunction:
abort();
break;
}
if (msg){
g_eventLogger->debug("%s: %s, '%s'",
name(),
msg,
ctx.m_currentToken != 0 ?
ctx.m_currentToken : "<NULL>");
// Send result to client
m_output->println("result: %s, '%s'",
msg,
ctx.m_currentToken != 0 ?
ctx.m_currentToken : "<NULL>");
m_output->print("\n");
}
}
NdbMutex_Unlock(m_mutex);
// Send output from command to the client
m_output->flush();
}
g_eventLogger->debug("%s: Stopped!", name());
NdbMutex_Lock(m_mutex);
m_ctx= NULL;
if(my_socket_valid(m_socket))
{
my_socket_close(m_socket);
my_socket_invalidate(&m_socket);
}
NdbMutex_Unlock(m_mutex);
g_eventLogger->debug("%s: Disconnected!", name());
DBUG_VOID_RETURN;
}
void
MgmApiSession::get_nodeid(Parser_t::Context &,
const class Properties &args)
{
Uint32 version, nodeid= 0, nodetype= 0xff;
Uint32 timeout= 20; // timeout in seconds
const char * endian= NULL;
const char * name= NULL;
Uint32 log_event= 1;
args.get("version", &version);
args.get("nodetype", &nodetype);
// transporter
args.get("nodeid", &nodeid);
// user
// password
// public key
args.get("endian", &endian);
args.get("name", &name);
args.get("timeout", &timeout);
/* for backwards compatability keep track if client uses new protocol */
const bool log_event_version= args.get("log_event", &log_event);
m_output->println("get nodeid reply");
// Check that client says it's using same endian
{
union { long l; char c[sizeof(long)]; } endian_check;
endian_check.l = 1;
if (endian &&
strcmp(endian,(endian_check.c[sizeof(long)-1])?"big":"little")!=0)
{
m_output->println("result: Node does not have the same "
"endianness as the management server.");
m_output->println("%s", "");
return;
}
}
bool compatible;
switch (nodetype) {
case NODE_TYPE_MGM:
case NODE_TYPE_API:
compatible = ndbCompatible_mgmt_api(NDB_VERSION, version);
break;
case NODE_TYPE_DB:
compatible = ndbCompatible_mgmt_ndb(NDB_VERSION, version);
break;
default:
m_output->println("result: unknown nodetype %d", nodetype);
m_output->println("%s", "");
return;
}
struct sockaddr_in addr;
{
SOCKET_SIZE_TYPE addrlen= sizeof(addr);
int r = my_getpeername(m_socket, (struct sockaddr*)&addr, &addrlen);
if (r != 0 )
{
m_output->println("result: getpeername(" MY_SOCKET_FORMAT \
") failed, err= %d",
MY_SOCKET_FORMAT_VALUE(m_socket), r);
m_output->println("%s", "");
return;
}
}
/* Check nodeid parameter */
if (nodeid > MAX_NODES_ID)
{
m_output->println("result: illegal nodeid %u", nodeid);
m_output->println("%s", "");
return;
}
NodeId tmp= nodeid;
BaseString error_string;
int error_code;
if (!m_mgmsrv.alloc_node_id(tmp,
(ndb_mgm_node_type)nodetype,
(struct sockaddr*)&addr,
error_code, error_string,
log_event,
timeout))
{
m_output->println("result: %s", error_string.c_str());
/* only use error_code in reply if client knows about it */
if (log_event_version)
m_output->println("error_code: %d", error_code);
m_output->println("%s", "");
return;
}
m_output->println("nodeid: %u", tmp);
m_output->println("result: Ok");
m_output->println("%s", "");
if (name)
g_eventLogger->info("Node %d: %s", tmp, name);
return;
}
void
MgmApiSession::getConfig(Parser_t::Context &,
const class Properties &args)
{
Uint32 nodetype = NDB_MGM_NODE_TYPE_UNKNOWN;
Uint32 from_node = 0;
// Ignoring mandatory parameter "version"
// Ignoring optional parameter "node"
args.get("nodetype", &nodetype);
args.get("from_node", &from_node);
SLEEP_ERROR_INSERTED(1);
m_output->println("get config reply");
BaseString pack64, error;
UtilBuffer packed;
bool success = (from_node > 0) ?
m_mgmsrv.get_packed_config_from_node(from_node,
pack64, error) :
m_mgmsrv.get_packed_config((ndb_mgm_node_type)nodetype,
pack64, error);
if (!success)
{
m_output->println("result: %s", error.c_str());
m_output->print("\n");
return;
}
m_output->println("result: Ok");
m_output->println("Content-Length: %u", pack64.length());
m_output->println("Content-Type: ndbconfig/octet-stream");
SLEEP_ERROR_INSERTED(2);
m_output->println("Content-Transfer-Encoding: base64");
m_output->print("\n");
unsigned len = (unsigned)strlen(pack64.c_str());
if(ERROR_INSERTED(3))
{
// Return only half the packed config
BaseString half64 = pack64.substr(0, pack64.length());
m_output->write(half64.c_str(), (unsigned)strlen(half64.c_str()));
m_output->write("\n", 1);
return;
}
m_output->write(pack64.c_str(), len);
m_output->write("\n\n", 2);
return;
}
void
MgmApiSession::insertError(Parser<MgmApiSession>::Context &,
Properties const &args) {
Uint32 extra = 0;
Uint32 node = 0, error = 0;
int result= 0;
args.get("node", &node);
args.get("error", &error);
const bool hasExtra = args.get("extra", &extra);
Uint32 * extraptr = hasExtra ? &extra : 0;
if(node==m_mgmsrv.getOwnNodeId()
&& error < MGM_ERROR_MAX_INJECT_SESSION_ONLY)
{
m_errorInsert= error;
if(error==0)
g_errorInsert= error;
}
else
{
result= m_mgmsrv.insertError(node, error, extraptr);
}
m_output->println("insert error reply");
if(result != 0)
m_output->println("result: %s", get_error_text(result));
else
m_output->println("result: Ok");
m_output->println("%s", "");
}
void
MgmApiSession::setTrace(Parser<MgmApiSession>::Context &,
Properties const &args) {
Uint32 node = 0, trace = 0;
args.get("node", &node);
args.get("trace", &trace);
int result = m_mgmsrv.setTraceNo(node, trace);
m_output->println("set trace reply");
if(result != 0)
m_output->println("result: %s", get_error_text(result));
else
m_output->println("result: Ok");
m_output->println("%s", "");
}
void
MgmApiSession::getVersion(Parser<MgmApiSession>::Context &,
Properties const &) {
m_output->println("version");
m_output->println("id: %d", NDB_VERSION_D);
m_output->println("major: %d", NDB_VERSION_MAJOR);
m_output->println("minor: %d", NDB_VERSION_MINOR);
m_output->println("build: %d", NDB_VERSION_BUILD);
m_output->println("string: %s", m_mgmsrv.get_version_string());
m_output->println("mysql_major: %d", NDB_MYSQL_VERSION_MAJOR);
m_output->println("mysql_minor: %d", NDB_MYSQL_VERSION_MINOR);
m_output->println("mysql_build: %d", NDB_MYSQL_VERSION_BUILD);
m_output->println("%s", "");
}
void
MgmApiSession::startBackup(Parser<MgmApiSession>::Context &,
Properties const &args) {
DBUG_ENTER("MgmApiSession::startBackup");
unsigned backupId;
unsigned input_backupId= 0;
unsigned backuppoint= 0;
Uint32 completed= 2;
int result;
args.get("completed", &completed);
if(args.contains("backupid"))
args.get("backupid", &input_backupId);
if(args.contains("backuppoint"))
args.get("backuppoint", &backuppoint);
result = m_mgmsrv.startBackup(backupId, completed, input_backupId, backuppoint);
m_output->println("start backup reply");
if(result != 0)
{
m_output->println("result: %s", get_error_text(result));
}
else{
m_output->println("result: Ok");
if (completed)
m_output->println("id: %d", backupId);
}
m_output->println("%s", "");
DBUG_VOID_RETURN;
}
void
MgmApiSession::abortBackup(Parser<MgmApiSession>::Context &,
Properties const &args) {
Uint32 id = 0;
args.get("id", &id);
int result = m_mgmsrv.abortBackup(id);
m_output->println("abort backup reply");
if(result != 0)
m_output->println("result: %s", get_error_text(result));
else
m_output->println("result: Ok");
m_output->println("%s", "");
}
/*****************************************************************************/
void
MgmApiSession::dumpState(Parser<MgmApiSession>::Context &,
Properties const &args) {
Uint32 node;
BaseString args_str;
args.get("node", &node);
args.get("args", args_str);
int result = m_mgmsrv.dumpState(node, args_str.c_str());
m_output->println("dump state reply");
if(result != 0)
m_output->println("result: %s", get_error_text(result));
else
m_output->println("result: Ok");
m_output->println("%s", "");
}
void
MgmApiSession::bye(Parser<MgmApiSession>::Context &,
Properties const &) {
m_stop = true;
}
void
MgmApiSession::endSession(Parser<MgmApiSession>::Context &,
Properties const &)
{
SLEEP_ERROR_INSERTED(4);
m_output->println("end session reply");
}
void
MgmApiSession::getClusterLogLevel(Parser<MgmApiSession>::Context & , Properties const &) {
const char* names[] = { "startup",
"shutdown",
"statistics",
"checkpoint",
"noderestart",
"connection",
"info",
"warning",
"error",
"congestion",
"debug",
"backup",
"schema" };
int const loglevel_count = (CFG_MAX_LOGLEVEL - CFG_MIN_LOGLEVEL + 1);
NDB_STATIC_ASSERT(NDB_ARRAY_SIZE(names) == loglevel_count);
LogLevel::EventCategory category;
m_output->println("get cluster loglevel");
for(int i = 0; i < loglevel_count; i++) {
category = (LogLevel::EventCategory) i;
m_output->println("%s: %d", names[i], m_mgmsrv.m_event_listner[0].m_logLevel.getLogLevel(category));
}
m_output->println("%s", "");
}
void
MgmApiSession::setClusterLogLevel(Parser<MgmApiSession>::Context &,
Properties const &args) {
Uint32 node, level, cat;
BaseString errorString;
DBUG_ENTER("MgmApiSession::setClusterLogLevel");
args.get("node", &node);
args.get("category", &cat);
args.get("level", &level);
DBUG_PRINT("enter",("node=%d, category=%d, level=%d", node, cat, level));
m_output->println("set cluster loglevel reply");
if(level > NDB_MGM_MAX_LOGLEVEL) {
m_output->println("result: Invalid loglevel %d", level);
m_output->println("%s", "");
DBUG_VOID_RETURN;
}
LogLevel::EventCategory category=
(LogLevel::EventCategory)(cat-(int)CFG_MIN_LOGLEVEL);
m_mgmsrv.m_event_listner.lock();
if (m_mgmsrv.m_event_listner[0].m_logLevel.setLogLevel(category,level))
{
m_output->println("result: Invalid category %d", category);
m_output->println("%s", "");
m_mgmsrv.m_event_listner.unlock();
DBUG_VOID_RETURN;
}
m_mgmsrv.m_event_listner.unlock();
{
LogLevel tmp;
m_mgmsrv.m_event_listner.update_max_log_level(tmp);
}
m_output->println("result: Ok");
m_output->println("%s", "");
DBUG_VOID_RETURN;
}
void
MgmApiSession::setLogLevel(Parser<MgmApiSession>::Context &,
Properties const &args) {
Uint32 node = 0, level = 0, cat;
BaseString errorString;
SetLogLevelOrd logLevel;
logLevel.clear();
args.get("node", &node);
args.get("category", &cat);
args.get("level", &level);
if(level > NDB_MGM_MAX_LOGLEVEL) {
m_output->println("set loglevel reply");
m_output->println("result: Invalid loglevel: %s", errorString.c_str());
m_output->println("%s", "");
return;
}
LogLevel::EventCategory category=
(LogLevel::EventCategory)(cat-(int)CFG_MIN_LOGLEVEL);
{
LogLevel ll;
ll.setLogLevel(category,level);
m_mgmsrv.m_event_listner.update_max_log_level(ll);
}
m_output->println("set loglevel reply");
m_output->println("result: Ok");
m_output->println("%s", "");
}
void
MgmApiSession::stopSignalLog(Parser<MgmApiSession>::Context &,
Properties const &args) {
Uint32 node;
args.get("node", &node);
int result = m_mgmsrv.stopSignalTracing(node);
m_output->println("stop signallog");
if(result != 0)
m_output->println("result: %s", get_error_text(result));
else
m_output->println("result: Ok");
m_output->println("%s", "");
}
void
MgmApiSession::restart_v1(Parser<MgmApiSession>::Context &,
Properties const &args) {
restart(args,1);
}
void
MgmApiSession::restart_v2(Parser<MgmApiSession>::Context &,
Properties const &args) {
restart(args,2);
}
void
MgmApiSession::restart(Properties const &args, int version) {
Uint32
nostart = 0,
initialstart = 0,
abort = 0, force = 0;
char *nodes_str;
Vector<NodeId> nodes;
args.get("initialstart", &initialstart);
args.get("nostart", &nostart);
args.get("abort", &abort);
args.get("node", (const char **)&nodes_str);
args.get("force", &force);
char *p, *last;
for((p = my_strtok_r(nodes_str, " ", &last));
p;
(p = my_strtok_r(NULL, " ", &last))) {
nodes.push_back(atoi(p));
}
int restarted = 0;
int result= m_mgmsrv.restartNodes(nodes,
&restarted,
nostart != 0,
initialstart != 0,
abort != 0,
force != 0,
&m_stopSelf);
if (result == UNSUPPORTED_NODE_SHUTDOWN && nodes.size() > 1 && force)
{
/**
* We don't support multi node graceful shutdown...
* add "-a" and try again
*/
abort = 1;
result= m_mgmsrv.restartNodes(nodes,
&restarted,
nostart != 0,
initialstart != 0,
abort != 0,
force != 0,
&m_stopSelf);
}
if (force &&
(result == NODE_SHUTDOWN_WOULD_CAUSE_SYSTEM_CRASH ||
result == UNSUPPORTED_NODE_SHUTDOWN))
{
// Force restart by restarting all nodes
result = m_mgmsrv.restartDB(nostart, initialstart, false, &restarted);
}
m_output->println("restart reply");
if(result != 0){
m_output->println("result: %d-%s", result, get_error_text(result));
} else
m_output->println("result: Ok");
m_output->println("restarted: %d", restarted);
if(version>1)
m_output->println("disconnect: %d", (m_stopSelf)?1:0);
m_output->println("%s", "");
}
void
MgmApiSession::restartAll(Parser<MgmApiSession>::Context &,
Properties const &args)
{
Uint32 nostart = 0;
Uint32 initialstart = 0;
Uint32 abort = 0;
args.get("initialstart", &initialstart);
args.get("abort", &abort);
args.get("nostart", &nostart);
int count = 0;
int result = m_mgmsrv.restartDB(nostart, initialstart, abort, &count);
m_output->println("restart reply");
if(result != 0)
m_output->println("result: %s", get_error_text(result));