-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Remove extra flush events to increase performance #43363
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
base: main
Are you sure you want to change the base?
Conversation
48b6395
to
5eabd3e
Compare
Closes keycloak#43362 Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
5eabd3e
to
49dbbc5
Compare
@shawkins - may I ask you to review this PR, or should I ask a different team? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just added a couple of general thoughts. Ideally we can revisit the excess flushing more systematically. If I recall it was roughly doubling the time that it takes to perform an import.
action.setPriority(model.getPriority()); | ||
realm.getRequiredActionProviders().add(action); | ||
em.persist(action); | ||
em.flush(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine. In general there are a lot of flushes in RealmAdapter that seem ad hoc beyond the pattern of persist / flush / detach.
Set<ClientScopeClientMappingEntity> clientScopeEntities = clientScopes.stream() | ||
.filter(clientScope -> !existingClientScopes.containsKey(clientScope.getName())) | ||
.filter(clientScope -> { | ||
if (clientScope.getProtocol() == null) { | ||
// set default protocol if not set. Otherwise, we will get a NullPointer | ||
clientScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); | ||
} | ||
return acceptedClientProtocols.contains(clientScope.getProtocol()); | ||
}) | ||
.map(clientScope -> { | ||
ClientScopeClientMappingEntity entity = new ClientScopeClientMappingEntity(); | ||
entity.setClientScopeId(clientScope.getId()); | ||
entity.setClientId(client.getId()); | ||
entity.setDefaultScope(defaultScope); | ||
em.persist(entity); | ||
return entity; | ||
}).collect(Collectors.toSet()); | ||
if (!clientScopeEntities.isEmpty()) { | ||
em.flush(); | ||
clientScopeEntities.forEach(entity -> em.detach(entity)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In situations where you have less direct control over the transitive calls, the pattern using the EntityManagers logic looks like:
Set<ClientScopeClientMappingEntity> clientScopeEntities = clientScopes.stream() | |
.filter(clientScope -> !existingClientScopes.containsKey(clientScope.getName())) | |
.filter(clientScope -> { | |
if (clientScope.getProtocol() == null) { | |
// set default protocol if not set. Otherwise, we will get a NullPointer | |
clientScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); | |
} | |
return acceptedClientProtocols.contains(clientScope.getProtocol()); | |
}) | |
.map(clientScope -> { | |
ClientScopeClientMappingEntity entity = new ClientScopeClientMappingEntity(); | |
entity.setClientScopeId(clientScope.getId()); | |
entity.setClientId(client.getId()); | |
entity.setDefaultScope(defaultScope); | |
em.persist(entity); | |
return entity; | |
}).collect(Collectors.toSet()); | |
if (!clientScopeEntities.isEmpty()) { | |
em.flush(); | |
clientScopeEntities.forEach(entity -> em.detach(entity)); | |
} | |
EntityManagers.runInBatch(session, () -> { | |
clientScopes.stream() | |
.filter(clientScope -> !existingClientScopes.containsKey(clientScope.getName())) | |
.filter(clientScope -> { | |
if (clientScope.getProtocol() == null) { | |
// set default protocol if not set. Otherwise, we will get a NullPointer | |
clientScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL); | |
} | |
return acceptedClientProtocols.contains(clientScope.getProtocol()); | |
}) | |
.forEach(clientScope -> { | |
ClientScopeClientMappingEntity entity = new ClientScopeClientMappingEntity(); | |
entity.setClientScopeId(clientScope.getId()); | |
entity.setClientId(client.getId()); | |
entity.setDefaultScope(defaultScope); | |
em.persist(entity); | |
}); | |
}, true); |
However that will generally result in 2 flushes though, rather than the single flush that is done here.
Closes #43362