Search code examples
luauser-defined-functionspredicateaerospikettl

How to modify the TTL of all records set with a ttl of -1 in aerospike?


I want to modify the TTL of all the records that were accidentally set with a 'never expire' TTL (-1 in the client). How would I do that?


Solution

  • Just to clarify, setting a TTL of -1 in the client means never expire (equivalent to a default-ttl of 0 in the server's aerospike.conf file), while setting a TTL of 0 in the client means inherit the default-ttl for this namespace.

    With Predicate Filtering:

    If you're using the Java, C, C# and Go clients the easiest way to identify the records with a void time of 0 would be to use a predicate filter. You would apply a simple record UDF to all the records matched by the predicate filter.

    ttl.lua

    function set_ttl(rec, to_ttl)
      record.set_ttl(rec, to_ttl)
      aerospike:update(rec)
    end
    

    Register the Lua module using AQL:

    $ aql
    Aerospike Query Client
    Version 3.12.0
    C Client Version 4.1.4
    Copyright 2012-2017 Aerospike. All rights reserved.
    aql> register module './ttl.lua'
    OK, 1 module added.
    

    Then in the Java app:

    Statement stmt = new Statement();
    stmt.setNamespace(params.namespace);
    stmt.setSetName(params.set);
    stmt.setPredExp(
      PredExp.recVoidTime(),
      PredExp.integerValue(0),
      PredExp.integerEqual()
      );
    
    ExecuteTask task = client.execute(params.writePolicy, stmt, "ttl", "set_ttl", Value.IntegerValue(604800));
    task.waitTillComplete();
    

    Only using UDFs:

    With other clients that don't yet have predicate filtering (Python, PHP, etc), you would do it all through a record UDF that's applied to a scan. The filtering logic would have to live inside the UDF.

    ttl.lua

    function modify_zero_ttl(rec, to_ttl)
      local rec_ttl = record.ttl(rec)
      if rec_ttl == 0 then
        record.set_ttl(rec, to_ttl)
        aerospike:update(rec)
      end
    end
    

    In AQL:

    $ aql
    Aerospike Query Client
    Version 3.12.0
    C Client Version 4.1.4
    Copyright 2012-2017 Aerospike. All rights reserved.
    aql> register module './ttl.lua'
    OK, 1 module added.
    
    aql> execute ttl.modify_zero_ttl(604800) on test.foo