/**
* Indicates if shared memory should be used for cluster tending. Shared memory
* is useful when operating in single threaded mode with multiple client processes.
* This model is used by wrapper languages such as PHP and Python. When enabled,
* the data partition maps are maintained by only one process and all other processes
* use these shared memory maps.
*
* Shared memory should not be enabled for multi-threaded programs.
* Default: false
*/
bool use_shm;
/**
* Shared memory identifier. This identifier should be the same for all applications
* that use the Aerospike C client.
* Default: 0xA5000000
*/
int shm_key;
/**
* Shared memory maximum number of server nodes allowed. This value is used to size
* the fixed shared memory segment. Leave a cushion between actual server node
* count and shm_max_nodes so new nodes can be added without having to reboot the client.
* Default: 16
*/
uint32_t shm_max_nodes;
/**
* Shared memory maximum number of namespaces allowed. This value is used to size
* the fixed shared memory segment. Leave a cushion between actual namespaces
* and shm_max_namespaces so new namespaces can be added without having to reboot the
* client.
* Default: 8
*/
uint32_t shm_max_namespaces;
/**
* Take over shared memory cluster tending if the cluster hasn't been tended by this
* threshold in seconds.
* Default: 30
*/
uint32_t shm_takeover_threshold_sec;
啟用共享內存方式
共享內存方式需在aerospike_connect()之前啟用。
as_config config;
as_config_init(&config);
as_config_add_host(&config, host, port);
config.use_shm = true;
config.shm_key = 0xA5000000;
config.shm_max_nodes = 16;
config.shm_max_namespaces = 8;
config.shm_takeover_threshold_sec = 30;
aerospike client;
as_error err;
aerospike_init(&client, &config);
as_status status = aerospike_connect(&client, &err);
操作備注
若共享內存段不存在,客戶端會自動創建。
第一個客戶端進程通過獲得共享內存鎖,來占有集群侍服能力,。
後繼的客戶端進程從共享內存段讀取集群狀態,但不做集群侍服也不寫共享內存。
應用退出前應調用aerospike_close()。此方法釋放共享內存鎖。另一個客戶端進程會自動獲得鎖,成為新的群集侍服擁有者。
若應用在釋放鎖之前死掉,另一個客戶端進程仍將接管集群待服,但會有一個延遲(默認30秒)。在這個延遲期間集群狀態仍然可讀,但集群侍服(輪詢新的集群狀態)不會開始,直到另一個客戶端進程接管。
共享內存段可使用這個命令查看:
ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0xA5000000 622592 root 666 263224 0
若共享內存配置變量需要更改(比如:增加最大節點數),新的共享內存段要重新創建。有兩種方式升級應用。
一次升級
關閉所有應用。
若共享內存還存在,則刪除。
ipcrm -M 0xA5000000
重啟應用。
滾動升級
在新的應用中設置一個不同的共享內存key。
config.shm_key = 0xA5000001;
新應用實例使用新的共享內存段(由第一個新應用實例創建)。舊應用實例繼續使用舊共享內存段。
逐個停止舊應用實例,用新應用實例替代。
當所有舊應用實例停止後,刪除舊共享內存段(若還存在)。
ipcrm -M 0xA5000000