Overpass API regression suite coverage (Saxony) / template_db/dispatcher.h

template_db/dispatcher.h

0 of 24 instrumented lines run by the suite (0.0%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#ifndef DE__OSM3S___TEMPLATE_DB__DISPATCHER_H
20#define DE__OSM3S___TEMPLATE_DB__DISPATCHER_H
22#include "file_tools.h"
23#include "types.h"
24#include "transaction_insulator.h"
26#include <map>
27#include <set>
28#include <vector>
31/** Is called to log all operations of the dispatcher */
32struct Dispatcher_Logger
34 typedef uint pid_t;
36 virtual void terminate_triggered(int32 countdown, pid_t writing_process) = 0;
38 virtual void write_start(pid_t pid, const std::vector< ::pid_t >& registered) = 0;
39 virtual void write_rollback(pid_t pid) = 0;
40 virtual void write_pending(pid_t pid, const std::set< pid_t >& reading) = 0;
41 virtual void write_commit(pid_t pid) = 0;
42 virtual void migrate_start(pid_t pid, const std::vector< ::pid_t >& registered) = 0;
43 virtual void migrate_rollback(pid_t pid) = 0;
44 virtual void migrate_commit(pid_t pid) = 0;
46 virtual void request_read_and_idx(pid_t pid, uint32 max_allowed_time, uint64 max_allowed_space)
47 = 0;
48 virtual void read_idx_finished(pid_t pid) = 0;
49 virtual void prolongate(pid_t pid) = 0;
50 virtual void idle_counter(uint32 idle_count) = 0;
51 virtual void read_finished(pid_t pid) = 0;
52 virtual void query_my_status(pid_t pid) = 0;
53 virtual void read_aborted(pid_t pid) = 0;
54 virtual void hangup(pid_t pid) = 0;
55 virtual void purge(pid_t pid) = 0;
56 virtual void arguments_mismatch(pid_t pid, uint num_expected, uint num_provided) = 0;
57};
60struct Reader_Entry
62 Reader_Entry(uint32 client_pid_, uint64 max_space_, uint32 max_time_, uint32 client_token_, uint32 start_time_)
63 : client_pid(client_pid_), max_space(max_space_), max_time(max_time_), start_time(start_time_),
640 client_token(client_token_) {}
66 pid_t client_pid;
67 uint64 max_space;
68 uint32 max_time;
69 uint32 start_time;
70 uint32 client_token;
71};
74struct Quota_Entry
76 Quota_Entry(uint32 client_token_, uint32 expiration_time_)
770 : client_token(client_token_), expiration_time(expiration_time_) {}
79 uint32 client_token;
80 uint32 expiration_time;
81};
84struct Pending_Client
860 Pending_Client(pid_t pid_, uint32 first_seen_) : pid(pid_), first_seen(first_seen_) {}
88 pid_t pid;
89 uint32 first_seen;
90};
93class Global_Resource_Planner
95public:
96 Global_Resource_Planner(
97 uint32 global_available_time_, uint64 global_available_space_,
98 uint32 rate_limit_, bool allow_duplicate_queries_, bool exempt_client_zero_)
99 : global_used_time(0), global_available_time(global_available_time_),
100 global_used_space(0), global_available_space(global_available_space_),
101 rate_limit(rate_limit_), allow_duplicate_queries(allow_duplicate_queries_),
102 exempt_client_zero(exempt_client_zero_),
103 recent_average_used_time(15), recent_average_used_space(15),
104 last_update_time(0), last_used_time(0), last_used_space(0), last_counted(0),
1050 average_used_time(0), average_used_space(0) {}
107 // Returns true if the process is acceptable in terms of server load and quotas
108 // In this case it is registered as running
109 int probe(pid_t pid, uint32 client_token, uint32 time_units, uint64 max_space, uint32 socket_ratio);
111 // Unregisters the process
112 void remove(pid_t pid);
114 // Unregister all processes that don't have a connection anymore
115 void purge(Connection_Per_Pid_Map& connection_per_pid);
1170 void set_total_available_time(uint32 global_available_time_) { global_available_time = global_available_time_; }
1180 void set_total_available_space(uint64 global_available_space_) { global_available_space = global_available_space_; }
1190 void set_rate_limit(uint rate_limit_) { rate_limit = rate_limit_; }
1200 void set_allow_duplicate_queries(bool allow_duplicate_queries_) { allow_duplicate_queries = allow_duplicate_queries_; }
1210 void set_exempt_client_zero(bool exempt_client_zero_) { exempt_client_zero = exempt_client_zero_; }
1230 const std::vector< Reader_Entry >& get_active() const { return active; }
124 bool is_active(pid_t client_pid) const;
1250 const std::vector< Quota_Entry >& get_afterwards() const { return afterwards; }
1260 uint32 get_total_claimed_time() const { return global_used_time; }
1270 uint32 get_total_available_time() const { return global_available_time; }
1280 uint64 get_total_claimed_space() const { return global_used_space; }
1290 uint64 get_total_available_space() const { return global_available_space; }
1300 uint32 get_rate_limit() const { return rate_limit; }
1310 bool get_allow_duplicate_queries() const { return allow_duplicate_queries; }
1320 uint32 get_average_claimed_time() const { return average_used_time; }
1330 uint64 get_average_claimed_space() const { return average_used_space; }
135private:
136 std::map< uint32, std::vector< Pending_Client > > pending;
137 std::vector< Reader_Entry > active;
138 std::vector< Quota_Entry > afterwards;
139 uint32 global_used_time;
140 uint32 global_available_time;
141 uint64 global_used_space;
142 uint64 global_available_space;
143 uint32 rate_limit;
144 bool allow_duplicate_queries;
145 bool exempt_client_zero;
147 std::vector< uint32 > recent_average_used_time;
148 std::vector< uint64 > recent_average_used_space;
149 uint32 last_update_time;
150 uint64 last_used_time;
151 uint64 last_used_space;
152 uint32 last_counted;
153 uint32 average_used_time;
154 uint64 average_used_space;
156 void remove_entry(std::vector< Reader_Entry >::iterator& it);
157};
160// Cares for the communication between server and client
161class Dispatcher_Socket
163public:
164 Dispatcher_Socket(
165 const std::string& dispatcher_share_name, const std::string& db_dir_,
166 uint max_num_reading_processes_, uint max_num_socket_clients);
167 ~Dispatcher_Socket();
169 void look_for_a_new_connection(Connection_Per_Pid_Map& connection_per_pid);
1700 std::vector< int >::size_type num_started_connections() { return started_connections.size(); }
1720 uint get_open_socket_limit() const { return open_socket_limit; }
174private:
175 Unix_Socket socket;
176 std::string socket_name;
177 std::vector< int > started_connections;
178 uint open_socket_limit;
179};
182class Dispatcher
184public:
185 typedef uint pid_t;
187 static const int SHM_SIZE = 3*sizeof(uint32) + 2*sizeof(uint32);//20+12+2*(256+4);
188 static const int OFFSET_BACK = 20;
189 static const int OFFSET_DB_1 = OFFSET_BACK+12;
190 static const int OFFSET_DB_2 = OFFSET_DB_1+(256+4);
192 static const int32 TERMINATE_COUNTDOWN_START = 500;
194 static const uint32 TERMINATE = 0x100;
195 static const uint32 OUTPUT_STATUS = 0x200;
196 static const uint32 HANGUP = 0x300;
197 static const uint32 PURGE = 0x401;
198 static const uint32 SET_GLOBAL_LIMITS = 0x506;
200 static const uint32 QUERY_MY_STATUS = 0x1101;
201 static const uint32 REGISTER_PID = 0x1200;
202 static const uint32 SET_LIMITS = 0x1300;
203 static const uint32 PING = 0x1400;
204 static const uint32 UNREGISTER_PID = 0x1500;
205 static const uint32 QUERY_BY_TOKEN = 0x1601;
207 static const uint32 PROTOCOL_INVALID = 0x1f100;
208 static const uint32 RATE_LIMITED = 0x1f200;
209 static const uint32 DUPLICATE_QUERY = 0x1f300;
210 static const uint32 QUERY_REJECTED = 0x1f800;
212 static const uint32 WRITE_START = 0x10100;
213 static const uint32 WRITE_ROLLBACK = 0x10200;
214 static const uint32 WRITE_COMMIT = 0x10300;
215 static const uint32 MIGRATE_START = 0x11100;
216 static const uint32 MIGRATE_ROLLBACK = 0x11200;
217 static const uint32 MIGRATE_COMMIT = 0x11300;
218 static const uint32 REQUEST_READ_AND_IDX = 0x20106;
219 static const uint32 READ_IDX_FINISHED = 0x20200;
220 static const uint32 READ_FINISHED = 0x20300;
221 static const uint32 READ_ABORTED = 0x20400;
223 /** Opens a shared memory for dispatcher communication. Furthermore,
224 * detects whether idx or idy are valid, clears to idx if necessary,
225 * and loads them into the shared memory idx_share_name. */
226 Dispatcher(
227 const std::string& dispatcher_share_name_,
228 const std::string& index_share_name, const std::string& shadow_name_,
229 const std::string& db_dir_, const std::string& socket_dir,
230 uint max_num_reading_processes, uint max_num_socket_clients, uint purge_timeout,
231 uint64 total_available_space, uint64 total_available_time_units,
232 const std::vector< File_Properties* >& controlled_files,
233 Dispatcher_Logger* logger = 0);
235 ~Dispatcher();
237 /** Write operations: -------------------------------------------------- */
239 /** Allocates a write lock if possible. Returns without doing anything
240 otherwise. */
241 void write_start(pid_t pid);
243 /** Removes the mutex for the write process without changing any
244 index file. */
245 void write_rollback(pid_t pid);
247 /** Copies the shadow files onto the main index files. A lock prevents
248 that incomplete copies after a crash may leave the database in an
249 unstable state. Removes the mutex for the write process. */
250 void write_commit(pid_t pid);
252 /** Allocates a write lock if possible. Returns without doing anything
253 otherwise. */
254 void migrate_start(pid_t pid);
256 /** Removes the mutex for the write process and the migrated data and index files
257 without changing any mainline index file. */
258 void migrate_rollback(pid_t pid);
260 /** Replaces the data and index files with the migrated files
261 in all cases where those files exist. A lock prevents
262 that incomplete copies after a crash may leave the database in an
263 unstable state. Removes the mutex for the write process. */
264 void migrate_commit(pid_t pid);
266 /** Read operations: --------------------------------------------------- */
268 /** Request the index for a read operation and registers the reading process.
269 Reading the index files should be taking a quick copy, because if any process
270 is in this state, write_commits are blocked. */
271 void request_read_and_idx(
272 pid_t pid, uint32 max_allowed_time, uint64 max_allowed_space, uint32 client_token);
274 /** Changes the registered state from reading the index to reading the
275 database. Can be safely called multiple times for the same process. */
276 void read_idx_finished(pid_t pid);
278 /** Unregisters a reading process on its request. */
279 void read_finished(pid_t pid);
281 /** Unregisters a reading process for other reasons. */
282 void read_aborted(pid_t pid);
284 /** Unregisters a non-reading process. */
285 void hangup(pid_t pid);
287 /** Other operations: -------------------------------------------------- */
289 /** Waits for input for the given amount of time. If milliseconds if zero,
290 it remains in standby forever. */
291 void standby_loop(uint64 milliseconds);
293 /** Outputs the status of the processes registered with the dispatcher
294 into shadow_name.status. */
295 void output_status();
297 /** Set the limit of simultaneous queries from a single IP address. */
2980 void set_rate_limit(uint rate_limit) { global_resource_planner.set_rate_limit(rate_limit); }
300 void set_allow_duplicate_queries(bool allow_duplicate_queries)
3010 { global_resource_planner.set_allow_duplicate_queries(allow_duplicate_queries); }
303 void set_exempt_client_zero(bool exempt_client_zero)
3040 { global_resource_planner.set_exempt_client_zero(exempt_client_zero); }
306private:
307 Dispatcher_Socket socket;
308 Connection_Per_Pid_Map connection_per_pid;
309 Transaction_Insulator transaction_insulator;
310 std::set< pid_t > processes_reading_idx;
311 pid_t writing_process;
312 std::string shadow_name;
313 std::string dispatcher_share_name;
314 int dispatcher_shm_fd;
315 volatile uint8* dispatcher_shm_ptr;
316 Dispatcher_Logger* logger;
317 std::set< pid_t > disconnected;
318 bool pending_commit;
319 int32 terminate_countdown;
320 uint32 requests_started_counter;
321 uint32 requests_finished_counter;
322 uint32 requests_load_rejected;
323 uint32 requests_rate_limited;
324 uint32 requests_as_duplicate_rejected;
325 Global_Resource_Planner global_resource_planner;
327 bool get_lock_for_idx_change(pid_t pid);
328 uint64 total_claimed_space() const;
329 uint64 total_claimed_time_units() const;
330};
333#endif