-
Notifications
You must be signed in to change notification settings - Fork 416
/
run_append_frag.cc
275 lines (229 loc) · 8.79 KB
/
run_append_frag.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
/** Copyright 2020 Alibaba Group Holding Limited.
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 <sys/stat.h>
#include <cstdio>
#include <fstream>
#include <memory>
#include <string>
#include "gflags/gflags.h"
#include "gnn_sampler/kafka_consumer.h"
#include "grape/util.h"
#include "vineyard/client/client.h"
#include "apps/property/sssp_property_append.h"
#include "core/flags.h"
#include "core/loader/append_only_arrow_fragment_loader.h"
#include "core/loader/arrow_fragment_appender.h"
DEFINE_string(efile, "", "edge file");
DEFINE_string(vfile, "", "vertex file");
DEFINE_bool(directed, false, "input graph is directed or not.");
DEFINE_int64(sssp_source, 0, "Source vertex of sssp.");
DEFINE_string(vineyard_socket, "", "Unix domain socket path for vineyardd");
// for append frag
DEFINE_int32(elabel_num, 1, "");
DEFINE_int32(vlabel_num, 1, "");
DEFINE_bool(debug, false, "");
DEFINE_string(input_topic, "append_only_frag", "kafka input topic.");
DEFINE_string(
broker_list, "localhost:9092",
"list of kafka brokers, with formati: \'server1:port,server2:port,...\'.");
DEFINE_string(group_id, "grape_consumer", "kafka consumer group id.");
DEFINE_int32(partition_num, 1, "kafka topic partition number.");
DEFINE_int32(batch_size, 10000, "kafka consume messages batch size.");
DEFINE_int64(time_interval, 10, "kafka consume time interval/s");
template <typename FRAG_T>
void RunSSSP(std::shared_ptr<FRAG_T> fragment, const grape::CommSpec& comm_spec,
const std::string& out_prefix, typename FRAG_T::oid_t src_oid) {
using AppType = gs::SSSPPropertyAppend<FRAG_T>;
auto app = std::make_shared<AppType>();
auto worker = AppType::CreateWorker(app, fragment);
auto spec = grape::DefaultParallelEngineSpec();
spec.thread_num = 1;
worker->Init(comm_spec, spec);
worker->Query(src_oid);
std::ofstream ostream;
std::string output_path =
grape::GetResultFilename(out_prefix, fragment->fid());
ostream.open(output_path);
worker->Output(ostream);
ostream.close();
worker->Finalize();
}
void read_lines(std::string path,
std::function<void(std::vector<std::string>&)> cb) {
std::vector<std::string> buffer;
std::ifstream in(path);
std::string line;
bool first = true;
while (std::getline(in, line)) {
if (first) {
first = false;
continue;
}
buffer.push_back(line);
if (buffer.size() == (size_t) FLAGS_batch_size) {
cb(buffer);
buffer.clear();
}
}
if (!buffer.empty()) {
cb(buffer);
}
in.close();
}
int main(int argc, char** argv) {
FLAGS_stderrthreshold = 0;
google::SetUsageMessage(
"Usage: mpiexec [mpi_opts] ./run_append_frag <ipc_socket> <efile_prefix> "
"<e_label_num> <vfile_prefix> <v_label_num> <kafka config>");
if (argc == 1) {
google::ShowUsageWithFlagsRestrict(argv[0], "run_append_frag");
exit(1);
}
google::ParseCommandLineFlags(&argc, &argv, true);
google::ShutDownCommandLineFlags();
google::InitGoogleLogging(argv[0]);
google::InstallFailureSignalHandler();
std::string ipc_socket = FLAGS_vineyard_socket;
std::string epath = FLAGS_efile;
int edge_label_num = FLAGS_elabel_num;
std::string vpath = FLAGS_vfile;
int vertex_label_num = FLAGS_vlabel_num;
CHECK(vertex_label_num > 0 && vertex_label_num < 10);
CHECK(edge_label_num > 0 && edge_label_num < 10);
grape::InitMPIComm();
{
grape::CommSpec comm_spec;
comm_spec.Init(MPI_COMM_WORLD);
grape::Communicator communicator;
communicator.InitCommunicator(comm_spec.comm());
vineyard::Client client;
VINEYARD_CHECK_OK(client.Connect(ipc_socket));
LOG(INFO) << "Connected to IPCServer: " << ipc_socket;
vineyard::ObjectID fragment_id;
using OID_T = vineyard::property_graph_types::OID_TYPE;
// using OID_T = std::string;
using VID_T = vineyard::property_graph_types::VID_TYPE;
using GraphType = gs::AppendOnlyArrowFragment<OID_T, VID_T>;
if (FLAGS_debug) {
volatile int i = 0;
char hostname[256];
gethostname(hostname, sizeof(hostname));
printf("PID %d on %s ready for attach\n", getpid(), hostname);
fflush(stdout);
while (0 == i) {
sleep(1);
}
}
{
auto loader =
std::make_unique<gs::AppendOnlyArrowFragmentLoader<OID_T, VID_T>>(
client, comm_spec, vertex_label_num, edge_label_num, epath, vpath,
FLAGS_directed);
fragment_id = boost::leaf::try_handle_all(
[&loader]() { return loader->LoadFragment(); },
[](const vineyard::GSError& e) {
LOG(FATAL) << "Failed to load fragment: " << e.error_msg;
return 0;
},
[](boost::leaf::error_info const& unmatched) {
LOG(FATAL) << "Unknown failure detected" << unmatched;
return 0;
});
if (fragment_id == 0) {
return 1;
}
}
LOG(INFO) << "[worker-" << comm_spec.worker_id()
<< "] loaded graph to vineyard ...";
MPI_Barrier(comm_spec.comm());
std::shared_ptr<GraphType> fragment =
std::dynamic_pointer_cast<GraphType>(client.GetObject(fragment_id));
gs::ArrowFragmentAppender<OID_T, VID_T> appender(comm_spec, fragment);
mkdir("/tmp/sssp_out", 0777);
MPI_Barrier(comm_spec.comm());
{
auto begin = grape::GetCurrentTime();
RunSSSP<GraphType>(fragment, comm_spec, "/tmp/sssp_out",
FLAGS_sssp_source);
LOG(INFO) << "SSSP(original) time: " << grape::GetCurrentTime() - begin;
}
bool is_coordinator = (comm_spec.worker_id() == grape::kCoordinatorRank);
std::unique_ptr<KafkaConsumer> consumer;
if (is_coordinator) {
consumer = std::make_unique<KafkaConsumer>(
comm_spec.worker_id(), FLAGS_broker_list, FLAGS_group_id,
FLAGS_input_topic, FLAGS_partition_num, FLAGS_time_interval,
FLAGS_batch_size);
}
int64_t total_new_edges_num;
do {
std::vector<std::vector<std::string>> label_vertex_messages;
std::vector<std::vector<std::string>> label_edge_messages;
if (is_coordinator) {
label_vertex_messages.resize(FLAGS_vlabel_num);
label_edge_messages.resize(FLAGS_elabel_num);
std::vector<std::string> vertex_messages;
std::vector<std::string> edge_messages;
consumer->ConsumeMessages(vertex_messages, edge_messages);
// expected message format: vertex msg: "vlabel id,properties..."
// edge msg: "elabel
// src,dst,src_label,dst_label,properties..."
for (auto& v_msg : vertex_messages) {
auto v_label = v_msg[0] - '0';
CHECK(v_label >= 0 && v_label < FLAGS_vlabel_num);
label_vertex_messages[v_label].push_back(
v_msg.substr(2, v_msg.size() - 2));
}
for (auto& e_msg : edge_messages) {
auto e_label = e_msg[0] - '0';
CHECK(e_label >= 0 && e_label < FLAGS_elabel_num);
auto line = e_msg.substr(2, e_msg.size() - 2);
label_edge_messages[e_label].push_back(line);
}
}
{
auto begin = grape::GetCurrentTime();
int64_t new_edges_num = boost::leaf::try_handle_all(
[&] {
return appender.ExtendFragment(label_vertex_messages,
label_edge_messages, false, ',',
FLAGS_directed);
},
[](const vineyard::GSError& e) {
LOG(FATAL) << e.error_msg;
return 0;
},
[](boost::leaf::error_info const& unmatched) {
LOG(FATAL) << "Unknown failure detected" << unmatched;
return 0;
});
communicator.Sum(new_edges_num, total_new_edges_num);
MPI_Barrier(comm_spec.comm());
if (is_coordinator) {
LOG(INFO) << "New edges: " << total_new_edges_num
<< " Expend time: " << grape::GetCurrentTime() - begin;
}
}
{
auto begin = grape::GetCurrentTime();
RunSSSP<GraphType>(fragment, comm_spec, "/tmp/sssp_out",
FLAGS_sssp_source);
if (is_coordinator) {
LOG(INFO) << "SSSP(appended) time: "
<< grape::GetCurrentTime() - begin;
}
}
} while (total_new_edges_num > 0);
}
grape::FinalizeMPIComm();
return 0;
}