Overpass API regression suite coverage (Nova Scotia) / overpass_api/dispatch/dispatcher_stub.cc

overpass_api/dispatch/dispatcher_stub.cc

75 of 388 instrumented lines run by the suite (19.3%) 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#include "dispatcher_stub.h"
20#include "../frontend/hash_request.h"
21#include "../frontend/user_interface.h"
22#include "../statements/statement_dump.h"
23#include "../../expat/escape_json.h"
25#include <errno.h>
26#include <fcntl.h>
27#include <sys/mman.h>
28#include <sys/resource.h>
29#include <sys/select.h>
30#include <sys/stat.h>
31#include <sys/time.h>
32#include <sys/types.h>
33#include <unistd.h>
35#include <cstdlib>
36#include <cstring>
37#include <ctime>
38#include <fstream>
39#include <sstream>
40#include <string>
41#include <vector>
44std::string de_escape(const std::string& input)
46697595 q std::string result;
47697595 q std::string::size_type pos = 0;
48697595 q while (pos < input.length())
490 {
500 if (input[pos] != '\\')
510 result += input[pos];
520 else
530 {
540 ++pos;
550 if (pos >= input.length())
560 break;
570 if (input[pos] == 'n')
580 result += '\n';
590 else if (input[pos] == 't')
600 result += '\t';
610 else
620 result += input[pos];
630 }
640 ++pos;
650 }
66697595 q return result;
70void set_limits(uint32 time, uint64 space)
72595595 q rlimit limit;
74595595 q int result = getrlimit(RLIMIT_CPU, &limit);
75595595 q if (result == 0 && time < limit.rlim_cur && time < limit.rlim_max)
76595595 q {
77595595 q limit.rlim_cur = time;
78595595 q limit.rlim_max = time;
79595595 q result = setrlimit(RLIMIT_CPU, &limit);
80595595 q }
82595595 q result = getrlimit(RLIMIT_AS, &limit);
83595595 q if (result == 0 && space < limit.rlim_cur && space < limit.rlim_max)
84595595 q {
85595595 q limit.rlim_cur = space;
86595595 q limit.rlim_max = space;
87595595 q result = setrlimit(RLIMIT_AS, &limit);
88595595 q }
92namespace
94 uint64_t decode_hex(const std::string& representation)
950 {
960 uint64_t result = 0;
970 std::string::size_type pos = 0;
990 while (pos < representation.size())
1000 {
1010 if (representation[pos] >= '0' && representation[pos] <= '9')
1020 result = (result<<4) | (representation[pos] - '0');
1030 else if (representation[pos] >= 'a' && representation[pos] <= 'f')
1040 result = (result<<4) | (representation[pos] - 'a' + 10);
1050 else if (representation[pos] >= 'A' && representation[pos] <= 'F')
1060 result = (result<<4) | (representation[pos] - 'A' + 10);
1070 ++pos;
1080 }
1090 return result;
1100 }
114bool hash_is_blacklisted(uint64_t hash, const std::string& db_dir)
1150{
1160 try
1170 {
1180 std::ifstream blacklist_f((db_dir + "anon_hash_blacklist").c_str());
1190 for (std::string buf; std::getline(blacklist_f, buf); )
1200 {
1210 if (hash == decode_hex(buf))
1220 return true;
1230 }
1240 }
1250 catch(...) {}
1270 return false;
1280}
131Dispatcher_Stub::Dispatcher_Stub
132 (std::string db_dir_, Error_Output* error_output_, const std::string& xml_raw,
133 int area_level, uint32 max_allowed_time, uint64 max_allowed_space, Parsed_Query& global_settings)
134595595 q : db_dir(db_dir_), error_output(error_output_),
135595595 q dispatcher_client(0), area_dispatcher_client(0),
136595595 q transaction(0), area_transaction(0), rman(0),
137595595 q full_hash(hash(sanitize_string(xml_raw, false))),
138595595 q anon_hash(hash(sanitize_string(xml_raw, true))), client_token(0)
140595595 q const uint32_t MAX_LOG_SIZE = 8192;
142595595 q if (max_allowed_time > 0)
143595595 q set_limits(2*max_allowed_time + 60, 2*max_allowed_space + 1024*1024*1024);
145595595 q if (db_dir == "")
1460 {
1470 client_token = probe_client_token();
1480 client_identifier = probe_client_identifier();
1490 if (client_identifier.empty())
1500 client_identifier = "::";
1510 dispatcher_client = new Dispatcher_Client(osm_base_settings().shared_name);
1520 Logger db_logger(dispatcher_client->get_db_dir());
1530 Logger client_logger(dispatcher_client->get_db_dir(), basic_settings().client_logfile_name);
1540 if (xml_raw.size() <= MAX_LOG_SIZE)
1550 client_logger.annotated_log("requesting " + escape_cstr(xml_raw));
1560 else
1570 client_logger.annotated_log("requesting " + escape_cstr(
1580 xml_raw.substr(0, MAX_LOG_SIZE/2) + " ... " + xml_raw.substr(xml_raw.size() - MAX_LOG_SIZE/2)));
1600 if (hash_is_blacklisted(anon_hash, dispatcher_client->get_db_dir()))
1610 {
1620 std::ostringstream out;
1630 out<<"hash_limited "<<std::hex<<anon_hash<<' '<<full_hash<<' '<<std::dec<<client_token<<' '<<client_identifier;
1640 client_logger.annotated_log(out.str());
1650 delete dispatcher_client;
1660 dispatcher_client = 0;
1670 throw File_Error(0, "-", "Dispatcher_Client::request_read_and_idx::timeout");
1680 }
1700 try
1710 {
1720 db_logger.annotated_log("request_read_and_idx() start");
1730 dispatcher_client->request_read_and_idx(max_allowed_time, max_allowed_space, client_token, full_hash);
1740 db_logger.annotated_log("request_read_and_idx() end");
1750 }
1760 catch (const File_Error& e)
1770 {
1780 std::ostringstream out;
1790 if (e.origin == "Dispatcher_Client::request_read_and_idx::rate_limited"
1800 || e.origin == "Dispatcher_Client::request_read_and_idx::timeout"
1810 || e.origin == "Dispatcher_Client::request_read_and_idx::duplicate_query")
1820 out<<e.origin.substr(41)<<' '
1830 <<std::hex<<anon_hash<<' '<<full_hash<<' '<<std::dec<<client_token<<' '<<client_identifier;
1840 else
1850 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number);
1860 client_logger.annotated_log(out.str());
1870 throw;
1880 }
1890 transaction = new Nonsynced_Transaction(Access_Mode::readonly, false, dispatcher_client->get_db_dir(), "");
1910 for (auto i : osm_base_settings().bin_idxs())
1920 transaction->data_index(i);
1930 for (auto i : osm_base_settings().map_idxs())
1940 transaction->random_index(i);
1950 for (auto i : meta_settings().bin_idxs())
1960 transaction->data_index(i);
197 // meta_settings().map_idxs() is always empty
1980 for (auto i : attic_settings().bin_idxs())
1990 transaction->data_index(i);
2000 for (auto i : attic_settings().map_idxs())
2010 transaction->random_index(i);
2030 {
2040 std::ifstream version((dispatcher_client->get_db_dir() + "osm_base_version").c_str());
2050 getline(version, timestamp);
2060 timestamp = de_escape(timestamp);
2070 }
2080 try
2090 {
2100 db_logger.annotated_log("read_idx_finished() start");
2110 dispatcher_client->read_idx_finished();
2120 db_logger.annotated_log("read_idx_finished() end");
2130 }
2140 catch (const File_Error& e)
2150 {
2160 std::ostringstream out;
2170 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number);
2180 db_logger.annotated_log(out.str());
2190 throw;
2200 }
2220 if (area_level > 0)
2230 {
2240 area_dispatcher_client = new Dispatcher_Client(area_settings().shared_name);
2250 Logger logger(area_dispatcher_client->get_db_dir());
2270 if (area_level == 1)
2280 {
2290 try
2300 {
2310 db_logger.annotated_log("request_read_and_idx() area start");
2320 area_dispatcher_client->request_read_and_idx(max_allowed_time, max_allowed_space, client_token, full_hash);
2330 db_logger.annotated_log("request_read_and_idx() area end");
2340 }
2350 catch (const File_Error& e)
2360 {
2370 std::ostringstream out;
2380 if (e.origin == "Dispatcher_Client::request_read_and_idx::rate_limited"
2390 || e.origin == "Dispatcher_Client::request_read_and_idx::timeout"
2400 || e.origin == "Dispatcher_Client::request_read_and_idx::duplicate_query")
2410 out<<"Area::"<<e.origin.substr(41)<<' '
2420 <<std::hex<<anon_hash<<' '<<full_hash<<' '<<std::dec<<client_token<<' '<<client_identifier;
2430 else
2440 out<<"Area::"<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number);
2450 client_logger.annotated_log(out.str());
2460 throw;
2470 }
2480 area_transaction = new Nonsynced_Transaction(
2490 Access_Mode::readonly, false, area_dispatcher_client->get_db_dir(), "");
2500 {
2510 std::ifstream version((area_dispatcher_client->get_db_dir() +
2520 "area_version").c_str());
2530 getline(version, area_timestamp);
2540 area_timestamp = de_escape(area_timestamp);
2550 }
2560 }
2570 else if (area_level == 2)
2580 {
2590 try
2600 {
2610 db_logger.annotated_log("write_start() area start");
2620 area_dispatcher_client->write_start();
2630 db_logger.annotated_log("write_start() area end");
2640 }
2650 catch (const File_Error& e)
2660 {
2670 std::ostringstream out;
2680 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number);
2690 db_logger.annotated_log(out.str());
2700 throw;
2710 }
2720 area_transaction = new Nonsynced_Transaction(
2730 Access_Mode::writeable, true, area_dispatcher_client->get_db_dir(), "");
2740 {
2750 std::ofstream area_version((area_dispatcher_client->get_db_dir()
2760 + "area_version.shadow").c_str());
2770 area_version<<timestamp<<'\n';
2780 area_timestamp = de_escape(timestamp);
2790 }
2800 }
2820 area_transaction->data_index(area_settings().AREAS);
2830 area_transaction->data_index(area_settings().AREA_BLOCKS);
2840 area_transaction->data_index(area_settings().AREA_TAGS_LOCAL);
2850 area_transaction->data_index(area_settings().AREA_TAGS_GLOBAL);
2870 if (area_level == 1)
2880 {
2890 try
2900 {
2910 db_logger.annotated_log("read_idx_finished() area start");
2920 area_dispatcher_client->read_idx_finished();
2930 db_logger.annotated_log("read_idx_finished() area end");
2940 }
2950 catch (const File_Error& e)
2960 {
2970 std::ostringstream out;
2980 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number);
2990 db_logger.annotated_log(out.str());
3000 throw;
3010 }
3020 }
3040 rman = new Resource_Manager(*transaction, global_settings, area_level == 2 ? error_output : 0,
3050 *area_transaction, this, area_level == 2 ? new Area_Updater(*area_transaction) : 0);
3060 }
3070 else
3080 rman = new Resource_Manager(*transaction, &global_settings, this, error_output);
3090 }
310595595 q else
312595595 q if (file_present(db_dir + osm_base_settings().shared_name))
3130 throw Context_Error("File " + db_dir + osm_base_settings().shared_name + " present, "
3140 "which indicates a running dispatcher. Delete file if no dispatcher is running.");
316595595 q transaction = new Nonsynced_Transaction(Access_Mode::readonly, false, db_dir, "");
317595595 q if (area_level > 0)
319102102 q area_transaction = new Nonsynced_Transaction(
320102102 q area_level == 2 ? Access_Mode::writeable : Access_Mode::readonly, false, db_dir, "");
321102102 q rman = new Resource_Manager(*transaction, global_settings, area_level == 2 ? error_output : 0,
322102102 q *area_transaction, this, area_level == 2 ? new Area_Updater(*area_transaction) : 0);
324493493 q else
325493493 q rman = new Resource_Manager(*transaction, &global_settings, this, error_output);
328595595 q std::ifstream version((db_dir + "osm_base_version").c_str());
329595595 q getline(version, timestamp);
330595595 q timestamp = de_escape(timestamp);
332595595 q if (area_level == 1)
334102102 q std::ifstream version((db_dir + "area_version").c_str());
335102102 q getline(version, area_timestamp);
336102102 q area_timestamp = de_escape(timestamp);
338493493 q else if (area_level == 2)
3390 {
3400 std::ofstream area_version((db_dir + "area_version").c_str());
3410 area_version<<timestamp<<'\n';
3420 area_timestamp = de_escape(timestamp);
3430 }
348void Dispatcher_Stub::ping() const
3503328 q if (dispatcher_client)
3510 dispatcher_client->ping();
3523328 q if (area_dispatcher_client)
3530 area_dispatcher_client->ping();
357bool Dispatcher_Stub::all_meta_empty() const
3580{
3590 for (auto i : meta_settings().bin_idxs())
3600 {
3610 if (transaction->data_index(i) && !transaction->data_index(i)->empty())
3620 return false;
3630 }
3640 return true;
3650}
368std::string basename(const std::string& filename)
3690{
3700 uint start = filename.size();
3710 while (start > 0 && filename[--start] != '/')
3720 ;
3730 if (start < filename.size() && filename[start] == '/')
3740 ++start;
3750 uint end = start;
3760 while (end < filename.size() && filename[end] != '.')
3770 ++end;
3780 return filename.substr(start, end-start);
3790}
382bool Dispatcher_Stub::is_meta_file(const std::string& filename) const
3830{
3840 std::string trunk = basename(filename);
3850 for (auto i : meta_settings().bin_idxs())
3860 {
3870 if (i && i->get_file_name_trunk() == trunk)
3880 return true;
3890 }
3900 return false;
3910}
394bool Dispatcher_Stub::all_attic_empty() const
3950{
3960 for (auto i : attic_settings().bin_idxs())
3970 {
3980 if (transaction->data_index(i) && !transaction->data_index(i)->empty())
3990 return false;
4000 }
4010 return true;
4020}
405bool Dispatcher_Stub::is_attic_file(const std::string& filename) const
4060{
4070 std::string trunk = basename(filename);
4080 for (auto i : attic_settings().bin_idxs())
4090 {
4100 if (i && i->get_file_name_trunk() == trunk)
4110 return true;
4120 }
4130 return false;
4140}
417Dispatcher_Stub::~Dispatcher_Stub()
419595595 q bool areas_written = (rman->area_updater() != 0);
420595595 q std::vector< uint64 > cpu_runtime = rman ? rman->cpu_time() : std::vector< uint64 >();
421595595 q delete rman;
422595595 q if (transaction)
423595595 q delete transaction;
424595595 q if (area_transaction)
425102102 q delete area_transaction;
426595595 q if (dispatcher_client)
4270 {
4280 Logger db_logger(dispatcher_client->get_db_dir());
4290 Logger client_logger(dispatcher_client->get_db_dir(), basic_settings().client_logfile_name);
4300 try
4310 {
4320 std::ostringstream out;
4330 out<<"read_finished "<<std::hex<<anon_hash<<' '<<full_hash<<' '
4340 <<std::dec<<client_token<<' '<<client_identifier<<' '<<global_read_counter();
4350 for (std::vector< uint64 >::const_iterator it = cpu_runtime.begin(); it != cpu_runtime.end(); ++it)
4360 out<<' '<<*it;
4370 client_logger.annotated_log(out.str());
4380 db_logger.annotated_log("read_finished() start");
4390 dispatcher_client->read_finished();
4400 db_logger.annotated_log("read_finished() end");
4410 }
4420 catch (const File_Error& e)
4430 {
4440 std::ostringstream out;
4450 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number);
4460 db_logger.annotated_log(out.str());
4470 }
4480 delete dispatcher_client;
4490 }
450595595 q if (area_dispatcher_client)
4510 {
4520 if (areas_written)
4530 {
4540 Logger logger(area_dispatcher_client->get_db_dir());
4550 try
4560 {
4570 logger.annotated_log("write_commit() area start");
4580 area_dispatcher_client->write_commit();
4590 rename((area_dispatcher_client->get_db_dir() + "area_version.shadow").c_str(),
4600 (area_dispatcher_client->get_db_dir() + "area_version").c_str());
4610 logger.annotated_log("write_commit() area end");
4620 }
4630 catch (const File_Error& e)
4640 {
4650 std::ostringstream out;
4660 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number);
4670 logger.annotated_log(out.str());
4680 }
4690 }
4700 else
4710 {
4720 Logger logger(area_dispatcher_client->get_db_dir());
4730 try
4740 {
4750 logger.annotated_log("read_finished() area start");
4760 area_dispatcher_client->read_finished();
4770 logger.annotated_log("read_finished() area end");
4780 }
4790 catch (const File_Error& e)
4800 {
4810 std::ostringstream out;
4820 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number);
4830 logger.annotated_log(out.str());
4840 }
4850 }
4860 delete area_dispatcher_client;
4870 }