Skip to main content

Backup and restore of the Lucene index

Info

The commands index-backup, index-restore and index-backups of mercury-cmd order the server to create a backup of the Lucene index of cases, to restore the index from a backup and to list the backups. This article describes the commands and their options, the configuration of the client, and what the server does during a backup and a restore, on one node and in a cluster.

The principle​

The commands index-backup, index-restore and index-backups are only a proxy: they do not open the index and do not start the application context. They call the REST service LuceneIndexBackupExtRest of the server (operations startBackup, startRestore, getBackupState, see the OpenAPI description), and the server makes the copy and restores the index, because only the server knows what its secretary and its queues are doing.

The operations concern one node, the node that answered the request. After the start the command waits for the end of the operation (--no-wait does not wait, --timeout <seconds> limits the waiting, 3600 s by default) and prints the result.

Commands and options​

CommandAliasWhat it does
index-backup [options]ibOrders the server to create a backup of the Lucene index of cases
index-restore [--backup-id <id>] [options]irOrders the server to restore the index from a backup (the latest by default)
index-backups [options]ibsLists the backups kept by the server (the latest first) and shows the state of a running operation
OptionCommandsMeaning
--user <name>allUser name of the system administrator (asked for when missing)
--passwd <password>allPassword of the user (asked for when missing, typed without an echo when a console is available)
--node <name>allName of the cluster node that should do the operation
--url <url>allAddress of the server application, for example http://localhost:8080/mercury-ws-app
--backup-id <id>index-restoreIdentifier of the backup to restore, taken from index-backups (default: the latest backup)
--no-waitindex-backup, index-restoreDo not wait for the end of the operation
--timeout <seconds>index-backup, index-restoreHow long to wait for the end of the operation (default 3600)
--lang <code>, -h, -VallLanguage of the messages, help, version

The exit codes are 0 (success), 1 (error: configuration, login, a response of the server, a timeout) and 2 (wrong arguments).

Configuration of the client​

In config/commandLine.properties of the tool:

PropertyMeaning
mercury.cmd.rest.urlAddress of the server application (the option --url has priority)
mercury.cmd.rest.nodeName of the cluster node that should do the operation (the option --node has priority)
mercury.cmd.rest.url.<node>Address of the given node (for example mercury.cmd.rest.url.node1)
mercury.cmd.rest.clientId, mercury.cmd.rest.clientSecretData of the OAuth client (client_id and client_secret) used to log in; the secret can be written in plain text or encoded with the command ep
mercury.cmd.rest.url=http://localhost:8080/mercury-ws-app
mercury.cmd.rest.clientId=GIUPortalClient
mercury.cmd.rest.clientSecret=<the secret of the client, plain or encoded with ep>

Logging in​

The service requires the role of the system administrator, mrc-sysadmin. The tool logs in with an OAuth 2.0 token (grant password) whose scope is always mrc-sysadmin (there is no option to change it), and the server checks the role once again.

  • client_id and client_secret come from the configuration (mercury.cmd.rest.clientId and mercury.cmd.rest.clientSecret).
  • The user and the password are given with the options --user and --passwd. When one of them is missing the command asks for it (the password is typed without an echo when a console is available).
$ mercury-cmd.sh ib --node node1
User name: admin
Password:
The server started the backup of the index.
Backup created: id 4, SCN 59620, location /data/lucene/backup/index_59620_20-11-2026 20_56_34

Choosing the node in a cluster​

The address of the server is taken from, in this order: the option --url, the property mercury.cmd.rest.url.<node> (when a node is named), the property mercury.cmd.rest.url. The node is named with the option --node <name> or the property mercury.cmd.rest.node. When a node is named, after logging in the command checks that exactly this node answered (an address is often the address of a load balancer); otherwise it ends with an error.

mercury-cmd.sh ibs --node node2                  # backups kept by node2
mercury-cmd.sh ir --node node2 --backup-id 3 # restore of node2 from the backup 3

What the server does​

Every backup is described in the description file of backups (JSON, lucene.backup.index.desc.file.* in lucene.properties) with an identifier, the SCN number (the last number of the archive buffer included in the backup), the date and the location. The copy goes to lucene.backup.base.indexDir, to a directory named index_<SCN>_<date>.

Use an absolute path

Set lucene.backup.base.indexDir and lucene.backup.index.desc.file.dir (the directory of the description file) to absolute paths with enough free space. A relative path is resolved against the working directory of the application server (for Tomcat apache-tomcat/bin), and a path with a leading slash is the root of the file system, which the user of the server cannot write. In the Docker image the paths are absolute (/opt/mercury-3.0/lucene/backup and /opt/mercury-3.0/lucene/backup/backupDesc), so the backups lie on the volume lucene.

One node (without a cluster)​

  • Backup. The secretary of the index stops (the backup holds the lock of the secretary), so the index does not change and the changes wait in the buffer in the database. The server copies the directory of the index, writes the backup in the description file and wakes the secretary, which processes the buffer itself.
  • Restore. The server copies the backup to the unused directory of the index (lucene.base.indexDir and lucene.base.indexDir2 take turns), clears this directory first, switches the index to it and indexes again the cases changed after the SCN of the backup (from the archive buffer). The secretary processes the remaining buffer. During the restore the node answers client requests with 503 (see below).

Cluster​

Only the MASTER processes the buffer of changes (a SLAVE does it itself only when no MASTER is online); it writes to its own index, sends the packages of the changes through JMS to the other nodes and then archives them. A SLAVE applies the packages from its own queue. During a backup or a restore the secretary of the node does not apply the packages, and the node does not use the packages that arrive in the meantime: the server receives and discards them (it reads the message and does nothing). This is safe, because the archive buffer is the source of truth and indexing a case again always writes its current state:

  • The SCN of a backup is the last number of the archive buffer at the start, lowered below the lowest number of a package still waiting in the queue, so a backup never claims changes that the index has not received yet.
  • After a backup the server discards the packages and indexes again the cases of the archive from the SCN of the backup, so the live index is aligned with the archive.
  • A restore works in several passes: it indexes again from the SCN of the backup to the end of the archive, discards the packages, checks how many entries of the archive are still missing and repeats. When at most 100 entries are missing (mercury.lucene.restore.onlineGap), the node returns online and makes one more pass for the rest. Under a continuous heavy write (about 66 cases per second in a test on a cluster of three nodes) the indexing of the archive does not catch up with the new changes, so the restore ends with the time limit (mercury.lucene.restore.maxCatchUpMillis, about 10 minutes) and the node returns online with a gap that the last pass closes.
  • Packages already loaded from the archive are ignored. Every entry of a package carries the SCN. The node keeps the number up to which its index is aligned with the archive in the data of the commit of the Lucene index (the key mercury.index.scn), raises it only after the alignment with the archive, and skips the entries of the packages with the SCN not greater than this number. The index writes a case with updateDocument, so writing the same case twice leaves one document.
  • A restore silences the node. For its whole duration the node does not send a heartbeat, so the other nodes treat it as OFFLINE and do not send it requests or elect it as the MASTER. It starts to send the heartbeat again when it is nearly aligned (or after mercury.lucene.restore.maxCatchUpMillis, 10 minutes by default, with a warning in the log).
  • A MASTER passes the baton first. Before a backup or a restore a MASTER that has another online SLAVE stops to send its heartbeat and waits (at most the time after which the other nodes consider it offline plus one interval of the heartbeat, 22.5 s by default) until the other node takes the role and this node becomes a SLAVE by itself. Then it does the operation. It stays a SLAVE afterwards. When no other node is online, the operation runs as on one node. The switch can be turned off with mercury.lucene.backup.handoffMaster=false.
  • A node without a MASTER tries to take the role. On every heartbeat (and when it marks the MASTER as offline) a SLAVE that does not see an online MASTER checks whether it should take the role (the first online SLAVE by the names does), unless it is silent, in maintenance or it has started a moment ago (mercury.node.lucene.masterReviewGraceMillis, by default two intervals of the heartbeat).

A node with the role SHARED (a shared index) does not make backups.

The restored index after a restart of the server​

After a restore the current directory of the index is the other one of the pair (lucene.base.indexDir or lucene.base.indexDir2). The server writes this choice to the file lucene-index-selected.properties in the parent directory of lucene.base.indexDir (in the Docker image the volume lucene, so the choice survives the replacement of the container). At the start the file takes precedence over lucene.base.indexDir.selected from lucene.properties; the log then says (in Polish) Używam katalogu indeksu ... zapisanego w ... po odtworzeniu z kopii. Deleting the file brings back the choice from lucene.properties.

Installations that restored the index before 3.0.4.33

Earlier versions kept the choice only in memory, so after a restart the server returned to the directory from lucene.properties, that is to the index from before the restore. If the restored data lies in the other directory, create the file before the update, for example lucene.base.indexDir.selected=lucene.base.indexDir2 in lucene-index-selected.properties next to the directories of the index. Otherwise the node starts on the old index.

Maintenance: 503 for clients​

While a node restores its index, its index is out of date, so the node answers the requests of the clients with 503 Service Unavailable and the headers Retry-After and X-Hgdb-Maintenance. Still available are: /rest/token, the service of the backup (/rest/service/LuceneIndexBackupExtRest) and the traffic between the nodes (/remoting/, /monitor/); more paths can be added with mercury.node.maintenance.allowPaths. The load balancer (mercury-lb) repeats the request on another node after a 503 and skips the node for 30 seconds; see the description in the repository of mercury-docker (mercury-lb/README.md).

Properties of the server​

PropertyDefaultMeaning
lucene.base.indexDir.selectedlucene.base.indexDirThe current directory of the index; after a restore the choice is kept in the file lucene-index-selected.properties (see above)
mercury.lucene.restore.onlineGap100How many entries of the archive may be missing when a restored node returns online
mercury.lucene.restore.maxCatchUpMillis600000The longest time of aligning a restored node before it returns online
mercury.lucene.backup.handoffMastertrueA MASTER passes the role before a backup or a restore
mercury.node.lucene.masterReviewGraceMillistwo intervals of the heartbeatThe time after the start in which a node does not take the role of the MASTER
mercury.node.maintenance.allowPathsemptyMore paths (comma separated, relative to the application) available in maintenance
mercury.node.maintenance.retryAfterSeconds30The value of the header Retry-After in the answer 503

See also: mercury-cmd (all commands, the configuration of the tool), Lucene index, Lucene index configuration parameters.