Hi, i’m using go-client and found that in case of starting application before one of the aerospike nodes started, application wont connect to it never. For example, I have nodes in aerospike(as1,as2 and as3) running in replication of the factor 3. If I add a firewall rule to stop communication between my client and as3, but the communication between all the three servers are intact then my client wont start, I have tried using ClientPolicy.failIfNotConnected Field as false. I followed the instructions given in ClientPolicy.failIfNotConnected Field (aerospike.com). I am using golang client v6. Please let me know howI can proceed further.
FailIfNotConnected serves a different purpose. On a multi-node cluster, if the client fails to connect to some of the peers after the initial seed, this controls whether the client should fail or continue. Wording of godoc: Throw exception if host connection fails during addHost().
To keep trying to connect to Aerospike even if the initial seed has failed can be easily implemented with a loop and a sleep, below is a complete example that includes configurable retry attempts and sleep between retries:
package main
import (
"errors"
"log"
"time"
"github.com/aerospike/aerospike-client-go/v6"
)
func main() {
client, err := connect("10.88.0.2", 3000, -1, time.Second)
if err != nil {
log.Fatal(err)
}
client.Close()
}
// hostname - seed IP/DNS
// port - seed port
// maxRetries - maximum number of times to retry the connection, set to -1 to keep trying forever
// sleepBetweenRetries - how long to sleep between each connection retry
func connect(hostname string, port int, maxRetries int, sleepBetweenRetries time.Duration) (*aerospike.Client, error) {
// policy - setting connection timeout of each connection
cp := aerospike.NewClientPolicy()
cp.Timeout = 5 * time.Second
// if we manage to connect to seed, but fail to connect to other nodes, still continue
cp.FailIfNotConnected = false
// create new client loop
var client *aerospike.Client
var err error
log.Printf("Connecting to %s:%d", hostname, port)
retry := -1
for {
retry++
if retry > 0 {
log.Printf("Retrying connection")
}
client, err = aerospike.NewClientWithPolicy(cp, hostname, port)
if err == nil {
break
}
log.Printf("Connection failure: %s", err)
if maxRetries != -1 && retry >= maxRetries {
break
}
time.Sleep(sleepBetweenRetries)
}
if err != nil {
return nil, errors.New("all connection attempts failed, aborting")
}
log.Print("Connected")
// warm up 100 connections
client.WarmUp(100)
// done
return client, nil
}
For clarity, this is the basic example of a connect function loop which just retries every second until success.
func connect(hostname string, port int) (client *aerospike.Client, err error) {
for {
client, err = aerospike.NewClient(hostname, port)
if err == nil {
break
}
log.Print(err)
time.Sleep(time.Second)
}
return client, nil
}