Search code examples
aerospikettl

Aerospike TTL updates


I am trying to update .put() into existing records in Aerospike using the python client.

I was excited to discover that you can actually put into existing records and not update the TTL as shown here (meta params): https://www.aerospike.com/apidocs/python/client.html?highlight=ttl#aerospike.Client.put . I used to do this by reading the TTL and then re-setting it on the new put.

However, I discovered that when you use the aerospike.TTL_DONT_UPDATE option, the TTL resets to the cold-start-evict-ttl that is set on the namespace. How do I avoid that and just maintain the previous TTL unchanged?

Crossposted to Aerospike forums: https://discuss.aerospike.com/t/aerospike-ttls-on-put/4993


Solution

  • Not posting sample code is to a 'bug report' the equivalent of not washing your hands after using the toilet. It also would help if you simply provided the version of your server and the version of your Python client.

    ttl-test.py

    from __future__ import print_function
    import aerospike
    import sys
    from aerospike import exception as e
    from time import sleep
    
    config = { 'hosts': [ ('127.0.0.1', 3000) ] }
    
    try:
        client = aerospike.client(config).connect()
    except ex.ClientError as e:
        print("Error: {0} [{1}]".format(e.msg, e.code))
        sys.exit(1)
    
    key = ('test', 'demo', 'foo')
    
    try:
        # Write a record
        client.put(key, {
            'name': 'Hey Joe',
            'age': 66
            }, meta={'ttl': 4})
    except ex.RecordError as e:
        print("Error: {0} [{1}]".format(e.msg, e.code))
    client.put(key, {'z': 26}, meta={'ttl': aerospike.TTL_DONT_UPDATE})
    
    try:
        (key, meta) = client.exists(key)
        print(meta)
    except ex.RecordError as e:
        print("Error: {0} [{1}]".format(e.msg, e.code))
    
    sleep(5)
    
    try:
        (key, meta) = client.exists(key)
        print(meta)
    except ex.RecordError as e:
        print("Error: {0} [{1}]".format(e.msg, e.code))
    
    client.close()
    

    And now to test it:

    $ python ttl-test.py 
    {'gen': 2, 'ttl': 4}
    None
    

    Seems to work. Note that I'm using a server version >= 3.10.1