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

APPSERV-114 Addresses possible sources of Race Conditions in InvocationManager #4602

Merged
merged 5 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -42,22 +42,24 @@

import static java.lang.ThreadLocal.withInitial;
import static java.util.Collections.emptyList;
import static java.util.logging.Level.WARNING;
import static org.glassfish.api.invocation.ComponentInvocation.ComponentInvocationType.EJB_INVOCATION;
import static org.glassfish.api.invocation.ComponentInvocation.ComponentInvocationType.SERVICE_STARTUP;
import static org.glassfish.api.invocation.ComponentInvocation.ComponentInvocationType.SERVLET_INVOCATION;
import static org.glassfish.api.invocation.ComponentInvocation.ComponentInvocationType.UN_INITIALIZED;

import java.lang.reflect.Method;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand All @@ -71,9 +73,11 @@
@Singleton
public class InvocationManagerImpl implements InvocationManager {

private static final Logger LOGGER = Logger.getLogger(InvocationManagerImpl.class.getName());

private final InheritableThreadLocal<InvocationFrames> framesByThread;
private final ThreadLocal<Deque<ApplicationEnvironment>> appEnvironments = withInitial(ConcurrentLinkedDeque::new);
private final ThreadLocal<Deque<Method>> webServiceMethods = withInitial(ConcurrentLinkedDeque::new);
private final ThreadLocal<Deque<ApplicationEnvironment>> appEnvironments = withInitial(ArrayDeque::new);
private final ThreadLocal<Deque<Method>> webServiceMethods = withInitial(ArrayDeque::new);
private final ConcurrentMap<ComponentInvocationType, ListComponentInvocationHandler> typeHandlers = new ConcurrentHashMap<>();
private final ComponentInvocationHandler allTypesHandler;

Expand Down Expand Up @@ -173,7 +177,11 @@ public <T extends ComponentInvocation> void postInvoke(T invocation) throws Invo
throw new InvocationException();
}

iter.next(); // the last is the current is "invocation"
ComponentInvocation current = iter.next(); // the last is the current is "invocation"
if (invocation != current) {
LOGGER.log(WARNING, "postInvoke not called with top of the invocation stack. Expected {0} but was: {1}",
new Object[] { current, invocation });
Comment on lines +182 to +183
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last Nitpick would be, that only EjbInvocation provides reasonable toString(). It would be great to add one into WebComponentInvocation and ComponentInvocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I remember checking it myself but then forgot to do something about it. Too many context switches. Will look into it tomorrow.

}
ComponentInvocation prev = iter.hasNext() ? iter.next() : null;

ComponentInvocationType type = invocation.getInvocationType();
Expand Down Expand Up @@ -295,7 +303,7 @@ static InvocationFrames computeChildTheadInvocation(InvocationFrames parent) {
}


static final class InvocationFrames extends ConcurrentLinkedDeque<ComponentInvocation> {
static final class InvocationFrames extends ArrayDeque<ComponentInvocation> {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private static void assertCallTimesNested2Levels(int times, InvocationManager ma
if (handler != null) {
assertPostCalledTimes(times, handler, level1, level2);
}
manager.postInvoke(level2);
manager.postInvoke(level1);
assertNull(manager.getCurrentInvocation());
}

Expand All @@ -448,7 +448,7 @@ private static void assertCallTimesNested(int times, InvocationManager manager,
if (handler != null) {
assertPostCalledTimes(times, handler, outer, inner);
}
manager.postInvoke(inner);
manager.postInvoke(outer);
assertNull(manager.getCurrentInvocation());
assertNull(manager.getPreviousInvocation());
assertEquals(emptyList(), manager.getAllInvocations());
Expand Down