高并发系统设计

高并发系统设计

作者:周顺利

注:本文大多数观点和代码都是从网上或者开源代码中抄来的,为了疏理和组织这片文章,作者也费了不少心血,为了表示对我劳动的尊重,请转载时注明作者和出处。

 

一、     引子

最近失业在家,闲来无事。通过网上查找资料和查看开源代码,研究了一下互联网高并发系统的一些设计。这里主要从服务器内部设计和整个系统设计两个方面讨论,更多的是从互联网大型网站设计方面考虑,高性能计算之类系统没有研究过。

 

二、     服务器内部设计

服务器设计涉及Socket的阻塞/非阻塞,操作系统IO的同步和异步(之前被人问到过两次。第一次让我说说知道的网络模型,我说ISO模型和TCP/IP模型,结果被鄙视了。最后人说了解linux epoll吗?不了解呀!汉,回去查资料才知道是这回事。第二次让我说说知道线程模型,汉!这个名词感觉没有听说过,线程?模型?半同步/半异步,领导者/跟随者知道吗。再汉,我知道同步/异步,还有半同步/半异步?啥呀?领导者/跟随者,我现在没有领导。回去一顿恶补,原来是ACE框架里边经常有这样的提法,Reactor属于同步/半同步,PREACTOR属于领导者/跟随者模式。瀑布汗。小插曲一段,这些不懂没关系,下边我慢慢分解),事件分离器,线程池等。内部设计希望通过各个模块的给出一个简单设计,经过您的进一步的组合和打磨,就可以实现一个基本的高并发服务器。

1.     Java高并发服务器

Java设计高并发服务器相对比较简单。直接是用ServerSocket或者Channel+selector实现。前者属于同步IO设计,后者采用了模拟的异步IO。为什么说模拟的异步IO呢?记得网上看到一篇文章分析了java的selector。在windows上通过建立一个127.0.0.1到127.0.0.1的连接实现IO的异步通知。在linux上通过建立一个管道实现IO的异步通知。考虑到高并并发系统的要求和java上边的异步IO的限制(通常操作系统同时打开的文件数是有限制的)和效率问题,java的高并发服务器设计不做展开深入的分析,可以参考C高并发服务器的分析做同样的设计。

 

2.     C高并发服务器设计

1)    基本概念

Ø  阻塞和非阻塞socket

所谓阻塞Socket,是指其完成指定的任务之前不允许程序调用另一个函数,在Windows下还会阻塞本线程消息的发送。所谓非阻塞Socket,是指操作启动之后,如果可以立即得到结果就返回结果,否则返回表示结果需要等待的错误信息,不等待任务完成函数就返回。一个比较有意思的问题是accept的Socket是阻塞的还是非阻塞的呢?下边是MSDN上边的一段话:The accept function extracts thefirst connection on the queue of pending connections on socket s. It thencreates and returns a handle to the new socket. The newly created socket is thesocket that will handle the actual connection; it has the same properties assocket s, including the asynchronous events registered with the WSAAsyncSelector WSAEventSelect functions.

Ø  同步/异步IO

有两种类型的文件IO同步:同步文件IO和异步文件IO。异步文件IO也就是重叠IO。 
      在同步文件IO中,线程启动一个IO操作然后就立即进入等待状态,直到IO操作完成后才醒来继续执行。而异步文件IO方式中,线程发送一个IO请求到内核,然后继续处理其他的事情,内核完成IO请求后,将会通知线程IO操作完成了。 
      如果IO请求需要大量时间执行的话,异步文件IO方式可以显著提高效率,因为在线程等待的这段时间内,CPU将会调度其他线程进行执行,如果没有其他线程需要执行的话,这段时间将会浪费掉(可能会调度操作系统的零页线程)。如果IO请求操作很快,用异步IO方式反而还低效,还不如用同步IO方式。 
      同步IO在同一时刻只允许一个IO操作,也就是说对于同一个文件句柄的IO操作是序列化的,即使使用两个线程也不能同时对同一个文件句柄同时发出读写操作。重叠IO允许一个或多个线程同时发出IO请求。异步IO在请求完成时,通过将文件句柄设为有信号状态来通知应用程序,或者应用程序通过GetOverlappedResult察看IO请求是否完成,也可以通过一个事件对象来通知应用程序。高并发系统通常采用异步IO方式提高系统性能。

Ø  事件分离器

事件分离器的概念是针对异步IO来说的。在同步IO的情况下,执行操作等待返回结果,不要事件分离器。异步IO的时候,发送请求后,结果是通过事件通知的。这是产生了事件分离器的需求。事件分离器主要任务是管理和分离不同文件描述符上的所发生的事件,让后通知相应的事件,派发相应的动作。下边是lighthttpd事件分离器定义:

[cpp]  view plain copy
  1. /** 
  2.  * fd-event handler for select(), poll() andrt-signals on Linux 2.4 
  3.  * 
  4.  */  
  5. typedef struct fdevents {  
  6.          fdevent_handler_t type;  
  7.    
  8.          fdnode **fdarray;  
  9.          size_t maxfds;  
  10.    
  11. #ifdef USE_LINUX_SIGIO  
  12.          int in_sigio;  
  13.          int signum;  
  14.          sigset_t sigset;  
  15.          siginfo_t siginfo;  
  16.          bitset *sigbset;  
  17. #endif  
  18. #ifdef USE_LINUX_EPOLL  
  19.          int epoll_fd;  
  20.          struct epoll_event *epoll_events;  
  21. #endif  
  22. #ifdef USE_POLL  
  23.          struct pollfd *pollfds;  
  24.    
  25.          size_t size;  
  26.          size_t used;  
  27.    
  28.          buffer_int unused;  
  29. #endif  
  30. #ifdef USE_SELECT  
  31.          fd_set select_read;  
  32.          fd_set select_write;  
  33.          fd_set select_error;  
  34.    
  35.          fd_set select_set_read;  
  36.          fd_set select_set_write;  
  37.          fd_set select_set_error;  
  38.    
  39.          int select_max_fd;  
  40. #endif  
  41. #ifdef USE_SOLARIS_DEVPOLL  
  42.          int devpoll_fd;  
  43.          struct pollfd *devpollfds;  
  44. #endif  
  45. #ifdef USE_FREEBSD_KQUEUE  
  46.          int kq_fd;  
  47.          struct kevent *kq_results;  
  48.          bitset *kq_bevents;  
  49. #endif  
  50. #ifdef USE_SOLARIS_PORT  
  51.          int port_fd;  
  52. #endif  
  53.          int (*reset)(struct fdevents *ev);  
  54.          void (*free)(struct fdevents *ev);  
  55.    
  56.          int (*event_add)(struct fdevents *ev, int fde_ndx, int fd,int events);  
  57.          int (*event_del)(struct fdevents *ev, int fde_ndx, int fd);  
  58.          int (*event_get_revent)(struct fdevents *ev, size_t ndx);  
  59.          int (*event_get_fd)(struct fdevents *ev, size_t ndx);  
  60.    
  61.          int (*event_next_fdndx)(struct fdevents *ev, int ndx);  
  62.    
  63.          int (*poll)(struct fdevents *ev, int timeout_ms);  
  64.    
  65.          int (*fcntl_set)(struct fdevents *ev, int fd);  
  66. } fdevents;  
  67.    
  68. fdevents *fdevent_init(size_tmaxfds, fdevent_handler_t type);  
  69. int fdevent_reset(fdevents*ev);  
  70. void fdevent_free(fdevents*ev);  
  71.    
  72. int fdevent_event_add(fdevents*ev, int *fde_ndx, int fd, int events);  
  73. int fdevent_event_del(fdevents*ev, int *fde_ndx, int fd);  
  74. intfdevent_event_get_revent(fdevents *ev, size_t ndx);  
  75. intfdevent_event_get_fd(fdevents *ev, size_t ndx);  
  76. fdevent_handlerfdevent_get_handler(fdevents *ev, int fd);  
  77. void *fdevent_get_context(fdevents *ev, int fd);  
  78.    
  79. int fdevent_event_next_fdndx(fdevents*ev, int ndx);  
  80.    
  81. int fdevent_poll(fdevents *ev,int timeout_ms);  
  82.    
  83. int fdevent_register(fdevents*ev, int fd, fdevent_handler handler, void *ctx);  
  84. int fdevent_unregister(fdevents*ev, int fd);  
  85.    
  86. int fdevent_fcntl_set(fdevents*ev, int fd);  
  87.    
  88. intfdevent_select_init(fdevents *ev);  
  89. int fdevent_poll_init(fdevents*ev);  
  90. intfdevent_linux_rtsig_init(fdevents *ev);  
  91. intfdevent_linux_sysepoll_init(fdevents *ev);  
  92. intfdevent_solaris_devpoll_init(fdevents *ev);  
  93. intfdevent_freebsd_kqueue_init(fdevents *ev);  

具体系统的事件操作通过:

fdevent_freebsd_kqueue.c

fdevent_linux_rtsig.c

fdevent_linux_sysepoll.c

fdevent_poll.c

fdevent_select.c

fdevent_solaris_devpoll.c

几个文件实现。

Ø  线程池

线程池基本上比较简单,实现线程的借入和借出,创建和销毁。最完好可以做到通过一个事件触发一个线程开始工作。下边给出一个简单的,没有实现根据事件触发的,linux和windows通用的线程池模型:

[cpp]  view plain copy
  1. spthread.h  
  2. #ifndef __spthread_hpp__  
  3. #define __spthread_hpp__  
  4.    
  5. #ifndef WIN32  
  6.    
  7. /// pthread  
  8.    
  9. #include <pthread.h>  
  10. #include <unistd.h>  
  11.    
  12. typedef void *sp_thread_result_t;  
  13. typedef pthread_mutex_tsp_thread_mutex_t;  
  14. typedef pthread_cond_t  sp_thread_cond_t;  
  15. typedef pthread_t       sp_thread_t;  
  16. typedef pthread_attr_t  sp_thread_attr_t;  
  17.    
  18. #definesp_thread_mutex_init(m,a)  pthread_mutex_init(m,a)  
  19. #definesp_thread_mutex_destroy(m) pthread_mutex_destroy(m)  
  20. #definesp_thread_mutex_lock(m)    pthread_mutex_lock(m)  
  21. #define sp_thread_mutex_unlock(m)   pthread_mutex_unlock(m)  
  22.    
  23. #definesp_thread_cond_init(c,a)   pthread_cond_init(c,a)  
  24. #definesp_thread_cond_destroy(c)  pthread_cond_destroy(c)  
  25. #definesp_thread_cond_wait(c,m)   pthread_cond_wait(c,m)  
  26. #definesp_thread_cond_signal(c)   pthread_cond_signal(c)  
  27.    
  28. #definesp_thread_attr_init(a)       pthread_attr_init(a)  
  29. #definesp_thread_attr_setdetachstate pthread_attr_setdetachstate  
  30. #defineSP_THREAD_CREATE_DETACHED    PTHREAD_CREATE_DETACHED  
  31.    
  32. #define sp_thread_self    pthread_self  
  33. #define sp_thread_create  pthread_create  
  34.    
  35. #define SP_THREAD_CALL  
  36. typedef sp_thread_result_t ( *sp_thread_func_t )( void * args );  
  37.    
  38. #define sp_sleep(x) sleep(x)  
  39.    
  40. #else///  
  41.    
  42. // win32 thread  
  43.    
  44. #include <winsock2.h>  
  45. #include <process.h>  
  46.    
  47. typedef unsigned sp_thread_t;  
  48.    
  49. typedef unsignedsp_thread_result_t;  
  50. #define SP_THREAD_CALL__stdcall  
  51. typedef sp_thread_result_t (__stdcall * sp_thread_func_t )( void * args );  
  52.    
  53. typedef HANDLE  sp_thread_mutex_t;  
  54. typedef HANDLE  sp_thread_cond_t;  
  55. typedef DWORD   sp_thread_attr_t;  
  56.    
  57. #defineSP_THREAD_CREATE_DETACHED 1  
  58. #define sp_sleep(x)Sleep(1000*x)  
  59.    
  60. int sp_thread_mutex_init(sp_thread_mutex_t * mutex, void * attr )  
  61. {  
  62.          *mutex = CreateMutex( NULL, FALSE, NULL );  
  63.          return NULL == * mutex ? GetLastError() : 0;  
  64. }  
  65.    
  66. int sp_thread_mutex_destroy(sp_thread_mutex_t * mutex )  
  67. {  
  68.          int ret = CloseHandle( *mutex );  
  69.    
  70.          return 0 == ret ? GetLastError() : 0;  
  71. }  
  72.    
  73. int sp_thread_mutex_lock(sp_thread_mutex_t * mutex )  
  74. {  
  75.          int ret = WaitForSingleObject( *mutex, INFINITE );  
  76.          return WAIT_OBJECT_0 == ret ? 0 : GetLastError();  
  77. }  
  78.    
  79. int sp_thread_mutex_unlock(sp_thread_mutex_t * mutex )  
  80. {  
  81.          int ret = ReleaseMutex( *mutex );  
  82.          return 0 != ret ? 0 : GetLastError();  
  83. }  
  84.    
  85. int sp_thread_cond_init(sp_thread_cond_t * cond, void * attr )  
  86. {  
  87.          *cond = CreateEvent( NULL, FALSE, FALSE, NULL );  
  88.          return NULL == *cond ? GetLastError() : 0;  
  89. }  
  90.    
  91. int sp_thread_cond_destroy(sp_thread_cond_t * cond )  
  92. {  
  93.          int ret = CloseHandle( *cond );  
  94.          return 0 == ret ? GetLastError() : 0;  
  95. }  
  96.    
  97. /* 
  98. Caller MUST be holding themutex lock; the 
  99. lock is released and the calleris blocked waiting 
  100. on 'cond'. When 'cond' issignaled, the mutex 
  101. is re-acquired before returningto the caller. 
  102. */  
  103. int sp_thread_cond_wait(sp_thread_cond_t * cond, sp_thread_mutex_t * mutex )  
  104. {  
  105.          int ret = 0;  
  106.    
  107.          sp_thread_mutex_unlock( mutex );  
  108.    
  109.          ret = WaitForSingleObject( *cond, INFINITE );  
  110.    
  111.          sp_thread_mutex_lock( mutex );  
  112.    
  113.          return WAIT_OBJECT_0 == ret ? 0 : GetLastError();  
  114. }  
  115.    
  116. int sp_thread_cond_signal(sp_thread_cond_t * cond )  
  117. {  
  118.          int ret = SetEvent( *cond );  
  119.          return 0 == ret ? GetLastError() : 0;  
  120. }  
  121.    
  122. sp_thread_t sp_thread_self()  
  123. {  
  124.          return GetCurrentThreadId();  
  125. }  
  126.    
  127. int sp_thread_attr_init(sp_thread_attr_t * attr )  
  128. {  
  129.          *attr = 0;  
  130.          return 0;  
  131. }  
  132.    
  133. intsp_thread_attr_setdetachstate( sp_thread_attr_t * attr, int detachstate )  
  134. {  
  135.          *attr |= detachstate;  
  136.          return 0;  
  137. }  
  138.    
  139. int sp_thread_create(sp_thread_t * thread, sp_thread_attr_t * attr,  
  140.                    sp_thread_func_t myfunc, void * args )  
  141. {  
  142.          // _beginthreadex returns 0 on an error  
  143.          HANDLE h = (HANDLE)_beginthreadex( NULL, 0, myfunc, args, 0,thread );  
  144.          return h > 0 ? 0 : GetLastError();  
  145. }  
  146.    
  147. #endif  
  148.    
  149. #endif  
  150. threadpool.h  
  151. /** 
  152.  * threadpool.h 
  153.  * 
  154.  * This file declares the functionalityassociated with 
  155.  * your implementation of a threadpool. 
  156.  */  
  157.    
  158. #ifndef __threadpool_h__  
  159. #define __threadpool_h__  
  160.    
  161. #ifdef __cplusplus  
  162. extern "C" {  
  163. #endif  
  164.    
  165. // maximum number of threadsallowed in a pool  
  166. #define MAXT_IN_POOL 200  
  167.    
  168. // You must hide the internaldetails of the threadpool  
  169. // structure from callers, thusdeclare threadpool of type "void".  
  170. // In threadpool.c, you willuse type conversion to coerce  
  171. // variables of type"threadpool" back and forth to a  
  172. // richer, internal type.  (See threadpool.c for details.)  
  173.    
  174. typedef void *threadpool;  
  175.    
  176. // "dispatch_fn"declares a typed function pointer.  A  
  177. // variable of type"dispatch_fn" points to a function  
  178. // with the followingsignature:  
  179. //  
  180. //     void dispatch_function(void *arg);  
  181.    
  182. typedef void(*dispatch_fn)(void *);  
  183.    
  184. /** 
  185.  * create_threadpool creates a fixed-sizedthread 
  186.  * pool. If the function succeeds, it returns a (non-NULL) 
  187.  * "threadpool", else it returnsNULL. 
  188.  */  
  189. threadpoolcreate_threadpool(int num_threads_in_pool);  
  190.    
  191.    
  192. /** 
  193.  * dispatch sends a thread off to do somework.  If 
  194.  * all threads in the pool are busy, dispatchwill 
  195.  * block until a thread becomes free and isdispatched. 
  196.  * 
  197.  * Once a thread is dispatched, this functionreturns 
  198.  * immediately. 
  199.  * 
  200.  * The dispatched thread calls into thefunction 
  201.  * "dispatch_to_here" with argument"arg". 
  202.  */  
  203. intdispatch_threadpool(threadpool from_me, dispatch_fn dispatch_to_here,  
  204.                void *arg);  
  205.    
  206. /** 
  207.  * destroy_threadpool kills the threadpool,causing 
  208.  * all threads in it to commit suicide, andthen 
  209.  * frees all the memory associated with thethreadpool. 
  210.  */  
  211. voiddestroy_threadpool(threadpool destroyme);  
  212.    
  213. #ifdef __cplusplus  
  214. }  
  215. #endif  
  216.    
  217. #endif  
  218. threadpool.c  
  219. /** 
  220.  * threadpool.c 
  221.  * 
  222.  * This file will contain your implementation ofa threadpool. 
  223.  */  
  224.    
  225. #include <stdio.h>  
  226. #include <stdlib.h>  
  227. //#include <unistd.h>  
  228. //#include <sp_thread.h>  
  229. #include <string.h>  
  230.    
  231. #include"threadpool.h"  
  232. #include "spthread.h"  
  233.    
  234. typedef struct _thread_st {  
  235.          sp_thread_t id;  
  236.          sp_thread_mutex_t mutex;  
  237.          sp_thread_cond_t cond;  
  238.          dispatch_fn fn;  
  239.          void *arg;  
  240.          threadpool parent;  
  241. } _thread;  
  242.    
  243. // _threadpool is the internalthreadpool structure that is  
  244. // cast to type"threadpool" before it given out to callers  
  245. typedef struct _threadpool_st {  
  246.          // you should fill in this structure with whatever you need  
  247.          sp_thread_mutex_t tp_mutex;  
  248.          sp_thread_cond_t tp_idle;  
  249.          sp_thread_cond_t tp_full;  
  250.          sp_thread_cond_t tp_empty;  
  251.          _thread ** tp_list;  
  252.          int tp_index;  
  253.          int tp_max_index;  
  254.          int tp_stop;  
  255.    
  256.          int tp_total;  
  257. } _threadpool;  
  258.    
  259. threadpoolcreate_threadpool(int num_threads_in_pool)  
  260. {  
  261.          _threadpool *pool;  
  262.    
  263.          // sanity check the argument  
  264.          if ((num_threads_in_pool <= 0) || (num_threads_in_pool> MAXT_IN_POOL))  
  265.                    return NULL;  
  266.    
  267.          pool = (_threadpool *) malloc(sizeof(_threadpool));  
  268.          if (pool == NULL) {  
  269.                    fprintf(stderr, "Out of memory creating a newthreadpool!\n");  
  270.                    return NULL;  
  271.          }  
  272.    
  273.          // add your code here to initialize the newly createdthreadpool  
  274.          sp_thread_mutex_init( &pool->tp_mutex, NULL );  
  275.          sp_thread_cond_init( &pool->tp_idle, NULL );  
  276.          sp_thread_cond_init( &pool->tp_full, NULL );  
  277.          sp_thread_cond_init( &pool->tp_empty, NULL );  
  278.          pool->tp_max_index = num_threads_in_pool;  
  279.          pool->tp_index = 0;  
  280.          pool->tp_stop = 0;  
  281.          pool->tp_total = 0;  
  282.          pool->tp_list = ( _thread ** )malloc( sizeofvoid * ) *MAXT_IN_POOL );  
  283.          memset( pool->tp_list, 0, sizeofvoid * ) * MAXT_IN_POOL);  
  284.    
  285.          return (threadpool) pool;  
  286. }  
  287.    
  288. int save_thread( _threadpool *pool, _thread * thread )  
  289. {  
  290.          int ret = -1;  
  291.    
  292.          sp_thread_mutex_lock( &pool->tp_mutex );  
  293.    
  294.          if( pool->tp_index < pool->tp_max_index ) {  
  295.                    pool->tp_list[ pool->tp_index ] = thread;  
  296.                    pool->tp_index++;  
  297.                    ret = 0;  
  298.    
  299.                    sp_thread_cond_signal( &pool->tp_idle );  
  300.    
  301.                    if( pool->tp_index >= pool->tp_total ) {  
  302.                             sp_thread_cond_signal(&pool->tp_full );  
  303.                    }  
  304.          }  
  305.    
  306.          sp_thread_mutex_unlock( &pool->tp_mutex );  
  307.    
  308.          return ret;  
  309. }  
  310.    
  311. sp_thread_result_tSP_THREAD_CALL wrapper_fn( void * arg )  
  312. {  
  313.          _thread * thread = (_thread*)arg;  
  314.          _threadpool * pool = (_threadpool*)thread->parent;  
  315.    
  316.          for( ; 0 == ((_threadpool*)thread->parent)->tp_stop; ){  
  317.                    thread->fn( thread->arg );  
  318.    
  319.                    if( 0 !=((_threadpool*)thread->parent)->tp_stop ) break;  
  320.    
  321.                    sp_thread_mutex_lock( &thread->mutex );  
  322.                    if( 0 == save_thread( thread->parent, thread )) {  
  323.                             sp_thread_cond_wait(&thread->cond, &thread->mutex );  
  324.                             sp_thread_mutex_unlock(&thread->mutex );  
  325.                    } else {  
  326.                             sp_thread_mutex_unlock(&thread->mutex );  
  327.                             sp_thread_cond_destroy(&thread->cond );  
  328.                             sp_thread_mutex_destroy(&thread->mutex );  
  329.    
  330.                             free( thread );  
  331.                             break;  
  332.                    }  
  333.          }  
  334.    
  335.          sp_thread_mutex_lock( &pool->tp_mutex );  
  336.          pool->tp_total--;  
  337.          if( pool->tp_total <= 0 ) sp_thread_cond_signal(&pool->tp_empty );  
  338.          sp_thread_mutex_unlock( &pool->tp_mutex );  
  339.    
  340.          return 0;  
  341. }  
  342.    
  343. intdispatch_threadpool(threadpool from_me, dispatch_fn dispatch_to_here, void*arg)  
  344. {  
  345.          int ret = 0;  
  346.    
  347.          _threadpool *pool = (_threadpool *) from_me;  
  348.          sp_thread_attr_t attr;  
  349.          _thread * thread = NULL;  
  350.    
  351.          // add your code here to dispatch a thread  
  352.          sp_thread_mutex_lock( &pool->tp_mutex );  
  353.    
  354.          if( pool->tp_index <= 0 && pool->tp_total>= pool->tp_max_index ) {  
  355.                    sp_thread_cond_wait( &pool->tp_idle,&pool->tp_mutex );  
  356.          }  
  357.    
  358.          if( pool->tp_index <= 0 ) {  
  359.                    _thread * thread = ( _thread * )malloc( sizeof(_thread ) );  
  360.                    memset( &( thread->id ), 0, sizeof(thread->id ) );  
  361.                    sp_thread_mutex_init( &thread->mutex, NULL);  
  362.                    sp_thread_cond_init( &thread->cond, NULL );  
  363.                    thread->fn = dispatch_to_here;  
  364.                    thread->arg = arg;  
  365.                    thread->parent = pool;  
  366.    
  367.                    sp_thread_attr_init( &attr );  
  368.                    sp_thread_attr_setdetachstate( &attr,SP_THREAD_CREATE_DETACHED );  
  369.    
  370.                    if( 0 == sp_thread_create( &thread->id,&attr, wrapper_fn, thread ) ) {  
  371.                             pool->tp_total++;  
  372.                             printf( "create thread#%ld\n",thread->id );  
  373.                    } else {  
  374.                             ret = -1;  
  375.                             printf( "cannot createthread\n" );  
  376.                             sp_thread_mutex_destroy(&thread->mutex );  
  377.                             sp_thread_cond_destroy(&thread->cond );  
  378.                             free( thread );  
  379.                    }  
  380.          } else {  
  381.                    pool->tp_index--;  
  382.                    thread = pool->tp_list[ pool->tp_index ];  
  383.                    pool->tp_list[ pool->tp_index ] = NULL;  
  384.    
  385.                    thread->fn = dispatch_to_here;  
  386.                    thread->arg = arg;  
  387.                    thread->parent = pool;  
  388.    
  389.                    sp_thread_mutex_lock( &thread->mutex );  
  390.                    sp_thread_cond_signal( &thread->cond ) ;  
  391.                    sp_thread_mutex_unlock ( &thread->mutex );  
  392.          }  
  393.    
  394.          sp_thread_mutex_unlock( &pool->tp_mutex );  
  395.    
  396.          return ret;  
  397. }  
  398.    
  399. void destroy_threadpool(threadpooldestroyme)  
  400. {  
  401.          _threadpool *pool = (_threadpool *) destroyme;  
  402.    
  403.          // add your code here to kill a threadpool  
  404.          int i = 0;  
  405.    
  406.          sp_thread_mutex_lock( &pool->tp_mutex );  
  407.    
  408.          if( pool->tp_index < pool->tp_total ) {  
  409.                    printf( "waiting for %d thread(s) tofinish\n", pool->tp_total - pool->tp_index );  
  410.                    sp_thread_cond_wait( &pool->tp_full,&pool->tp_mutex );  
  411.          }  
  412.    
  413.          pool->tp_stop = 1;  
  414.    
  415.          for( i = 0; i < pool->tp_index; i++ ) {  
  416.                    _thread * thread = pool->tp_list[ i ];  
  417.    
  418.                    sp_thread_mutex_lock( &thread->mutex );  
  419.                    sp_thread_cond_signal( &thread->cond ) ;  
  420.                    sp_thread_mutex_unlock ( &thread->mutex );  
  421.          }  
  422.    
  423.          if( pool->tp_total > 0 ) {  
  424.                    printf( "waiting for %d thread(s) toexit\n", pool->tp_total );  
  425.                    sp_thread_cond_wait( &pool->tp_empty,&pool->tp_mutex );  
  426.          }  
  427.    
  428.          for( i = 0; i < pool->tp_index; i++ ) {  
  429.                    free( pool->tp_list[ i ] );  
  430.                    pool->tp_list[ i ] = NULL;  
  431.          }  
  432.    
  433.          sp_thread_mutex_unlock( &pool->tp_mutex );  
  434.    
  435.          pool->tp_index = 0;  
  436.    
  437.          sp_thread_mutex_destroy( &pool->tp_mutex );  
  438.          sp_thread_cond_destroy( &pool->tp_idle );  
  439.          sp_thread_cond_destroy( &pool->tp_full );  
  440.          sp_thread_cond_destroy( &pool->tp_empty );  
  441.    
  442.          free( pool->tp_list );  
  443.          free( pool );  
  444. }  

2)    常见的设计模式

根据Socket的阻塞非阻塞,IO的同步和异步。可以分为如下4中情形

阻塞同步     |    阻塞异步

_________|______________

非阻塞同步  |   非阻塞异步

 

阻塞同步方式是原始的方式,也是许多教科书上介绍的方式,因为Socket和IO默认的为阻塞和同步方式。基本流程如下:

[cpp]  view plain copy
  1. listen_fd = socket( AF_INET,SOCK_STREAM,0 )  
  2. bind( listen_fd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr_in))  
  3. listen( listen_fd,1 )  
  4. accept( listen_fd,  (struct sockaddr*)&remote_addr,&addr_len )  
  5. recv( accept_fd ,&in_buf ,1024 ,0 )  
  6. close(accept_fd)  

阻塞异步方式有所改进,但是Socket的阻塞方式,前一个连接没有处理完成,下一个连接不能接入,是高并发服务器所不可接收的方式。只不过在上边阻塞同步方式的基础上使用select(严格来说select是一种IO多路服用技术。因为linux尚没有完整的实现异步IO,而winsock实在理解socket没有linux上面那么直观。,这里为了方便,没有做严格的区分)或者其它异步IO方式。

非阻塞同步方式,通过设置socket选项为NONBLOCK,可以很快的接收连接,但是处理采用同步IO方式,服务器处理性能也比较差。

上边三种方式不做深入介绍。下边主要从非阻塞异步IO方式介绍。

非阻塞异步IO方式中,由于异步IO方式在同一系统可能有多种实现,不同系统也有不同实现,下边介绍几种常见的IO方式和服务器框架。

 

Ø  Select

Select采用轮训注册的fd方式。是一种比较老的IO多路服用实现方式,效率相对要差一些。Select方式在windows和linux上都支持。

基本框架如下:

[cpp]  view plain copy
  1. socket( AF_INET,SOCK_STREAM,0 )  
  2. fcntl(listen_fd, F_SETFL,flags|O_NONBLOCK);  
  3. bind( listen_fd, (structsockaddr *)&my_addr,sizeof(struct sockaddr_in))  
  4. listen( listen_fd,1 )  
  5. FD_ZERO( &fd_sets );  
  6. FD_SET(listen_fd,&fd_sets);  
  7. for(k=0; k<=i; k++){  
  8.          FD_SET(accept_fds[k],&fd_sets);  
  9. }  
  10. events = select( max_fd + 1,&fd_sets, NULL, NULL, NULL );  
  11. if(FD_ISSET(listen_fd,&fd_sets) ){  
  12. accept_fd = accept( listen_fd, (structsockaddr *)&remote_addr,&addr_len );  
  13. }  
  14. for( j=0; j<=i; j++ ){  
  15.          if( FD_ISSET(accept_fds[j],&fd_sets) ){  
  16.                    recv( accept_fds[j] ,&in_buf ,1024 ,0 );  
  17.          }  
  18. }  

Ø  Epoll

Epoll是linux2.6内核以后支持的一种高性能的IO多路服用技术。服务器框架如下:

[cpp]  view plain copy
  1. socket( AF_INET,SOCK_STREAM,0 )  
  2. fcntl(listen_fd, F_SETFL,flags|O_NONBLOCK);  
  3. bind( listen_fd, (structsockaddr *)&my_addr,sizeof(struct sockaddr_in))  
  4. listen( listen_fd,1 )  
  5. epoll_ctl(epfd,EPOLL_CTL_ADD,listen_fd,&ev);  
  6. ev_s = epoll_wait(epfd,events,20,500 );  
  7. for(i=0; i<ev_s;i++){  
  8.                    if(events[i].data.fd==listen_fd){  
  9.                             accept_fd = accept( listen_fd,(structsockaddr *)&remote_addr,&addr_len );  
  10.                             fcntl(accept_fd, F_SETFL,flags|O_NONBLOCK);  
  11.                             epoll_ctl(epfd,EPOLL_CTL_ADD,accept_fd,&ev);  
  12.                    }  
  13.                    else if(events[i].events&EPOLLIN){  
  14.                             recv( events[i].data.fd ,&in_buf,1024 ,0 );  
  15.                    }  
  16. }  

Ø  AIO

在windows上微软实现了异步IO,通过AIO可以方便的实现高并发的服务器。框架如下:

[cpp]  view plain copy
  1. WSAStartup( 0x0202 ,  & wsaData)  
  2. CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,  0 ,  0 )  
  3. WSASocket(AF_INET,SOCK_STREAM,  0 , NULL,  0 , WSA_FLAG_OVERLAPPED)  
  4. bind(Listen, (PSOCKADDR)  & InternetAddr,  sizeof (InternetAddr))  
  5. listen(Listen,  5 )  
  6. WSAAccept(Listen, NULL, NULL,NULL,  0 )  
  7. PerHandleData  = (LPPER_HANDLE_DATA) GlobalAlloc(GPTR, sizeof (PER_HANDLE_DATA)  
  8. CreateIoCompletionPort((HANDLE)Accept, CompletionPort, (DWORD) PerHandleData, 0 )  
  9. PerIoData= (LPPER_IO_OPERATION_DATA)GlobalAlloc(GPTR, sizeof (PER_IO_OPERATION_DATA))  
  10. WSARecv(Accept,&(PerIoData->DataBuf),1,&RecvBytes,&Flags,&(PerIoData->Overlapped), NULL)  
  11. (GetQueuedCompletionStatus(CompletionPort,  & BytesTransferred,  
  12.          (LPDWORD) & PerHandleData,(LPOVERLAPPED  * )  & PerIoData, INFINITE)  
  13. if  (PerIoData -> BytesRECV  > PerIoData -> BytesSEND){  
  14. WSASend(PerHandleData-> Socket,  & (PerIoData ->DataBuf),  1 ,  & SendBytes,  0 ,  
  15.              & (PerIoData ->Overlapped), NULL)  
  16. }  

3)    引入线程池和事件分离器后

由于上边只是单纯的使用非阻塞Socket和异步IO的方式。提高了接收连接和处理的速度。但是还是不能解决两个客户端同时连接的问题。这时就需要引入多线程机制。引入多线程后,又有许多策略。Linux上通常采用主进程负责接收连接,之后fork子进程处理连接。Windows通常采用线程池方式,避免线程创建和销毁的开销,当然linux上也可以采用线程池方式。采用多进程和多线程方式后。事件处理也可以再优化,定义一个简单的事件处理器,把所有事件放入一个队列,各个线程去事件队列取相应的事件,然后自己开始工作。这就是我上边提到的半同步/半异步方式了。如果线程工作的时候是接收到连接后,自己处理后续的发送和接收,然后选出另外一个线程作为领导继续接收连接,其它线程作为追随者。这就是领导者/追随者模式了。具体可以参考ACE的Reactor和Preactor的具体实现。半同步和/半异步网上也有很多的讨论,可以自己深入研究。代码就比较复杂了,这里就不给出代码了。给出一个linux下类似,相对简单的fork子进程+epoll方式的实现:

[cpp]  view plain copy
  1. #include<sys/socket.h>  
  2. #include <sys/wait.h>  
  3. #include <netinet/in.h>  
  4. #include <netinet/tcp.h>  
  5. #include <sys/epoll.h>  
  6. #include <sys/sendfile.h>  
  7. #include <sys/stat.h>  
  8. #include <unistd.h>  
  9. #include <stdio.h>  
  10. #include <stdlib.h>  
  11. #include <string.h>  
  12. #include <strings.h>  
  13. #include <fcntl.h>  
  14. #include <errno.h>  
  15.   
  16. #define HANDLE_INFO   1  
  17. #define HANDLE_SEND   2  
  18. #define HANDLE_DEL    3  
  19. #define HANDLE_CLOSE  4  
  20.   
  21. #define MAX_REQLEN         1024  
  22. #define MAX_PROCESS_CONN    3  
  23. #define FIN_CHAR           0x00  
  24. #define SUCCESS  0  
  25. #define ERROR   -1  
  26.   
  27. typedef struct event_handle{  
  28.     int socket_fd;  
  29.     int file_fd;  
  30.     int file_pos;  
  31.     int epoll_fd;  
  32.     char request[MAX_REQLEN];  
  33.     int request_len;  
  34.     int ( * read_handle )( struct event_handle * ev );  
  35.     int ( * write_handle )( struct event_handle * ev );  
  36.     int handle_method;  
  37. } EV,* EH;  
  38. typedef int ( * EVENT_HANDLE )( struct event_handle * ev );  
  39.   
  40. int create_listen_fd( int port ){  
  41.     int listen_fd;  
  42.     struct sockaddr_inmy_addr;  
  43.     if( ( listen_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 ){  
  44.         perror( "create socket error" );  
  45.         exit( 1 );  
  46.     }  
  47.     int flag;  
  48.     int olen = sizeof(int);  
  49.     if( setsockopt( listen_fd, SOL_SOCKET, SO_REUSEADDR  
  50.                        , (const void *)&flag, olen ) == -1 ){  
  51.         perror( "setsockopt error" );  
  52.     }  
  53.     flag = 5;  
  54.     if( setsockopt( listen_fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &flag, olen ) == -1 ){  
  55.         perror( "setsockopt error" );  
  56.     }  
  57.     flag = 1;  
  58.     if( setsockopt( listen_fd, IPPROTO_TCP, TCP_CORK, &flag, olen ) == -1 ){  
  59.         perror( "setsockopt error" );  
  60.     }  
  61.     int flags = fcntl( listen_fd, F_GETFL, 0 );  
  62.     fcntl( listen_fd, F_SETFL, flags|O_NONBLOCK );  
  63.     my_addr.sin_family = AF_INET;  
  64.     my_addr.sin_port = htons( port );  
  65.     my_addr.sin_addr.s_addr = INADDR_ANY;  
  66.     bzero( &( my_addr.sin_zero ), 8 );  
  67.     if( bind( listen_fd, ( struct sockaddr * )&my_addr,  
  68.     sizeofstruct sockaddr_in ) ) == -1 ) {  
  69.         perror( "bind error" );  
  70.         exit( 1 );  
  71.     }  
  72.     if( listen( listen_fd, 1 ) == -1 ){  
  73.         perror( "listen error" );  
  74.         exit( 1 );  
  75.     }  
  76.     return listen_fd;  
  77. }  
  78.   
  79. int create_accept_fd( int listen_fd ){  
  80.     int addr_len = sizeofstruct sockaddr_in );  
  81.     struct sockaddr_inremote_addr;  
  82.     int accept_fd = accept( listen_fd,  
  83.         ( struct sockaddr * )&remote_addr, &addr_len );  
  84.     int flags = fcntl( accept_fd, F_GETFL, 0 );  
  85.     fcntl( accept_fd, F_SETFL, flags|O_NONBLOCK );  
  86.     return accept_fd;  
  87. }  
  88.   
  89. int fork_process( int process_num ){  
  90.     int i;  
  91.     int pid=-1;  
  92.     for( i = 0; i < process_num; i++ ){  
  93.         if( pid != 0 ){  
  94.             pid = fork();  
  95.         }  
  96.     }  
  97.     return pid;  
  98. }  
  99.   
  100. int init_evhandle(EH ev,int socket_fd,int epoll_fd,EVENT_HANDLEr_handle,EVENT_HANDLE w_handle){  
  101.     ev->epoll_fd = epoll_fd;  
  102.     ev->socket_fd = socket_fd;  
  103.     ev->read_handle = r_handle;  
  104.     ev->write_handle = w_handle;  
  105.     ev->file_pos = 0;  
  106.     ev->request_len = 0;  
  107.     ev->handle_method = 0;  
  108.     memset( ev->request, 0, 1024 );  
  109. }  
  110. //accept->accept_queue->request->request_queue->output->output_queue  
  111. //multi process sendfile  
  112. int parse_request(EH ev){  
  113.     ev->request_len--;  
  114.     *( ev->request + ev->request_len - 1 ) = 0x00;  
  115.     int i;  
  116.     for( i=0; i<ev->request_len; i++ ){  
  117.         if( ev->request[i] == ':' ){  
  118.             ev->request_len = ev->request_len-i-1;  
  119.             char temp[MAX_REQLEN];  
  120.             memcpy( temp, ev->request, i );  
  121.             ev->handle_method = atoi( temp );  
  122.             memcpy( temp, ev->request+i+1, ev->request_len );  
  123.             memcpy( ev->request, temp, ev->request_len );  
  124.             break;  
  125.         }  
  126.     }  
  127.     //handle_request(ev );  
  128.     //registerto epoll EPOLLOUT  
  129.   
  130.     struct epoll_eventev_temp;  
  131.     ev_temp.data.ptr = ev;  
  132.     ev_temp.events = EPOLLOUT|EPOLLET;  
  133.     epoll_ctl( ev->epoll_fd, EPOLL_CTL_MOD, ev->socket_fd, &ev_temp );  
  134.     return SUCCESS;  
  135. }  
  136.   
  137. int handle_request(EH ev){  
  138.     struct statfile_info;  
  139.     switch( ev->handle_method ){  
  140.         case HANDLE_INFO:  
  141.             ev->file_fd = open( ev->request, O_RDONLY );  
  142.             if( ev->file_fd == -1 ){  
  143.                send( ev->socket_fd, "open file failed\n", strlen("open file failed\n"), 0 );  
  144.                return -1;  
  145.             }  
  146.             fstat(ev->file_fd, &file_info);  
  147.             char info[MAX_REQLEN];  
  148.             sprintf(info,"filelen:%d\n",file_info.st_size);  
  149.             send( ev->socket_fd, info, strlen( info ), 0 );  
  150.             break;  
  151.         case HANDLE_SEND:  
  152.             ev->file_fd = open( ev->request, O_RDONLY );  
  153.             if( ev->file_fd == -1 ){  
  154.                send( ev->socket_fd, "open file failed\n", strlen("open file failed\n"), 0 );  
  155.                return -1;  
  156.             }  
  157.             fstat(ev->file_fd, &file_info);  
  158.             sendfile( ev->socket_fd, ev->file_fd, 0, file_info.st_size );  
  159.             break;  
  160.         case HANDLE_DEL:  
  161.             break;  
  162.         case HANDLE_CLOSE:  
  163.             break;  
  164.     }  
  165.     finish_request( ev );  
  166.     return SUCCESS;  
  167. }  
  168.   
  169. int finish_request(EH ev){  
  170.     close(ev->socket_fd);  
  171.     close(ev->file_fd);  
  172.     ev->handle_method = -1;  
  173.     clean_request( ev );  
  174.     return SUCCESS;  
  175. }  
  176.   
  177. int clean_request(EH ev){  
  178.     memset( ev->request, 0, MAX_REQLEN );  
  179.     ev->request_len = 0;  
  180. }  
  181.   
  182. int read_hook_v2( EH ev ){  
  183.     char in_buf[MAX_REQLEN];  
  184.     memset( in_buf, 0, MAX_REQLEN );  
  185.     int recv_num = recv( ev->socket_fd, &in_buf, MAX_REQLEN, 0 );  
  186.     if( recv_num ==0 ){  
  187.         close( ev->socket_fd );  
  188.         return ERROR;  
  189.     }  
  190.     else{  
  191.         //checkifoverflow  
  192.         if( ev->request_len > MAX_REQLEN-recv_num ){  
  193.             close( ev->socket_fd );  
  194.            clean_request( ev );  
  195.         }  
  196.         memcpy( ev->request + ev->request_len, in_buf, recv_num );  
  197.         ev->request_len += recv_num;  
  198.         if( recv_num == 2 && ( !memcmp( &in_buf[recv_num-2], "\r\n", 2 ) ) ){  
  199.            parse_request(ev);  
  200.         }  
  201.     }  
  202.     return recv_num;  
  203. }  
  204.   
  205. int write_hook_v1( EH ev ){  
  206.     struct statfile_info;  
  207.     ev->file_fd = open( ev->request, O_RDONLY );  
  208.     if( ev->file_fd == ERROR ){  
  209.         send( ev->socket_fd, "openfile failed\n", strlen("openfile failed\n"), 0 );  
  210.         return ERROR;  
  211.     }  
  212.     fstat(ev->file_fd, &file_info);  
  213.     int write_num;  
  214.     while(1){  
  215.         write_num = sendfile( ev->socket_fd, ev->file_fd, (off_t *)&ev->file_pos, 10240 );  
  216.         ev->file_pos += write_num;  
  217.         if( write_num == ERROR ){  
  218.             if( errno == EAGAIN ){  
  219.                break;  
  220.             }  
  221.         }  
  222.         else if( write_num == 0 ){  
  223.             printf( "writed:%d\n", ev->file_pos );  
  224.             //finish_request(ev );  
  225.             break;  
  226.         }  
  227.     }  
  228.     return SUCCESS;  
  229. }  
  230.   
  231. int main(){  
  232.     int listen_fd = create_listen_fd( 3389 );  
  233.     int pid = fork_process( 3 );  
  234.     if( pid == 0 ){  
  235.         int accept_handles = 0;  
  236.         struct epoll_eventev, events[20];  
  237.         int epfd = epoll_create( 256 );  
  238.         int ev_s = 0;  
  239.   
  240.         ev.data.fd = listen_fd;  
  241.         ev.events = EPOLLIN|EPOLLET;  
  242.         epoll_ctl( epfd, EPOLL_CTL_ADD, listen_fd, &ev );  
  243.         struct event_handleev_handles[256];  
  244.         for( ;; ){  
  245.             ev_s = epoll_wait( epfd, events, 20, 500 );  
  246.             int i = 0;  
  247.             for( i = 0; i<ev_s; i++ ){  
  248.                if( events[i].data.fd == listen_fd ){  
  249.                    if( accept_handles < MAX_PROCESS_CONN ){  
  250.                        accept_handles++;  
  251.                        int accept_fd = create_accept_fd( listen_fd );  
  252.                        init_evhandle(&ev_handles[accept_handles],accept_fd,epfd,read_hook_v2,write_hook_v1);  
  253.                        ev.data.ptr = &ev_handles[accept_handles];  
  254.                        ev.events = EPOLLIN|EPOLLET;  
  255.                        epoll_ctl( epfd, EPOLL_CTL_ADD, accept_fd, &ev );  
  256.                    }  
  257.                }  
  258.                else if( events[i].events&EPOLLIN ){  
  259.                    EVENT_HANDLE current_handle = ( ( EH )( events[i].data.ptr ) )->read_handle;  
  260.                    EH current_event = ( EH )( events[i].data.ptr );  
  261.                    ( *current_handle )( current_event );  
  262.                }  
  263.                else if( events[i].events&EPOLLOUT ){  
  264.                    EVENT_HANDLE current_handle = ( ( EH )( events[i].data.ptr ) )->write_handle;  
  265.                    EH current_event = ( EH )( events[i].data.ptr );  
  266.                    if( ( *current_handle )( current_event )  == 0 ){  
  267.                        accept_handles--;  
  268.                    }  
  269.                }  
  270.             }  
  271.         }  
  272.     }  
  273.     else{  
  274.         //managerthe process  
  275.         int child_process_status;  
  276.         wait( &child_process_status );  
  277.     }  
  278.   
  279.     return SUCCESS;  
  280. }  

三、     分布式系统设计

前面讲述了分布式系统中的核心的服务器的实现。可以是http服务器,缓存服务器,分布式文件系统等的内部实现。下边主要从一个高并发的大型网站出发,看一个高并发系统的设计。下边是一个高并发系统的逻辑结构:


主要是参考这篇文章http://www.chinaz.com/web/2010/0310/108211.shtml。下边主要想从这个架构的各个部分的实现展开。

1.     缓存系统

缓存是每一个高并发,高可用系统不可或缺的模块。下边就几个常见缓存系统系统进行介绍。

Squid

Squid作为一个前端缓存,通常部署在网络的离用户最近的地方,通过缓存网站的页面,使用户不必每次都跑到服务器去取数据,提高系统响应和性能。实现应该比较简单:一个带有存储功能的代理。用户访问页面的时候,由它代理,然后存储请求结果,下次再访问的时候,查看是否需要更新,有更新就去服务器取新数据,否则直接返回用户页面。

 

Ehcache

Ehcache是一个对象缓存系统。通常在J2EE中配合Hibernate使用,这里请原谅作者本人之前是做J2EE开发的,其它使用方式暂不是很了解。应用查询数据库,对经常需要查询,却更新不频繁的数据,可以放入ehcache缓存,提高访问速度。Ehcahe支持内存缓存和硬盘两种方式,支持分布式缓存。数据缓存的基本原理就是:为需要缓存的对象建立一个map,临时对象放入map,查询的时候先查询map,没有找到再查找数据库。关机时可以序列化到硬盘。分布式缓存没有研究过。

页面缓存和动态页面静态化

在大型网站经常使用的一种缓存技术就是动态页面的缓存。由于动态页面经常更新,上边的缓存就不起作用了。通常会采用SSI(Server side include)等技术将动态页面的或者页面片段进行缓存。

还有一种就是动态页面静态化。下边是一本讲spring的书中给出的j2EE中的动态页面静态化的示例:

[java]  view plain copy
  1. /** 
  2.  * 动态内容静态化的Filter。将变化非常缓慢的动态文件生成静态文件。 
  3.  */  
  4. package com.zsl.cache.filter;  
  5.    
  6. import java.io.File;  
  7. import java.io.IOException;  
  8. import java.io.UnsupportedEncodingException;  
  9. import java.net.URLEncoder;  
  10. import java.util.Map;  
  11.    
  12. importjavax.management.RuntimeErrorException;  
  13. importjavax.servlet.FilterChain;  
  14. importjavax.servlet.ServletException;  
  15. importjavax.servlet.ServletRequest;  
  16. importjavax.servlet.ServletResponse;  
  17. importjavax.servlet.http.HttpServletRequest;  
  18. importjavax.servlet.http.HttpServletResponse;  
  19.    
  20. importorg.apache.commons.io.FilenameUtils;  
  21. importorg.apache.http.HttpResponse;  
  22. importorg.springframework.core.io.Resource;  
  23.    
  24. importcom.sun.xml.bind.v2.runtime.output.Encoded;  
  25.    
  26.    
  27. /** 
  28.  * @author zsl 
  29.  * 
  30.  */  
  31. public class FileCacheFilterextends AbstractCacheFilter{  
  32.          private String root;  
  33.           
  34.          private final String SUFFIX = ".html";  
  35.           
  36.          public final void setFileDir(Resource dir){  
  37.                    try {  
  38.                             File f = dir.getFile();  
  39.                             f.mkdirs();  
  40.                             if(!f.isDirectory()){  
  41.                                      throw newIllegalArgumentException("Invalid directory: "+f.getPath());  
  42.                             }  
  43.                             if(!f.canWrite())  
  44.                                      throw newIllegalArgumentException("Cannot write to directory: "+f.getPath());  
  45.                             root = f.getPath();  
  46.                              
  47.                             if(!root.endsWith("/")&&!root.endsWith("//"))  
  48.                                      root = root+"/";  
  49.                    } catch (IOException e) {  
  50.                             // TODO Auto-generated catch block  
  51.                             throw new IllegalArgumentException(e);  
  52.                    }  
  53.          }  
  54.           
  55.          public void afterPropertiesSet() throws Exception {  
  56.                    super.afterPropertiesSet();  
  57.                    if(!new File(root).isDirectory()){  
  58.                             throw newIllegalArgumentException("No directory: "+root);  
  59.                    }  
  60.          }  
  61.           
  62.          public void doFilter(ServletRequest request,ServletResponseresponse,FilterChain chain) throws IOException,ServletException {  
  63.                    HttpServletRequest httpRequest =(HttpServletRequest)request;  
  64.                    String key = getKey(httpRequest);  
  65.                    if(key == null){  
  66.                             chain.doFilter(request,response);  
  67.                    }else{  
  68.                             File file = key2File(key);  
  69.                             if(file.isFile()){  
  70.                                      HttpServletResponse httpResponse= (HttpServletResponse)response;  
  71.                                      httpResponse.setContentType(getContentType());  
  72.                                      httpResponse.setHeader("Content-Encoding","gzip");  
  73.                                      httpResponse.setContentLength((int)file.length());  
  74.                                      FileUtil.readFil(file,httpResponse.getOutputStream());  
  75.                             }else{  
  76.                                      //缓存未找到文件  
  77.                                      HttpServletResponse httpResponse= (HttpServletResponse)response;  
  78.                                      CachedResponseWrapper wrapper =new CachedResponseWrapper(httpResponse);  
  79.                                      chain.doFilter(request,response);  
  80.                                      if(wrapper.getStatus() ==HttpServletResponse.SC_OK){  
  81.                                                byte[] data =GZipUtil.gzip(wrapper.getResponseData());  
  82.                                                FileUtil.writeFile(file,data);  
  83.                                                httpResponse.setContentType(getContentType());  
  84.                                                httpResponse.setHeader("Content-Encoding","gzip");  
  85.                                                httpResponse.setContentLength(data.length);  
  86.                                                httpResponse.getOutputStream().write(data);  
  87.                                      }  
  88.                             }  
  89.                    }  
  90.                     
  91.          }  
  92.           
  93.          private File key2File(String key){  
  94.                    int  hash =key.hashCode();  
  95.                    int dir1 = (hash &0xff00)>>8;  
  96.                    int dir2 = hash & 0xff;  
  97.                    String  dir= root+dir1+"/"+dir2;  
  98.                    File fdir = new File(dir);  
  99.                    if(!fdir.isAbsolute()){  
  100.                             if(!fdir.mkdirs()){  
  101.                                      return null;  
  102.                             }  
  103.                    }  
  104.                    return newFile(dir+"/"+encode(key)+SUFFIX);  
  105.          }  
  106.           
  107.          private String encode(String key){  
  108.                    try {  
  109.                             return URLEncoder.encode(key,"UTF-8");  
  110.                    } catch (UnsupportedEncodingException e) {  
  111.                             throw new RuntimeException(e);  
  112.                    }  
  113.                     
  114.          }  
  115.           
  116.          public void remove(String url,Map<String,String>parameters){  
  117.                    String key =getKey(HttpServletRequestFactory.create(url,parameters));  
  118.                    if(key != null){  
  119.                             FileUtil.remveFile(key2File(key));  
  120.                    }  
  121.          }  
  122. }  

书中给到的另外一个客户端缓存,不知道该归那类:

[java]  view plain copy
  1. /** 
  2.  * 网页静态资源(gif图像,css资源的缓存时间设置filter,避免频繁请求静态资源 
  3.  */  
  4. package com.zsl.cache.filter;  
  5.    
  6. import java.io.IOException;  
  7. import java.util.Enumeration;  
  8. import java.util.Map;  
  9.    
  10. importjavax.servlet.FilterChain;  
  11. importjavax.servlet.FilterConfig;  
  12. importjavax.servlet.ServletException;  
  13. import javax.servlet.Filter;  
  14. importjavax.servlet.ServletRequest;  
  15. importjavax.servlet.ServletResponse;  
  16. importjavax.servlet.http.HttpServletRequest;  
  17. importjavax.servlet.http.HttpServletResponse;  
  18.    
  19. importorg.apache.commons.collections.map.HashedMap;  
  20. importorg.apache.commons.logging.Log;  
  21. importorg.apache.commons.logging.LogFactory;  
  22.    
  23.    
  24. /** 
  25.  * @author zsl 
  26.  * 
  27.  */  
  28. public class ExpireFilterimplements Filter {  
  29.    
  30.          private Log log = LogFactory.getLog(ExpireFilter.class);  
  31.           
  32.          private Map<String, Long> map = new HashedMap();  
  33.    
  34.          @Override  
  35.          public void destroy() {  
  36.                    log.info("destory ExpiredFilter");  
  37.          }  
  38.    
  39.          @Override  
  40.          public void doFilter(ServletRequest request, ServletResponseresponse,  
  41.                             FilterChain chain) throws IOException,ServletException {  
  42.                             String uriString =((HttpServletRequest)request).getRequestURI();  
  43.                             int n = uriString.lastIndexOf('.');  
  44.                             if(n!= -1){  
  45.                                      String ext =uriString.substring(n);  
  46.                                      Long exp = map.get(ext);  
  47.                                      if(exp != null){  
  48.                                                HttpServletResponseresp = (HttpServletResponse)response;  
  49.                                                resp.setHeader("Expires",System.currentTimeMillis()+exp*1000+"");  
  50.                                      }  
  51.                             }  
  52.                             chain.doFilter(request,response);  
  53.          }  
  54.    
  55.          @Override  
  56.          public void init(FilterConfig config) throwsServletException {  
  57.                    Enumeration em = config.getInitParameterNames();  
  58.                    while(em.hasMoreElements()){  
  59.                             String paramName =em.nextElement().toString();  
  60.                             String paramValue = config.getInitParameter(paramName);  
  61.                             try {  
  62.                                      int time =Integer.valueOf(paramValue);  
  63.                                      if(time>0){  
  64.                                                log.info("set"+paramName + " expired seconds: "+time);  
  65.                                                map.put(paramName, newLong(time));  
  66.                                      }  
  67.                             } catch (Exception e) {  
  68.                                      log.warn("Exception ininitilizing ExpiredFilter.",e);  
  69.                             }  
  70.                    }  
  71.          }  
  72. }  

2.     负载均衡系统

Ø  负载均衡策略

负载均衡策略有随机分配,平均分配,分布式一致性hash等。随机分配就是通过随机数选择一个服务器来服务。平均分配就是一次循环分配一次。分布式一致性hash算法,比较负载,把资源和节点映射到一个换上,然后通过一定的算法资源对应到节点上,使得添加和去掉服务器变得非常容易,减少对其它服务器的影响。很有名的一个算法,据说是P2P的基础。了解不是很深,就不详细说了,要露马脚了。

Ø  软件负载均衡

软件负载均衡可以采用很多方案,常见的几个方案有:

基于DNS的负载均衡,通过DNS正向区域的配置,将一个域名根据一定的策略解析到多个ip地址,实现负载均衡,这里需要DNS服务器的配合。

基于LVS的负载均衡。LVS可以将多个linux服务器做成一个虚拟的服务器,对外提供服务器,实现负载均衡。

基于Iptables的负载均衡。Iptables可以通过做nat,对外提供一个虚拟IP,对内映射到多个服务器实现负载均衡。基本上可以和硬件均衡方案一致了,这里的linux服务器相当于一台路由器。

Ø  硬件负载均衡

基于路由器的负载均衡,在路由器上配置nat实现负载均衡。对外网一个虚拟IP,内网映射几个内网IP。

一些网络设备厂商也提供了一些负载均衡的设备,如F5,不过价格不菲哦。

数据库的负载均衡

数据库的负载均衡可以是数据库厂商提供的集群方案。

――――――――――――――――――――――――――――――――――――――

今天先写到这里,这个题目太大了,东西太多。后边是将来要写的。还没有组织出来。

好多东西也没有展开。东西太多了。

――――――――――――――――――――――――――――――――――――――

分布式文件系统

Gfs

hfs

Map Reduce系统

 

云计算


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值