1
2 /**
3 * Copyright (C) 2018-present MongoDB, Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the Server Side Public License, version 1,
7 * as published by MongoDB, Inc.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * Server Side Public License for more details.
13 *
14 * You should have received a copy of the Server Side Public License
15 * along with this program. If not, see
16 * <http://www.mongodb.com/licensing/server-side-public-license>.
17 *
18 * As a special exception, the copyright holders give permission to link the
19 * code of portions of this program with the OpenSSL library under certain
20 * conditions as described in each individual source file and distribute
21 * linked combinations including the program with the OpenSSL library. You
22 * must comply with the Server Side Public License in all respects for
23 * all of the code used other than as permitted herein. If you modify file(s)
24 * with this exception, you may extend this exception to your version of the
25 * file(s), but you are not obligated to do so. If you do not wish to do so,
26 * delete this exception statement from your version. If you delete this
27 * exception statement from all source files in the program, then also delete
28 * it in the license file.
29 */
30
31 #include "mongo/base/initializer_dependency_graph.h"
32
33 #include <algorithm>
34 #include <iterator>
35 #include <sstream>
36
37 namespace mongo {
38
InitializerDependencyGraph()39 InitializerDependencyGraph::InitializerDependencyGraph() {}
~InitializerDependencyGraph()40 InitializerDependencyGraph::~InitializerDependencyGraph() {}
41
addInitializer(const std::string & name,const InitializerFunction & fn,const std::vector<std::string> & prerequisites,const std::vector<std::string> & dependents)42 Status InitializerDependencyGraph::addInitializer(const std::string& name,
43 const InitializerFunction& fn,
44 const std::vector<std::string>& prerequisites,
45 const std::vector<std::string>& dependents) {
46 if (!fn)
47 return Status(ErrorCodes::BadValue, "Illegal to supply a NULL function");
48
49 NodeData& newNode = _nodes[name];
50 if (newNode.fn) {
51 return Status(ErrorCodes::DuplicateKey, name);
52 }
53
54 newNode.fn = fn;
55
56 for (size_t i = 0; i < prerequisites.size(); ++i) {
57 newNode.prerequisites.insert(prerequisites[i]);
58 }
59
60 for (size_t i = 0; i < dependents.size(); ++i) {
61 _nodes[dependents[i]].prerequisites.insert(name);
62 }
63
64 return Status::OK();
65 }
66
getInitializerFunction(const std::string & name) const67 InitializerFunction InitializerDependencyGraph::getInitializerFunction(
68 const std::string& name) const {
69 NodeMap::const_iterator iter = _nodes.find(name);
70 if (iter == _nodes.end())
71 return InitializerFunction();
72 return iter->second.fn;
73 }
74
topSort(std::vector<std::string> * sortedNames) const75 Status InitializerDependencyGraph::topSort(std::vector<std::string>* sortedNames) const {
76 /*
77 * This top-sort is implemented by performing a depth-first traversal of the dependency
78 * graph, once for each node. "visitedNodeNames" tracks the set of node names ever visited,
79 * and it is used to prune each DFS. A node that has been visited once on any DFS is never
80 * visited again. Complexity of this implementation is O(n+m) where "n" is the number of
81 * nodes and "m" is the number of prerequisite edges. Space complexity is O(n), in both
82 * stack space and size of the "visitedNodeNames" set.
83 *
84 * "inProgressNodeNames" is used to detect and report cycles.
85 */
86
87 std::vector<std::string> inProgressNodeNames;
88 unordered_set<std::string> visitedNodeNames;
89
90 sortedNames->clear();
91 for (const auto& node : _nodes) {
92 Status status =
93 recursiveTopSort(_nodes, node, &inProgressNodeNames, &visitedNodeNames, sortedNames);
94 if (Status::OK() != status)
95 return status;
96 }
97 for (const auto& node : _nodes) {
98 if (!node.second.fn) {
99 std::ostringstream os;
100 os << "No implementation provided for initializer " << node.first;
101 return {ErrorCodes::BadValue, os.str()};
102 }
103 }
104 return Status::OK();
105 }
106
recursiveTopSort(const NodeMap & nodeMap,const Node & currentNode,std::vector<std::string> * inProgressNodeNames,unordered_set<std::string> * visitedNodeNames,std::vector<std::string> * sortedNames)107 Status InitializerDependencyGraph::recursiveTopSort(const NodeMap& nodeMap,
108 const Node& currentNode,
109 std::vector<std::string>* inProgressNodeNames,
110 unordered_set<std::string>* visitedNodeNames,
111 std::vector<std::string>* sortedNames) {
112
113 /*
114 * The top sort is performed by depth-first traversal starting at each node in the
115 * dependency graph, short-circuited any time a node is seen that has already been visited
116 * in any traversal. "visitedNodeNames" is the set of nodes that have been successfully
117 * visited, while "inProgressNodeNames" are nodes currently in the exploration chain. This
118 * structure is kept explicitly to facilitate cycle detection.
119 *
120 * This function implements a depth-first traversal, and is called once for each node in the
121 * graph by topSort(), above.
122 */
123
124 if ((*visitedNodeNames).count(currentNode.first))
125 return Status::OK();
126
127 inProgressNodeNames->push_back(currentNode.first);
128
129 auto firstOccurence =
130 std::find(inProgressNodeNames->begin(), inProgressNodeNames->end(), currentNode.first);
131 if (std::next(firstOccurence) != inProgressNodeNames->end()) {
132 sortedNames->clear();
133 std::copy(firstOccurence, inProgressNodeNames->end(), std::back_inserter(*sortedNames));
134 std::ostringstream os;
135 os << "Cycle in dependendcy graph: " << sortedNames->at(0);
136 for (size_t i = 1; i < sortedNames->size(); ++i)
137 os << " -> " << sortedNames->at(i);
138 return Status(ErrorCodes::GraphContainsCycle, os.str());
139 }
140
141 for (const auto& prereq : currentNode.second.prerequisites) {
142 auto nextNode = nodeMap.find(prereq);
143 if (nextNode == nodeMap.end()) {
144 std::ostringstream os;
145 os << "Initializer " << currentNode.first << " depends on missing initializer "
146 << prereq;
147 return {ErrorCodes::BadValue, os.str()};
148 }
149
150 Status status = recursiveTopSort(
151 nodeMap, *nextNode, inProgressNodeNames, visitedNodeNames, sortedNames);
152 if (Status::OK() != status)
153 return status;
154 }
155 sortedNames->push_back(currentNode.first);
156 if (inProgressNodeNames->back() != currentNode.first)
157 return Status(ErrorCodes::InternalError, "inProgressNodeNames stack corrupt");
158 inProgressNodeNames->pop_back();
159 visitedNodeNames->insert(currentNode.first);
160 return Status::OK();
161 }
162
163 } // namespace mongo
164