-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycle_breaker_unittest.cc
278 lines (228 loc) · 9.03 KB
/
cycle_breaker_unittest.cc
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
//
// Copyright (C) 2010 The Android Open Source Project
//
// 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.
//
#include "update_engine/payload_generator/cycle_breaker.h"
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <base/logging.h>
#include <gtest/gtest.h>
#include "update_engine/common/utils.h"
#include "update_engine/payload_generator/graph_types.h"
using std::make_pair;
using std::pair;
using std::set;
using std::string;
using std::vector;
namespace chromeos_update_engine {
namespace {
void SetOpForNodes(Graph* graph) {
for (Vertex& vertex : *graph) {
vertex.aop.op.set_type(InstallOperation::MOVE);
}
}
} // namespace
class CycleBreakerTest : public ::testing::Test {};
TEST(CycleBreakerTest, SimpleTest) {
int counter = 0;
const Vertex::Index n_a = counter++;
const Vertex::Index n_b = counter++;
const Vertex::Index n_c = counter++;
const Vertex::Index n_d = counter++;
const Vertex::Index n_e = counter++;
const Vertex::Index n_f = counter++;
const Vertex::Index n_g = counter++;
const Vertex::Index n_h = counter++;
const Graph::size_type kNodeCount = counter++;
Graph graph(kNodeCount);
SetOpForNodes(&graph);
graph[n_a].out_edges.insert(make_pair(n_e, EdgeProperties()));
graph[n_a].out_edges.insert(make_pair(n_f, EdgeProperties()));
graph[n_b].out_edges.insert(make_pair(n_a, EdgeProperties()));
graph[n_c].out_edges.insert(make_pair(n_d, EdgeProperties()));
graph[n_d].out_edges.insert(make_pair(n_e, EdgeProperties()));
graph[n_d].out_edges.insert(make_pair(n_f, EdgeProperties()));
graph[n_e].out_edges.insert(make_pair(n_b, EdgeProperties()));
graph[n_e].out_edges.insert(make_pair(n_c, EdgeProperties()));
graph[n_e].out_edges.insert(make_pair(n_f, EdgeProperties()));
graph[n_f].out_edges.insert(make_pair(n_g, EdgeProperties()));
graph[n_g].out_edges.insert(make_pair(n_h, EdgeProperties()));
graph[n_h].out_edges.insert(make_pair(n_g, EdgeProperties()));
CycleBreaker breaker;
set<Edge> broken_edges;
breaker.BreakCycles(graph, &broken_edges);
// The following cycles must be cut:
// A->E->B
// C->D->E
// G->H
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_e)) ||
utils::SetContainsKey(broken_edges, make_pair(n_e, n_b)) ||
utils::SetContainsKey(broken_edges, make_pair(n_b, n_a)));
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_c, n_d)) ||
utils::SetContainsKey(broken_edges, make_pair(n_d, n_e)) ||
utils::SetContainsKey(broken_edges, make_pair(n_e, n_c)));
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_g, n_h)) ||
utils::SetContainsKey(broken_edges, make_pair(n_h, n_g)));
EXPECT_EQ(3U, broken_edges.size());
}
namespace {
pair<Vertex::Index, EdgeProperties> EdgeWithWeight(Vertex::Index dest,
uint64_t weight) {
EdgeProperties props;
props.extents.resize(1);
props.extents[0].set_num_blocks(weight);
return make_pair(dest, props);
}
} // namespace
// This creates a bunch of cycles like this:
//
// root <------.
// (t)-> / | \ |
// V V V |
// N N N |
// \ | / |
// VVV |
// N |
// / | \ |
// V V V |
// N N N |
// ... |
// (s)-> \ | / |
// VVV |
// N |
// \_________/
//
// such that the original cutting algo would cut edges (s). We changed
// the algorithm to cut cycles (t) instead, since they are closer to the
// root, and that can massively speed up cycle cutting.
TEST(CycleBreakerTest, AggressiveCutTest) {
size_t counter = 0;
const int kNodesPerGroup = 4;
const int kGroups = 33;
Graph graph(kGroups * kNodesPerGroup + 1); // + 1 for the root node
SetOpForNodes(&graph);
const Vertex::Index n_root = counter++;
Vertex::Index last_hub = n_root;
for (int i = 0; i < kGroups; i++) {
uint64_t weight = 5;
if (i == 0)
weight = 2;
else if (i == (kGroups - 1))
weight = 1;
const Vertex::Index next_hub = counter++;
for (int j = 0; j < (kNodesPerGroup - 1); j++) {
const Vertex::Index node = counter++;
graph[last_hub].out_edges.insert(EdgeWithWeight(node, weight));
graph[node].out_edges.insert(EdgeWithWeight(next_hub, weight));
}
last_hub = next_hub;
}
graph[last_hub].out_edges.insert(EdgeWithWeight(n_root, 5));
EXPECT_EQ(counter, graph.size());
CycleBreaker breaker;
set<Edge> broken_edges;
LOG(INFO) << "If this hangs for more than 1 second, the test has failed.";
breaker.BreakCycles(graph, &broken_edges);
set<Edge> expected_cuts;
for (Vertex::EdgeMap::const_iterator it = graph[n_root].out_edges.begin(),
e = graph[n_root].out_edges.end(); it != e; ++it) {
expected_cuts.insert(make_pair(n_root, it->first));
}
EXPECT_TRUE(broken_edges == expected_cuts);
}
TEST(CycleBreakerTest, WeightTest) {
size_t counter = 0;
const Vertex::Index n_a = counter++;
const Vertex::Index n_b = counter++;
const Vertex::Index n_c = counter++;
const Vertex::Index n_d = counter++;
const Vertex::Index n_e = counter++;
const Vertex::Index n_f = counter++;
const Vertex::Index n_g = counter++;
const Vertex::Index n_h = counter++;
const Vertex::Index n_i = counter++;
const Vertex::Index n_j = counter++;
const Graph::size_type kNodeCount = counter++;
Graph graph(kNodeCount);
SetOpForNodes(&graph);
graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 4));
graph[n_a].out_edges.insert(EdgeWithWeight(n_f, 3));
graph[n_a].out_edges.insert(EdgeWithWeight(n_h, 2));
graph[n_b].out_edges.insert(EdgeWithWeight(n_a, 3));
graph[n_b].out_edges.insert(EdgeWithWeight(n_c, 4));
graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 5));
graph[n_c].out_edges.insert(EdgeWithWeight(n_d, 3));
graph[n_d].out_edges.insert(EdgeWithWeight(n_a, 6));
graph[n_d].out_edges.insert(EdgeWithWeight(n_e, 3));
graph[n_e].out_edges.insert(EdgeWithWeight(n_d, 4));
graph[n_e].out_edges.insert(EdgeWithWeight(n_g, 5));
graph[n_f].out_edges.insert(EdgeWithWeight(n_g, 2));
graph[n_g].out_edges.insert(EdgeWithWeight(n_f, 3));
graph[n_g].out_edges.insert(EdgeWithWeight(n_d, 5));
graph[n_h].out_edges.insert(EdgeWithWeight(n_i, 8));
graph[n_i].out_edges.insert(EdgeWithWeight(n_e, 4));
graph[n_i].out_edges.insert(EdgeWithWeight(n_h, 9));
graph[n_i].out_edges.insert(EdgeWithWeight(n_j, 6));
CycleBreaker breaker;
set<Edge> broken_edges;
breaker.BreakCycles(graph, &broken_edges);
// These are required to be broken:
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_b, n_a)));
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_b, n_c)));
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_d, n_e)));
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_f, n_g)));
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_h, n_i)));
}
TEST(CycleBreakerTest, UnblockGraphTest) {
size_t counter = 0;
const Vertex::Index n_a = counter++;
const Vertex::Index n_b = counter++;
const Vertex::Index n_c = counter++;
const Vertex::Index n_d = counter++;
const Graph::size_type kNodeCount = counter++;
Graph graph(kNodeCount);
SetOpForNodes(&graph);
graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 1));
graph[n_a].out_edges.insert(EdgeWithWeight(n_c, 1));
graph[n_b].out_edges.insert(EdgeWithWeight(n_c, 2));
graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 2));
graph[n_b].out_edges.insert(EdgeWithWeight(n_d, 2));
graph[n_d].out_edges.insert(EdgeWithWeight(n_a, 2));
CycleBreaker breaker;
set<Edge> broken_edges;
breaker.BreakCycles(graph, &broken_edges);
// These are required to be broken:
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_b)));
EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_c)));
}
TEST(CycleBreakerTest, SkipOpsTest) {
size_t counter = 0;
const Vertex::Index n_a = counter++;
const Vertex::Index n_b = counter++;
const Vertex::Index n_c = counter++;
const Graph::size_type kNodeCount = counter++;
Graph graph(kNodeCount);
SetOpForNodes(&graph);
graph[n_a].aop.op.set_type(InstallOperation::REPLACE_BZ);
graph[n_c].aop.op.set_type(InstallOperation::REPLACE);
graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 1));
graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 1));
CycleBreaker breaker;
set<Edge> broken_edges;
breaker.BreakCycles(graph, &broken_edges);
EXPECT_EQ(2U, breaker.skipped_ops());
}
} // namespace chromeos_update_engine