Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions lib/ldclient-rb/impl/integrations/file_data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def initialize(data_store, data_source_update_sink, logger, options={})

@version_lock = Mutex.new
@last_version = 1
@load_lock = Mutex.new
end

def initialized?
Expand All @@ -56,24 +57,38 @@ def start
# the difference can be detected by checking "initialized?"
ready.set

load_all

if @auto_update
# If we're going to watch files, then the start event will be set the first time we get
# a successful load.
@listener = start_listener
begin
@listener = start_listener
rescue => e
@logger.error { "[LDClient] Unable to watch data files for changes, so file changes will not be applied: #{e.message}" }
end
end

loaded = false
begin
load_all
loaded = true
ensure
stop unless loaded
end

ready
end

def stop
@listener.stop unless @listener.nil?
listener = @listener
@listener = nil
listener&.stop
end

private

def load_all
@load_lock.synchronize { load_all_locked }
end

def load_all_locked
all_data = {
Impl::DataStore::FEATURES => {},
Impl::DataStore::SEGMENTS => {},
Expand Down Expand Up @@ -174,7 +189,13 @@ def start_listener_with_listen_gem(resolved_paths)
load_all
end
end
l.start
started = false
begin
l.start
started = true
ensure
l.stop unless started
end
l
end

Expand Down
47 changes: 34 additions & 13 deletions lib/ldclient-rb/impl/integrations/file_data_source_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,23 @@ def fetch(selector_store)
# @return [void]
#
def sync(selector_store)
# First yield initial data
listener = nil
begin
listener = start_listener unless @lock.synchronize { @closed }
rescue => e
@logger.error { "[LDClient] Unable to watch data files for changes, so file changes will not be applied: #{e.message}" }
end
if listener
closed = @lock.synchronize do
@listener = listener unless @closed
@closed
end
listener.stop if closed
end

initial_fetch = fetch(selector_store)
unless initial_fetch.success?
stop_listener
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
yield LaunchDarkly::Interfaces::DataSystem::Update.new(
state: LaunchDarkly::Interfaces::DataSource::Status::OFF,
error: LaunchDarkly::Interfaces::DataSource::ErrorInfo.new(
Expand All @@ -136,11 +150,6 @@ def sync(selector_store)
change_set: initial_fetch.value.change_set
)

# Start watching for file changes
@lock.synchronize do
@listener = start_listener unless @closed
end

until @closed
begin
update = @update_queue.pop
Expand Down Expand Up @@ -173,17 +182,22 @@ def stop
@lock.synchronize do
return if @closed
@closed = true

listener = @listener
@listener = nil

listener&.stop
end
stop_listener

# Signal shutdown to sync generator
@update_queue.push(nil)
end

private def stop_listener
listener = @lock.synchronize do
l = @listener
@listener = nil
l
end
listener&.stop
end

#
# Load all files and build a changeset.
#
Expand Down Expand Up @@ -362,7 +376,7 @@ def stop
#
# Start watching files for changes.
#
# @return [Object] auto-updater instance
# @return [Object, nil] auto-updater instance, or nil if no path can be watched
#
private def start_listener
resolved_paths = @paths.map do |p|
Expand All @@ -373,6 +387,7 @@ def stop
nil
end
end.compact
return nil if resolved_paths.empty?

if @@have_listen
start_listener_with_listen_gem(resolved_paths)
Expand All @@ -397,7 +412,13 @@ def stop
on_file_change
end
end
l.start
started = false
begin
l.start
started = true
ensure
l.stop unless started
end
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
l
end
end
Expand Down
15 changes: 6 additions & 9 deletions lib/ldclient-rb/impl/integrations/file_override_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ def initialize(paths:, duplicate_keys_handling:, change_detection:, poll_interva
attr_reader :poll_interval

#
# Performs the initial load synchronously, so overrides present in the files are in effect
# when this method returns, then starts change detection. A file that does not exist yet
# Starts change detection, then performs the initial load synchronously, so overrides
# present in the files are in effect when this method returns. A file that does not exist yet
# contributes no overrides. A file that cannot be read or parsed is not fatal: the client
# runs with the last good overrides, the failure is logged, and the retry plus the change
# signal recover once the file is readable.
#
# (see LaunchDarkly::Interfaces::Overrides::OverrideSource#start)
#
def start(sink)
@lock.synchronize do
reloader = @lock.synchronize do
return if @stopped

@reloader = FileData::Reloader.new(
Expand All @@ -77,12 +77,6 @@ def start(sink)
log_overrides_in_effect(merged)
end
)
end

@reloader.reload_now

@lock.synchronize do
return if @stopped

trigger = @reloader.method(:trigger)
@change_detector =
Expand All @@ -91,7 +85,10 @@ def start(sink)
else
FileData::Poller.new(@paths, @poll_interval, trigger, @logger)
end
@reloader
end

reloader.reload_now
end

# (see LaunchDarkly::Interfaces::Overrides::OverrideSource#stop)
Expand Down
11 changes: 8 additions & 3 deletions lib/ldclient-rb/integrations/file_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ module FileData
# Note that the default implementation of this feature is based on polling the filesystem,
# which may not perform well. If you install the 'listen' gem (not included by default, to
# avoid adding unwanted dependencies to the SDK), its native file watching mechanism will be
# used instead.
# used instead. On platforms other than Linux, the 'listen' gem can miss a change that occurs
# while the data source starts. The data source then uses the earlier contents until the
# next change to a file.
# @option options [Float] :poll_interval The minimum interval, in seconds, between checks for
# file modifications - used only if auto_update is true, and if the native file-watching
# mechanism from 'listen' is not being used. The default value is 1 second.
Expand All @@ -125,7 +127,9 @@ def self.data_source(options={})
# may be absolute paths or relative to the current working directory. (Required)
# @option options [Float] :poll_interval The minimum interval, in seconds, between checks for
# file modifications - used only if the native file-watching mechanism from 'listen' is not
# being used. The default value is 1 second.
# being used. The default value is 1 second. On platforms other than Linux, the 'listen' gem can miss a change that occurs
# while the data source starts. The data source then uses the earlier contents until the
# next change to a file.
# @return [FileDataSourceV2Builder] a builder that can be used as an FDv2 initializer or synchronizer
#
# @example Using as an initializer
Expand Down Expand Up @@ -225,7 +229,8 @@ def self.data_source_v2(options = {})
# including the update of a mounted Kubernetes ConfigMap. It does not follow a deployment that
# points a link further along the path, or a configured directory, at another location, such as a
# `current` link to a release directory, or that replaces a configured directory. Use polling for
# such deployments.
# such deployments. On platforms other than Linux, watching can miss a change that occurs while
# the source starts. The source then uses the earlier contents until the next change to a file.
# @option options [Numeric] :poll_interval Seconds between examinations of the files in polling
# mode. The default is 1. An interval below 1 is raised to 1 with a warning.
# @return [FileOverrideSourceBuilder] a builder for {LaunchDarkly::DataSystem::ConfigBuilder#overrides}
Expand Down
115 changes: 115 additions & 0 deletions spec/integrations/file_data_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,121 @@ def test_auto_reload(options)
test_auto_reload({ auto_update: true, force_polling: true, poll_interval: 0.1 })
end

def test_reload_of_change_during_first_load(options)
file = make_temp_file(flag_only_json)
options[:paths] = [ file.path ]

with_data_source(options) do |ds|
first_load = true
allow(ds).to receive(:load_all_locked).and_wrap_original do |original|
original.call
if first_load
first_load = false
sleep(1)
IO.write(file, all_properties_json)
end
end

ds.start

max_time = 10
ok = wait_for_condition(max_time) { @store.all(LaunchDarkly::Impl::DataStore::SEGMENTS).keys == all_segment_keys }
expect(ok).to eq(true), "Waited #{max_time}s after modifying file during the first load and it did not reload"
end
end

it "reloads a file modified during the first load if auto-update is on" do
test_reload_of_change_during_first_load({ auto_update: true })
end

it "reloads a file modified during the first load in polling mode" do
test_reload_of_change_during_first_load({ auto_update: true, force_polling: true, poll_interval: 0.1 })
end

def test_reload_of_change_before_first_store_update(options)
file = make_temp_file(flag_only_json)
options[:paths] = [ file.path ]
sink = @config.data_source_update_sink

with_data_source(options) do |ds|
first_init = true
allow(sink).to receive(:init).and_wrap_original do |original, data|
if first_init
first_init = false
sleep(1)
IO.write(file, all_properties_json)
sleep(1.5)
end
original.call(data)
end

ds.start

max_time = 10
ok = wait_for_condition(max_time) { @store.all(LaunchDarkly::Impl::DataStore::SEGMENTS).keys == all_segment_keys }
expect(ok).to eq(true), "Waited #{max_time}s and the older first load replaced the newer data"
end
end

it "keeps a change made before the first load updates the store if auto-update is on" do
test_reload_of_change_before_first_store_update({ auto_update: true, poll_interval: 0.1 })
end

it "keeps a change made before the first load updates the store in polling mode" do
test_reload_of_change_before_first_store_update({ auto_update: true, force_polling: true, poll_interval: 0.1 })
end

def test_stop_watching_if_first_load_raises(options, error_class = StandardError)
file = make_temp_file(flag_only_json)
sink = @config.data_source_update_sink
allow(sink).to receive(:init).and_raise(error_class, "store failure")
ds = FileData.data_source(options.merge(paths: [ file.path ], auto_update: true)).call('', @config)
listener = nil
allow(ds).to receive(:start_listener).and_wrap_original do |original|
listener = original.call
end

expect { ds.start }.to raise_error(error_class, "store failure")
yield listener
expect { ds.stop }.not_to raise_error
end

it "stops watching if the first load raises" do
test_stop_watching_if_first_load_raises({}) do |listener|
expect(listener.stopped?).to eq(true)
end
end

it "stops watching if the first load raises an exception that is not a StandardError" do
test_stop_watching_if_first_load_raises({}, Interrupt) do |listener|
expect(listener.stopped?).to eq(true)
end
end

it "stops polling if the first load raises" do
test_stop_watching_if_first_load_raises({ force_polling: true, poll_interval: 0.1 }) do |listener|
expect(listener.instance_variable_get(:@thread).join(2)).not_to be_nil
end
end

it "loads the files if the file watcher cannot start" do
file = make_temp_file(all_properties_json)

with_data_source({ paths: [ file.path ], auto_update: true }) do |ds|
allow(ds).to receive(:start_listener).and_raise(StandardError, "watch failure")
expect { ds.start }.not_to raise_error
expect(@store.initialized?).to eq(true)
expect(@store.all(LaunchDarkly::Impl::DataStore::SEGMENTS).keys).to eq(all_segment_keys)
end
end

it "reports a missing file instead of raising if auto-update is on" do
with_data_source({ paths: [ bad_file_path ], auto_update: true }) do |ds|
expect { ds.start }.not_to raise_error
expect(@store.initialized?).to eq(false)
end
end

it "evaluates simplified flag with client as expected" do
file = make_temp_file(all_properties_json)
factory = FileData.data_source({ paths: file.path })
Expand Down
Loading