Skip to content

Commit a45d5aa

Browse files
committed
sm: launcher: rename update instances to run instances
This patch updated launcher interface to handle run instances instead of update instances: - a list of desired run instances is passed to the launcher instead stop and start instances lists; - the launcher is responsible for stopping and starting instances based on the provided list of desired run instances; - instances are started based on the desired run instances priorities. Signed-off-by: Mykhailo Lohvynenko <mykhailo_lohvynenko@epam.com>
1 parent b572146 commit a45d5aa

5 files changed

Lines changed: 104 additions & 47 deletions

File tree

src/core/sm/launcher/itf/launcher.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,12 @@ class LauncherItf {
2727
virtual ~LauncherItf() = default;
2828

2929
/**
30-
* Update running instances.
30+
* Runs instances.
3131
*
32-
* @param stopInstances instances to stop.
33-
* @param startInstances instances to start.
32+
* @param instances instances to run.
3433
* @return Error.
3534
*/
36-
virtual Error UpdateInstances(const Array<InstanceIdent>& stopInstances, const Array<InstanceInfo>& startInstances)
37-
= 0;
35+
virtual Error RunInstances(const Array<InstanceInfo>& instances) = 0;
3836
};
3937

4038
/** @}*/

src/core/sm/launcher/launcher.cpp

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Error Launcher::Start()
7777

7878
lock.Unlock();
7979

80-
if (auto err = UpdateInstances({}, *storedInstances); !err.IsNone()) {
80+
if (auto err = RunInstances(*storedInstances); !err.IsNone()) {
8181
return AOS_ERROR_WRAP(err);
8282
}
8383

@@ -131,25 +131,43 @@ Error Launcher::Stop()
131131
return stopErr;
132132
}
133133

134-
Error Launcher::UpdateInstances(const Array<InstanceIdent>& stopInstances, const Array<InstanceInfo>& startInstances)
134+
Error Launcher::RunInstances(const Array<InstanceInfo>& instances)
135135
{
136+
LOG_DBG() << "Run instances" << Log::Field("count", instances.Size());
137+
136138
if (auto err = StartLaunch(); !err.IsNone()) {
137139
return AOS_ERROR_WRAP(err);
138140
}
139141

140142
// Wait in case previous request is not yet finished
141143
mThread.Join();
142144

143-
auto stop = MakeShared<StaticArray<InstanceIdent, cMaxNumInstances>>(&mAllocator, stopInstances);
144-
auto start = MakeShared<InstanceInfoArray>(&mAllocator, startInstances);
145+
mStopInstances.Clear();
146+
mStartInstances.Clear();
147+
148+
auto err = mStartInstances.Assign(instances);
149+
if (!err.IsNone()) {
150+
return AOS_ERROR_WRAP(err);
151+
}
152+
153+
SortStartInstances();
145154

146-
if (auto err = mThread.Run([this, stop, start](void*) {
147-
UpdateInstancesImpl(*stop, *start);
155+
auto clear = DeferRelease(&err, [this](const Error* err) {
156+
if (!err->IsNone()) {
148157
FinishLaunch();
149-
});
150-
!err.IsNone()) {
151-
FinishLaunch();
158+
}
159+
});
152160

161+
err = SetStopInstances();
162+
if (!err.IsNone()) {
163+
return AOS_ERROR_WRAP(err);
164+
}
165+
166+
err = mThread.Run([this](void*) {
167+
RunInstancesImpl();
168+
FinishLaunch();
169+
});
170+
if (!err.IsNone()) {
153171
return AOS_ERROR_WRAP(err);
154172
}
155173

@@ -546,10 +564,27 @@ Error Launcher::HandleComponentStatus(const aos::InstanceStatus& status)
546564
return ErrorEnum::eNone;
547565
}
548566

549-
void Launcher::UpdateInstancesImpl(Array<InstanceIdent>& stopInstances, const Array<InstanceInfo>& startInstances)
567+
void Launcher::SortStartInstances()
568+
{
569+
auto sortTmpValue = MakeUnique<InstanceInfo>(&mAllocator);
570+
571+
// Sort instances by priority and instance ID to have deterministic order
572+
// of start/stop in case of same priority.
573+
mStartInstances.Sort(
574+
[](const InstanceInfo& a, const InstanceInfo& b) {
575+
if (a.mPriority != b.mPriority) {
576+
return a.mPriority > b.mPriority;
577+
}
578+
579+
return static_cast<const InstanceIdent&>(a) < static_cast<const InstanceIdent&>(b);
580+
},
581+
*sortTmpValue);
582+
}
583+
584+
void Launcher::RunInstancesImpl()
550585
{
551-
LOG_INF() << "Update instances" << Log::Field("stopCount", stopInstances.Size())
552-
<< Log::Field("startCount", startInstances.Size());
586+
LOG_INF() << "Update instances" << Log::Field("stopCount", mStopInstances.Size())
587+
<< Log::Field("startCount", mStartInstances.Size());
553588

554589
auto sendStatus = DeferRelease(&mInstances, [this](Array<InstanceData>*) {
555590
LockGuard lock {mMutex};
@@ -569,34 +604,30 @@ void Launcher::UpdateInstancesImpl(Array<InstanceIdent>& stopInstances, const Ar
569604
return;
570605
}
571606

572-
if (auto err = AppendInstancesWithModifiedParams(startInstances, stopInstances); !err.IsNone()) {
573-
LOG_ERR() << "Failed to append instances with modified params to stop list" << Log::Field(AOS_ERROR_WRAP(err));
574-
}
575-
576607
auto removeItems = MakeUnique<StaticArray<UpdateItemInfo, cMaxNumUpdateItems>>(&mAllocator);
577608

578609
if (!mFirstStart) {
579-
GetRemoveUpdateItems(stopInstances, startInstances, *removeItems);
610+
GetRemoveUpdateItems(mStopInstances, mStartInstances, *removeItems);
580611
}
581612

582-
StopInstances(stopInstances);
613+
StopInstances(mStopInstances);
583614

584615
if (auto err = mLaunchPool.Wait(); !err.IsNone()) {
585616
LOG_ERR() << "Thread pool wait failed" << Log::Field(AOS_ERROR_WRAP(err));
586617
}
587618

588-
RemoveInstancesData(stopInstances);
619+
RemoveInstancesData(mStopInstances);
589620

590621
if (!mFirstStart) {
591622
RemoveUpdateItems(*removeItems);
592-
InstallUpdateItems(startInstances);
623+
InstallUpdateItems(mStartInstances);
593624

594625
if (auto err = mLaunchPool.Wait(); !err.IsNone()) {
595626
LOG_ERR() << "Thread pool wait failed" << Log::Field(AOS_ERROR_WRAP(err));
596627
}
597628
}
598629

599-
StartInstances(startInstances);
630+
StartInstances(mStartInstances);
600631

601632
if (auto err = mLaunchPool.Wait(); !err.IsNone()) {
602633
LOG_ERR() << "Thread pool wait failed" << Log::Field(AOS_ERROR_WRAP(err));
@@ -607,6 +638,32 @@ void Launcher::UpdateInstancesImpl(Array<InstanceIdent>& stopInstances, const Ar
607638
}
608639
}
609640

641+
Error Launcher::SetStopInstances()
642+
{
643+
mStopInstances.Clear();
644+
645+
for (const auto& instance : mInstances) {
646+
if (mStartInstances.ContainsIf([&instance](const auto& startInstance) {
647+
return static_cast<const InstanceIdent&>(startInstance)
648+
== static_cast<const InstanceIdent&>(instance.mInfo)
649+
&& (startInstance.mVersion == instance.mInfo.mVersion
650+
|| startInstance.mManifestDigest == instance.mInfo.mManifestDigest);
651+
})) {
652+
continue;
653+
}
654+
655+
if (auto err = mStopInstances.EmplaceBack(instance.mStatus); !err.IsNone()) {
656+
return AOS_ERROR_WRAP(err);
657+
}
658+
}
659+
660+
if (auto err = AppendInstancesWithModifiedParams(mStartInstances, mStopInstances); !err.IsNone()) {
661+
return AOS_ERROR_WRAP(err);
662+
}
663+
664+
return ErrorEnum::eNone;
665+
}
666+
610667
void Launcher::StopInstances(const Array<InstanceIdent>& stopInstances)
611668
{
612669
for (const auto& instance : stopInstances) {
@@ -767,6 +824,8 @@ Error Launcher::StartLaunch()
767824
{
768825
LockGuard lock {mMutex};
769826

827+
LOG_DBG() << "Start launch" << Log::Field("launchInProgress", mLaunchInProgress);
828+
770829
if (mLaunchInProgress) {
771830
return AOS_ERROR_WRAP(ErrorEnum::eWrongState);
772831
}

src/core/sm/launcher/launcher.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,12 @@ class Launcher : public LauncherItf,
7070
Error Stop();
7171

7272
/**
73-
* Update running instances.
73+
* Runs instances.
7474
*
75-
* @param stopInstances instances to stop.
76-
* @param startInstances instances to start.
75+
* @param instances instances to run.
7776
* @return Error.
7877
*/
79-
Error UpdateInstances(
80-
const Array<InstanceIdent>& stopInstances, const Array<InstanceInfo>& startInstances) override;
78+
Error RunInstances(const Array<InstanceInfo>& instances) override;
8179

8280
/**
8381
* Receives instances statuses.
@@ -173,7 +171,9 @@ class Launcher : public LauncherItf,
173171
void RunRebootThread();
174172
void HandleOfflineTTLs();
175173
Error HandleComponentStatus(const aos::InstanceStatus& status);
176-
void UpdateInstancesImpl(Array<InstanceIdent>& stopInstances, const Array<InstanceInfo>& startInstances);
174+
void SortStartInstances();
175+
void RunInstancesImpl();
176+
Error SetStopInstances();
177177
void StopInstances(const Array<InstanceIdent>& stopInstances);
178178
Error StopInstance(InstanceData& instanceData);
179179
void StopAllInstances();
@@ -227,6 +227,8 @@ class Launcher : public LauncherItf,
227227
bool mIsRunning {};
228228
bool mFirstStart {true};
229229
Optional<Time> mOfflineTime {};
230+
StaticArray<InstanceIdent, cMaxNumInstances> mStopInstances;
231+
InstanceInfoArray mStartInstances;
230232
};
231233

232234
} // namespace aos::sm::launcher

src/core/sm/launcher/tests/launcher.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ TEST_F(LauncherTest, SendActiveComponentNodeInstancesStatusOnModuleStart)
279279
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
280280
}
281281

282-
TEST_F(LauncherTest, DoNotSendUpdateInstancesStatusesBeforeModuleStart)
282+
TEST_F(LauncherTest, DoNotSendRunInstancesStatusesBeforeModuleStart)
283283
{
284284
const std::vector cRuntime0Components = {
285285
CreateInstanceStatus(CreateInstanceInfo("item1", 1, "1.0.0", "runtime0"), InstanceStateEnum::eActive,
@@ -459,7 +459,7 @@ TEST_F(LauncherTest, StopInstancesWithExpiredOfflineTTL)
459459
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
460460
}
461461

462-
TEST_F(LauncherTest, UpdateInstances)
462+
TEST_F(LauncherTest, RunInstances)
463463
{
464464
const std::vector cStoredInfos = {
465465
CreateInstanceInfo("item0", 0, "1.0.0", "runtime0"),
@@ -468,8 +468,7 @@ TEST_F(LauncherTest, UpdateInstances)
468468
CreateInstanceInfo("item1", 1, "1.0.0", "runtime0"),
469469
CreateInstanceInfo("item2", 2, "1.0.0", "runtime1"),
470470
};
471-
const Array<InstanceInfo> cStartInstances(&cStartInstanceInfos.front(), cStartInstanceInfos.size());
472-
const Array<InstanceIdent> cStopInstances(&static_cast<const InstanceIdent&>(cStoredInfos.front()), 1);
471+
const Array<InstanceInfo> cStartInstances(&cStartInstanceInfos.front(), cStartInstanceInfos.size());
473472

474473
mStorage.Init(cStoredInfos);
475474

@@ -530,7 +529,7 @@ TEST_F(LauncherTest, UpdateInstances)
530529
return ErrorEnum::eNone;
531530
}));
532531

533-
err = mLauncher.UpdateInstances(cStopInstances, cStartInstances);
532+
err = mLauncher.RunInstances(cStartInstances);
534533
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
535534

536535
err = mSender.WaitStatuses(mReceivedStatuses, cWaitTimeout);
@@ -567,7 +566,7 @@ TEST_F(LauncherTest, UpdateInstances)
567566
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
568567
}
569568

570-
TEST_F(LauncherTest, UpdateInstancesRestartsInstancesWithModifiedParams)
569+
TEST_F(LauncherTest, RunInstancesRestartsInstancesWithModifiedParams)
571570
{
572571
const std::vector cStoredInfos = {
573572
CreateInstanceInfo("item0", 0, "1.0.0", "runtime0"),
@@ -617,7 +616,7 @@ TEST_F(LauncherTest, UpdateInstancesRestartsInstancesWithModifiedParams)
617616
return ErrorEnum::eNone;
618617
}));
619618

620-
err = mLauncher.UpdateInstances({}, cStartInstances);
619+
err = mLauncher.RunInstances(cStartInstances);
621620
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
622621

623622
err = mSender.WaitStatuses(mReceivedStatuses, cWaitTimeout);
@@ -646,7 +645,7 @@ TEST_F(LauncherTest, UpdateInstancesRestartsInstancesWithModifiedParams)
646645
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
647646
}
648647

649-
TEST_F(LauncherTest, ParallelUpdateInstancesDoesNotInterfere)
648+
TEST_F(LauncherTest, ParallelRunInstancesDoesNotInterfere)
650649
{
651650
const std::vector cStartInstanceInfos = {
652651
CreateInstanceInfo("item0", 0, "1.0.0", "runtime0"),
@@ -678,10 +677,10 @@ TEST_F(LauncherTest, ParallelUpdateInstancesDoesNotInterfere)
678677
return ErrorEnum::eNone;
679678
}));
680679

681-
err = mLauncher.UpdateInstances({}, cStartFirstInstance);
680+
err = mLauncher.RunInstances(cStartFirstInstance);
682681
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
683682

684-
err = mLauncher.UpdateInstances({}, cStartInstances);
683+
err = mLauncher.RunInstances(cStartInstances);
685684
ASSERT_TRUE(err.Is(ErrorEnum::eWrongState)) << tests::utils::ErrorToStr(err);
686685

687686
launchPromise.set_value();
@@ -752,7 +751,7 @@ TEST_F(LauncherTest, GetInstancesStatuses)
752751
err = mLauncher.GetInstancesStatuses(mReceivedStatuses);
753752
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
754753

755-
err = mLauncher.UpdateInstances({}, cStartInstances);
754+
err = mLauncher.RunInstances(cStartInstances);
756755
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
757756

758757
err = mSender.WaitStatuses(mReceivedStatuses, cWaitTimeout);
@@ -809,7 +808,7 @@ TEST_F(LauncherTest, GetInstanceMonitoringParams)
809808
err = mLauncher.GetInstancesStatuses(mReceivedStatuses);
810809
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
811810

812-
err = mLauncher.UpdateInstances({}, cStartInstances);
811+
err = mLauncher.RunInstances(cStartInstances);
813812
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
814813

815814
err = mSender.WaitStatuses(mReceivedStatuses, cWaitTimeout);
@@ -865,7 +864,7 @@ TEST_F(LauncherTest, GetInstanceMonitoringData)
865864
return ErrorEnum::eNone;
866865
}));
867866

868-
err = mLauncher.UpdateInstances({}, cStartInstances);
867+
err = mLauncher.RunInstances(cStartInstances);
869868
ASSERT_TRUE(err.IsNone()) << tests::utils::ErrorToStr(err);
870869

871870
err = mSender.WaitStatuses(mReceivedStatuses, cWaitTimeout);

src/core/sm/tests/mocks/launchermock.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ namespace aos::sm::launcher {
1919
*/
2020
class LauncherMock : public LauncherItf {
2121
public:
22-
MOCK_METHOD(Error, UpdateInstances,
23-
(const Array<InstanceIdent>& stopInstances, const Array<InstanceInfo>& startInstances), (override));
22+
MOCK_METHOD(Error, RunInstances, (const Array<InstanceInfo>& instances), (override));
2423
};
2524

2625
} // namespace aos::sm::launcher

0 commit comments

Comments
 (0)