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

Add structured config compilation #8098

Merged
merged 6 commits into from
Dec 13, 2024
Merged

Add structured config compilation #8098

merged 6 commits into from
Dec 13, 2024

Conversation

fantix
Copy link
Member

@fantix fantix commented Dec 10, 2024

Structured config values are Python objects from environment variables or (future) TOML config files. The compilation re- assembles the objects into ConfigOp ASTs and uses the static evaluation mechanism to generate verified config values.

This supersedes #8059 as a less-hacky way to validate config values.

As a side-effect, such config sources now support specifying EdgeQL values directly in a double-braces, for example:

env GEL_SERVER_CONFIG_cfg::session_idle_timeout="{{<duration>'8 minutes 32 seconds'}}"

Or even simpler:

env GEL_SERVER_CONFIG_cfg::session_idle_timeout='8 minutes 32 seconds'

(because the typecast is included by default)

This allows setting nested config objects with INSERT statements too:

env GEL_SERVER_CONFIG_cfg::email_providers="{{ { (insert cfg::SMTPProviderConfig { name := 'gmail'}) } }}"

Sample:

ops = compile_structured_config(
    {
        "cfg::Config": {
            "singleprop": "value",
            "memprop": 512,
            "durprop": "16s",
            "enumprop": "One",
            "multiprop": ["v1", "v2", "v3"],
            "listen_port": 5,
            "email_providers": [
                {"_tname": "cfg::SMTPProviderConfig", "name": "gmail"},
                {"_tname": "cfg::SMTPProviderConfig", "name": "outlook"},
            ],
            "sysobj": [
                {
                    "name": "1",
                    "obj": {
                        "_tname": "cfg::Subclass1",
                        "name": "aa",
                        "sub1": "aa",
                    },
                },
                {
                    "name": "2",
                    "_tname": "cfg::TestInstanceConfigStatTypes",
                    "memprop": 128,
                },
            ],
        },
    },
    spec=spec,
    schema=stdlib.stdschema,
    source="config file",
)
print(ops)
{
 'cfg::Config': immutables.Map({
  'durprop': SettingValue(name='durprop', value=<statypes.Duration 'PT16S'>, source='config file', scope=<ConfigScope.INSTANCE: 'INSTANCE'>, secret=False),
  'email_providers': SettingValue(name='email_providers', value=frozenset({cfg::SMTPProviderConfig(timeout_per_email=<statypes.Duration 'PT1M'>, security='STARTTLSOrPlainText', sender=None, name='outlook', port=None, timeout_per_attempt=<statypes.Duration 'PT15S'>, validate_certs=True, password=None, host=None, username=None), cfg::SMTPProviderConfig(timeout_per_email=<statypes.Duration 'PT1M'>, security='STARTTLSOrPlainText', sender=None, name='gmail', port=None, timeout_per_attempt=<statypes.Duration 'PT15S'>, validate_certs=True, password=None, host=None, username=None)}), source='config file', scope=<ConfigScope.INSTANCE: 'INSTANCE'>, secret=False),
  'enumprop': SettingValue(name='enumprop', value='One', source='config file', scope=<ConfigScope.INSTANCE: 'INSTANCE'>, secret=False),
  'listen_port': SettingValue(name='listen_port', value=5, source='config file', scope=<ConfigScope.INSTANCE: 'INSTANCE'>, secret=False),
  'memprop': SettingValue(name='memprop', value=<statypes.ConfigMemory '512B'>, source='config file', scope=<ConfigScope.INSTANCE: 'INSTANCE'>, secret=False),
  'multiprop': SettingValue(name='multiprop', value=frozenset({'v3'}), source='config file', scope=<ConfigScope.INSTANCE: 'INSTANCE'>, secret=False),
  'singleprop': SettingValue(name='singleprop', value='value', source='config file', scope=<ConfigScope.INSTANCE: 'INSTANCE'>, secret=False),
  'sysobj': SettingValue(name='sysobj', value=frozenset({cfg::TestInstanceConfigStatTypes(obj=None, name='2', durprop=None, memprop=<statypes.ConfigMemory '128B'>), cfg::TestInstanceConfig(obj=cfg::Subclass1(sub1='aa', name='aa'), name='1')}), source='config file', scope=<ConfigScope.INSTANCE: 'INSTANCE'>, secret=False)
 })
}

@fantix fantix force-pushed the compile-structured-config branch 2 times, most recently from d8b875e to 3eff4c4 Compare December 12, 2024 16:53
@fantix fantix changed the base branch from master to bool-staeval December 12, 2024 16:53
Base automatically changed from bool-staeval to master December 12, 2024 17:25
Structured config values are Python objects from environment
variables or (future) TOML config files. The compilation re-
assembles the objects into ConfigOp ASTs and use the static
evaluation mechanism to generate verified config values.

As a side-effect, such config sources now support specifying
EdgeQL values directly in a double-braces, for example:

    env GEL_SERVER_CONFIG_cfg::session_idle_timeout \
        = "{{<duration>'5s' * 2}}"

Technically, this allows setting nested config objects with
INSERT statements too.
@fantix fantix force-pushed the compile-structured-config branch from 3eff4c4 to 12d3a25 Compare December 12, 2024 18:24
@fantix fantix marked this pull request as ready for review December 12, 2024 19:01
@fantix fantix changed the title [WIP] Add structured config compilation Add structured config compilation Dec 12, 2024
@fantix fantix requested review from msullivan and elprans December 12, 2024 19:01
@elprans
Copy link
Member

elprans commented Dec 12, 2024

Technically, this allows setting nested config objects with INSERT statements too.

Can you give an example of how that might look?

@fantix
Copy link
Member Author

fantix commented Dec 12, 2024

This works:

env GEL_SERVER_CONFIG_cfg::email_providers="{{ { (insert cfg::SMTPProviderConfig { name := 'gmail'}) } }}" edb server

It's compiled into such AST:

configure INSTANCE insert cfg::SMTPProviderConfig{ name := 'gmail' };

Then it's statically evaluated into:

immutables.Map(
  {'email_providers': SettingValue(
    name='email_providers',
    value=frozenset(
      {cfg::SMTPProviderConfig(
        host=None,
        sender=None,
        port=None,
        timeout_per_attempt=<statypes.Duration 'PT15S'>,
        password=None,
        name='gmail',
        validate_certs=True,
        username=None,
        timeout_per_email=<statypes.Duration 'PT1M'>,
        security='STARTTLSOrPlainText'
      )}
    ),
    source='environment variable', scope=<ConfigScope.INSTANCE: 'INSTANCE'>,
    secret=False)
  }
)

Copy link
Member

@msullivan msullivan left a comment

Choose a reason for hiding this comment

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

Looks good broadly, but I have some concerns we need to figure out about the config object situtation

) -> dict[str, immutables.Map[str, config.SettingValue]]:
# XXX: only config in the stdlib is supported currently, so the only
# key allowed in objects is "cfg::Config". API for future compatibility
if list(objects) != ["cfg::Config"]:
Copy link
Member

Choose a reason for hiding this comment

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

Should it arguably be cfg::AbstractConfig?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes? Though, it's a bit weird for the user to write [cfg::AbstractConfig] in the TOML config file.

Fwiw using cfg::Config here is following this line which was added before we even had cfg::AbstractConfig and never got updated.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, ok

Comment on lines 87 to 103
def _test_compile_structured_config(
self,
values: dict[str, Any],
*,
source: str = "config file",
**expected: Any,
) -> dict[str, config.SettingValue]:
result = self.compiler.compile_structured_config(
{"cfg::Config": values}, source=source
)
rv = dict(result["cfg::Config"])
for name, setting in rv.items():
self.assertEqual(setting.name, name)
self.assertEqual(setting.scope, config.ConfigScope.INSTANCE)
self.assertEqual(setting.source, source)
self.assertDictEqual({k: v.value for k, v in rv.items()}, expected)
return rv
Copy link
Member

Choose a reason for hiding this comment

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

Could we get an end-to-end test, also?

One thing I am concerned about is how INSERTed objects specified via structured config will interact with objects inserted manually via CONFIGURE INSTANCE INSERT ....

The best option is for the sets of objects to get merged. I spent about a day trying to make that work, and came to the conclusion that it would probably take me closer to a week, and would be pretty hairy. This is why I introduced EDGEDB_MAGIC_SMTP_CONFIG, so we wouldn't need to handle it.

If manually inserted objects just fully override, that is probably acceptable behavior (though it wouldn't work for our SMTP config case, so we would still need EDGEDB_MAGIC_SMTP_CONFIG), but I think it will take a little care to make that work properly also. (If you INSERT an object and then RESET it away, probably the structured config one should come back? But I think as implemented it won't quite work.)

Anyway, I don't think this really blocks us, but I think we might need to disable object config for now, and stick with EDGEDB_MAGIC_SMTP_CONFIG for the case we need. (We should probably use the structured parser for EDGEDB_MAGIC_SMTP_CONFIG though!)
I think getting objects to work well enough to cover that case for us will be a decent amount of extra work, and not a priority yet.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I also left 2 similar questions in the previous PR

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a switch to turn configure ... insert off for now. End-to-end tests for non-insert configs are covered by the env var static config tests now; the upcoming TOML config file PR will include more e2e tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

We should probably use the structured parser for EDGEDB_MAGIC_SMTP_CONFIG

Fixed!

@fantix fantix requested a review from msullivan December 13, 2024 17:38
tenant=tenant,
use_monitor_fs=args.reload_config_files in [
srvargs.ReloadTrigger.Default,
srvargs.ReloadTrigger.FileSystemEvent,
],
net_worker_mode=args.net_worker_mode,
)
magic_smtp = os.getenv('EDGEDB_MAGIC_SMTP_CONFIG')
Copy link
Member

Choose a reason for hiding this comment

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

Did you do a manual test of this, since apparently I half-assed it and didn't write one

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, manually tested ✅

@fantix fantix merged commit e80225e into master Dec 13, 2024
23 checks passed
@fantix fantix deleted the compile-structured-config branch December 13, 2024 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants