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/include/binarystore.hpp b/include/binarystore.hpp
index 792de4a..aa83714 100644
--- a/include/binarystore.hpp
+++ b/include/binarystore.hpp
@@ -74,6 +74,7 @@
BinaryStore& operator=(BinaryStore&&) = default;
std::string getBaseBlobId() const override;
+ bool setBaseBlobId(const std::string& baseBlobId) override;
std::vector<std::string> getBlobIds() const override;
bool openOrCreateBlob(const std::string& blobId, uint16_t flags) override;
bool deleteBlob(const std::string& blobId) override;
@@ -91,10 +92,10 @@
* @returns unique_ptr to constructed BinaryStore. Caller should take
* ownership of the instance.
*/
- static std::unique_ptr<BinaryStoreInterface>
- createFromConfig(const std::string& baseBlobId,
- std::unique_ptr<SysFile> file,
- std::optional<uint32_t> maxSize = std::nullopt);
+ static std::unique_ptr<BinaryStoreInterface> createFromConfig(
+ const std::string& baseBlobId, std::unique_ptr<SysFile> file,
+ std::optional<uint32_t> maxSize = std::nullopt,
+ std::optional<std::string> aliasBlobBaseId = std::nullopt);
/**
* Helper factory method to create a BinaryStore instance
@@ -111,7 +112,8 @@
private:
/* Load the serialized data from sysfile if commit state is dirty.
* Returns False if encountered error when loading */
- bool loadSerializedData();
+ bool loadSerializedData(
+ std::optional<std::string> aliasBlobBaseId = std::nullopt);
std::string baseBlobId_;
binaryblobproto::BinaryBlobBase blob_;
diff --git a/include/binarystore_interface.hpp b/include/binarystore_interface.hpp
index ba6dca3..3760305 100644
--- a/include/binarystore_interface.hpp
+++ b/include/binarystore_interface.hpp
@@ -30,6 +30,13 @@
virtual std::string getBaseBlobId() const = 0;
/**
+ * Set the base blob id
+ * @param baseBlobId: The blob id to update to.
+ * @returns true if the binary store is updated with the new baseBlobId
+ */
+ virtual bool setBaseBlobId(const std::string& baseBlobId) = 0;
+
+ /**
* @returns List of all open blob IDs, plus the base.
*/
virtual std::vector<std::string> getBlobIds() const = 0;
diff --git a/include/binarystore_mock.hpp b/include/binarystore_mock.hpp
index 9360dd3..30442a0 100644
--- a/include/binarystore_mock.hpp
+++ b/include/binarystore_mock.hpp
@@ -45,6 +45,7 @@
}
MOCK_CONST_METHOD0(getBaseBlobId, std::string());
MOCK_CONST_METHOD0(getBlobIds, std::vector<std::string>());
+ MOCK_METHOD1(setBaseBlobId, bool(const std::string&));
MOCK_METHOD2(openOrCreateBlob, bool(const std::string&, uint16_t));
MOCK_METHOD1(deleteBlob, bool(const std::string&));
MOCK_METHOD2(read, std::vector<uint8_t>(uint32_t, uint32_t));
diff --git a/include/parse_config.hpp b/include/parse_config.hpp
index 766830c..4771e2e 100644
--- a/include/parse_config.hpp
+++ b/include/parse_config.hpp
@@ -13,10 +13,12 @@
struct BinaryBlobConfig
{
- std::string blobBaseId; // Required
- std::string sysFilePath; // Required
- std::optional<uint32_t> offsetBytes; // Optional
- std::optional<uint32_t> maxSizeBytes; // Optional
+ std::string blobBaseId; // Required
+ std::string sysFilePath; // Required
+ std::optional<uint32_t> offsetBytes; // Optional
+ std::optional<uint32_t> maxSizeBytes; // Optional
+ std::optional<std::string> aliasBlobBaseId; // Optional
+ bool migrateToAlias = false; // Optional
};
/**
@@ -38,6 +40,16 @@
{
j.at("maxSizeBytes").get_to(config.maxSizeBytes.emplace());
}
+
+ if (j.contains("aliasBlobBaseId"))
+ {
+ j.at("aliasBlobBaseId").get_to(config.aliasBlobBaseId.emplace());
+ }
+
+ if (j.contains("migrateToAlias"))
+ {
+ config.migrateToAlias = j.at("migrateToAlias");
+ }
}
} // namespace conf