litebackup 1.3.0-x86_64-darwin
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +109 -0
- data/lib/litebackup/bin/litebackup +4 -0
- data/lib/litebackup/binary.rb +108 -0
- data/lib/litebackup/client.rb +520 -0
- data/lib/litebackup/credential.rb +65 -0
- data/lib/litebackup/errors.rb +132 -0
- data/lib/litebackup/failover.rb +416 -0
- data/lib/litebackup/foreground.rb +185 -0
- data/lib/litebackup/generated/error_codes.rb +38 -0
- data/lib/litebackup/generated/models.rb +724 -0
- data/lib/litebackup/integrations/rails.rb +142 -0
- data/lib/litebackup/integrations/sqlite3.rb +55 -0
- data/lib/litebackup/integrations/sqlite_file.rb +81 -0
- data/lib/litebackup/launch.rb +146 -0
- data/lib/litebackup/paths.rb +86 -0
- data/lib/litebackup/pool.rb +790 -0
- data/lib/litebackup/release.rb +98 -0
- data/lib/litebackup/transport.rb +352 -0
- data/lib/litebackup/umask.rb +53 -0
- data/lib/litebackup/version.rb +8 -0
- data/lib/litebackup/wire.rb +436 -0
- data/lib/litebackup/wire_model.rb +55 -0
- data/lib/litebackup.rb +61 -0
- metadata +66 -0
|
@@ -0,0 +1,790 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Litebackup
|
|
4
|
+
# Opens and closes the handle a Pool yields to its calls. An adapter is any
|
|
5
|
+
# object with these two methods; including this module documents it.
|
|
6
|
+
#
|
|
7
|
+
# open must open an existing file and never create one: a missing file is
|
|
8
|
+
# an offloaded copy the pool brings back, never an empty new database;
|
|
9
|
+
# raise Error with code not_found for it. close must roll back a
|
|
10
|
+
# transaction a call left unfinished, never commit it, and raise when a
|
|
11
|
+
# connection may still be open: the pool releases a leased database only
|
|
12
|
+
# after close succeeds. Litebackup::Integrations::SQLite3.adapter is one.
|
|
13
|
+
module Adapter
|
|
14
|
+
def open(path)
|
|
15
|
+
raise NotImplementedError, "#{self.class} must open #{path}"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def close(handle)
|
|
19
|
+
raise NotImplementedError, "#{self.class} must close #{handle.inspect}"
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Seconds that limit closing and releasing a leased database together
|
|
24
|
+
# unless cleanup_timeout says otherwise.
|
|
25
|
+
DEFAULT_CLEANUP_TIMEOUT = 600.0
|
|
26
|
+
|
|
27
|
+
# The warnings the pool and the integrations report: to logger when one is
|
|
28
|
+
# set, such as Rails.logger, and to standard error otherwise.
|
|
29
|
+
class << self
|
|
30
|
+
attr_accessor :logger
|
|
31
|
+
|
|
32
|
+
def report_warning(message)
|
|
33
|
+
if logger.nil?
|
|
34
|
+
warn("litebackup: #{message}")
|
|
35
|
+
else
|
|
36
|
+
logger.warn("litebackup: #{message}")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Keeps databases open for calls; safe to share between threads. Create it
|
|
42
|
+
# with databases.pool:
|
|
43
|
+
#
|
|
44
|
+
# litebackup.databases.pool(Litebackup::Integrations::SQLite3.adapter) do |pool|
|
|
45
|
+
# pool.lease("agents/42.sqlite", path: "data/agents/42.sqlite") do |db|
|
|
46
|
+
# db.execute("INSERT INTO steps (body) VALUES (?)", ["tests passed"])
|
|
47
|
+
# end
|
|
48
|
+
# end
|
|
49
|
+
#
|
|
50
|
+
# use is for a database a backup rule on this machine covers: it stays here,
|
|
51
|
+
# open between calls. lease is for a database that moves between machines:
|
|
52
|
+
# the first lease acquires the write lock, and the pool keeps it, with the
|
|
53
|
+
# open handle, until idle_release runs out (a minute by default), release,
|
|
54
|
+
# or close. The app coordinates handovers: another machine's lease with
|
|
55
|
+
# lock_timeout succeeds once this one released the database. A lease on a
|
|
56
|
+
# database whose lock was lost meanwhile fails with lock_lost before its
|
|
57
|
+
# block, and the next lease acquires again. A close that fails never
|
|
58
|
+
# releases: a connection may still be open, and another machine must not
|
|
59
|
+
# write while this one still can.
|
|
60
|
+
#
|
|
61
|
+
# Close the pool when done: it waits for active blocks, then releases every
|
|
62
|
+
# leased database and closes every open one. Never close a pool from inside
|
|
63
|
+
# one of its blocks. A pool made before a fork is refused in the child.
|
|
64
|
+
class Pool
|
|
65
|
+
DEFAULT_IDLE_RELEASE = 60.0
|
|
66
|
+
# Seconds a database whose backup rule offloads (maximum_local_databases)
|
|
67
|
+
# stays open unused before the pool closes it, so the rule offloads it,
|
|
68
|
+
# unless idle_close says otherwise. It is well inside the client's
|
|
69
|
+
# 10-minute idle window.
|
|
70
|
+
DEFAULT_OFFLOAD_IDLE_CLOSE = 120.0
|
|
71
|
+
# How often a lease with cloud durability asks whether the bucket has its version yet.
|
|
72
|
+
CLOUD_DURABILITY_POLL_SECONDS = 1.0
|
|
73
|
+
# A release timeout of zero means the daemon's default, so a spent
|
|
74
|
+
# cleanup timeout sends the smallest one instead.
|
|
75
|
+
MINIMUM_RELEASE_SECONDS = 0.001
|
|
76
|
+
IDLE_RELEASE_STEP = "release"
|
|
77
|
+
IDLE_CLOSE_STEP = "close"
|
|
78
|
+
|
|
79
|
+
class << self
|
|
80
|
+
# How often a pool looks for idle databases, and how long a database
|
|
81
|
+
# whose rule offloads stays open unused; tests shorten them.
|
|
82
|
+
attr_accessor :check_seconds, :offload_idle_close_seconds
|
|
83
|
+
end
|
|
84
|
+
self.check_seconds = 15.0
|
|
85
|
+
self.offload_idle_close_seconds = DEFAULT_OFFLOAD_IDLE_CLOSE
|
|
86
|
+
|
|
87
|
+
# What a pool was created with, checked once. idle_release nil never
|
|
88
|
+
# releases an unused lease.
|
|
89
|
+
Settings = Data.define(:idle_close, :idle_release, :cleanup_seconds, :machine_label, :on_release_error) do
|
|
90
|
+
def self.build(adapter, idle_close:, idle_release:, cleanup_timeout:, machine_label:, on_release_error:)
|
|
91
|
+
raise Litebackup.invalid_argument("a pool needs an adapter") if adapter.nil?
|
|
92
|
+
|
|
93
|
+
cleanup_seconds = if cleanup_timeout.nil?
|
|
94
|
+
DEFAULT_CLEANUP_TIMEOUT
|
|
95
|
+
else
|
|
96
|
+
Wire.seconds(cleanup_timeout, "the cleanup timeout")
|
|
97
|
+
end
|
|
98
|
+
raise Litebackup.invalid_argument("the cleanup timeout must be positive") if cleanup_seconds.nil?
|
|
99
|
+
|
|
100
|
+
new(
|
|
101
|
+
idle_close: Wire.seconds(idle_close, "the idle close"),
|
|
102
|
+
idle_release: idle_release.nil? ? DEFAULT_IDLE_RELEASE : Wire.seconds(idle_release, "the idle release"),
|
|
103
|
+
cleanup_seconds: cleanup_seconds,
|
|
104
|
+
machine_label: machine_label,
|
|
105
|
+
on_release_error: on_release_error
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# One lease call's settings; the release that ends the lease uses the last call's.
|
|
111
|
+
LeaseSettings = Data.define(:path, :require_existing, :durability, :offload, :lock_timeout, :acquire_timeout,
|
|
112
|
+
:maintenance) do
|
|
113
|
+
def self.build(path:, require_existing:, durability:, offload:, lock_timeout:, acquire_timeout:, maintenance:)
|
|
114
|
+
unless durability.nil? || Durability::ALL.include?(durability)
|
|
115
|
+
raise Litebackup.invalid_argument("a lease's durability must be server or cloud")
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
new(path: path, require_existing: require_existing, durability: durability, offload: offload,
|
|
119
|
+
lock_timeout: Wire.seconds(lock_timeout, "the lock timeout"),
|
|
120
|
+
acquire_timeout: Wire.seconds(acquire_timeout, "the acquire timeout"),
|
|
121
|
+
maintenance: maintenance)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def release_durability
|
|
125
|
+
durability.nil? ? Durability::SERVER : durability
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# One database of a pool. turn serializes opening, bringing back,
|
|
130
|
+
# acquiring, lease calls, and releasing; use calls share the open handle
|
|
131
|
+
# without it. users and last_used are guarded by the pool's mutex.
|
|
132
|
+
class Entry
|
|
133
|
+
attr_reader :name, :turn
|
|
134
|
+
# handle is open while opened is set; leased is set while this pool
|
|
135
|
+
# has the write lock through lease; last_call_failed keeps the release
|
|
136
|
+
# from offloading after a failed call; idle_close is the seconds a
|
|
137
|
+
# rule's database stays open unused, nil never closing it;
|
|
138
|
+
# close_after_use is set for a rollback-journal database whose rule
|
|
139
|
+
# offloads rollback-journal databases, which the pool closes as soon as
|
|
140
|
+
# the last use block exits; users counts calls between joining and
|
|
141
|
+
# leaving, blocks included.
|
|
142
|
+
attr_accessor :handle, :path, :opened, :leased, :lease_settings, :last_call_failed, :idle_close,
|
|
143
|
+
:close_after_use, :users, :last_used
|
|
144
|
+
|
|
145
|
+
def initialize(name)
|
|
146
|
+
@name = name
|
|
147
|
+
@turn = Mutex.new
|
|
148
|
+
@handle = nil
|
|
149
|
+
@path = ""
|
|
150
|
+
@opened = false
|
|
151
|
+
@leased = false
|
|
152
|
+
@lease_settings = nil
|
|
153
|
+
@last_call_failed = false
|
|
154
|
+
@idle_close = nil
|
|
155
|
+
@close_after_use = false
|
|
156
|
+
@users = 0
|
|
157
|
+
@last_used = nil
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def initialize(client, adapter, settings)
|
|
162
|
+
@client = client
|
|
163
|
+
@adapter = adapter
|
|
164
|
+
@settings = settings
|
|
165
|
+
@owner_pid = Process.pid
|
|
166
|
+
@mutex = Mutex.new
|
|
167
|
+
@calls_done = ConditionVariable.new
|
|
168
|
+
@stop_signal = ConditionVariable.new
|
|
169
|
+
@entries = {}
|
|
170
|
+
@rules = {}
|
|
171
|
+
@active_calls = 0
|
|
172
|
+
@closing = false
|
|
173
|
+
@stopped = false
|
|
174
|
+
@idle_thread = Thread.new { close_idle_databases }
|
|
175
|
+
@idle_thread.name = "litebackup-pool-idle"
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Yields the open handle of a database a backup rule on this machine
|
|
179
|
+
# covers, and returns what the block returns.
|
|
180
|
+
#
|
|
181
|
+
# The pool opens it on first use and keeps it open; an offloaded copy is
|
|
182
|
+
# brought back first with acquire, never recreated empty. A
|
|
183
|
+
# rollback-journal database under a rule with offload_rollback_journal is
|
|
184
|
+
# closed as soon as its last use block exits instead, because that rule
|
|
185
|
+
# offloads it while nothing has it open. Uses of one database share the
|
|
186
|
+
# handle, concurrently, so the adapter's handle must be safe to share
|
|
187
|
+
# between threads. A name no rule on this machine covers, or an acquired
|
|
188
|
+
# database, is refused with invalid_argument; lease it instead.
|
|
189
|
+
def use(name, &block)
|
|
190
|
+
raise Litebackup.invalid_argument("use needs a block") if block.nil?
|
|
191
|
+
|
|
192
|
+
Thread.handle_interrupt(Object => :never) do
|
|
193
|
+
entry = join(name)
|
|
194
|
+
begin
|
|
195
|
+
Thread.handle_interrupt(Object => :immediate) do
|
|
196
|
+
handle = entry.turn.synchronize do
|
|
197
|
+
raise Litebackup.invalid_argument("#{name} is leased in this pool; use lease for it") if entry.leased
|
|
198
|
+
|
|
199
|
+
# Opening is never interrupted halfway, so an open handle is always the entry's.
|
|
200
|
+
Thread.handle_interrupt(Object => :never) { open_rule_database(entry) } unless entry.opened
|
|
201
|
+
entry.handle
|
|
202
|
+
end
|
|
203
|
+
block.call(handle)
|
|
204
|
+
end
|
|
205
|
+
ensure
|
|
206
|
+
leave(entry)
|
|
207
|
+
close_after_last_use(entry) if entry.close_after_use
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Yields the open handle of a database that moves between machines,
|
|
213
|
+
# exclusively, and returns what the block returns.
|
|
214
|
+
#
|
|
215
|
+
# Leases of one name run one at a time, and no other machine writes while
|
|
216
|
+
# the pool keeps the lock. The first lease acquires the write lock,
|
|
217
|
+
# creating an empty file and its directory at path when nothing is there
|
|
218
|
+
# unless require_existing, and opens it; later leases reuse the lock and
|
|
219
|
+
# the handle. lock_timeout is how long that acquire waits while another
|
|
220
|
+
# machine has the write lock; without it the lease fails at once with
|
|
221
|
+
# lock_unavailable. acquire_timeout limits downloading the head;
|
|
222
|
+
# maintenance checkpoints on release and VACUUMs past the freelist
|
|
223
|
+
# threshold.
|
|
224
|
+
#
|
|
225
|
+
# A later lease first asks the daemon whether this machine still has the
|
|
226
|
+
# lock: when it lost it, the handle is closed, the lease dropped, and
|
|
227
|
+
# lock_lost raised without calling the block; the next lease acquires
|
|
228
|
+
# again. A loss while a block is active shows at the next lease.
|
|
229
|
+
#
|
|
230
|
+
# With durability, a block that ends normally syncs and returns only once
|
|
231
|
+
# the server, or the bucket with "cloud", has what it committed; without
|
|
232
|
+
# it, the block's writes sync in the background. The release that ends
|
|
233
|
+
# the lease waits for the same durability, the server when nil, and with
|
|
234
|
+
# offload removes the local file, unless the last call did not finish:
|
|
235
|
+
# a block that raised or that the code left with break or return, or a
|
|
236
|
+
# durability wait that failed, keeps the lease and its local copy, and
|
|
237
|
+
# never offloads. next ends the block normally.
|
|
238
|
+
#
|
|
239
|
+
# Checking, acquiring, and opening the lease, and the release that ends
|
|
240
|
+
# it, are never interrupted halfway: Timeout, Thread#raise, and Ctrl-C
|
|
241
|
+
# take effect once they return, so an acquired lock and an open handle
|
|
242
|
+
# always stay the pool's to release.
|
|
243
|
+
#
|
|
244
|
+
# The lease ends after idle_release without a block, on release, or on
|
|
245
|
+
# close. The adapters never commit for the block: commit what it should keep.
|
|
246
|
+
def lease(name, path: nil, require_existing: false, durability: nil, offload: false, lock_timeout: nil,
|
|
247
|
+
acquire_timeout: nil, maintenance: false, &block)
|
|
248
|
+
raise Litebackup.invalid_argument("lease needs a block") if block.nil?
|
|
249
|
+
|
|
250
|
+
lease_settings = LeaseSettings.build(path: path, require_existing: require_existing, durability: durability,
|
|
251
|
+
offload: offload, lock_timeout: lock_timeout,
|
|
252
|
+
acquire_timeout: acquire_timeout, maintenance: maintenance)
|
|
253
|
+
Thread.handle_interrupt(Object => :never) do
|
|
254
|
+
entry = join(name)
|
|
255
|
+
begin
|
|
256
|
+
Thread.handle_interrupt(Object => :immediate) do
|
|
257
|
+
entry.turn.synchronize do
|
|
258
|
+
if entry.opened && !entry.leased
|
|
259
|
+
raise Litebackup.invalid_argument("#{name} is a backup rule's database in this pool; use use for it")
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
Thread.handle_interrupt(Object => :never) { prepare_lease(entry, lease_settings) }
|
|
263
|
+
finished = false
|
|
264
|
+
begin
|
|
265
|
+
block_value = block.call(entry.handle)
|
|
266
|
+
wait_durable(name, lease_settings.durability) unless lease_settings.durability.nil?
|
|
267
|
+
finished = true
|
|
268
|
+
ensure
|
|
269
|
+
entry.last_call_failed = !finished
|
|
270
|
+
end
|
|
271
|
+
block_value
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
ensure
|
|
275
|
+
leave(entry)
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# Ends the lease of a database once an active lease block returns: closes
|
|
281
|
+
# the handle, then releases the write lock once the final version reaches
|
|
282
|
+
# the lease's durability. Raises LeaseCleanupError when closing or
|
|
283
|
+
# releasing fails, and invalid_argument for a database the pool does not lease.
|
|
284
|
+
def release(name)
|
|
285
|
+
ensure_owner
|
|
286
|
+
entry = @mutex.synchronize { @entries[name] }
|
|
287
|
+
raise Litebackup.invalid_argument("#{name} is not leased in this pool") if entry.nil?
|
|
288
|
+
|
|
289
|
+
entry.turn.synchronize do
|
|
290
|
+
raise Litebackup.invalid_argument("#{name} is not leased in this pool") unless entry.leased
|
|
291
|
+
|
|
292
|
+
release_lease(entry)
|
|
293
|
+
end
|
|
294
|
+
nil
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# Refuses new calls, waits for every active use and lease block, then
|
|
298
|
+
# releases every leased database and closes every open one. A failure
|
|
299
|
+
# raises alone, several as an ErrorGroup; a LeaseCleanupError names a
|
|
300
|
+
# database that stays acquired. A second close returns at once.
|
|
301
|
+
def close
|
|
302
|
+
ensure_owner
|
|
303
|
+
return nil unless start_closing
|
|
304
|
+
|
|
305
|
+
@mutex.synchronize do
|
|
306
|
+
@calls_done.wait(@mutex) until @active_calls.zero?
|
|
307
|
+
@stopped = true
|
|
308
|
+
@stop_signal.broadcast
|
|
309
|
+
end
|
|
310
|
+
@idle_thread.join unless @idle_thread == Thread.current
|
|
311
|
+
failures = []
|
|
312
|
+
all_entries.each do |entry|
|
|
313
|
+
entry.turn.synchronize do
|
|
314
|
+
if entry.leased
|
|
315
|
+
begin
|
|
316
|
+
release_lease(entry)
|
|
317
|
+
rescue LeaseCleanupError => e
|
|
318
|
+
failures << e
|
|
319
|
+
end
|
|
320
|
+
elsif entry.opened
|
|
321
|
+
failure = Thread.handle_interrupt(Object => :never) { close_rule_database(entry) }
|
|
322
|
+
failures << failure unless failure.nil?
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
Litebackup.raise_together(failures, "closing the pool")
|
|
327
|
+
nil
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
private
|
|
331
|
+
|
|
332
|
+
# Closes an open rule database and drops its entry; returns the close's
|
|
333
|
+
# error, or nil. The caller has entry's turn.
|
|
334
|
+
def close_rule_database(entry)
|
|
335
|
+
@adapter.close(entry.handle)
|
|
336
|
+
entry.handle = nil
|
|
337
|
+
entry.opened = false
|
|
338
|
+
remove_unused_entry(entry)
|
|
339
|
+
nil
|
|
340
|
+
rescue StandardError => e
|
|
341
|
+
e
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
# Checks a kept lease, or acquires a new one, and opens its handle; the
|
|
345
|
+
# caller has entry's turn.
|
|
346
|
+
def prepare_lease(entry, lease_settings)
|
|
347
|
+
check_lease(entry) if entry.leased
|
|
348
|
+
entry.lease_settings = lease_settings
|
|
349
|
+
if !entry.leased
|
|
350
|
+
acquire_lease(entry, lease_settings)
|
|
351
|
+
elsif !entry.opened
|
|
352
|
+
reopen_lease(entry)
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def ensure_owner
|
|
357
|
+
return if Process.pid == @owner_pid
|
|
358
|
+
|
|
359
|
+
raise Litebackup.invalid_argument(
|
|
360
|
+
"this pool was made in process #{@owner_pid} and cannot be used after a fork; make a new pool in this process"
|
|
361
|
+
)
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def pool_closed
|
|
365
|
+
Error.new(ErrorCode::CLOSED, "the pool is closed")
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
def monotonic
|
|
369
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
# Counts a call on the name's entry, creating it.
|
|
373
|
+
def join(name)
|
|
374
|
+
ensure_owner
|
|
375
|
+
raise Litebackup.invalid_argument("a pool call needs a database name") unless Wire.supplied?(name)
|
|
376
|
+
|
|
377
|
+
@mutex.synchronize do
|
|
378
|
+
raise pool_closed if @closing
|
|
379
|
+
|
|
380
|
+
entry = (@entries[name] ||= Entry.new(name))
|
|
381
|
+
entry.users += 1
|
|
382
|
+
@active_calls += 1
|
|
383
|
+
entry
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
# Ends a call on an entry; an entry with nothing open or leased is dropped.
|
|
388
|
+
def leave(entry)
|
|
389
|
+
@mutex.synchronize do
|
|
390
|
+
entry.users -= 1
|
|
391
|
+
@active_calls -= 1
|
|
392
|
+
entry.last_used = monotonic
|
|
393
|
+
if entry.users.zero? && !entry.opened && !entry.leased && @entries[entry.name].equal?(entry)
|
|
394
|
+
@entries.delete(entry.name)
|
|
395
|
+
end
|
|
396
|
+
@calls_done.broadcast if @closing && @active_calls.zero?
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# Refuses new calls; false when the pool was closing already.
|
|
401
|
+
def start_closing
|
|
402
|
+
@mutex.synchronize do
|
|
403
|
+
return false if @closing
|
|
404
|
+
|
|
405
|
+
@closing = true
|
|
406
|
+
true
|
|
407
|
+
end
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def all_entries
|
|
411
|
+
@mutex.synchronize { @entries.values }
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def idle_entries
|
|
415
|
+
@mutex.synchronize { @entries.values.select { |entry| entry.users.zero? && !entry.last_used.nil? } }
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
# How long an entry has been unused, or nil while a call uses it.
|
|
419
|
+
def idle_seconds(entry, now)
|
|
420
|
+
@mutex.synchronize do
|
|
421
|
+
entry.users.positive? || entry.last_used.nil? ? nil : now - entry.last_used
|
|
422
|
+
end
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# Drops an entry with nothing open or leased once no call uses it.
|
|
426
|
+
def remove_unused_entry(entry)
|
|
427
|
+
@mutex.synchronize do
|
|
428
|
+
if @entries[entry.name].equal?(entry) && entry.users.zero? && !entry.opened && !entry.leased
|
|
429
|
+
@entries.delete(entry.name)
|
|
430
|
+
end
|
|
431
|
+
end
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
def rule_lookup_needed?(status)
|
|
435
|
+
@settings.idle_close.nil? || status.journal_mode == Wire::JOURNAL_MODE_ROLLBACK
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
# Sets when a rule's database closes: after its idle close, and after
|
|
439
|
+
# each use under a rule that offloads rollback-journal databases.
|
|
440
|
+
def set_rule_closing(entry, status, rule)
|
|
441
|
+
entry.idle_close = idle_close_seconds(rule)
|
|
442
|
+
entry.close_after_use = status.journal_mode == Wire::JOURNAL_MODE_ROLLBACK && !rule.nil? &&
|
|
443
|
+
rule.offload_rollback_journal == true
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# Whether an entry that closes after use has no active call and an open
|
|
447
|
+
# handle; the caller has its turn.
|
|
448
|
+
def last_use_ended?(entry)
|
|
449
|
+
@mutex.synchronize { entry.close_after_use && entry.users.zero? && entry.opened && !entry.leased }
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
def idle_close_seconds(rule)
|
|
453
|
+
return @settings.idle_close unless @settings.idle_close.nil?
|
|
454
|
+
return Pool.offload_idle_close_seconds if !rule.nil? && rule.maximum_local_databases.to_i.positive?
|
|
455
|
+
|
|
456
|
+
nil
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
def release_durability_and_offload(entry)
|
|
460
|
+
lease_settings = entry.lease_settings
|
|
461
|
+
return [Durability::SERVER, false] if lease_settings.nil?
|
|
462
|
+
|
|
463
|
+
[lease_settings.release_durability, lease_settings.offload == true && !entry.last_call_failed]
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# What an idle check does with an entry whose turn the caller has: release, close, or nothing.
|
|
467
|
+
def idle_step(entry, now)
|
|
468
|
+
idle = idle_seconds(entry, now)
|
|
469
|
+
return nil if idle.nil?
|
|
470
|
+
|
|
471
|
+
idle_release = @settings.idle_release
|
|
472
|
+
return IDLE_RELEASE_STEP if entry.leased && !idle_release.nil? && idle >= idle_release
|
|
473
|
+
# Its close after the last use failed: try again at once.
|
|
474
|
+
return IDLE_CLOSE_STEP if !entry.leased && entry.opened && entry.close_after_use
|
|
475
|
+
return IDLE_CLOSE_STEP if !entry.leased && entry.opened && !entry.idle_close.nil? && idle >= entry.idle_close
|
|
476
|
+
|
|
477
|
+
nil
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
def acquired_path(acquired, lease_settings, name)
|
|
481
|
+
unless acquired.database.acquired
|
|
482
|
+
raise Litebackup.invalid_argument("#{name} is a backup rule's database on this machine; use use for it")
|
|
483
|
+
end
|
|
484
|
+
return acquired.database.path unless acquired.database.path.to_s.empty?
|
|
485
|
+
|
|
486
|
+
Wire.supplied?(lease_settings.path) ? Wire.absolute_path(lease_settings.path) : ""
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
def rule_database?(status)
|
|
490
|
+
status.role == Wire::ROLE_ORIGIN && !status.acquired && !status.failover && Wire.supplied?(status.backup_rule_id)
|
|
491
|
+
end
|
|
492
|
+
|
|
493
|
+
# Closes a database its rule offloads while nothing has it open, once no use block is active.
|
|
494
|
+
def close_after_last_use(entry)
|
|
495
|
+
failure = nil
|
|
496
|
+
entry.turn.synchronize do
|
|
497
|
+
return unless last_use_ended?(entry)
|
|
498
|
+
|
|
499
|
+
begin
|
|
500
|
+
@adapter.close(entry.handle)
|
|
501
|
+
rescue StandardError => e
|
|
502
|
+
failure = use_close_failed(entry, e)
|
|
503
|
+
else
|
|
504
|
+
entry.handle = nil
|
|
505
|
+
entry.opened = false
|
|
506
|
+
remove_unused_entry(entry)
|
|
507
|
+
end
|
|
508
|
+
end
|
|
509
|
+
report_release_error(failure) unless failure.nil?
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
# Opens a rule's database, bringing an offloaded copy back first, and
|
|
513
|
+
# tries once more when the copy was offloaded between the two.
|
|
514
|
+
def open_rule_database(entry)
|
|
515
|
+
status = nil
|
|
516
|
+
handle = nil
|
|
517
|
+
2.times do |attempt|
|
|
518
|
+
status = @client.databases.get(entry.name)
|
|
519
|
+
unless rule_database?(status)
|
|
520
|
+
raise Litebackup.invalid_argument(
|
|
521
|
+
"#{entry.name} is not a backup rule's database on this machine; " \
|
|
522
|
+
"use lease for a database that moves between machines"
|
|
523
|
+
)
|
|
524
|
+
end
|
|
525
|
+
@client.databases.acquire(entry.name) if status.state == Wire::STATE_OFFLOADED
|
|
526
|
+
begin
|
|
527
|
+
handle = @adapter.open(status.path)
|
|
528
|
+
rescue StandardError
|
|
529
|
+
next if attempt.zero? && !File.exist?(status.path)
|
|
530
|
+
|
|
531
|
+
raise
|
|
532
|
+
end
|
|
533
|
+
break
|
|
534
|
+
end
|
|
535
|
+
entry.handle = handle
|
|
536
|
+
entry.path = status.path
|
|
537
|
+
entry.opened = true
|
|
538
|
+
rule = rule_lookup_needed?(status) ? database_rule(status.backup_rule_id) : nil
|
|
539
|
+
set_rule_closing(entry, status, rule)
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
# The backup rule with rule_id, from the pool's cache or rules.list.
|
|
543
|
+
def database_rule(rule_id)
|
|
544
|
+
rule = @mutex.synchronize { @rules[rule_id] }
|
|
545
|
+
return rule unless rule.nil?
|
|
546
|
+
|
|
547
|
+
listed = @client.rules.list
|
|
548
|
+
@mutex.synchronize do
|
|
549
|
+
listed.each { |listed_rule| @rules[listed_rule.id] = listed_rule }
|
|
550
|
+
@rules[rule_id]
|
|
551
|
+
end
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
# Drops a lease whose lock this machine lost and raises lock_lost; the caller has entry's turn.
|
|
555
|
+
def check_lease(entry)
|
|
556
|
+
status = begin
|
|
557
|
+
@client.databases.get(entry.name)
|
|
558
|
+
rescue Error => e
|
|
559
|
+
raise unless e.code == ErrorCode::NOT_FOUND
|
|
560
|
+
|
|
561
|
+
nil
|
|
562
|
+
end
|
|
563
|
+
return unless status.nil? || !status.acquired || status.state == Wire::STATE_CONFLICTED
|
|
564
|
+
|
|
565
|
+
drop_lost_lease(entry)
|
|
566
|
+
raise Error.new(
|
|
567
|
+
ErrorCode::LOCK_LOST,
|
|
568
|
+
"this machine no longer has the write lock of #{entry.name}, so the pool dropped its lease; " \
|
|
569
|
+
"the next lease acquires it again"
|
|
570
|
+
)
|
|
571
|
+
end
|
|
572
|
+
|
|
573
|
+
# Opens the handle of a lease whose release failed after its handle closed.
|
|
574
|
+
def reopen_lease(entry)
|
|
575
|
+
begin
|
|
576
|
+
entry.handle = @adapter.open(entry.path)
|
|
577
|
+
rescue Exception # rubocop:disable Lint/RescueException
|
|
578
|
+
entry.last_call_failed = true
|
|
579
|
+
raise
|
|
580
|
+
end
|
|
581
|
+
entry.opened = true
|
|
582
|
+
end
|
|
583
|
+
|
|
584
|
+
# Acquires the write lock and opens the database; one that does not open is released again at once.
|
|
585
|
+
def acquire_lease(entry, lease_settings)
|
|
586
|
+
if Wire.supplied?(lease_settings.path) && !lease_settings.require_existing
|
|
587
|
+
Pool.create_empty_file(lease_settings.path)
|
|
588
|
+
end
|
|
589
|
+
acquired = @client.databases.acquire(
|
|
590
|
+
entry.name,
|
|
591
|
+
path: lease_settings.path, maintenance: lease_settings.maintenance,
|
|
592
|
+
machine_label: @settings.machine_label, timeout: lease_settings.acquire_timeout,
|
|
593
|
+
lock_timeout: lease_settings.lock_timeout
|
|
594
|
+
)
|
|
595
|
+
path = acquired_path(acquired, lease_settings, entry.name)
|
|
596
|
+
begin
|
|
597
|
+
handle = @adapter.open(path)
|
|
598
|
+
rescue StandardError => e
|
|
599
|
+
# Nothing is open: release the lock again, without offload.
|
|
600
|
+
entry.path = path
|
|
601
|
+
entry.leased = true
|
|
602
|
+
entry.last_call_failed = true
|
|
603
|
+
begin
|
|
604
|
+
release_lease(entry)
|
|
605
|
+
rescue LeaseCleanupError => release_error
|
|
606
|
+
Litebackup.raise_together([e, release_error], "leasing #{entry.name}")
|
|
607
|
+
end
|
|
608
|
+
raise
|
|
609
|
+
end
|
|
610
|
+
entry.handle = handle
|
|
611
|
+
entry.path = path
|
|
612
|
+
entry.opened = true
|
|
613
|
+
entry.leased = true
|
|
614
|
+
end
|
|
615
|
+
|
|
616
|
+
# Syncs now and returns once the server, or the bucket with cloud, has the version the sync reached.
|
|
617
|
+
def wait_durable(name, durability)
|
|
618
|
+
status = @client.databases.sync(name)
|
|
619
|
+
return unless durability == Durability::CLOUD
|
|
620
|
+
|
|
621
|
+
target = status.synced_server.version
|
|
622
|
+
while status.synced_cloud.version < target
|
|
623
|
+
sleep(CLOUD_DURABILITY_POLL_SECONDS)
|
|
624
|
+
status = @client.databases.get(name)
|
|
625
|
+
end
|
|
626
|
+
end
|
|
627
|
+
|
|
628
|
+
# Closes the handle and releases the lock within one cleanup timeout; the
|
|
629
|
+
# caller has entry's turn. Closing is never cut short; the release gets
|
|
630
|
+
# what closing left. Neither is interrupted halfway, so the entry always
|
|
631
|
+
# says whether the handle is open and the lock is the pool's.
|
|
632
|
+
def release_lease(entry)
|
|
633
|
+
Thread.handle_interrupt(Object => :never) { close_and_release(entry) }
|
|
634
|
+
end
|
|
635
|
+
|
|
636
|
+
def close_and_release(entry)
|
|
637
|
+
deadline = monotonic + @settings.cleanup_seconds
|
|
638
|
+
if entry.opened
|
|
639
|
+
begin
|
|
640
|
+
@adapter.close(entry.handle)
|
|
641
|
+
rescue StandardError => e
|
|
642
|
+
raise close_failed(entry, e), cause: e
|
|
643
|
+
end
|
|
644
|
+
entry.handle = nil
|
|
645
|
+
entry.opened = false
|
|
646
|
+
end
|
|
647
|
+
durability, offload = release_durability_and_offload(entry)
|
|
648
|
+
timeout = [deadline - monotonic, MINIMUM_RELEASE_SECONDS].max
|
|
649
|
+
begin
|
|
650
|
+
@client.databases.release(entry.name, durability: durability, offload: offload, timeout: timeout)
|
|
651
|
+
rescue StandardError => e
|
|
652
|
+
raise release_failed(entry, durability, e), cause: e
|
|
653
|
+
end
|
|
654
|
+
entry.leased = false
|
|
655
|
+
remove_unused_entry(entry)
|
|
656
|
+
end
|
|
657
|
+
|
|
658
|
+
# Closes the handle of a database whose lock was lost, quietly, and drops
|
|
659
|
+
# the lease; the copy stays as the daemon left it. The caller has entry's turn.
|
|
660
|
+
def drop_lost_lease(entry)
|
|
661
|
+
if entry.opened
|
|
662
|
+
begin
|
|
663
|
+
@adapter.close(entry.handle)
|
|
664
|
+
rescue StandardError => e
|
|
665
|
+
Litebackup.report_warning("closing #{entry.name} after its write lock was lost failed: #{e.message}")
|
|
666
|
+
end
|
|
667
|
+
entry.handle = nil
|
|
668
|
+
entry.opened = false
|
|
669
|
+
end
|
|
670
|
+
entry.leased = false
|
|
671
|
+
end
|
|
672
|
+
|
|
673
|
+
def close_failed(entry, error)
|
|
674
|
+
LeaseCleanupError.new(
|
|
675
|
+
LeaseCleanupStep::CLOSE, entry.name,
|
|
676
|
+
"closing #{entry.name} failed, so it was not released and stays acquired by this machine: #{error.message}. " \
|
|
677
|
+
"Close every connection to #{entry.path}, then call pool.release(#{entry.name.inspect}) again",
|
|
678
|
+
{ "step" => LeaseCleanupStep::CLOSE, "name" => entry.name, "path" => entry.path, "acquired" => true }
|
|
679
|
+
)
|
|
680
|
+
end
|
|
681
|
+
|
|
682
|
+
def release_failed(entry, durability, error)
|
|
683
|
+
LeaseCleanupError.new(
|
|
684
|
+
LeaseCleanupStep::RELEASE, entry.name,
|
|
685
|
+
"releasing #{entry.name} failed: #{error.message}. Its file at #{entry.path} is intact and this machine " \
|
|
686
|
+
"might still have its write lock; call pool.release(#{entry.name.inspect}) again",
|
|
687
|
+
{ "step" => LeaseCleanupStep::RELEASE, "name" => entry.name, "path" => entry.path, "durability" => durability }
|
|
688
|
+
)
|
|
689
|
+
end
|
|
690
|
+
|
|
691
|
+
def use_close_failed(entry, error)
|
|
692
|
+
failure = LeaseCleanupError.new(
|
|
693
|
+
LeaseCleanupStep::CLOSE, entry.name,
|
|
694
|
+
"closing #{entry.name} after its last use failed, so it stays open and its rule does not offload it: " \
|
|
695
|
+
"#{error.message}. The pool tries again at its next idle check",
|
|
696
|
+
{ "step" => LeaseCleanupStep::CLOSE, "name" => entry.name, "path" => entry.path, "acquired" => false }
|
|
697
|
+
)
|
|
698
|
+
Pool.with_cause(failure, error)
|
|
699
|
+
end
|
|
700
|
+
|
|
701
|
+
# Closes rule databases past their idle close and releases leases past
|
|
702
|
+
# idle_release, until the pool closes.
|
|
703
|
+
def close_idle_databases
|
|
704
|
+
loop do
|
|
705
|
+
stopped = @mutex.synchronize do
|
|
706
|
+
@stop_signal.wait(@mutex, Pool.check_seconds) unless @stopped
|
|
707
|
+
@stopped
|
|
708
|
+
end
|
|
709
|
+
break if stopped
|
|
710
|
+
|
|
711
|
+
now = monotonic
|
|
712
|
+
idle_entries.each do |entry|
|
|
713
|
+
failure = close_idle_entry(entry, now)
|
|
714
|
+
report_release_error(failure) unless failure.nil?
|
|
715
|
+
end
|
|
716
|
+
end
|
|
717
|
+
end
|
|
718
|
+
|
|
719
|
+
# Releases or closes one idle entry; returns a failed release to report once its turn is free.
|
|
720
|
+
def close_idle_entry(entry, now)
|
|
721
|
+
return nil unless entry.turn.try_lock
|
|
722
|
+
|
|
723
|
+
begin
|
|
724
|
+
case idle_step(entry, now)
|
|
725
|
+
when IDLE_RELEASE_STEP
|
|
726
|
+
begin
|
|
727
|
+
release_lease(entry)
|
|
728
|
+
rescue LeaseCleanupError => e
|
|
729
|
+
return e
|
|
730
|
+
end
|
|
731
|
+
when IDLE_CLOSE_STEP
|
|
732
|
+
begin
|
|
733
|
+
@adapter.close(entry.handle)
|
|
734
|
+
rescue StandardError => e
|
|
735
|
+
# The entry keeps its handle, so the next check and close try again.
|
|
736
|
+
Litebackup.report_warning("closing the idle database #{entry.name} failed: #{e.message}")
|
|
737
|
+
return nil
|
|
738
|
+
end
|
|
739
|
+
entry.handle = nil
|
|
740
|
+
entry.opened = false
|
|
741
|
+
remove_unused_entry(entry)
|
|
742
|
+
end
|
|
743
|
+
nil
|
|
744
|
+
ensure
|
|
745
|
+
entry.turn.unlock
|
|
746
|
+
end
|
|
747
|
+
end
|
|
748
|
+
|
|
749
|
+
# Passes a failed background release to on_release_error on its own
|
|
750
|
+
# thread, which close never waits for, so the callback may call the pool;
|
|
751
|
+
# reports it as a warning without a callback.
|
|
752
|
+
def report_release_error(error)
|
|
753
|
+
callback = @settings.on_release_error
|
|
754
|
+
if callback.nil?
|
|
755
|
+
Litebackup.report_warning("the pool could not release #{error.name}: #{error.message}")
|
|
756
|
+
return
|
|
757
|
+
end
|
|
758
|
+
Thread.new { callback.call(error) }.name = "litebackup-pool-release-error"
|
|
759
|
+
end
|
|
760
|
+
|
|
761
|
+
class << self
|
|
762
|
+
# Creates an empty file and its directory unless something is already
|
|
763
|
+
# there. The daemon treats an empty file as no copy: a new database when
|
|
764
|
+
# the server has none, replaced by the server's head otherwise.
|
|
765
|
+
def create_empty_file(path)
|
|
766
|
+
file = Wire.absolute_path(path)
|
|
767
|
+
begin
|
|
768
|
+
# A file where the directory should be fails here too.
|
|
769
|
+
FileUtils.mkdir_p(File.dirname(file))
|
|
770
|
+
rescue SystemCallError => e
|
|
771
|
+
raise Litebackup.invalid_argument("creating #{file} failed: #{e.message}")
|
|
772
|
+
end
|
|
773
|
+
begin
|
|
774
|
+
File.open(file, File::CREAT | File::EXCL | File::WRONLY, 0o644, &:close)
|
|
775
|
+
rescue Errno::EEXIST
|
|
776
|
+
nil
|
|
777
|
+
rescue SystemCallError => e
|
|
778
|
+
raise Litebackup.invalid_argument("creating #{file} failed: #{e.message}")
|
|
779
|
+
end
|
|
780
|
+
end
|
|
781
|
+
|
|
782
|
+
# The error with cause set, as raise sets it, for an error that is reported rather than raised.
|
|
783
|
+
def with_cause(error, cause)
|
|
784
|
+
raise error, cause: cause
|
|
785
|
+
rescue LeaseCleanupError => e
|
|
786
|
+
e
|
|
787
|
+
end
|
|
788
|
+
end
|
|
789
|
+
end
|
|
790
|
+
end
|