zookeeper c-client 源码分析

1.头文件
zookeeper.h
zookeeper_version.h
zookeeper_log.h
recordio.h
proto.h
  • proto.h 分析

定义行为代号。

static const int NOTIFY_OP=0;

static const int CREATE_OP=1;

static const int DELETE_OP=2;

static const int EXISTS_OP=3;

static const int GETDATA_OP=4;

static const int SETDATA_OP=5;

static const int GETACL_OP=6;

static const int SETACL_OP=7;

static const int GETCHILDREN_OP=8;

static const int SYNC_OP=9;

static const int PING_OP=11;

static const int GETCHILDREN2_OP=12;

static const int CLOSE_OP=-11;

static const int SETAUTH_OP=100;

static const int SETWATCHES_OP=101;

  • zookeeper_version.h 分析

定义版本号

#define ZOO_MAJOR_VERSION 3
#define ZOO_MINOR_VERSION 3
#define ZOO_PATCH_VERSION 5
  • recordio.h 分析
操作记录的协议,每个buffer为一个数据项。
struct buffer {
    int32_t len; //数据长度
    char *buff; //实际数据
};
void deallocate_String(char **s);
void deallocate_Buffer(struct buffer *b);
void deallocate_vector(void *d);
struct iarchive {
    int (*start_record)(struct iarchive *ia, const char *tag);
    int (*end_record)(struct iarchive *ia, const char *tag);
    int (*start_vector)(struct iarchive *ia, const char *tag, int32_t *count);
    int (*end_vector)(struct iarchive *ia, const char *tag);
    int (*deserialize_Bool)(struct iarchive *ia, const char *name, int32_t *);
    int (*deserialize_Int)(struct iarchive *ia, const char *name, int32_t *);
    int (*deserialize_Long)(struct iarchive *ia, const char *name, int64_t *);
    int (*deserialize_Buffer)(struct iarchive *ia, const char *name,
            struct buffer *);
    int (*deserialize_String)(struct iarchive *ia, const char *name, char **);
    void *priv;
};
struct oarchive {
    int (*start_record)(struct oarchive *oa, const char *tag);
    int (*end_record)(struct oarchive *oa, const char *tag);
    int (*start_vector)(struct oarchive *oa, const char *tag, const int32_t *count);
    int (*end_vector)(struct oarchive *oa, const char *tag);
    int (*serialize_Bool)(struct oarchive *oa, const char *name, const int32_t *);
    int (*serialize_Int)(struct oarchive *oa, const char *name, const int32_t *);
    int (*serialize_Long)(struct oarchive *oa, const char *name,
            const int64_t *);
    int (*serialize_Buffer)(struct oarchive *oa, const char *name,
            const struct buffer *);
    int (*serialize_String)(struct oarchive *oa, const char *name, char **);
    void *priv;
};
struct oarchive *create_buffer_oarchive(void);
void close_buffer_oarchive(struct oarchive **oa, int free_buffer);
struct iarchive *create_buffer_iarchive(char *buffer, int len);
void close_buffer_iarchive(struct iarchive **ia);
char *get_buffer(struct oarchive *);
int get_buffer_len(struct oarchive *);
int64_t htonll(int64_t v);
  • zookeeper_log.h 分析
extern ZOOAPI ZooLogLevel logLevel; //定义全变量
#define LOGSTREAM getLogStream() //宏定义,获取一个日志流
#define LOG_ERROR(x) if(logLevel>=ZOO_LOG_LEVEL_ERROR) \
    log_message(ZOO_LOG_LEVEL_ERROR,__LINE__,__func__,format_log_message x)
#define LOG_WARN(x) if(logLevel>=ZOO_LOG_LEVEL_WARN) \
    log_message(ZOO_LOG_LEVEL_WARN,__LINE__,__func__,format_log_message x)
#define LOG_INFO(x) if(logLevel>=ZOO_LOG_LEVEL_INFO) \
    log_message(ZOO_LOG_LEVEL_INFO,__LINE__,__func__,format_log_message x)
#define LOG_DEBUG(x) if(logLevel==ZOO_LOG_LEVEL_DEBUG) \
    log_message(ZOO_LOG_LEVEL_DEBUG,__LINE__,__func__,format_log_message x)
// 实际写日志操作
ZOOAPI void log_message(ZooLogLevel curLevel, int line,const char* funcName,
    const char* message);
ZOOAPI const char* format_log_message(const char* format,…);
FILE* getLogStream();
  • zookeeper.h 分析
error code 定义
ZOK 正常返回
ZSYSTEMERROR 系统或服务器端错误(System and server-side errors),服务器不会抛出该错误,该错误也只是用来标识错误范围的,即大于该错误值,且小于 ZAPIERROR 都是系统错误。
ZRUNTIMEINCONSISTENCY 运行时非一致性错误。
ZDATAINCONSISTENCY 数据非一致性错误。
ZCONNECTIONLOSS Zookeeper 客户端与服务器端失去连接
ZMARSHALLINGERROR 在 marshalling 和 unmarshalling 数据时出现错误(Error while marshalling or unmarshalling data)
ZUNIMPLEMENTED 该操作未实现(Operation is unimplemented)
ZOPERATIONTIMEOUT 该操作超时(Operation timeout)
ZBADARGUMENTS 非法参数错误(Invalid arguments)
ZINVALIDSTATE 非法句柄状态(Invliad zhandle state)
ZAPIERROR API 错误(API errors),服务器不会抛出该错误,该错误也只是用来标识错误范围的,错误值大于该值的标识 API 错误,而小于该值的标识 ZSYSTEMERROR。
ZNONODE 节点不存在(Node does not exist)
ZNOAUTH 没有经过授权(Not authenticated)
ZBADVERSION 版本冲突(Version conflict)
ZNOCHILDRENFOREPHEMERALS 临时节点不能拥有子节点(Ephemeral nodes may not have children)
ZNODEEXISTS 节点已经存在(The node already exists)
ZNOTEMPTY 该节点具有自身的子节点(The node has children)
ZSESSIONEXPIRED 会话过期(The session has been expired by the server)
ZINVALIDCALLBACK 非法的回调函数(Invalid callback specified)
ZINVALIDACL 非法的ACL(Invalid ACL specified)
ZAUTHFAILED 客户端授权失败(Client authentication failed)
ZCLOSING Zookeeper 连接关闭(ZooKeeper is closing)
ZNOTHING 并非错误,客户端不需要处理服务器的响应(not error, no server responses to process)
ZSESSIONMOVED 会话转移至其他服务器,所以操作被忽略(session moved to another server, so operation is ignored)

定义日志记录级别

/**

*  @name Debug levels

*/

typedef enum {ZOO_LOG_LEVEL_ERROR=1,ZOO_LOG_LEVEL_WARN=2,ZOO_LOG_LEVEL_INFO=3,ZOO_LOG_LEVEL_DEBUG=4} ZooLogLevel;

定义权限
extern ZOOAPI const int ZOO_PERM_READ;//允许客户端读取 znode 节点的值以及子节点列表。
extern ZOOAPI const int ZOO_PERM_WRITE;// 允许客户端设置 znode 节点的值。
extern ZOOAPI const int ZOO_PERM_CREATE; //允许客户端在该 znode 节点下创建子节点。
extern ZOOAPI const int ZOO_PERM_DELETE;//允许客户端删除子节点。
extern ZOOAPI const int ZOO_PERM_ADMIN;//允许客户端执行 set_acl()。
extern ZOOAPI const int ZOO_PERM_ALL;//允许客户端执行所有操作,等价与上述所有标志的或(OR) 。
extern ZOOAPI struct Id ZOO_ANYONE_ID_UNSAFE;/** This Id represents anyone. */
extern ZOOAPI struct Id ZOO_AUTH_IDS; /** This Id is only usable to set ACLs. It will get substituted with the * Id’s the client authenticated with. */
extern ZOOAPI struct ACL_vector ZOO_OPEN_ACL_UNSAFE; /** This is a completely open ACL*/
extern ZOOAPI struct ACL_vector ZOO_READ_ACL_UNSAFE;/** This ACL gives the world the ability to read. */
extern ZOOAPI struct ACL_vector ZOO_CREATOR_ALL_ACL;/** This ACL gives the creators authentication id’s all permissions. */
extern ZOOAPI const int ZOOKEEPER_WRITE;
extern ZOOAPI const int ZOOKEEPER_READ;
extern ZOOAPI const int ZOO_EPHEMERAL;
extern ZOOAPI const int ZOO_SEQUENCE;
extern ZOOAPI const int ZOO_EXPIRED_SESSION_STATE;
extern ZOOAPI const int ZOO_AUTH_FAILED_STATE;
extern ZOOAPI const int ZOO_CONNECTING_STATE;
extern ZOOAPI const int ZOO_ASSOCIATING_STATE;
extern ZOOAPI const int ZOO_CONNECTED_STATE;
typedef struct {
    int64_t client_id;
    char passwd[16];
} clientid_t;
        所有函数定义看下面:
typedef void (*watcher_fn)(zhandle_t *zh, int type,  int state, const char *path,void *watcherCtx);
ZOOAPI zhandle_t *zookeeper_init(const char *host, watcher_fn fn, int recv_timeout, const clientid_t *clientid, void *context, int flags);
ZOOAPI int zookeeper_close(zhandle_t *zh);
ZOOAPI const clientid_t *zoo_client_id(zhandle_t *zh);
ZOOAPI int zoo_recv_timeout(zhandle_t *zh);
ZOOAPI const void *zoo_get_context(zhandle_t *zh);
ZOOAPI void zoo_set_context(zhandle_t *zh, void *context);
ZOOAPI watcher_fn zoo_set_watcher(zhandle_t *zh,watcher_fn newFn);
#ifndef THREADED
ZOOAPI int zookeeper_interest(zhandle_t *zh, int *fd, int *interest,  struct timeval *tv);
ZOOAPI int zookeeper_process(zhandle_t *zh, int events);
#endif
typedef void (*void_completion_t)(int rc, const void *data);
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);
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);
ZOOAPI int zoo_state(zhandle_t *zh);
ZOOAPI 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);
ZOOAPI int zoo_adelete(zhandle_t *zh, const char *path, int version,  void_completion_t completion, const void *data);
ZOOAPI int zoo_aexists(zhandle_t *zh, const char *path, int watch,  stat_completion_t completion, const void *data);
ZOOAPI int zoo_awexists(zhandle_t *zh, const char *path,  watcher_fn watcher, void* watcherCtx,  stat_completion_t completion, const void *data);
ZOOAPI int zoo_aget(zhandle_t *zh, const char *path, int watch,  data_completion_t completion, const void *data);
ZOOAPI int zoo_awget(zhandle_t *zh, const char *path,  watcher_fn watcher, void* watcherCtx,  data_completion_t completion, const void *data);
ZOOAPI int zoo_aset(zhandle_t *zh, const char *path, const char *buffer, int buflen,  int version, stat_completion_t completion, const void *data);
ZOOAPI int zoo_aget_children(zhandle_t *zh, const char *path, int watch,  strings_completion_t completion, const void *data);
ZOOAPI int zoo_awget_children(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx,  strings_completion_t completion, const void *data);
ZOOAPI int zoo_aget_children2(zhandle_t *zh, const char *path, int watch,  strings_stat_completion_t completion, const void *data);
ZOOAPI int zoo_awget_children2(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx,  strings_stat_completion_t completion, const void *data);
ZOOAPI int zoo_async(zhandle_t *zh, const char *path,  string_completion_t completion, const void *data);
ZOOAPI int zoo_aget_acl(zhandle_t *zh, const char *path, acl_completion_t completion,  const void *data);
ZOOAPI int zoo_aset_acl(zhandle_t *zh, const char *path, int version,  struct ACL_vector *acl, void_completion_t, const void *data);
ZOOAPI const char* zerror(int c);
ZOOAPI int zoo_add_auth(zhandle_t *zh,const char* scheme,const char* cert,  int certLen, void_completion_t completion, const void *data);
ZOOAPI int is_unrecoverable(zhandle_t *zh);
ZOOAPI void zoo_set_debug_level(ZooLogLevel logLevel);
ZOOAPI void zoo_set_log_stream(FILE* logStream);
ZOOAPI void zoo_deterministic_conn_order(int yesOrNo);
ZOOAPI 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);
ZOOAPI int zoo_delete(zhandle_t *zh, const char *path, int version);
ZOOAPI int zoo_exists(zhandle_t *zh, const char *path, int watch, struct Stat *stat);
ZOOAPI int zoo_wexists(zhandle_t *zh, const char *path, watcher_fn watcher, void* watcherCtx, struct Stat *stat);
ZOOAPI int zoo_get(zhandle_t *zh, const char *path, int watch, char *buffer,    int* buffer_len, struct Stat *stat);
ZOOAPI int zoo_wget(zhandle_t *zh, const char *path,  watcher_fn watcher, void* watcherCtx,  char *buffer, int* buffer_len, struct Stat *stat);
ZOOAPI int zoo_set(zhandle_t *zh, const char *path, const char *buffer, int buflen, int version);
ZOOAPI int zoo_set2(zhandle_t *zh, const char *path, const char *buffer, int buflen, int version, struct Stat *stat);
ZOOAPI int zoo_get_children(zhandle_t *zh, const char *path, int watch, struct String_vector *strings);
ZOOAPI int zoo_wget_children(zhandle_t *zh, const char *path,  watcher_fn watcher, void* watcherCtx, struct String_vector *strings);
ZOOAPI int zoo_get_children2(zhandle_t *zh, const char *path, int watch, struct String_vector *strings, struct Stat *stat);
ZOOAPI int zoo_wget_children2(zhandle_t *zh, const char *path,  watcher_fn watcher, void* watcherCtx, struct String_vector *strings, struct Stat *stat);
ZOOAPI int zoo_get_acl(zhandle_t *zh, const char *path, struct ACL_vector *acl, struct Stat *stat);
ZOOAPI int zoo_set_acl(zhandle_t *zh, const char *path, int version, const struct ACL_vector *acl);

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>