This chapter describes how to use the REST API to configure database replication. For details on database replication, see the Database Replication Guide. The procedures are:
Use GET /manage/v2/properties to return the properties of each cluster to participate in the database replication configuration and pass the properties to POST /manage/v2/clusters to couple the clusters.
The example Python script below gets the properties (in JSON format) from the master
and replica
clusters and passes the master
properties to the replica
cluster and the replica
properties to the master cluster
.
#! /usr/bin/env python # -*- coding: utf-8 -*- import json import requests from requests.auth import HTTPDigestAuth from pprint import pprint def main(): # Get the properties for each cluster to be coupled auth = HTTPDigestAuth('admin', 'admin') headers = {'accept': 'application/json'} masterURI = 'http://master.marklogic.com:8002/manage/v2/properties' replicaURI = 'http://replica.marklogic.com:8002/manage/v2/properties' g1 = requests.get(masterURI, auth=auth, headers=headers) master_info = g1.json() g2 = requests.get(replicaURI, auth=auth, headers=headers) replica_info = g2.json() # Couple the clusters masterURI = 'http://master.marklogic.com:8002/manage/v2/clusters' replicaURI = 'http://replica.marklogic.com:8002/manage/v2/clusters' headers = {'content-type': 'application/json'} p1 = requests.post(masterURI, auth=auth, data=json.dumps(replica_info), headers=headers) p2 = requests.post(replicaURI, auth=auth, data=json.dumps(master_info), headers=headers) # Return reponses from the REST calls print p1.text print p2.text main()
POST /manage/v2/clusters accepts all of the properties returned from GET /manage/v2/properties as its payload. Should you have a need for finer control over the cluster properties when coupling clusters, you can manually build the payload for POST /manage/v2/clusters.
The Python script below extracts the properties from the master
cluster and builds the payload needed by POST /manage/v2/clusters to couple the replica
cluster to the master
cluster. You would need to write similar code to couple the master
cluster to the replica
cluster.
#! /usr/bin/env python # -*- coding: utf-8 -*- import json import requests from requests.auth import HTTPDigestAuth from pprint import pprint def main(): # Get the properties for master cluster auth = HTTPDigestAuth('admin', 'admin') headers = {'accept': 'application/json'} masterURI = 'http://master.marklogic.com:8002/manage/v2/properties' g1 = requests.get(masterURI, auth=auth, headers=headers) master_info = g1.json() # Extract the properties needed byPOST:/manage/v2/clusters
fhostid = master_info['bootstrap-host'][0]['bootstrap-host-id'] fhostn = master_info['bootstrap-host'][0]['bootstrap-host-name'] fcid = master_info['cluster-id'] fcn = master_info['cluster-name'] fccert = master_info['xdqp-ssl-certificate'] # Build the payload forPOST:/manage/v2/clusters
payload = { "foreign-cluster-id": fcid, "foreign-cluster-name": fcn, "foreign-protocol": "http", "foreign-ssl-certificate": fccert, "xdqp-ssl-enabled": True, "xdqp-ssl-allow-sslv3": True, "xdqp-ssl-allow-tls": True, "xdqp-ssl-ciphers": "ALL:!LOW:@STRENGTH", "xdqp-timeout": 10, "host-timeout": 30, "foreign-bootstrap-host": [{ "foreign-host-id": fhostid, "foreign-host-name": fhostn, "foreign-connect-port": 7998 }]} # Couple the replica cluster to the master cluster. url = 'http://replica.marklogic.com:8002/manage/v2/clusters' headers = {'content-type': 'application/json'} r = requests.post(url, auth=auth, data=json.dumps(payload), headers=headers) # Return reponse from the REST call print r.text main()
Once the clusters are coupled you can use POST /manage/v2/databases/{id|name} to configure the master and replica databases.
The Python script below uses POST /manage/v2/databases/{id|name} to configure the Documents
database on the master
cluster as the master database and the Documents
database on the replica
cluster as the replica database.
#! /usr/bin/env python # -*- coding: utf-8 -*- import json import requests from requests.auth import HTTPDigestAuth from pprint import pprint def main(): # Define the operations to set the master and replica databases. set_master = { "operation": "set-foreign-master", "foreign-master": { "foreign-cluster-name": "master.marklogic.com-cluster", "foreign-database-name": "Documents", "connect-forests-by-name": True}} set_replica = { "operation": "add-foreign-replicas", "foreign-replica": [{ "foreign-cluster-name": "replica.marklogic.com-cluster", "foreign-database-name": "Documents", "connect-forests-by-name": True, "lag-limit": 23, "enabled": True}]} masterURI = 'http://master.marklogic.com:8002/manage/v2/databases/Documents' replicaURI = 'http://replica.marklogic.com:8002/manage/v2/databases/Documents' headers = {'content-type': 'application/json', 'accept': 'application/json'} # Set the master and replica databases. r1 = requests.post(replicaURI, auth=HTTPDigestAuth('admin', 'admin'), data=json.dumps(set_master), headers=headers) r2 = requests.post(masterURI, auth=HTTPDigestAuth('admin', 'admin'), data=json.dumps(set_replica), headers=headers) # Return reponses from the REST calls print r1.text print r2.text main()