Class: LaunchDarkly::Impl::FileData::Reloader Private
- Inherits:
-
Object
- Object
- LaunchDarkly::Impl::FileData::Reloader
- Defined in:
- lib/ldclient-rb/impl/file_data/reloader.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Owns the reload cycle for a set of data files. It serializes reloads, debounces change
signals, retains the last good result on failure (by not calling apply), retries after
failures, holds back a file that was emptied before applying it, and skips no-op
applications.
Applications and editors rewrite files in place: they truncate the file, then write the new
content. A load in between finds the file empty, or, for YAML, can find only a comment or
"---", which the YAML parser reads as nothing. Whether a file with no entries is valid
depends on its format (see Document.parse), so a parser cannot tell an emptied file from
one in the middle of a rewrite. So a load that finds a file with no content (empty, white
space, or YAML that is only a null), while the data currently applied has content from that
file, holds the file back: it reads the other files, so that files emptied together are
held back together, then keeps the current data and arms the retry, without reporting a
failure. A file that cannot be read or parsed still fails the load. A YAML document such as
{} is content, and is applied at once.
A file is held back until it has stayed so, without being written, for the retry delay, counted from the load that read it. A change of its state (its modification time and size, read through any symbolic link) means it was written since a load last found it so, for example rewritten and truncated again between two loads, so the delay starts over. Each file is held back for at most five retry delays from the first load that held it back. The bound goes on across reads that find the file with content, and starts over only at a load that holds nothing back, whether that load applies, is skipped as unchanged, or fails, for each file it read. A load reads every file, even after one fails to read or parse, and fails with the first failure after them. So a file that keeps being rewritten, or files rewritten in turn, cannot hold the data back without end. A read that finds a file with content, even content that fails to parse, or finds it missing, ends its empty state, so its next emptying starts a new delay. The retry that a hold arms comes when the first held file's delay or bound is over, plus a tenth of the retry delay. A file that contributed no content to the data currently applied, such as a placeholder that has been empty from the start, a file that was missing, or a file whose emptying has been applied, is parsed at once. So is every file when there are no automatic retries, because nothing would read it again. After the delay, an empty JSON file fails to parse and the data stays; an empty YAML file is a document with no entries.
The worker thread starts on the first #reload_now or #trigger call rather than in the constructor. A reloader can be constructed by a component whose lifecycle never uses it, and construction alone must not leak a thread.
Constant Summary collapse
- DEFAULT_DEBOUNCE_DELAY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
A settle window long enough to coalesce the burst of change notifications produced by a single file edit, and short enough to stay responsive. In seconds.
0.1- DEFAULT_RETRY_DELAY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Bounds how long a failed reload can go uncorrected when no further change notification arrives, for example when the failure came from reading a file mid-write. Reading a local file is cheap, so this can be short. In seconds.
1.0- MAX_HOLD_RETRY_DELAYS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
A file is held back for at most this many retry delays from the first load that held it back. The bound starts over at a load that holds nothing back.
5
Instance Method Summary collapse
-
#initialize(paths:, logger:, apply:, on_error: nil, duplicate_keys_handling: DuplicateKeysHandling::FAIL, skip_missing_paths: false, debounce_delay: DEFAULT_DEBOUNCE_DELAY, retry_delay: DEFAULT_RETRY_DELAY, skip_unchanged: false, log_prefix: "[LDClient]", check_document: nil, clock: nil) ⇒ Reloader
constructor
private
A new instance of Reloader.
-
#last_armed_retry_delay ⇒ Numeric?
private
The delay in seconds of the last retry that was armed, from the time it was armed.
-
#reload_now ⇒ Boolean
private
Synchronously loads the files and applies the result, or reports the failure.
-
#stop ⇒ Object
private
Stops the reloader.
-
#trigger ⇒ Object
private
Signals that the files may have changed and a reload should happen after the debounce delay.
Constructor Details
#initialize(paths:, logger:, apply:, on_error: nil, duplicate_keys_handling: DuplicateKeysHandling::FAIL, skip_missing_paths: false, debounce_delay: DEFAULT_DEBOUNCE_DELAY, retry_delay: DEFAULT_RETRY_DELAY, skip_unchanged: false, log_prefix: "[LDClient]", check_document: nil, clock: nil) ⇒ Reloader
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Reloader.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/ldclient-rb/impl/file_data/reloader.rb', line 108 def initialize(paths:, logger:, apply:, on_error: nil, duplicate_keys_handling: DuplicateKeysHandling::FAIL, skip_missing_paths: false, debounce_delay: DEFAULT_DEBOUNCE_DELAY, retry_delay: DEFAULT_RETRY_DELAY, skip_unchanged: false, log_prefix: "[LDClient]", check_document: nil, clock: nil) @paths = paths @logger = logger @log_prefix = log_prefix @apply = apply @on_error = on_error @check_document = check_document @duplicate_keys_handling = duplicate_keys_handling @skip_missing_paths = skip_missing_paths @debounce_delay = debounce_delay @retry_delay = retry_delay @skip_unchanged = skip_unchanged @clock = clock || method(:monotonic_now) # Guards the scheduling state below and wakes the worker. @mutex = Mutex.new @cond = ConditionVariable.new @worker = nil @stopped = false @trigger_pending = false @requested_retry_delay = nil @debounce_deadline = nil @retry_deadline = nil # Serializes the actual load work between reload_now and the worker. @reload_mutex = Mutex.new @last_good_digest = nil @last_error_message = nil # Whether each configured path contributed content to the data currently applied; the # EmptyFile of each path whose content is in effect and that loads have found with no # content since; whether the last load held a file back; and whether the load in progress # holds one back. Only touched while holding @reload_mutex. @applied_had_content = {} @empty_files = {} @read_again_pending = false @holding_this_load = false # The delay of the last retry that was armed. Only tests read it. Guarded by @mutex. @last_armed_retry_delay = nil end |
Instance Method Details
#last_armed_retry_delay ⇒ Numeric?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The delay in seconds of the last retry that was armed, from the time it was armed. Only tests use it.
177 178 179 |
# File 'lib/ldclient-rb/impl/file_data/reloader.rb', line 177 def last_armed_retry_delay @mutex.synchronize { @last_armed_retry_delay } end |
#reload_now ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Synchronously loads the files and applies the result, or reports the failure. Use it for the initial load. A failure here schedules the same automatic retry as a failed triggered reload, and a load that holds back a file it finds empty arms the retry for when the file may be read as it is.
164 165 166 167 168 169 |
# File 'lib/ldclient-rb/impl/file_data/reloader.rb', line 164 def reload_now ensure_started retry_in = reload(retrying: false) request_retry(retry_in) unless retry_in.nil? retry_in.nil? end |
#stop ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Stops the reloader. It does not wait for a reload that is already in progress. A reload
wedged in a blocking file read must not be able to wedge shutdown. Such a reload can
still deliver its result through apply or on_error shortly after this method
returns, and consumers tolerate that. A reload that has not yet reached its callbacks
when this method is called does not invoke them.
201 202 203 204 205 206 |
# File 'lib/ldclient-rb/impl/file_data/reloader.rb', line 201 def stop @mutex.synchronize do @stopped = true @cond.broadcast end end |
#trigger ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Signals that the files may have changed and a reload should happen after the debounce delay. It never blocks. Signals that arrive while a reload is already pending are coalesced.
186 187 188 189 190 191 192 |
# File 'lib/ldclient-rb/impl/file_data/reloader.rb', line 186 def trigger ensure_started @mutex.synchronize do @trigger_pending = true @cond.signal end end |