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
|
// s/commands_admin.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* TODO
_ concurrency control.
_ limit() works right?
_ KillCursors
later
_ secondary indexes
*/
#include "pch.h"
#include "../util/message.h"
#include "../util/processinfo.h"
#include "../client/connpool.h"
#include "../db/dbmessage.h"
#include "../db/commands.h"
#include "../db/stats/counters.h"
#include "config.h"
#include "chunk.h"
#include "strategy.h"
#include "stats.h"
namespace mongo {
extern string ourHostname;
namespace dbgrid_cmds {
set<string> dbgridCommands;
class GridAdminCmd : public Command {
public:
GridAdminCmd( const char * n ) : Command( n ){
dbgridCommands.insert( n );
}
virtual bool slaveOk() const {
return true;
}
virtual bool adminOnly() const {
return true;
}
// all grid commands are designed not to lock
virtual LockType locktype() const { return NONE; }
};
// --------------- misc commands ----------------------
class NetStatCmd : public GridAdminCmd {
public:
NetStatCmd() : GridAdminCmd("netstat") { }
virtual void help( stringstream& help ) const {
help << " shows status/reachability of servers in the cluster";
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
result.append("configserver", configServer.getPrimary().getConnString() );
result.append("isdbgrid", 1);
return true;
}
} netstat;
class ServerStatusCmd : public Command {
public:
ServerStatusCmd() : Command( "serverStatus" , true ){
_started = time(0);
}
virtual bool slaveOk() const { return true; }
virtual LockType locktype() const { return NONE; }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool fromRepl) {
result.append("uptime",(double) (time(0)-_started));
result.appendDate( "localTime" , jsTime() );
{
BSONObjBuilder t( result.subobjStart( "mem" ) );
ProcessInfo p;
if ( p.supported() ){
t.appendNumber( "resident" , p.getResidentSize() );
t.appendNumber( "virtual" , p.getVirtualMemorySize() );
t.appendBool( "supported" , true );
}
else {
result.append( "note" , "not all mem info support on this platform" );
t.appendBool( "supported" , false );
}
t.done();
}
{
BSONObjBuilder bb( result.subobjStart( "connections" ) );
bb.append( "current" , connTicketHolder.used() );
bb.append( "available" , connTicketHolder.available() );
bb.done();
}
{
BSONObjBuilder bb( result.subobjStart( "extra_info" ) );
bb.append("note", "fields vary by platform");
ProcessInfo p;
p.getExtraInfo(bb);
bb.done();
}
result.append( "opcounters" , globalOpCounters.getObj() );
{
BSONObjBuilder bb( result.subobjStart( "ops" ) );
bb.append( "sharded" , opsSharded.getObj() );
bb.append( "notSharded" , opsNonSharded.getObj() );
bb.done();
}
result.append( "shardCursorType" , shardedCursorTypes.getObj() );
{
BSONObjBuilder asserts( result.subobjStart( "asserts" ) );
asserts.append( "regular" , assertionCount.regular );
asserts.append( "warning" , assertionCount.warning );
asserts.append( "msg" , assertionCount.msg );
asserts.append( "user" , assertionCount.user );
asserts.append( "rollovers" , assertionCount.rollovers );
asserts.done();
}
return 1;
}
time_t _started;
} cmdServerStatus;
class ListGridCommands : public GridAdminCmd {
public:
ListGridCommands() : GridAdminCmd("gridcommands") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
BSONObjBuilder arr;
int num=0;
for ( set<string>::iterator i = dbgridCommands.begin(); i != dbgridCommands.end(); i++ ){
string s = BSONObjBuilder::numStr( num++ );
arr.append( s.c_str() , *i );
}
result.appendArray( "commands" , arr.done() );
return true;
}
} listGridCommands;
// ------------ database level commands -------------
class ListDatabaseCommand : public GridAdminCmd {
public:
ListDatabaseCommand() : GridAdminCmd("listdatabases") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
ShardConnection conn( configServer.getPrimary() );
auto_ptr<DBClientCursor> cursor = conn->query( "config.databases" , BSONObj() );
BSONObjBuilder list;
int num = 0;
while ( cursor->more() ){
string s = BSONObjBuilder::numStr( num++ );
BSONObj o = cursor->next();
list.append( s.c_str() , o["name"].valuestrsafe() );
}
result.appendArray("databases" , list.obj() );
conn.done();
return true;
}
} gridListDatabase;
class MoveDatabasePrimaryCommand : public GridAdminCmd {
public:
MoveDatabasePrimaryCommand() : GridAdminCmd("moveprimary") { }
virtual void help( stringstream& help ) const {
help << " example: { moveprimary : 'foo' , to : 'localhost:9999' } TODO: locking? ";
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string dbname = cmdObj["moveprimary"].valuestrsafe();
if ( dbname.size() == 0 ){
errmsg = "no db";
return false;
}
if ( dbname == "config" ){
errmsg = "can't move config db";
return false;
}
DBConfig * config = grid.getDBConfig( dbname , false );
if ( ! config ){
errmsg = "can't find db!";
return false;
}
string to = cmdObj["to"].valuestrsafe();
if ( ! to.size() ){
errmsg = "you have to specify where you want to move it";
return false;
}
if ( config->getPrimary() == to ){
errmsg = "thats already the primary";
return false;
}
if ( ! grid.knowAboutShard( to ) ){
errmsg = "that server isn't known to me";
return false;
}
ShardConnection conn( configServer.getPrimary() );
log() << "movePrimary: moving " << dbname << " primary from: " << config->getPrimary().toString() << " to: " << to << endl;
// TODO LOCKING: this is not safe with multiple mongos
ShardConnection toconn( to );
// TODO AARON - we need a clone command which replays operations from clone start to now
// using a seperate smaller oplog
BSONObj cloneRes;
bool worked = toconn->runCommand( dbname.c_str() , BSON( "clone" << config->getPrimary().getConnString() ) , cloneRes );
toconn.done();
if ( ! worked ){
log() << "clone failed" << cloneRes << endl;
errmsg = "clone failed";
conn.done();
return false;
}
ShardConnection fromconn( config->getPrimary() );
config->setPrimary( to );
config->save( true );
log() << "movePrimary: dropping " << dbname << " from old" << endl;
fromconn->dropDatabase( dbname.c_str() );
fromconn.done();
result << "primary" << to;
conn.done();
return true;
}
} movePrimary;
class EnableShardingCmd : public GridAdminCmd {
public:
EnableShardingCmd() : GridAdminCmd( "enablesharding" ){}
virtual void help( stringstream& help ) const {
help
<< "Enable sharding for a db. (Use 'shardcollection' command afterwards.)\n"
<< " { enablesharding : \"<dbname>\" }\n";
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string dbname = cmdObj["enablesharding"].valuestrsafe();
if ( dbname.size() == 0 ){
errmsg = "no db";
return false;
}
DBConfig * config = grid.getDBConfig( dbname );
if ( config->isShardingEnabled() ){
errmsg = "already enabled";
return false;
}
log() << "enabling sharding on: " << dbname << endl;
config->enableSharding();
config->save( true );
return true;
}
} enableShardingCmd;
// ------------ collection level commands -------------
class ShardCollectionCmd : public GridAdminCmd {
public:
ShardCollectionCmd() : GridAdminCmd( "shardcollection" ){}
virtual void help( stringstream& help ) const {
help
<< "Shard a collection. Requires key. Optional unique. Sharding must already be enabled for the database.\n"
<< " { enablesharding : \"<dbname>\" }\n";
}
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj["shardcollection"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
if ( ! config->isShardingEnabled() ){
errmsg = "sharding not enabled for db";
return false;
}
if ( config->isSharded( ns ) ){
errmsg = "already sharded";
return false;
}
BSONObj key = cmdObj.getObjectField( "key" );
if ( key.isEmpty() ){
errmsg = "no shard key";
return false;
} else if (key.nFields() > 1){
errmsg = "compound shard keys not supported yet";
return false;
}
if ( ns.find( ".system." ) != string::npos ){
errmsg = "can't shard system namespaces";
return false;
}
ShardKeyPattern proposedKey( key );
{
ShardConnection conn( config->getPrimary() );
BSONObjBuilder b;
b.append( "ns" , ns );
b.appendBool( "unique" , true );
auto_ptr<DBClientCursor> cursor = conn->query( config->getName() + ".system.indexes" , b.obj() );
while ( cursor->more() ){
BSONObj idx = cursor->next();
if ( proposedKey.uniqueAllowd( idx["key"].embeddedObjectUserCheck() ) )
continue;
errmsg = (string)"can't shard collection with unique index on: " + idx.toString();
conn.done();
return false;
}
BSONObj res = conn->findOne( config->getName() + ".system.namespaces" , BSON( "name" << ns ) );
if ( res["options"].type() == Object && res["options"].embeddedObject()["capped"].trueValue() ){
errmsg = "can't shard capped collection";
conn.done();
return false;
}
conn.done();
}
log() << "CMD: shardcollection: " << cmdObj << endl;
config->shardCollection( ns , key , cmdObj["unique"].trueValue() );
config->save( true );
result << "collectionsharded" << ns;
return true;
}
} shardCollectionCmd;
class GetShardVersion : public GridAdminCmd {
public:
GetShardVersion() : GridAdminCmd( "getShardVersion" ){}
virtual void help( stringstream& help ) const {
help << " example: { getShardVersion : 'alleyinsider.foo' } ";
}
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj["getShardVersion"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "need to speciy fully namespace";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
if ( ! config->isSharded( ns ) ){
errmsg = "ns not sharded.";
return false;
}
ChunkManager * cm = config->getChunkManager( ns );
if ( ! cm ){
errmsg = "no chunk manager?";
return false;
}
result.appendTimestamp( "version" , cm->getVersion() );
return 1;
}
} getShardVersionCmd;
class SplitCollectionHelper : public GridAdminCmd {
public:
SplitCollectionHelper( const char * name ) : GridAdminCmd( name ) , _name( name ){}
virtual void help( stringstream& help ) const {
help
<< " example: { split : 'alleyinsider.blog.posts' , find : { ts : 1 } } - split the shard that contains give key \n"
<< " example: { split : 'alleyinsider.blog.posts' , middle : { ts : 1 } } - split the shard that contains the key with this as the middle \n"
<< " NOTE: this does not move move the chunks, it merely creates a logical seperation \n"
;
}
virtual bool _split( BSONObjBuilder& result , string&errmsg , const string& ns , ChunkManager * manager , Chunk& old , BSONObj middle ) = 0;
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj[_name.c_str()].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
if ( ! config->isSharded( ns ) ){
errmsg = "ns not sharded. have to shard before can split";
return false;
}
BSONObj find = cmdObj.getObjectField( "find" );
if ( find.isEmpty() ){
find = cmdObj.getObjectField( "middle" );
if ( find.isEmpty() ){
errmsg = "need to specify find or middle";
return false;
}
}
ChunkManager * info = config->getChunkManager( ns );
Chunk& old = info->findChunk( find );
return _split( result , errmsg , ns , info , old , cmdObj.getObjectField( "middle" ) );
}
protected:
string _name;
};
class SplitValueCommand : public SplitCollectionHelper {
public:
SplitValueCommand() : SplitCollectionHelper( "splitvalue" ){}
virtual bool _split( BSONObjBuilder& result , string& errmsg , const string& ns , ChunkManager * manager , Chunk& old , BSONObj middle ){
result << "shardinfo" << old.toString();
result.appendBool( "auto" , middle.isEmpty() );
if ( middle.isEmpty() )
middle = old.pickSplitPoint();
result.append( "middle" , middle );
return true;
}
} splitValueCmd;
class SplitCollection : public SplitCollectionHelper {
public:
SplitCollection() : SplitCollectionHelper( "split" ){}
virtual bool _split( BSONObjBuilder& result , string& errmsg , const string& ns , ChunkManager * manager , Chunk& old , BSONObj middle ){
log() << "splitting: " << ns << " shard: " << old << endl;
if ( middle.isEmpty() )
old.split();
else
old.split( middle );
return true;
}
} splitCollectionCmd;
class MoveChunkCmd : public GridAdminCmd {
public:
MoveChunkCmd() : GridAdminCmd( "movechunk" ){}
virtual void help( stringstream& help ) const {
help << "{ movechunk : 'test.foo' , find : { num : 1 } , to : 'localhost:30001' }";
}
bool run(const char *cmdns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
string ns = cmdObj["movechunk"].valuestrsafe();
if ( ns.size() == 0 ){
errmsg = "no ns";
return false;
}
DBConfig * config = grid.getDBConfig( ns );
if ( ! config->isSharded( ns ) ){
errmsg = "ns not sharded. have to shard before can move a chunk";
return false;
}
BSONObj find = cmdObj.getObjectField( "find" );
if ( find.isEmpty() ){
errmsg = "need to specify find. see help";
return false;
}
string toString = cmdObj["to"].valuestrsafe();
if ( ! toString.size() ){
errmsg = "you have to specify where you want to move the chunk";
return false;
}
Shard to = Shard::make( toString );
log() << "CMD: movechunk: " << cmdObj << endl;
ChunkManager * info = config->getChunkManager( ns );
Chunk& c = info->findChunk( find );
const Shard& from = c.getShard();
if ( from == to ){
errmsg = "that chunk is already on that shard";
return false;
}
if ( ! c.moveAndCommit( to , errmsg ) )
return false;
return true;
}
} moveChunkCmd;
// ------------ server level commands -------------
class ListShardsCmd : public GridAdminCmd {
public:
ListShardsCmd() : GridAdminCmd("listshards") { }
virtual void help( stringstream& help ) const {
help << "list all shards of the system";
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
ShardConnection conn( configServer.getPrimary() );
vector<BSONObj> all;
auto_ptr<DBClientCursor> cursor = conn->query( "config.shards" , BSONObj() );
while ( cursor->more() ){
BSONObj o = cursor->next();
all.push_back( o );
}
result.append("shards" , all );
conn.done();
return true;
}
} listShardsCmd;
/* a shard is a single mongod server or a replica pair. add it (them) to the cluster as a storage partition. */
class AddShard : public GridAdminCmd {
public:
AddShard() : GridAdminCmd("addshard") { }
virtual void help( stringstream& help ) const {
help << "add a new shard to the system";
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
ShardConnection conn( configServer.getPrimary() );
string host = cmdObj["addshard"].valuestrsafe();
if ( host == "localhost" || host.find( "localhost:" ) == 0 ||
host == "127.0.0.1" || host.find( "127.0.0.1:" ) == 0 ){
if ( ! cmdObj["allowLocal"].trueValue() ){
errmsg =
"can't use localhost as a shard since all shards need to communicate. "
"allowLocal to override for testing";
return false;
}
}
if ( host.find( ":" ) == string::npos ){
stringstream ss;
ss << host << ":" << CmdLine::ShardServerPort;
host = ss.str();
}
string name = "";
if ( cmdObj["name"].type() == String )
name = cmdObj["name"].valuestrsafe();
if ( name.size() == 0 ){
stringstream ss;
ss << "shard";
ss << conn->count( "config.shards" );
name = ss.str();
}
BSONObj shard;
{
BSONObjBuilder b;
b.append( "_id" , name );
b.append( "host" , host );
if ( cmdObj["maxSize"].isNumber() )
b.append( cmdObj["maxSize"] );
shard = b.obj();
}
BSONObj old = conn->findOne( "config.shards" , BSON( "host" << host ) );
if ( ! old.isEmpty() ){
result.append( "msg" , "host already used" );
conn.done();
return false;
}
try {
ShardConnection newShardConn( host );
newShardConn->getLastError();
newShardConn.done();
}
catch ( DBException& e ){
errmsg = "couldn't connect to new shard";
result.append( "host" , host );
result.append( "exception" , e.what() );
conn.done();
return false;
}
log() << "going to add shard: " << shard << endl;
conn->insert( "config.shards" , shard );
errmsg = conn->getLastError();
if ( errmsg.size() ){
log() << "error adding shard: " << shard << " err: " << errmsg << endl;
return false;
}
result.append( "added" , shard["host"].valuestrsafe() );
conn.done();
return true;
}
} addServer;
class RemoveShardCmd : public GridAdminCmd {
public:
RemoveShardCmd() : GridAdminCmd("removeshard") { }
virtual void help( stringstream& help ) const {
help << "remove a shard to the system.\nshard must be empty or command will return an error.";
}
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool){
if ( 1 ){
errmsg = "removeshard not yet implemented";
return 0;
}
ShardConnection conn( configServer.getPrimary() );
BSONObj server = BSON( "host" << cmdObj["removeshard"].valuestrsafe() );
conn->remove( "config.shards" , server );
conn.done();
return true;
}
} removeShardCmd;
// --------------- public commands ----------------
class IsDbGridCmd : public Command {
public:
virtual LockType locktype() const { return NONE; }
virtual bool slaveOk() const {
return true;
}
IsDbGridCmd() : Command("isdbgrid") { }
bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
result.append("isdbgrid", 1);
result.append("hostname", ourHostname);
return true;
}
} isdbgrid;
class CmdIsMaster : public Command {
public:
virtual LockType locktype() const { return NONE; }
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() const {
return true;
}
virtual void help( stringstream& help ) const {
help << "test if this is master half of a replica pair";
}
CmdIsMaster() : Command("ismaster") { }
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
result.append("ismaster", 1.0 );
result.append("msg", "isdbgrid");
return true;
}
} ismaster;
class CmdWhatsMyUri : public Command {
public:
CmdWhatsMyUri() : Command("whatsmyuri") { }
virtual bool logTheOp() {
return false; // the modification will be logged directly
}
virtual bool slaveOk() const {
return true;
}
virtual LockType locktype() const { return NONE; }
virtual bool requiresAuth() {
return false;
}
virtual void help( stringstream &help ) const {
help << "{whatsmyuri:1}";
}
virtual bool run(const char *dbname, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
result << "you" << ClientInfo::get()->getRemote();
return true;
}
} cmdWhatsMyUri;
class CmdShardingGetPrevError : public Command {
public:
virtual LockType locktype() const { return NONE; }
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() const {
return true;
}
virtual void help( stringstream& help ) const {
help << "get previous error (since last reseterror command)";
}
CmdShardingGetPrevError() : Command("getpreverror") { }
virtual bool run(const char *ns, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
errmsg += "getpreverror not supported for sharded environments";
return false;
}
} cmdGetPrevError;
class CmdShardingGetLastError : public Command {
public:
virtual LockType locktype() const { return NONE; }
virtual bool requiresAuth() { return false; }
virtual bool slaveOk() const {
return true;
}
virtual void help( stringstream& help ) const {
help << "check for an error on the last command executed";
}
CmdShardingGetLastError() : Command("getlasterror") { }
virtual bool run(const char *nsraw, BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
{
LastError * le = lastError.get();
assert( le );
if ( le->msg.size() ){
le->appendSelf( result );
return true;
}
}
string dbName = nsraw;
dbName = dbName.substr( 0 , dbName.size() - 5 );
DBConfig * conf = grid.getDBConfig( dbName , false );
ClientInfo * client = ClientInfo::get();
set<string> * shards = client->getPrev();
if ( shards->size() == 0 ){
result.appendNull( "err" );
return true;
}
if ( shards->size() == 1 ){
string theShard = *(shards->begin() );
result.append( "theshard" , theShard.c_str() );
ShardConnection conn( theShard );
BSONObj res;
bool ok = conn->runCommand( conf->getName() , cmdObj , res );
result.appendElements( res );
conn.done();
return ok;
}
vector<string> errors;
for ( set<string>::iterator i = shards->begin(); i != shards->end(); i++ ){
string theShard = *i;
ShardConnection conn( theShard );
string temp = conn->getLastError();
if ( temp.size() )
errors.push_back( temp );
conn.done();
}
if ( errors.size() == 0 ){
result.appendNull( "err" );
return true;
}
result.append( "err" , errors[0].c_str() );
BSONObjBuilder all;
for ( unsigned i=0; i<errors.size(); i++ ){
all.append( all.numStr( i ).c_str() , errors[i].c_str() );
}
result.appendArray( "errs" , all.obj() );
return true;
}
} cmdGetLastError;
}
class CmdListDatabases : public Command {
public:
CmdListDatabases() : Command("listDatabases") {}
virtual bool logTheOp() { return false; }
virtual bool slaveOk() const { return true; }
virtual bool slaveOverrideOk() { return true; }
virtual bool adminOnly() const { return true; }
virtual LockType locktype() const { return NONE; }
virtual void help( stringstream& help ) const { help << "list databases on cluster"; }
bool run(const char *ns, BSONObj& jsobj, string& errmsg, BSONObjBuilder& result, bool /*fromRepl*/) {
list<Shard> shards;
Shard::getAllShards( shards );
map<string,long long> sizes;
map< string,shared_ptr<BSONObjBuilder> > dbShardInfo;
for ( list<Shard>::iterator i=shards.begin(); i!=shards.end(); i++ ){
Shard s = *i;
BSONObj x = s.runCommand( "admin" , "listDatabases" );
cout << s.toString() << "\t" << x.jsonString() << endl;
BSONObjIterator j( x["databases"].Obj() );
while ( j.more() ){
BSONObj theDB = j.next().Obj();
string name = theDB["name"].String();
long long size = theDB["sizeOnDisk"].numberLong();
long long& totalSize = sizes[name];
if ( size == 1 ){
if ( totalSize <= 1 )
totalSize = 1;
}
else
totalSize += size;
shared_ptr<BSONObjBuilder>& bb = dbShardInfo[name];
if ( ! bb.get() )
bb.reset( new BSONObjBuilder() );
bb->appendNumber( s.getName() , size );
}
}
BSONArrayBuilder bb( result.subarrayStart( "databases" ) );
for ( map<string,long long>::iterator i=sizes.begin(); i!=sizes.end(); ++i ){
string name = i->first;
long long size = i->second;
BSONObjBuilder temp;
temp.append( "name" , name );
temp.appendNumber( "size" , size );
temp.appendBool( "empty" , size == 1 );
temp.append( "shards" , dbShardInfo[name]->obj() );
bb.append( temp.obj() );
}
bb.done();
return 1;
}
} cmdListDatabases;
} // namespace mongo
|