Add alias and option to support migration
The main goal of the change is to allow us to change the name of the
blobId while making sure the existing data is still valid during the
migration. Added an alias option which allow us to have two possible
valid blobId and will use the `baseBlobId` to commit the data even if
the current data match the alish ID. This allow us to change the blobId
of the binary store without loosing the data.
Also added an option to revert back to the alias blobId with the
`blobtool` to support backward compatibility when we need to downgrade
to a version without alias support. Created the base service that gets
trigger to start the blob id revert.
Tested:
Tested backward compatibility and working fine. The blob data are not
lost between versions.
Change-Id: I75bcbce3aff20c6ee8a491bef36fac260595d6f7
Signed-off-by: Willy Tu <wltu@google.com>
diff --git a/src/binarystore.cpp b/src/binarystore.cpp
index 7c1bc42..c69a653 100644
--- a/src/binarystore.cpp
+++ b/src/binarystore.cpp
@@ -29,10 +29,9 @@
using namespace phosphor::logging;
-std::unique_ptr<BinaryStoreInterface>
- BinaryStore::createFromConfig(const std::string& baseBlobId,
- std::unique_ptr<SysFile> file,
- std::optional<uint32_t> maxSize)
+std::unique_ptr<BinaryStoreInterface> BinaryStore::createFromConfig(
+ const std::string& baseBlobId, std::unique_ptr<SysFile> file,
+ std::optional<uint32_t> maxSize, std::optional<std::string> aliasBlobBaseId)
{
if (baseBlobId.empty() || !file)
{
@@ -44,7 +43,7 @@
auto store =
std::make_unique<BinaryStore>(baseBlobId, std::move(file), maxSize);
- if (!store->loadSerializedData())
+ if (!store->loadSerializedData(aliasBlobBaseId))
{
return nullptr;
}
@@ -73,7 +72,7 @@
return store;
}
-bool BinaryStore::loadSerializedData()
+bool BinaryStore::loadSerializedData(std::optional<std::string> aliasBlobBaseId)
{
/* Load blob from sysfile if we know it might not match what we have.
* Note it will overwrite existing unsaved data per design. */
@@ -128,6 +127,16 @@
return true;
}
+ std::string alias = aliasBlobBaseId.value_or("");
+ if (blob_.blob_base_id() == alias)
+ {
+ log<level::WARNING>("Alias blob id, rename blob id...",
+ entry("LOADED=%s", alias.c_str()),
+ entry("RENAMED=%s", baseBlobId_.c_str()));
+ std::string tmpBlobId = baseBlobId_;
+ baseBlobId_ = alias;
+ return setBaseBlobId(tmpBlobId);
+ }
if (blob_.blob_base_id() != baseBlobId_ && !readOnly_)
{
/* Uh oh, stale data loaded. Clean it and commit. */
@@ -154,6 +163,29 @@
return blob_.blob_base_id();
}
+bool BinaryStore::setBaseBlobId(const std::string& baseBlobId)
+{
+ if (baseBlobId_.empty())
+ {
+ baseBlobId_ = blob_.blob_base_id();
+ }
+
+ std::string oldBlobId = baseBlobId_;
+ size_t oldPrefixIndex = baseBlobId_.size();
+ baseBlobId_ = baseBlobId;
+ blob_.set_blob_base_id(baseBlobId_);
+ auto blobsPtr = blob_.mutable_blobs();
+ for (auto blob = blobsPtr->begin(); blob != blobsPtr->end(); blob++)
+ {
+ const std::string& blodId = blob->blob_id();
+ if (blodId.starts_with(oldBlobId))
+ {
+ blob->set_blob_id(baseBlobId_ + blodId.substr(oldPrefixIndex));
+ }
+ }
+ return this->commit();
+}
+
std::vector<std::string> BinaryStore::getBlobIds() const
{
std::vector<std::string> result;