int fd; /* the descriptor used to talk to zookeeper */ 与服务器连接的socket fd
char *hostname; /* the hostname of zookeeper */主机名
struct sockaddr_storage *addrs; /* the addresses that correspond to the hostname */UNIX网络编程中的一个标准地址格式
int addrs_count; /* The number of addresses in the addrs array */记录连接的服务器的个数
watcher_fn watcher; /* the registered watcher */注册的回调函数
struct timeval last_recv; /* The time that the last message was received */
struct timeval last_send; /* The time that the last message was sent */
struct timeval last_ping; /* The time that the last PING was sent */
struct timeval next_deadline; /* The time of the next deadline */ ?
int recv_timeout; /* The maximum amount of time that can go by without receiving anything from the zookeeper server */ 记录客户端设置的断链时间
buffer_list_t *input_buffer; /* the current buffer being read in */正在处理的一个包
buffer_head_t to_process; /* The buffers that have been read and are ready to be processed. */基于buffer_list_t的有锁的双向队列,存放已经读取好的待处理的数据
buffer_head_t to_send; /* The packets queued to send */存放等待发送的数据
completion_head_t sent_requests; /* The outstanding requests */基于completion_list_t的有锁和互斥变量的双向队列,存放未解决的数据
completion_head_t completions_to_process; /* completions that are ready to run */ 存放准备好了待处理的数据
int connect_index; /* The index of the address to connect to */连接到的地址的索引
clientid_t client_id;客户端的id,由服务器唯一标识的客户端
long long last_zxid;zookeeper 服务器上当前最新事务的 ID
int outstanding_sync; /* Number of outstanding synchronous requests */未完成的同步请求数
struct _buffer_list primer_buffer; /* The buffer used for the handshake at the start of a connection */在连接开始时用于握手的缓冲区
struct prime_struct primer_storage; /* the connect response */连接的返回,记录连接相关的信息
char primer_storage_buffer[40]; /* the true size of primer_storage */struct prime_struct的实际大小为40,可能是用来存放primer_storage中的具体内容的
volatile int state;标识当前是否为活跃状态,0为不活跃状态,>0为活跃状态
void *context;
auth_list_head_t auth_h; /* authentication data list */认证数据相关的列表
/* zookeeper_close is not reentrant because it de-allocates the zhandler. This guard variable is used to defer the destruction of zhandle till right before top-level API call returns to the caller */
int32_t ref_counter;定义调用close后延时多长时间销毁zkhandle
volatile int close_requested; ?
void *adaptor_priv; 一个适配器结构,启动成功后赋值
/* Used for debugging only: non-zero value indicates the time when the zookeeper_process call returned while there was at least one unprocessed server response available in the socket recv buffer */
struct timeval socket_readable;指示zookeeper_process调用返回的时间
zk_hashtable* active_node_watchers;
zk_hashtable* active_exist_watchers;
zk_hashtable* active_child_watchers;
/** used for chroot path at the client side **/
char *chroot;记录程序的根目录地址
};
带有唯一标识的链表结构的一个节点
typedef struct _completion_list {
int xid;唯一标识,用于记录请求发起的先后序号,用于确定单个客户端请求的响应顺继。
completion_t c;标识操作的类型,可支持多操作
const void *data;
buffer_list_t *buffer;。正在处理的一个包
struct _completion_list *next;
watcher_registration_t* watcher;一个用来临时存储回调相关数据的结构
} completion_list_t;
一个用来临时存储回调相关数据的结构
typedef struct _watcher_registration {
watcher_fn watcher; init对应的回调
void* context;
result_checker_fn checker; 另一个回调 ?
const char* path;
} watcher_registration_t;
typedef struct completion {
int type; /* one of COMPLETION_* values above */
union {
void_completion_t void_result;
stat_completion_t stat_result;
data_completion_t data_result;
strings_completion_t strings_result;
strings_stat_completion_t strings_stat_result;
acl_completion_t acl_result;
string_completion_t string_result;
struct watcher_object_list *watcher_result;
};
completion_head_t clist; /* For multi-op */
} completion_t;
连接的返回,记录连接相关的信息
struct prime_struct {
int32_t len;
int32_t protocolVersion;
int32_t timeOut;
int64_t sessionId;
int32_t passwd_len;
char passwd[16];
};
内部用于线程管理的
struct adaptor_threads {
pthread_t io;
pthread_t completion;
int threadsToWait; // barrier 等待两个线程启动成功在打开阻碍(锁)
pthread_cond_t cond; // barrier's conditional 条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号);必须和一个互斥锁配合,以防止多个线程同时请求;等待:无条件等待 pthread_cond_wait();计时等待 pthread_cond_timedwait();激活:pthread_cond_signal();
激活一个等待该条件的线程,存在多个等待线程时按入队顺序激活其中一个;pthread_cond_broadcast(); 激活所有等待线程。
pthread_mutex_t lock; // ... and a lock
pthread_mutex_t zh_lock; // critical section lock
管道,用来唤醒线程
int self_pipe[2];
#endif
};