2016-01-07 13:19:55 -05:00
|
|
|
#include "engine/engine.hpp"
|
|
|
|
#include "engine/engine_config.hpp"
|
|
|
|
#include "engine/route_parameters.hpp"
|
2015-01-27 10:35:19 -05:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "engine/plugins/distance_table.hpp"
|
|
|
|
#include "engine/plugins/hello_world.hpp"
|
|
|
|
#include "engine/plugins/nearest.hpp"
|
|
|
|
#include "engine/plugins/timestamp.hpp"
|
|
|
|
#include "engine/plugins/trip.hpp"
|
|
|
|
#include "engine/plugins/viaroute.hpp"
|
|
|
|
#include "engine/plugins/match.hpp"
|
2016-01-07 13:19:55 -05:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "engine/datafacade/datafacade_base.hpp"
|
|
|
|
#include "engine/datafacade/internal_datafacade.hpp"
|
|
|
|
#include "engine/datafacade/shared_datafacade.hpp"
|
2016-01-07 13:19:55 -05:00
|
|
|
|
|
|
|
#include "storage/shared_barriers.hpp"
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "util/make_unique.hpp"
|
|
|
|
#include "util/routed_options.hpp"
|
|
|
|
#include "util/simple_logger.hpp"
|
2015-01-27 10:35:19 -05:00
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <boost/interprocess/sync/named_condition.hpp>
|
|
|
|
#include <boost/interprocess/sync/scoped_lock.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace engine
|
|
|
|
{
|
|
|
|
|
2016-01-07 13:19:55 -05:00
|
|
|
Engine::Engine(EngineConfig &config)
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
2016-01-07 13:19:55 -05:00
|
|
|
if (config.use_shared_memory)
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
2016-01-07 13:19:55 -05:00
|
|
|
barrier = util::make_unique<storage::SharedBarriers>();
|
2016-01-05 10:51:13 -05:00
|
|
|
query_data_facade = new datafacade::SharedDataFacade<contractor::QueryEdge::EdgeData>();
|
2015-01-27 10:35:19 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// populate base path
|
2016-01-07 13:19:55 -05:00
|
|
|
util::populate_base_path(config.server_paths);
|
2016-01-05 10:51:13 -05:00
|
|
|
query_data_facade = new datafacade::InternalDataFacade<contractor::QueryEdge::EdgeData>(
|
2016-01-07 13:19:55 -05:00
|
|
|
config.server_paths);
|
2015-01-27 10:35:19 -05:00
|
|
|
}
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
using DataFacade = datafacade::BaseDataFacade<contractor::QueryEdge::EdgeData>;
|
|
|
|
|
2015-01-27 10:35:19 -05:00
|
|
|
// The following plugins handle all requests.
|
2016-01-05 10:51:13 -05:00
|
|
|
RegisterPlugin(new plugins::DistanceTablePlugin<DataFacade>(
|
2016-01-07 13:19:55 -05:00
|
|
|
query_data_facade, config.max_locations_distance_table));
|
2016-01-05 10:51:13 -05:00
|
|
|
RegisterPlugin(new plugins::HelloWorldPlugin());
|
|
|
|
RegisterPlugin(new plugins::NearestPlugin<DataFacade>(query_data_facade));
|
|
|
|
RegisterPlugin(new plugins::MapMatchingPlugin<DataFacade>(
|
2016-01-07 13:19:55 -05:00
|
|
|
query_data_facade, config.max_locations_map_matching));
|
2016-01-05 10:51:13 -05:00
|
|
|
RegisterPlugin(new plugins::TimestampPlugin<DataFacade>(query_data_facade));
|
|
|
|
RegisterPlugin(new plugins::ViaRoutePlugin<DataFacade>(query_data_facade,
|
2016-01-07 13:19:55 -05:00
|
|
|
config.max_locations_viaroute));
|
2016-01-05 10:51:13 -05:00
|
|
|
RegisterPlugin(
|
2016-01-07 13:19:55 -05:00
|
|
|
new plugins::RoundTripPlugin<DataFacade>(query_data_facade, config.max_locations_trip));
|
2015-01-27 10:35:19 -05:00
|
|
|
}
|
|
|
|
|
2016-01-07 13:19:55 -05:00
|
|
|
void Engine::RegisterPlugin(plugins::BasePlugin *raw_plugin_ptr)
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
std::unique_ptr<plugins::BasePlugin> plugin_ptr(raw_plugin_ptr);
|
|
|
|
util::SimpleLogger().Write() << "loaded plugin: " << plugin_ptr->GetDescriptor();
|
2015-12-14 16:00:20 -05:00
|
|
|
plugin_map[plugin_ptr->GetDescriptor()] = std::move(plugin_ptr);
|
2015-01-27 10:35:19 -05:00
|
|
|
}
|
|
|
|
|
2016-01-07 13:19:55 -05:00
|
|
|
int Engine::RunQuery(const RouteParameters &route_parameters,
|
2016-01-05 10:51:13 -05:00
|
|
|
util::json::Object &json_result)
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
|
|
|
const auto &plugin_iterator = plugin_map.find(route_parameters.service);
|
|
|
|
|
|
|
|
if (plugin_map.end() == plugin_iterator)
|
|
|
|
{
|
2015-12-16 22:14:06 -05:00
|
|
|
json_result.values["status_message"] = "Service not found";
|
2015-01-27 10:35:19 -05:00
|
|
|
return 400;
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:57:36 -05:00
|
|
|
osrm::engine::plugins::BasePlugin::Status return_code;
|
2015-01-27 10:35:19 -05:00
|
|
|
increase_concurrent_query_count();
|
2016-01-15 03:57:36 -05:00
|
|
|
if (barrier) {
|
|
|
|
// Get a shared data lock so that other threads won't update
|
|
|
|
// things while the query is running
|
|
|
|
boost::shared_lock<boost::shared_mutex> data_lock{
|
|
|
|
(static_cast<datafacade::SharedDataFacade<contractor::QueryEdge::EdgeData> *>(
|
|
|
|
query_data_facade))->data_mutex};
|
|
|
|
return_code = plugin_iterator->second->HandleRequest(route_parameters, json_result);
|
|
|
|
} else {
|
|
|
|
return_code = plugin_iterator->second->HandleRequest(route_parameters, json_result);
|
|
|
|
}
|
2015-01-27 10:35:19 -05:00
|
|
|
decrease_concurrent_query_count();
|
2015-12-17 10:45:15 -05:00
|
|
|
return static_cast<int>(return_code);
|
2015-01-27 10:35:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrease number of concurrent queries
|
2016-01-07 13:19:55 -05:00
|
|
|
void Engine::decrease_concurrent_query_count()
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
|
|
|
if (!barrier)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// lock query
|
|
|
|
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> query_lock(
|
|
|
|
barrier->query_mutex);
|
|
|
|
|
|
|
|
// decrement query count
|
|
|
|
--(barrier->number_of_queries);
|
|
|
|
BOOST_ASSERT_MSG(0 <= barrier->number_of_queries, "invalid number of queries");
|
|
|
|
|
|
|
|
// notify all processes that were waiting for this condition
|
|
|
|
if (0 == barrier->number_of_queries)
|
|
|
|
{
|
|
|
|
barrier->no_running_queries_condition.notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// increase number of concurrent queries
|
2016-01-07 13:19:55 -05:00
|
|
|
void Engine::increase_concurrent_query_count()
|
2015-01-27 10:35:19 -05:00
|
|
|
{
|
|
|
|
if (!barrier)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lock update pending
|
|
|
|
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> pending_lock(
|
|
|
|
barrier->pending_update_mutex);
|
|
|
|
|
|
|
|
// lock query
|
|
|
|
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> query_lock(
|
|
|
|
barrier->query_mutex);
|
|
|
|
|
|
|
|
// unlock update pending
|
|
|
|
pending_lock.unlock();
|
|
|
|
|
|
|
|
// increment query count
|
|
|
|
++(barrier->number_of_queries);
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
(static_cast<datafacade::SharedDataFacade<contractor::QueryEdge::EdgeData> *>(
|
|
|
|
query_data_facade))
|
2015-01-27 10:35:19 -05:00
|
|
|
->CheckAndReloadFacade();
|
|
|
|
}
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
}
|