Как изменить TTL всех записей, установленных с ttl -1 в аэроспайке?
Я хочу изменить TTL всех записей, которые были случайно установлены с TTL "никогда не истекает" (-1 в клиенте). Как бы я это сделал?
1 ответ
Просто чтобы уточнить, установка TTL -1 в клиенте означает, что никогда не истекает (эквивалентно default-ttl
0 в файле aerospike.conf сервера), а установка TTL на 0 в клиенте означает наследование default-ttl для этого пространства имен.
С фильтрацией предикатов:
Если вы используете клиенты Java, C, C# и Go, самый простой способ идентифицировать записи со временем пустоты 0 - использовать фильтр предикатов. Вы бы применили простой UDF записи ко всем записям, сопоставленным фильтром предикатов.
ttl.lua
function set_ttl(rec, to_ttl)
record.set_ttl(rec, to_ttl)
-- for the TTL update to happen a bin must be 'dirty'
-- set the first bin back with the value of the first bin
-- no changes will occur other than the TTL change, but the
-- aerospike:update(rec) will actually execute
local bin_names = record.bin_names(rec)
local do_nothing = rec[bin_names[1]]
rec[bin_names[1]] = do_nothing
aerospike:update(rec)
end
Зарегистрируйте модуль Lua, используя 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.
Затем в приложении Java:
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();
Только с использованием UDF:
С другими клиентами, у которых еще нет фильтрации предикатов (Python, PHP и т. Д.), Вы могли бы делать все это с помощью UDF-записи, применяемой к сканированию. Логика фильтрации должна была бы жить внутри 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)
-- for the TTL update to happen a bin must be 'dirty'
-- set the first bin back with the value of the first bin
-- no changes will occur other than the TTL change, but the
-- aerospike:update(rec) will actually execute
local bin_names = record.bin_names(rec)
local do_nothing = rec[bin_names[1]]
rec[bin_names[1]] = do_nothing
aerospike:update(rec)
end
end
В 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