I need to integrate the aerospike in my sprint boot application for caching purpose but i am getting the client timeout error. Aerospike mvn dependency :
<dependency>
<groupId>com.aerospike</groupId>
<artifactId>aerospike-client</artifactId>
<version>3.0.22</version>
</dependency>
Java version : 17 Sprint boot version : 2.5.12
@Service
class AerospikeClientFactoryImpl {
private final ConcurrentMap<Triple<String, Integer, String>, STLAerospikeClient> map = new ConcurrentHashMap<>();
private final ConcurrentMap<Triple<String, String, String>, STLAerospikeClient> mapForClientCreationBasedOnCacheTypeStrategy = new ConcurrentHashMap<>();
public synchronized STLAerospikeClient getOrCreateCacheClient(String namespace, String host, int port,
int expireInSec) throws AerospikeException {
Triple<String, Integer, String> entryKey = Triple.of(host, expireInSec, namespace);
STLAerospikeClient STLAerospikeClient = map.get(Triple.of(host, expireInSec, namespace));
if (STLAerospikeClient == null) {
STLAerospikeClient = new STLAerospikeClient(namespace, host, port, expireInSec);
map.put(entryKey, STLAerospikeClient);
}
return STLAerospikeClient;
}
public synchronized STLAerospikeClient getOrCreateCacheClient(String namespace, WritePolicy writePolicy,
String host, int port, int expireInSec) throws AerospikeException {
Triple<String, Integer, String> entryKey = Triple.of(host, expireInSec, namespace);
STLAerospikeClient STLAerospikeClient = map.get(Triple.of(host, expireInSec, namespace));
if (STLAerospikeClient == null) {
STLAerospikeClient = new STLAerospikeClient(namespace, writePolicy, host, port, expireInSec);
map.put(entryKey, STLAerospikeClient);
}
return STLAerospikeClient;
}
}
@Service
@PropertySource({"classpath:application-${spring.profiles.active}.properties"})
public class LockerImpl implements Locker {
private static final Logger LOGGER = LoggerFactory.getLogger(LockerImpl.class);
private String LOCKER_NAMESPACE;
private static final int EXPIRE_IN_SEC = 30;
private static final String LOCK = "lock";
private final String host;
private final Integer port;
private STLAerospikeClient aerospikeClient;
@Autowired
private AerospikeClientFactory aerospikeClientFactory;
@Autowired
public LockerImpl(@Value("${aerospike.hosts}") String host,@Value("${aerospike.port}") Integer port, @Value("${aerospike.namespace}") String cacheNameSpace, AerospikeClientFactory clientFactory) throws AerospikeException {
this.host=host;
this.port=port;
this.LOCKER_NAMESPACE = cacheNameSpace;
WritePolicy writePolicy = new WritePolicy();
writePolicy.recordExistsAction = RecordExistsAction.CREATE_ONLY;
aerospikeClient = clientFactory.getOrCreateCacheClient(LOCKER_NAMESPACE, writePolicy, host, port,
EXPIRE_IN_SEC);
}
@Override
public boolean acquireLock(LockerType lockerType, String key) {
try {
Key aKey = new Key(aerospikeClient.getNamespace(), lockerType.name(), key);
String valueToWrite = ip + new Date().getTime();
Bin bin = new Bin(LOCK, valueToWrite);
try {
aerospikeClient.getClient().put(aerospikeClient.getWritePolicy(), aKey, bin);
} catch (AerospikeException ex) {
LOGGER.error("Exception in taking lock locker type " + lockerType.name() + " on key " + key, ex);
return false;
}
Record record = aerospikeClient.getClient().get(null, aKey);
String valueReceived = (String) record.getValue(LOCK);
return StringUtils.equalsIgnoreCase(valueToWrite, valueReceived);
} catch (Exception ex) {
LOGGER.error("Exception in taking lock locker type " + lockerType.name() + " on key " + key, ex);
return false;
}
}
@Override
public void releaseLockQuietly(LockerType lockerType, String key) {
try {
Key aKey = new Key(aerospikeClient.getNamespace(), lockerType.name(), key);
boolean success;
int retryLimit = 3;
int count = 1;
do {
success = aerospikeClient.getClient().delete(aerospikeClient.getWritePolicy(), aKey);
count++;
} while (!success && count <= retryLimit);
} catch (Exception ex) {
LOGGER.error("Exception in releaseLockQuietly", ex);
}
}
Aerospike have default timeout of 1 sec.
Aerospike server are working fine, i have can see some connections on server but getting this timeout error very frequently. There is not much traffic, TPS is less then 1. Unable to findout the root cause.