@@ -260,7 +260,6 @@ ClusterInfo::ClusterInfo(AgencyCallbackRegistry* agencyCallbackRegistry)
260
260
// //////////////////////////////////////////////////////////////////////////////
261
261
262
262
ClusterInfo::~ClusterInfo () {
263
- clearCurrentDatabases (_currentDatabases);
264
263
}
265
264
266
265
// //////////////////////////////////////////////////////////////////////////////
@@ -305,7 +304,7 @@ void ClusterInfo::flush() {
305
304
loadCurrentDBServers ();
306
305
loadCurrentCoordinators ();
307
306
loadPlan ();
308
- loadCurrentDatabases ();
307
+ loadCurrent ();
309
308
loadCurrentCollections ();
310
309
}
311
310
@@ -317,9 +316,9 @@ bool ClusterInfo::doesDatabaseExist(DatabaseID const& databaseID, bool reload) {
317
316
int tries = 0 ;
318
317
319
318
if (reload || !_planProt.isValid ||
320
- !_currentDatabasesProt .isValid || !_DBServersProt.isValid ) {
319
+ !_currentProt .isValid || !_DBServersProt.isValid ) {
321
320
loadPlan ();
322
- loadCurrentDatabases ();
321
+ loadCurrent ();
323
322
loadCurrentDBServers ();
324
323
++tries; // no need to reload if the database is not found
325
324
}
@@ -343,7 +342,7 @@ bool ClusterInfo::doesDatabaseExist(DatabaseID const& databaseID, bool reload) {
343
342
344
343
if (it != _plannedDatabases.end ()) {
345
344
// found the database in Plan
346
- READ_LOCKER (readLocker, _currentDatabasesProt .lock );
345
+ READ_LOCKER (readLocker, _currentProt .lock );
347
346
// _currentDatabases is
348
347
// a map-type<DatabaseID, a map-type<ServerID, TRI_json_t*>>
349
348
auto it2 = _currentDatabases.find (databaseID);
@@ -361,7 +360,7 @@ bool ClusterInfo::doesDatabaseExist(DatabaseID const& databaseID, bool reload) {
361
360
}
362
361
363
362
loadPlan ();
364
- loadCurrentDatabases ();
363
+ loadCurrent ();
365
364
loadCurrentDBServers ();
366
365
}
367
366
@@ -376,9 +375,9 @@ std::vector<DatabaseID> ClusterInfo::listDatabases(bool reload) {
376
375
std::vector<DatabaseID> result;
377
376
378
377
if (reload || !_planProt.isValid ||
379
- !_currentDatabasesProt .isValid || !_DBServersProt.isValid ) {
378
+ !_currentProt .isValid || !_DBServersProt.isValid ) {
380
379
loadPlan ();
381
- loadCurrentDatabases ();
380
+ loadCurrent ();
382
381
loadCurrentDBServers ();
383
382
}
384
383
@@ -393,7 +392,7 @@ std::vector<DatabaseID> ClusterInfo::listDatabases(bool reload) {
393
392
394
393
{
395
394
READ_LOCKER (readLockerPlanned, _planProt.lock );
396
- READ_LOCKER (readLockerCurrent, _currentDatabasesProt .lock );
395
+ READ_LOCKER (readLockerCurrent, _currentProt .lock );
397
396
// _plannedDatabases is a map-type<DatabaseID, TRI_json_t*>
398
397
auto it = _plannedDatabases.begin ();
399
398
@@ -540,43 +539,17 @@ void ClusterInfo::loadPlan() {
540
539
<< " body: " << result.body ();
541
540
}
542
541
543
- // //////////////////////////////////////////////////////////////////////////////
544
- // / @brief deletes a list of current databases
545
- // //////////////////////////////////////////////////////////////////////////////
546
-
547
- void ClusterInfo::clearCurrentDatabases (
548
- std::unordered_map<DatabaseID, std::unordered_map<ServerID, TRI_json_t*>>&
549
- databases) {
550
- auto it = databases.begin ();
551
- while (it != databases.end ()) {
552
- auto it2 = (*it).second .begin ();
553
-
554
- while (it2 != (*it).second .end ()) {
555
- TRI_json_t* json = (*it2).second ;
556
-
557
- if (json != nullptr ) {
558
- TRI_FreeJson (TRI_UNKNOWN_MEM_ZONE, json);
559
- }
560
-
561
- ++it2;
562
- }
563
- ++it;
564
- }
565
-
566
- databases.clear ();
567
- }
568
-
569
542
// //////////////////////////////////////////////////////////////////////////////
570
543
// / @brief (re-)load the information about current databases
571
544
// / Usually one does not have to call this directly.
572
545
// //////////////////////////////////////////////////////////////////////////////
573
546
574
547
static std::string const prefixCurrentDatabases = " Current/Databases" ;
575
548
576
- void ClusterInfo::loadCurrentDatabases () {
577
- uint64_t storedVersion = _currentDatabasesProt .version ;
578
- MUTEX_LOCKER (mutexLocker, _currentDatabasesProt .mutex );
579
- if (_currentDatabasesProt .version > storedVersion) {
549
+ void ClusterInfo::loadCurrent () {
550
+ uint64_t storedVersion = _currentProt .version ;
551
+ MUTEX_LOCKER (mutexLocker, _currentProt .mutex );
552
+ if (_currentProt .version > storedVersion) {
580
553
// Somebody else did, what we intended to do, so just return
581
554
return ;
582
555
}
@@ -591,48 +564,54 @@ void ClusterInfo::loadCurrentDatabases() {
591
564
}
592
565
593
566
if (result.successful ()) {
594
-
595
- velocypack::Slice databases =
567
+
568
+ velocypack::Slice slice =
596
569
result.slice ()[0 ].get (std::vector<std::string>(
597
- {AgencyComm::prefixStripped (), " Current" , " Databases " }));
570
+ {AgencyComm::prefixStripped (), " Current" }));
598
571
599
- if (!databases.isNone ()) {
572
+ auto currentBuilder = std::make_shared<VPackBuilder>();
573
+ currentBuilder->add (slice);
574
+
575
+ VPackSlice currentSlice = currentBuilder->slice ();
576
+ if (currentSlice.isObject ()) {
600
577
decltype (_currentDatabases) newDatabases;
601
578
602
- for ( auto const & dbase : VPackObjectIterator (databases)) {
579
+ bool swapDatabases = false ;
603
580
604
- std::string const database = dbase.key .copyString ();
581
+ VPackSlice databasesSlice;
582
+ databasesSlice = currentSlice.get (" Databases" );
583
+ if (databasesSlice.isObject ()) {
584
+ for (auto const & databaseSlicePair : VPackObjectIterator (databasesSlice)) {
585
+ std::string const database = databaseSlicePair.key .copyString ();
605
586
606
- // _currentDatabases is
607
- // a map-type<DatabaseID, a map-type<ServerID, TRI_json_t*>>
608
- auto it2 = newDatabases. find (database);
587
+ if (!databaseSlicePair. value . isObject ()) {
588
+ continue ;
589
+ }
609
590
610
- if (it2 == newDatabases.end ()) {
611
- // insert an empty list for this database
612
- decltype (it2->second ) empty;
613
- it2 = newDatabases.insert (std::make_pair (database, empty)).first ;
614
- }
591
+ std::unordered_map<ServerID, VPackSlice> serverList;
592
+ for (auto const & serverSlicePair : VPackObjectIterator (databaseSlicePair.value )) {
593
+ serverList.insert (std::make_pair (serverSlicePair.key .copyString (), serverSlicePair.value ));
594
+ }
615
595
616
- // TODO: _plannedDatabases need to be moved to velocypack
617
- // Then this can be merged to swap
618
- for (auto const & server : VPackObjectIterator (dbase.value )) {
619
- TRI_json_t* json = arangodb::basics::VelocyPackHelper::velocyPackToJson (
620
- server.value );
621
- (*it2).second .insert (std::make_pair (server.key .copyString (), json));
596
+ newDatabases.insert (std::make_pair (database, serverList));
622
597
}
623
-
598
+ swapDatabases = true ;
624
599
}
625
600
626
601
// Now set the new value:
627
602
{
628
- WRITE_LOCKER (writeLocker, _currentDatabasesProt.lock );
629
- _currentDatabases.swap (newDatabases);
630
- _currentDatabasesProt.version ++; // such that others notice our change
631
- _currentDatabasesProt.isValid = true ; // will never be reset to false
603
+ WRITE_LOCKER (writeLocker, _currentProt.lock );
604
+ _current = currentBuilder;
605
+ if (swapDatabases) {
606
+ _currentDatabases.swap (newDatabases);
607
+ }
608
+ _currentProt.version ++; // such that others notice our change
609
+ _currentProt.isValid = true ; // will never be reset to false
632
610
}
633
- clearCurrentDatabases (newDatabases); // delete the old stuff
634
- return ;
611
+ } else {
612
+ LOG (ERR) << " Current is not an object! " ;
635
613
}
614
+ return ;
636
615
}
637
616
638
617
LOG (DEBUG) << " Error while loading " << prefixCurrentDatabases
@@ -946,7 +925,7 @@ int ClusterInfo::createDatabaseCoordinator(std::string const& name,
946
925
dbServerResult = TRI_ERROR_CLUSTER_COULD_NOT_CREATE_DATABASE;
947
926
return true ;
948
927
}
949
- loadCurrentDatabases (); // update our cache
928
+ loadCurrent (); // update our cache
950
929
dbServerResult = setErrormsg (TRI_ERROR_NO_ERROR, errorMsg);
951
930
}
952
931
return true ;
@@ -2576,8 +2555,8 @@ void ClusterInfo::invalidateCurrent() {
2576
2555
_coordinatorsProt.isValid = false ;
2577
2556
}
2578
2557
{
2579
- WRITE_LOCKER (writeLocker, _currentDatabasesProt .lock );
2580
- _currentDatabasesProt .isValid = false ;
2558
+ WRITE_LOCKER (writeLocker, _currentProt .lock );
2559
+ _currentProt .isValid = false ;
2581
2560
}
2582
2561
{
2583
2562
WRITE_LOCKER (writeLocker, _currentCollectionsProt.lock );
0 commit comments