欢迎投稿

今日深度:

AerospikeC客户端手册———共享内存

AerospikeC客户端手册———共享内存




共享内存

每个客户端实例运行一个额外的集群侍服线程,周期地轮询集群各服务器节点以得到集群的状态。在一个单进程/多线程环境,只需要一个客户端实例。此实例被设计成跨多线程共享。
在一个多进程/单线程环境,需要多个客户端实例,意味着多个集群侍服线程轮询各服务器节点,并得到完全相同的集群状态。共享内存方式被设计出来,以解决这种效率低下情况。
启用共享方式时,集群状态(包括节点和数据分区映射)存放进一个共享内存段。然后,只有一个集群侍服拥有者进程轮询各服务节点,获取集群的状态,并将写入这个共享内存段。其它客户端从共享内存中读取集群状态,不再去轮询各服务器节点。
多进程/单线程环境是类PHP和Python等语言环境中常见的方式。Aerospike PHP和Python客户端实现,就是通过调用Aerospike C客户端,来获得客户端共享内存方式的能力。

共享内存配置

共享内存使用as_config的如下域成员来配置:
/**
 *  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

www.htsjk.Com true http://www.htsjk.com/DB2/20344.html NewsArticle AerospikeC客户端手册———共享内存 共享内存 每个客户端实例运行一个额外的集群侍服线程,周期地轮询集群各服务器节点以得到集群的状态。在一个单进程/多线程环境,只需要一个客...
评论暂时关闭