zookeeper C API
生活随笔
收集整理的這篇文章主要介紹了
zookeeper C API
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
typedef void (*watcher_fn)(zhandle_t *zh, int type, int state, const char *path,void *watcherCtx);
watch回調(diào)函數(shù),兩種watch事件通知方式:
1.legacy:預(yù)先實(shí)現(xiàn)watch回調(diào)函數(shù)并將函數(shù)指針傳入zookeeper_init,然后使用其他api設(shè)置watch。 (即在init函數(shù)中設(shè)置watcher,然后后邊調(diào)用時(shí),設(shè)置整型值watch來表明使用或者不使用watcher)
2.watcher object:一個(gè)函數(shù)指針和一個(gè)watcher上下文指針。watch觸發(fā)時(shí),兩者結(jié)合調(diào)用。使用此類型,需使用'w'前綴api,如zoo_awexists、zoo_wget等.(直接使用回調(diào)函數(shù))
zhandle_t *zookeeper_init(const char *host, watcher_fn fn, int recv_timeout, const clientid_t *clientid, void *context, int flags);
創(chuàng)建與ZooKeeper服務(wù)端通信的句柄和對(duì)應(yīng)于此句柄的會(huì)話。會(huì)話的創(chuàng)建過程是異步的,收到ZOO_CONNECTED_STATE狀態(tài)事件后,確認(rèn)會(huì)話成功建立。 int zookeeper_close(zhandle_t *zh);
關(guān)閉句柄,釋放資源。調(diào)用函數(shù)后,會(huì)話將不可用,函數(shù)返回前會(huì)將未發(fā)送完畢的請(qǐng)求發(fā)送完,所以可能會(huì)引起阻塞。
對(duì)一個(gè)句柄來說,這個(gè)方法只許調(diào)用一次,調(diào)用多次將產(chǎn)生不確定的結(jié)果。對(duì)于調(diào)用過此方法的句柄,其他句柄操作也將產(chǎn)生不確定的結(jié)果。const clientid_t *zoo_client_id(zhandle_t *zh);
返回客戶端會(huì)話id,僅在與服務(wù)端連接正常時(shí)有效int zoo_recv_timeout(zhandle_t *zh);
返回會(huì)話超時(shí)時(shí)間,僅在于服務(wù)端連接正常時(shí)有效,該值在與服務(wù)器重連后可能改變const void *zoo_get_context(zhandle_t *zh);
返回句柄上下文void zoo_set_context(zhandle_t *zh, void *context);
設(shè)置句柄上下文watcher_fn zoo_set_watcher(zhandle_t *zh,watcher_fn newFn);
設(shè)置watch回調(diào),返回之前的watch回調(diào)struct sockaddr* zookeeper_get_connected_host(zhandle_t *zh, struct sockaddr *addr, socklen_t *addr_len);
返回服務(wù)端的網(wǎng)絡(luò)地址(sockaddr結(jié)構(gòu)),僅在與服務(wù)端連接正常是有效int zookeeper_interest(zhandle_t *zh, int *fd, int *interest, struct timeval *tv);
暫時(shí)不太理解,可能是返回zookeeper在監(jiān)聽某個(gè)fd的讀或者寫int zookeeper_process(zhandle_t *zh, int events);
暫時(shí)不太理解,通知zookeeper監(jiān)聽的事件發(fā)生了typedef void (*void_completion_t)(int rc, const void *data);
函數(shù)類型定義,異步調(diào)用或連接斷開或連接超時(shí)執(zhí)行的回調(diào)類型typedef void (*stat_completion_t)(int rc, const struct Stat *stat, const void *data);
同上,有返回值typedef void (*data_completion_t)(int rc, const char *value, int value_len, const struct Stat *stat, const void *data);
同上,返回詳細(xì)數(shù)據(jù)typedef void (*strings_completion_t)(int rc, const struct String_vector *strings, const void *data);
同上typedef void (*strings_stat_completion_t)(int rc, const struct String_vector *strings, const struct Stat *stat, const void *data);
同上typedef void (*string_completion_t)(int rc, const char *value, const void *data);
同上typedef void (*acl_completion_t)(int rc, struct ACL_vector *acl, struct Stat *stat, const void *data);
同上int zoo_state(zhandle_t *zh);
返回句柄狀態(tài)int zoo_acreate(zhandle_t *zh, const char *path, const char *value, int valuelen, const struct ACL_vector *acl, int flags, string_completion_t completion, const void *data);
創(chuàng)建一個(gè)之前不存在的節(jié)點(diǎn)。如果設(shè)置ZOO_EPHEMERAL,客戶端會(huì)話失效,節(jié)點(diǎn)將自動(dòng)刪除;如果設(shè)置ZOO_SEQUENCE,一個(gè)唯一的自動(dòng)增加的序列號(hào)附加到路徑名,序列號(hào)寬度是10個(gè)數(shù)字的寬度,不足用0填充int zoo_adelete(zhandle_t *zh, const char *path, int version, void_completion_t completion, const void *data);
刪除一個(gè)節(jié)點(diǎn)int zoo_aexists(zhandle_t *zh, const char *path, int watch, stat_completion_t completion, const void *data);
檢查一個(gè)節(jié)點(diǎn)是否存在int zoo_awexists(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, stat_completion_t completion, const void *data);
檢查一個(gè)節(jié)點(diǎn)是否存在,它允許指定一個(gè)watcher對(duì)象(一個(gè)函數(shù)指針watcher和對(duì)應(yīng)的上下文watcherCtx),在watch解除時(shí),此函數(shù)會(huì)調(diào)用,watcherCtx作為watcher的傳入?yún)?shù)int zoo_aget(zhandle_t *zh, const char *path, int watch, data_completion_t completion, const void *data);
獲取節(jié)點(diǎn)數(shù)據(jù)(legacy方式)。completion是回調(diào)函數(shù),其rc參數(shù)可能是以下參數(shù):ZOK-完成,ZNONODE-節(jié)點(diǎn)不存在,ZNOAUTH-客戶端無權(quán)限int zoo_awget(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, data_completion_t completion, const void *data);
獲取節(jié)點(diǎn)數(shù)據(jù)(watcher object方式)。int zoo_aset(zhandle_t *zh, const char *path, const char *buffer, int buflen, int version, stat_completion_t completion, const void *data);
設(shè)置節(jié)點(diǎn)數(shù)據(jù)int zoo_aget_children(zhandle_t *zh, const char *path, int watch, strings_completion_t completion, const void *data);
獲取子節(jié)點(diǎn)列表(legacy)int zoo_awget_children(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, strings_completion_t completion, const void *data);
獲取子節(jié)點(diǎn)列表(watcher object)int zoo_aget_children2(zhandle_t *zh, const char *path, int watch, strings_stat_completion_t completion, const void *data);
獲取子節(jié)點(diǎn)列表,3.3.0版本加入(legacy)int zoo_awget_children2(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, strings_stat_completion_t completion, const void *data);
獲取子節(jié)點(diǎn)列表,3.3.0版本加入(watcher object)int zoo_async(zhandle_t *zh, const char *path, string_completion_t completion, const void *data);
Flush leader channel. 暫時(shí)不明確int zoo_aget_acl(zhandle_t *zh, const char *path, acl_completion_t completion, const void *data);
獲取節(jié)點(diǎn)的ACL。ACL描述了操作該節(jié)點(diǎn)所需具備的條件,即哪些人(id)具備哪些權(quán)限后才允許對(duì)節(jié)點(diǎn)執(zhí)行哪些操作。int zoo_aset_acl(zhandle_t *zh, const char *path, int version, struct ACL_vector *acl, void_completion_t, const void *data);
設(shè)置節(jié)點(diǎn)的ACLint zoo_amulti(zhandle_t *zh, int count, const zoo_op_t *ops, zoo_op_result_t *results, void_completion_t, const void *data);
以原子方式執(zhí)行一系列操作const char* zerror(int c);
返回錯(cuò)誤信息int zoo_add_auth(zhandle_t *zh, const char* scheme, const char* cert, int certLen, void_completion_t completion, const void *data);
為應(yīng)用程序指定證書。調(diào)用此函數(shù)用于認(rèn)證的證書。服務(wù)端用scheme指定的安全服務(wù)對(duì)客戶端連接進(jìn)行認(rèn)證。
如果認(rèn)證失敗,將與服務(wù)端斷開連接,watcher觸發(fā),狀態(tài)碼是ZOO_AUTH_FAILED_STATEint is_unrecoverable(zhandle_t *zh);
檢查zookeeper連接是否可恢復(fù)void zoo_set_debug_level(ZooLogLevel logLevel);
設(shè)置調(diào)試級(jí)別void zoo_set_log_stream(FILE* logStream);
設(shè)置用于記錄日志的文件流。默認(rèn)使用stderr。若logStream為NULL,則使用默認(rèn)值stderr。void zoo_deterministic_conn_order(int yesOrNo);
用于啟用或停用quarum端點(diǎn)的隨機(jī)化排序,通常僅在測(cè)試時(shí)使用。
如果非0,使得client連接到quarum端按照被初始化的順序。
如果是0,zookeeper_init將變更端點(diǎn)順序,使得client連接分布在更優(yōu)的端點(diǎn)上。int zoo_create(zhandle_t *zh, const char *path, const char *value, int valuelen, const struct ACL_vector *acl, int flags, char *path_buffer, int path_buffer_len);
同步建立節(jié)點(diǎn)int zoo_delete(zhandle_t *zh, const char *path, int version);
同步刪除節(jié)點(diǎn)int zoo_exists(zhandle_t *zh, const char *path, int watch, struct Stat *stat);
同步檢查節(jié)點(diǎn)是否存在int zoo_wexists(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, struct Stat *stat);
同步檢查節(jié)點(diǎn)是否存在(watcher object)int zoo_get(zhandle_t *zh, const char *path, int watch, char *buffer, int* buffer_len, struct Stat *stat);
同步獲取節(jié)點(diǎn)數(shù)據(jù)(legacy)int zoo_wget(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, char *buffer, int* buffer_len, struct Stat *stat);
同步獲取節(jié)點(diǎn)數(shù)據(jù)(watcher object)int zoo_set(zhandle_t *zh, const char *path, const char *buffer, int buflen, int version);
同步設(shè)置節(jié)點(diǎn)數(shù)據(jù)int zoo_set2(zhandle_t *zh, const char *path, const char *buffer, int buflen, int version, struct Stat *stat);
同步設(shè)置節(jié)點(diǎn)數(shù)據(jù)并返回當(dāng)前節(jié)點(diǎn)的stat信息int zoo_get_children(zhandle_t *zh, const char *path, int watch, struct String_vector *strings);
同步獲取子節(jié)點(diǎn)列表(legacy)int zoo_wget_children(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, struct String_vector *strings);
同步獲取子節(jié)點(diǎn)列表(watcher object)int zoo_get_children2(zhandle_t *zh, const char *path, int watch, struct String_vector *strings, struct Stat *stat);
同步獲取子節(jié)點(diǎn)列表并返回當(dāng)前節(jié)點(diǎn)的stat信息(legacy),3.3.0版本加入int zoo_wget_children2(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, struct String_vector *strings, struct Stat *stat);
同步獲取子節(jié)點(diǎn)列表并返回當(dāng)前節(jié)點(diǎn)的stat信息(watcher object),3.3.0版本加入int zoo_get_acl(zhandle_t *zh, const char *path, struct ACL_vector *acl, struct Stat *stat);
同步獲取節(jié)點(diǎn)ACLint zoo_set_acl(zhandle_t *zh, const char *path, int version, const struct ACL_vector *acl);
同步設(shè)置節(jié)點(diǎn)ACLint zoo_multi(zhandle_t *zh, int count, const zoo_op_t *ops, zoo_op_result_t *results);
同步以原子方式執(zhí)行一系列操作
說明:keeper C API 大部分接口可以根據(jù)同步和異步特性分為兩類,同步接口以 zoo_* 開頭,異步接口以則以 zoo_a* 開頭。以w開頭的函數(shù)表明使用watcher回調(diào)函數(shù)而不是整型watch和zookeeper中注冊(cè)的watcher函數(shù)。
轉(zhuǎn)自 :?http://www.cnblogs.com/caosiyang/archive/2012/11/09/2763190.html
轉(zhuǎn)載于:https://www.cnblogs.com/catkins/p/5270464.html
總結(jié)
以上是生活随笔為你收集整理的zookeeper C API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EasyUI Combogrid Bug
- 下一篇: 删除临时表