Repository navigation
Expand file tree
/
Copy pathDTL.cpp
More file actions
162 lines (142 loc) · 5.87 KB
/
Copy pathDTL.cpp
File metadata and controls
162 lines (142 loc) · 5.87 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
/* Copyright (c) 2022-2026. The SWAT Team. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* under the terms of the license (GNU LGPL) which comes with this package. */
#include <simgrid/s4u/Actor.hpp>
#include <simgrid/s4u/MessageQueue.hpp>
#include "dtlmod/DTL.hpp"
#include "dtlmod/DTLException.hpp"
#include <fstream>
#include <nlohmann/json.hpp>
XBT_LOG_NEW_DEFAULT_CATEGORY(dtlmod, "DTLMod internal logging");
XBT_LOG_NEW_SUBCATEGORY(dtl, dtlmod, "DTL logging");
namespace sg4 = simgrid::s4u;
namespace dtlmod {
/// \cond EXCLUDE_FROM_DOCUMENTATION
DTL::DTL(std::string_view filename)
{
// No configuration file has been provided. Nothing else to do
if (filename.empty())
return;
// Parse the configuration file
std::ifstream f{std::string(filename)};
auto data = nlohmann::json::parse(f);
// Get the list of declared Streams
for (auto const& stream : data["streams"]) {
// Get the Stream name
auto name = stream["name"].get<std::string>();
// Parse the Engine type
Engine::Type type;
if (stream["engine"]["type"] == "File")
type = Engine::Type::File;
else if (stream["engine"]["type"] == "Staging")
type = Engine::Type::Staging;
else
throw UnknownEngineTypeException(XBT_THROW_POINT, "");
// Parse the Transport Method
Transport::Method transport_method;
if (stream["engine"]["transport_method"] == "File")
transport_method = Transport::Method::File;
else if (stream["engine"]["transport_method"] == "Mailbox")
transport_method = Transport::Method::Mailbox;
else if (stream["engine"]["transport_method"] == "MQ")
transport_method = Transport::Method::MQ;
else
throw UnknownTransportMethodException(XBT_THROW_POINT, "");
// If the Stream doesn't exist yet, create it
if (streams_.find(name) == streams_.end())
streams_.try_emplace(name, std::make_shared<Stream>(name, this));
// And set its engine type and transport method
streams_[name]->set_engine_type(type).set_transport_method(transport_method);
// Check if metadata must be exported for this stream
if (stream.contains("export_metadata")) {
streams_[name]->set_metadata_export();
}
}
}
void DTL::connection_manager_connect(sg4::Actor* actor)
{
if (active_connections_.find(actor) == active_connections_.end()) {
active_connections_.insert(actor);
XBT_DEBUG("Connection of %s to internal DTL server: %zu active connections", actor->get_cname(),
active_connections_.size());
} else
XBT_WARN("%s is already connected to the DTL. Check your code. ", actor->get_cname());
}
void DTL::connection_manager_disconnect(sg4::Actor* actor)
{
if (active_connections_.find(actor) == active_connections_.end()) {
XBT_WARN("%s is not connected to the DTL. Check your code. ", actor->get_cname());
} else {
active_connections_.erase(actor);
XBT_DEBUG("Disconnection from internal DTL server: %zu active connections", active_connections_.size());
}
}
__attribute__((noreturn)) void DTL::connection_manager_init(std::shared_ptr<DTL> dtl)
{
XBT_DEBUG("Connection_manager is running. waiting for connections");
auto connect_mq = sg4::MessageQueue::by_name("dtlmod::connection_manager_connect");
auto handler_mq = sg4::MessageQueue::by_name("dtlmod::connection_manager_handle");
do {
// Wait for a connection/disconnection message on connect_mq
bool* connect = nullptr;
auto mess = connect_mq->get_async(&connect);
mess->wait();
std::unique_ptr<bool> connect_guard(connect); // RAII: automatic cleanup
auto* sender = mess->get_sender();
if (*connect_guard) { // Connection
dtl->connection_manager_connect(sender);
// Transfer ownership via unique_ptr for exception safety
auto dtl_copy = std::make_unique<std::shared_ptr<DTL>>(dtl);
handler_mq->put_init(dtl_copy.release())->detach();
} else { // Disconnection
dtl->connection_manager_disconnect(sender);
auto payload = std::make_unique<bool>(true);
handler_mq->put_init(payload.release())->detach();
if (!dtl->has_active_connections())
XBT_WARN("The DTL has no active connection");
}
// connect_guard automatically deletes connect at end of scope
} while (true);
}
///\endcond
/*** Public interface ***/
void DTL::create(std::string_view filename)
{
XBT_DEBUG("Creating the DTL connection manager"); // as a daemon running on the first actor in the platform
sg4::Engine::get_instance()
->get_all_hosts()
.front()
->add_actor("dtlmod::connection_manager", DTL::connection_manager_init, std::make_shared<DTL>(filename))
->daemonize();
}
std::shared_ptr<DTL> DTL::connect()
{
auto payload = std::make_unique<bool>(true);
sg4::MessageQueue::by_name("dtlmod::connection_manager_connect")->put(payload.release());
auto handle = sg4::MessageQueue::by_name("dtlmod::connection_manager_handle")->get_unique<std::shared_ptr<DTL>>();
return *handle;
}
void DTL::disconnect()
{
auto payload = std::make_unique<bool>(false);
sg4::MessageQueue::by_name("dtlmod::connection_manager_connect")->put(payload.release());
sg4::MessageQueue::by_name("dtlmod::connection_manager_handle")->get_unique<bool>();
}
std::shared_ptr<Stream> DTL::add_stream(std::string_view name)
{
// This has to be done in critical section to avoid concurrent creation. First actor to get the lock creates the
// Stream. Other actors will retrieve it from the map.
std::unique_lock lock(*mutex_);
std::string name_str(name);
if (streams_.find(name_str) == streams_.end())
streams_.try_emplace(name_str, std::make_shared<Stream>(name_str, this));
return streams_[name_str];
}
std::optional<std::shared_ptr<Stream>> DTL::get_stream_by_name(std::string_view name) const
{
auto it = streams_.find(std::string(name));
if (it == streams_.end())
return std::nullopt;
return it->second;
}
} // namespace dtlmod