~posulliv/drizzle/memcached_applier

« back to all changes in this revision

Viewing changes to drizzled/set_var.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-08-10 02:26:14 UTC
  • mfrom: (1059.3.30 replication)
  • Revision ID: osullivan.padraig@gmail.com-20090810022614-2d29ln52yv0szgi2
Merge with the latest from Jay's replication tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
using namespace std;
72
72
 
73
73
extern const CHARSET_INFO *character_set_filesystem;
74
 
extern I_List<NAMED_LIST> key_caches;
75
74
extern size_t my_thread_stack_size;
76
75
 
77
76
class sys_var_pluginvar;
111
110
static bool get_unsigned64(Session *session, set_var *var);
112
111
bool throw_bounds_warning(Session *session, bool fixed, bool unsignd,
113
112
                          const std::string &name, int64_t val);
114
 
static KEY_CACHE *create_key_cache(const char *name, uint32_t length);
115
113
static unsigned char *get_error_count(Session *session);
116
114
static unsigned char *get_warning_count(Session *session);
117
115
static unsigned char *get_tmpdir(Session *session);
275
273
static sys_var_session_uint32_t sys_net_wait_timeout(&vars, "wait_timeout",
276
274
                                                     &SV::net_wait_timeout);
277
275
 
278
 
/* Condition pushdown to storage engine */
279
 
static sys_var_session_bool
280
 
sys_engine_condition_pushdown(&vars, "engine_condition_pushdown",
281
 
                              &SV::engine_condition_pushdown);
282
 
 
283
276
/* Variables that are bits in Session */
284
277
 
285
278
sys_var_session_bit sys_autocommit(&vars, "autocommit", 0,
1300
1293
}
1301
1294
 
1302
1295
 
1303
 
LEX_STRING default_key_cache_base= {(char *) "default", 7 };
1304
 
 
1305
 
static KEY_CACHE zero_key_cache;
1306
 
 
1307
 
KEY_CACHE *get_key_cache(const LEX_STRING *cache_name)
1308
 
{
1309
 
  safe_mutex_assert_owner(&LOCK_global_system_variables);
1310
 
  if (!cache_name || ! cache_name->length)
1311
 
    cache_name= &default_key_cache_base;
1312
 
  return ((KEY_CACHE*) find_named(&key_caches,
1313
 
                                  cache_name->str, cache_name->length, 0));
1314
 
}
1315
 
 
1316
 
 
1317
1296
unsigned char *sys_var_key_cache_param::value_ptr(Session *, enum_var_type,
1318
 
                                                  const LEX_STRING *base)
1319
 
{
1320
 
  KEY_CACHE *key_cache= get_key_cache(base);
1321
 
  if (!key_cache)
1322
 
    key_cache= &zero_key_cache;
1323
 
  return (unsigned char*) key_cache + offset ;
1324
 
}
 
1297
                                                  const LEX_STRING *)
 
1298
{
 
1299
  return (unsigned char*) dflt_key_cache + offset ;
 
1300
}
 
1301
 
 
1302
/**
 
1303
  Resize key cache.
 
1304
*/
 
1305
static int resize_key_cache_with_lock(KEY_CACHE *key_cache)
 
1306
{
 
1307
  assert(key_cache->key_cache_inited);
 
1308
 
 
1309
  pthread_mutex_lock(&LOCK_global_system_variables);
 
1310
  long tmp_buff_size= (long) key_cache->param_buff_size;
 
1311
  long tmp_block_size= (long) key_cache->param_block_size;
 
1312
  uint32_t division_limit= key_cache->param_division_limit;
 
1313
  uint32_t age_threshold=  key_cache->param_age_threshold;
 
1314
  pthread_mutex_unlock(&LOCK_global_system_variables);
 
1315
 
 
1316
  return(!resize_key_cache(key_cache, tmp_block_size,
 
1317
                           tmp_buff_size,
 
1318
                           division_limit, age_threshold));
 
1319
}
 
1320
 
1325
1321
 
1326
1322
 
1327
1323
bool sys_var_key_buffer_size::update(Session *session, set_var *var)
1328
1324
{
1329
1325
  uint64_t tmp= var->save_result.uint64_t_value;
1330
 
  LEX_STRING *base_name= &var->base;
1331
1326
  KEY_CACHE *key_cache;
1332
1327
  bool error= 0;
1333
1328
 
1334
 
  /* If no basename, assume it's for the key cache named 'default' */
1335
 
  if (!base_name->length)
1336
 
    base_name= &default_key_cache_base;
1337
 
 
1338
1329
  pthread_mutex_lock(&LOCK_global_system_variables);
1339
 
  key_cache= get_key_cache(base_name);
1340
 
 
1341
 
  if (!key_cache)
1342
 
  {
1343
 
    /* Key cache didn't exists */
1344
 
    if (!tmp)                                   // Tried to delete cache
1345
 
      goto end;                                 // Ok, nothing to do
1346
 
    if (!(key_cache= create_key_cache(base_name->str, base_name->length)))
1347
 
    {
1348
 
      error= 1;
1349
 
      goto end;
1350
 
    }
1351
 
  }
 
1330
  key_cache= dflt_key_cache;
1352
1331
 
1353
1332
  /*
1354
1333
    Abort if some other thread is changing the key cache
1358
1337
  if (key_cache->in_init)
1359
1338
    goto end;
1360
1339
 
1361
 
  if (!tmp)                                     // Zero size means delete
 
1340
  if (tmp == 0)                                 // Zero size means delete
1362
1341
  {
1363
 
    if (key_cache == dflt_key_cache)
1364
 
    {
1365
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1366
 
                          ER_WARN_CANT_DROP_DEFAULT_KEYCACHE,
1367
 
                          ER(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE));
1368
 
      goto end;                                 // Ignore default key cache
1369
 
    }
1370
 
 
1371
 
    if (key_cache->key_cache_inited)            // If initied
1372
 
    {
1373
 
      /*
1374
 
        Move tables using this key cache to the default key cache
1375
 
        and clear the old key cache.
1376
 
      */
1377
 
      NAMED_LIST *list;
1378
 
      key_cache= (KEY_CACHE *) find_named(&key_caches, base_name->str,
1379
 
                                              base_name->length, &list);
1380
 
      key_cache->in_init= 1;
1381
 
      pthread_mutex_unlock(&LOCK_global_system_variables);
1382
 
      error= reassign_keycache_tables(session, key_cache, dflt_key_cache);
1383
 
      pthread_mutex_lock(&LOCK_global_system_variables);
1384
 
      key_cache->in_init= 0;
1385
 
    }
1386
 
    /*
1387
 
      We don't delete the key cache as some running threads my still be
1388
 
      in the key cache code with a pointer to the deleted (empty) key cache
1389
 
    */
1390
 
    goto end;
 
1342
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
1343
                        ER_WARN_CANT_DROP_DEFAULT_KEYCACHE,
 
1344
                        ER(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE));
 
1345
    goto end;                                   // Ignore default key cache
1391
1346
  }
1392
1347
 
1393
1348
  key_cache->param_buff_size=
1397
1352
  key_cache->in_init= 1;
1398
1353
  pthread_mutex_unlock(&LOCK_global_system_variables);
1399
1354
 
1400
 
  if (!key_cache->key_cache_inited)
1401
 
    error= (bool) (ha_init_key_cache("", key_cache));
1402
 
  else
1403
 
    error= (bool)(ha_resize_key_cache(key_cache));
 
1355
  error= (bool)(resize_key_cache_with_lock(key_cache));
1404
1356
 
1405
1357
  pthread_mutex_lock(&LOCK_global_system_variables);
1406
1358
  key_cache->in_init= 0;
1420
1372
bool sys_var_key_cache_uint32_t::update(Session *session, set_var *var)
1421
1373
{
1422
1374
  uint64_t tmp= (uint64_t) var->value->val_int();
1423
 
  LEX_STRING *base_name= &var->base;
1424
1375
  bool error= 0;
1425
1376
 
1426
 
  if (!base_name->length)
1427
 
    base_name= &default_key_cache_base;
1428
 
 
1429
1377
  pthread_mutex_lock(&LOCK_global_system_variables);
1430
 
  KEY_CACHE *key_cache= get_key_cache(base_name);
1431
 
 
1432
 
  if (!key_cache && !(key_cache= create_key_cache(base_name->str,
1433
 
                                                  base_name->length)))
1434
 
  {
1435
 
    error= 1;
1436
 
    goto end;
1437
 
  }
1438
1378
 
1439
1379
  /*
1440
1380
    Abort if some other thread is changing the key cache
1441
1381
    TODO: This should be changed so that we wait until the previous
1442
1382
    assignment is done and then do the new assign
1443
1383
  */
1444
 
  if (key_cache->in_init)
 
1384
  if (dflt_key_cache->in_init)
1445
1385
    goto end;
1446
1386
 
1447
 
  *((uint32_t*) (((char*) key_cache) + offset))=
 
1387
  *((uint32_t*) (((char*) dflt_key_cache) + offset))=
1448
1388
    (uint32_t) fix_unsigned(session, tmp, option_limits);
1449
1389
 
1450
1390
  /*
1451
1391
    Don't create a new key cache if it didn't exist
1452
1392
    (key_caches are created only when the user sets block_size)
1453
1393
  */
1454
 
  key_cache->in_init= 1;
 
1394
  dflt_key_cache->in_init= 1;
1455
1395
 
1456
1396
  pthread_mutex_unlock(&LOCK_global_system_variables);
1457
1397
 
1458
 
  error= (bool) (ha_resize_key_cache(key_cache));
 
1398
  error= (bool) (resize_key_cache_with_lock(dflt_key_cache));
1459
1399
 
1460
1400
  pthread_mutex_lock(&LOCK_global_system_variables);
1461
 
  key_cache->in_init= 0;
 
1401
  dflt_key_cache->in_init= 0;
1462
1402
 
1463
1403
end:
1464
1404
  pthread_mutex_unlock(&LOCK_global_system_variables);
1929
1869
}
1930
1870
 
1931
1871
 
1932
 
NAMED_LIST::NAMED_LIST(I_List<NAMED_LIST> *links, const char *name_arg,
1933
 
                       uint32_t name_length_arg, unsigned char* data_arg)
1934
 
    :data(data_arg)
1935
 
{
1936
 
  name.assign(name_arg, name_length_arg);
1937
 
  links->push_back(this);
1938
 
}
1939
 
 
1940
 
 
1941
 
bool NAMED_LIST::cmp(const char *name_cmp, uint32_t length)
1942
 
{
1943
 
  return length == name.length() && !name.compare(name_cmp);
1944
 
}
1945
 
 
1946
 
 
1947
1872
/*
1948
1873
  Initialize the system variables
1949
1874
 
2320
2245
 
2321
2246
 
2322
2247
/****************************************************************************
2323
 
  Named list handling
2324
 
****************************************************************************/
2325
 
 
2326
 
unsigned char* find_named(I_List<NAMED_LIST> *list, const char *name, uint32_t length,
2327
 
                NAMED_LIST **found)
2328
 
{
2329
 
  I_List_iterator<NAMED_LIST> it(*list);
2330
 
  NAMED_LIST *element;
2331
 
  while ((element= it++))
2332
 
  {
2333
 
    if (element->cmp(name, length))
2334
 
    {
2335
 
      if (found)
2336
 
        *found= element;
2337
 
      return element->data;
2338
 
    }
2339
 
  }
2340
 
  return 0;
2341
 
}
2342
 
 
2343
 
 
2344
 
void delete_elements(I_List<NAMED_LIST> *list,
2345
 
                     void (*free_element)(const char *name, unsigned char*))
2346
 
{
2347
 
  NAMED_LIST *element;
2348
 
  while ((element= list->get()))
2349
 
  {
2350
 
    (*free_element)(element->name.c_str(), element->data);
2351
 
    delete element;
2352
 
  }
2353
 
  return;
2354
 
}
2355
 
 
2356
 
 
2357
 
/* Key cache functions */
2358
 
 
2359
 
static KEY_CACHE *create_key_cache(const char *name, uint32_t length)
2360
 
{
2361
 
  KEY_CACHE *key_cache;
2362
 
 
2363
 
  if ((key_cache= (KEY_CACHE*) malloc(sizeof(KEY_CACHE))))
2364
 
  {
2365
 
    memset(key_cache, 0, sizeof(KEY_CACHE));
2366
 
    if (!new NAMED_LIST(&key_caches, name, length, (unsigned char*) key_cache))
2367
 
    {
2368
 
      free((char*) key_cache);
2369
 
      key_cache= 0;
2370
 
    }
2371
 
    else
2372
 
    {
2373
 
      /*
2374
 
        Set default values for a key cache
2375
 
        The values in dflt_key_cache_var is set by my_getopt() at startup
2376
 
 
2377
 
        We don't set 'buff_size' as this is used to enable the key cache
2378
 
      */
2379
 
      key_cache->param_block_size=     dflt_key_cache_var.param_block_size;
2380
 
      key_cache->param_division_limit= dflt_key_cache_var.param_division_limit;
2381
 
      key_cache->param_age_threshold=  dflt_key_cache_var.param_age_threshold;
2382
 
    }
2383
 
  }
2384
 
  return(key_cache);
2385
 
}
2386
 
 
2387
 
 
2388
 
KEY_CACHE *get_or_create_key_cache(const char *name, uint32_t length)
2389
 
{
2390
 
  LEX_STRING key_cache_name;
2391
 
  KEY_CACHE *key_cache;
2392
 
 
2393
 
  key_cache_name.str= (char *) name;
2394
 
  key_cache_name.length= length;
2395
 
  pthread_mutex_lock(&LOCK_global_system_variables);
2396
 
  if (!(key_cache= get_key_cache(&key_cache_name)))
2397
 
    key_cache= create_key_cache(name, length);
2398
 
  pthread_mutex_unlock(&LOCK_global_system_variables);
2399
 
  return key_cache;
2400
 
}
2401
 
 
2402
 
 
2403
 
void free_key_cache(const char *, KEY_CACHE *key_cache)
2404
 
{
2405
 
  ha_end_key_cache(key_cache);
2406
 
  free((char*) key_cache);
2407
 
}
2408
 
 
2409
 
 
2410
 
bool process_key_caches(process_key_cache_t func)
2411
 
{
2412
 
  I_List_iterator<NAMED_LIST> it(key_caches);
2413
 
  NAMED_LIST *element;
2414
 
 
2415
 
  while ((element= it++))
2416
 
  {
2417
 
    KEY_CACHE *key_cache= (KEY_CACHE *) element->data;
2418
 
    func(element->name.c_str(), key_cache);
2419
 
  }
2420
 
  return 0;
2421
 
}
2422
 
 
2423
 
/****************************************************************************
2424
2248
  Used templates
2425
2249
****************************************************************************/
2426
2250