The Aerospike Knowledge Base has moved to https://support.aerospike.com. Content on https://discuss.aerospike.com is being migrated to either https://support.aerospike.com or https://docs.aerospike.com. Maintenance on articles stored in this repository ceased on December 31st 2022 and this article may be stale. If you have any questions, please do not hesitate to raise a case via https://support.aerospike.com.
FAQ - How to modify the ttl using an UDF
Context
Objects can have a ttl
(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
).
Refer to this FAQ ttl for details about the TTL
.
Method
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.
Here is an example:
1. ttl.lua
script to set record’s ttl
to 1 day (86400 seconds):
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. Run the following commands in aql to create two non-expirable records:
aql> set RECORD_TTL -1
RECORD_TTL = -1
aql> insert into test.demoset (PK, name) VALUES (101, 'name1');
OK, 1 record affected.
aql> insert into test.demoset (PK, name) VALUES (102, 'name2');
OK, 1 record affected.
3. Run the following commands to create two records that will expire in 1000 seconds:
aql> set RECORD_TTL 1000
RECORD_TTL = 1000
aql> insert into test.demoset (PK, name) VALUES (103, 'name3');
OK, 1 record affected.
aql> insert into test.demoset (PK, name) VALUES (104, 'name4');
OK, 1 record affected.
4. Confirm the data
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
5. Apply the lua script:
aql> register module 'ttl.lua'
OK, 1 module added.
aql> execute ttl.expireRecord() on test.demoset
OK, Scan job (12893429980729116258) created.
6. Confirm that only the non-expirable records are updated (with generation 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
ttl
as-1
(no expiration), it is actually set to0
on the server. You cannot set it to 0 to expire immediately but intepret as using the server default. -
The
ttl
is capped at 10 years as of version 3.8.3. 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.
Keywords
UDF TTL NON-EXPIRABLE OBJECTS
Timestamp
11/13/2018