Class: LaunchDarkly::Impl::FileData::Reloader Private

Inherits:
Object
  • Object
show all
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.

Since:

  • 5.5.0

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.

Since:

  • 5.5.0

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.

Since:

  • 5.5.0

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.

Since:

  • 5.5.0

5

Instance Method Summary collapse

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.

Parameters:

  • paths (Array<String>) —

    the files to load, already resolved to absolute paths. The order is significant: it determines which file wins under the duplicate keys handling.

  • logger (Logger)
  • apply (#call) —

    invoked with each successfully merged MergeResult. Calls are serialized, so implementations do not need their own synchronization against other reloads. An exception from apply is a failed reload: it is reported through on_error, the result is not remembered as applied, and the reload is retried. apply and on_error must not call back into #stop.

  • on_error (#call, nil) (defaults to: nil) —

    invoked with the error when a reload fails, once per distinct failure. With automatic retries, repeats of an identical failure do not re-invoke it. A success re-arms it. The error is a LaunchDarkly::Impl::FileData::ReadError when a file could not be read or parsed, a MergeError when the documents could not be combined or check_document rejected one, or the exception that apply raised. The reloader logs failures itself.

  • duplicate_keys_handling (Symbol) (defaults to: DuplicateKeysHandling::FAIL) —

    one of the DuplicateKeysHandling values

  • skip_missing_paths (Boolean) (defaults to: false) —

    when true, a configured file that does not exist contributes no data, and the reload succeeds with the data of the files that exist. When false, a missing file fails the reload like any other read error.

  • debounce_delay (Numeric) (defaults to: DEFAULT_DEBOUNCE_DELAY) —

    seconds to wait after a #trigger call for further calls to settle before reloading. If zero or negative, each trigger reloads at once.

  • retry_delay (Numeric) (defaults to: DEFAULT_RETRY_DELAY) —

    seconds to wait after a failed reload before retrying automatically. A file whose content is in effect and that is found empty is held back for the same delay. If zero or negative, there is no automatic retry, and an empty file is parsed as it is.

  • skip_unchanged (Boolean) (defaults to: false) —

    if true, apply is not invoked when the files' raw contents are byte-identical to the last successfully applied contents.

  • log_prefix (String) (defaults to: "[LDClient]") —

    the prefix of every log line this reloader writes

  • check_document (#call, nil) (defaults to: nil) —

    invoked with the path and the Document of each file that a load parsed, before the documents are combined. A MergeError that it raises fails the load in the same way as a file that cannot be parsed. When nil, every document is combined.

  • clock (#call, nil) (defaults to: nil) —

    returns the seconds of a monotonic clock, for the time a file has been held back; nil for the system's. Scheduling always uses the system's clock.

Since:

  • 5.5.0



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.

Returns:

  • (Numeric, nil)

Since:

  • 5.5.0



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.

Returns:

  • (Boolean) —

    true if the load succeeded, false if it failed or held back an empty file

Since:

  • 5.5.0



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.

Since:

  • 5.5.0



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.

Since:

  • 5.5.0



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