You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
UserLoginFailure: Transaction not successfully started thrown in multiple tests
#11176
I've finished the implementation of the user login failures JPA storage and while testing it I came across this error in tests that remove more than one user via UserManager:
java.lang.IllegalStateException: Transaction not successfully started
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:98)
at org.keycloak.models.map.storage.jpa.JpaMapKeycloakTransaction.commit(JpaMapKeycloakTransaction.java:176)
at org.keycloak.services.DefaultKeycloakTransactionManager.commit(DefaultKeycloakTransactionManager.java:146)
UserSessionProviderTest is a good example of a test that causes this error. Basically this is what happens:
Test calls UserManager.removeUser to remove more than one user as part of the same transaction.
the factory creates a new MapUserLoginFailureProvider, which on creation calls store.createTransaction(session).
the code for the JPA store checks the session for an existing transaction using a predefined key jpa-map-tx-modelType.hashCode, returning the previous tx if one is found in the session or creating a new tx otherwise.
the user login failure provider enlists the tx.
So when removing the first user, a new transaction is created for the user login failure and enlisted. From that point on, every time the MapUserLoginFailureProviderFactory handles the removal event, it creates a MapUserLoginFailureProvider that in turn fetches the previously created tx from the session and enlists it again.
When the overall transaction is committed, the user login failure transaction is committed multiple times because it has been enlisted more than once while handling the user removal event, which causes the exception described above.
I could think of a few ways to get around it:
removeUser is called multiple times in the same UserManager only by the tests. Our endpoints offer no bulk user removal, so it always processes one removal per transaction. That means we can probably adjust the tests to, for example, remove the created users calling the endpoint directly for each user (adminClient.realm("test").users().get(userId1).remove();), but this requires keeping track o the ids of the users created in each test.
make all the enlist methods in DefaultKeycloakTransactionManager check if the same instance has already been enlisted - this would prevent multiple commit() calls to the same transaction object.
Adjust the MapUserLoginFailureProvider's constructor so that it somehow checks if the tx already exists in the session before calling enlistAfterComplete. This would ensure that only the provider that created the transaction that is currently active in the session gets to enlist it.
I'm currently leaning towards option 2 and make the enlist methods check if the list of transactions already contain the transaction being enlisted to avoid enlisting it twice. Option 1 works but will require many changes to all the tests that remove users. Option 3 is feasible although I would need to investigate a viable option for looking up the session because the key is constructed by the JpaMapStorageProvider and is not available to the user login failure provider.
Hi @sguilhen, looking at the code option 2 seems to be the smallest change.
Looking a bit more closer at the implementation I did for LDAP, it is strange that the code in the storage provider already decides if a new transaction should be created or an existing should be used, and some other code would decide if it should be enlisted. Also, that other code would decide if it would be enlist() or enlistAfterCompletion().
Therefore I would propose the following change: the one who creates the transaction should also enlist it with the session, in this case the one who calls factory.createTransaction() would then also enlist it with the session.
Some afterthoughts after talking to @sguilhen: If the decision of enlist vs. enlistAfterCompletion stays with the caller, a transaction could be enlisted within both or - depending on a future logic - with one or the other depending on who is calling it first.
@hmlnarik do you have any thoughts on how to proceed with this? I was able to get the user login failures JPA store working with all tests by adding checks to the transaction manager so it doesn't enlist the same instance, but it feels like we could do better so that this situation doesn't even happen in the first place.
Yeah, I agree with the proposal. The challenge here is that each MapStorageProvider.createTransaction impl is different. HotRodMapStorage doesn't use the session, while JpaMapStorageProvider, LapMapStorageProvider and ConcurrentHashMapStorage all use the session but with different keys. So the particular Map*Provider that is creating and enlisting the transaction has no clue about what format it should use to look up the session.
Note that there might be two separate transactions active at the same time, say for a configuration where there are two LDAP connections targetting separate servers configured in the tree store, both active at the same time.
So it is not desirable for a storage instance to interfere with a transaction created with the other instance. Particular Map*Provider should not be looking up the transaction, it should be the storage that owns and understands how to look the transaction up, e.g.
Some afterthoughts after talking to @sguilhen: If the decision of enlist vs. enlistAfterCompletion stays with the caller, a transaction could be enlisted within both or - depending on a future logic - with one or the other depending on who is calling it first.
Where the transaction is enlisted depends on the semantics of the caller and thus cannot be enlisted at both, nor arbitrarily altered in FCFS manner.
[...] it feels like we could do better so that this situation doesn't even happen in the first place.
I agree. The legacy JPA transaction is never enlisted twice since it is created in a separate provider. Sadly, we don't have this comfort here, as there might be several JPA servers being targetted (cf. several LDAP servers example above).
It seems that once we solve the question how to get unique key to a session attribute, we'd be done. In case of JPA, the transaction should be the same for the same connection which in turn is represented by a respective map storage ProviderFactory. IOW, contrary to ConcurrentHashMapStorage above, rather than hash code on that storage, we need an identifier that is passed to each map storage from the factory and used as the key (could be a hash code, but unique a identifier obtained from an AtomicInteger could be even better as it is collision-free). WDYT?
That seems reasonable to me. So each storage instance is created with a different identifier that it will use in it's createTransaction method, meaning that you will only get to reuse the same tx if the same storage instance is being called to create transactions multiple times?
Almost - this works in case of CHM or HotRod where the transaction is processed per each storage (i.e. per each model class). In JPA the transaction is obtained from the datasource which is likely shared across storages created by the same MapStorageProvider for different areas; here the identifier (and thus the transaction) would be shared within the session across areas.
A per-area map storage instance may contain a direct or indirect reference to the main JPA transaction, (e.g. by reference to an entity manager or a specific field referring directly the JPA transaction - as needed)
The thing is currently we don't invoke this method when obtaining the map storage. When a Map*ProviderFactory creates a particular provider, it calls getStorage:
Which essentially obtains the storage provider factory and then calls create(session) on it instead of calling the method you've referenced. This has the downside of the session not being able to track and cache the particular storage provider, so I wonder if we shouldn't change the getStorage implementation to call this method instead and reap the benefits of the session cache. Do you envision any problems with this change? For JPA we surely want the same provider instance to be used for the duration of the session and I can't see any of the other implementations requiring a new instance of their provider every time getStorage is called.
Ah, yes, that's right. The reason is that in area Map*Providers, there is a storage option in config which can either point to system-wide storage, adjusted storage config, or is just not there. It's related to #10324. In that case, let's go with the workaround you suggested (i.e. implementing the in-session caching of the created JpaMapStorageProviders), just add a comment in appropriate place in the code that should be revisited once #10324 is resolved
I don't even think we will have to revisit this later because the getComponentProvider method uses a hash for key that includes the componentId, and this componentId will be different for each profile. So as long as the component id is computed in a way that takes the profile into consideration (which is something included in #10324 discussion) we should have proper providers being handed out by session.getComponentProvider.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I've finished the implementation of the user login failures JPA storage and while testing it I came across this error in tests that remove more than one user via
UserManager:UserSessionProviderTest is a good example of a test that causes this error. Basically this is what happens:
UserManager.removeUserto remove more than one user as part of the same transaction.UserRemovedEventis generated and is handled by the MapUserLoginFailureProviderFactory.MapUserLoginFailureProvider, which on creation callsstore.createTransaction(session).jpa-map-tx-modelType.hashCode, returning the previoustxif one is found in the session or creating a newtxotherwise.So when removing the first user, a new transaction is created for the user login failure and enlisted. From that point on, every time the
MapUserLoginFailureProviderFactoryhandles the removal event, it creates aMapUserLoginFailureProviderthat in turn fetches the previously createdtxfrom the session and enlists it again.When the overall transaction is committed, the user login failure transaction is committed multiple times because it has been enlisted more than once while handling the user removal event, which causes the exception described above.
I could think of a few ways to get around it:
removeUseris called multiple times in the sameUserManageronly by the tests. Our endpoints offer no bulk user removal, so it always processes one removal per transaction. That means we can probably adjust the tests to, for example, remove the created users calling the endpoint directly for each user (adminClient.realm("test").users().get(userId1).remove();), but this requires keeping track o the ids of the users created in each test.enlistmethods inDefaultKeycloakTransactionManagercheck if the same instance has already been enlisted - this would prevent multiplecommit()calls to the same transaction object.MapUserLoginFailureProvider's constructor so that it somehow checks if thetxalready exists in the session before callingenlistAfterComplete. This would ensure that only the provider that created the transaction that is currently active in the session gets to enlist it.I'm currently leaning towards option 2 and make the
enlistmethods check if the list of transactions already contain the transaction being enlisted to avoid enlisting it twice. Option 1 works but will require many changes to all the tests that remove users. Option 3 is feasible although I would need to investigate a viable option for looking up the session because the key is constructed by theJpaMapStorageProviderand is not available to the user login failure provider.All reactions
Replies: 2 comments · 14 replies
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi @sguilhen, looking at the code option 2 seems to be the smallest change.
Looking a bit more closer at the implementation I did for LDAP, it is strange that the code in the storage provider already decides if a new transaction should be created or an existing should be used, and some other code would decide if it should be enlisted. Also, that other code would decide if it would be enlist() or enlistAfterCompletion().
Therefore I would propose the following change: the one who creates the transaction should also enlist it with the session, in this case the one who calls factory.createTransaction() would then also enlist it with the session.
WDYT?
All reactions
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Some afterthoughts after talking to @sguilhen: If the decision of enlist vs. enlistAfterCompletion stays with the caller, a transaction could be enlisted within both or - depending on a future logic - with one or the other depending on who is calling it first.
All reactions
Uh oh!
There was an error while loading. Please reload this page.
@hmlnarik do you have any thoughts on how to proceed with this? I was able to get the user login failures JPA store working with all tests by adding checks to the transaction manager so it doesn't enlist the same instance, but it feels like we could do better so that this situation doesn't even happen in the first place.
All reactions
Uh oh!
There was an error while loading. Please reload this page.
Note that there might be two separate transactions active at the same time, say for a configuration where there are two LDAP connections targetting separate servers configured in the tree store, both active at the same time.
So it is not desirable for a storage instance to interfere with a transaction created with the other instance. Particular
Map*Providershould not be looking up the transaction, it should be the storage that owns and understands how to look the transaction up, e.g.keycloak/model/map/src/main/java/org/keycloak/models/map/storage/chm/ConcurrentHashMapStorage.java
Lines 131 to 132 in fb81242
Where the transaction is enlisted depends on the semantics of the caller and thus cannot be enlisted at both, nor arbitrarily altered in FCFS manner.
I agree. The legacy JPA transaction is never enlisted twice since it is created in a separate provider. Sadly, we don't have this comfort here, as there might be several JPA servers being targetted (cf. several LDAP servers example above).
It seems that once we solve the question how to get unique key to a session attribute, we'd be done. In case of JPA, the transaction should be the same for the same connection which in turn is represented by a respective map storage
ProviderFactory. IOW, contrary toConcurrentHashMapStorageabove, rather than hash code on that storage, we need an identifier that is passed to each map storage from the factory and used as the key (could be a hash code, but unique a identifier obtained from anAtomicIntegercould be even better as it is collision-free). WDYT?All reactions
Uh oh!
There was an error while loading. Please reload this page.
That seems reasonable to me. So each storage instance is created with a different identifier that it will use in it's
createTransactionmethod, meaning that you will only get to reuse the same tx if the same storage instance is being called to create transactions multiple times?All reactions
Uh oh!
There was an error while loading. Please reload this page.
Almost - this works in case of CHM or HotRod where the transaction is processed per each storage (i.e. per each model class). In JPA the transaction is obtained from the datasource which is likely shared across storages created by the same
MapStorageProviderfor different areas; here the identifier (and thus the transaction) would be shared within the session across areas.All reactions
Uh oh!
There was an error while loading. Please reload this page.
Attached is the diagram of the setup that we agreed on in the call:
getStorageAll reactions
Uh oh!
There was an error while loading. Please reload this page.
You're correct. This is handled in
keycloak/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java
Lines 353 to 370 in a6dd9dc
JpaMapStorageProviderinstance, then we should better ATM put the logic into thecreatemethodAll reactions
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
The thing is currently we don't invoke this method when obtaining the map storage. When a
Map*ProviderFactorycreates a particular provider, it callsgetStorage:keycloak/model/map/src/main/java/org/keycloak/models/map/common/AbstractMapProviderFactory.java
Lines 59 to 65 in ed79c2a
Which essentially obtains the storage provider factory and then calls
create(session)on it instead of calling the method you've referenced. This has the downside of the session not being able to track and cache the particular storage provider, so I wonder if we shouldn't change thegetStorageimplementation to call this method instead and reap the benefits of the session cache. Do you envision any problems with this change? For JPA we surely want the same provider instance to be used for the duration of the session and I can't see any of the other implementations requiring a new instance of their provider every timegetStorageis called.All reactions
Uh oh!
There was an error while loading. Please reload this page.
Ah, yes, that's right. The reason is that in area
Map*Providers, there is astorageoption inconfigwhich can either point to system-wide storage, adjusted storage config, or is just not there. It's related to #10324. In that case, let's go with the workaround you suggested (i.e. implementing the in-session caching of the createdJpaMapStorageProviders), just add a comment in appropriate place in the code that should be revisited once #10324 is resolvedAll reactions
Uh oh!
There was an error while loading. Please reload this page.
I don't even think we will have to revisit this later because the
getComponentProvidermethod uses a hash for key that includes thecomponentId, and thiscomponentIdwill be different for each profile. So as long as the component id is computed in a way that takes the profile into consideration (which is something included in #10324 discussion) we should have proper providers being handed out bysession.getComponentProvider.All reactions
Uh oh!
There was an error while loading. Please reload this page.
This work is tracked by #11230
All reactions