From 53cbe72cda0a0dfe7ec3d40c6b500b5d9e3d805b Mon Sep 17 00:00:00 2001 From: ase-101 Date: Sat, 9 Nov 2024 02:17:10 +0530 Subject: [PATCH] slot removal issue fixed Signed-off-by: ase-101 --- .../signup/services/CacheUtilService.java | 33 ++++++++++++------- .../resources/application-default.properties | 4 +-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/signup-service/src/main/java/io/mosip/signup/services/CacheUtilService.java b/signup-service/src/main/java/io/mosip/signup/services/CacheUtilService.java index d770f382..f7b91a1a 100644 --- a/signup-service/src/main/java/io/mosip/signup/services/CacheUtilService.java +++ b/signup-service/src/main/java/io/mosip/signup/services/CacheUtilService.java @@ -29,6 +29,8 @@ import org.springframework.data.redis.connection.ReturnType; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; + +import java.nio.charset.StandardCharsets; import java.util.Locale; import static io.mosip.signup.util.SignUpConstants.*; @@ -47,8 +49,17 @@ public class CacheUtilService { - private static final String CLEANUP_SCRIPT = "local hash_name = KEYS[1]\n" + - "local current_time = tonumber(ARGV[1])\n" + + private static final String CLEANUP_SCRIPT = "local function binary_to_long(binary_str)\n" + + " local result = 0\n" + + " for i = 1, #binary_str do\n" + + " result = result * 256 + binary_str:byte(i)\n" + + " end\n" + + " return result\n" + + "end" + + "\n" + + "local hash_name = KEYS[1]\n" + + "local time = redis.call(\"TIME\")\n" + + "local current_time = ( tonumber(time[1]) * 1000) + math.floor( tonumber(time[2]) / 1000)\n" + "local verified_slot_cache_keys = {}\n" + "local fields_to_delete = {}\n" + "local delcount=0\n" + @@ -59,8 +70,8 @@ public class CacheUtilService { " local hash_data = result[2]\n" + " for i = 1, #hash_data, 2 do\n" + " local field = hash_data[i]\n" + - " local value = tonumber(hash_data[i + 1])\n" + - " if value and value < current_time then\n" + + " local value = binary_to_long(hash_data[i + 1])\n" + + " if value < current_time then\n" + " local separator_index = string.find(field, \"###\")\n" + " if separator_index then \n" + " local key_part = string.sub(field, 1, separator_index - 1)\n" + @@ -270,16 +281,14 @@ public void clearExpiredSlots() { scriptHash = redisConnectionFactory.getConnection().scriptingCommands().scriptLoad(CLEANUP_SCRIPT.getBytes()); } LockAssert.assertLocked(); - Long currentTimeMillis = System.currentTimeMillis(); // Current time in millis log.info("Running scheduled cleanup task - task to clear expired slots with script hash: {} {}", scriptHash, SLOTS_CONNECTED); - int keysDeleted = redisConnectionFactory.getConnection().scriptingCommands().evalSha( + long keysDeleted = redisConnectionFactory.getConnection().scriptingCommands().evalSha( scriptHash, ReturnType.INTEGER, 1, // Number of keys - SLOTS_CONNECTED.getBytes(), // The Redis hash name (key) - Longs.toByteArray(currentTimeMillis) // Current time in milliseconds + SLOTS_CONNECTED.getBytes() // The Redis hash name (key) ); log.info("Running scheduled cleanup task - Keys Deleted count: {}", keysDeleted); } @@ -297,10 +306,10 @@ public Long getSetSlotCount(String field, long expireTimeInMillis, Integer maxCo addSlotScriptHash, ReturnType.INTEGER, 1, // Number of keys (SLOTS_CONNECTED is the key here) - SLOTS_CONNECTED.getBytes(), // key (first argument in Lua script) - field.getBytes(), // field (second argument in Lua script) - Longs.toByteArray(expireTimeInMillis), // value (third argument in Lua script) - String.valueOf(maxCount).getBytes() // maxCount (fourth argument, should be passed as a string) + SLOTS_CONNECTED.getBytes(StandardCharsets.UTF_8), // key (first argument in Lua script) + field.getBytes(StandardCharsets.UTF_8), // field (second argument in Lua script) + String.valueOf(expireTimeInMillis).getBytes(StandardCharsets.UTF_8), // value + String.valueOf(maxCount).getBytes(StandardCharsets.UTF_8) // maxCount ); } return -1L; diff --git a/signup-service/src/main/resources/application-default.properties b/signup-service/src/main/resources/application-default.properties index 3d1c75bd..c3a76b3d 100644 --- a/signup-service/src/main/resources/application-default.properties +++ b/signup-service/src/main/resources/application-default.properties @@ -67,7 +67,7 @@ mosip.signup.slot.expire-in-seconds=1200 ## cron trigger to run the expired slots cleanup script from the "slots_connected" cache. As the slot expire is set to 20 # minutes configuring the cleanup script to run every 20 minutes once -mosip.signup.slot.cleanup-cron=0 0/20 * * * * +mosip.signup.slot.cleanup-cron=0/20 * * * * * ## Time(in seconds) allowed to get slot after the authorization code exchange. mosip.signup.identity-verification.txn.timeout=180 @@ -110,7 +110,6 @@ mosip.esignet.cache.size={'challenge_generated': 200, \ 'identity_verifiers' : 20, \ 'idv_metadata' : 30,\ 'slot_allotted' : 200, \ - 'slots_connected': 200,\ 'verified_slot' : 200 } ## Note: keystore TTL should be more than the key_alias cache TTL. @@ -126,7 +125,6 @@ mosip.esignet.cache.expire-in-seconds={'challenge_generated': ${mosip.signup.una 'identity_verifiers' : 800, \ 'idv_metadata' : 500,\ 'slot_allotted' : ${mosip.signup.slot-allotted.timeout}, \ - 'slots_connected': 1000,\ 'verified_slot' : ${mosip.signup.verified-slot.timeout} } ## ------------------------------------- Auth adapter ------------------------------------------------------------------