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

Database grows unexpectedly using EFCore adapter #176

Closed
thoraj opened this issue Jun 22, 2021 · 6 comments
Closed

Database grows unexpectedly using EFCore adapter #176

thoraj opened this issue Jun 22, 2021 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@thoraj
Copy link

thoraj commented Jun 22, 2021

In our solution we're noticing that the Casbin table grows "unexpectedly". Before I start producing a simpler repro I thought I should check if my expectations make sense.

I would expect there not to be any duplicate rows in the table, and that the rows correspond to individual AddXXC calls. But when we're adding entries using either enforcer.AddNamedPolicyAsync() or enforcer.AddRoleForUserAsync() we're seeing that a single call to enforcer.AddXXX() produces duplicate (rows that differ only in the PK).

image

Is this to be expected?

Not sure if it's relevant but we're using a custom CasbinDbContext, and a Postgres database with the adapter.

@hsluoyz
Copy link
Member

hsluoyz commented Jun 23, 2021

@sagilio

@hsluoyz hsluoyz self-assigned this Jun 23, 2021
@hsluoyz hsluoyz added the bug Something isn't working label Jun 23, 2021
@sagilio
Copy link
Member

sagilio commented Jun 23, 2021

It may be a bug, casbin should keep no duplicate rules in the model.
I will check about it and wait for the simpler repo.

@thoraj
Copy link
Author

thoraj commented Jun 23, 2021

While working on the repro I discovered what the issue is/was. I was calling enforcer.SavePolicyAsync() while AutoSave was enabled.

So the issue was really a couple of things:

  1. Assumption that save SavePolicy() would make the database correspond to the rules in the enforcer. Instead it seems to simply create a new row for each rule in the enforcer, which leads to duplicates.
  2. AutoSave is enabled by default.

Not sure if item 1. is by design? If so it makes for a very crude mechanism to it hard to keep the database in sync with the enforcer. Also it means that calling SavePolicy with AutoSave enabled is always (?) a mistake.

So perhaps this is not considered a bug, and the ticket can be closed, but I would like your comment on:

  • Is the SavePolicy() supposed to avoid creating duplicates, or does it work as intended?
  • Are there plans to improve the api/interface to allow more flexibility in how the backend database is kept in sync with the enforcer?

@sagilio
Copy link
Member

sagilio commented Jun 23, 2021

Thank you for the feedback.

  • I think we should not add duplicates to model and storage (like DB or others), and we also need to ensure the idempotence of repeated operations.
  • I plan to improve the IAdapter on v2.x, I hope it can use a more simple way to test whether the need to sync the policy and provide more flexibility on sync workflow. Do you have any suggestions?

@sagilio sagilio added question Further information is requested and removed bug Something isn't working labels Jun 23, 2021
@thoraj
Copy link
Author

thoraj commented Jun 24, 2021

Thanks @sagilio
IMHO I think getting duplicates on enforcer.SavePolicy() needs to be fixed. Otherwise the documented usage will always produce duplicates. I think the only scenario which will not produce duplicates is the case where the database is empty to start with.

So in the short term I think adding "de-duplication' on SavePolicy() is necessary and will make SavePolicy() behave closer to the expected . (How does it behave in other implementations on other platforms?)

As for other improvement for IAdapter v2.x, I don't have anything concrete right now, but I believe the lack of rule identity may cause some friction. In the next few days we will be implementing a couple of synchronization scenarios, and I will let you know if we discover any "friction" implementing those. Perhaps we will discover if/how IAdapter can be improved?

@sagilio
Copy link
Member

sagilio commented Jun 28, 2021

Look forward to your discoveries and welcome to comment at #184 anytime.

@sagilio sagilio closed this as completed Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants