Skip to content

Instantly share code, notes, and snippets.

@ochaton
Created June 26, 2023 16:25
Show Gist options
  • Save ochaton/c6c7189a292f1fb061d2242fa49c3748 to your computer and use it in GitHub Desktop.
Save ochaton/c6c7189a292f1fb061d2242fa49c3748 to your computer and use it in GitHub Desktop.
PoC: all spaces dumper to jsonl file
#!/usr/bin/env tarantool
-- Fill address here
local ADDR = "10.0.1.21:7402"
--- Main script goes here
local log = require 'log'
local json = require 'json'
local function version_at_least(v, maj, mid, min)
local v_maj, v_mid, v_min = v:match("^(%d+)%.(%d+)%.(%d+)")
v_maj = tonumber(v_maj)
v_mid = tonumber(v_mid)
v_min = tonumber(v_min)
if v_maj ~= maj then return v_maj > maj end
if v_mid ~= mid then return v_mid > mid end
if v_min ~= min then return v_min >= min end
return false
end
if not version_at_least(_TARANTOOL, 2, 3, 1) then
log.error("Sorry, you need at least Tarantool 2.3.1 to execute this script")
os.exit(1)
end
local function get_file()
local fd = assert(io.open('dump.jsonl', 'wx'))
return fd
end
local fd = get_file()
local function file_dump(old, new, sp, op)
if op ~= "INSERT" then return old end
fd:write(json.encode(new).."\n")
return old
end
box.ctl.on_schema_init(function()
log.info("schema_init")
box.space._index:before_replace(function(old, new, op)
if not new then return end
if not old then
if new[2] > 0 then
-- we do not create secondary indexes
return old
end
end
end)
box.space._space:before_replace(function(_, sp, _, op)
if not sp then return end
if sp[1] < 512 then return end
if op ~= "INSERT" then return end
local new_sp = sp:totable()
new_sp[6] = new_sp[6] or { temporary = true }
new_sp[6].temporary = true
return box.tuple.new(new_sp)
end)
box.space._space:on_replace(function(_, sp, _, op)
if not sp then return end
if sp[1] < 512 then return end
if op ~= "INSERT" then return end
assert(sp.name, "sp.name is not defined")
box.on_commit(function()
box.space[sp.name]:before_replace(file_dump)
end)
end)
end)
local netbox = require 'net.box'
local tt = netbox.connect(ADDR)
assert(tt:ping(), "tarantool is unavailble on "..ADDR)
local v = tt:eval("return box.info.version")
assert(v)
if not version_at_least(v, 2, 3, 1) then
log.error("Sorry tarantool at %s has version %s but should be at least 2.3.1", ADDR, v)
os.exit(1)
end
box.cfg{
replication_anon=true,
read_only=true,
wal_mode='none',
replication={ADDR},
}
box.cfg{replication={}}
fd:close()
local last_checkpoint = box.info.gc().checkpoints[1]
local snap_file = ("%020d.snap"):format(last_checkpoint.signature)
local fio = require 'fio'
log.warn("removing snap file %s", snap_file)
fio.unlink(snap_file)
os.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment