Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't delete shared memory segments if we're about to use them #1892

Merged
merged 3 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/datastore/shared_memory_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class SharedMemory

SharedMemory() = delete;
SharedMemory(const SharedMemory &) = delete;
SharedMemory &operator=(const SharedMemory &) = delete;

template <typename IdentifierT>
SharedMemory(const boost::filesystem::path &lock_file,
Expand Down Expand Up @@ -187,11 +188,13 @@ class SharedMemory
class SharedMemory
{
SharedMemory(const SharedMemory &) = delete;
SharedMemory &operator=(const SharedMemory &) = delete;
// Remove shared memory on destruction
class shm_remove
{
private:
shm_remove(const shm_remove &) = delete;
shm_remove &operator=(const shm_remove &) = delete;
char *m_shmid;
bool m_initialized;

Expand Down Expand Up @@ -355,6 +358,7 @@ template <class LockFileT = OSRMLockFile> class SharedMemoryFactory_tmpl

SharedMemoryFactory_tmpl() = delete;
SharedMemoryFactory_tmpl(const SharedMemoryFactory_tmpl &) = delete;
SharedMemoryFactory_tmpl &operator=(const SharedMemoryFactory_tmpl &) = delete;
};

using SharedMemoryFactory = SharedMemoryFactory_tmpl<>;
Expand Down
94 changes: 61 additions & 33 deletions include/engine/datafacade/shared_datafacade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,14 @@ template <class EdgeDataT> class SharedDataFacade final : public BaseDataFacade<
public:
virtual ~SharedDataFacade() {}

boost::shared_mutex data_mutex;

SharedDataFacade()
{
if (!datastore::SharedMemory::RegionExists(CURRENT_REGIONS))
{
throw util::exception("No shared memory blocks found, have you forgotten to run osrm-datastore?");
}
data_timestamp_ptr = (SharedDataTimestamp *)datastore::SharedMemoryFactory::Get(
CURRENT_REGIONS, sizeof(SharedDataTimestamp), false, false)
->Ptr();
Expand All @@ -242,51 +248,73 @@ template <class EdgeDataT> class SharedDataFacade final : public BaseDataFacade<
CURRENT_DATA != data_timestamp_ptr->data ||
CURRENT_TIMESTAMP != data_timestamp_ptr->timestamp)
{
// release the previous shared memory segments
datastore::SharedMemory::Remove(CURRENT_LAYOUT);
datastore::SharedMemory::Remove(CURRENT_DATA);

CURRENT_LAYOUT = data_timestamp_ptr->layout;
CURRENT_DATA = data_timestamp_ptr->data;
CURRENT_TIMESTAMP = data_timestamp_ptr->timestamp;

m_layout_memory.reset(datastore::SharedMemoryFactory::Get(CURRENT_LAYOUT));
// Get exclusive lock
util::SimpleLogger().Write(logDEBUG) << "Updates available, getting exclusive lock";
boost::unique_lock<boost::shared_mutex> lock(data_mutex);

data_layout = (SharedDataLayout *)(m_layout_memory->Ptr());
if (CURRENT_LAYOUT != data_timestamp_ptr->layout ||
CURRENT_DATA != data_timestamp_ptr->data)
{
// release the previous shared memory segments
datastore::SharedMemory::Remove(CURRENT_LAYOUT);
datastore::SharedMemory::Remove(CURRENT_DATA);

m_large_memory.reset(datastore::SharedMemoryFactory::Get(CURRENT_DATA));
shared_memory = (char *)(m_large_memory->Ptr());
CURRENT_LAYOUT = data_timestamp_ptr->layout;
CURRENT_DATA = data_timestamp_ptr->data;
CURRENT_TIMESTAMP = 0; // Force trigger a reload

const char *file_index_ptr =
data_layout->GetBlockPtr<char>(shared_memory, SharedDataLayout::FILE_INDEX_PATH);
file_index_path = boost::filesystem::path(file_index_ptr);
if (!boost::filesystem::exists(file_index_path))
util::SimpleLogger().Write(logDEBUG) << "Current layout was different to new layout, swapping";
}
else
{
util::SimpleLogger().Write(logDEBUG) << "Leaf file name "
<< file_index_path.string();
throw util::exception("Could not load leaf index file. "
"Is any data loaded into shared memory?");
util::SimpleLogger().Write(logDEBUG) << "Current layout was same to new layout, not swapping";

}

LoadGraph();
LoadChecksum();
LoadNodeAndEdgeInformation();
LoadGeometries();
LoadTimestamp();
LoadViaNodeList();
LoadNames();
LoadCoreInformation();
if (CURRENT_TIMESTAMP != data_timestamp_ptr->timestamp)
{
CURRENT_TIMESTAMP = data_timestamp_ptr->timestamp;

data_layout->PrintInformation();
util::SimpleLogger().Write(logDEBUG) << "Performing data reload";
m_layout_memory.reset(datastore::SharedMemoryFactory::Get(CURRENT_LAYOUT));

util::SimpleLogger().Write() << "number of geometries: " << m_coordinate_list->size();
for (unsigned i = 0; i < m_coordinate_list->size(); ++i)
{
if (!GetCoordinateOfNode(i).IsValid())
data_layout = (SharedDataLayout *) (m_layout_memory->Ptr());

m_large_memory.reset(datastore::SharedMemoryFactory::Get(CURRENT_DATA));
shared_memory = (char *) (m_large_memory->Ptr());

const char *file_index_ptr =
data_layout->GetBlockPtr<char>(shared_memory, SharedDataLayout::FILE_INDEX_PATH);
file_index_path = boost::filesystem::path(file_index_ptr);
if (!boost::filesystem::exists(file_index_path)) {
util::SimpleLogger().Write(logDEBUG) << "Leaf file name "
<< file_index_path.string();
throw util::exception("Could not load leaf index file. "
"Is any data loaded into shared memory?");
}

LoadGraph();
LoadChecksum();
LoadNodeAndEdgeInformation();
LoadGeometries();
LoadTimestamp();
LoadViaNodeList();
LoadNames();
LoadCoreInformation();

data_layout->PrintInformation();

util::SimpleLogger().Write() << "number of geometries: " << m_coordinate_list->size();
for (unsigned i = 0; i < m_coordinate_list->size(); ++i)
{
util::SimpleLogger().Write() << "coordinate " << i << " not valid";
if (!GetCoordinateOfNode(i).IsValid())
{
util::SimpleLogger().Write() << "coordinate " << i << " not valid";
}
}
}
util::SimpleLogger().Write(logDEBUG) << "Releasing exclusive lock";
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/engine/osrm_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,18 @@ int OSRM::OSRM_impl::RunQuery(const RouteParameters &route_parameters,
return 400;
}

osrm::engine::plugins::BasePlugin::Status return_code;
increase_concurrent_query_count();
auto return_code = plugin_iterator->second->HandleRequest(route_parameters, json_result);
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);
}
decrease_concurrent_query_count();
return static_cast<int>(return_code);
}
Expand Down