]> git.sesse.net Git - cubemap/blob - serverpool.cpp
Update performance claims in README.
[cubemap] / serverpool.cpp
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4
5 #include "client.h"
6 #include "log.h"
7 #include "server.h"
8 #include "serverpool.h"
9 #include "state.pb.h"
10 #include "udpstream.h"
11 #include "util.h"
12
13 struct sockaddr_in6;
14
15 using namespace std;
16
17 ServerPool::ServerPool(int size)
18         : servers(new Server[size]),
19           num_servers(size)
20 {
21 }
22
23 CubemapStateProto ServerPool::serialize()
24 {
25         CubemapStateProto state;
26
27         unordered_map<const string *, size_t> short_response_pool;
28
29         for (int i = 0; i < num_servers; ++i) {
30                 CubemapStateProto local_state = servers[i].serialize(&short_response_pool);
31
32                 // The stream state should be identical between the servers, so we only store it once,
33                 // save for the fds, which we keep around to distribute to the servers after re-exec.
34                 if (i == 0) {
35                         state.mutable_streams()->MergeFrom(local_state.streams());
36                 } else {
37                         assert(state.streams_size() == local_state.streams_size());
38                         for (int j = 0; j < local_state.streams_size(); ++j) {
39                                 assert(local_state.streams(j).data_fds_size() == 1);
40                                 state.mutable_streams(j)->add_data_fds(local_state.streams(j).data_fds(0));
41                         }
42                 }
43                 for (const ClientProto &client : local_state.clients()) {
44                         state.add_clients()->MergeFrom(client);
45                 }
46         }
47
48         for (size_t i = 0; i < short_response_pool.size(); ++i) {
49                 state.mutable_short_response_pool()->Add();
50         }
51         for (const auto &string_and_index : short_response_pool) {
52                 state.mutable_short_response_pool(string_and_index.second)->set_header_or_short_response(*string_and_index.first);
53         }
54
55         return state;
56 }
57
58 void ServerPool::add_client(int sock, Acceptor *acceptor)
59 {
60         servers[clients_added++ % num_servers].add_client_deferred(sock, acceptor);
61 }
62
63 void ServerPool::add_client_from_serialized(const ClientProto &client, const std::vector<std::shared_ptr<const std::string>> &short_responses)
64 {
65         servers[clients_added++ % num_servers].add_client_from_serialized(client, short_responses);
66 }
67
68 int ServerPool::lookup_stream_by_url(const string &url) const
69 {
70         assert(servers != nullptr);
71         return servers[0].lookup_stream_by_url(url);
72 }
73
74 int ServerPool::add_stream(const string &url,
75                            const string &hls_url,
76                            size_t backlog_size,
77                            size_t prebuffering_bytes,
78                            Stream::Encoding encoding,
79                            Stream::Encoding src_encoding,
80                            unsigned hls_frag_duration,
81                            size_t hls_backlog_margin,
82                            const string &allow_origin)
83 {
84         // Adding more HTTP streams after UDP streams would cause the UDP stream
85         // indices to move around, which is obviously not good.
86         assert(udp_streams.empty());
87
88         for (int i = 0; i < num_servers; ++i) {
89                 int stream_index = servers[i].add_stream(url, hls_url, backlog_size, prebuffering_bytes, encoding, src_encoding, hls_frag_duration, hls_backlog_margin, allow_origin);
90                 assert(stream_index == num_http_streams);
91         }
92         return num_http_streams++;
93 }
94
95 int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vector<int> &data_fds)
96 {
97         // Adding more HTTP streams after UDP streams would cause the UDP stream
98         // indices to move around, which is obviously not good.
99         assert(udp_streams.empty());
100
101         assert(!data_fds.empty());
102         string contents;
103         for (int i = 0; i < num_servers; ++i) {
104                 int data_fd;
105                 if (i < int(data_fds.size())) {
106                         // Reuse one of the existing file descriptors.
107                         data_fd = data_fds[i];
108                 } else {
109                         // Clone the first one.
110                         if (contents.empty()) {
111                                 if (!read_tempfile(data_fds[0], &contents)) {
112                                         exit(1);
113                                 }
114                         }
115                         data_fd = make_tempfile(contents);
116                 }
117
118                 int stream_index = servers[i].add_stream_from_serialized(stream, data_fd);
119                 assert(stream_index == num_http_streams);
120         }
121
122         // Close and delete any leftovers, if the number of servers was reduced.
123         for (size_t i = num_servers; i < data_fds.size(); ++i) {
124                 safe_close(data_fds[i]);  // Implicitly deletes the file.
125         }
126
127         return num_http_streams++;
128 }
129         
130 int ServerPool::add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index)
131 {
132         udp_streams.emplace_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index));
133         return num_http_streams + udp_streams.size() - 1;
134 }
135
136 void ServerPool::set_header(int stream_index, const string &http_header, const string &stream_header)
137 {
138         assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size()));
139
140         if (stream_index >= num_http_streams) {
141                 // UDP stream. TODO: Log which stream this is.
142                 if (!stream_header.empty()) {
143                         log(WARNING, "Trying to send stream format with headers to a UDP destination. This is unlikely to work well.");
144                 }
145
146                 // Ignore the HTTP header.
147                 return;
148         }
149
150         // HTTP stream.
151         for (int i = 0; i < num_servers; ++i) {
152                 servers[i].set_header(stream_index, http_header, stream_header);
153         }
154 }
155
156 void ServerPool::add_data(int stream_index, const char *data, size_t bytes, uint16_t metacube_flags, const RationalPTS &pts)
157 {
158         assert(stream_index >= 0 && stream_index < ssize_t(num_http_streams + udp_streams.size()));
159
160         if (stream_index >= num_http_streams) {
161                 // UDP stream.
162                 udp_streams[stream_index - num_http_streams]->send(data, bytes);
163                 return;
164         }
165
166         // HTTP stream.
167         for (int i = 0; i < num_servers; ++i) {
168                 servers[i].add_data_deferred(stream_index, data, bytes, metacube_flags, pts);
169         }
170 }
171
172 void ServerPool::add_gen204(const std::string &url, const std::string &allow_origin)
173 {
174         for (int i = 0; i < num_servers; ++i) {
175                 servers[i].add_gen204(url, allow_origin);
176         }
177 }
178
179 void ServerPool::create_tls_context_for_acceptor(const Acceptor *acceptor)
180 {
181         for (int i = 0; i < num_servers; ++i) {
182                 servers[i].create_tls_context_for_acceptor(acceptor);
183         }
184 }
185
186 void ServerPool::run()
187 {
188         for (int i = 0; i < num_servers; ++i) {
189                 servers[i].run();
190         }
191 }
192         
193 void ServerPool::stop()
194 {
195         for (int i = 0; i < num_servers; ++i) {
196                 servers[i].stop();
197         }
198 }
199         
200 vector<ClientStats> ServerPool::get_client_stats() const
201 {
202         vector<ClientStats> ret;
203         for (int i = 0; i < num_servers; ++i) {
204                 vector<ClientStats> stats = servers[i].get_client_stats();
205                 ret.insert(ret.end(), stats.begin(), stats.end());
206         }
207         return ret;
208 }
209         
210 void ServerPool::set_pacing_rate(int stream_index, uint32_t pacing_rate)
211 {
212         for (int i = 0; i < num_servers; ++i) {
213                 servers[i].set_pacing_rate(stream_index, pacing_rate);
214         }       
215 }
216
217 void ServerPool::set_backlog_size(int stream_index, size_t new_size)
218 {
219         for (int i = 0; i < num_servers; ++i) {
220                 servers[i].set_backlog_size(stream_index, new_size);
221         }       
222 }
223
224 void ServerPool::set_prebuffering_bytes(int stream_index, size_t new_amount)
225 {
226         for (int i = 0; i < num_servers; ++i) {
227                 servers[i].set_prebuffering_bytes(stream_index, new_amount);
228         }
229 }
230
231 void ServerPool::set_encoding(int stream_index, Stream::Encoding encoding)
232 {
233         for (int i = 0; i < num_servers; ++i) {
234                 servers[i].set_encoding(stream_index, encoding);
235         }       
236 }
237
238 void ServerPool::set_src_encoding(int stream_index, Stream::Encoding encoding)
239 {
240         for (int i = 0; i < num_servers; ++i) {
241                 servers[i].set_src_encoding(stream_index, encoding);
242         }
243 }
244
245 void ServerPool::set_hls_frag_duration(int stream_index, unsigned hls_frag_duration)
246 {
247         for (int i = 0; i < num_servers; ++i) {
248                 servers[i].set_hls_frag_duration(stream_index, hls_frag_duration);
249         }
250 }
251
252 void ServerPool::set_hls_backlog_margin(int stream_index, size_t hls_backlog_margin)
253 {
254         for (int i = 0; i < num_servers; ++i) {
255                 servers[i].set_hls_backlog_margin(stream_index, hls_backlog_margin);
256         }
257 }
258
259 void ServerPool::set_allow_origin(int stream_index, const std::string &allow_origin)
260 {
261         for (int i = 0; i < num_servers; ++i) {
262                 servers[i].set_allow_origin(stream_index, allow_origin);
263         }
264 }
265
266 void ServerPool::register_hls_url(int stream_index, const string &hls_url)
267 {
268         for (int i = 0; i < num_servers; ++i) {
269                 servers[i].register_hls_url(stream_index, hls_url);
270         }
271 }