Vikas Jindal
Vikas Jindal

Reputation: 81

How to write aeropike service file so that it reconnects if connection failed?

My cacheService.js file is -

const Aerospike = require('aerospike');
const AerospikeConfig = require("../config/aerospike");
const hash = require('object-hash');
const NAMESPACE = AerospikeConfig.NAMESPACE;
const SET_NAME = AerospikeConfig.SET_NAME;
const CONNECTION_CONFIG = AerospikeConfig.CONNECTION_CONFIG;
const logger = require('./logger')(__filename);
const META = { ttl: AerospikeConfig.DEFAULT_TTL }

let client;

function connect() {
    Aerospike.connect(CONNECTION_CONFIG)
        .then(cacheClient => {
            client = cacheClient;
            logger.info(`[connect] Aerospike connected successfully`);
        }).catch(function(err) {
            logger.error(`[connect] Unable to create Aerospike Client, Error: ${err}`);
        });
}

// Write a record
function write(key, value) {
    return new Promise((resolve, reject) => {
        client.put(new Aerospike.Key(NAMESPACE, SET_NAME, hash(key)), value, META, (error) => {
            if (error) {
                logger.error(`[write] Error while writing to cache - ${error}`);
                return reject(error);
            }
            else {
                logger.info('[write] Data written successfully');
                return resolve(200);
            }
        });
    });
}

// Read a record
function read(key) {
    return new Promise((resolve, reject) => {
        client.get(new Aerospike.Key(NAMESPACE, SET_NAME, hash(key)), (error, record) => {
            if (error) {
                logger.info('[read] Record does not exist in cache');
                return reject(error);
            }
            else {
                logger.info('[read] Record found in cache');
                return resolve(record.bins);
            }
        });
    });
}

module.exports = {
    connect: connect,
    write: write,
    read: read
}

How should I rewrite it so that before every read and write it should check if client is connected and if not then connects and then do the further operation?

Currently when I am restarting aerospike, then the read and write operations fail and I have to restart the service again. I need to write it in a way that it should reconnect without failing to read and write.

Upvotes: 0

Views: 235

Answers (1)

Albert Autin
Albert Autin

Reputation: 43

If you're using a multi-node Aerospike cluster, like most folks, you shouldn't have to do this at all. The client will be aware of the cluster ips and once a node drops out, it will automatically tend the cluster to discover an ip has dropped - then stop communicating with it - or vice versa, if a node is added it will be discovered and connected to it. If you are running a multi-node cluster and running into this, look into the quiesce functionality, which allows a graceful node removal https://www.aerospike.com/docs/operations/manage/cluster_mng/quiescing_node/index.html

If you for some reason have to run a single Aerospike instance, I'm not sure you want to check for connectivity on every single operation. You may just want to add a catch to the exception which indicates you aren't connected to any live nodes, and in that catch re-instantiate the client and try to perform the operation again. The best solution though, for your client and data, is to run a multi-node cluster and utilize quiescense.

Upvotes: 2

Related Questions