YR_SHUTDOWN#
-
YR_SHUTDOWN(...)#
Users can use this interface to perform data cleanup or data persistence operations.
Functions to be executed during graceful shutdown must be decorated with YR_SHUTDOWN. The function must have exactly one parameter of type uint64_t. Otherwise, the function will fail to execute on the cloud due to parameter mismatch. These functions are used to decorate user-defined graceful shutdown member functions within actors executing in the cloud. These functions can utilize Yuanrong interfaces. Functions decorated with YR_SHUTDOWN will be executed in the following scenarios:
When the runtime receives a shutdown request (e.g., when a user calls the terminate interface in the cloud environment).
When the runtime captures a Sigterm signal (e.g., during cluster scaling down, where the runtime-manager captures the Sigterm signal and forwards it to the runtime).
class Counter { public: int count; Counter() {} explicit Counter(int init) : count(init) {} static Counter *FactoryCreate(int init) { return new Counter(init); } void Counter::MyShutdown(uint64_t gracePeriodSecond) { YR::KV().Set("myKey", "myValue"); } int Add(int x) { count += x; return count; } }; YR_INVOKE(Counter::FactoryCreate, &Counter::Add) YR_SHUTDOWN(&Counter::MyShutdown); int main(int argc, char **argv) { YR::KV().Del("myKey"); auto counter = YR::Instance(Counter::FactoryCreate).Invoke(1); auto ret = counter.Function(&Counter::Add).Invoke(1); std::cout << *YR::Get(ret) << std::endl; // 2 // Using the KV interface in the cloud to get the value mapped by `my_key` will return `'my_value'`. counter.Terminate(); std::string result = YR::KV().Get("myKey", 30); EXPECT_EQ(result, "myValue"); YR::KV().Del("myKey"); YR::InvokeOptions opt; opt.customExtensions.insert({"GRACEFUL_SHUTDOWN_TIME", "10"}); auto counter2 = YR::Instance(Counter::FactoryCreate).Options(opt).Invoke(1); auto ret2 = counter2.Function(&Counter::Add).Invoke(1); std::cout << *YR::Get(ret2) << std::endl; // 2 counter2.Terminate(); std::string result2 = YR::KV().Get("myKey", 30); EXPECT_EQ(result2, "myValue"); }
Note
When defining custom graceful shutdown functions, the function must have exactly one parameter named gracePeriodSeconds of type uint64_t. Otherwise, the function will fail to execute on the cloud due to parameter mismatch.
If the execution time of the custom graceful shutdown function exceeds gracePeriodSeconds seconds, the cloud instance will not wait for the function to complete and will be immediately recycled.