@@ -424,8 +424,8 @@ Error Launcher::ProcessLastInstances()
424
424
425
425
auto stopInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator );
426
426
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;
429
429
}
430
430
431
431
if (auto err = mLaunchPool .Run (); !err.IsNone ()) {
@@ -442,19 +442,23 @@ Error Launcher::ProcessLastInstances()
442
442
return ErrorEnum::eNone;
443
443
}
444
444
445
- Error Launcher::ProcessInstances (const Array<InstanceInfo>& instances, const bool forceRestart)
445
+ Error Launcher::ProcessInstances (const Array<InstanceInfo>& instances, bool forceRestart)
446
446
{
447
447
LOG_DBG () << " Process instances: restart=" << forceRestart;
448
448
449
- auto startInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator );
449
+ auto desiredInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator );
450
450
451
- if (auto err = GetStartInstances (instances, *startInstances ); !err.IsNone ()) {
451
+ if (auto err = GetDesiredInstancesData (instances, *desiredInstances ); !err.IsNone ()) {
452
452
return err;
453
453
}
454
454
455
- auto stopInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator );
455
+ auto startInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator );
456
+ auto stopInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator );
457
+
458
+ CacheServices (*desiredInstances);
456
459
457
- if (auto err = GetStopInstances (*startInstances, *stopInstances, forceRestart); !err.IsNone ()) {
460
+ if (auto err = CalculateInstances (*desiredInstances, forceRestart, *startInstances, *stopInstances);
461
+ !err.IsNone ()) {
458
462
return err;
459
463
}
460
464
@@ -463,7 +467,6 @@ Error Launcher::ProcessInstances(const Array<InstanceInfo>& instances, const boo
463
467
}
464
468
465
469
StopInstances (*stopInstances);
466
- CacheServices (*startInstances);
467
470
StartInstances (*startInstances);
468
471
469
472
if (auto err = mLaunchPool .Shutdown (); !err.IsNone ()) {
@@ -572,54 +575,50 @@ Error Launcher::SendOutdatedInstancesStatus(const Array<InstanceData>& instances
572
575
return ErrorEnum::eNone;
573
576
}
574
577
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
577
580
{
578
581
LockGuard lock {mMutex };
579
582
580
- LOG_DBG () << " Get start instances" ;
583
+ LOG_DBG () << " Get desired instances data " ;
581
584
582
585
auto currentInstances = MakeUnique<InstanceDataStaticArray>(&mAllocator );
583
586
584
587
if (auto err = mStorage ->GetAllInstances (*currentInstances); !err.IsNone ()) {
585
588
return AOS_ERROR_WRAP (err);
586
589
}
587
590
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 ;
591
594
});
595
+ if (currentInstance == currentInstances->end ()) {
596
+ const auto instanceID = uuid::UUIDToString (uuid::CreateUUID ());
592
597
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);
607
600
}
608
601
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
+ }
610
605
611
606
continue ;
612
607
}
613
608
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 ()) {
617
610
return AOS_ERROR_WRAP (err);
618
611
}
619
612
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
+ }
622
619
}
620
+
621
+ currentInstances->Erase (currentInstance);
623
622
}
624
623
625
624
// Remove old instances
@@ -630,48 +629,63 @@ Error Launcher::GetStartInstances(
630
629
}
631
630
}
632
631
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
+
633
642
return ErrorEnum::eNone;
634
643
}
635
644
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)
638
647
{
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
+ });
644
652
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
+ }
647
657
648
- if (auto err = mServiceManager ->GetAllServices (*services); !err.IsNone ()) {
649
- return AOS_ERROR_WRAP (err);
658
+ continue ;
650
659
}
651
- }
652
660
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 ());
656
662
657
- compareInfo->mInstanceInfo . mPriority = instance. Info () .mPriority ;
663
+ compareInfo->mPriority = desiredInstance. mInstanceInfo .mPriority ;
658
664
659
- return compareInfo->mInstanceID == instance.InstanceID () && compareInfo->mInstanceInfo == instance.Info ();
660
- }) != startInstances.end ();
665
+ auto service = GetService (desiredInstance.mInstanceInfo .mInstanceIdent .mServiceID );
661
666
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
+ }
667
673
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) ;
670
676
}
671
677
}
678
+ }
672
679
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 ([¤tInstance](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
+ }
675
689
}
676
690
}
677
691
0 commit comments