From accfe34c44364ddc30ca8b9d212f831bdd84cedd Mon Sep 17 00:00:00 2001 From: sebaszm Date: Tue, 8 Oct 2024 14:11:05 +0200 Subject: [PATCH 1/3] [ProcessContainers] Update Dobby implementation --- .../DobbyImplementation.cpp | 73 ++++++++----------- .../DobbyImplementation/DobbyImplementation.h | 44 +++++------ 2 files changed, 51 insertions(+), 66 deletions(-) diff --git a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp index ac624c18b..7a7a2b903 100644 --- a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp +++ b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp @@ -17,6 +17,12 @@ * limitations under the License. */ + +#include "processcontainers/Messaging.h" +#include "processcontainers/ContainerProducer.h" +#include "processcontainers/ContainerAdministrator.h" +#include "processcontainers/common/CGroupContainerInfo.h" + #include "DobbyImplementation.h" #include #include @@ -26,31 +32,22 @@ namespace Thunder { namespace ProcessContainers { - // Container administrator - // ---------------------------------- - IContainerAdministrator& ProcessContainers::IContainerAdministrator::Instance() - { - static DobbyContainerAdministrator& dobbyContainerAdministrator = Core::SingletonType::Instance(); - return dobbyContainerAdministrator; - } - - IContainer* DobbyContainerAdministrator::Container(const string& id, IStringIterator& searchpaths, const string& logpath, const string& configuration VARIABLE_IS_NOT_USED) + Core::ProxyType DobbyContainerAdministrator::Container(const string& id, IStringIterator& searchpaths, + const string& logpath, const string& configuration VARIABLE_IS_NOT_USED) { searchpaths.Reset(0); while (searchpaths.Next()) { auto path = searchpaths.Current(); - auto createContainer = [&](bool useSpecFile, const string &path) -> IContainer* { + auto createContainer = [&](bool useSpecFile, const string &path) -> Core::ProxyType { // Make sure no leftover will interfere... if (ContainerNameTaken(id)) { DestroyContainer(id); } - this->InternalLock(); - DobbyContainer* container = new DobbyContainer(id, path, logpath, useSpecFile); - InsertContainer(container); - this->InternalUnlock(); + Core::ProxyType container; + container = ContainerAdministrator::Instance().Create(*this, id, path, logpath, useSpecFile); return container; }; @@ -71,17 +68,16 @@ namespace ProcessContainers { TRACE(Trace::Error, (_T("Could not find suitable container config for %s in any search path"), id.c_str())); - return nullptr; + return Core::ProxyType(); } - DobbyContainerAdministrator::DobbyContainerAdministrator() - : BaseContainerAdministrator() + uint32_t DobbyContainerAdministrator::Initialize(const string& config VARIABLE_IS_NOT_USED) { mIpcService = AI_IPC::createIpcService("unix:path=/var/run/dbus/system_bus_socket", "org.rdk.dobby.processcontainers"); if (!mIpcService) { TRACE(Trace::Error, (_T("Failed to create Dobby IPC service"))); - return; + return Core::ERROR_GENERAL; } else { // Start the IPCService which kicks off the event dispatcher thread mIpcService->start(); @@ -90,15 +86,15 @@ namespace ProcessContainers { // Create a DobbyProxy remote service that wraps up the dbus API // calls to the Dobby daemon mDobbyProxy = std::make_shared(mIpcService, DOBBY_SERVICE, DOBBY_OBJECT); - } - DobbyContainerAdministrator::~DobbyContainerAdministrator() - { + return Core::ERROR_NONE; } - void DobbyContainerAdministrator::Logging(const string& logPath VARIABLE_IS_NOT_USED, const string& loggingOptions VARIABLE_IS_NOT_USED) + void DobbyContainerAdministrator::Deinitialize() { - // Only container-scope logging + mDobbyProxy.reset(); + mIpcService->stop(); + mIpcService.reset(); } void DobbyContainerAdministrator::DestroyContainer(const string& name) @@ -115,7 +111,7 @@ namespace ProcessContainers { // Dobby stop is async - block until we get the notification the container // has actually stopped - this->InternalLock(); + // this->InternalLock(); _stopPromise = std::promise(); const void* vp = static_cast(new std::string(name)); @@ -139,7 +135,7 @@ namespace ProcessContainers { } } - this->InternalUnlock(); + // this->InternalUnlock(); // Always make sure we unregister our callback mDobbyProxy->unregisterListener(listenerId); @@ -186,8 +182,9 @@ namespace ProcessContainers { // Container // ------------------------------------ - DobbyContainer::DobbyContainer(const string& name, const string& path, const string& logPath, bool useSpecFile) + DobbyContainer::DobbyContainer(DobbyContainerAdministrator& admin, const string& name, const string& path, const string& logPath, const bool useSpecFile) : _adminLock() + , _admin(admin) , _name(name) , _path(path) , _logPath(logPath) @@ -199,13 +196,9 @@ namespace ProcessContainers { DobbyContainer::~DobbyContainer() { - auto& admin = static_cast(DobbyContainerAdministrator::Instance()); - - if (admin.ContainerNameTaken(_name) == true) { + if (_admin.ContainerNameTaken(_name) == true) { Stop(Core::infinite); } - - admin.RemoveContainer(this); } const string& DobbyContainer::Id() const @@ -216,10 +209,9 @@ namespace ProcessContainers { uint32_t DobbyContainer::Pid() const { uint32_t returnedPid = 0; - auto& admin = static_cast(DobbyContainerAdministrator::Instance()); if (_pid.IsSet() == false) { - std::string containerInfoString = admin.mDobbyProxy->getContainerInfo(_descriptor); + std::string containerInfoString = _admin.mDobbyProxy->getContainerInfo(_descriptor); if (containerInfoString.empty()) { TRACE(Trace::Warning, (_T("Failed to get info for container %s"), _name.c_str())); @@ -315,10 +307,9 @@ namespace ProcessContainers { bool DobbyContainer::IsRunning() const { bool result = false; - auto& admin = static_cast(DobbyContainerAdministrator::Instance()); // We got a state back successfully, work out what that means in English - switch (static_cast(admin.mDobbyProxy->getContainerState(_descriptor))) { + switch (static_cast(_admin.mDobbyProxy->getContainerState(_descriptor))) { case IDobbyProxyEvents::ContainerState::Invalid: result = false; break; @@ -350,8 +341,6 @@ namespace ProcessContainers { bool result = false; _adminLock.Lock(); - auto& admin = static_cast(DobbyContainerAdministrator::Instance()); - std::list emptyList; // construct the full command to run with all the arguments @@ -372,9 +361,9 @@ namespace ProcessContainers { std::string containerSpecString = specFileStream.str(); TRACE(ProcessContainers::ProcessContainerization, (_T("container spec string: %s"), containerSpecString.c_str())); - _descriptor = admin.mDobbyProxy->startContainerFromSpec(_name, containerSpecString , emptyList, fullCommand); + _descriptor = _admin.mDobbyProxy->startContainerFromSpec(_name, containerSpecString , emptyList, fullCommand); } else { - _descriptor = admin.mDobbyProxy->startContainerFromBundle(_name, _path, emptyList, fullCommand); + _descriptor = _admin.mDobbyProxy->startContainerFromBundle(_name, _path, emptyList, fullCommand); } // startContainer returns -1 on failure if (_descriptor <= 0) { @@ -395,9 +384,8 @@ namespace ProcessContainers { bool result = false; _adminLock.Lock(); - auto& admin = static_cast(DobbyContainerAdministrator::Instance()); - bool stoppedSuccessfully = admin.mDobbyProxy->stopContainer(_descriptor, false); + bool stoppedSuccessfully = _admin.mDobbyProxy->stopContainer(_descriptor, false); if (!stoppedSuccessfully) { TRACE(Trace::Error, (_T("Failed to stop container, internal Dobby error. id: %s descriptor: %d"), _name.c_str(), _descriptor)); @@ -409,6 +397,9 @@ namespace ProcessContainers { return result; } + // FACTORY REGISTRATION + static ContainerProducerRegistrationType registration; + } // namespace ProcessContainers } // namespace Thunder diff --git a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.h b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.h index f2cc3d315..55b91586a 100644 --- a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.h +++ b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.h @@ -19,13 +19,9 @@ #pragma once -#include "processcontainers/ProcessContainer.h" -#include "processcontainers/common/BaseAdministrator.h" -#include "processcontainers/common/BaseRefCount.h" +#include "processcontainers/ContainerAdministrator.h" #include "processcontainers/common/CGroupContainerInfo.h" -#include "Messaging.h" - #include #include @@ -37,30 +33,30 @@ namespace ProcessContainers { const string CONFIG_NAME = "/config.json"; const string CONFIG_NAME_SPEC = "/spec.json"; - class DobbyContainer : public BaseRefCount { - private: - friend class DobbyContainerAdministrator; - - DobbyContainer(const string& name, const string& path, const string& logPath, bool useSpecFile = false); + class DobbyContainerAdministrator; + class DobbyContainer : public IContainer { public: + DobbyContainer(DobbyContainerAdministrator& admin, const string& name, const string& path, const string& logPath, const bool useSpecFile = false); DobbyContainer(const DobbyContainer&) = delete; DobbyContainer& operator=(const DobbyContainer&) = delete; ~DobbyContainer() override; + public: // IContainerMethods + containertype Type() const override { return IContainer::DOBBY; } const string& Id() const override; uint32_t Pid() const override; bool IsRunning() const override; bool Start(const string& command, IStringIterator& parameters) override; bool Stop(const uint32_t timeout /*ms*/) override; - IMemoryInfo* Memory() const override; IProcessorInfo* ProcessorInfo() const override; INetworkInterfaceIterator* NetworkInterfaces() const override; private: mutable Core::CriticalSection _adminLock; + DobbyContainerAdministrator& _admin; string _name; string _path; string _logPath; @@ -69,29 +65,27 @@ namespace ProcessContainers { bool _useSpecFile; }; - class DobbyContainerAdministrator : public BaseContainerAdministrator { - private: + class DobbyContainerAdministrator : public IContainerProducer { friend class DobbyContainer; - friend class Core::SingletonType; - DobbyContainerAdministrator(); + public: + DobbyContainerAdministrator() = default; + ~DobbyContainerAdministrator() override = default; DobbyContainerAdministrator(const DobbyContainerAdministrator&) = delete; DobbyContainerAdministrator& operator=(const DobbyContainerAdministrator&) = delete; + private: std::shared_ptr mIpcService; // Ipc Service instance std::shared_ptr mDobbyProxy; // DobbyProxy instance - public: - ~DobbyContainerAdministrator() override; - IContainer* Container(const string& id, - IStringIterator& searchpaths, - const string& logpath, - const string& configuration) override; //searchpaths will be searched in order in which they are iterated - - // IContainerAdministrator methods - void Logging(const string& logDir, const string& loggingOptions) override; + public: + // IContainerProducer methods + uint32_t Initialize(const string& config) override; + void Deinitialize() override; + Core::ProxyType Container(const string& id, IStringIterator& searchpaths, + const string& logpath, const string& configuration) override; - protected: + private: void DestroyContainer(const string& name); // make sure that no leftovers from previous launch will cause crash bool ContainerNameTaken(const string& name); void containerStopCallback(int32_t cd, const std::string& containerId, From c6b16ef5c0983861089b5c6a450b2c0b347ac54c Mon Sep 17 00:00:00 2001 From: sebaszm Date: Thu, 10 Oct 2024 18:57:08 +0200 Subject: [PATCH 2/3] Remove commented out InternalLock calls --- .../DobbyImplementation/DobbyImplementation.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp index 7a7a2b903..dee89ded4 100644 --- a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp +++ b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp @@ -111,7 +111,6 @@ namespace ProcessContainers { // Dobby stop is async - block until we get the notification the container // has actually stopped - // this->InternalLock(); _stopPromise = std::promise(); const void* vp = static_cast(new std::string(name)); @@ -135,8 +134,6 @@ namespace ProcessContainers { } } - // this->InternalUnlock(); - // Always make sure we unregister our callback mDobbyProxy->unregisterListener(listenerId); From d98ed83bb1ff00469b2dd430f89643b2e3c97e4e Mon Sep 17 00:00:00 2001 From: sebaszm Date: Fri, 11 Oct 2024 08:51:57 +0200 Subject: [PATCH 3/3] Remove notice --- .../DobbyImplementation.cpp | 19 ------------------- .../DobbyImplementation/DobbyImplementation.h | 18 ------------------ 2 files changed, 37 deletions(-) diff --git a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp index dee89ded4..a0b6900c6 100644 --- a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp +++ b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.cpp @@ -1,22 +1,3 @@ -/* - * If not stated otherwise in this file or this component's LICENSE file the - * following copyright and licenses apply: - * - * Copyright 2020 Metrological - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - #include "processcontainers/Messaging.h" #include "processcontainers/ContainerProducer.h" diff --git a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.h b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.h index 55b91586a..9e020801b 100644 --- a/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.h +++ b/Source/extensions/processcontainers/implementations/DobbyImplementation/DobbyImplementation.h @@ -1,21 +1,3 @@ -/* - * If not stated otherwise in this file or this component's LICENSE file the - * following copyright and licenses apply: - * - * Copyright 2020 Metrological - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ #pragma once