Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UCP: make "AutoRandom" to be auto_increment_increment-aware. #16508

Closed
bb7133 opened this issue Apr 17, 2020 · 2 comments · Fixed by #17575
Closed

UCP: make "AutoRandom" to be auto_increment_increment-aware. #16508

bb7133 opened this issue Apr 17, 2020 · 2 comments · Fixed by #17575
Assignees

Comments

@bb7133
Copy link
Member

bb7133 commented Apr 17, 2020

Description

Feature "AutoRandom" is added to TiDB v3.1 and v4.0, as a replacement of "AutoIncrement". This feature aims to provide a better solution to automatically generate primary key ID and avoid hotspots during a heavy write scenario.

For now, as an alternative to generate "auto" keys, "AutoRandom" is not auto_increment_increment aware, which lead to the following unexpected results when using JDBC, for example:

# Using MySQL client.
tidb> create table t(a bigint key auto_random)
Query OK, 0 rows affected, 1 warning (0.02 sec)
tidb> set global auto_increment_increment=2;                                                           
Query OK, 0 rows affected (0.00 sec)

And then,

// Scala code
object Test {
  val tidbUrl = s"jdbc:mysql://127.0.0.1:4000/test"

  def main(args: Array[String]): Unit = {
    val conn = DriverManager.getConnection(tidbUrl, username, password)
    val stmt = conn.createStatement()
    val incremental = {
      val res = stmt.executeQuery("select @@auto_increment_increment")
      res.next()
      res.getInt(1)
    }
    println(s"Current auto_increment_increment: $incremental")
    stmt.executeUpdate("insert into t values (), (), (), ()", Statement.RETURN_GENERATED_KEYS)
    val res = stmt.getGeneratedKeys()
    new Iterator[String] {
      def hasNext = res.next()
      def next() = res.getString(1)
    }.toStream.foreach(println)
    stmt.close()
    conn.close()
  }
}

Run the codes above, you'll get something like:

Current auto_increment_increment: 2
1879048217
1879048219
1879048221
1879048223

However, the actual inserted keys are:

tidb> select * from t;
+------------+
| a          |
+------------+
| 1879048217 |
| 1879048218 |
| 1879048219 |
| 1879048220 |
+------------+
4 rows in set (0.00 sec)

Reason of the wrong answer is that JDBC tries to calculate the generated keys through LastInsertID() and auto_increment_increment.

You can refer to #14301 to get the way how it is supported by "AutoIncrement".

Score

  • 1680

Mentor(s)

Contact the mentors: #tidb-challenge-program channel in TiDB Community Slack Workspace

Recommended Skills

  • Golang only.

Learning Materials

@bb7133 bb7133 changed the title auto_id: make "AutoRandom" to be auto_increment_increment-aware. UCP: make "AutoRandom" to be auto_increment_increment-aware. Apr 17, 2020
@tangenta tangenta self-assigned this May 11, 2020
@niedhui
Copy link
Contributor

niedhui commented May 13, 2020

@bb7133 @tangenta Hi mentors, currently I had a pending pr waiting for review, may I do this without pick-up-challenge first?

@tangenta
Copy link
Contributor

@niedhui Sorry, I am working on this issue because the deadline is coming soon. We are appreciated that if you could pick another challenge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants