概述:本来自己写了一篇,但是看到http://www.tuicool.com/articles/NfqeEf这篇更好,于是就算转载过来了吧。下一篇我们会讲到twemproxy的核心,即其转发响应转发请求。
作为一个proxy服务,我们应该深入了解它的进程和事件模型。
进程模型:
通过grep代码,查找fork()和pthread_create()函数的调用,并分析调用者两个函数的代码。我们可以看到twemproxy使用的是单进程单线程来处理请求,另外有一个线程来处理统计数据,但并不参与处理请求功能。
事件模型:
Twemproxy自己封装了一个简单的事件驱动模型,相关代码在event目录下。
我们可以先看一下event/nc_event.h:
ifndef #define _NC_EVENT_H_
#include
#define EVENT_SIZE
#define EVENT_READ #define EVENT_WRITE 0x00ff00 #define EVENT_ERR typedef typedef |
|
Twemproxy针对不同的平台定义了不同的event_base结构,因为我们用的是Linux,所以只关心epoll下的代码就行了:
|
|
epoll下的event_base定义很简单:一个epoll句柄ep,一个指向epoll_event数组的指针*event,一个event计数nevent,一个callback函数。
那么需要实现哪些函数呢?在event/nc_event.h的最底下有声明:
|
|
创建:
我们先看看如何创建event_base,下面是event/nc_epoll.c当中的event_base_create()函数定义:
event_base_create
{
}
|
创建event_base的过程,就是初始化前面定义的event_base结构体的内容:
- 调用epoll_create()创建epoll句柄
- 申请nevent * sizeof(struct
epoll_event)大小的空间,存放epoll_event指针数组 - 申请sizeof(struct
event_base),存放event_base - 对event_base中的ep、event、nevent、cb进行赋值。
调用event_base_create的地点在nc_core.c当中:
1
2
|
|
其中EVENT_SIZE刚才nc_event.h的代码里有定义(1024),而core_core函数则在nc_core.c当中定义。调用event_base_create()之后,我们便得到了一个可以容纳1024个event的event_base。
添加event:
创建完event_base之后,便要向event_base中添加event,需要用到event_add_*函数,我们先来看看event_add_conn()函数:
|
int
event_add_conn
{
}
|
conn的结构非常复杂,我们这里不去细看,因为这个函数只用到了conn->sd、conn->send_active和conn->recv_active,其中sd是要注册的文件描述符,而send_active和read_active只是布尔型的标记位,用来标记当前连接是否开启了读/写。
初始化event的时候,加入了EPOLLIN和EPOLLOUT事件,并指定了边缘触发EPOLLET模式,即当文件描述符的可读/可写状态切换的时候返回,成功后设置conn的send_active和read_active为1。
event_add_in()和event_add_out()其实是修改event的,具体分别将其修改为只读或者读写,同时修改send_active或者read_active。
那么什么时候添加event呢,一般是在创建连接之后,我们检索一下event_add_conn的调用位置:
|
event event event event nc_proxy nc_proxy nc_server |
其中nc_server.c中的调用是在Twemproxy创建了与后端Memcached Server的链接之后,而nc_proxy.c中的调用则分别是在创建listen端口之后,以及accept客户端的链接之后,这符合我们的预期。接下来我们分别来看看这3处。
监听端口:
针对每一个server pool,Twemproxy会创建一个proxy,每个proxy监听一个指定的端口,监听后即需要像event_base添加事件,相关代码在nc_proxy.c的proxy_listen()函数中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
static proxy_listen
{
}
|
代码已经省略掉了不相关的部分,可以看出,proxy_listen()函数先调用event_add_conn添加一个事件,然后将其修改为只读(等待客户端连接)。
等待连接:
accept的代码在nc_proxy.c的proxy_accept()函数中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
static proxy_accept
{
}
|
同样省略了不相关的部分,可以看出在accept到客户端的文件描述符后,也是通过event_add_conn添加事件,不过这时没有调用event_del_out(),因为我们是需要向客户端写回数据的。
连接Memcached:
连接后端Memcached的代码在nc_server.c当中的server_connect()函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
rstatus_t
server_connect
{
}
|
初始化连接之后,调用event_add_conn()来添加事件,然后连接Memcached,因为连接需要读写,所以没有调用event_del_out()。
从上面的分析看,添加事件的过程基本上是一致的。
事件循环:
完成了event_base的创建和event的添加之后,就进入事件循环,前一节我们讲到时间循环是在core_loop()中做的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
rstatus_t
core_loop
{
}
|
core_loop()调用了event_wait()函数做事件循环,event_wait()的代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
int
event_wait
{
}
|
epoll_wait()返回当前I/O就绪的句柄数nsd,同时将epoll_event放入*event指向的数组中,随后就可以根据nsd和*event去依次访问每一个句柄,调用event_base的cb函数进行处理。
事件触发:
前面我们分析event_base_create()的时候了解到,event_base_create()这个函数仅在nc_core.c当中调用,也就是说全局只有一个event_base,而这个event_base上所有的事件触发的回调函数event_base->cb都是指向调用event_base_create()函数时所设置的core_core()。那么问题来了,如何区分不同的连接(后端Memcached连接句柄,客户端连接句柄以及proxy的监听端口句柄)呢?
我们需要具体看一下core_core()这个函数的代码,这个函数非常短:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
rstatus_t
core_core
{
}
|
可以看出,针对不同的事件:EVENT_READ、EVENT_WRITE,core_core()函数只是分别去调用了core_recv()和core_send()函数,而EVENT_ERR事件则报错。
那么core_recv()和core_send()函数做了什么呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
static core_recv
{
}
static core_send
{
}
|
可以看出他们只是分别调用了conn->recv()和conn->send(),我们看一下conn这个结构体相关部分的定义:
1
2
3
4
5
6
7
8
9
10
11
12
|
struct
}
|
省略掉了无关的部分,可以看到conn包含了若干handler的函数指针,这其中就包含recv和send。那么这些字段是在创建conn的时候初始化的。针对不同的conn,为他们初始化不同的handler即可。
那么,首先我们看看listen的过程,调用proxy_listen()函数的位置是在nc_proxy.c的proxy_each_init()当中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
rstatus_t
proxy_each_init
{
}
|
在调用proxy_listen()之前,调用了conn_get_proxy()来创建并初始化conn:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
struct
conn_get_proxy
{
}
|
通过这个函数定义,我们看到为conn初始化了recv、close、ref和unref这4个handler,其中recv的handler是proxy_recv,那么也就是说,当监听句柄由不可读变为可读时,将会触发proxy_recv这个回调函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
rstatus_t
proxy_recv
{
}
|
果不其然,这个函数调用了proxy_accept():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
static proxy_accept
{
}
|
代码已经滤掉了无关部分,proxy_accept在accept到客户端的句柄后,通过调用conn_get()创建并初始化conn,在conn_get()中对conn做了如下初始化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
|
而在创建Memcached连接时,是这么初始化的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
|
这些handler的作用,这里暂时不细究,只需要理解事件触发回调函数的过程就可以了。
小结:
通过上面的分析,我们知道了Twemproxy使用了单进程模型,并且自己封装了事件模型,这与redis是类似的。这也就意味着,Twemproxy不适合做CPU密集的操作,仅作为proxy来用是OK的,若要对Twemproxy的功能进行扩展,要特别注意这一点,以免影响Twemproxy的吞吐量。