Skip to content

Commit 5cc96b0

Browse files
authored
docs: Explain the cached data warning from the FDv2 file data source (#591)
1 parent fa654b0 commit 5cc96b0

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎ldclient/datasystem.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ def streaming_ds_builder() -> StreamingDataSourceBuilder:
155155
def file_ds_builder(paths: List[str]) -> FileDataSourceV2Builder:
156156
"""
157157
Returns a builder for a file-based data source.
158+
159+
File data has no LaunchDarkly selector, so the SDK reports it as cached data. Refer to
160+
:func:`ldclient.integrations.Files.new_data_source_v2` for the warning that this causes.
158161
"""
159162
return FileDataSourceV2Builder(paths)
160163

‎ldclient/integrations/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ def new_data_source_v2(paths: str | List[str], poll_interval: float = 1, force_p
459459
If the data source encounters any error in any file-- malformed content, a missing file, or a
460460
duplicate key-- it will not load flags from any of the files.
461461
462+
File data has no LaunchDarkly selector, so the SDK reports it as cached data, not as data
463+
refreshed from LaunchDarkly. When no other source supplies data from LaunchDarkly, the first
464+
flag evaluation logs this warning once: "Feature Flag evaluation attempted before client has
465+
initialized - using last known values from feature store". The evaluation uses the file data.
466+
462467
:param paths: the paths of the source files for loading flag data. These may be absolute paths
463468
or relative to the current working directory. Files will be parsed as JSON unless the ``pyyaml``
464469
package is installed, in which case YAML is also allowed.

‎ldclient/testing/integrations/test_file_data_sources_pinned_behavior.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,45 @@ def test_v2_flag_values_evaluate_with_a_fallthrough_reason(tmp_path):
150150
assert detail.reason == {'kind': 'FALLTHROUGH'}
151151

152152

153+
# ---------------------------------------------------------------------------
154+
# Evaluation before the client counts as initialized
155+
# ---------------------------------------------------------------------------
156+
157+
CACHED_DATA_WARNING = "Feature Flag evaluation attempted before client has initialized - using last known values"
158+
159+
160+
def cached_data_warnings(caplog) -> List[str]:
161+
return [r.getMessage() for r in caplog.records if r.getMessage().startswith(CACHED_DATA_WARNING)]
162+
163+
164+
def test_v1_evaluation_does_not_log_the_cached_data_warning(tmp_path, caplog):
165+
path = os.path.join(str(tmp_path), 'data.json')
166+
write_file(path, DOCUMENT)
167+
config = Config('SDK_KEY', update_processor_class=Files.new_data_source(paths=[path]), send_events=False)
168+
with caplog.at_level(logging.WARNING), LDClient(config) as client:
169+
assert client.variation('flag2', user, 'default') == 'value2'
170+
assert cached_data_warnings(caplog) == []
171+
172+
173+
@pytest.mark.parametrize('use_as', ['initializer', 'synchronizer', 'both'])
174+
def test_v2_evaluation_logs_the_cached_data_warning_once(tmp_path, caplog, use_as):
175+
# File data has no selector, so the data system reports it as cached, not refreshed.
176+
# The first evaluation then logs the cached data warning, although the value is correct.
177+
path = os.path.join(str(tmp_path), 'data.json')
178+
write_file(path, DOCUMENT)
179+
file_source = Files.new_data_source_v2(paths=[path])
180+
builder = custom()
181+
if use_as in ('initializer', 'both'):
182+
builder = builder.initializers([file_source])
183+
if use_as in ('synchronizer', 'both'):
184+
builder = builder.synchronizers(file_source)
185+
config = Config('SDK_KEY', datasystem_config=builder.build(), send_events=False)
186+
with caplog.at_level(logging.WARNING), LDClient(config) as client:
187+
assert client.variation('flag2', user, 'default') == 'value2'
188+
assert client.variation('flag1', user, 'default') == 'on'
189+
assert len(cached_data_warnings(caplog)) == 1
190+
191+
153192
# ---------------------------------------------------------------------------
154193
# Version fallback
155194
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)