Skip to content

Commit ffb83ba

Browse files
committed
cleanup: move checksum code to handler class
make live checksum to be returned in handler::info(), and slow table-scan checksum to be calculated in handler::checksum(). part of MDEV-16249 CHECKSUM TABLE for a spider table is not parallel and saves all data in memory in the spider head by default
1 parent 651a43e commit ffb83ba

File tree

10 files changed

+120
-142
lines changed

10 files changed

+120
-142
lines changed

sql/ha_partition.cc

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8202,8 +8202,9 @@ int ha_partition::info(uint flag)
82028202
stats.deleted= 0;
82038203
stats.data_file_length= 0;
82048204
stats.index_file_length= 0;
8205-
stats.check_time= 0;
82068205
stats.delete_length= 0;
8206+
stats.check_time= 0;
8207+
stats.checksum= 0;
82078208
for (i= bitmap_get_first_set(&m_part_info->read_partitions);
82088209
i < m_tot_parts;
82098210
i= bitmap_get_next_set(&m_part_info->read_partitions, i))
@@ -8217,6 +8218,7 @@ int ha_partition::info(uint flag)
82178218
stats.delete_length+= file->stats.delete_length;
82188219
if (file->stats.check_time > stats.check_time)
82198220
stats.check_time= file->stats.check_time;
8221+
stats.checksum+= file->stats.checksum;
82208222
}
82218223
if (stats.records && stats.records < 2 &&
82228224
!(m_file[0]->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT))
@@ -8372,10 +8374,7 @@ void ha_partition::get_dynamic_partition_info(PARTITION_STATS *stat_info,
83728374
stat_info->create_time= file->stats.create_time;
83738375
stat_info->update_time= file->stats.update_time;
83748376
stat_info->check_time= file->stats.check_time;
8375-
stat_info->check_sum= 0;
8376-
if (file->ha_table_flags() & (HA_HAS_OLD_CHECKSUM | HA_HAS_NEW_CHECKSUM))
8377-
stat_info->check_sum= file->checksum();
8378-
return;
8377+
stat_info->check_sum= file->stats.checksum;
83798378
}
83808379

83818380

@@ -10563,27 +10562,6 @@ void ha_partition::init_table_handle_for_HANDLER()
1056310562
}
1056410563

1056510564

10566-
/**
10567-
Return the checksum of the table (all partitions)
10568-
*/
10569-
10570-
uint ha_partition::checksum() const
10571-
{
10572-
ha_checksum sum= 0;
10573-
10574-
DBUG_ENTER("ha_partition::checksum");
10575-
if ((table_flags() & (HA_HAS_OLD_CHECKSUM | HA_HAS_NEW_CHECKSUM)))
10576-
{
10577-
handler **file= m_file;
10578-
do
10579-
{
10580-
sum+= (*file)->checksum();
10581-
} while (*(++file));
10582-
}
10583-
DBUG_RETURN(sum);
10584-
}
10585-
10586-
1058710565
/****************************************************************************
1058810566
MODULE enable/disable indexes
1058910567
****************************************************************************/

sql/ha_partition.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,6 @@ class ha_partition :public handler
14541454
virtual int dump(THD* thd, int fd = -1);
14551455
virtual int net_read_dump(NET* net);
14561456
*/
1457-
virtual uint checksum() const;
14581457
/* Enabled keycache for performance reasons, WL#4571 */
14591458
virtual int assign_to_keycache(THD* thd, HA_CHECK_OPT *check_opt);
14601459
virtual int preload_keys(THD* thd, HA_CHECK_OPT* check_opt);

sql/handler.cc

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4876,10 +4876,7 @@ void handler::get_dynamic_partition_info(PARTITION_STATS *stat_info,
48764876
stat_info->create_time= stats.create_time;
48774877
stat_info->update_time= stats.update_time;
48784878
stat_info->check_time= stats.check_time;
4879-
stat_info->check_sum= 0;
4880-
if (table_flags() & (HA_HAS_OLD_CHECKSUM | HA_HAS_NEW_CHECKSUM))
4881-
stat_info->check_sum= checksum();
4882-
return;
4879+
stat_info->check_sum= stats.checksum;
48834880
}
48844881

48854882

@@ -5001,6 +4998,98 @@ void handler::update_global_index_stats()
50014998
}
50024999

50035000

5001+
static void flush_checksum(ha_checksum *row_crc, uchar **checksum_start,
5002+
size_t *checksum_length)
5003+
{
5004+
if (*checksum_start)
5005+
{
5006+
*row_crc= my_checksum(*row_crc, *checksum_start, *checksum_length);
5007+
*checksum_start= NULL;
5008+
*checksum_length= 0;
5009+
}
5010+
}
5011+
5012+
5013+
/* calculating table's checksum */
5014+
int handler::calculate_checksum()
5015+
{
5016+
int error;
5017+
THD *thd=ha_thd();
5018+
DBUG_ASSERT(table->s->last_null_bit_pos < 8);
5019+
uchar null_mask= table->s->last_null_bit_pos
5020+
? 256 - (1 << table->s->last_null_bit_pos) : 0;
5021+
5022+
table->use_all_columns();
5023+
stats.checksum= 0;
5024+
5025+
if ((error= ha_rnd_init(1)))
5026+
return error;
5027+
5028+
for (;;)
5029+
{
5030+
if (thd->killed)
5031+
return HA_ERR_ABORTED_BY_USER;
5032+
5033+
ha_checksum row_crc= 0;
5034+
error= table->file->ha_rnd_next(table->record[0]);
5035+
if (error)
5036+
break;
5037+
5038+
if (table->s->null_bytes)
5039+
{
5040+
/* fix undefined null bits */
5041+
table->record[0][table->s->null_bytes-1] |= null_mask;
5042+
if (!(table->s->db_create_options & HA_OPTION_PACK_RECORD))
5043+
table->record[0][0] |= 1;
5044+
5045+
row_crc= my_checksum(row_crc, table->record[0], table->s->null_bytes);
5046+
}
5047+
5048+
uchar *checksum_start= NULL;
5049+
size_t checksum_length= 0;
5050+
for (uint i= 0; i < table->s->fields; i++ )
5051+
{
5052+
Field *f= table->field[i];
5053+
5054+
if (! thd->variables.old_mode && f->is_real_null(0))
5055+
{
5056+
flush_checksum(&row_crc, &checksum_start, &checksum_length);
5057+
continue;
5058+
}
5059+
/*
5060+
BLOB and VARCHAR have pointers in their field, we must convert
5061+
to string; GEOMETRY is implemented on top of BLOB.
5062+
BIT may store its data among NULL bits, convert as well.
5063+
*/
5064+
switch (f->type()) {
5065+
case MYSQL_TYPE_BLOB:
5066+
case MYSQL_TYPE_VARCHAR:
5067+
case MYSQL_TYPE_GEOMETRY:
5068+
case MYSQL_TYPE_BIT:
5069+
{
5070+
flush_checksum(&row_crc, &checksum_start, &checksum_length);
5071+
String tmp;
5072+
f->val_str(&tmp);
5073+
row_crc= my_checksum(row_crc, (uchar*) tmp.ptr(), tmp.length());
5074+
break;
5075+
}
5076+
default:
5077+
if (!checksum_start)
5078+
checksum_start= f->ptr;
5079+
DBUG_ASSERT(checksum_start + checksum_length == f->ptr);
5080+
checksum_length+= f->pack_length();
5081+
break;
5082+
}
5083+
}
5084+
flush_checksum(&row_crc, &checksum_start, &checksum_length);
5085+
5086+
stats.checksum+= row_crc;
5087+
}
5088+
table->file->ha_rnd_end();
5089+
return error == HA_ERR_END_OF_FILE ? 0 : error;
5090+
}
5091+
5092+
50045093
/****************************************************************************
50055094
** Some general functions that isn't in the handler class
50065095
****************************************************************************/

sql/handler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2780,6 +2780,7 @@ class ha_statistics
27802780
time_t check_time;
27812781
time_t update_time;
27822782
uint block_size; /* index block size */
2783+
ha_checksum checksum;
27832784

27842785
/*
27852786
number of buffer bytes that native mrr implementation needs,
@@ -3804,7 +3805,7 @@ class handler :public Sql_alloc
38043805
virtual uint max_supported_key_part_length() const { return 255; }
38053806
virtual uint min_record_length(uint options) const { return 1; }
38063807

3807-
virtual uint checksum() const { return 0; }
3808+
virtual int calculate_checksum();
38083809
virtual bool is_crashed() const { return 0; }
38093810
virtual bool auto_repair(int error) const { return 0; }
38103811

sql/sql_show.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5636,7 +5636,7 @@ static int get_schema_tables_record(THD *thd, TABLE_LIST *tables,
56365636
}
56375637
if (file->ha_table_flags() & (HA_HAS_OLD_CHECKSUM | HA_HAS_NEW_CHECKSUM))
56385638
{
5639-
table->field[18]->store((longlong) file->checksum(), TRUE);
5639+
table->field[18]->store((longlong) file->stats.checksum, TRUE);
56405640
table->field[18]->set_notnull();
56415641
}
56425642
}

sql/sql_table.cc

Lines changed: 18 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -10630,18 +10630,6 @@ bool mysql_recreate_table(THD *thd, TABLE_LIST *table_list, bool table_copy)
1063010630
}
1063110631

1063210632

10633-
static void flush_checksum(ha_checksum *row_crc, uchar **checksum_start,
10634-
size_t *checksum_length)
10635-
{
10636-
if (*checksum_start)
10637-
{
10638-
*row_crc= my_checksum(*row_crc, *checksum_start, *checksum_length);
10639-
*checksum_start= NULL;
10640-
*checksum_length= 0;
10641-
}
10642-
}
10643-
10644-
1064510633
bool mysql_checksum_table(THD *thd, TABLE_LIST *tables,
1064610634
HA_CHECK_OPT *check_opt)
1064710635
{
@@ -10718,96 +10706,31 @@ bool mysql_checksum_table(THD *thd, TABLE_LIST *tables,
1071810706
if (!(check_opt->flags & T_EXTEND) &&
1071910707
(((t->file->ha_table_flags() & HA_HAS_OLD_CHECKSUM) && thd->variables.old_mode) ||
1072010708
((t->file->ha_table_flags() & HA_HAS_NEW_CHECKSUM) && !thd->variables.old_mode)))
10721-
protocol->store((ulonglong)t->file->checksum());
10709+
{
10710+
if (t->file->info(HA_STATUS_VARIABLE))
10711+
protocol->store_null();
10712+
else
10713+
protocol->store((longlong)t->file->stats.checksum);
10714+
}
1072210715
else if (check_opt->flags & T_QUICK)
1072310716
protocol->store_null();
1072410717
else
1072510718
{
10726-
/* calculating table's checksum */
10727-
ha_checksum crc= 0;
10728-
DBUG_ASSERT(t->s->last_null_bit_pos < 8);
10729-
uchar null_mask= (t->s->last_null_bit_pos ?
10730-
(256 - (1 << t->s->last_null_bit_pos)):
10731-
0);
10732-
10733-
t->use_all_columns();
10734-
10735-
if (t->file->ha_rnd_init(1))
10736-
protocol->store_null();
10737-
else
10719+
int error= t->file->calculate_checksum();
10720+
if (thd->killed)
1073810721
{
10739-
for (;;)
10740-
{
10741-
if (thd->killed)
10742-
{
10743-
/*
10744-
we've been killed; let handler clean up, and remove the
10745-
partial current row from the recordset (embedded lib)
10746-
*/
10747-
t->file->ha_rnd_end();
10748-
thd->protocol->remove_last_row();
10749-
goto err;
10750-
}
10751-
ha_checksum row_crc= 0;
10752-
int error= t->file->ha_rnd_next(t->record[0]);
10753-
if (unlikely(error))
10754-
{
10755-
break;
10756-
}
10757-
if (t->s->null_bytes)
10758-
{
10759-
/* fix undefined null bits */
10760-
t->record[0][t->s->null_bytes-1] |= null_mask;
10761-
if (!(t->s->db_create_options & HA_OPTION_PACK_RECORD))
10762-
t->record[0][0] |= 1;
10763-
10764-
row_crc= my_checksum(row_crc, t->record[0], t->s->null_bytes);
10765-
}
10766-
10767-
uchar *checksum_start= NULL;
10768-
size_t checksum_length= 0;
10769-
for (uint i= 0; i < t->s->fields; i++ )
10770-
{
10771-
Field *f= t->field[i];
10772-
10773-
if (! thd->variables.old_mode && f->is_real_null(0))
10774-
{
10775-
flush_checksum(&row_crc, &checksum_start, &checksum_length);
10776-
continue;
10777-
}
10778-
/*
10779-
BLOB and VARCHAR have pointers in their field, we must convert
10780-
to string; GEOMETRY is implemented on top of BLOB.
10781-
BIT may store its data among NULL bits, convert as well.
10782-
*/
10783-
switch (f->type()) {
10784-
case MYSQL_TYPE_BLOB:
10785-
case MYSQL_TYPE_VARCHAR:
10786-
case MYSQL_TYPE_GEOMETRY:
10787-
case MYSQL_TYPE_BIT:
10788-
{
10789-
flush_checksum(&row_crc, &checksum_start, &checksum_length);
10790-
String tmp;
10791-
f->val_str(&tmp);
10792-
row_crc= my_checksum(row_crc, (uchar*) tmp.ptr(),
10793-
tmp.length());
10794-
break;
10795-
}
10796-
default:
10797-
if (!checksum_start)
10798-
checksum_start= f->ptr;
10799-
DBUG_ASSERT(checksum_start + checksum_length == f->ptr);
10800-
checksum_length+= f->pack_length();
10801-
break;
10802-
}
10803-
}
10804-
flush_checksum(&row_crc, &checksum_start, &checksum_length);
10805-
10806-
crc+= row_crc;
10807-
}
10808-
protocol->store((ulonglong)crc);
10722+
/*
10723+
we've been killed; let handler clean up, and remove the
10724+
partial current row from the recordset (embedded lib)
10725+
*/
1080910726
t->file->ha_rnd_end();
10727+
thd->protocol->remove_last_row();
10728+
goto err;
1081010729
}
10730+
if (error)
10731+
protocol->store_null();
10732+
else
10733+
protocol->store((longlong)t->file->stats.checksum);
1081110734
}
1081210735
trans_rollback_stmt(thd);
1081310736
close_thread_tables(thd);

storage/maria/ha_maria.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,7 @@ int ha_maria::info(uint flag)
25282528
stats.delete_length= maria_info.delete_length;
25292529
stats.check_time= maria_info.check_time;
25302530
stats.mean_rec_length= maria_info.mean_reclength;
2531+
stats.checksum= file->state->checksum;
25312532
}
25322533
if (flag & HA_STATUS_CONST)
25332534
{
@@ -3277,12 +3278,6 @@ int ha_maria::ft_read(uchar * buf)
32773278
}
32783279

32793280

3280-
uint ha_maria::checksum() const
3281-
{
3282-
return (uint) file->state->checksum;
3283-
}
3284-
3285-
32863281
bool ha_maria::check_if_incompatible_data(HA_CREATE_INFO *create_info,
32873282
uint table_changes)
32883283
{

storage/maria/ha_maria.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ class ha_maria :public handler
6868
uint max_supported_key_part_length() const
6969
{ return max_supported_key_length(); }
7070
enum row_type get_row_type() const;
71-
uint checksum() const;
7271
void change_table_ptr(TABLE *table_arg, TABLE_SHARE *share);
7372
virtual double scan_time();
7473

storage/myisam/ha_myisam.cc

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,7 @@ int ha_myisam::info(uint flag)
20002000
stats.delete_length= misam_info.delete_length;
20012001
stats.check_time= (ulong) misam_info.check_time;
20022002
stats.mean_rec_length= misam_info.mean_reclength;
2003+
stats.checksum= file->state->checksum;
20032004
}
20042005
if (flag & HA_STATUS_CONST)
20052006
{
@@ -2304,12 +2305,6 @@ int ha_myisam::ft_read(uchar *buf)
23042305
return error;
23052306
}
23062307

2307-
uint ha_myisam::checksum() const
2308-
{
2309-
return (uint)file->state->checksum;
2310-
}
2311-
2312-
23132308
enum_alter_inplace_result
23142309
ha_myisam::check_if_supported_inplace_alter(TABLE *new_table,
23152310
Alter_inplace_info *alter_info)

storage/myisam/ha_myisam.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class ha_myisam: public handler
6666
uint max_supported_key_parts() const { return HA_MAX_KEY_SEG; }
6767
uint max_supported_key_length() const { return HA_MAX_KEY_LENGTH; }
6868
uint max_supported_key_part_length() const { return HA_MAX_KEY_LENGTH; }
69-
uint checksum() const;
7069
void change_table_ptr(TABLE *table_arg, TABLE_SHARE *share);
7170
int open(const char *name, int mode, uint test_if_locked);
7271
int close(void);

0 commit comments

Comments
 (0)