Skip to content
Open
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
17 changes: 10 additions & 7 deletions src/cm/database/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ Error Database::AddInstance(const launcher::InstanceInfo& info)

FromAos(info, row);
*mSession << "INSERT INTO launcher_instances (itemID, subjectID, instance, type, manifestDigest, "
"nodeID, prevNodeID, runtimeID, uid, gid, timestamp, state, isUnitSubject) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
"nodeID, prevNodeID, runtimeID, uid, gid, timestamp, state, isUnitSubject, version) "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about ownerID and subjectType? Are they stored in db?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, should add to InstanceInfo

"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
bind(row), now;
} catch (const std::exception& e) {
return AOS_ERROR_WRAP(common::utils::ToAosError(e));
Expand All @@ -494,12 +494,12 @@ Error Database::UpdateInstance(const launcher::InstanceInfo& info)

statement << "UPDATE launcher_instances SET manifestDigest = ?, nodeID = ?, prevNodeID = "
"?, runtimeID = ?, uid = ?, gid = ?, timestamp = ?, state = ?, isUnitSubject = ? "
"WHERE itemID = ? AND subjectID = ? AND instance = ? AND type = ?;",
"WHERE itemID = ? AND subjectID = ? AND instance = ? AND type = ? AND version = ?;",
bind(info.mManifestDigest.CStr()), bind(info.mNodeID.CStr()), bind(info.mPrevNodeID.CStr()),
bind(info.mRuntimeID.CStr()), bind(info.mUID), bind(info.mGID), bind(info.mTimestamp.UnixNano()),
bind(info.mState.ToString().CStr()), bind(info.mIsUnitSubject), bind(info.mInstanceIdent.mItemID.CStr()),
bind(info.mInstanceIdent.mSubjectID.CStr()), bind(info.mInstanceIdent.mInstance),
bind(info.mInstanceIdent.mType.ToString().CStr());
bind(info.mInstanceIdent.mType.ToString().CStr()), bind(info.mVersion.CStr());

if (statement.execute() != 1) {
return ErrorEnum::eNotFound;
Expand All @@ -519,7 +519,7 @@ Error Database::GetInstance(const InstanceIdent& instanceID, launcher::InstanceI
std::vector<LauncherInstanceInfoRow> rows;

*mSession << "SELECT itemID, subjectID, instance, type, manifestDigest, nodeID, prevNodeID, "
"runtimeID, uid, gid, timestamp, state, isUnitSubject "
"runtimeID, uid, gid, timestamp, state, isUnitSubject, version "
"FROM launcher_instances WHERE itemID = ? AND subjectID = ? AND instance = ? AND type = ?;",
bind(instanceID.mItemID.CStr()), bind(instanceID.mSubjectID.CStr()), bind(instanceID.mInstance),
bind(instanceID.mType.ToString().CStr()), into(rows), now;
Expand All @@ -544,7 +544,7 @@ Error Database::GetActiveInstances(Array<launcher::InstanceInfo>& instances) con
std::vector<LauncherInstanceInfoRow> rows;

*mSession << "SELECT itemID, subjectID, instance, type, manifestDigest, nodeID, prevNodeID, "
"runtimeID, uid, gid, timestamp, state, isUnitSubject FROM launcher_instances;",
"runtimeID, uid, gid, timestamp, state, isUnitSubject, version FROM launcher_instances;",
into(rows), now;

auto instanceInfo = std::make_unique<launcher::InstanceInfo>();
Expand Down Expand Up @@ -781,7 +781,8 @@ void Database::CreateTables()
"timestamp INTEGER,"
"state TEXT,"
"isUnitSubject INTEGER,"
"PRIMARY KEY(itemID,subjectID,instance,type)"
"version TEXT,"
"PRIMARY KEY(itemID,subjectID,instance,type,version)"
");",
now;
}
Expand Down Expand Up @@ -889,6 +890,7 @@ void Database::FromAos(const launcher::InstanceInfo& src, LauncherInstanceInfoRo
dst.set<ToInt(LauncherInstanceInfoColumns::eTimestamp)>(src.mTimestamp.UnixNano());
dst.set<ToInt(LauncherInstanceInfoColumns::eState)>(src.mState.ToString().CStr());
dst.set<ToInt(LauncherInstanceInfoColumns::eIsUnitSubject)>(src.mIsUnitSubject);
dst.set<ToInt(LauncherInstanceInfoColumns::eVersion)>(src.mVersion.CStr());
}

void Database::ToAos(const LauncherInstanceInfoRow& src, launcher::InstanceInfo& dst)
Expand All @@ -910,6 +912,7 @@ void Database::ToAos(const LauncherInstanceInfoRow& src, launcher::InstanceInfo&
dst.mTimestamp = Time::Unix(timestamp / Time::cSeconds.Nanoseconds(), timestamp % Time::cSeconds.Nanoseconds());

dst.mIsUnitSubject = src.get<ToInt(LauncherInstanceInfoColumns::eIsUnitSubject)>();
dst.mVersion = src.get<ToInt(LauncherInstanceInfoColumns::eVersion)>().c_str();

const auto& stateStr = src.get<ToInt(LauncherInstanceInfoColumns::eState)>();
AOS_ERROR_CHECK_AND_THROW(dst.mState.FromString(stateStr.c_str()), "failed to parse instance state");
Expand Down
5 changes: 3 additions & 2 deletions src/cm/database/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,11 @@ class Database : public storagestate::StorageItf,
eGID,
eTimestamp,
eState,
eIsUnitSubject
eIsUnitSubject,
eVersion
};
using LauncherInstanceInfoRow = Poco::Tuple<std::string, std::string, uint64_t, std::string, std::string,
std::string, std::string, std::string, uint32_t, uint32_t, uint64_t, std::string, bool>;
std::string, std::string, std::string, uint32_t, uint32_t, uint64_t, std::string, bool, std::string>;

enum class ImageManagerItemInfoColumns : int { eItemID = 0, eVersion, eIndexDigest, eState, eTimestamp };
using ImageManagerItemInfoRow = Poco::Tuple<std::string, std::string, std::string, int, uint64_t>;
Expand Down
45 changes: 28 additions & 17 deletions src/cm/database/tests/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ networkmanager::Instance CreateInstance(const char* itemID, const char* subjectI

launcher::InstanceInfo CreateLauncherInstanceInfo(const char* itemID, const char* subjectID, uint64_t instance,
const char* manifestDigest, const char* nodeID, UpdateItemType itemType = UpdateItemTypeEnum::eService,
launcher::InstanceStateEnum state = launcher::InstanceStateEnum::eCached, bool isUnitSubject = false)
launcher::InstanceStateEnum state = launcher::InstanceStateEnum::eCached, bool isUnitSubject = false,
const char* version = "1.0.0")
{
launcher::InstanceInfo info;

Expand All @@ -117,6 +118,7 @@ launcher::InstanceInfo CreateLauncherInstanceInfo(const char* itemID, const char
info.mTimestamp = Time::Now();
info.mState = state;
info.mIsUnitSubject = isUnitSubject;
info.mVersion = version;

return info;
}
Expand Down Expand Up @@ -497,36 +499,42 @@ TEST_F(CMDatabaseTest, LauncherAddInstance)
ASSERT_TRUE(mDB.Init(mDatabaseConfig).IsNone());

auto instance1 = CreateLauncherInstanceInfo("service1", "subject1", 0, "image1", "node1",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false, "1.0.0");
auto instance2 = CreateLauncherInstanceInfo("service1", "subject1", 1, "image1", "node1",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, true);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, true, "1.0.0");
auto instance3 = CreateLauncherInstanceInfo("service2", "subject2", 0, "image2", "node2",
UpdateItemTypeEnum::eComponent, launcher::InstanceStateEnum::eDisabled, false);
UpdateItemTypeEnum::eComponent, launcher::InstanceStateEnum::eDisabled, false, "2.0.0");

// Add instances
ASSERT_TRUE(mDB.AddInstance(instance1).IsNone());
ASSERT_TRUE(mDB.AddInstance(instance2).IsNone());
ASSERT_TRUE(mDB.AddInstance(instance3).IsNone());

// Add duplicate instance
auto duplicateInstance = CreateLauncherInstanceInfo("service1", "subject1", 0, "image99", "node99");
// Add duplicate instance (same primary key including version)
auto duplicateInstance = CreateLauncherInstanceInfo("service1", "subject1", 0, "image99", "node99",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, false, "1.0.0");
ASSERT_FALSE(mDB.AddInstance(duplicateInstance).IsNone());

// Add instance with same InstanceIdent but different version (should succeed)
auto instance1v2 = CreateLauncherInstanceInfo("service1", "subject1", 0, "image1", "node1",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false, "2.0.0");
ASSERT_TRUE(mDB.AddInstance(instance1v2).IsNone());

// Verify instances
StaticArray<launcher::InstanceInfo, 3> instances;
StaticArray<launcher::InstanceInfo, 4> instances;
ASSERT_TRUE(mDB.GetActiveInstances(instances).IsNone());

EXPECT_THAT(ToVector(instances), UnorderedElementsAre(instance1, instance2, instance3));
EXPECT_THAT(ToVector(instances), UnorderedElementsAre(instance1, instance2, instance3, instance1v2));
}

TEST_F(CMDatabaseTest, LauncherUpdateInstance)
{
ASSERT_TRUE(mDB.Init(mDatabaseConfig).IsNone());

auto instance1 = CreateLauncherInstanceInfo("service1", "subject1", 0, "image1", "node1",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, false);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, false, "1.0.0");
auto instance2 = CreateLauncherInstanceInfo("service2", "subject2", 0, "image2", "node2",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, true);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, true, "1.0.0");

// Add instances
ASSERT_TRUE(mDB.AddInstance(instance1).IsNone());
Expand All @@ -544,7 +552,8 @@ TEST_F(CMDatabaseTest, LauncherUpdateInstance)
ASSERT_TRUE(mDB.UpdateInstance(instance1).IsNone());

// Update non-existent instance
auto nonExistentInstance = CreateLauncherInstanceInfo("nonexistent", "subject", 99, "image99", "node99");
auto nonExistentInstance = CreateLauncherInstanceInfo("nonexistent", "subject", 99, "image99", "node99",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, false, "1.0.0");
ASSERT_FALSE(mDB.UpdateInstance(nonExistentInstance).IsNone());

// Verify updated instance
Expand All @@ -564,9 +573,9 @@ TEST_F(CMDatabaseTest, LauncherGetInstance)
ASSERT_TRUE(mDB.Init(mDatabaseConfig).IsNone());

auto instance1 = CreateLauncherInstanceInfo("service1", "subject1", 0, "image1", "node1",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false, "1.0.0");
auto instance2 = CreateLauncherInstanceInfo("service2", "subject2", 0, "image2", "node2",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, true);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, true, "2.0.0");

// Add instances
ASSERT_TRUE(mDB.AddInstance(instance1).IsNone());
Expand All @@ -577,9 +586,11 @@ TEST_F(CMDatabaseTest, LauncherGetInstance)

ASSERT_TRUE(mDB.GetInstance(instance1.mInstanceIdent, retrievedInstance).IsNone());
EXPECT_EQ(retrievedInstance, instance1);
EXPECT_EQ(retrievedInstance.mVersion, "1.0.0");

ASSERT_TRUE(mDB.GetInstance(instance2.mInstanceIdent, retrievedInstance).IsNone());
EXPECT_EQ(retrievedInstance, instance2);
EXPECT_EQ(retrievedInstance.mVersion, "2.0.0");

// Get non-existent instance
auto nonExistentIdent = CreateInstanceIdent("nonexistent", "subject", 99);
Expand All @@ -596,9 +607,9 @@ TEST_F(CMDatabaseTest, LauncherGetActiveInstances)
EXPECT_EQ(emptyInstances.Size(), 0);

auto instance1 = CreateLauncherInstanceInfo("service1", "subject1", 0, "image1", "node1",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false, "1.0.0");
auto instance2 = CreateLauncherInstanceInfo("service2", "subject2", 0, "image2", "node2",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eDisabled, true);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eDisabled, true, "2.0.0");

// Add instances
ASSERT_TRUE(mDB.AddInstance(instance1).IsNone());
Expand All @@ -616,9 +627,9 @@ TEST_F(CMDatabaseTest, LauncherRemoveInstance)
ASSERT_TRUE(mDB.Init(mDatabaseConfig).IsNone());

auto instance1 = CreateLauncherInstanceInfo("service1", "subject1", 0, "image1", "node1",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, true);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eCached, true, "1.0.0");
auto instance2 = CreateLauncherInstanceInfo("service2", "subject2", 0, "image2", "node2",
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false);
UpdateItemTypeEnum::eService, launcher::InstanceStateEnum::eActive, false, "2.0.0");

ASSERT_TRUE(mDB.AddInstance(instance1).IsNone());
ASSERT_TRUE(mDB.AddInstance(instance2).IsNone());
Expand Down
Loading