summaryrefslogtreecommitdiff
path: root/libmemcachedutil/memcached_pool.c
blob: afc2fee3c26304cd24302d84074ee4009bc57f2e (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
#include "libmemcached/common.h"
#include "libmemcached/memcached_pool.h"

#include <errno.h>
#include <pthread.h>

struct memcached_pool_st 
{
  pthread_mutex_t mutex;
  pthread_cond_t cond;
  memcached_st *master;
  memcached_st **mmc;
  int firstfree;
  uint32_t size;
  uint32_t current_size;
  char* version;
};

static memcached_return mutex_enter(pthread_mutex_t *mutex) 
{
  int ret;
  do 
    ret= pthread_mutex_lock(mutex);
  while (ret == -1 && errno == EINTR);

  return (ret == -1) ? MEMCACHED_ERRNO : MEMCACHED_SUCCESS;
}

static memcached_return mutex_exit(pthread_mutex_t *mutex) {
  int ret;
  do
    ret= pthread_mutex_unlock(mutex);
  while (ret == -1 && errno == EINTR);

  return (ret == -1) ? MEMCACHED_ERRNO : MEMCACHED_SUCCESS;
}

/**
 * Grow the connection pool by creating a connection structure and clone the
 * original memcached handle.
 */
static int grow_pool(memcached_pool_st* pool) {
  memcached_st *obj= calloc(1, sizeof(*obj));
  if (obj == NULL)
    return -1;

  if (memcached_clone(obj, pool->master) == NULL)
  {
    free(obj);
    return -1;
  }

  pool->mmc[++pool->firstfree] = obj;
  pool->current_size++;

  return 0;
}

memcached_pool_st *memcached_pool_create(memcached_st* mmc, 
                                         uint32_t initial, uint32_t max) 
{
  memcached_pool_st* ret = NULL;
  memcached_pool_st object = { .mutex = PTHREAD_MUTEX_INITIALIZER, 
                               .cond = PTHREAD_COND_INITIALIZER,
                               .master = mmc,
                               .mmc = calloc(max, sizeof(memcached_st*)),
                               .firstfree = -1,
                               .size = max, 
                               .current_size = 0 };

  if (object.mmc != NULL) 
  {
    ret= calloc(1, sizeof(*ret));
    if (ret == NULL) 
    {
      free(object.mmc);
      return NULL;
    } 

    *ret = object;

    /* Try to create the initial size of the pool. An allocation failure at
     * this time is not fatal..
     */
    for (unsigned int ii=0; ii < initial; ++ii)
      if (grow_pool(ret) == -1)
        break;
  }

  return ret;
}

memcached_st*  memcached_pool_destroy(memcached_pool_st* pool) 
{
  memcached_st *ret = pool->master;

  for (int xx= 0; xx <= pool->firstfree; ++xx) 
  {
    memcached_free(pool->mmc[xx]);
    free(pool->mmc[xx]);
    pool->mmc[xx] = NULL;
  }
  
  pthread_mutex_destroy(&pool->mutex);
  pthread_cond_destroy(&pool->cond);
  free(pool->mmc);
  free(pool);

  return ret;
}

memcached_st* memcached_pool_pop(memcached_pool_st* pool,
                                 bool block,
                                 memcached_return *rc) 
{
  memcached_st *ret= NULL;
  if ((*rc= mutex_enter(&pool->mutex)) != MEMCACHED_SUCCESS) 
    return NULL;

  do 
  {
    if (pool->firstfree > -1)
       ret= pool->mmc[pool->firstfree--];
    else if (pool->current_size == pool->size) 
    {
      if (!block) 
      {
        *rc= mutex_exit(&pool->mutex);
        return NULL;
      }

      if (pthread_cond_wait(&pool->cond, &pool->mutex) == -1) 
      {
        int err = errno;
        mutex_exit(&pool->mutex);
        errno = err; 
        *rc= MEMCACHED_ERRNO;
        return NULL;
      }
    } 
    else if (grow_pool(pool) == -1) 
    {
       *rc= mutex_exit(&pool->mutex);
       return NULL;
    }
  } 
  while (ret == NULL);

  *rc= mutex_exit(&pool->mutex);

  return ret;
}

memcached_return memcached_pool_push(memcached_pool_st* pool, 
                                     memcached_st *mmc)
{
  memcached_return rc= mutex_enter(&pool->mutex);

  if (rc != MEMCACHED_SUCCESS)
    return rc;

  char* version = memcached_get_user_data(mmc);
  /* Someone updated the behavior on the object.. */
  if (version != pool->version) 
  {
    memcached_free(mmc);
    memset(mmc, 0, sizeof(*mmc));
    if (memcached_clone(mmc, pool->master) == NULL)
    {
      rc= MEMCACHED_SOME_ERRORS;
    }
  }

  pool->mmc[++pool->firstfree]= mmc;

  if (pool->firstfree == 0 && pool->current_size == pool->size) 
  {
    /* we might have people waiting for a connection.. wake them up :-) */
    pthread_cond_broadcast(&pool->cond);
  }

  memcached_return rval= mutex_exit(&pool->mutex);
  if (rc == MEMCACHED_SOME_ERRORS)
    return rc;

  return rval;
}


memcached_return memcached_pool_behavior_set(memcached_pool_st *pool, 
                                             memcached_behavior flag, 
                                             uint64_t data)
{

  memcached_return rc= mutex_enter(&pool->mutex);
  if (rc != MEMCACHED_SUCCESS)
     return rc;

  /* update the master */
  rc= memcached_behavior_set(pool->master, flag, data);
  if (rc != MEMCACHED_SUCCESS)
  {
    mutex_exit(&pool->mutex);
    return rc;
  }

  ++pool->version;
  memcached_set_user_data(pool->master, pool->version);
  /* update the clones */
  for (int xx= 0; xx <= pool->firstfree; ++xx) 
  {
    rc= memcached_behavior_set(pool->mmc[xx], flag, data);
    if (rc == MEMCACHED_SUCCESS)
      memcached_set_user_data(pool->mmc[xx], pool->version);
    else
    {
      memcached_free(pool->mmc[xx]);
      memset(pool->mmc[xx], 0, sizeof(*pool->mmc[xx]));
      if (memcached_clone(pool->mmc[xx], pool->master) == NULL)
      {
        /* I'm not sure what to do in this case.. this would happen
           if we fail to push the server list inside the client..
           I should add a testcase for this, but I believe the following
           would work, except that you would add a hole in the pool list..
           in theory you could end up with an empty pool....
        */
        free(pool->mmc[xx]);
        pool->mmc[xx]= NULL;
      }
    }
  }

  return mutex_exit(&pool->mutex);
}

memcached_return memcached_pool_behavior_get(memcached_pool_st *pool, 
                                             memcached_behavior flag,
                                             uint64_t *value)
{
  memcached_return rc= mutex_enter(&pool->mutex);
  if (rc != MEMCACHED_SUCCESS)
    return rc;
  *value= memcached_behavior_get(pool->master, flag);
  return mutex_exit(&pool->mutex);
}