2121#include < gen_cpp/cloud.pb.h>
2222
2323#include < algorithm>
24+ #include < cctype>
2425#include < charconv>
2526#include < chrono>
2627#include < numeric>
@@ -47,7 +48,7 @@ using namespace std::chrono;
4748namespace {
4849constexpr char pattern_str[] = " ^[a-zA-Z][0-9a-zA-Z_]*$" ;
4950
50- constexpr char SNAPSHOT_ENABLED_KEY [] = " enabled " ;
51+ constexpr char SNAPSHOT_STATUS_KEY [] = " status " ;
5152constexpr char SNAPSHOT_MAX_RESERVED_KEY[] = " max_reserved_snapshots" ;
5253constexpr char SNAPSHOT_INTERVAL_SECONDS_KEY[] = " snapshot_interval_seconds" ;
5354
@@ -1820,30 +1821,50 @@ std::pair<MetaServiceCode, std::string> handle_snapshot_switch(const std::string
18201821 const std::string& key,
18211822 const std::string& value,
18221823 InstanceInfoPB* instance) {
1823- if (value != " true" && value != " false" ) {
1824+ // Only allow "ENABLED" and "DISABLED" values (case insensitive)
1825+ std::string value_upper = value;
1826+ std::ranges::transform (value_upper, value_upper.begin (), ::toupper);
1827+
1828+ if (value_upper != " ENABLED" && value_upper != " DISABLED" ) {
18241829 return std::make_pair (MetaServiceCode::INVALID_ARGUMENT,
18251830 " Invalid value for enabled property: " + value +
1826- " , expected 'true ' or 'false' " +
1831+ " , expected 'ENABLED ' or 'DISABLED' (case insensitive) " +
18271832 " , instance_id: " + instance_id);
18281833 }
1834+
1835+ // Check if snapshot is not ready (UNSUPPORTED state)
18291836 if (instance->snapshot_switch_status () == SNAPSHOT_SWITCH_DISABLED) {
18301837 return std::make_pair (MetaServiceCode::INVALID_ARGUMENT,
18311838 " Snapshot not ready, instance_id: " + instance_id);
18321839 }
1833- if (value == " true" && instance->snapshot_switch_status () == SNAPSHOT_SWITCH_ON) {
1834- return std::make_pair (
1835- MetaServiceCode::INVALID_ARGUMENT,
1836- " Snapshot is already set to SNAPSHOT_SWITCH_ON, instance_id: " + instance_id);
1837- }
1838- if (value == " false" && instance->snapshot_switch_status () == SNAPSHOT_SWITCH_OFF) {
1840+
1841+ // Determine target status
1842+ SnapshotSwitchStatus target_status =
1843+ (value_upper == " ENABLED" ) ? SNAPSHOT_SWITCH_ON : SNAPSHOT_SWITCH_OFF;
1844+
1845+ // Check if the status is already set to the target value
1846+ if (instance->snapshot_switch_status () == target_status) {
1847+ std::string status_name = (target_status == SNAPSHOT_SWITCH_ON) ? " ENABLED" : " DISABLED" ;
18391848 return std::make_pair (
18401849 MetaServiceCode::INVALID_ARGUMENT,
1841- " Snapshot is already set to SNAPSHOT_SWITCH_OFF , instance_id: " + instance_id);
1850+ " Snapshot is already set to " + status_name + " , instance_id: " + instance_id);
18421851 }
1843- if (value == " true" ) {
1844- instance->set_snapshot_switch_status (SNAPSHOT_SWITCH_ON);
1845- } else {
1846- instance->set_snapshot_switch_status (SNAPSHOT_SWITCH_OFF);
1852+
1853+ // Set the new status
1854+ instance->set_snapshot_switch_status (target_status);
1855+
1856+ // Set default values when first enabling snapshot
1857+ if (target_status == SNAPSHOT_SWITCH_ON) {
1858+ if (!instance->has_snapshot_interval_seconds () ||
1859+ instance->snapshot_interval_seconds () == 0 ) {
1860+ instance->set_snapshot_interval_seconds (3600 );
1861+ LOG (INFO) << " Set default snapshot_interval_seconds to 3600 for instance "
1862+ << instance_id;
1863+ }
1864+ if (!instance->has_max_reserved_snapshot () || instance->max_reserved_snapshot () == 0 ) {
1865+ instance->set_max_reserved_snapshot (1 );
1866+ LOG (INFO) << " Set default max_reserved_snapshots to 1 for instance " << instance_id;
1867+ }
18471868 }
18481869
18491870 std::string msg = " Set snapshot enabled to " + value + " for instance " + instance_id;
@@ -1862,9 +1883,11 @@ std::pair<MetaServiceCode, std::string> handle_max_reserved_snapshots(
18621883 return std::make_pair (MetaServiceCode::INVALID_ARGUMENT,
18631884 " max_reserved_snapshots must be non-negative, got: " + value);
18641885 }
1865- if (max_snapshots > 35 ) {
1886+ if (max_snapshots > config::snapshot_max_reserved_num ) {
18661887 return std::make_pair (MetaServiceCode::INVALID_ARGUMENT,
1867- " max_reserved_snapshots too large, maximum is 35, got: " + value);
1888+ " max_reserved_snapshots too large, maximum is " +
1889+ std::to_string (config::snapshot_max_reserved_num) +
1890+ " , got: " + value);
18681891 }
18691892 } catch (const std::exception& e) {
18701893 return std::make_pair (MetaServiceCode::INVALID_ARGUMENT,
@@ -1886,10 +1909,11 @@ std::pair<MetaServiceCode, std::string> handle_snapshot_intervals(const std::str
18861909 int intervals;
18871910 try {
18881911 intervals = std::stoi (value);
1889- if (intervals < 3600 ) {
1890- return std::make_pair (
1891- MetaServiceCode::INVALID_ARGUMENT,
1892- " snapshot_intervals too small, minimum is 3600 seconds, got: " + value);
1912+ if (intervals < config::snapshot_min_interval_seconds) {
1913+ return std::make_pair (MetaServiceCode::INVALID_ARGUMENT,
1914+ " snapshot_intervals too small, minimum is " +
1915+ std::to_string (config::snapshot_min_interval_seconds) +
1916+ " seconds, got: " + value);
18931917 }
18941918 } catch (const std::exception& e) {
18951919 return std::make_pair (MetaServiceCode::INVALID_ARGUMENT,
@@ -2097,8 +2121,8 @@ void MetaServiceImpl::alter_instance(google::protobuf::RpcController* controller
20972121 * Handle SET_SNAPSHOT_PROPERTY operation - configures snapshot-related properties for an instance.
20982122 *
20992123 * Supported property keys and their expected values:
2100- * - "enabled ": "true " | "false"
2101- * Controls whether snapshot functionality is enabled for the instance
2124+ * - "status ": "UNSUPPORTED " | "ENABLED" | "DISABLED"
2125+ * Controls the snapshot functionality status for the instance
21022126 *
21032127 * - "max_reserved_snapshots": numeric string (0-35)
21042128 * Sets the maximum number of snapshots to retain for the instance
@@ -2124,7 +2148,7 @@ void MetaServiceImpl::alter_instance(google::protobuf::RpcController* controller
21242148
21252149 std::pair<MetaServiceCode, std::string> result;
21262150
2127- if (key == SNAPSHOT_ENABLED_KEY ) {
2151+ if (key == SNAPSHOT_STATUS_KEY ) {
21282152 result = handle_snapshot_switch (request->instance_id (), key, value, instance);
21292153 } else if (key == SNAPSHOT_MAX_RESERVED_KEY) {
21302154 result = handle_max_reserved_snapshots (request->instance_id (), key, value,
@@ -4277,6 +4301,7 @@ void MetaServiceImpl::get_cluster_status(google::protobuf::RpcController* contro
42774301void notify_refresh_instance (std::shared_ptr<TxnKv> txn_kv, const std::string& instance_id,
42784302 KVStats* stats) {
42794303 LOG (INFO) << " begin notify_refresh_instance" ;
4304+ TEST_SYNC_POINT_RETURN_WITH_VOID (" notify_refresh_instance_return" );
42804305 std::unique_ptr<Transaction> txn;
42814306 TxnErrorCode err = txn_kv->create_txn (&txn);
42824307 if (err != TxnErrorCode::TXN_OK) {
0 commit comments