libsnapshot: Add GetSnapshotMergeStatsInstance

This is preferred over SnapshotMergeStats::GetInstance because
the latter needs a concrete SnapshotManager, not the ISnapshotManager
interface.

SnapshotManagerStub::GetSnapshotMergeStatsInstance returns
a SnapshotMergeStatsStub instance.

Test: vts_libsnapshot_test
Bug: 148956645
Change-Id: Ife0ad6d3ce85333cbf395d07f74dedc9ca3fe675
This commit is contained in:
Yifan Hong 2020-04-15 13:47:34 -07:00
parent fedb270908
commit ee5032a436
5 changed files with 42 additions and 7 deletions

View file

@ -70,6 +70,8 @@ struct AutoDeleteCowImage;
struct AutoDeleteSnapshot;
struct AutoDeviceList;
struct PartitionCowCreator;
class ISnapshotMergeStats;
class SnapshotMergeStats;
class SnapshotStatus;
static constexpr const std::string_view kCowGroupName = "cow";
@ -235,6 +237,9 @@ class ISnapshotManager {
// b.reset() // unmounts
// a.reset() // does nothing
virtual std::unique_ptr<AutoDevice> EnsureMetadataMounted() = 0;
// Return the associated ISnapshotMergeStats instance. Never null.
virtual ISnapshotMergeStats* GetSnapshotMergeStatsInstance() = 0;
};
class SnapshotManager final : public ISnapshotManager {
@ -289,6 +294,7 @@ class SnapshotManager final : public ISnapshotManager {
const std::unique_ptr<AutoDevice>& metadata_device) override;
bool Dump(std::ostream& os) override;
std::unique_ptr<AutoDevice> EnsureMetadataMounted() override;
ISnapshotMergeStats* GetSnapshotMergeStatsInstance() override;
private:
FRIEND_TEST(SnapshotTest, CleanFirstStageMount);

View file

@ -23,14 +23,12 @@
namespace android {
namespace snapshot {
class SnapshotMergeStats {
class ISnapshotMergeStats {
public:
// Not thread safe.
static SnapshotMergeStats* GetInstance(SnapshotManager& manager);
virtual ~ISnapshotMergeStats() = default;
// Called when merge starts or resumes.
bool Start();
void set_state(android::snapshot::UpdateState state);
virtual bool Start() = 0;
virtual void set_state(android::snapshot::UpdateState state) = 0;
// Called when merge ends. Properly clean up permanent storage.
class Result {
@ -40,7 +38,19 @@ class SnapshotMergeStats {
// Time between successful Start() / Resume() to Finish().
virtual std::chrono::steady_clock::duration merge_time() const = 0;
};
std::unique_ptr<Result> Finish();
// Return nullptr if any failure.
virtual std::unique_ptr<Result> Finish() = 0;
};
class SnapshotMergeStats : public ISnapshotMergeStats {
public:
// Not thread safe.
static SnapshotMergeStats* GetInstance(SnapshotManager& manager);
// ISnapshotMergeStats overrides
bool Start() override;
void set_state(android::snapshot::UpdateState state) override;
std::unique_ptr<Result> Finish() override;
private:
bool ReadState();

View file

@ -46,6 +46,7 @@ class SnapshotManagerStub : public ISnapshotManager {
const std::unique_ptr<AutoDevice>& metadata_device) override;
bool Dump(std::ostream& os) override;
std::unique_ptr<AutoDevice> EnsureMetadataMounted() override;
ISnapshotMergeStats* GetSnapshotMergeStatsInstance() override;
};
} // namespace android::snapshot

View file

@ -2685,5 +2685,9 @@ bool SnapshotManager::UpdateForwardMergeIndicator(bool wipe) {
return true;
}
ISnapshotMergeStats* SnapshotManager::GetSnapshotMergeStatsInstance() {
return SnapshotMergeStats::GetInstance(*this);
}
} // namespace snapshot
} // namespace android

View file

@ -16,6 +16,8 @@
#include <android-base/logging.h>
#include <libsnapshot/snapshot_stats.h>
using android::fs_mgr::CreateLogicalPartitionParams;
using chromeos_update_engine::DeltaArchiveManifest;
@ -108,4 +110,16 @@ std::unique_ptr<AutoDevice> SnapshotManagerStub::EnsureMetadataMounted() {
return nullptr;
}
class SnapshotMergeStatsStub : public ISnapshotMergeStats {
bool Start() override { return false; }
void set_state(android::snapshot::UpdateState) override {}
std::unique_ptr<Result> Finish() override { return nullptr; }
};
ISnapshotMergeStats* SnapshotManagerStub::GetSnapshotMergeStatsInstance() {
static SnapshotMergeStatsStub snapshot_merge_stats;
LOG(ERROR) << __FUNCTION__ << " should never be called.";
return &snapshot_merge_stats;
}
} // namespace android::snapshot