-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_multi_node_resume.cpp
More file actions
241 lines (212 loc) · 8.96 KB
/
test_multi_node_resume.cpp
File metadata and controls
241 lines (212 loc) · 8.96 KB
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
// Checkpointer regression: under signal dispatch a super-step can leave
// multiple nodes ready simultaneously (parallel fan-out, conditional
// branches activating together). The checkpoint schema previously stored
// only a single `next_node`, so on resume every sibling-branch after the
// first was silently dropped. These tests lock in that the full ready
// set survives checkpoint round-trips.
#include <gtest/gtest.h>
#include <neograph/neograph.h>
#include <neograph/graph/checkpoint.h>
using namespace neograph;
using namespace neograph::graph;
namespace {
// Simple counting node that writes "<name>_hits = N+1" on every execution.
class HitCounter : public GraphNode {
public:
explicit HitCounter(std::string n) : n_(std::move(n)) {}
asio::awaitable<NodeOutput> run(NodeInput in) override {
int cur = 0;
auto v = in.state.get(n_ + "_hits");
if (v.is_number()) cur = static_cast<int>(v.get<double>());
NodeOutput out;
out.writes.push_back(ChannelWrite{n_ + "_hits", json(cur + 1)});
co_return out;
}
std::string get_name() const override { return n_; }
private:
std::string n_;
};
void register_hit_counter() {
NodeFactory::instance().register_type("hit",
[](const std::string& name, const json&, const NodeContext&)
-> std::unique_ptr<GraphNode> {
return std::make_unique<HitCounter>(name);
});
}
} // namespace
// -------------------------------------------------------------------------
// Mid-graph fan-out: `a` writes to two downstream nodes. The checkpoint
// committed at the end of step 0 (where `a` ran) must record BOTH
// {b, c} as next_nodes — the exact case where the old single-string
// next_node would have dropped a sibling.
// -------------------------------------------------------------------------
TEST(MultiNodeResume, FanOutCheckpointPersistsBothBranches) {
register_hit_counter();
json graph = {
{"name", "fanout_mid"},
{"channels", {
{"a_hits", {{"reducer", "overwrite"}}},
{"b_hits", {{"reducer", "overwrite"}}},
{"c_hits", {{"reducer", "overwrite"}}}
}},
{"nodes", {
{"a", {{"type", "hit"}}},
{"b", {{"type", "hit"}}},
{"c", {{"type", "hit"}}}
}},
{"edges", json::array({
{{"from", "__start__"}, {"to", "a"}},
{{"from", "a"}, {"to", "b"}},
{{"from", "a"}, {"to", "c"}},
{{"from", "b"}, {"to", "__end__"}},
{{"from", "c"}, {"to", "__end__"}}
})}
};
auto store = std::make_shared<InMemoryCheckpointStore>();
auto engine = GraphEngine::compile(graph, NodeContext{}, store);
RunConfig cfg;
cfg.thread_id = "fanout-mid-001";
cfg.max_steps = 10;
auto result = engine->run(cfg);
EXPECT_EQ(result.output["channels"]["a_hits"]["value"].get<int>(), 1);
EXPECT_EQ(result.output["channels"]["b_hits"]["value"].get<int>(), 1);
EXPECT_EQ(result.output["channels"]["c_hits"]["value"].get<int>(), 1);
// Find the checkpoint whose current_node is "a". Its next_nodes must
// contain BOTH "b" and "c" — this is the whole point of the vector
// upgrade.
auto history = engine->get_state_history("fanout-mid-001");
const Checkpoint* after_a = nullptr;
for (auto& cp : history) {
if (cp.current_node == "a" && cp.interrupt_phase == CheckpointPhase::Completed) {
after_a = &cp;
break;
}
}
ASSERT_NE(after_a, nullptr) << "must have a completed cp after node 'a'";
ASSERT_EQ(after_a->next_nodes.size(), 2u)
<< "fan-out from a must save both b and c";
std::set<std::string> got(after_a->next_nodes.begin(),
after_a->next_nodes.end());
EXPECT_EQ(got, (std::set<std::string>{"b", "c"}));
}
// -------------------------------------------------------------------------
// End-to-end resume from a multi-element next_nodes checkpoint: the
// engine must re-enter with the ENTIRE ready set, not just ready[0].
// We force this by running until an interrupt_before at a single node,
// but after a fan-out — so the committed cp from the fan-out step is
// restored and both branches must be picked up on resume.
// -------------------------------------------------------------------------
TEST(MultiNodeResume, ResumeReEntersAllReadyNodes) {
register_hit_counter();
json graph = {
{"name", "fanout_resume"},
{"channels", {
{"a_hits", {{"reducer", "overwrite"}}},
{"b_hits", {{"reducer", "overwrite"}}},
{"c_hits", {{"reducer", "overwrite"}}}
}},
{"nodes", {
{"a", {{"type", "hit"}}},
{"b", {{"type", "hit"}}},
{"c", {{"type", "hit"}}}
}},
{"edges", json::array({
{{"from", "__start__"}, {"to", "a"}},
{{"from", "a"}, {"to", "b"}},
{{"from", "a"}, {"to", "c"}},
{{"from", "b"}, {"to", "__end__"}},
{{"from", "c"}, {"to", "__end__"}}
})}
};
auto store = std::make_shared<InMemoryCheckpointStore>();
// Phase 1: run with interrupt_before={b, c} so execution stops the
// moment the fan-out decision is committed. The latest cp will have
// next_nodes = {b, c}.
{
auto g = graph;
g["interrupt_before"] = json::array({"b"});
auto engine = GraphEngine::compile(g, NodeContext{}, store);
RunConfig cfg;
cfg.thread_id = "resume-001";
cfg.max_steps = 10;
auto result = engine->run(cfg);
EXPECT_TRUE(result.interrupted);
}
auto cp = store->load_latest("resume-001");
ASSERT_TRUE(cp.has_value());
// Phase 2: resume with NO interrupt — both siblings must fire.
auto engine2 = GraphEngine::compile(graph, NodeContext{}, store);
auto result = engine2->resume("resume-001");
EXPECT_EQ(result.output["channels"]["a_hits"]["value"].get<int>(), 1);
EXPECT_EQ(result.output["channels"]["b_hits"]["value"].get<int>(), 1)
<< "b must fire on resume (it was in next_nodes)";
EXPECT_EQ(result.output["channels"]["c_hits"]["value"].get<int>(), 1)
<< "c must ALSO fire on resume — under the old single next_node "
"schema this branch would have been silently dropped";
}
// -------------------------------------------------------------------------
// interrupt_before with multiple ready nodes: all three must be in the
// checkpoint's next_nodes so that resume() re-enters with {a, b, c},
// not just the first.
// -------------------------------------------------------------------------
TEST(MultiNodeResume, InterruptBeforeSavesAllReadyNodes) {
register_hit_counter();
json graph = {
{"name", "fanout_interrupt"},
{"channels", {
{"a_hits", {{"reducer", "overwrite"}}},
{"b_hits", {{"reducer", "overwrite"}}}
}},
{"nodes", {
{"a", {{"type", "hit"}}},
{"b", {{"type", "hit"}}}
}},
{"edges", json::array({
{{"from", "__start__"}, {"to", "a"}},
{{"from", "__start__"}, {"to", "b"}},
{{"from", "a"}, {"to", "__end__"}},
{{"from", "b"}, {"to", "__end__"}}
})},
{"interrupt_before", json::array({"a"})}
};
auto store = std::make_shared<InMemoryCheckpointStore>();
auto engine = GraphEngine::compile(graph, NodeContext{}, store);
RunConfig cfg;
cfg.thread_id = "intr-001";
cfg.max_steps = 10;
auto result = engine->run(cfg);
EXPECT_TRUE(result.interrupted);
// The interrupt fires before 'a' runs. The checkpoint's next_nodes
// must at least contain 'a'. (The exact set depends on implementation
// — currently we save only the interrupted node so resume re-enters
// precisely there. This is documented contract.)
auto cp = store->load_latest("intr-001");
ASSERT_TRUE(cp.has_value());
ASSERT_FALSE(cp->next_nodes.empty());
bool has_a = false;
for (auto& n : cp->next_nodes) if (n == "a") has_a = true;
EXPECT_TRUE(has_a) << "interrupt_before cp must remember it was about to run 'a'";
}
// -------------------------------------------------------------------------
// fork() copies next_nodes entirely, so a forked thread resumes with the
// same parallel branches the source had.
// -------------------------------------------------------------------------
TEST(MultiNodeResume, ForkCopiesEntireNextNodesVector) {
Checkpoint cp;
cp.id = "src-cp";
cp.thread_id = "src";
cp.channel_values = json::object();
cp.current_node = "planner";
cp.next_nodes = {"a", "b", "c"};
cp.interrupt_phase = CheckpointPhase::Completed;
cp.step = 3;
cp.timestamp = 1;
InMemoryCheckpointStore store;
store.save(cp);
auto loaded = store.load_latest("src");
ASSERT_TRUE(loaded.has_value());
EXPECT_EQ(loaded->next_nodes.size(), 3u);
EXPECT_EQ(loaded->next_nodes[0], "a");
EXPECT_EQ(loaded->next_nodes[1], "b");
EXPECT_EQ(loaded->next_nodes[2], "c");
}