Context
Objects can have attl (time to live) set which means that they will expire (deleted) when after the ttl is reached. Objects can also never expire if their ttl is set to -1 (will cause server to set the ttl to 0 -- setting the ttl to 0 from the client policy would cause the server to use the configured default-ttl).We will use the
record.set_ttl() and aerospike:update() methods to set a ttl. Reter to this page https://www.aerospike.com/docs/udf/api/record.html#record-set_ttl- for details. Method
1. Create a lua file similar to ttl.lua script to set record's ttl to 1 day (86400 seconds) for non-expirable records:
function expireRecord(rec)
local currTTL = record.ttl(rec)
if ( currTTL == 0 ) then
record.set_ttl(rec, 86400)
local result = aerospike:update(rec)
if ( result ~= nil and result ~= 0 ) then
warn("expireRecord:Failed to UPDATE the record: resultCode (%s)", tostring(result))
end
end
end
2. Verify current record TTL
aql> set RECORD_PRINT_METADATA true
RECORD_PRINT_METADATA = true
aql> select * from test.demoset
+---------+--------------------------------+-----------+-------+-------+
| name | {edigest} | {set} | {ttl} | {gen} |
+---------+--------------------------------+-----------+-------+-------+
| "name2" | "CaRk4OUs2Xm+7Wzbz93Tz7b0cdg=" | "demoset" | -1 | 1 |
| "name3" | "8lU4X7fI8SrbLDuoAeshW9CCphs=" | "demoset" | 986 | 1 |
| "name4" | "RxiiUM398Ycwg1amNhlh8qfSsgA=" | "demoset" | 991 | 1 |
| "name1" | "bcpatNmxoJ53HxWz3EkD/TSMCQ4=" | "demoset" | -1 | 1 |
+---------+--------------------------------+-----------+-------+-------+
4 rows in set (0.170 secs)
OK
3. Register and Apply the lua UDF script:
aql> register module 'ttl.lua' OK, 1 module added. aql> execute ttl.expireRecord() on test.demoset OK, Scan job (12893429980729116258) created.
4. Confirm the record TTLon non-expirable records has been updated. (gen number also incremented)
aql> select * from test.demoset
+---------+--------------------------------+-----------+-------+-------+
| name | {edigest} | {set} | {ttl} | {gen} |
+---------+--------------------------------+-----------+-------+-------+
| "name2" | "CaRk4OUs2Xm+7Wzbz93Tz7b0cdg=" | "demoset" | 86395 | 2 |
| "name3" | "8lU4X7fI8SrbLDuoAeshW9CCphs=" | "demoset" | 968 | 1 |
| "name4" | "RxiiUM398Ycwg1amNhlh8qfSsgA=" | "demoset" | 973 | 1 |
| "name1" | "bcpatNmxoJ53HxWz3EkD/TSMCQ4=" | "demoset" | 86395 | 2 |
+---------+--------------------------------+-----------+-------+-------+
4 rows in set (0.166 secs)
OK
Notes
-
While aql (or client) shows the
ttlas-1(no expiration), it is actually set to0on the server. You cannot set it to 0 to expire immediately but this is interpreted as using the server default. -
The
ttlis capped at 10 years. There following errors will occur when attempting to set a higher value:
Oct 12 2018 17:42:13 GMT: WARNING (udf): (udf_aerospike.c:524) invalid ttl 3153600000
Oct 12 2018 17:42:13 GMT: WARNING (udf): (udf_aerospike.c:859) udf_aerospike_rec_update: failure executing record updates (-1)
Oct 12 2018 17:42:13 GMT: WARNING (udf): (/opt/aerospike/usr/udf/lua/ttl.lua:55) expireNextCentury:Failed to UPDATE the record: resultCode (-1)
- Refer to the following articles for impact of cold restart on deleted records (which have not expired):
- Refer to the UDF Management article for details on managing UDF modules in Aerospike.