Skip to content

feat: add a business logic layer#403

Merged
chrisburr merged 2 commits intoDIRACGrid:mainfrom
aldbr:main_FEAT_add-new-bl-layer
Mar 6, 2025
Merged

feat: add a business logic layer#403
chrisburr merged 2 commits intoDIRACGrid:mainfrom
aldbr:main_FEAT_add-new-bl-layer

Conversation

@aldbr
Copy link
Copy Markdown
Contributor

@aldbr aldbr commented Feb 25, 2025

Details explained in #348

  • Split diracx-db into 2 sub-directory: business and data_access.

  • Update: create a diracx-logic layer

    • diracx-routers becomes the presentation layer: manages user authN/Z, calls the business logic layer and converts the returned results/raised exceptions into HTTP-friendly responses for the end users.
    • diracx-logic: deals with the business logic by performing operations and calling the data access layer to get additional data and persist them.
    • diracx-db: abstracts sql/no sql complexities.
    • auth: adapt diracx-routers and move business logic to diracx-logic.
    • jobs: adapt diracx-routers and move business logic to diracx-logic.
      • remove DIRAC from diracx-db.jobs.db
      • add type hinting as much as possible + docstrings
    • make diracx-db more simple (atomic operations + business logic moved to diracx-logic)
    • extensions
  • Enforce consistency using import-linter

  • Update: enforce consistency by adding the right dependencies in the right package:

    • diracx-routers depends on fastapi, not dirac nor sqlalchemy/opensearchpy.
    • diracx-logic depends on dirac, not fastapi nor sqlalchemy/opensearchpy.
    • diracx-db depends on sqlalchemy/opensearchpy, not dirac nor fastapi.
    • There is one limitation though: we can't easily programmatically prevent a developer from using dbs from diracx.routers because they are injected through diracx.routers.dependencies, so we would need to be careful when reviewing the code.
  • documentation about the 3-layer architecture within diracx

@aldbr aldbr linked an issue Feb 25, 2025 that may be closed by this pull request
@aldbr aldbr force-pushed the main_FEAT_add-new-bl-layer branch 4 times, most recently from add09a4 to 168a6bc Compare February 27, 2025 09:17
@chaen
Copy link
Copy Markdown
Contributor

chaen commented Feb 27, 2025

I thought we had agreed not to do it in general, and only on adhoc cases ?

@aldbr
Copy link
Copy Markdown
Contributor Author

aldbr commented Feb 27, 2025

Oops, I thought we said we would add a business logic layer within diracx.db: #348 (comment)

I thought it would be better to be consistent and have all the business logic at the same place to have a single responsibility principle between the layers + follow a standard architectural pattern.

@aldbr aldbr force-pushed the main_FEAT_add-new-bl-layer branch from 3a39077 to 3006c3b Compare February 27, 2025 12:57
@aldbr aldbr force-pushed the main_FEAT_add-new-bl-layer branch from af55ea2 to b03406b Compare March 4, 2025 14:10
@aldbr aldbr marked this pull request as ready for review March 4, 2025 14:33
@aldbr
Copy link
Copy Markdown
Contributor Author

aldbr commented Mar 4, 2025

  • First of all, sorry this PR is actually massive and very hard to review.

  • Deployment/docker and Integration Tests are not passing, I think they would pass once feat: add diracx-logic in services-base container-images#31 will be merged (should be safe to merge IIUC).

  • I remove some methods that were unused in diracx-db (mostly because they had a bulk version, which should be preferred)

  • I remove DevelopmentSettings from the /dirac-metadata route (I had a circular dependency issue, which brings me to question why it was here and I haven't found any reason). If you think it's important, then I can revert that change.

@aldbr aldbr requested review from chaen and chrisburr March 4, 2025 14:38
@aldbr
Copy link
Copy Markdown
Contributor Author

aldbr commented Mar 4, 2025

  • Deployment/docker is passing because services-base includes logic
  • Integration Tests are still not passing because services has not been updated yet (would require to merge another PR to trigger the deployment of a new dev tag)

@chaen
Copy link
Copy Markdown
Contributor

chaen commented Mar 4, 2025

You can create the dev tag yourself by triggering the deployment :-) Even if it creates a new real tag, it does not really matter... :-)

@aldbr
Copy link
Copy Markdown
Contributor Author

aldbr commented Mar 5, 2025

Alright, all is 🟢 now

Copy link
Copy Markdown
Contributor

@fstagni fstagni left a comment

Choose a reason for hiding this comment

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

It is close to impossible to properly review this PR, so these are just few comments.

For a PR that mostly should be a lot of "move around" of code, I am surprised that there are more than 700 lines of code added

Comment on lines +137 to +139
# TODO: this should probably be something mandatory
# to set by the user
token_issuer: str = "http://lhcbdirac.cern.ch/" # noqa: S105
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this something used (e.g. by the tests) or something here as an example?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's used, let's try to remove it and see if it breaks

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know you did not start this file, anyway: what is the rationale for having all (?) models in it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good question actually, I think we should agree on the purpose of this file and it would deserve a docstring.
I added all the models that were (i) passed as inputs to the routers; (ii) returned as outputs by the routers, so the ones that could be shared between the clients (cli, api) and the services (routers, logic, db).

At some point we should probably make a models directory with multiple modules such as jobs, auth...

self, owner_id: int, se_name: str, pfn: str, size: int
) -> None:
"""Add a new sandbox in SandboxMetadataDB."""
# TODO: Follow https://github.com/DIRACGrid/diracx/issues/49
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The linked issue is not solved. It does not apply here anymore?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The linked issue is not solved:

  • I break down the method into smaller methods in diracx-db to move the logic from it to diracx-logic.
  • The comment was moved to diracx-logic because it makes more sense to read it from here now.

Comment on lines -61 to -65
# We shouldn't be able to retrieve it twice
async with auth_db as auth_db:
with pytest.raises(AuthorizationError, match="already used"):
res = await auth_db.get_authorization_flow(code, MAX_VALIDITY)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why remove this test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because:

  • I break down get_authorization_flow into smaller methods to move the logic from diracx-db to diracx-logic
  • The new get_authorization_flow is now very simple and does not require complex testing as it was the case before: complex testing should now be done in diracx-routers and it's already done.

@aldbr aldbr force-pushed the main_FEAT_add-new-bl-layer branch 2 times, most recently from f92d3f7 to d2c33ac Compare March 6, 2025 07:33
@aldbr aldbr force-pushed the main_FEAT_add-new-bl-layer branch from d2c33ac to 30bf8ff Compare March 6, 2025 07:55
@chrisburr chrisburr merged commit 36dc88a into DIRACGrid:main Mar 6, 2025
26 checks passed
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.

Moving towards a 3-tier layer architecture?

4 participants