Skip to content

Commit

Permalink
Merge pull request #3125 from canonical/record-snapshot-creation-time
Browse files Browse the repository at this point in the history
[snapshots] record snapshot creation time
  • Loading branch information
ricab committed Oct 30, 2023
2 parents 73a94e2 + 4b800d5 commit f4c8050
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/multipass/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <unordered_map>

class QJsonObject;
class QDateTime;

namespace multipass
{
Expand All @@ -40,6 +41,7 @@ class Snapshot : private DisabledCopyMove
virtual std::string get_name() const = 0;
virtual std::string get_comment() const = 0;
virtual std::string get_parent_name() const = 0;
virtual QDateTime get_creation_timestamp() const = 0;
virtual std::shared_ptr<const Snapshot> get_parent() const = 0;
virtual std::shared_ptr<Snapshot> get_parent() = 0;

Expand Down
5 changes: 4 additions & 1 deletion src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,10 @@ try // clang-format on
fundamentals->set_snapshot_name(snapshot->get_name());
fundamentals->set_parent(snapshot->get_parent_name());
fundamentals->set_comment(snapshot->get_comment());
// TODO@snapshots populate snapshot creation time once available

auto timestamp = fundamentals->mutable_creation_timestamp();
timestamp->set_seconds(snapshot->get_creation_timestamp().toSecsSinceEpoch());
timestamp->set_nanos(snapshot->get_creation_timestamp().time().msec() * 1'000'000);
};

if (const auto& it = instance_snapshots_map.find(name);
Expand Down
23 changes: 18 additions & 5 deletions src/platform/backends/shared/base_snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ std::shared_ptr<mp::Snapshot> find_parent(const QJsonObject& json, mp::VirtualMa
}
} // namespace

mp::BaseSnapshot::BaseSnapshot(const std::string& name, const std::string& comment, int num_cores, MemorySize mem_size,
mp::BaseSnapshot::BaseSnapshot(const std::string& name, const std::string& comment, // NOLINT(modernize-pass-by-value)
const QDateTime& creation_timestamp, int num_cores, MemorySize mem_size,
MemorySize disk_space, VirtualMachine::State state,
std::unordered_map<std::string, VMMount> mounts, QJsonObject metadata,
std::shared_ptr<Snapshot> parent)
: name{name},
comment{comment},
creation_timestamp{creation_timestamp},
num_cores{num_cores},
mem_size{mem_size},
disk_space{disk_space},
Expand All @@ -104,8 +106,16 @@ mp::BaseSnapshot::BaseSnapshot(const std::string& name, const std::string& comme

mp::BaseSnapshot::BaseSnapshot(const std::string& name, const std::string& comment, const VMSpecs& specs,
std::shared_ptr<Snapshot> parent)
: BaseSnapshot{name, comment, specs.num_cores, specs.mem_size, specs.disk_space,
specs.state, specs.mounts, specs.metadata, std::move(parent)}
: BaseSnapshot{name,
comment,
QDateTime::currentDateTimeUtc(),
specs.num_cores,
specs.mem_size,
specs.disk_space,
specs.state,
specs.mounts,
specs.metadata,
std::move(parent)}
{
}

Expand All @@ -115,8 +125,10 @@ mp::BaseSnapshot::BaseSnapshot(const QJsonObject& json, VirtualMachine& vm)
}

mp::BaseSnapshot::BaseSnapshot(InnerJsonTag, const QJsonObject& json, VirtualMachine& vm)
: BaseSnapshot{json["name"].toString().toStdString(), // name
json["comment"].toString().toStdString(), // comment
: BaseSnapshot{json["name"].toString().toStdString(), // name
json["comment"].toString().toStdString(), // comment
QDateTime::fromString(json["creation_timestamp"].toString(),
Qt::ISODateWithMs), // creation_timestamp
json["num_cores"].toInt(), // num_cores
MemorySize{json["mem_size"].toString().toStdString()}, // mem_size
MemorySize{json["disk_space"].toString().toStdString()}, // disk_space
Expand All @@ -134,6 +146,7 @@ QJsonObject multipass::BaseSnapshot::serialize() const

snapshot.insert("name", QString::fromStdString(name));
snapshot.insert("comment", QString::fromStdString(comment));
snapshot.insert("creation_timestamp", creation_timestamp.toString(Qt::ISODateWithMs));
snapshot.insert("num_cores", num_cores);
snapshot.insert("mem_size", QString::number(mem_size.in_bytes()));
snapshot.insert("disk_space", QString::number(disk_space.in_bytes()));
Expand Down
14 changes: 11 additions & 3 deletions src/platform/backends/shared/base_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BaseSnapshot : public Snapshot

std::string get_name() const override;
std::string get_comment() const override;
QDateTime get_creation_timestamp() const override;
std::string get_parent_name() const override;
std::shared_ptr<const Snapshot> get_parent() const override;
std::shared_ptr<Snapshot> get_parent() override;
Expand Down Expand Up @@ -73,15 +74,17 @@ class BaseSnapshot : public Snapshot
{
};
BaseSnapshot(InnerJsonTag, const QJsonObject& json, VirtualMachine& vm);
BaseSnapshot(const std::string& name, const std::string& comment, int num_cores, MemorySize mem_size,
MemorySize disk_space, VirtualMachine::State state, std::unordered_map<std::string, VMMount> mounts,
QJsonObject metadata, std::shared_ptr<Snapshot> parent);
BaseSnapshot(const std::string& name, const std::string& get_comment, const QDateTime& creation_timestamp,
int num_cores, MemorySize mem_size, MemorySize disk_space, VirtualMachine::State state,
std::unordered_map<std::string, VMMount> mounts, QJsonObject metadata,
std::shared_ptr<Snapshot> parent);

private:
std::string name;
std::string comment;

// This class is non-copyable and having these const simplifies thread safety
const QDateTime creation_timestamp; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
const int num_cores; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
const MemorySize mem_size; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
const MemorySize disk_space; // NOLINT(cppcoreguidelines-avoid-const-or-ref-data-members)
Expand All @@ -107,6 +110,11 @@ inline std::string multipass::BaseSnapshot::get_comment() const
return comment;
}

inline QDateTime multipass::BaseSnapshot::get_creation_timestamp() const
{
return creation_timestamp;
}

inline std::string multipass::BaseSnapshot::get_parent_name() const
{
std::unique_lock lock{mutex};
Expand Down
5 changes: 5 additions & 0 deletions tests/stub_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ struct StubSnapshot : public Snapshot
return {};
}

QDateTime get_creation_timestamp() const noexcept override
{
return QDateTime{};
}

std::string get_parent_name() const override
{
return {};
Expand Down

0 comments on commit f4c8050

Please sign in to comment.