Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(refactor): load configuration and merge recursively #15405

Merged
merged 6 commits into from Jun 28, 2021

Conversation

ofekisr
Copy link
Contributor

@ofekisr ofekisr commented Jun 27, 2021

SUMMARY

  • support recursive merge by using pydash
  • move load logic from config to app
  • change from depracted imp module to importlib

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@ofekisr ofekisr changed the title refactor load configuration [wip] refactor load configuration Jun 27, 2021
@amitmiran137 amitmiran137 changed the title [wip] refactor load configuration chore(refactor): load configuration and merge recursively Jun 27, 2021
@codecov
Copy link

codecov bot commented Jun 27, 2021

Codecov Report

Merging #15405 (57f80e3) into master (a3d91ae) will decrease coverage by 0.21%.
The diff coverage is 64.28%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #15405      +/-   ##
==========================================
- Coverage   77.23%   77.02%   -0.22%     
==========================================
  Files         975      975              
  Lines       50615    50630      +15     
  Branches     6212     6212              
==========================================
- Hits        39094    38996      -98     
- Misses      11314    11427     +113     
  Partials      207      207              
Flag Coverage Δ
hive ?
mysql 81.73% <64.28%> (+0.02%) ⬆️
postgres 81.75% <64.28%> (+0.02%) ⬆️
presto ?
python 81.83% <64.28%> (-0.42%) ⬇️
sqlite 81.37% <64.28%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
superset/app.py 68.42% <63.41%> (-14.92%) ⬇️
superset/config.py 97.03% <100.00%> (+5.88%) ⬆️
superset/db_engines/hive.py 0.00% <0.00%> (-82.15%) ⬇️
superset/db_engine_specs/hive.py 69.20% <0.00%> (-17.21%) ⬇️
superset/db_engine_specs/presto.py 83.36% <0.00%> (-6.53%) ⬇️
superset/views/database/mixins.py 81.03% <0.00%> (-1.73%) ⬇️
superset/connectors/sqla/models.py 88.22% <0.00%> (-1.65%) ⬇️
superset/db_engine_specs/base.py 87.97% <0.00%> (-0.41%) ⬇️
superset/models/core.py 89.76% <0.00%> (-0.27%) ⬇️
superset/utils/core.py 88.97% <0.00%> (-0.13%) ⬇️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a3d91ae...57f80e3. Read the comment docs.

Copy link
Member

@amitmiran137 amitmiran137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
this will support a reacher config structure in #15296

@amitmiran137 amitmiran137 merged commit d8a1acf into apache:master Jun 28, 2021
@amitmiran137 amitmiran137 deleted the refactor/init_configurations branch June 28, 2021 10:53
mvoitko pushed a commit to mvoitko/superset that referenced this pull request Jun 28, 2021
* refactor load configuration

* refactor init configuration to enable recursive merge

* Update config.py

* Update app.py

* fix lint and black issue
@betodealmeida
Copy link
Member

@ofekisr currently my superset_config.py has this line:

RESULTS_BACKEND = RedisCache(host="localhost", port=6379, key_prefix="superset_results")

And it failes to run because pydash tries to make a deepcopy of it:

Traceback (most recent call last):
  File "/Users/beto/.pyenv/versions/superset/bin/superset", line 33, in <module>
    sys.exit(load_entry_point('apache-superset', 'console_scripts', 'superset')())
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/click/core.py", line 1256, in invoke
    Command.invoke(self, ctx)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/flask/cli.py", line 425, in decorator
    with __ctx.ensure_object(ScriptInfo).load_app().app_context():
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/flask/cli.py", line 381, in load_app
    app = call_factory(self, self.create_app)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/flask/cli.py", line 119, in call_factory
    return app_factory()
  File "/Users/beto/Projects/incubator-superset/superset/app.py", line 52, in create_app
    raise ex
  File "/Users/beto/Projects/incubator-superset/superset/app.py", line 41, in create_app
    config = init_config()
  File "/Users/beto/Projects/incubator-superset/superset/app.py", line 62, in init_config
    return merge(config, override_conf)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/pydash-5.0.1-py3.8.egg/pydash/objects.py", line 859, in merge
    return merge_with(obj, *sources)
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/pydash-5.0.1-py3.8.egg/pydash/objects.py", line 907, in merge_with
    sources = [copy.deepcopy(source) for source in sources]
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/pydash-5.0.1-py3.8.egg/pydash/objects.py", line 907, in <listcomp>
    sources = [copy.deepcopy(source) for source in sources]
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 172, in deepcopy
    y = _reconstruct(x, memo, *rv)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 270, in _reconstruct
    state = deepcopy(state, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 146, in deepcopy
    y = copier(x, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/Users/beto/.pyenv/versions/3.8.8/lib/python3.8/copy.py", line 161, in deepcopy
    rv = reductor(4)
TypeError: cannot pickle '_thread.lock' object
Exception ignored in: <function Redis.__del__ at 0x1109a7790>
Traceback (most recent call last):
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/redis/client.py", line 885, in __del__
    self.close()
  File "/Users/beto/.pyenv/versions/3.8.8/envs/superset/lib/python3.8/site-packages/redis/client.py", line 888, in close
    conn = self.connection
AttributeError: 'Redis' object has no attribute 'connection'

def init_config() -> Dict[Any, Any]:
config = convert_to_dict(load_default_config())
override_conf = convert_to_dict(load_override_config())
return merge(config, override_conf)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think bringing a new dependency (pydash) just to merge two dicts here is overkill. Why not just use dict.update?

I'm also not sure if a recursive merge is the right strategy here. If we have:

# superset/config.py
a = {"b": "c"}

And I create a custom config:

# superset_config.py
a = {"d": "e"}

This would result in the following configuration:

{'a': {'b': 'c', 'd': 'e'}}

Which is probably not what we want.

As a concrete example, we currently have languages configured in superset/config.py:

LANGUAGES = {
    "en": {"flag": "us", "name": "English"},
    "es": {"flag": "es", "name": "Spanish"},
    "it": {"flag": "it", "name": "Italian"},
    "fr": {"flag": "fr", "name": "French"},
    "zh": {"flag": "cn", "name": "Chinese"},
    "ja": {"flag": "jp", "name": "Japanese"},
    "de": {"flag": "de", "name": "German"},
    "pt": {"flag": "pt", "name": "Portuguese"},
    "pt_BR": {"flag": "br", "name": "Brazilian Portuguese"},
    "ru": {"flag": "ru", "name": "Russian"},
    "ko": {"flag": "kr", "name": "Korean"},
    "sl": {"flag": "si", "name": "Slovenian"},
}

If I want to have just Portuguese in my Superset instance I would add to my config:

LANGUAGES = {
    "pt_BR": {"flag": "br", "name": "Brazilian Portuguese"},
}

But the merge would bring back all languages.

@betodealmeida betodealmeida mentioned this pull request Jun 28, 2021
8 tasks
@villebro villebro mentioned this pull request Jun 29, 2021
8 tasks
@john-bodley
Copy link
Member

john-bodley commented Jul 6, 2021

@ofekisr @amitmiran137 @betodealmeida @villebro I'm all for standardizing the configuration import logic but why don't we simply use the recommended Flask pattern , i.e.,

app = Flask(__name__)
app.config.from_object("superset.config")
app.config.from_envvar("SUPERSET_CONFIG_PATH", silent=True)

instead of writing our own import/merge logic?

@john-bodley
Copy link
Member

john-bodley commented Jul 6, 2021

@ofekisr @amitmiran137 this updated logic breaks existing config functionality, specifically where callable configuration references other configuration variables:

i.e., locally overriding the CSV_TO_HIVE_UPLOAD_DIRECTORY has no effect.

Note I'm supportive of the general direction of this PR, especially given the previous logic is atypical from a Flask perspective, however the regression needs to be addressed by either i) amending the logic or ii) reverting this PR (alongside @betodealmeida's and @villebro's fixes).

betodealmeida added a commit to betodealmeida/incubator-superset that referenced this pull request Jul 6, 2021
@betodealmeida
Copy link
Member

@john-bodley I have PR reverting the changes: #15558

I think we need to evaluate this better, since we already tried to fix it twice and it's still breaking our deployments.

betodealmeida added a commit that referenced this pull request Jul 6, 2021
graceguo-supercat pushed a commit to airbnb/superset-fork that referenced this pull request Jul 6, 2021
cccs-RyanS pushed a commit to CybercentreCanada/superset that referenced this pull request Dec 17, 2021
* refactor load configuration

* refactor init configuration to enable recursive merge

* Update config.py

* Update app.py

* fix lint and black issue
cccs-RyanS pushed a commit to CybercentreCanada/superset that referenced this pull request Dec 17, 2021
QAlexBall pushed a commit to QAlexBall/superset that referenced this pull request Dec 29, 2021
* refactor load configuration

* refactor init configuration to enable recursive merge

* Update config.py

* Update app.py

* fix lint and black issue
QAlexBall pushed a commit to QAlexBall/superset that referenced this pull request Dec 29, 2021
cccs-rc pushed a commit to CybercentreCanada/superset that referenced this pull request Mar 6, 2024
* refactor load configuration

* refactor init configuration to enable recursive merge

* Update config.py

* Update app.py

* fix lint and black issue
cccs-rc pushed a commit to CybercentreCanada/superset that referenced this pull request Mar 6, 2024
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 1.3.0 labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/M 🚢 1.3.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants