TiDB Lightning Checkpoints

Importing a large database usually takes hours or days, and if such long running processes spuriously crashes, it can be very time-wasting to redo the previously completed tasks. To solve this, TiDB Lightning uses checkpoints to store the import progress, so that tidb-lightning continues importing from where it lefts off after restarting.

This document describes how to enable, configure, store, and control checkpoints.

Enable and configure checkpoints

  1. [checkpoint]
  2. # Whether to enable checkpoints.
  3. # While importing data, TiDB Lightning records which tables have been imported, so
  4. # even if TiDB Lightning or some other component crashes, you can start from a known
  5. # good state instead of restarting from scratch.
  6. enable = true
  7. # Where to store the checkpoints.
  8. # - file: store as a local file (requires v2.1.1 or later)
  9. # - mysql: store into a remote MySQL-compatible database
  10. driver = "file"
  11. # The schema name (database name) to store the checkpoints
  12. # Enabled only when `driver = "mysql"`.
  13. # schema = "tidb_lightning_checkpoint"
  14. # The data source name (DSN) indicating the location of the checkpoint storage.
  15. #
  16. # For the "file" driver, the DSN is a path. If the path is not specified, Lightning would
  17. # default to "/tmp/CHECKPOINT_SCHEMA.pb".
  18. #
  19. # For the "mysql" driver, the DSN is a URL in the form of "USER:PASS@tcp(HOST:PORT)/".
  20. # If the URL is not specified, the TiDB server from the [tidb] section is used to
  21. # store the checkpoints. You should specify a different MySQL-compatible
  22. # database server to reduce the load of the target TiDB cluster.
  23. #dsn = "/tmp/tidb_lightning_checkpoint.pb"
  24. # Whether to keep the checkpoints after all data are imported. If false, the
  25. # checkpoints are deleted. Keeping the checkpoints can aid debugging but
  26. # might leak metadata about the data source.
  27. # keep-after-success = false

Checkpoints storage

TiDB Lightning supports two kinds of checkpoint storage: a local file or a remote MySQL-compatible database.

  • With driver = "file", checkpoints are stored in a local file at the path given by the dsn setting. Checkpoints are updated rapidly, so we highly recommend placing the checkpoint file on a drive with very high write endurance, such as a RAM disk.

  • With driver = "mysql", checkpoints can be saved in any databases compatible with MySQL 5.7 or later, including MariaDB and TiDB. By default, the checkpoints are saved in the target database.

While using the target database as the checkpoints storage, Lightning is importing large amounts of data at the same time. This puts extra stress on the target database and sometimes leads to communication timeout. Therefore, it is strongly recommended to install a temporary MySQL server to store these checkpoints. This server can be installed on the same host as tidb-lightning and can be uninstalled after the importer progress is completed.

Checkpoints control

If tidb-lightning exits abnormally due to unrecoverable errors (for example, data corruption), it refuses to reuse the checkpoints until the errors are resolved. This is to prevent worsening the situation. The checkpoint errors can be resolved using the tidb-lightning-ctl program.

--checkpoint-error-destroy

  1. tidb-lightning-ctl --checkpoint-error-destroy='`schema`.`table`'

This option allows you to restart importing the table from scratch. The schema and table names must be quoted with backquotes and are case-sensitive.

  • If importing the table `schema`.`table` failed previously, this option executes the following operations:

    1. DROPs the table `schema`.`table` from the target database, which means removing all imported data.
    2. Resets the checkpoints record of this table to be “not yet started”.
  • If there is no errors involving the table `schema`.`table` , this operation does nothing.

It is the same as applying the above on every table. This is the most convenient, safe and conservative solution to fix the checkpoint error problem:

  1. tidb-lightning-ctl --checkpoint-error-destroy=all

--checkpoint-error-ignore

  1. tidb-lightning-ctl --checkpoint-error-ignore='`schema`.`table`'
  2. tidb-lightning-ctl --checkpoint-error-ignore=all

If importing the table `schema`.`table` failed previously, this clears the error status as if nothing ever happened. The all variant applies this operation to all tables.

Checkpoints - 图1

Note

Use this option only when you are sure that the error can indeed be ignored. If not, some imported data can be lost. The only safety net is the final “checksum” check, and thus you need to keep the “checksum” option always enabled when using --checkpoint-error-ignore.

--checkpoint-remove

  1. tidb-lightning-ctl --checkpoint-remove='`schema`.`table`'
  2. tidb-lightning-ctl --checkpoint-remove=all

This option simply removes all checkpoint information about one table or all tables, regardless of their status.

--checkpoint-dump

  1. tidb-lightning-ctl --checkpoint-dump=output/directory

This option dumps the content of the checkpoint into the given directory, which is mainly used for debugging by the technical staff. This option is only enabled when driver = "mysql".