Skip to content

Commit 734e50a

Browse files
committed
Use our normal config setting style for the pickled metadata warning
1 parent 496809e commit 734e50a

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

docs/mkdocs/docs/tutorials/metadata.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Pickling can have [serious downsides](https://nedbatchelder.com/blog/202006/pick
5454
unpickle data written with one set of library versions from a client with a different set of library versions.
5555

5656
Because of this, we log a warning when metadata gets pickled. You can disable the warning by setting an environment
57-
variable `PICKLED_METADATA_LOGLEVEL_DEBUG` to any value. The log message looks like:
57+
variable `ARCTICDB_PickledMetadata_loglevel_str` to `DEBUG`. The log message looks like:
5858

5959
```
6060
Pickling metadata - may not be readable by other clients

python/arcticdb/version_store/_normalization.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pickle
2020
from abc import ABCMeta, abstractmethod
2121

22+
from arcticdb_ext import get_config_string
2223
from pandas.api.types import is_integer_dtype
2324
from arcticc.pb2.descriptors_pb2 import UserDefinedMetadata, NormalizationMetadata, MsgPackSerialization
2425
from arcticc.pb2.storage_pb2 import VersionStoreConfig
@@ -79,7 +80,26 @@ def check_is_utc_if_newer_pandas(*args, **kwargs):
7980
NormalizedInput = NamedTuple("NormalizedInput", [("item", NPDDataFrame), ("metadata", NormalizationMetadata)])
8081

8182

82-
PICKLED_METADATA_LOGLEVEL = LogLevel.DEBUG if os.getenv("PICKLED_METADATA_LOGLEVEL_DEBUG") else LogLevel.WARN
83+
_PICKLED_METADATA_LOGLEVEL = None # set lazily with function below
84+
85+
86+
def get_pickled_metadata_loglevel():
87+
global _PICKLED_METADATA_LOGLEVEL
88+
if _PICKLED_METADATA_LOGLEVEL:
89+
return _PICKLED_METADATA_LOGLEVEL
90+
91+
log_level = get_config_string("PickledMetadata.LogLevel")
92+
expected_settings = ("DEBUG", "INFO", "WARN", "ERROR")
93+
if log_level:
94+
if log_level.upper() not in expected_settings:
95+
log.warn(f"Expected PickledMetadata.LogLevel setting to be in {expected_settings} or absent but was {log_level}")
96+
_PICKLED_METADATA_LOGLEVEL = LogLevel.WARN
97+
else:
98+
_PICKLED_METADATA_LOGLEVEL = getattr(LogLevel, log_level.upper())
99+
else:
100+
_PICKLED_METADATA_LOGLEVEL = LogLevel.WARN
101+
102+
return _PICKLED_METADATA_LOGLEVEL
83103

84104

85105
# To simplify unit testing of serialization logic. This maps the cpp _FrameData exposed object
@@ -1068,7 +1088,7 @@ def read(data, pickled_in_python2=False):
10681088

10691089
@staticmethod
10701090
def write(obj):
1071-
log.log(PICKLED_METADATA_LOGLEVEL, f"Pickling metadata - may not be readable by other clients")
1091+
log.log(get_pickled_metadata_loglevel(), f"Pickling metadata - may not be readable by other clients")
10721092
return pickle.dumps(obj, protocol=PICKLE_PROTOCOL)
10731093

10741094

python/tests/integration/arcticdb/version_store/test_metadata_support.py

+31
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
import numpy as np
99
import pandas as pd
10+
from arcticdb_ext import set_config_string, unset_config_string
1011
from pandas import DataFrame, Timestamp
1112
import pytest
1213

@@ -32,6 +33,36 @@ def test_rt_df_with_small_meta(object_and_mem_and_lmdb_version_store):
3233
assert meta == vit.metadata
3334

3435

36+
@pytest.mark.parametrize("log_level", ("error", "warn", "debug", "info", "ERROR", "eRror", "", None))
37+
def test_pickled_metadata_warning(lmdb_version_store_v1, log_level):
38+
import arcticdb.version_store._normalization as norm
39+
norm._PICKLED_METADATA_LOGLEVEL = None
40+
if log_level is not None:
41+
set_config_string("PickledMetadata.LogLevel", log_level)
42+
lib = lmdb_version_store_v1
43+
df = DataFrame(data=["A", "B", "C"])
44+
meta = df
45+
lib.write("pandas", df, metadata=meta)
46+
vit = lib.read("pandas")
47+
assert_frame_equal(df, vit.data)
48+
assert_frame_equal(df, vit.metadata)
49+
unset_config_string("PickledMetadata.LogLevel")
50+
51+
52+
def test_pickled_metadata_warning_bad_config(lmdb_version_store_v1):
53+
"""Don't block writes just because they set this wrong."""
54+
import arcticdb.version_store._normalization as norm
55+
norm._PICKLED_METADATA_LOGLEVEL = None
56+
set_config_string("PickledMetadata.LogLevel", "cat")
57+
lib = lmdb_version_store_v1
58+
df = DataFrame(data=["A", "B", "C"])
59+
meta = df
60+
lib.write("pandas", df, metadata=meta)
61+
vit = lib.read("pandas")
62+
assert_frame_equal(df, vit.data)
63+
assert_frame_equal(df, vit.metadata)
64+
65+
3566
def test_rt_df_with_humonguous_meta(object_and_mem_and_lmdb_version_store):
3667
with pytest.raises(ArcticDbNotYetImplemented):
3768
from arcticdb.version_store._normalization import _MAX_USER_DEFINED_META as MAX

0 commit comments

Comments
 (0)