Skip to content

Commit 7836cb6

Browse files
committedFeb 26, 2025·
[sm,launcher] Reimplement calculating start and stop instances
Current approach doesn't properly calculate instances that should be stopped and started on run instances command. Reimplement this calculation by adding 'CalculateInstances' function. This function calculate instances that should be stopped and started based on currently running instances and desired instances received in run instances request. This function is implemented in the following way: * if desired instance ident not in current instances list, add this instance to start list; * if desired instance ident is in current instances list, check if instance data, or service version are changed, or restart all instances is requested. In this case, add this item to start and stop lists; * add all instances from current instances list that are not in desired list to stop list. This commit also updates the unit test that check run instances API. Test data is taken from golang sm implementation to make sure that it behaves in the same way. Signed-off-by: Oleksandr Grytsov <oleksandr_grytsov@epam.com>
1 parent 5bd3edb commit 7836cb6

File tree

4 files changed

+293
-147
lines changed

4 files changed

+293
-147
lines changed
 

‎include/aos/sm/launcher.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ class Launcher : public LauncherItf,
347347
static constexpr auto cHostFSWhiteoutsDir = "whiteouts";
348348

349349
static constexpr auto cAllocatorSize
350-
= Max(sizeof(InstanceInfoStaticArray) + sizeof(InstanceDataStaticArray) * 2 + sizeof(ServiceInfoStaticArray)
350+
= Max(sizeof(InstanceInfoStaticArray) + sizeof(InstanceDataStaticArray) * 3 + sizeof(ServiceInfoStaticArray)
351351
+ sizeof(LayerInfoStaticArray) + sizeof(servicemanager::ServiceDataStaticArray)
352352
+ sizeof(InstanceStatusStaticArray) + sizeof(servicemanager::ServiceData) + sizeof(InstanceData),
353353
sizeof(EnvVarsArray) + sizeof(InstanceStatusStaticArray) + sizeof(InstanceDataStaticArray));
@@ -365,9 +365,10 @@ class Launcher : public LauncherItf,
365365
void StartInstances(const Array<InstanceData>& instances);
366366
void RestartInstances(const Array<InstanceData>& instances);
367367
void CacheServices(const Array<InstanceData>& instances);
368-
Error GetStartInstances(const Array<InstanceInfo>& desiredInstances, Array<InstanceData>& startInstances) const;
369-
Error GetStopInstances(
370-
const Array<InstanceData>& startInstances, Array<InstanceData>& stopInstances, bool forceRestart) const;
368+
Error GetDesiredInstancesData(
369+
const Array<InstanceInfo>& desiredInstancesInfo, Array<InstanceData>& desiredInstancesData) const;
370+
Error CalculateInstances(const Array<InstanceData>& desiredInstancesData, bool forceRestart,
371+
Array<InstanceData>& startInstances, Array<InstanceData>& stopInstances);
371372
Error GetCurrentInstances(Array<InstanceData>& instances) const;
372373
Error StartInstance(const InstanceData& info);
373374
Error StopInstance(const String& instanceID);

‎src/sm/launcher/launcher.cpp

+75-61
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ Error Launcher::ProcessLastInstances()
424424

425425
auto stopInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator);
426426

427-
if (auto err = GetStopInstances(*startInstances, *stopInstances, true); !err.IsNone()) {
428-
LOG_WRN() << "Error occurred while getting stop instances: err=" << err;
427+
if (auto err = GetCurrentInstances(*stopInstances); !err.IsNone()) {
428+
LOG_WRN() << "Error occurred while getting current instances: err=" << err;
429429
}
430430

431431
if (auto err = mLaunchPool.Run(); !err.IsNone()) {
@@ -442,19 +442,23 @@ Error Launcher::ProcessLastInstances()
442442
return ErrorEnum::eNone;
443443
}
444444

445-
Error Launcher::ProcessInstances(const Array<InstanceInfo>& instances, const bool forceRestart)
445+
Error Launcher::ProcessInstances(const Array<InstanceInfo>& instances, bool forceRestart)
446446
{
447447
LOG_DBG() << "Process instances: restart=" << forceRestart;
448448

449-
auto startInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator);
449+
auto desiredInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator);
450450

451-
if (auto err = GetStartInstances(instances, *startInstances); !err.IsNone()) {
451+
if (auto err = GetDesiredInstancesData(instances, *desiredInstances); !err.IsNone()) {
452452
return err;
453453
}
454454

455-
auto stopInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator);
455+
auto startInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator);
456+
auto stopInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator);
457+
458+
CacheServices(*desiredInstances);
456459

457-
if (auto err = GetStopInstances(*startInstances, *stopInstances, forceRestart); !err.IsNone()) {
460+
if (auto err = CalculateInstances(*desiredInstances, forceRestart, *startInstances, *stopInstances);
461+
!err.IsNone()) {
458462
return err;
459463
}
460464

@@ -463,7 +467,6 @@ Error Launcher::ProcessInstances(const Array<InstanceInfo>& instances, const boo
463467
}
464468

465469
StopInstances(*stopInstances);
466-
CacheServices(*startInstances);
467470
StartInstances(*startInstances);
468471

469472
if (auto err = mLaunchPool.Shutdown(); !err.IsNone()) {
@@ -572,54 +575,50 @@ Error Launcher::SendOutdatedInstancesStatus(const Array<InstanceData>& instances
572575
return ErrorEnum::eNone;
573576
}
574577

575-
Error Launcher::GetStartInstances(
576-
const Array<InstanceInfo>& desiredInstances, Array<InstanceData>& runningInstances) const
578+
Error Launcher::GetDesiredInstancesData(
579+
const Array<InstanceInfo>& desiredInstancesInfo, Array<InstanceData>& desiredInstancesData) const
577580
{
578581
LockGuard lock {mMutex};
579582

580-
LOG_DBG() << "Get start instances";
583+
LOG_DBG() << "Get desired instances data";
581584

582585
auto currentInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator);
583586

584587
if (auto err = mStorage->GetAllInstances(*currentInstances); !err.IsNone()) {
585588
return AOS_ERROR_WRAP(err);
586589
}
587590

588-
for (const auto& desiredInstance : desiredInstances) {
589-
auto currentInstance = currentInstances->FindIf([&desiredInstance](const InstanceData& instance) {
590-
return instance.mInstanceInfo.mInstanceIdent == desiredInstance.mInstanceIdent;
591+
for (const auto& instanceInfo : desiredInstancesInfo) {
592+
auto currentInstance = currentInstances->FindIf([&instanceInfo](const InstanceData& instance) {
593+
return instance.mInstanceInfo.mInstanceIdent == instanceInfo.mInstanceIdent;
591594
});
595+
if (currentInstance == currentInstances->end()) {
596+
const auto instanceID = uuid::UUIDToString(uuid::CreateUUID());
592597

593-
if (currentInstance != currentInstances->end() && currentInstance) {
594-
// Update instance if parameters are changed
595-
if (currentInstance->mInstanceInfo != desiredInstance) {
596-
if (auto err = runningInstances.PushBack(*currentInstance); !err.IsNone()) {
597-
return AOS_ERROR_WRAP(err);
598-
}
599-
600-
auto& updateInstance = runningInstances.Back();
601-
602-
updateInstance.mInstanceInfo = desiredInstance;
603-
604-
if (auto err = mStorage->UpdateInstance(updateInstance); !err.IsNone()) {
605-
LOG_ERR() << "Can't update instance: instanceID=" << updateInstance.mInstanceID << ", err=" << err;
606-
}
598+
if (auto err = desiredInstancesData.EmplaceBack(instanceInfo, instanceID); !err.IsNone()) {
599+
return AOS_ERROR_WRAP(err);
607600
}
608601

609-
currentInstances->Erase(currentInstance);
602+
if (auto err = mStorage->AddInstance(desiredInstancesData.Back()); !err.IsNone()) {
603+
LOG_ERR() << "Can't add instance: instanceID=" << instanceID << ", err=" << err;
604+
}
610605

611606
continue;
612607
}
613608

614-
const auto instanceID = uuid::UUIDToString(uuid::CreateUUID());
615-
616-
if (auto err = runningInstances.EmplaceBack(desiredInstance, instanceID); !err.IsNone()) {
609+
if (auto err = desiredInstancesData.EmplaceBack(instanceInfo, currentInstance->mInstanceID); !err.IsNone()) {
617610
return AOS_ERROR_WRAP(err);
618611
}
619612

620-
if (auto err = mStorage->AddInstance(runningInstances.Back()); !err.IsNone()) {
621-
LOG_ERR() << "Can't add instance: instanceID=" << instanceID << ", err=" << err;
613+
// Update instance if parameters are changed
614+
if (currentInstance->mInstanceInfo != instanceInfo) {
615+
if (auto err = mStorage->UpdateInstance(desiredInstancesData.Back()); !err.IsNone()) {
616+
LOG_ERR() << "Can't update instance: instanceID=" << desiredInstancesData.Back().mInstanceID
617+
<< ", err=" << err;
618+
}
622619
}
620+
621+
currentInstances->Erase(currentInstance);
623622
}
624623

625624
// Remove old instances
@@ -630,48 +629,63 @@ Error Launcher::GetStartInstances(
630629
}
631630
}
632631

632+
// Sort by priority
633+
634+
auto tmpValue = MakeUnique<InstanceData>(&mAllocator);
635+
636+
desiredInstancesData.Sort(
637+
[](const InstanceData& instance1, const InstanceData& instance2) {
638+
return instance1.mInstanceInfo.mPriority > instance2.mInstanceInfo.mPriority;
639+
},
640+
*tmpValue);
641+
633642
return ErrorEnum::eNone;
634643
}
635644

636-
Error Launcher::GetStopInstances(
637-
const Array<InstanceData>& startInstances, Array<InstanceData>& stopInstances, bool forceRestart) const
645+
Error Launcher::CalculateInstances(const Array<InstanceData>& desiredInstancesData, bool forceRestart,
646+
Array<InstanceData>& startInstances, Array<InstanceData>& stopInstances)
638647
{
639-
LockGuard lock {mMutex};
640-
641-
LOG_DBG() << "Get stop instances";
642-
643-
UniquePtr<servicemanager::ServiceDataStaticArray> services;
648+
for (const auto& desiredInstance : desiredInstancesData) {
649+
auto currentInstance = mCurrentInstances.FindIf([&desiredInstance](const Instance& instance) {
650+
return instance.Info().mInstanceIdent == desiredInstance.mInstanceInfo.mInstanceIdent;
651+
});
644652

645-
if (!forceRestart) {
646-
services = MakeUnique<servicemanager::ServiceDataStaticArray>(&mAllocator);
653+
if (currentInstance == mCurrentInstances.end()) {
654+
if (auto err = startInstances.EmplaceBack(desiredInstance); !err.IsNone()) {
655+
return AOS_ERROR_WRAP(err);
656+
}
647657

648-
if (auto err = mServiceManager->GetAllServices(*services); !err.IsNone()) {
649-
return AOS_ERROR_WRAP(err);
658+
continue;
650659
}
651-
}
652660

653-
for (const auto& instance : mCurrentInstances) {
654-
auto found = startInstances.FindIf([this, &instance = instance](const InstanceData& info) {
655-
auto compareInfo = MakeUnique<InstanceData>(&mAllocator, info);
661+
auto compareInfo = MakeUnique<InstanceInfo>(&mAllocator, currentInstance->Info());
656662

657-
compareInfo->mInstanceInfo.mPriority = instance.Info().mPriority;
663+
compareInfo->mPriority = desiredInstance.mInstanceInfo.mPriority;
658664

659-
return compareInfo->mInstanceID == instance.InstanceID() && compareInfo->mInstanceInfo == instance.Info();
660-
}) != startInstances.end();
665+
auto service = GetService(desiredInstance.mInstanceInfo.mInstanceIdent.mServiceID);
661666

662-
// Stop instance if: forceRestart or not in instances array or not active state or Aos version changed
663-
if (!forceRestart && found && instance.RunState() == InstanceRunStateEnum::eActive) {
664-
auto findService = services->FindIf([&instance = instance](const servicemanager::ServiceData& service) {
665-
return instance.Info().mInstanceIdent.mServiceID == service.mServiceID;
666-
});
667+
if ((*compareInfo != desiredInstance.mInstanceInfo) || service == mCurrentServices.end()
668+
|| service->mVersion != currentInstance->GetServiceVersion() || forceRestart) {
669+
if (auto err = stopInstances.EmplaceBack(currentInstance->Info(), currentInstance->InstanceID());
670+
!err.IsNone()) {
671+
return AOS_ERROR_WRAP(err);
672+
}
667673

668-
if (findService != services->end() && instance.GetServiceVersion() == findService->mVersion) {
669-
continue;
674+
if (auto err = startInstances.EmplaceBack(desiredInstance); !err.IsNone()) {
675+
return AOS_ERROR_WRAP(err);
670676
}
671677
}
678+
}
672679

673-
if (auto err = stopInstances.EmplaceBack(instance.Info(), instance.InstanceID()); !err.IsNone()) {
674-
return AOS_ERROR_WRAP(err);
680+
for (const auto& currentInstance : mCurrentInstances) {
681+
if (desiredInstancesData.FindIf([&currentInstance](const InstanceData& instance) {
682+
return instance.mInstanceInfo.mInstanceIdent == currentInstance.Info().mInstanceIdent;
683+
})
684+
== desiredInstancesData.end()) {
685+
if (auto err = stopInstances.EmplaceBack(currentInstance.Info(), currentInstance.InstanceID());
686+
!err.IsNone()) {
687+
return AOS_ERROR_WRAP(err);
688+
}
675689
}
676690
}
677691

0 commit comments

Comments
 (0)
Please sign in to comment.