How to Get TiDB Cluster Initialization Time
1. Preface
It has been a long time since I wrote an article. Today, with a reminder from a colleague, I handled a problem about getting the TiDB cluster creation time. I think it is worth recording to avoid forgetting it, and sharing it with everyone.
2. Exploration
First, even after searching through TiDB official documentation, I could not find a method to query the TiDB cluster initialization time. All monitoring information (Dashboard, Grafana) can only query each component's startup time, and querying a table's create time is also not accurate.
Second, I knew that when a cluster is initialized, PD initializes and persists a ClusterID. Since TiDB official docs do not provide a query interface, maybe this is the only information that can distinguish clusters. At the beginning, I kept trying to directly decode TSO with pd-ctl using tiup ctl:v5.2.3 pd tso 1571036791, but it never worked.
Finally, with a reminder from a colleague, I checked the PD source code where Cluster_ID is initialized, and solved the problem successfully. See section 3. Solution for details.
3. Solution
After checking PD code, in this initOrGetClusterID function, Cluster ID is generated by taking the current Unix timestamp, left-shifting it by 32 bits, and then adding a random number. So direct translation/decoding cannot get the correct time.

I made some small changes and extracted time in reverse based on the code logic. The result basically meets the requirement and can ensure year/month/day-level accuracy. You can also directly click code link and modify the Cluster ID value to get the result.

Actually, I also tried right-shifting 32 bits first and then decoding with pd-ctl, but it still did not work. I guess this is because TiDB TSO is composed of "physical time + logical time", while Cluster ID is composed of "physical time + random number". They are fundamentally different things.
4. Code Logic
package main
import ("fmt"
"time")
func main() {
//Get from pd.log (cat {{/path/to}}/pd.log|grep "init cluster id")
// [2019/10/14 10:35:38.880 +00:00] [INFO] [server.go:212] ["init cluster id"] [cluster-id=6747551640615446306]
history_ts := uint64(6747551640615446306)
sub_history_ts := history_ts >> 32
ret := time.Unix(int64(sub_history_ts),0)
fmt.Println(ret)
fmt.Println("And the direct >>32 cluster-id is : ", sub_history_ts ,",you can try it by pd-ctl,but not correct!!!")
//Although there is random number, but the year/month/day is still accurate
// --> 2019/10/14 vs 2019-10-14
}Hope this helps everyone! At the same time, I also shared this article on AskTUG - How to get TiDB cluster creation time. Welcome to discuss.