mariadb/storage/heap/hp_test1.c
Konstantin Osipov 33c57e2f9e WL#5419 "LOCK_open scalability: make tdc_refresh_version
an atomic counter"

Split the large LOCK_open section in open_table(). 
Do not call open_table_from_share() under LOCK_open.
Remove thd->version.

This fixes
Bug#50589 "Server hang on a query evaluated using a temporary 
table"
Bug#51557 "LOCK_open and kernel_mutex are not happy together"
Bug#49463 "LOCK_table and innodb are not nice when handler 
instances are created".

This patch has effect on storage engines that rely on
ha_open() PSEA method being called under LOCK_open.
In particular:

1) NDB is broken and left unfixed. NDB relies on LOCK_open
being kept as part of ha_open(), since it uses auto-discovery.
While previously the NDB open code was race-prone, now
it simply fails on asserts.

2) HEAP engine had a race in ha_heap::open() when
a share for the same table could be added twice
to the list of shares, or a dangling reference to a share
stored in HEAP handler. This patch aims to address this
problem by 'pinning' the newly created share in the 
internal HEAP engine share list until at least one
handler instance is created using that share.


include/heap.h:
  Add members to HP_CREATE_INFO.
  Declare heap_release_share().
sql/lock.cc:
  Remove thd->version, use thd->open_tables->s->version instead.
sql/repl_failsafe.cc:
  Remove thd->version.
sql/sql_base.cc:
  - close_thread_table(): move handler cleanup code outside the critical section protected by LOCK_open.
  - remove thd->version
  - split the large critical section in open_table() that
  opens a new table from share and is protected by LOCK_open
  into 2 critical sections, thus reducing the critical path.
  - make check_if_table_exists() acquire LOCK_open internally.
  - use thd->open_tables->s->version instead of thd->refresh_version to make sure that all tables in
  thd->open_tables are in the same refresh series.
sql/sql_base.h:
  Add declaration for check_if_table_exists().
sql/sql_class.cc:
  Remove init_open_tables_state(), it's now equal to
  reset_open_tables_state().
sql/sql_class.h:
  Remove thd->version, THD::init_open_tables_state().
sql/sql_plugin.cc:
  Use table->m_needs_reopen to mark the table as stale
  rather than manipulate with thd->version, which is no more.
sql/sql_udf.cc:
  Use table->m_needs_reopen to mark the table as stale
  rather than manipulate with thd->version, which is no more.
sql/table.h:
  Remove an unused variable.
sql/tztime.cc:
  Use table->m_needs_reopen to mark the table as stale
  rather than manipulate with thd->version, which is no more.
storage/heap/CMakeLists.txt:
  Add heap tests to cmake build files.
storage/heap/ha_heap.cc:
  Fix a race when ha_heap::ha_open() could insert two 
  HP_SHARE objects into the internal share list or store
  a dangling reference to a share in ha_heap instance,
  or wrongly set implicit_emptied.
storage/heap/hp_create.c:
  Optionally pin a newly created share in the list of shares
  by increasing its open_count. This is necessary to 
  make sure that a newly created share doesn't disappear while
  a HP_INFO object is being created to reference it.
storage/heap/hp_open.c:
  When adding a new HP_INFO object to the list of objects
  in the heap share, make sure the open_count is not increased
  twice.
storage/heap/hp_test1.c:
  Adjust the test to new function signatures.
storage/heap/hp_test2.c:
  Adjust the test to new function signatures.
2010-06-11 19:28:18 +04:00

200 lines
5.2 KiB
C

/* Copyright (C) 2000-2003, 2006 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* Test av heap-database */
/* Programmet skapar en heap-databas. Till denna skrivs ett antal poster.
Databasen st{ngs. D{refter |ppnas den p} nytt och en del av posterna
raderas.
*/
#include <my_global.h>
#include <my_sys.h>
#include <m_string.h>
#include "heap.h"
static int get_options(int argc, char *argv[]);
static int flag=0,verbose=0,remove_ant=0,flags[50];
int main(int argc, char **argv)
{
int i,j,error,deleted;
HP_INFO *file;
uchar record[128],key[32];
const char *filename;
HP_KEYDEF keyinfo[10];
HA_KEYSEG keyseg[4];
HP_CREATE_INFO hp_create_info;
HP_SHARE *tmp_share;
my_bool unused;
MY_INIT(argv[0]);
filename= "test1";
get_options(argc,argv);
bzero(&hp_create_info, sizeof(hp_create_info));
hp_create_info.max_table_size= 1024L*1024L;
hp_create_info.keys= 1;
hp_create_info.keydef= keyinfo;
hp_create_info.reclength= 30;
hp_create_info.max_records= (ulong) flag*100000L;
hp_create_info.min_records= 10UL;
keyinfo[0].keysegs=1;
keyinfo[0].seg=keyseg;
keyinfo[0].algorithm= HA_KEY_ALG_HASH;
keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
keyinfo[0].seg[0].start=1;
keyinfo[0].seg[0].length=6;
keyinfo[0].seg[0].charset= &my_charset_latin1;
keyinfo[0].seg[0].null_bit= 0;
keyinfo[0].flag = HA_NOSAME;
deleted=0;
bzero((uchar*) flags,sizeof(flags));
printf("- Creating heap-file\n");
if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
!(file= heap_open(filename, 2)))
goto err;
printf("- Writing records:s\n");
strmov((char*) record," ..... key ");
for (i=49 ; i>=1 ; i-=2 )
{
j=i%25 +1;
sprintf((char*) key,"%6d",j);
bmove(record+1,key,6);
error=heap_write(file,record);
if (heap_check_heap(file,0))
{
puts("Heap keys crashed");
goto err;
}
flags[j]=1;
if (verbose || error) printf("J= %2d heap_write: %d my_errno: %d\n",
j,error,my_errno);
}
if (heap_close(file))
goto err;
printf("- Reopening file\n");
if (!(file=heap_open(filename, 2)))
goto err;
printf("- Removing records\n");
for (i=1 ; i<=10 ; i++)
{
if (i == remove_ant) { (void) heap_close(file); return (0) ; }
sprintf((char*) key,"%6d",(j=(int) ((rand() & 32767)/32767.*25)));
if ((error = heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT)))
{
if (verbose || (flags[j] == 1 ||
(error && my_errno != HA_ERR_KEY_NOT_FOUND)))
printf("key: %s rkey: %3d my_errno: %3d\n",(char*) key,error,my_errno);
}
else
{
error=heap_delete(file,record);
if (error || verbose)
printf("key: %s delete: %d my_errno: %d\n",(char*) key,error,my_errno);
flags[j]=0;
if (! error)
deleted++;
}
if (heap_check_heap(file,0))
{
puts("Heap keys crashed");
goto err;
}
}
printf("- Reading records with key\n");
for (i=1 ; i<=25 ; i++)
{
sprintf((char*) key,"%6d",i);
bmove(record+1,key,6);
my_errno=0;
error=heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT);
if (verbose ||
(error == 0 && flags[i] != 1) ||
(error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
{
printf("key: %s rkey: %3d my_errno: %3d record: %s\n",
(char*) key,error,my_errno,record+1);
}
}
#ifdef OLD_HEAP_VERSION
{
int found;
printf("- Reading records with position\n");
for (i=1,found=0 ; i <= 30 ; i++)
{
my_errno=0;
if ((error=heap_rrnd(file,record,i == 1 ? 0L : (ulong) -1)) ==
HA_ERR_END_OF_FILE)
{
if (found != 25-deleted)
printf("Found only %d of %d records\n",found,25-deleted);
break;
}
if (!error)
found++;
if (verbose || (error != 0 && error != HA_ERR_RECORD_DELETED))
{
printf("pos: %2d ni_rrnd: %3d my_errno: %3d record: %s\n",
i-1,error,my_errno,(char*) record+1);
}
}
}
#endif
if (heap_close(file) || hp_panic(HA_PANIC_CLOSE))
goto err;
my_end(MY_GIVE_INFO);
return(0);
err:
printf("got error: %d when using heap-database\n",my_errno);
return(1);
} /* main */
/* Read options */
static int get_options(int argc, char **argv)
{
char *pos;
while (--argc >0 && *(pos = *(++argv)) == '-' ) {
switch(*++pos) {
case 'B': /* Big file */
flag=1;
break;
case 'v': /* verbose */
verbose=1;
break;
case 'm':
remove_ant=atoi(++pos);
break;
case 'V':
printf("hp_test1 Ver 3.0 \n");
exit(0);
case '#':
DBUG_PUSH (++pos);
break;
}
}
return 0;
} /* get options */