How does a PD server generate timestamps?

Does it use a thread to generate an increasing sequence of numbers like what is usually done in a snapshot isolation implementation? Or does it refer to the local clock of the PD server for that purpose?

The transaction id in TiDB, we call it “TSO”, which means “TimeStamp Oracle”. It is constructed by Physical time and Logical time.

// server/tso/tso.go
// atomicObject represents a tso
type atomicObject struct {
    physical time.Time
    logical  int64  // maxLogical = int64(1 << 18)
}

physical here represents the unix timestamp in milliseconds. and logical try to solve the conflict with the same milliseconds in physical time, to make the TSO to be global unique.

Basically, it don’t just use a thread to generate the TSO, you can refer to this code: pd/allocator_manager.go at 8227cdba21205fea57b0aca9644e97c2c53a94a8 · tikv/pd · GitHub

It generate TSO concurrently in PD leader and generally requests will be batched by the client, and use mutex lock to deal with the multi-thread data race.