//////////////////////////////////////////////////////////////////////////////// /// DISCLAIMER /// /// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany /// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. /// /// Copyright holder is ArangoDB GmbH, Cologne, Germany /// /// @author Jan Steemann //////////////////////////////////////////////////////////////////////////////// #include "CollectionNameResolver.h" #include "Basics/NumberUtils.h" #include "Basics/ReadLocker.h" #include "Basics/StringUtils.h" #include "Basics/WriteLocker.h" #include "Cluster/ClusterFeature.h" #include "Cluster/ClusterInfo.h" #include "VocBase/LogicalCollection.h" #include "VocBase/LogicalView.h" #include "VocBase/vocbase.h" namespace { std::string const UNKNOWN("_unknown"); } namespace arangodb { std::shared_ptr CollectionNameResolver::getCollection(TRI_voc_cid_t id) const { #ifdef ARANGODB_ENABLE_MAINTAINER_MODE return std::dynamic_pointer_cast(getDataSource(id)); #else auto dataSource = getDataSource(id); return dataSource && dataSource->category() == LogicalCollection::category() ? std::static_pointer_cast(dataSource) : nullptr; #endif } std::shared_ptr CollectionNameResolver::getCollection(std::string const& nameOrId) const { #ifdef ARANGODB_ENABLE_MAINTAINER_MODE return std::dynamic_pointer_cast(getDataSource(nameOrId)); #else auto dataSource = getDataSource(nameOrId); return dataSource && dataSource->category() == LogicalCollection::category() ? std::static_pointer_cast(dataSource) : nullptr; #endif } ////////////////////////////////////////////////////////////////////////////// /// @brief look up a collection id for a collection name (local case), /// use this if you know you are on a single server or on a DBserver /// and need to look up a local collection name (or shard name). ////////////////////////////////////////////////////////////////////////////// TRI_voc_cid_t CollectionNameResolver::getCollectionIdLocal(std::string const& name) const { if (name.empty()) { return 0; } if (name[0] >= '0' && name[0] <= '9') { // name is a numeric id return NumberUtils::atoi_zero(name.data(), name.data() + name.size()); } auto collection = _vocbase.lookupCollection(name); if (collection != nullptr) { return collection->id(); } auto view = _vocbase.lookupView(name); if (view) { return view->id(); } return 0; } ////////////////////////////////////////////////////////////////////////////// /// @brief look up a cluster collection id for a cluster collection name, /// only use this is in cluster mode on a coordinator or DBserver, in both /// cases the name is resolved as a cluster wide collection name and the /// cluster wide collection id is returned. ////////////////////////////////////////////////////////////////////////////// TRI_voc_cid_t CollectionNameResolver::getCollectionIdCluster(std::string const& name) const { if (!ServerState::isRunningInCluster(_serverRole)) { return getCollectionIdLocal(name); } if (name.empty()) { return 0; } if (name[0] >= '0' && name[0] <= '9') { // name is a numeric id TRI_voc_cid_t cid = NumberUtils::atoi_zero(name.data(), name.data() + name.size()); auto collection = getCollection(cid); // Now validate the cid auto type = collection ? collection->type() : TRI_COL_TYPE_UNKNOWN; if (type == TRI_COL_TYPE_UNKNOWN) { return 0; } return cid; } try { // We have to look up the collection info: if (!_vocbase.server().hasFeature()) { return 0; } auto& ci = _vocbase.server().getFeature().clusterInfo(); auto const cinfo = ci.getCollectionNT(_vocbase.name(), name); if (cinfo != nullptr) { return cinfo->id(); } auto const vinfo = ci.getView(_vocbase.name(), name); if (vinfo) { return vinfo->id(); } } catch (...) { } return 0; } std::shared_ptr CollectionNameResolver::getCollectionStructCluster( std::string const& name) const { if (!ServerState::isRunningInCluster(_serverRole)) { return _vocbase.lookupCollection(name); } // We have to look up the collection info: return _vocbase.server().hasFeature() ? _vocbase.server().getFeature().clusterInfo().getCollectionNT( _vocbase.name(), name) : nullptr; } ////////////////////////////////////////////////////////////////////////////// /// @brief look up a collection id for a collection name, this is the /// default one to use, which will usually do the right thing. On a /// single server or DBserver it will use the local lookup and on a /// coordinator it will use the cluster wide lookup. ////////////////////////////////////////////////////////////////////////////// TRI_voc_cid_t CollectionNameResolver::getCollectionId(std::string const& name) const { if (!ServerState::isRunningInCluster(_serverRole) || ServerState::isDBServer(_serverRole)) { return getCollectionIdLocal(name); } return getCollectionIdCluster(name); } ////////////////////////////////////////////////////////////////////////////// /// @brief look up a collection name for a collection id, this implements /// some magic in the cluster case: a DBserver in a cluster will automatically /// translate the local collection ID into a cluster wide collection name. ////////////////////////////////////////////////////////////////////////////// std::string CollectionNameResolver::getCollectionName(TRI_voc_cid_t cid) const { { READ_LOCKER(locker, _idLock); auto it = _resolvedIds.find(cid); if (it != _resolvedIds.end()) { return (*it).second; } } std::string name = lookupName(cid); { WRITE_LOCKER(locker, _idLock); _resolvedIds.emplace(cid, name); } return name; } ////////////////////////////////////////////////////////////////////////////// /// @brief look up a cluster-wide collection name for a cluster-wide /// collection id ////////////////////////////////////////////////////////////////////////////// std::string CollectionNameResolver::getCollectionNameCluster(TRI_voc_cid_t cid) const { if (!ServerState::isClusterRole(_serverRole)) { // This handles the case of a standalone server return getCollectionName(cid); } // First check the cache: { READ_LOCKER(locker, _idLock); auto it = _resolvedIds.find(cid); if (it != _resolvedIds.end()) { return (*it).second; } } std::string name; if (ServerState::isDBServer(_serverRole)) { // This might be a local system collection: name = lookupName(cid); if (name != ::UNKNOWN) { WRITE_LOCKER(locker, _idLock); _resolvedIds.emplace(cid, name); return name; } } int tries = 0; while (tries++ < 2) { auto ci = _vocbase.server().getFeature().clusterInfo().getCollectionNT( _vocbase.name(), arangodb::basics::StringUtils::itoa(cid)); if (ci != nullptr) { name = ci->name(); { WRITE_LOCKER(locker, _idLock); _resolvedIds.emplace(cid, name); } return name; } else { // most likely collection not found. now try again _vocbase.server().getFeature().clusterInfo().flush(); } } LOG_TOPIC("817e8", DEBUG, arangodb::Logger::FIXME) << "CollectionNameResolver: was not able to resolve id " << cid; return ::UNKNOWN; } ////////////////////////////////////////////////////////////////////////////// /// @brief return collection name if given string is either the name or /// a string with the (numerical) collection id, this returns the cluster /// wide collection name in the DBserver case ////////////////////////////////////////////////////////////////////////////// std::string CollectionNameResolver::getCollectionName(std::string const& nameOrId) const { if (!nameOrId.empty() && (nameOrId[0] < '0' || nameOrId[0] > '9')) { return nameOrId; } return getCollectionName( NumberUtils::atoi_zero(nameOrId.data(), nameOrId.data() + nameOrId.size())); } std::shared_ptr CollectionNameResolver::getDataSource(TRI_voc_cid_t id) const { auto itr = _dataSourceById.find(id); if (itr != _dataSourceById.end()) { return itr->second; } // db server / standalone auto ptr = ServerState::isCoordinator(_serverRole) ? getDataSource(std::to_string(id)) : _vocbase.lookupDataSource(id); if (ptr) { _dataSourceById.emplace(id, ptr); } return ptr; } std::shared_ptr CollectionNameResolver::getDataSource(std::string const& nameOrId) const { auto itr = _dataSourceByName.find(nameOrId); if (itr != _dataSourceByName.end()) { return itr->second; } std::shared_ptr ptr; // db server / standalone if (!ServerState::isCoordinator(_serverRole)) { ptr = _vocbase.lookupDataSource(nameOrId); } else { // cluster coordinator if (!_vocbase.server().hasFeature()) { return nullptr; } auto& ci = _vocbase.server().getFeature().clusterInfo(); ptr = ci.getCollectionNT(_vocbase.name(), nameOrId); if (ptr == nullptr) { try { ptr = ci.getView(_vocbase.name(), nameOrId); } catch (...) { LOG_TOPIC("426e6", ERR, arangodb::Logger::FIXME) << "caught exception while resolving cluster data-source: " << nameOrId; } } } if (ptr) { _dataSourceByName.emplace(nameOrId, ptr); } return ptr; } std::shared_ptr CollectionNameResolver::getView(TRI_voc_cid_t id) const { #ifdef ARANGODB_ENABLE_MAINTAINER_MODE return std::dynamic_pointer_cast(getDataSource(id)); #else auto dataSource = getDataSource(id); return dataSource && dataSource->category() == LogicalView::category() ? std::static_pointer_cast(dataSource) : nullptr; #endif } std::shared_ptr CollectionNameResolver::getView(std::string const& nameOrId) const { #ifdef ARANGODB_ENABLE_MAINTAINER_MODE return std::dynamic_pointer_cast(getDataSource(nameOrId)); #else auto dataSource = getDataSource(nameOrId); return dataSource && dataSource->category() == LogicalView::category() ? std::static_pointer_cast(dataSource) : nullptr; #endif } bool CollectionNameResolver::visitCollections(std::function const& visitor, TRI_voc_cid_t id) const { auto dataSource = getDataSource(id); if (!dataSource) { return false; // no way to determine what to visit } if (LogicalCollection::category() == dataSource->category()) { #ifdef ARANGODB_ENABLE_MAINTAINER_MODE auto collection = std::dynamic_pointer_cast(dataSource); TRI_ASSERT(collection); #else auto collection = std::static_pointer_cast(dataSource); #endif // TODO resolve smart edge collection CIDs here return visitor(*collection); } if (LogicalView::category() == dataSource->category()) { #ifdef ARANGODB_ENABLE_MAINTAINER_MODE auto view = std::dynamic_pointer_cast(dataSource); #else auto view = std::static_pointer_cast(dataSource); #endif // each CID in a view might need further resolution return view->visitCollections([this, &visitor, id](TRI_voc_cid_t cid) -> bool { return cid == id ? false : visitCollections(visitor, cid); // avoid infinite recursion }); } return false; // no way to determine what to visit } /// PRIVATE --------------- std::string CollectionNameResolver::lookupName(TRI_voc_cid_t cid) const { auto collection = _vocbase.lookupCollection(cid); // exactly as in the non-cluster case if (!ServerState::isDBServer(_serverRole)) { return collection ? collection->name() : ::UNKNOWN; } // DBserver case of a shard: if (collection && collection->planId() != collection->id()) { collection = _vocbase.server().getFeature().clusterInfo().getCollectionNT( collection->vocbase().name(), std::to_string(collection->planId())); } // can be empty, if collection unknown if (collection != nullptr && !collection->name().empty()) { return collection->name(); } return ::UNKNOWN; } } // namespace arangodb