Tidb: CURRENT_TIMESTAMP consistencies between TiKV node?

Created on 25 Jun 2019  路  5Comments  路  Source: pingcap/tidb

General Question

I just wonder if I insert into test(timestamp) values (CURRENT_TIMESTAMP); then result of the timestamp is consistency between tikv node?

I see that tiDB use

PD works as the cluster manager of TiDB, which manages TiKV metadata, allocates timestamps, and makes decisions for data placement and load balancing.

But not really sure CURRENT_TIMESTAMP allocated by PD ?.

THANK YOU.

typquestion

Most helpful comment

Hi @VoxT ,

  • About the execution of CURRENT_TIMESTAMP:
    CURRENT_TIMESTAMP is not allocated by PD, as an expression, its value is simply evaluated in
    TiDB as a constant when building the SQL plan. When the plan is executed, TiKV only sees constants,
    so the consistency is guaranteed.

    You can check the following codes for the details:

    When building the plan, expressionRewriter tries to convert an ast.Expression to expression.Expression

https://github.com/pingcap/tidb/blob/cf5f42b8ece0fad2b3f93c6753747b392f33f4c8/planner/core/expression_rewriter.go#L1389

And a foldConstant function tries to evaluate expressions like CURRENT_TIMESTAMP:

https://github.com/pingcap/tidb/blob/cf5f42b8ece0fad2b3f93c6753747b392f33f4c8/expression/constant_fold.go#L81

  • About PD:
    The timestamps allocated from PD are only used for transactions of TiDB/TiKV.

Please let me know if you have any question.

All 5 comments

Hi @VoxT ,

  • About the execution of CURRENT_TIMESTAMP:
    CURRENT_TIMESTAMP is not allocated by PD, as an expression, its value is simply evaluated in
    TiDB as a constant when building the SQL plan. When the plan is executed, TiKV only sees constants,
    so the consistency is guaranteed.

    You can check the following codes for the details:

    When building the plan, expressionRewriter tries to convert an ast.Expression to expression.Expression

https://github.com/pingcap/tidb/blob/cf5f42b8ece0fad2b3f93c6753747b392f33f4c8/planner/core/expression_rewriter.go#L1389

And a foldConstant function tries to evaluate expressions like CURRENT_TIMESTAMP:

https://github.com/pingcap/tidb/blob/cf5f42b8ece0fad2b3f93c6753747b392f33f4c8/expression/constant_fold.go#L81

  • About PD:
    The timestamps allocated from PD are only used for transactions of TiDB/TiKV.

Please let me know if you have any question.

@bb7133 thank you for your response.

Address from your answer then CURRENT_TIMESTAMP is not allocated by TiDB component right?

My problem is I could run many tidb component but I need to get a CURRENT_TIMESTAMP consistency for the whole cluster.

Is there a way I can get a value like that in tidb cluster, like an api to get CURRENT_TIMESTAMP from PD leader?

Thank and hope there is a way

hi @VoxT

Address from your answer then CURRENT_TIMESTAMP is not allocated by TiDB component right?

It is not allocated by PD, but evaluated in TiDB in each connection.

My problem is I could run many tidb component but I need to get a CURRENT_TIMESTAMP consistency for the whole cluster.

When you talk about 'TiDB component', you mean there are multiple TiDB servers, right?

Are you connecting to all of them and insert CURRENT_TIMESTAMP in each connection? If so, there's no way to get the timestamp you want... You may need to get a consistent value by yourself for all your connections and insert the value, rather than using CURRENT_TIMESTAMP.

When you talk about 'TiDB component', you mean there are multiple TiDB servers, right?

Yeah.

Are you connecting to all of them and insert CURRENT_TIMESTAMP in each connection?

Right.

So I see there is no way. But hopeful that there is the way to get a timestamp from PD in the future.

Thank you for your answer and your contribution to an awesome TiDB.

Got answer, close here

Was this page helpful?
0 / 5 - 0 ratings