Components

Replication Logger

The replication logger will write all data-modification operations into the write-ahead log.This log may then be read by clients to replay any data modification on a different server.

Checking the state

To query the current state of the logger, use the state command:

  1. require("@arangodb/replication").logger.state();

The result might look like this:

  1. {
  2. "state" : {
  3. "running" : true,
  4. "lastLogTick" : "133322013",
  5. "totalEvents" : 16,
  6. "time" : "2014-07-06T12:58:11Z"
  7. },
  8. "server" : {
  9. "version" : "2.2.0-devel",
  10. "serverId" : "40897075811372"
  11. },
  12. "clients" : {
  13. }
  14. }

The running attribute will always be true. In earlier versions of ArangoDB the replication was optional and this could have been false.

The totalEvents attribute indicates how many log events have been logged since the start of the ArangoDB server. Finally, the lastLogTick value indicates the id of the last operation that was written to the server’s write-ahead log. It can be used to determine whether new operations were logged, and is also used by the replication applier for incremental fetching of data.

Note: The replication logger state can also be queried via theHTTP API.

To query which data ranges are still available for replication clients to fetch,the logger provides the firstTick and tickRanges functions:

  1. require("@arangodb/replication").logger.firstTick();

This will return the minimum tick value that the server can provide to replicationclients via its replication APIs. The tickRanges function returns the minimum andmaximum tick values per logfile:

  1. require("@arangodb/replication").logger.tickRanges();

Replication Applier

The purpose of the replication applier is to read data from a master database’s event log, and apply them locally. The applier will check the master database for new operations periodically. It will perform an incremental synchronization, i.e. only asking the master for operationsthat occurred after the last synchronization.

The replication applier does not get notified by the master database when there are “new” operations available, but instead uses the pull principle. It might thus take some time (the so-called replication lag) before an operation from the master database gets shipped to and applied in a slave database.

The replication applier of a database is run in a separate thread. It may encounter problems when an operation from the master cannot be applied safely, or when the connection to the masterdatabase goes down (network outage, master database is down or unavailable etc.). In this case, the database’s replication applier thread might terminate itself. It is then up to the administrator to fix the problem and restart the database’s replication applier.

If the replication applier cannot connect to the master database, or the communication fails atsome point during the synchronization, the replication applier will try to reconnect tothe master database. It will give up reconnecting only after a configurable amount of connection attempts.

The replication applier state is queryable at any time by using the state command of theapplier. This will return the state of the applier of the current database:

  1. require("@arangodb/replication").applier.state();

The result might look like this:

  1. {
  2. "state" : {
  3. "running" : true,
  4. "lastAppliedContinuousTick" : "152786205",
  5. "lastProcessedContinuousTick" : "152786205",
  6. "lastAvailableContinuousTick" : "152786205",
  7. "progress" : {
  8. "time" : "2014-07-06T13:04:57Z",
  9. "message" : "fetching master log from offset 152786205",
  10. "failedConnects" : 0
  11. },
  12. "totalRequests" : 38,
  13. "totalFailedConnects" : 0,
  14. "totalEvents" : 1,
  15. "lastError" : {
  16. "errorNum" : 0
  17. },
  18. "time" : "2014-07-06T13:04:57Z"
  19. },
  20. "server" : {
  21. "version" : "2.2.0-devel",
  22. "serverId" : "210189384542896"
  23. },
  24. "endpoint" : "tcp://master.example.org:8529",
  25. "database" : "_system"
  26. }

The running attribute indicates whether the replication applier of the current databaseis currently running and polling the server at endpoint for new events.

The progress.failedConnects attribute shows how many failed connection attempts the replicationapplier currently has encountered in a row. In contrast, the totalFailedConnects attributeindicates how many failed connection attempts the applier has made in total. ThetotalRequests attribute shows how many requests the applier has sent to the master databasein total. The totalEvents attribute shows how many log events the applier has read from themaster.

The progress.message sub-attribute provides a brief hint of what the applier currently does (if it is running). The lastError attribute also has an optional errorMessage sub-attribute, showing the latest error message. The errorNum sub-attribute of the lastError attribute can be used by clients to programmatically check for errors. It should be 0 if there is no error, and it should be non-zero if the applier terminated itself due to a problem.

Here is an example of the state after the replication applier terminated itself due to (repeated) connection problems:

  1. {
  2. "state" : {
  3. "running" : false,
  4. "progress" : {
  5. "time" : "2014-07-06T13:14:37Z",
  6. "message" : "applier stopped",
  7. "failedConnects" : 6
  8. },
  9. "totalRequests" : 79,
  10. "totalFailedConnects" : 11,
  11. "totalEvents" : 0,
  12. "lastError" : {
  13. "time" : "2014-07-06T13:09:41Z",
  14. "errorMessage" : "could not connect to master at tcp://master.example.org:8529: Could not connect to 'tcp:/...",
  15. "errorNum" : 1400
  16. },
  17. ...
  18. }
  19. }

Note: the state of a database’s replication applier is queryable via the HTTP API, too. Please refer to HTTP Interface for Replication for more details.

All-in-one setup

To copy the initial data from the slave to the master and start thecontinuous replication, there is an all-in-one command setupReplication:

  1. require("@arangodb/replication").setupReplication(configuration);

The following example demonstrates how to use the command for setting up replicationfor the _system database. Note that it should be run on the slave and not the master:

  1. db._useDatabase("_system");
  2. require("@arangodb/replication").setupReplication({
  3. endpoint: "tcp://master.domain.org:8529",
  4. username: "myuser",
  5. password: "mypasswd",
  6. verbose: false,
  7. includeSystem: false,
  8. incremental: true,
  9. autoResync: true
  10. });

The command will return when the initial synchronization is finished and the continuous replicationis started, or in case the initial synchronization has failed.

If the initial synchronization is successful, the command will store the given configuration onthe slave. It also configures the continuous replication to start automatically if the slave is restarted, i.e. autoStart is set to true.

If the command is run while the slave’s replication applier is already running, it will firststop the running applier, drop its configuration and do a resynchronization of data with themaster. It will then use the provided configration, overwriting any previously existing replicationconfiguration on the slave.

Starting and Stopping

To manually start and stop the applier in the current database, the start and stop commands can be used like this:

  1. require("@arangodb/replication").applier.start(<tick>);
  2. require("@arangodb/replication").applier.stop();

Note: Starting a replication applier without setting up an initial configuration will fail. The replication applier will look for its configuration in a file named REPLICATION-APPLIER-CONFIG in the current database’s directory. If the file is not present, ArangoDB will use some default configuration, but it cannot guess the endpoint (the address of the master database) the applier should connect to. Thus starting the applier without configuration will fail.

Note that at the first time you start the applier, you should pass the value returned in thelastLogTick attribute of the initial sync operation.

Note: Starting a database’s replication applier via the start command will not necessarily start the applier on the next and following ArangoDB server restarts. Additionally, stopping adatabase’s replication applier manually will not necessarily prevent the applier from being started again on the next server start. All of this is configurable separately (hang on reading).

Note: when stopping and restarting the replication applier of database, it will resume where it last stopped. This is sensible because replication log events should be applied incrementally. If the replication applier of a database has never been started before, it needs some tick value from the master’s log from which to start fetching events.

There is one caveat to consider when stopping a replication on the slave: if there are stillongoing replicated transactions that are neither committed or aborted, stopping the replicationapplier will cause these operations to be lost for the slave. If these transactions commit on themaster later and the replication is resumed, the slave will not be able to commit these transactions,too. Thus stopping the replication applier on the slave manually should only be done if thereis certainty that there are no ongoing transactions on the master.

Configuration

To configure the replication applier of a specific database, use the properties command. Using it without any arguments will return the applier’s current configuration:

  1. require("@arangodb/replication").applier.properties();

The result might look like this:

  1. {
  2. "requestTimeout" : 600,
  3. "connectTimeout" : 10,
  4. "ignoreErrors" : 0,
  5. "maxConnectRetries" : 10,
  6. "chunkSize" : 0,
  7. "autoStart" : false,
  8. "adaptivePolling" : true,
  9. "includeSystem" : true,
  10. "requireFromPresent" : false,
  11. "autoResync" : false,
  12. "autoResyncRetries" : 2,
  13. "verbose" : false
  14. }

Note: There is no endpoint attribute configured yet. The endpoint attribute is requiredfor the replication applier to be startable. You may also want to configure a username and passwordfor the connection via the username and password attributes.

  1. require("@arangodb/replication").applier.properties({
  2. endpoint: "tcp://master.domain.org:8529",
  3. username: "root",
  4. password: "secret",
  5. verbose: false
  6. });

This will re-configure the replication applier for the current database. The configuration will be used from the next start of the replication applier. The replication applier cannot be re-configured while it is running. It must be stopped first to be re-configured.

To make the replication applier of the current database start automatically when the ArangoDB server starts, use the autoStart attribute.

Setting the adaptivePolling attribute to true will make the replication applier poll the master database for changes with a variable frequency. The replication applier will then lower the frequency when the master is idle, and increase it when the master can provide new events).Otherwise the replication applier will poll the master database for changes with a constant frequency.

The idleMinWaitTime attribute controls the minimum wait time (in seconds) that the replication applier will intentionally idle before fetching more log data from the master in case the master has already sent all its log data. This wait time can be used to control the frequency with which the replication applier sends HTTP log fetch requests to the master in case there is no write activity on the master.

The idleMaxWaitTime attribute controls the maximum wait time (in seconds) that the replication applier will intentionally idle before fetching more log data from the master in case the master has already sent all its log data and there have been previous log fetch attempts that resulted in no more log data. This wait time can be used to control the maximum frequency with which the replication applier sends HTTP log fetch requests to the master in case there is no write activity on the master for longer periods. Note that this configuration value will only be used if the option adaptivePolling is set to true.

To set a timeout for connection and following request attempts, use the connectTimeout and requestTimeout values. The maxConnectRetries attribute configures after how many failed connection attempts in a row the replication applier will give up and turn itself off. You may want to set this to a high value so that temporary network outages do not lead to the replication applier stopping itself.The connectRetryWaitTime attribute configures how long the replication applier will waitbefore retrying the connection to the master in case of connection problems.

The chunkSize attribute can be used to control the approximate maximum size of a master’sresponse (in bytes). Setting it to a low value may make the master respond faster (less data isassembled before the master sends the response), but may require more request-response roundtrips.Set it to 0 to use ArangoDB’s built-in default value.

The includeSystem attribute controls whether changes to system collections (such as graphs_ orusers) should be applied. If set to _true, changes in these collections will be replicated, otherwise, they will not be replicated. It is often not necessary to replicate data from systemcollections, especially because it may lead to confusion on the slave because the slave needs to have its own system collections in order to start and keep operational.

The requireFromPresent attribute controls whether the applier will start synchronizing in caseit detects that the master cannot provide data for the initial tick value provided by the slave. This may be the case if the master does not have a big enough backlog of historic WAL logfiles,and when the replication is re-started after a longer pause. When requireFromPresent is set to true, then the replication applier will check at start whether the start tick from which it starts or resumes replication is still present on the master. If not, then there would be data loss. If requireFromPresent is true, the replication applier will abort with an appropriate error message. If set to false, then the replication applier will still start, and ignore the data loss.

The autoResync option can be used in conjunction with the requireFromPresent option as follows:when both requireFromPresent and autoResync are set to true and the master cannot provide the log data the slave requests, the replication applier will stop as usual. But due to the factthat autoResync is set to true, the slave will automatically trigger a full resync of all data with the master. After that, the replication applier will go into continuous replication mode again.Additionally, setting autoResync to true will trigger a full re-synchronization of data whenthe continuous replication is started and detects that there is no start tick value.

Automatic re-synchronization may transfer a lot of data from the master to the slave and can be expensive. It is therefore turned off by default. When turned off, the slave will never perform an automatic re-synchronization with the master.

The autoResyncRetries option can be used to control the number of resynchronization retries that will be performed in a row when automatic resynchronization is enabled and kicks in. Setting this to 0 will effectively disable autoResync. Setting it to some other value will limit the number of retries that are performed. This helps preventing endless retries in case resynchronizations always fail.

The verbose attribute controls the verbosity of the replication logger. Setting it to true willmake the replication applier write a line to the log for every operation it performs. This shouldonly be used for diagnosing replication problems.

The following example will set most of the discussed properties for the current database’s applier:

  1. require("@arangodb/replication").applier.properties({
  2. endpoint: "tcp://master.domain.org:8529",
  3. username: "root",
  4. password: "secret",
  5. adaptivePolling: true,
  6. connectTimeout: 15,
  7. maxConnectRetries: 100,
  8. chunkSize: 262144,
  9. autoStart: true,
  10. includeSystem: true,
  11. autoResync: true,
  12. autoResyncRetries: 2,
  13. });

After the applier is now fully configured, it could theoretically be started. However, wemay first need an initial synchronization of all collections and their data from the master beforewe start the replication applier.

The only safe method for doing a full synchronization (or re-synchronization) is thus to

  • stop the replication applier on the slave (if currently running)
  • perform an initial full sync with the master database
  • note the master database’s lastLogTick value and
  • start the continuous replication applier on the slave using this tick value.The initial synchronization for the current database is executed with the sync command:
  1. require("@arangodb/replication").sync({
  2. endpoint: "tcp://master.domain.org:8529",
  3. username: "root",
  4. password: "secret",
  5. includeSystem: true
  6. });

The includeSystem option controls whether data from system collections (such as graphs_ andusers_) shall be synchronized.

The initial synchronization can optionally be configured to include or exclude specific collections using the restrictType and restrictCollection parameters.

The following command only synchronizes collection foo and bar:

  1. require("@arangodb/replication").sync({
  2. endpoint: "tcp://master.domain.org:8529",
  3. username: "root",
  4. password: "secret",
  5. restrictType: "include",
  6. restrictCollections: [ "foo", "bar" ]
  7. });

Using a restrictType of exclude, all collections but the specified will be synchronized.

Warning: sync will do a full synchronization of the collections in the current database withcollections present in the master database.Any local instances of the collections and all their data are removed! Only execute thiscommand if you are sure you want to remove the local data!

As sync does a full synchronization, it might take a while to execute.When sync completes successfully, it returns an array of collections it has synchronized in itscollections attribute. It will also return the master database’s last log tick value at the time the sync was started on the master. The tick value is contained in the lastLogTick_attribute of the _sync command:

  1. {
  2. "lastLogTick" : "231848833079705",
  3. "collections" : [ ... ]
  4. }

Now you can start the continuous synchronization for the current database on the slavewith the command

  1. require("@arangodb/replication").applier.start("231848833079705");

Note: The tick values should be treated as strings. Using numeric data types for tickvalues is unsafe because they might exceed the 32 bit value and the IEEE754 double accuracyranges.