Author: mgrigorov
Date: Wed Oct 26 10:07:09 2011
New Revision: 1189104
URL: http://svn.apache.org/viewvc?rev=1189104&view=rev
Log:
WICKET-4170 Dump the threads' stack traces if a thread cannot acquire the lock
on a page instance
Manually dump the threads' stack traces because there is no API in Java 5/6 to
do this automatically.
The format is close to the real thread dump format but not exact because the
API doesn't provide all needed information (tid, nid, waiting conditions, ...).
The most important information is there: the name of the thread that cannot
acquire the lock and the name of the thread which holds the lock.
Added:
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Threads.java
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/page/PageAccessSynchronizer.java
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IExceptionSettings.java
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ExceptionSettings.java
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/page/PageAccessSynchronizer.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/page/PageAccessSynchronizer.java?rev=1189104&r1=1189103&r2=1189104&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/page/PageAccessSynchronizer.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/page/PageAccessSynchronizer.java
Wed Oct 26 10:07:09 2011
@@ -21,8 +21,10 @@ import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
+import org.apache.wicket.Application;
import org.apache.wicket.util.IProvider;
import org.apache.wicket.util.LazyInitializer;
+import org.apache.wicket.util.lang.Threads;
import org.apache.wicket.util.time.Duration;
import org.apache.wicket.util.time.Time;
import org.slf4j.Logger;
@@ -88,6 +90,8 @@ public class PageAccessSynchronizer impl
final boolean isDebugEnabled = logger.isDebugEnabled();
+ PageLock previous = null;
+
while (!locked && start.elapsedSince().lessThan(timeout))
{
if (isDebugEnabled)
@@ -96,7 +100,8 @@ public class PageAccessSynchronizer impl
thread.getName(), pageId);
}
- PageLock previous = locks.get().putIfAbsent(pageId,
lock);
+ previous = locks.get().putIfAbsent(pageId, lock);
+
if (previous == null || previous.thread == thread)
{
// first thread to acquire lock or lock is
already owned by this thread
@@ -140,8 +145,14 @@ public class PageAccessSynchronizer impl
if (logger.isWarnEnabled())
{
logger.warn(
- "{} failed to acquire lock to page {},
attempted for {} out of allowed {}",
- new Object[] { thread.getName(),
pageId, start.elapsedSince(), timeout });
+ "Thread '{}' failed to acquire lock to
page with id '{}', attempted for {} out of allowed {}. The thread that holds
the lock has name '{}'.",
+ new Object[] { thread.getName(),
pageId, start.elapsedSince(), timeout,
+
previous.thread.getName() });
+ if (Application.exists() &&
+
Application.get().getExceptionSettings().getDumpThreadTraces())
+ {
+ Threads.dumpAllThreads();
+ }
}
throw new CouldNotLockPageException(pageId,
thread.getName(), timeout);
}
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IExceptionSettings.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IExceptionSettings.java?rev=1189104&r1=1189103&r2=1189104&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IExceptionSettings.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/IExceptionSettings.java
Wed Oct 26 10:07:09 2011
@@ -115,4 +115,17 @@ public interface IExceptionSettings
/** invoke client side failure handler */
INVOKE_FAILURE_HANDLER
}
+
+ /**
+ * @param dump
+ * a flag indicating whether Wicket can initiate dumping of
the stack traces of all
+ * live threads in the JVM.
+ */
+ void setDumpThreadsTraces(boolean dump);
+
+ /**
+ * @return {@code true} if Wicket can initiate dumping of the stack
traces of all live threads
+ * in the JVM.
+ */
+ boolean getDumpThreadTraces();
}
Modified:
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ExceptionSettings.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ExceptionSettings.java?rev=1189104&r1=1189103&r2=1189104&view=diff
==============================================================================
---
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ExceptionSettings.java
(original)
+++
wicket/trunk/wicket-core/src/main/java/org/apache/wicket/settings/def/ExceptionSettings.java
Wed Oct 26 10:07:09 2011
@@ -36,6 +36,12 @@ public class ExceptionSettings implement
private AjaxErrorStrategy errorHandlingStrategyDuringAjaxRequests =
AjaxErrorStrategy.REDIRECT_TO_ERROR_PAGE;
/**
+ * A flag indicating whether Wicket can initiate dumping of the stack
traces of all live threads
+ * in the JVM.
+ */
+ private boolean dumpThreadTraces = true;
+
+ /**
* @see
org.apache.wicket.settings.IRequestCycleSettings#getUnexpectedExceptionDisplay()
*/
public UnexpectedExceptionDisplay getUnexpectedExceptionDisplay()
@@ -67,4 +73,14 @@ public class ExceptionSettings implement
{
this.errorHandlingStrategyDuringAjaxRequests =
errorHandlingStrategyDuringAjaxRequests;
}
+
+ public void setDumpThreadsTraces(boolean dump)
+ {
+ dumpThreadTraces = dump;
+ }
+
+ public boolean getDumpThreadTraces()
+ {
+ return dumpThreadTraces;
+ }
}
Added:
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Threads.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Threads.java?rev=1189104&view=auto
==============================================================================
---
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Threads.java
(added)
+++
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/lang/Threads.java
Wed Oct 26 10:07:09 2011
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.util.lang;
+
+import java.io.PrintStream;
+import java.lang.management.ManagementFactory;
+import java.lang.management.RuntimeMXBean;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.apache.wicket.util.string.interpolator.MapVariableInterpolator;
+
+/**
+ * A utility class for dealing with {@link Thread}s.
+ */
+public class Threads
+{
+
+ private Threads()
+ {
+ }
+
+ /**
+ * Dumps the threads' stack traces in {@link System#out}.
+ *
+ * @see #dumpAllThreads(PrintStream)
+ */
+ public static void dumpAllThreads()
+ {
+ dumpAllThreads(System.out);
+ }
+
+ /**
+ * Creates a dump of the threads' state and stack traces similar to the
one that the JVM
+ * produces when signal SIGQUIT is send to the process on Unix machine.
+ * <p>
+ * Note: This is a best effort to dump as much information as possible
because the Java API
+ * doesn't provide means to get all the information that is produced by
jstack program for
+ * example.
+ * </p>
+ *
+ * @param out
+ * the output stream where the collected information will be
written
+ */
+ public static void dumpAllThreads(final PrintStream out)
+ {
+ Args.notNull(out, "out");
+
+ RuntimeMXBean runtimeMXBean =
ManagementFactory.getRuntimeMXBean();
+
+ StringBuilder dump = new StringBuilder();
+
+ String newLine = System.getProperty("line.separator", "\n");
+
+ String format = "\"${name}\"${isDaemon} prio=${priority}
tid=${threadIdDec} state=${state} ";
+
+ dump.append("Full thread dump ")
+ .append(runtimeMXBean.getVmName())
+ .append('(')
+ .append(runtimeMXBean.getVmVersion())
+ .append(')');
+ dump.append(newLine).append(newLine);
+
+ Map<Thread, StackTraceElement[]> allStackTraces =
Thread.getAllStackTraces();
+ Iterator<Entry<Thread, StackTraceElement[]>> itor =
allStackTraces.entrySet().iterator();
+ while (itor.hasNext())
+ {
+ Entry<Thread, StackTraceElement[]> entry = itor.next();
+ Thread thread = entry.getKey();
+
+ Map<CharSequence, Object> variables = new
HashMap<CharSequence, Object>();
+ variables.put("name", thread.getName());
+ variables.put("isDaemon", thread.isDaemon() ? " daemon"
: "");
+ variables.put("priority", thread.getPriority());
+ variables.put("threadIdDec", thread.getId());
+ variables.put("state", thread.getState());
+
+ String interpolated =
MapVariableInterpolator.interpolate(format, variables);
+ dump.append(interpolated).append(newLine);
+
+ StackTraceElement[] traceElements = entry.getValue();
+ for (int i = 0; i < traceElements.length; i++)
+ {
+ StackTraceElement element = traceElements[i];
+
+ dump.append("\tat ")
+ .append(element.getClassName())
+ .append('.')
+ .append(element.getMethodName())
+ .append('(');
+
+ if (element.getLineNumber() > 0)
+ {
+
dump.append(element.getFileName()).append(':').append(element.getLineNumber());
+ }
+ else
+ {
+ dump.append("Native method");
+ }
+
+ dump.append(')').append(newLine);
+ }
+
+ dump.append(newLine);
+ }
+
+ out.println(dump.toString());
+ }
+
+}