8000 Merge branch 'json_agency_comm' into devel · lethalbrains/arangodb@dc95f2b · GitHub
[go: up one dir, main page]

Skip to content

Commit dc95f2b

Browse files
author
Andreas Streichardt
committed
Merge branch 'json_agency_comm' into devel
2 parents 9cbe13d + 1233186 commit dc95f2b

File tree

2 files changed

+60
-74
lines changed

2 files changed

+60
-74
lines changed

arangod/Cluster/ClusterInfo.cpp

Lines changed: 48 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ ClusterInfo::ClusterInfo(AgencyCallbackRegistry* agencyCallbackRegistry)
260260
////////////////////////////////////////////////////////////////////////////////
261261

262262
ClusterInfo::~ClusterInfo() {
263-
clearCurrentDatabases(_currentDatabases);
264263
}
265264

266265
////////////////////////////////////////////////////////////////////////////////
@@ -305,7 +304,7 @@ void ClusterInfo::flush() {
305304
loadCurrentDBServers();
306305
loadCurrentCoordinators();
307306
loadPlan();
308-
loadCurrentDatabases();
307+
loadCurrent();
309308
loadCurrentCollections();
310309
}
311310

@@ -317,9 +316,9 @@ bool ClusterInfo::doesDatabaseExist(DatabaseID const& databaseID, bool reload) {
317316
int tries = 0;
318317

319318
if (reload || !_planProt.isValid ||
320-
!_currentDatabasesProt.isValid || !_DBServersProt.isValid) {
319+
!_currentProt.isValid || !_DBServersProt.isValid) {
321320
loadPlan();
322-
loadCurrentDatabases();
321+
loadCurrent();
323322
loadCurrentDBServers();
324323
++tries; // no need to reload if the database is not found
325324
}
@@ -343,7 +342,7 @@ bool ClusterInfo::doesDatabaseExist(DatabaseID const& databaseID, bool reload) {
343342

344343
if (it != _plannedDatabases.end()) {
345344
// found the database in Plan
346-
READ_LOCKER(readLocker, _currentDatabasesProt.lock);
345+
READ_LOCKER(readLocker, _currentProt.lock);
347346
// _currentDatabases is
348347
// a map-type<DatabaseID, a map-type<ServerID, TRI_json_t*>>
349348
auto it2 = _currentDatabases.find(databaseID);
@@ -361,7 +360,7 @@ bool ClusterInfo::doesDatabaseExist(DatabaseID const& databaseID, bool reload) {
361360
}
362361

363362
loadPlan();
364-
loadCurrentDatabases();
363+
loadCurrent();
365364
loadCurrentDBServers();
366365
}
367366

@@ -376,9 +375,9 @@ std::vector<DatabaseID> ClusterInfo::listDatabases(bool reload) {
376375
std::vector<DatabaseID> result;
377376

378377
if (reload || !_planProt.isValid ||
379-
!_currentDatabasesProt.isValid || !_DBServersProt.isValid) {
378+
!_currentProt.isValid || !_DBServersProt.isValid) {
380379
loadPlan();
381-
loadCurrentDatabases();
380+
loadCurrent();
382381
loadCurrentDBServers();
383382
}
384383

@@ -393,7 +392,7 @@ std::vector<DatabaseID> ClusterInfo::listDatabases(bool reload) {
393392

394393
{
395394
READ_LOCKER(readLockerPlanned, _planProt.lock);
396-
READ_LOCKER(readLockerCurrent, _currentDatabasesProt.lock);
395+
READ_LOCKER(readLockerCurrent, _currentProt.lock);
397396
// _plannedDatabases is a map-type<DatabaseID, TRI_json_t*>
398397
auto it = _plannedDatabases.begin();
399398

@@ -540,43 +539,17 @@ void ClusterInfo::loadPlan() {
540539
<< " body: " << result.body();
541540
}
542541

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-
569542
////////////////////////////////////////////////////////////////////////////////
570543
/// @brief (re-)load the information about current databases
571544
/// Usually one does not have to call this directly.
572545
////////////////////////////////////////////////////////////////////////////////
573546

574547
static std::string const prefixCurrentDatabases = "Current/Databases";
575548

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) {
580553
// Somebody else did, what we intended to do, so just return
581554
return;
582555
}
@@ -591,48 +564,54 @@ void ClusterInfo::loadCurrentDatabases() {
591564
}
592565

593566
if (result.successful()) {
594-
595-
velocypack::Slice databases =
567+
568+
velocypack::Slice slice =
596569
result.slice()[0].get(std::vector<std::string>(
597-
{AgencyComm::prefixStripped(), "Current", "Databases"}));
570+
{AgencyComm::prefixStripped(), "Current"}));
598571

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()) {
600577
decltype(_currentDatabases) newDatabases;
601578

602-
for (auto const& dbase : VPackObjectIterator(databases)) {
579+
bool swapDatabases = false;
603580

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();
605586

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+
}
609590

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+
}
615595

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));
622597
}
623-
598+
swapDatabases = true;
624599
}
625600

626601
// Now set the new value:
627602
{
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
632610
}
633-
clearCurrentDatabases(newDatabases); // delete the old stuff
634-
return;
611+
} else {
612+
LOG(ERR) << "Current is not an object!";
635613
}
614+
return;
636615
}
637616

638617
LOG(DEBUG) << "Error while loading " << prefixCurrentDatabases
@@ -946,7 +925,7 @@ int ClusterInfo::createDatabaseCoordinator(std::string const& name,
946925
dbServerResult = TRI_ERROR_CLUSTER_COULD_NOT_CREATE_DATABASE;
947926
return true;
948927
}
949-
loadCurrentDatabases(); // update our cache
928+
loadCurrent(); // update our cache
950929
dbServerResult = setErrormsg(TRI_ERROR_NO_ERROR, errorMsg);
951930
}
952931
return true;
@@ -2576,8 +2555,8 @@ void ClusterInfo::invalidateCurrent() {
25762555
_coordinatorsProt.isValid = false;
25772556
}
25782557
{
2579-
WRITE_LOCKER(writeLocker, _currentDatabasesProt.lock);
2580-
_currentDatabasesProt.isValid = false;
2558+
WRITE_LOCKER(writeLocker, _currentProt.lock);
2559+
_currentProt.isValid = false;
25812560
}
25822561
{
25832562
WRITE_LOCKER(writeLocker, _currentCollectionsProt.lock);

arangod/Cluster/ClusterInfo.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class CollectionInfo {
107107
//////////////////////////////////////////////////////////////////////////////
108108

109109
std::string id_as_string() const {
110+
if (!_slice.isObject()) {
111+
return std::string("");
112+
}
110113
return arangodb::basics::VelocyPackHelper::getStringValue(_slice, "id", "");
111114
}
112115

@@ -115,6 +118,9 @@ class CollectionInfo {
115118
//////////////////////////////////////////////////////////////////////////////
116119

117120
std::string name() const {
121+
if (!_slice.isObject()) {
122+
return std::string("");
123+
}
118124
return arangodb::basics::VelocyPackHelper::getStringValue(_slice, "name", "");
119125
}
120126

@@ -277,7 +283,7 @@ class CollectionInfo {
277283
return false;
278284
}
279285

280-
auto firstElement = shardKeysSlice.get(0);
286+
auto firstElement = shardKeysSlice.at(0);
281287
TRI_ASSERT(firstElement.isString());
282288
std::string shardKey =
283289
arangodb::basics::VelocyPackHelper::getStringValue(firstElement, "");
@@ -593,11 +599,11 @@ class ClusterInfo {
593599
void loadPlan();
594600

595601
//////////////////////////////////////////////////////////////////////////////
596-
/// @brief (re-)load the information about current databases
602+
/// @brief (re-)load the information about current state
597603
/// Usually one does not have to call this directly.
598604
//////////////////////////////////////////////////////////////////////////////
599605

600-
void loadCurrentDatabases();
606+
void loadCurrent();
601607

602608
//////////////////////////////////////////////////////////////////////////////
603609
/// @brief ask about 97AE a collection
@@ -901,15 +907,16 @@ class ClusterInfo {
901907
ProtectionData _coordinatorsProt;
902908

903909
std::shared_ptr<VPackBuilder> _plan;
910+
std::shared_ptr<VPackBuilder> _current;
904911

905912
std::unordered_map<DatabaseID, VPackSlice> _plannedDatabases; // from Plan/Databases
906913

907914
ProtectionData _planProt;
908915

909916
std::unordered_map<DatabaseID,
910-
std::unordered_map<ServerID, struct TRI_json_t*>>
917+
std::unordered_map<ServerID, VPackSlice>>
911918
_currentDatabases; // from Current/Databases
912-
ProtectionData _currentDatabasesProt;
919+
ProtectionData _currentProt;
913920

914921
// We need information about collections, again we have
915922
// data from Plan and from Current.

0 commit comments

Comments
 (0)
0