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

[SYNCOPE-1716] fixed membership hashmap multi-threaded problem with concurrent pull #571

Merged
merged 5 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
package org.apache.syncope.core.provisioning.java.pushpull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.syncope.common.lib.request.AnyUR;
import org.apache.syncope.common.lib.request.MembershipUR;
import org.apache.syncope.common.lib.request.UserUR;
Expand Down Expand Up @@ -78,9 +79,9 @@ public class LDAPMembershipPullActions implements PullActions {
@Autowired
protected UserProvisioningManager userProvisioningManager;

protected final Map<String, Set<String>> membershipsBefore = new HashMap<>();
protected final Map<String, Set<String>> membershipsBefore = new ConcurrentHashMap<>();

protected final Map<String, Set<String>> membershipsAfter = new HashMap<>();
protected final Map<String, Set<String>> membershipsAfter = new ConcurrentHashMap<>();

/**
* Allows easy subclassing for the ConnId AD connector bundle.
Expand Down Expand Up @@ -153,11 +154,9 @@ public void beforeUpdate(
}

groupDAO.findUMemberships(groupDAO.find(entity.getKey())).forEach(uMembership -> {
Set<String> memb = membershipsBefore.get(uMembership.getLeftEnd().getKey());
if (memb == null) {
memb = new HashSet<>();
membershipsBefore.put(uMembership.getLeftEnd().getKey(), memb);
}
Set<String> memb = membershipsBefore.computeIfAbsent(
uMembership.getLeftEnd().getKey(),
k -> Collections.synchronizedSet(new HashSet<>()));
memb.add(entity.getKey());
});
}
Expand Down Expand Up @@ -192,11 +191,9 @@ public void after(
profile.getTask().getResource(),
profile.getConnector());
if (match.isPresent()) {
Set<String> memb = membershipsAfter.get(match.get().getAny().getKey());
if (memb == null) {
memb = new HashSet<>();
membershipsAfter.put(match.get().getAny().getKey(), memb);
}
Set<String> memb = membershipsAfter.computeIfAbsent(
match.get().getAny().getKey(),
k -> Collections.synchronizedSet(new HashSet<>()));
memb.add(entity.getKey());
} else {
LOG.warn("Could not find matching user for {}", membValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
Expand Down Expand Up @@ -98,7 +97,6 @@ public class LDAPMembershipPullActionsTest extends AbstractTest {
@Mock
private ProvisioningReport result;

@Mock
private Map<String, Set<String>> membershipsAfter;

@Mock
Expand Down Expand Up @@ -139,6 +137,7 @@ public void initTest() {

anyReq = new UserUR();
membershipsBefore = new HashMap<>();
membershipsAfter = new HashMap<>();
ReflectionTestUtils.setField(ldapMembershipPullActions, "membershipsBefore", membershipsBefore);
ReflectionTestUtils.setField(ldapMembershipPullActions, "membershipsAfter", membershipsAfter);

Expand Down Expand Up @@ -208,8 +207,7 @@ public void after() throws JobExecutionException {

ldapMembershipPullActions.after(profile, syncDelta, entity, result);

verify(membershipsAfter).get(anyString());
verify(membershipsAfter).put(anyString(), any());
assertEquals(1, membershipsAfter.get(user.getKey()).size());
assertEquals(expected, attribute.getValue());
}
}