diff --git a/cxx-client/CMakeLists.txt b/cxx-client/CMakeLists.txt index 7c3f13c4ce..f5cef9d7b6 100644 --- a/cxx-client/CMakeLists.txt +++ b/cxx-client/CMakeLists.txt @@ -75,6 +75,28 @@ target_link_libraries(BookkeepingApi target_compile_features(BookkeepingApi PUBLIC cxx_std_17) +add_library(BookkeepingProtoApi SHARED + src/BkpProtoClientFactory.cxx + src/grpc/GrpcBkpProtoClient.cxx + src/grpc/services/GrpcRunProtoClient.cxx + src/grpc/services/GrpcDplProcessExecutionProtoClient.cxx +) + +target_include_directories(BookkeepingProtoApi + PUBLIC $ # public header once installed + $ # to build it + PRIVATE $ # because of the proto generated files + $ # private headers +) + +target_link_libraries(BookkeepingProtoApi + PUBLIC BookkeepingProtos + PUBLIC protobuf::libprotobuf + PRIVATE gRPC::grpc++ +) + +target_compile_features(BookkeepingProtoApi PUBLIC cxx_std_17) + ### EXAMPLES add_executable(exampleSpecificService example/exampleSpecificServices.cxx) @@ -83,6 +105,14 @@ target_link_libraries(exampleSpecificService PUBLIC BookkeepingApi ) +add_executable(exampleProtoService example/exampleProtoServices.cxx) + +target_link_libraries(exampleProtoService + PUBLIC BookkeepingProtoApi +) + +target_include_directories(exampleProtoService PRIVATE $) + # PACKAGE INFO include(CMakePackageConfigHelpers) @@ -105,7 +135,7 @@ install(FILES DESTINATION "include" ) -install(TARGETS BookkeepingProtos BookkeepingApi +install(TARGETS BookkeepingProtos BookkeepingApi BookkeepingProtoApi EXPORT BookkeepingApiTargets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} diff --git a/cxx-client/cmake/BookkeepingProtoApiConfig.cmake.in b/cxx-client/cmake/BookkeepingProtoApiConfig.cmake.in new file mode 100644 index 0000000000..f799fa8033 --- /dev/null +++ b/cxx-client/cmake/BookkeepingProtoApiConfig.cmake.in @@ -0,0 +1,12 @@ +@PACKAGE_INIT@ +include(CMakeFindDependencyMacro) +get_filename_component(BookkeepingProtoApi_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) + +find_dependency(Protobuf CONFIG REQUIRED) +find_dependency(gRPC REQUIRED) + +if(NOT TARGET AliceO2::BookkeepingProtoApi) + include("${BookkeepingProtoApi_CMAKE_DIR}/BookkeepingProtoApiTargets.cmake") +endif() + +message(STATUS "BookkeepingProtoApi-O2 ${BookkeepingApi_VERSION} found") diff --git a/cxx-client/example/exampleProtoServices.cxx b/cxx-client/example/exampleProtoServices.cxx new file mode 100644 index 0000000000..d1c06ec49e --- /dev/null +++ b/cxx-client/example/exampleProtoServices.cxx @@ -0,0 +1,69 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#include +#include +#include +#include "BookkeepingApi/BkpProtoClientFactory.h" +#include "run.pb.h" +#include "dplProcessExecution.pb.h" + +using namespace o2::bkp::api::proto; +using namespace o2::bookkeeping; + +int main(int argc, char** argv) +{ + if (argc < 2) { + std::cerr << "You need to provide the gRPC URI as first argument" << std::endl; + exit(1); + } + + try { + auto client = BkpProtoClientFactory::create(argv[1]); + + // First option: direct implementation, using constructed request + auto request = std::make_shared(); + request->set_runnumber(106); + request->add_relations(RUN_RELATIONS_LHC_FILL); + std::shared_ptr run106WithRelations = client->run()->Get(request); + std::ostringstream messageStreamRun106; + messageStreamRun106 << "Retrieved run 106 info, such as time o2 start <" << run106WithRelations->run().timeo2start() << ">"; + if (run106WithRelations->has_lhcfill()) { + messageStreamRun106 << " and related fill info such as fill beam type <" << run106WithRelations->lhcfill().beamtype() << ">"; + } + std::cout << messageStreamRun106.str() << std::endl; + + // Second option: use shortcut method + std::shared_ptr run105WithRelations = client->run()->Get(105, { RUN_RELATIONS_LHC_FILL }); + std::ostringstream messageStreamRun105; + messageStreamRun105 << "Retrieved run 105 info, such as time o2 start <" << run106WithRelations->run().timeo2start() << ">"; + if (run106WithRelations->has_lhcfill()) { + messageStreamRun105 << " and related fill info such as fill beam type <" << run106WithRelations->lhcfill().beamtype() << ">"; + } + std::cout << messageStreamRun105.str() << std::endl; + + // Test of DPL process execution + auto creationRequest = std::make_shared(); + creationRequest->set_runnumber(106); + creationRequest->set_detectorname("DETECTOR"); + creationRequest->set_processname("PROCESS-NAME"); + creationRequest->set_type(o2::bookkeeping::DPL_PROCESS_TYPE_MERGER); + creationRequest->set_hostname("HOSTNAME"); + std::shared_ptr dplProcessExecution = client->dplProcessExecution()->Create(creationRequest); + + // Short version + client->dplProcessExecution()->registerProcessExecution(106, o2::bookkeeping::DPL_PROCESS_TYPE_QC_CHECKER, "SECOND-HOSTNAME", "PROCESS-NAME", "", "DEFAUlT"); + } catch (std::runtime_error& error) { + std::cerr << "An error occurred: " << error.what() << std::endl; + exit(2); + } + return 0; +} diff --git a/cxx-client/include/BookkeepingApi/BkpProtoClient.h b/cxx-client/include/BookkeepingApi/BkpProtoClient.h new file mode 100644 index 0000000000..3834cadac2 --- /dev/null +++ b/cxx-client/include/BookkeepingApi/BkpProtoClient.h @@ -0,0 +1,32 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#ifndef CXX_CLIENT_BOOKKEEPINGAPI_BKPPROTOCLIENT_H +#define CXX_CLIENT_BOOKKEEPINGAPI_BKPPROTOCLIENT_H + +#include +#include "RunProtoClient.h" +#include "DplProcessExecutionProtoClient.h" + +namespace o2::bkp::api::proto { +class BkpProtoClient { + public: + virtual ~BkpProtoClient() = default; + + /// Returns the implementation of the Run service defined in run.proto + virtual const std::unique_ptr& run() const = 0; + + /// Returns the implementation of the DPL process execution service defined in dpl-process-execution.proto + virtual const std::unique_ptr& dplProcessExecution() const = 0; +}; +} + +#endif // CXX_CLIENT_BOOKKEEPINGAPI_BKPPROTOCLIENT_H diff --git a/cxx-client/include/BookkeepingApi/BkpProtoClientFactory.h b/cxx-client/include/BookkeepingApi/BkpProtoClientFactory.h new file mode 100644 index 0000000000..babf14b4e8 --- /dev/null +++ b/cxx-client/include/BookkeepingApi/BkpProtoClientFactory.h @@ -0,0 +1,22 @@ +// +// Created by mboulais on 02/03/23. +// + +#ifndef CXX_CLIENT_BOOKKEEPINGAPI_BKPPROTOCLIENTFACTORY_H +#define CXX_CLIENT_BOOKKEEPINGAPI_BKPPROTOCLIENTFACTORY_H + +#include +#include "BkpProtoClient.h" + +namespace o2::bkp::api::proto { +class BkpProtoClientFactory { + public: + BkpProtoClientFactory() = delete; + + /// Provides a Bookkeeping proto API client configured from a given configuration URI + /// Proto api implements the services defined in the proto files + static ::std::unique_ptr create(const ::std::string& gRPCUri); +}; +} + +#endif // CXX_CLIENT_BOOKKEEPINGAPI_BKPPROTOCLIENTFACTORY_H diff --git a/cxx-client/include/BookkeepingApi/DplProcessExecutionProtoClient.h b/cxx-client/include/BookkeepingApi/DplProcessExecutionProtoClient.h new file mode 100644 index 0000000000..5ac7688062 --- /dev/null +++ b/cxx-client/include/BookkeepingApi/DplProcessExecutionProtoClient.h @@ -0,0 +1,40 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#ifndef CXX_CLIENT_BOOKKEEPINGAPI_DPLPROCESSEXECUTIONPROTOCLIENT_H +#define CXX_CLIENT_BOOKKEEPINGAPI_DPLPROCESSEXECUTIONPROTOCLIENT_H + +#include +#include "dplProcessExecution.pb.h" + +namespace o2::bkp::api::proto +{ +class DplProcessExecutionProtoClient +{ + public: + virtual ~DplProcessExecutionProtoClient() = default; + + /// Returns the implementation of the DPL process execution service defined in dplProcessExecution.proto + virtual std::shared_ptr Create(std::shared_ptr request) = 0; + + /// Register the execution fo a DPL process + virtual void registerProcessExecution( + int runNumber, + o2::bookkeeping::DplProcessType type, + std::string hostname, + std::string deviceId, + std::string args, + std::string detector + ) = 0; +}; +} // namespace o2::bkp::api::proto + +#endif // CXX_CLIENT_BOOKKEEPINGAPI_DPLPROCESSEXECUTIONPROTOCLIENT_H diff --git a/cxx-client/include/BookkeepingApi/RunProtoClient.h b/cxx-client/include/BookkeepingApi/RunProtoClient.h new file mode 100644 index 0000000000..46c26cefa8 --- /dev/null +++ b/cxx-client/include/BookkeepingApi/RunProtoClient.h @@ -0,0 +1,26 @@ +// +// Created by mboulais on 02/03/23. +// + +#ifndef CXX_CLIENT_BOOKKEEPINGAPI_RUNPROTOCLIENT_H +#define CXX_CLIENT_BOOKKEEPINGAPI_RUNPROTOCLIENT_H + +#include +#include "run.pb.h" + +namespace o2::bkp::api::proto +{ +class RunProtoClient +{ + public: + virtual ~RunProtoClient() = default; + + /// Returns the run corresponding to the given run number with optionally its relations + virtual std::shared_ptr Get(const int runNumber, const std::vector& relations) = 0; + + /// Returns the run and the asked relations defined in the given request + virtual std::shared_ptr Get(std::shared_ptr request) = 0; +}; +} // namespace o2::bkp::api::proto + +#endif // CXX_CLIENT_BOOKKEEPINGAPI_RUNPROTOCLIENT_H diff --git a/cxx-client/src/BkpProtoClientFactory.cxx b/cxx-client/src/BkpProtoClientFactory.cxx new file mode 100644 index 0000000000..45ece34a3d --- /dev/null +++ b/cxx-client/src/BkpProtoClientFactory.cxx @@ -0,0 +1,10 @@ +#include "BookkeepingApi/BkpProtoClientFactory.h" +#include "grpc/GrpcBkpProtoClient.h" + +namespace o2::bkp::api::proto +{ +std::unique_ptr BkpProtoClientFactory::create(const std::string& gRPCUri) +{ + return std::make_unique(gRPCUri); +} +}; // namespace o2::bkp::api \ No newline at end of file diff --git a/cxx-client/src/grpc/GrpcBkpProtoClient.cxx b/cxx-client/src/grpc/GrpcBkpProtoClient.cxx new file mode 100644 index 0000000000..407b5ab741 --- /dev/null +++ b/cxx-client/src/grpc/GrpcBkpProtoClient.cxx @@ -0,0 +1,40 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#include "GrpcBkpProtoClient.h" +#include +#include +#include "grpc/services/GrpcRunProtoClient.h" +#include "grpc/services/GrpcDplProcessExecutionProtoClient.h" + +using grpc::InsecureChannelCredentials; +using o2::bkp::api::proto::RunProtoClient; +using std::make_unique; + +namespace o2::bkp::api::proto::grpc +{ +GrpcBkpProtoClient::GrpcBkpProtoClient(const std::string& uri) +{ + auto channel = CreateChannel(uri, InsecureChannelCredentials()); + mRunClient = make_unique(channel); + mDplProcessExecutionClient = make_unique(channel); +} + +const std::unique_ptr& GrpcBkpProtoClient::run() const +{ + return mRunClient; +} + +const std::unique_ptr& GrpcBkpProtoClient::dplProcessExecution() const +{ + return mDplProcessExecutionClient; +} +} // namespace o2::bkp::api::proto diff --git a/cxx-client/src/grpc/GrpcBkpProtoClient.h b/cxx-client/src/grpc/GrpcBkpProtoClient.h new file mode 100644 index 0000000000..da33c8e53a --- /dev/null +++ b/cxx-client/src/grpc/GrpcBkpProtoClient.h @@ -0,0 +1,35 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#ifndef CXX_CLIENT_BOOKKEEPINGAPI_GRPCBKPPROTOCLIENT_H +#define CXX_CLIENT_BOOKKEEPINGAPI_GRPCBKPPROTOCLIENT_H + +#include "BookkeepingApi/BkpProtoClient.h" + +namespace o2::bkp::api::proto::grpc +{ +class GrpcBkpProtoClient : public BkpProtoClient +{ + public: + explicit GrpcBkpProtoClient(const std::string& uri); + ~GrpcBkpProtoClient() override = default; + + const std::unique_ptr& run() const override; + + const std::unique_ptr& dplProcessExecution() const override; + + private: + std::unique_ptr<::o2::bkp::api::proto::RunProtoClient> mRunClient; + std::unique_ptr<::o2::bkp::api::proto::DplProcessExecutionProtoClient> mDplProcessExecutionClient; +}; +} // namespace o2::bkp::api::proto + +#endif // CXX_CLIENT_BOOKKEEPINGAPI_GRPCBKPPROTOCLIENT_H diff --git a/cxx-client/src/grpc/services/GrpcDplProcessExecutionProtoClient.cxx b/cxx-client/src/grpc/services/GrpcDplProcessExecutionProtoClient.cxx new file mode 100644 index 0000000000..6cb26e145f --- /dev/null +++ b/cxx-client/src/grpc/services/GrpcDplProcessExecutionProtoClient.cxx @@ -0,0 +1,56 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#include "GrpcDplProcessExecutionProtoClient.h" + +using grpc::ClientContext; +using o2::bookkeeping::DplProcessType; +using o2::bookkeeping::DplProcessExecution; +using o2::bookkeeping::DplProcessExecutionService; +using o2::bookkeeping::DplProcessExecutionCreationRequest; + +namespace o2::bkp::api::proto::grpc::services +{ +GrpcDplProcessExecutionProtoClient::GrpcDplProcessExecutionProtoClient(const std::shared_ptr<::grpc::ChannelInterface>& channel) +{ + mStub = DplProcessExecutionService::NewStub(channel); +} + +std::shared_ptr GrpcDplProcessExecutionProtoClient::Create(std::shared_ptr request) +{ + ClientContext context; + auto response = std::make_shared(); + + auto status = mStub->Create(&context, *request, response.get()); + + if (!status.ok()) { + throw std::runtime_error(status.error_message()); + } + return response; +} +void GrpcDplProcessExecutionProtoClient::registerProcessExecution( + int runNumber, + DplProcessType type, + std::string hostname, + std::string deviceId, + std::string args, + std::string detector) +{ + auto request = std::make_shared(); + request->set_runnumber(runNumber); + request->set_detectorname(detector); + request->set_processname(deviceId); + request->set_type(type); + request->set_hostname(hostname); + + Create(request); +} +} // namespace o2::bkp::api::proto::grpc::services diff --git a/cxx-client/src/grpc/services/GrpcDplProcessExecutionProtoClient.h b/cxx-client/src/grpc/services/GrpcDplProcessExecutionProtoClient.h new file mode 100644 index 0000000000..a4f2262065 --- /dev/null +++ b/cxx-client/src/grpc/services/GrpcDplProcessExecutionProtoClient.h @@ -0,0 +1,40 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +#ifndef CXX_CLIENT_BOOKKEEPINGAPI_GRPC_SERVICES_GRPCDPLPROCESSEXECUTIONPROTOCLIENT_H +#define CXX_CLIENT_BOOKKEEPINGAPI_GRPC_SERVICES_GRPCDPLPROCESSEXECUTIONPROTOCLIENT_H + +#include "BookkeepingApi/DplProcessExecutionProtoClient.h" +#include "dplProcessExecution.grpc.pb.h" + +namespace o2::bkp::api::proto::grpc::services +{ +class GrpcDplProcessExecutionProtoClient : public ::o2::bkp::api::proto::DplProcessExecutionProtoClient +{ + public: + explicit GrpcDplProcessExecutionProtoClient(const std::shared_ptr<::grpc::ChannelInterface>& channel); + + std::shared_ptr Create(std::shared_ptr request) override; + + void registerProcessExecution( + int runNumber, + o2::bookkeeping::DplProcessType type, + std::string hostname, + std::string deviceId, + std::string args, + std::string detector) override; + + private: + std::unique_ptr mStub; +}; +} // namespace o2::bkp::api::proto::grpc::services + +#endif // CXX_CLIENT_BOOKKEEPINGAPI_GRPC_SERVICES_GRPCDPLPROCESSEXECUTIONPROTOCLIENT_H diff --git a/cxx-client/src/grpc/services/GrpcRunProtoClient.cxx b/cxx-client/src/grpc/services/GrpcRunProtoClient.cxx new file mode 100644 index 0000000000..04aa2844c5 --- /dev/null +++ b/cxx-client/src/grpc/services/GrpcRunProtoClient.cxx @@ -0,0 +1,43 @@ +// +// Created by mboulais on 02/03/23. +// + +#include "GrpcRunProtoClient.h" + +using grpc::ClientContext; +using o2::bookkeeping::RunWithRelations; +using o2::bookkeeping::Run; +using o2::bookkeeping::RunFetchRequest; +using o2::bookkeeping::RunService; + +namespace o2::bkp::api::proto::grpc::services +{ +GrpcRunProtoClient::GrpcRunProtoClient(const std::shared_ptr<::grpc::ChannelInterface>& channel) +{ + mStub = RunService::NewStub(channel); +} + +std::shared_ptr GrpcRunProtoClient::Get(const int runNumber, const std::vector& relations) +{ + auto request = std::make_shared(); + request->set_runnumber(runNumber); + for (auto relation: relations) { + request->add_relations(relation); + } + + return Get(request); +} + +std::shared_ptr GrpcRunProtoClient::Get(std::shared_ptr request) +{ + ClientContext context; + auto response = std::make_shared(); + + auto status = mStub->Get(&context, *request, response.get()); + + if (!status.ok()) { + throw std::runtime_error(status.error_message()); + } + return response; +} +} // namespace o2::bkp::api::proto::grpc::services diff --git a/cxx-client/src/grpc/services/GrpcRunProtoClient.h b/cxx-client/src/grpc/services/GrpcRunProtoClient.h new file mode 100644 index 0000000000..a7d7dbb027 --- /dev/null +++ b/cxx-client/src/grpc/services/GrpcRunProtoClient.h @@ -0,0 +1,26 @@ +// +// Created by mboulais on 02/03/23. +// + +#ifndef BOOKKEEPINGAPI_GRPC_SERVICES_GRPCRUNPROTOCLIENT_H +#define BOOKKEEPINGAPI_GRPC_SERVICES_GRPCRUNPROTOCLIENT_H + +#include "BookkeepingApi/RunProtoClient.h" +#include "run.grpc.pb.h" + +namespace o2::bkp::api::proto::grpc::services +{ +class GrpcRunProtoClient : public ::o2::bkp::api::proto::RunProtoClient +{ + public: + explicit GrpcRunProtoClient(const std::shared_ptr<::grpc::ChannelInterface>& channel); + + std::shared_ptr Get(const int runNumber, const std::vector& relations) override; + std::shared_ptr Get(std::shared_ptr request) override; + + private: + std::unique_ptr mStub; +}; +} // namespace o2::bkp::api::proto + +#endif // BOOKKEEPINGAPI_GRPC_SERVICES_GRPCRUNPROTOCLIENT_H