Changeset cd26d0


Ignore:
Timestamp:
01/19/09 13:06:02 (3 years ago)
Author:
George Potapov <nephrite@…>
Branches:
master-v0.7, upstream_count_limit
Children:
f8bbb6
Parents:
ca37e8 (diff), b3765f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
George Potapov <nephrite@…> (01/19/09 13:06:02)
git-committer:
George Potapov <nephrite@…> (01/19/09 13:06:02)
Message:

Merge branch 'master-v0.7' of  git://openhack.ru/nginx-patched into master-v0.7

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • memcached_hash/Changes

    r413b1d r38354f  
    11Revision history of ngx_http_upstream_memcached_hash_module. 
     2 
     30.04  ??? 
     4        - ??? 
     5 
     6        Do not change callback and data pointers passed by nginx after 
     7        module initialization.  This makes the module compatible with 
     8        other intermediate "balancers" up the chain.  Such balancers 
     9        do not perform actual balancing, but may process host address 
     10        that we return, for instance map it to cached open connection. 
     11 
    212 
    3130.03  2008-05-01 
  • memcached_hash/README

    rfb3f9e r9ceabd  
    1 ngx_http_upstream_memcached_hash_module 0.03 
     1ngx_http_upstream_memcached_hash_module 0.04 
    22============================================ 
    33 
  • memcached_hash/dist.sh

    r817d03 rb3765f  
    11#! /bin/sh 
     2 
     3BRANCH=$1 
     4if [ "x$BRANCH" = "x" ]; then 
     5    echo "Usage: $0 NGINX-BRANCH" 
     6    exit 1 
     7fi 
    28 
    39PWD=`pwd` 
  • memcached_hash/ngx_http_upstream_memcached_hash_module.c

    ra1f117 r12bc79  
    11/* 
    2   Copyright (C) 2007-2008 Tomash Brechko.  All rights reserved. 
     2  Copyright (C) 2007-2009 Tomash Brechko.  All rights reserved. 
    33 
    44  Development of this module was sponsored by Monashev Co. Ltd. 
     
    77  source code. 
    88 
    9   Version 0.03. 
     9  Version 0.04. 
    1010*/ 
    1111 
     
    5555  ngx_http_upstream_server_t *server; 
    5656  ngx_http_request_t *request; 
     57  struct memcached_hash_peer *peer; 
    5758}; 
    5859 
     
    9798 
    9899static 
    99 ngx_int_t 
    100 memcached_hash_get_peer(ngx_peer_connection_t *pc, void *data) 
    101 { 
    102   struct memcached_hash_peer *peer = data; 
    103   ngx_peer_addr_t *addr; 
    104  
    105   if (peer->server->down) 
    106     goto fail; 
    107  
    108   if (peer->server->max_fails > 0 && peer->fails >= peer->server->max_fails) 
    109     { 
    110       time_t now = ngx_time(); 
    111       if (now - peer->accessed <= peer->server->fail_timeout) 
    112         goto fail; 
    113       else 
    114         peer->fails = 0; 
    115     } 
    116  
    117   addr = &peer->server->addrs[peer->addr_index]; 
    118  
    119   pc->sockaddr = addr->sockaddr; 
    120   pc->socklen = addr->socklen; 
    121   pc->name = &addr->name; 
    122  
    123   return NGX_OK; 
    124  
    125 fail: 
    126   /* This is the last try.  */ 
    127   pc->tries = 1; 
    128  
    129   return NGX_BUSY; 
    130 } 
    131  
    132  
    133 static 
    134 ngx_int_t 
    135 memcached_hash_find_peer(ngx_peer_connection_t *pc, void *data) 
    136 { 
    137   struct memcached_hash_find_ctx *find_ctx = data; 
     100unsigned int 
     101memcached_hash_find_peer(struct memcached_hash_find_ctx *find_ctx) 
     102{ 
    138103  struct memcached_hash *memd = find_ctx->memd; 
    139104  u_char *key; 
     
    185150    } 
    186151 
    187   pc->data = &memd->peers[index]; 
    188   pc->get = memcached_hash_get_peer; 
    189   pc->tries = find_ctx->server[index].naddrs; 
    190  
    191   return memcached_hash_get_peer(pc, pc->data); 
     152  find_ctx->peer = &memd->peers[index]; 
     153 
     154  return index; 
     155} 
     156 
     157 
     158static 
     159ngx_int_t 
     160memcached_hash_get_peer(ngx_peer_connection_t *pc, void *data) 
     161{ 
     162  struct memcached_hash_find_ctx *find_ctx = data; 
     163  struct memcached_hash_peer *peer = find_ctx->peer; 
     164  ngx_peer_addr_t *addr; 
     165 
     166  if (! peer) 
     167    { 
     168      unsigned int index; 
     169 
     170      index = memcached_hash_find_peer(find_ctx); 
     171 
     172      peer = find_ctx->peer; 
     173      pc->tries = find_ctx->server[index].naddrs; 
     174    } 
     175 
     176  if (peer->server->down) 
     177    goto fail; 
     178 
     179  if (peer->server->max_fails > 0 && peer->fails >= peer->server->max_fails) 
     180    { 
     181      time_t now = ngx_time(); 
     182      if (now - peer->accessed <= peer->server->fail_timeout) 
     183        goto fail; 
     184      else 
     185        peer->fails = 0; 
     186    } 
     187 
     188  addr = &peer->server->addrs[peer->addr_index]; 
     189 
     190  pc->sockaddr = addr->sockaddr; 
     191  pc->socklen = addr->socklen; 
     192  pc->name = &addr->name; 
     193 
     194  return NGX_OK; 
     195 
     196fail: 
     197  /* This is the last try.  */ 
     198  pc->tries = 1; 
     199 
     200  return NGX_BUSY; 
    192201} 
    193202 
     
    198207                         ngx_uint_t state) 
    199208{ 
    200   struct memcached_hash_peer *peer = data; 
     209  struct memcached_hash_find_ctx *find_ctx = data; 
     210  struct memcached_hash_peer *peer = find_ctx->peer; 
    201211 
    202212  if (state & NGX_PEER_FAILED) 
     
    243253  find_ctx->request = r; 
    244254  find_ctx->server = us->servers->elts; 
     255  find_ctx->peer = NULL; 
    245256 
    246257  r->upstream->peer.free = memcached_hash_free_peer; 
     
    250261    memcached_hash_find_peer(). 
    251262  */ 
    252   r->upstream->peer.get = memcached_hash_find_peer; 
     263  r->upstream->peer.get = memcached_hash_get_peer; 
    253264  r->upstream->peer.data = find_ctx; 
    254265  r->upstream->peer.tries = 1; 
  • upstream_keepalive/ngx_http_upstream_keepalive_module.c

    r2904d4 rca37e8  
    2626    ngx_http_upstream_keepalive_srv_conf_t  *conf; 
    2727 
     28    ngx_http_upstream_t               *upstream; 
     29 
    2830    void                              *data; 
    2931 
    3032    ngx_event_get_peer_pt              original_get_peer; 
    3133    ngx_event_free_peer_pt             original_free_peer; 
     34 
     35    ngx_uint_t                         failed;       /* unsigned:1 */ 
    3236 
    3337} ngx_http_upstream_keepalive_peer_data_t; 
     
    170174 
    171175    kp->conf = kcf; 
     176    kp->upstream = r->upstream; 
    172177    kp->data = r->upstream->peer.data; 
    173178    kp->original_get_peer = r->upstream->peer.get; 
     
    194199    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, 
    195200                   "get keepalive peer"); 
     201 
     202    kp->failed = 0; 
    196203 
    197204    /* single pool of cached connections */ 
     
    265272    ngx_http_upstream_keepalive_cache_t      *item; 
    266273 
    267     ngx_queue_t       *q; 
    268     ngx_connection_t  *c; 
     274    ngx_uint_t            status; 
     275    ngx_queue_t          *q; 
     276    ngx_connection_t     *c; 
     277    ngx_http_upstream_t  *u; 
    269278 
    270279    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, 
    271280                   "free keepalive peer"); 
    272281 
    273     if (!(state & NGX_PEER_FAILED) 
    274         && pc->connection != NULL) 
     282    /* remember failed state - peer.free() may be called more than once */ 
     283 
     284    if (state & NGX_PEER_FAILED) { 
     285        kp->failed = 1; 
     286    } 
     287 
     288    /* 
     289     * cache valid connections 
     290     * 
     291     * For memcached this means status either 404 or 200.  For status 200 we 
     292     * should also check if all response body was read (u->length == 0) and 
     293     * make sure that u->length is valid (we use u->header_sent flag to test 
     294     * this).  Memcached is the only supported protocol for now. 
     295     * 
     296     * Some notes on other possibilities (incomplete): 
     297     * 
     298     * fastcgi: u->pipe->upstream_done should be sufficient 
     299     * 
     300     * proxy buffered: u->pipe->upstream_done, 304 replies, replies to head 
     301     * requests (see RFC 2616, 4.4 Message Length) 
     302     * 
     303     * proxy unbuffered: 200 as for memcached (with u->length == 0 and 
     304     * header_sent), 304, replies to head requests 
     305     * 
     306     * subrequest_in_memory: won't work as of now 
     307     * 
     308     * TODO: move this logic to protocol modules (NGX_PEER_KEEPALIVE?) 
     309     */ 
     310 
     311    u = kp->upstream; 
     312    status = u->headers_in.status_n; 
     313 
     314    if (!kp->failed 
     315        && pc->connection != NULL 
     316        && (status == NGX_HTTP_NOT_FOUND 
     317            || (status == NGX_HTTP_OK && u->header_sent && u->length == 0))) 
    275318    { 
    276319        c = pc->connection; 
     320 
     321        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, 
     322                       "free keepalive peer: saving connection %p", c); 
    277323 
    278324        if (ngx_queue_empty(&kp->conf->free)) { 
  • upstream_keepalive/t/memcached-keepalive.t

    r2904d4 rca37e8  
    2121plain(skip_all => 'Cache::Memcached not installed') if $@; 
    2222 
    23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(10) 
     23my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(16) 
    2424    ->write_file_expand('nginx.conf', <<'EOF'); 
    2525 
     
    9595EOF 
    9696 
    97 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081'); 
    98 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8082'); 
     97if (`memcached -h` =~ /repcached/) { 
     98    # repcached patches adds additional listen socket memcached 
     99    # that should be different too 
     100 
     101    $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081', 
     102        '-X', '8091'); 
     103    $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8082', 
     104        '-X', '8092'); 
     105 
     106} else { 
     107    $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081'); 
     108    $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8082'); 
     109} 
     110 
    99111$t->run(); 
     112 
     113$t->waitforsocket('127.0.0.1:8081') 
     114    or die "Unable to start memcached"; 
     115$t->waitforsocket('127.0.0.1:8082') 
     116    or die "Unable to start second memcached"; 
    100117 
    101118############################################################################### 
     
    106123$memd1->set('/', 'SEE-THIS'); 
    107124$memd2->set('/', 'SEE-THIS'); 
     125$memd1->set('/big', 'X' x 1000000); 
    108126 
    109127my $total = $memd1->stats()->{total}->{total_connections}; 
     
    120138    'only one connection used'); 
    121139 
     140# Since nginx doesn't read all data from connection in some situations (head 
     141# requests, post_action, errors writing to client) we have to close such 
     142# connections.  Check if we really do close them. 
     143 
     144$total = $memd1->stats()->{total}->{total_connections}; 
     145 
     146unlike(http_head('/'), qr/SEE-THIS/, 'head request'); 
     147like(http_get('/'), qr/SEE-THIS/, 'get after head'); 
     148 
     149is($memd1->stats()->{total}->{total_connections}, $total + 1, 
     150    'head request closes connection'); 
     151 
     152$total = $memd1->stats()->{total}->{total_connections}; 
     153 
     154unlike(http_head('/big'), qr/XXX/, 'big head'); 
     155like(http_get('/'), qr/SEE-THIS/, 'get after big head'); 
     156 
     157is($memd1->stats()->{total}->{total_connections}, $total + 1, 
     158    'big head request closes connection'); 
     159 
    122160# two backends with 'single' option - should establish only one connection 
    123161 
Note: See TracChangeset for help on using the changeset viewer.