diff --git a/deploy/process/utils.sh b/deploy/process/utils.sh index db9a1eb3a88dd15bf8e362834f90df5331eb1bfc..a50ab9dc5b8c4a25642f59eb16728ad1104695f0 100644 --- a/deploy/process/utils.sh +++ b/deploy/process/utils.sh @@ -77,12 +77,12 @@ function get_hostname() { } function get_ip_list() { - if [ "$(command -v ip)" ]; then - ip addr show | grep inet | grep -vw "127.0.0.1" | grep -vw "172.17.0.1" | grep -v inet6 | awk '{print $2}' | cut -f1 -d '/' + if [ "$(command -v python3)" ]; then + python3 -c "import socket; s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM);s.connect(('8.8.8.8', 80));print(s.getsockname()[0]);" elif [ "$(command -v ifconfig)" ]; then ifconfig | grep inet | grep -vw "127.0.0.1" | grep -vw "172.17.0.1" | grep -v inet6 | awk '{print $2}' - elif [ "$(command -v python3)" ]; then - python3 -c "import socket; print('\n'.join(set(e[4][0] for e in socket.getaddrinfo(socket.gethostname(), None))));" + elif [ "$(command -v ip)" ]; then + ip addr show | grep inet | grep -vw "127.0.0.1" | grep -vw "172.17.0.1" | grep -v inet6 | awk '{print $2}' | cut -f1 -d '/' else die "cannot get host ip, need \"ip\" or \"ifconfig\" or \"python3\"" fi diff --git a/example/custom_runtime/CMakeLists.txt b/example/custom_runtime/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..09361a91d0baf21fb11b0d861b55b7d136c02a56 --- /dev/null +++ b/example/custom_runtime/CMakeLists.txt @@ -0,0 +1,37 @@ +# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved. +# +# 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. + +cmake_minimum_required(VERSION 3.16.1) +# 项目信息 +project(worker CXX) +#c++ 17 +set(CMAKE_CXX_STANDARD 17) +message(STATUS "PROJECT_SOURCE_DIR=${PROJECT_SOURCE_DIR}") + +include_directories( + ${PROJECT_SOURCE_DIR}/openyuanrong/runtime/sdk/cpp/include +) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -pthread -w") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3 -pthread -w") + + +# 链接库搜索目录 +link_directories( + ${PROJECT_SOURCE_DIR}/openyuanrong/runtime/sdk/cpp/lib +) + +# 指定生成目标 +add_executable(worker worker.cpp) +target_link_libraries(worker yr-api) + diff --git a/example/custom_runtime/package.sh b/example/custom_runtime/package.sh new file mode 100644 index 0000000000000000000000000000000000000000..797b6e6be3832c8744aedbb0cf36717e188feba5 --- /dev/null +++ b/example/custom_runtime/package.sh @@ -0,0 +1,47 @@ +#!/bin/bash +BASE_DIR=$( + cd "$(dirname "$0")" + pwd +) + + +if [ ! -d $BASE_DIR/openyuanrong ]; then + wget https://build-logs.openeuler.openatom.cn:38080/temp-archived/openeuler/openYuanrong/yr_release/x86_64/0.6.0/openyuanrong-0.6.0.tar.gz + tar -xzf openyuanrong-0.6.0.tar.gz -C $BASE_DIR/ +fi + +rm -rf $BASE_DIR/build +mkdir $BASE_DIR/build +cd $BASE_DIR/build +cmake .. +make -j + +cd $BASE_DIR/ + +output_dir=$BASE_DIR/worker + +rm -rf $output_dir/yuanrong +cp -ar $BASE_DIR/openyuanrong $output_dir/yuanrong +rm -rf $output_dir/yuanrong/runtime/sdk/ +rm -rf $output_dir/yuanrong/runtime/service/go +rm -rf $output_dir/yuanrong/runtime/service/python +rm -rf $output_dir/yuanrong/runtime/service/java +rm -rf $output_dir/yuanrong/data_system/sdk +rm -rf $output_dir/yuanrong/data_system/service/DATASYSTEM_SYM +rm -rf $output_dir/yuanrong/pattern +rm -rf $output_dir/yuanrong/function_system/metrics +rm -rf $output_dir/yuanrong/function_system/bin/client +rm -rf $output_dir/yuanrong/function_system/bin/collector +rm -rf $output_dir/yuanrong/function_system/bin/dashboard +rm -rf $output_dir/yuanrong/function_system/bin/domain_scheduler +rm -rf $output_dir/yuanrong/function_system/bin/iam_server +rm -rf $output_dir/yuanrong/function_system/bin/runtime_manager +rm -rf $output_dir/yuanrong/function_system/cli + +rm -rf $output_dir/bin/worker +cp $BASE_DIR/build/worker $output_dir/bin/ +chmod +x $output_dir/bin/* + +cp $BASE_DIR/services.yaml $output_dir/yuanrong/deploy/process/ + +tar zcf worker.tar.gz worker \ No newline at end of file diff --git a/example/custom_runtime/services.yaml b/example/custom_runtime/services.yaml new file mode 100644 index 0000000000000000000000000000000000000000..f9546f537903073caa68033da0e34a5df3af96b2 --- /dev/null +++ b/example/custom_runtime/services.yaml @@ -0,0 +1,24 @@ +- service: defaultservice # 服务名(必填) + kind: yrlib # 函数类型(必填) 进程部署只支持 yrlib + description: this is the default service # 函数描述(非必填) + functions: # 函数(必填) + default: # 函数名(必填) + cpu: 0 # 函数 CPU 大小,单位:1/1000 核(必填) + memory: 0 # 函数 MEM 大小,单位:MB(必填) + runtime: python3.11 # 函数 runtime 类型(必填) + # environment: # 函数环境变量,内置环境变量定义 + # "key1" : "value1" + # storageType: "local" # 代码包存储类型,默认值为 local, 进程部署只支持 local: 代码包存储在磁盘中 + # codePath: "/home/sn/function-packages" # 代码包本地路径,默认值为空(除了 python,此项必填) + py: + cpu: 0 + memory: 0 + runtime: python3.11 + java: + cpu: 500 + memory: 500 + runtime: java1.8 + cpp: + cpu: 500 + memory: 500 + runtime: posix-custom-runtime \ No newline at end of file diff --git a/example/custom_runtime/worker.cpp b/example/custom_runtime/worker.cpp new file mode 100644 index 0000000000000000000000000000000000000000..468603685773f35e71dc29c9654b13f4371bb0e5 --- /dev/null +++ b/example/custom_runtime/worker.cpp @@ -0,0 +1,67 @@ +#include "yr/yr.h" + +#include +#include +#include + +class Worker { +public: + int Run() + { + return 0; + } + + static Worker *FactoryCreate() + { + return new Worker(); + } + YR_STATE(); +}; + +YR_INVOKE(Worker::FactoryCreate, &Worker::Run) + + +std::string getExecutablePath() { + char buffer[PATH_MAX]; + ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1); + if (len != -1) { + buffer[len] = '\0'; + return std::string(buffer); + } + return ""; +} + +int main(int argc, char *argv[]) +{ + if (getenv("YR_RUNTIME_ID") == nullptr) { + // driver + auto cfg = YR::Config(); + cfg.workingdir = std::filesystem::current_path(); + cfg.functionUrn = "sn:cn:yrk:12345678901234561234567890123456:function:0-defaultservice-cpp:$latest"; + YR::Init(cfg); + int workerNum = 2; + auto workerNumStr = getenv("WORKER_NUM"); + if (workerNumStr != nullptr) { + workerNum = std::stoi(workerNumStr); + } + std::vector> workers; + std::vector> results; + for (int i = 0; i < workerNum; i++) { + YR::InvokeOptions option; + option.cpu = 1000; + option.memory = 1024; + option.customExtensions["RUNTIME_ENTRYPOINT"] = getExecutablePath(); + workers.emplace_back(YR::Instance(Worker::FactoryCreate).Options(std::move(option)).Invoke()); + } + for (auto &worker : workers) { + results.emplace_back(worker.Function(&Worker::Run).Invoke()); + } + YR::Get(results); + std::cout << "execute finish." << std::endl; + YR::Finalize(); + } else { + // worker + YR::Run(argc, argv); + } + return 0; +} \ No newline at end of file diff --git a/example/custom_runtime/worker/bin/start b/example/custom_runtime/worker/bin/start new file mode 100755 index 0000000000000000000000000000000000000000..d4d126590f88ea3a9c457513c078f10777840317 --- /dev/null +++ b/example/custom_runtime/worker/bin/start @@ -0,0 +1,32 @@ +#!/bin/bash + +BASE_DIR=$( + cd "$(dirname "$0")" + pwd +) + +stop_yr() { + bash $YR_DIR/deploy/process/yr_stop.sh + exit 0 +} + +trap 'stop_yr' INT TERM ERR + +YR_DIR=$BASE_DIR/../yuanrong +export LD_LIBRARY_PATH=$YR_DIR/runtime/service/cpp/lib:$LD_LIBRARY_PATH + +DEPLOY_PATH=/tmp/yr_session/$(date -u "+%Y%m%d%H%M%S") +echo "deploy to $DEPLOY_PATH" +bash $YR_DIR/deploy/process/yr_master.sh \ + -c 2000 -m 4096 \ + -l DEBUG \ + --enable_inherit_env true \ + --enable_separated_redirect_runtime_std true \ + -o ./master.info -d $DEPLOY_PATH + +rm -rf ./deploy +ln -s $DEPLOY_PATH ./deploy + +export YR_MASTER_INFO_PATH=./master.info + +$BASE_DIR/worker diff --git a/src/dto/config.h b/src/dto/config.h index c5648b4869444d7760bca96eaa711eab9c553641..6b0d92978a7daa50467b2fd0ae4c7b972e893bf2 100644 --- a/src/dto/config.h +++ b/src/dto/config.h @@ -119,6 +119,8 @@ public: \ CONFIG_DECLARE(std::string, YR_SERVER_ADDRESS, ""); CONFIG_DECLARE(std::string, POSIX_LISTEN_ADDR, ""); CONFIG_DECLARE(std::string, YR_LOG_PATH, "./"); + CONFIG_DECLARE(std::string, YR_MASTER_INFO, ""); + CONFIG_DECLARE(std::string, YR_MASTER_INFO_PATH, "/tmp/yr_sessions/yr_current_master_info"); CONFIG_DECLARE(uint32_t, YR_MAX_LOG_SIZE_MB, 500); CONFIG_DECLARE(uint32_t, YR_MAX_LOG_FILE_NUM, 10); CONFIG_DECLARE(uint32_t, YR_HTTP_CONNECTION_NUM, 10); diff --git a/src/libruntime/auto_init.cpp b/src/libruntime/auto_init.cpp index d5639a32ec50564ace8167bb4b2616f3b88807b7..a807a35dcf3272d1e9a163c986c441b6b6132929 100644 --- a/src/libruntime/auto_init.cpp +++ b/src/libruntime/auto_init.cpp @@ -51,16 +51,20 @@ void ClusterAccessInfo::AutoParse() ParseFromMasterInfo(); } -void ClusterAccessInfo::ParseFromMasterInfo(const std::string &masterInfoPath) +void ClusterAccessInfo::ParseFromMasterInfo() { - std::ifstream masterInfoFile(masterInfoPath); - if (!masterInfoFile.is_open()) { - return; - } - std::string masterInfo; - if (!std::getline(masterInfoFile, masterInfo)) { - return; + if (!Config::Instance().YR_MASTER_INFO().empty()) { + masterInfo = Config::Instance().YR_MASTER_INFO(); + } else { + std::ifstream masterInfoFile(Config::Instance().YR_MASTER_INFO_PATH()); + if (!masterInfoFile.is_open()) { + return; + } + + if (!std::getline(masterInfoFile, masterInfo)) { + return; + } } std::map kvMap; @@ -252,7 +256,7 @@ void CommandRunner::RunCommandUntil(std::vector &args) bool fileExists = false; while (!fileExists && retries < MAX_RETRIES) { - fileExists = filesystem::exists(kDefaultDeployPathCurrMasterInfo); + fileExists = filesystem::exists(Config::Instance().YR_MASTER_INFO_PATH()); if (fileExists) { break; } diff --git a/src/libruntime/auto_init.h b/src/libruntime/auto_init.h index 4cf8e3cc4c6815c8de80d8d92890070dd0d72d79..84ed9c209f819ce2716e5535c137b4eccbac7c9b 100644 --- a/src/libruntime/auto_init.h +++ b/src/libruntime/auto_init.h @@ -61,9 +61,6 @@ Will try to get the cluster info in following steps, * and then get info the same as step 3. */ -const std::string kDefaultDeployPathBase = "/tmp/yr_sessions"; -const std::string kDefaultDeployPathCurrMasterInfo = kDefaultDeployPathBase + "/yr_current_master_info"; - const std::string kEnvYrServerAddress = "YR_SERVER_ADDRESS"; // address for posix invoke, can be frontend or bus const std::string kEnvYrDatasystemAddress = "YR_DS_ADDRESS"; // datasystem address @@ -82,7 +79,7 @@ public: public: // public methods void AutoParse(); - void ParseFromMasterInfo(const std::string &masterInfoPath = kDefaultDeployPathCurrMasterInfo); + void ParseFromMasterInfo(); private: void ParseFromEnv(); diff --git a/test/libruntime/auto_init_test.cpp b/test/libruntime/auto_init_test.cpp index 2b8461d2b8e30f74c236fbbaa4bf2bb898811ec8..195fbccdbb89385abcf5c33793c2c500283151c5 100644 --- a/test/libruntime/auto_init_test.cpp +++ b/test/libruntime/auto_init_test.cpp @@ -80,7 +80,7 @@ static void RemoveMasterInfoFile(const std::string &filepath) TEST_F(AutoInitTest, AutoCreateYuanRongClusterFailed) { - RemoveMasterInfoFile(YR::Libruntime::kDefaultDeployPathCurrMasterInfo); + RemoveMasterInfoFile(Config::Instance().YR_MASTER_INFO_PATH()); YR::Libruntime::ClusterAccessInfo info; auto info2 = YR::Libruntime::AutoGetClusterAccessInfo(info); @@ -91,7 +91,7 @@ TEST_F(AutoInitTest, AutoCreateYuanRongClusterFailed) TEST_F(AutoInitTest, AutoInitWithClusterAccessInfo) { - MakeMasterInfoFile(YR::Libruntime::kDefaultDeployPathCurrMasterInfo, masterInfoString); + MakeMasterInfoFile(Config::Instance().YR_MASTER_INFO_PATH(), masterInfoString); YR::Libruntime::ClusterAccessInfo info; auto info2 = YR::Libruntime::AutoGetClusterAccessInfo(info); @@ -103,7 +103,7 @@ TEST_F(AutoInitTest, AutoInitWithClusterAccessInfo) TEST_F(AutoInitTest, ParseFromMasterInfo) { - MakeMasterInfoFile(YR::Libruntime::kDefaultDeployPathCurrMasterInfo, masterInfoString); + MakeMasterInfoFile(Config::Instance().YR_MASTER_INFO_PATH(), masterInfoString); YR::Libruntime::ClusterAccessInfo info; info.ParseFromMasterInfo(); ASSERT_EQ(info.serverAddr, "127.0.0.1:34834");