Overpass API regression suite coverage (Nova Scotia) / template_db/dispatcher_client.cc

template_db/dispatcher_client.cc

5 of 340 instrumented lines run by the suite (1.5%) 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_client.h"
20#include "dispatcher.h"
22#include <fcntl.h>
23#include <signal.h>
24#include <sys/mman.h>
25#include <sys/socket.h>
26#include <sys/stat.h>
27#include <sys/types.h>
29#include <algorithm>
30#include <cstdlib>
31#include <cstring>
32#include <fstream>
33#include <iostream>
36Dispatcher_Client::Dispatcher_Client(const std::string& dispatcher_share_name_)
370 : dispatcher_share_name(dispatcher_share_name_), socket("")
380{
390 signal(SIGPIPE, SIG_IGN);
410 char* db_dir_env = getenv("OVERPASS_DB_DIR");
420 if (db_dir_env)
430 db_dir = db_dir_env;
450 if (db_dir.empty())
460 {
47 // open dispatcher_share
480 int dispatcher_shm_fd = shm_open
490 (dispatcher_share_name.c_str(), O_RDWR, S_666);
500 if (dispatcher_shm_fd < 0)
510 throw File_Error
520 (errno, dispatcher_share_name, "Dispatcher_Client::1");
530 struct stat stat_buf;
540 fstat(dispatcher_shm_fd, &stat_buf);
550 volatile uint8* dispatcher_shm_ptr = (uint8*)mmap(
560 0, stat_buf.st_size, PROT_READ, MAP_SHARED, dispatcher_shm_fd, 0);
580 if (dispatcher_shm_ptr)
590 {
60 // get db_dir and shadow_name
610 db_dir = std::string((const char *)(dispatcher_shm_ptr + 4*sizeof(uint32)),
620 *(uint32*)(dispatcher_shm_ptr + 3*sizeof(uint32)));
630 shadow_name = std::string((const char *)(dispatcher_shm_ptr + 5*sizeof(uint32)
640 + db_dir.size()), *(uint32*)(dispatcher_shm_ptr + db_dir.size() + 4*sizeof(uint32)));
660 munmap((void*)dispatcher_shm_ptr,
670 Dispatcher::SHM_SIZE + db_dir.size() + shadow_name.size());
680 }
690 close(dispatcher_shm_fd);
700 }
710 else
720 shadow_name = db_dir + (dispatcher_share_name.size() >= 8
730 && dispatcher_share_name.substr(dispatcher_share_name.size()-8) == "osm_base"
740 ? "/osm_base_shadow" : "/areas_shadow");
760 if (!db_dir.empty() && db_dir.back() != '/')
770 db_dir += '/';
79 // initialize the socket for the client
800 char* socket_dir_env = getenv("OVERPASS_SOCKET_DIR");
810 socket.open((socket_dir_env ? std::string(socket_dir_env) : db_dir) + dispatcher_share_name_);
820 std::string socket_name = db_dir + dispatcher_share_name_;
840 pid_t pid = getpid();
850 if (send(socket.descriptor(), &pid, sizeof(pid_t), 0) == -1)
860 throw File_Error(errno, dispatcher_share_name, "Dispatcher_Client::4");
870}
90bool file_present(const std::string& full_path)
92595595 q struct stat stat_buf;
93595595 q int result = stat(full_path.c_str(), &stat_buf);
94595595 q return result == 0;
98template< class TObject >
99void Dispatcher_Client::send_message(TObject message, const std::string& source_pos)
1000{
1010 if (send(socket.descriptor(), &message, sizeof(TObject), 0) == -1)
1020 throw File_Error(errno, dispatcher_share_name, source_pos);
1030}
106uint32 Dispatcher_Client::ack_arrived()
1070{
1080 uint32 answer = 0;
1090 int bytes_read = recv(socket.descriptor(), &answer, sizeof(uint32), 0);
1100 while (bytes_read == -1)
1110 {
1120 millisleep(50);
1130 bytes_read = recv(socket.descriptor(), &answer, sizeof(uint32), 0);
1140 }
1150 if (bytes_read == sizeof(uint32))
1160 return answer;
1180 return 0;
1190}
122void Dispatcher_Client::write_start()
1230{
1240 pid_t pid = getpid();
1260 send_message(Dispatcher::WRITE_START, "Dispatcher_Client::write_start::socket");
1280 while (true)
1290 {
1300 if (ack_arrived())
1310 {
1320 if (file_exists(shadow_name + ".lock"))
1330 {
1340 try
1350 {
1360 pid_t locked_pid = 0;
1370 std::ifstream lock((shadow_name + ".lock").c_str());
1380 lock>>locked_pid;
1390 if (locked_pid == pid)
1400 return;
1410 }
1420 catch (...) {}
1430 }
1440 send_message(Dispatcher::WRITE_START, "Dispatcher_Client::write_start::socket");
1450 }
1460 millisleep(500);
1470 }
1480}
151void Dispatcher_Client::migrate_start()
1520{
1530 pid_t pid = getpid();
1550 send_message(Dispatcher::MIGRATE_START, "Dispatcher_Client::migrate_start::socket");
1570 while (true)
1580 {
1590 if (ack_arrived())
1600 {
1610 if (file_exists(shadow_name + ".lock"))
1620 {
1630 try
1640 {
1650 pid_t locked_pid = 0;
1660 std::ifstream lock((shadow_name + ".lock").c_str());
1670 lock>>locked_pid;
1680 if (locked_pid == pid)
1690 return;
1700 }
1710 catch (...) {}
1720 }
1730 send_message(Dispatcher::MIGRATE_START, "Dispatcher_Client::migrate_start::socket");
1740 }
1750 millisleep(500);
1760 }
1770}
180void Dispatcher_Client::write_rollback()
1810{
1820 pid_t pid = getpid();
1840 send_message(Dispatcher::WRITE_ROLLBACK, "Dispatcher_Client::write_rollback::socket");
1860 while (true)
1870 {
1880 if (ack_arrived())
1890 {
1900 if (file_exists(shadow_name + ".lock"))
1910 {
1920 try
1930 {
1940 pid_t locked_pid = 0;
1950 std::ifstream lock((shadow_name + ".lock").c_str());
1960 lock>>locked_pid;
1970 if (locked_pid != pid)
1980 return;
1990 }
2000 catch (...) {}
2010 }
2020 else
2030 return;
2040 send_message(Dispatcher::WRITE_ROLLBACK, "Dispatcher_Client::write_rollback::socket");
2050 }
2070 millisleep(500);
2080 }
2090}
212void Dispatcher_Client::migrate_rollback()
2130{
2140 pid_t pid = getpid();
2160 send_message(Dispatcher::MIGRATE_ROLLBACK, "Dispatcher_Client::migrate_rollback::socket");
2180 while (true)
2190 {
2200 if (ack_arrived())
2210 {
2220 if (file_exists(shadow_name + ".lock"))
2230 {
2240 try
2250 {
2260 pid_t locked_pid = 0;
2270 std::ifstream lock((shadow_name + ".lock").c_str());
2280 lock>>locked_pid;
2290 if (locked_pid != pid)
2300 return;
2310 }
2320 catch (...) {}
2330 }
2340 else
2350 return;
2360 send_message(Dispatcher::MIGRATE_ROLLBACK, "Dispatcher_Client::migrate_rollback::socket");
2370 }
2390 millisleep(500);
2400 }
2410}
244void Dispatcher_Client::write_commit()
2450{
2460 pid_t pid = getpid();
2480 send_message(Dispatcher::WRITE_COMMIT, "Dispatcher_Client::write_commit::socket");
2490 millisleep(200);
2510 while (true)
2520 {
2530 if (ack_arrived())
2540 {
2550 if (file_exists(shadow_name + ".lock"))
2560 {
2570 try
2580 {
2590 pid_t locked_pid = 0;
2600 std::ifstream lock((shadow_name + ".lock").c_str());
2610 lock>>locked_pid;
2620 if (locked_pid != pid)
2630 return;
2640 }
2650 catch (...) {}
2660 }
2670 else
2680 return;
2690 }
2710 send_message(Dispatcher::WRITE_COMMIT, "Dispatcher_Client::write_commit::socket");
2720 millisleep(200);
2730 }
2740}
277void Dispatcher_Client::migrate_commit()
2780{
2790 pid_t pid = getpid();
2810 send_message(Dispatcher::MIGRATE_COMMIT, "Dispatcher_Client::migrate_commit::socket");
2820 millisleep(200);
2840 while (true)
2850 {
2860 if (ack_arrived())
2870 {
2880 if (file_exists(shadow_name + ".lock"))
2890 {
2900 try
2910 {
2920 pid_t locked_pid = 0;
2930 std::ifstream lock((shadow_name + ".lock").c_str());
2940 lock>>locked_pid;
2950 if (locked_pid != pid)
2960 return;
2970 }
2980 catch (...) {}
2990 }
3000 else
3010 return;
3020 }
3040 send_message(Dispatcher::MIGRATE_COMMIT, "Dispatcher_Client::migrate_commit::socket");
3050 millisleep(200);
3060 }
3070}
310void Dispatcher_Client::request_read_and_idx(
311 uint32 max_allowed_time, uint64 max_allowed_space, uint32 client_token, uint64 request_full_hash)
3120{
3130 uint counter = 0;
3140 uint32 ack = 0;
3150 while (ack == 0 && ++counter <= 100)
3160 {
3170 send_message(Dispatcher::REQUEST_READ_AND_IDX,
3180 "Dispatcher_Client::request_read_and_idx::socket::1");
3190 send_message(max_allowed_time, "Dispatcher_Client::request_read_and_idx::socket::2");
3200 send_message(max_allowed_space, "Dispatcher_Client::request_read_and_idx::socket::3");
3210 send_message(client_token, "Dispatcher_Client::request_read_and_idx::socket::4");
3220 send_message(request_full_hash, "Dispatcher_Client::request_read_and_idx::socket::5");
3240 ack = ack_arrived();
3250 if (ack == Dispatcher::REQUEST_READ_AND_IDX)
3260 return;
3280 millisleep(300);
3290 }
3300 if (ack == Dispatcher::RATE_LIMITED)
3310 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::request_read_and_idx::rate_limited");
3320 else if (ack == Dispatcher::QUERY_REJECTED || ack == 0)
3330 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::request_read_and_idx::timeout");
3340 else if (ack == Dispatcher::DUPLICATE_QUERY)
3350 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::request_read_and_idx::duplicate_query");
3360 else
3370 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::request_read_and_idx::protocol_error");
3380}
341void Dispatcher_Client::read_idx_finished()
3420{
3430 uint counter = 0;
3440 while (++counter <= 300)
3450 {
3460 send_message(Dispatcher::READ_IDX_FINISHED, "Dispatcher_Client::read_idx_finished::socket");
3480 if (ack_arrived())
3490 return;
3500 }
3510 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::read_idx_finished::timeout");
3520}
355void Dispatcher_Client::read_finished()
3560{
3570 uint counter = 0;
3580 while (++counter <= 300)
3590 {
3600 send_message(Dispatcher::READ_FINISHED, "Dispatcher_Client::read_finished::socket");
3620 if (ack_arrived())
3630 return;
3640 }
3650 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::read_finished::timeout");
3660}
369void Dispatcher_Client::purge(uint32 pid)
3700{
3710 while (true)
3720 {
3730 send_message(Dispatcher::PURGE, "Dispatcher_Client::purge::socket::1");
3740 send_message(pid, "Dispatcher_Client::purge::socket::2");
3760 if (ack_arrived())
3770 return;
3780 }
3790}
382pid_t Dispatcher_Client::query_by_token(uint32 token)
3830{
3840 send_message(Dispatcher::QUERY_BY_TOKEN, "Dispatcher_Client::query_by_token::socket::1");
3850 send_message(token, "Dispatcher_Client::query_by_token::socket::2");
3870 return ack_arrived();
3880}
391Client_Status Dispatcher_Client::query_my_status(uint32 token)
3920{
3930 send_message(Dispatcher::QUERY_MY_STATUS, "Dispatcher_Client::query_my_status::socket::1");
3940 send_message(token, "Dispatcher_Client::query_my_status::socket::2");
3960 Client_Status result;
3970 result.rate_limit = ack_arrived();
3990 while (true)
4000 {
4010 Running_Query query;
4020 query.status = ack_arrived();
4030 if (query.status == 0)
4040 break;
4050 query.pid = ack_arrived();
4060 query.max_time = ack_arrived();
4070 query.max_space = ((uint64)ack_arrived() <<32) | ack_arrived();
4080 query.start_time = ack_arrived();
4090 result.queries.push_back(query);
4100 }
4120 while (true)
4130 {
4140 uint32 slot_start = ack_arrived();
4150 if (slot_start == 0)
4160 break;
4170 result.slot_starts.push_back(slot_start);
4180 }
4200 std::sort(result.slot_starts.begin(), result.slot_starts.end());
4220 return result;
4230}
426void Dispatcher_Client::set_global_limits(
427 uint64 max_allowed_space, uint64 max_allowed_time_units, int32_t rate_limit, int32_t bit_limits)
4280{
4290 while (true)
4300 {
4310 send_message(Dispatcher::SET_GLOBAL_LIMITS, "Dispatcher_Client::set_global_limits::1");
4320 send_message(max_allowed_space, "Dispatcher_Client::set_global_limits::2");
4330 send_message(max_allowed_time_units, "Dispatcher_Client::set_global_limits::3");
4340 send_message(rate_limit, "Dispatcher_Client::set_global_limits::4");
4350 send_message(bit_limits, "Dispatcher_Client::set_global_limits::5");
4370 if (ack_arrived())
4380 return;
4390 }
4400}
443void Dispatcher_Client::ping()
4440{
445// Ping-Feature removed. The concept of unassured messages doesn't fit in the context of strict
446// two-directional communication.
447// send_message(Dispatcher::PING, "Dispatcher_Client::ping::socket");
4480}
451void Dispatcher_Client::terminate()
4520{
4530 while (true)
4540 {
4550 send_message(Dispatcher::TERMINATE, "Dispatcher_Client::terminate::socket");
4570 if (ack_arrived())
4580 return;
4590 }
4600}
463void Dispatcher_Client::output_status()
4640{
4650 while (true)
4660 {
4670 send_message(Dispatcher::OUTPUT_STATUS, "Dispatcher_Client::output_status::socket");
4690 if (ack_arrived())
4700 break;
4710 }
4730 std::ifstream status((shadow_name + ".status").c_str());
4740 std::string buffer;
4750 std::getline(status, buffer);
4760 while (status.good())
4770 {
4780 std::cout<<buffer<<'\n';
4790 std::getline(status, buffer);
4800 }
4810}