Skip to content

multi tenancy aware sequences#2305

Open
ThomasFOC wants to merge 9 commits into
ebean-orm:masterfrom
FOCONIS:feature/multi-tenancy-aware-sequences
Open

multi tenancy aware sequences#2305
ThomasFOC wants to merge 9 commits into
ebean-orm:masterfrom
FOCONIS:feature/multi-tenancy-aware-sequences

Conversation

@ThomasFOC

Copy link
Copy Markdown
Contributor

Hey Rob,
the TestSequenceMultiTenant shows the problem with multi tenancy. To address this problem we implemented a delegator in the BeanDescriptorManager.

There is an older attempt of @rPraml where he uses TenantDataSourceProvider and CurrentTenantProvider and extends them to the SequenceIdGenerator:
FOCONIS@2de1f21

Do you have any other suggestions or is this simple attempt sufficient?

This test case generates following stack:
```java
java.lang.NullPointerException
 at io.ebean.config.dbplatform.SequenceIdGenerator.getMoreIds(SequenceIdGenerator.java:158)
 at io.ebean.config.dbplatform.SequenceIdGenerator.loadMore(SequenceIdGenerator.java:117)
 at io.ebean.config.dbplatform.SequenceIdGenerator.nextId(SequenceIdGenerator.java:100)
 at io.ebeaninternal.server.deploy.BeanDescriptor.nextId(BeanDescriptor.java:1541)
 at io.ebeaninternal.server.core.DefaultServer.nextId(DefaultServer.java:851)
 at org.tests.idkeys.TestSequenceMultiTenant.test_multi_tenant_sequences(TestSequenceMultiTenant.java:35)
...
```
…erator instead of DataSource

To support multi tenancy for sequences a delegator is implemented in the BeanDescriptorManager
}

private PlatformIdGenerator get() {
return map.computeIfAbsent(dataSourceSupplier.getDataSource(), k -> create());

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.

FI: This works good for a DS-supplier that supplies different dataSources for different tenants (As it is done in TenantMode.DB/DB_WITH_MASTER - which is our use case @FOCONIS) - for other tenant modes like SCHEMA or CATALOG it may cause other problems. Maybe in a schema/catalog environment this should somehow configurable, where the Sequence lives.

@rbygrave

Copy link
Copy Markdown
Member

Hmmm, my initial thought is that this looks problematic.

@ThomasFOC

Copy link
Copy Markdown
Contributor Author

Hmmm, my initial thought is that this looks problematic.

Hello @rbygrave ,
we tried to provide a test case for sequences in TenantMode.SCHEMA:
FOCONIS@109958a

We have tried to generate the schemas, when there is a UserContext-switch and provide the new schema.

An exception is raised in io.ebean.config.dbplatform.SequenceIdGenerator.getMoreIds(int) in line 158. To be more precise the exception comes from io.ebeaninternal.server.core.MultiTenantDbSchemaSupplier.SchemaDataSource.getConnection() where the schema is set for the connection.

This seems correct for us and may address the issue @rPraml sees.

But to dive deeper into this topic the test has to run over the problem that no schema seems to be generated.

After multiple hours of debugging and searching we are at our wits' end. Do you have an idea how to test TenantMode.SCHEMA with multiple schemas?

}

private static DataSource createDataSource(Object tenantId) {
private Database setupSchema() {

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.

With the support of @rPraml we were able to write a failing test for tenantmode schema. THe main question seems to be, were the sequence schould live.

should
```java
UserContext.set("5711", "2");
assertEquals(1L, db.nextId(GenKeySeqA.class));
```

or
```java
UserContext.set("5711", "2");
assertEquals(3L, db.nextId(GenKeySeqA.class));
```

Does the sequence live in tenant schema or globally?
@rPraml

rPraml commented Aug 10, 2023

Copy link
Copy Markdown
Contributor

Statusupdate: We know, that sequences in a multi-tenant environment are problematic as the sequence may be stored in the wrong database. We do NOT use them. This PR is not part of our fork.

@rbygrave I think the problem still exists... If you decide to close this PR, then it should be mentioned as limitation in multi-tenant setups with one DB/schema per tenant.

@rPraml

rPraml commented May 2, 2025

Copy link
Copy Markdown
Contributor

Hi @rbygrave we will not pursue this topic any further as we do not need multi-tenant sequences.
You can decide, to close this issue (and maybe add a note to doc about known limitations on sequences + multi-tenant support)

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