Skip to content

Commit

Permalink
fix THD::system_time to follow, well, system time
Browse files Browse the repository at this point in the history
Because NOW() is set to system time, unless overriden.
And both should follow big manual system time changes,
while still coping with lowres system clocks.

Ignoring system time changes is both confusing and
breaks with restarts.
  • Loading branch information
vuvova committed Feb 25, 2018
1 parent 30981dc commit ac2d4d4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions sql/partition_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ void partition_info::vers_set_hist_part(THD *thd)

if (vers_info->interval.is_set())
{
if (vers_info->hist_part->range_value > thd->system_time)
if (vers_info->hist_part->range_value > thd->systime())
return;

partition_element *next= NULL;
Expand All @@ -911,7 +911,7 @@ void partition_info::vers_set_hist_part(THD *thd)
while ((next= it++) != vers_info->now_part)
{
vers_info->hist_part= next;
if (next->range_value > thd->system_time)
if (next->range_value > thd->systime())
return;
}
goto warn;
Expand Down
3 changes: 1 addition & 2 deletions sql/sql_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,7 @@ THD::THD(my_thread_id id, bool is_wsrep_applier, bool skip_global_sys_var_lock)
// Must be reset to handle error with THD's created for init of mysqld
lex->current_select= 0;
start_utime= utime_after_query= 0;
system_time= 0;
system_time_sec_part= 0;
system_time.start.val= system_time.sec= system_time.sec_part= 0;
utime_after_lock= 0L;
progress.arena= 0;
progress.report_to_client= 0;
Expand Down
45 changes: 25 additions & 20 deletions sql/sql_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -2418,8 +2418,6 @@ class THD :public Statement,
// track down slow pthread_create
ulonglong prior_thr_create_utime, thr_create_utime;
ulonglong utime_after_query;
my_time_t system_time;
ulong system_time_sec_part;

// Process indicator
struct {
Expand Down Expand Up @@ -3430,30 +3428,37 @@ class THD :public Statement,
{ query_start_sec_part_used=1; return start_time_sec_part; }
MYSQL_TIME query_start_TIME();

private:
bool system_time_ge(my_time_t secs, ulong usecs)
{
return (system_time == secs && system_time_sec_part >= usecs) ||
system_time > secs;
}
struct {
my_hrtime_t start;
my_time_t sec;
ulong sec_part;
} system_time;

ulong systime_sec_part() { return system_time.sec_part; }
my_time_t systime() { return system_time.sec; }

private:
void set_system_time()
{
my_hrtime_t hrtime= my_hrtime();
my_time_t secs= hrtime_to_my_time(hrtime);
ulong usecs= hrtime_sec_part(hrtime);
if (system_time_ge(secs, usecs))
my_time_t sec= hrtime_to_my_time(hrtime);
ulong sec_part= hrtime_sec_part(hrtime);
if (sec > system_time.sec ||
(sec == system_time.sec && sec_part > system_time.sec_part) ||
hrtime.val < system_time.start.val)
{
if (++system_time_sec_part == HRTIME_RESOLUTION)
{
++system_time;
system_time_sec_part= 0;
}
system_time.sec= sec;
system_time.sec_part= sec_part;
}
else
{
system_time= secs;
system_time_sec_part= usecs;
if (system_time.sec_part < TIME_MAX_SECOND_PART)
system_time.sec_part++;
else
{
system_time.sec++;
system_time.sec_part= 0;
}
}
}

Expand All @@ -3468,8 +3473,8 @@ class THD :public Statement,
}
else
{
start_time= system_time;
start_time_sec_part= system_time_sec_part;
start_time= system_time.sec;
start_time_sec_part= system_time.sec_part;
}
PSI_CALL_set_thread_start_time(start_time);
}
Expand Down
4 changes: 2 additions & 2 deletions sql/sql_partition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,7 @@ static bool check_vers_constants(THD *thd, partition_info *part_info)
my_tz_OFFSET0->TIME_to_gmt_sec(&ltime, &error);
if (error)
goto err;
if (vers_info->hist_part->range_value <= thd->system_time)
if (vers_info->hist_part->range_value <= thd->systime())
vers_info->hist_part= el;
}
return 0;
Expand Down Expand Up @@ -5314,7 +5314,7 @@ that are reorganised.
if (*fast_alter_table && tab_part_info->vers_info->interval.is_set())
{
partition_element *hist_part= tab_part_info->vers_info->hist_part;
if (hist_part->range_value <= thd->system_time)
if (hist_part->range_value <= thd->systime())
hist_part->part_state= PART_CHANGED;
}
}
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_yacc.yy
Original file line number Diff line number Diff line change
Expand Up @@ -5872,7 +5872,7 @@ opt_versioning_rotation:
opt_versioning_interval_start:
/* empty */
{
$$= thd->system_time;
$$= thd->systime();
}
| remember_tok_start STARTS_SYM ulong_num
{
Expand Down
12 changes: 6 additions & 6 deletions sql/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7764,8 +7764,8 @@ void TABLE::vers_update_fields()
{
if (!vers_write)
return;
if (vers_start_field()->store_timestamp(in_use->system_time,
in_use->system_time_sec_part))
if (vers_start_field()->store_timestamp(in_use->systime(),
in_use->systime_sec_part()))
DBUG_ASSERT(0);
}
else
Expand All @@ -7780,8 +7780,8 @@ void TABLE::vers_update_fields()

void TABLE::vers_update_end()
{
if (vers_end_field()->store_timestamp(in_use->system_time,
in_use->system_time_sec_part))
if (vers_end_field()->store_timestamp(in_use->systime(),
in_use->systime_sec_part()))
DBUG_ASSERT(0);
}

Expand Down Expand Up @@ -8622,9 +8622,9 @@ bool TR_table::update(ulonglong start_id, ulonglong end_id)
if (!table && open())
return true;

timeval start_time= {thd->system_time, long(thd->system_time_sec_part)};
timeval start_time= {thd->systime(), long(thd->systime_sec_part())};
thd->set_start_time();
timeval end_time= {thd->system_time, long(thd->system_time_sec_part)};
timeval end_time= {thd->systime(), long(thd->systime_sec_part())};
store(FLD_TRX_ID, start_id);
store(FLD_COMMIT_ID, end_id);
store(FLD_BEGIN_TS, start_time);
Expand Down

0 comments on commit ac2d4d4

Please sign in to comment.