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

[WIP] Feature/parallel download #2672

Draft
wants to merge 3 commits into
base: gz-sim8
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions include/gz/sim/ServerConfig.hh
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,20 @@ namespace gz
public: const std::chrono::time_point<std::chrono::system_clock> &
Timestamp() const;

/// \brief Set whether asset download should block the start of
/// simulation.
/// The default value is true.
/// \param[in] _set True to wait while assets download. False will
/// download assets in a background thread.
public: void SetWaitForAssets(bool _set);

/// \brief Get whether asset download should block the start of
/// simulation.
/// The default value is true.
/// \return True if simulation should wait while assets download. False
/// indicates assets should be downloaded in a separate thread.
public: bool WaitForAssets() const;

/// \brief Get the type of source
/// \return The source type.
public: SourceType Source() const;
Expand Down
1 change: 0 additions & 1 deletion src/SdfEntityCreator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ void SdfEntityCreator::CreateEntities(const sdf::World *_world,
const sdf::Model *model = _world->ModelByIndex(modelIndex);
if (levelEntityNames.empty() ||
levelEntityNames.find(model->Name()) != levelEntityNames.end())

{
Entity modelEntity = this->CreateEntities(model);

Expand Down
59 changes: 39 additions & 20 deletions src/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ Server::Server(const ServerConfig &_config)
config.SetCacheLocation(_config.ResourceCache());
this->dataPtr->fuelClient = std::make_unique<fuel_tools::FuelClient>(config);

// Turn off downloads so that we can do an initial parsing of the SDF
// file. This will let us get the world names, which in turn allows the
// GUI to not-block while simulation assets download.
this->dataPtr->enableDownload = false;

// Configure SDF to fetch assets from Gazebo Fuel.
sdf::setFindCallback(std::bind(&ServerPrivate::FetchResource,
this->dataPtr.get(), std::placeholders::_1));
Expand All @@ -77,37 +82,51 @@ Server::Server(const ServerConfig &_config)

addResourcePaths();

// Loads the SDF root object based on values in a ServerConfig object.
sdf::Errors errors = this->dataPtr->LoadSdfRootHelper(_config);
sdf::Root sdfRoot;

if (!errors.empty())
// Loads the SDF root object based on values in a ServerConfig object.
// Ignore the sdf::Errors returned by this function. The errors will be
// displayed later in the downloadThread.
ServerConfig cfg = _config;
cfg.SetBehaviorOnSdfErrors(
ServerConfig::SdfErrorBehavior::CONTINUE_LOADING);
sdf::Errors errors = this->dataPtr->LoadSdfRootHelper(
cfg, sdfRoot);

// Remove all the models, lights, and actors from the primary sdfRoot object
// so that they can be downloaded and added to simulation in the background.
for (uint64_t i = 0; i < sdfRoot.WorldCount(); ++i)
{
for (auto &err : errors)
gzerr << err << "\n";
if (_config.BehaviorOnSdfErrors() ==
ServerConfig::SdfErrorBehavior::EXIT_IMMEDIATELY)
{
return;
}
sdfRoot.WorldByIndex(i)->ClearModels();
sdfRoot.WorldByIndex(i)->ClearActors();
sdfRoot.WorldByIndex(i)->ClearLights();
}

// Add record plugin
if (_config.UseLogRecord())
{
this->dataPtr->AddRecordPlugin(_config);
}
// This will create the simulation runners.
this->dataPtr->CreateSimulationRunners(sdfRoot);

// Storing the sdf root. The ServerPrivate.hh header file mentions
// that other classes may keep pointers to child nodes of the root,
// so we need to keep this object around.
// However, everything seems to work fine without storing this.
// \todo(nkoenig): Look into removing the sdfRoot member variable.
this->dataPtr->sdfRoot = sdfRoot;

// Establish publishers and subscribers. Setup transport before
// downloading simulation assets so that the GUI is not blocked during
// download.
this->dataPtr->SetupTransport();

this->dataPtr->CreateEntities();
// Download the simulation assets. This function will block if
// _config.WaitForAssets() is true;
this->dataPtr->DownloadAssets(_config);

// Set the desired update period, this will override the desired RTF given in
// the world file which was parsed by CreateEntities.
// the world file which was parsed by LoadSdfRootHelper.
if (_config.UpdatePeriod())
{
this->SetUpdatePeriod(_config.UpdatePeriod().value());
}

// Establish publishers and subscribers.
this->dataPtr->SetupTransport();
}

/////////////////////////////////////////////////
Expand Down
19 changes: 18 additions & 1 deletion src/ServerConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,10 @@ class gz::sim::ServerConfigPrivate
seed(_cfg->seed),
logRecordTopics(_cfg->logRecordTopics),
isHeadlessRendering(_cfg->isHeadlessRendering),
sdfRoot(_cfg->sdfRoot),
source(_cfg->source),
behaviorOnSdfErrors(_cfg->behaviorOnSdfErrors){ }
behaviorOnSdfErrors(_cfg->behaviorOnSdfErrors),
waitForAssets(_cfg->waitForAssets) { }

// \brief The SDF file that the server should load
public: std::string sdfFile = "";
Expand Down Expand Up @@ -297,6 +299,9 @@ class gz::sim::ServerConfigPrivate
/// \brief Server loading behavior in presence of SDF errors.
public: ServerConfig::SdfErrorBehavior behaviorOnSdfErrors{
ServerConfig::SdfErrorBehavior::EXIT_IMMEDIATELY};

/// \brief True to block while simulation assets download.
public: bool waitForAssets = true;
};

//////////////////////////////////////////////////
Expand Down Expand Up @@ -737,6 +742,18 @@ ServerConfig::Timestamp() const
return this->dataPtr->timestamp;
}

/////////////////////////////////////////////////
void ServerConfig::SetWaitForAssets(bool _set)
{
this->dataPtr->waitForAssets = _set;
}

/////////////////////////////////////////////////
bool ServerConfig::WaitForAssets() const
{
return this->dataPtr->waitForAssets;
}

/////////////////////////////////////////////////
void ServerConfig::AddLogRecordTopic(const std::string &_topic)
{
Expand Down
Loading
Loading