提交 c6ce63d8 authored 作者: Thomas Mueller's avatar Thomas Mueller

Formatting (spaces instead of tabs and so on)

上级 94312cca
......@@ -36,7 +36,7 @@ public class Engine implements SessionFactory {
private Engine() {
// use getInstance()
ThreadDeadlockDetector.init();
ThreadDeadlockDetector.init();
}
public static Engine getInstance() {
......
......@@ -756,7 +756,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
* closed
*/
@Deprecated
@Override
@Override
public BigDecimal getBigDecimal(String columnLabel, int scale)
throws SQLException {
try {
......@@ -786,7 +786,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
* closed
*/
@Deprecated
@Override
@Override
public BigDecimal getBigDecimal(int columnIndex, int scale)
throws SQLException {
try {
......@@ -808,7 +808,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
* @deprecated since JDBC 2.0, use getCharacterStream
*/
@Deprecated
@Override
@Override
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
throw unsupported("unicodeStream");
}
......@@ -818,7 +818,7 @@ public class JdbcResultSet extends TraceObject implements ResultSet {
* @deprecated since JDBC 2.0, use setCharacterStream
*/
@Deprecated
@Override
@Override
public InputStream getUnicodeStream(String columnLabel) throws SQLException {
throw unsupported("unicodeStream");
}
......
......@@ -49,6 +49,10 @@ import org.h2.value.Value;
*/
public class MVTable extends TableBase {
public static final DebuggingThreadLocal<String> WAITING_FOR_LOCK = new DebuggingThreadLocal<String>();
public static final DebuggingThreadLocal<ArrayList<String>> EXCLUSIVE_LOCKS = new DebuggingThreadLocal<ArrayList<String>>();
public static final DebuggingThreadLocal<ArrayList<String>> SHARED_LOCKS = new DebuggingThreadLocal<ArrayList<String>>();
private MVPrimaryIndex primaryIndex;
private final ArrayList<Index> indexes = New.arrayList();
private long lastModificationId;
......@@ -58,10 +62,6 @@ public class MVTable extends TableBase {
private final ConcurrentHashMap<Session, Session> lockSharedSessions =
new ConcurrentHashMap<Session, Session>();
public static final DebuggingThreadLocal<String> WAITING_FOR_LOCK = new DebuggingThreadLocal<String>();
public static final DebuggingThreadLocal<ArrayList<String>> EXCLUSIVE_LOCKS = new DebuggingThreadLocal<ArrayList<String>>();
public static final DebuggingThreadLocal<ArrayList<String>> SHARED_LOCKS = new DebuggingThreadLocal<ArrayList<String>>();
/**
* The queue of sessions waiting to lock the table. It is a FIFO queue to
* prevent starvation, since Java's synchronized locking is biased.
......@@ -261,7 +261,7 @@ public class MVTable extends TableBase {
session.addLock(this);
lockSharedSessions.put(session, session);
if (SHARED_LOCKS.get() == null) {
SHARED_LOCKS.set(new ArrayList<String>());
SHARED_LOCKS.set(new ArrayList<String>());
}
SHARED_LOCKS.get().add(getName());
}
......@@ -385,7 +385,7 @@ public class MVTable extends TableBase {
if (lockSharedSessions.size() > 0) {
lockSharedSessions.remove(s);
if (SHARED_LOCKS.get() != null) {
SHARED_LOCKS.get().remove(getName());
SHARED_LOCKS.get().remove(getName());
}
}
if (!waitingSessions.isEmpty()) {
......
......@@ -9,7 +9,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import org.h2.api.ErrorCode;
import org.h2.api.TableEngine;
import org.h2.command.ddl.CreateTableData;
import org.h2.constraint.Constraint;
import org.h2.engine.Database;
......@@ -27,7 +26,6 @@ import org.h2.mvstore.db.MVTableEngine;
import org.h2.table.RegularTable;
import org.h2.table.Table;
import org.h2.table.TableLink;
import org.h2.util.JdbcUtils;
import org.h2.util.New;
/**
......@@ -568,9 +566,9 @@ public class Schema extends DbObjectBase {
}
data.schema = this;
if (data.tableEngine == null) {
DbSettings s = database.getSettings();
DbSettings s = database.getSettings();
if (s.defaultTableEngine != null) {
data.tableEngine = s.defaultTableEngine;
data.tableEngine = s.defaultTableEngine;
} else if (s.mvStore) {
data.tableEngine = MVTableEngine.class.getName();
}
......
......@@ -150,10 +150,10 @@ public class Sequence extends SchemaObjectBase {
maxValue > minValue &&
increment != 0 &&
// Math.abs(increment) < maxValue - minValue
// use BigInteger to avoid overflows when maxValue and minValue
// are really big
// use BigInteger to avoid overflows when maxValue and minValue
// are really big
BigInteger.valueOf(increment).abs().compareTo(
BigInteger.valueOf(maxValue).subtract(BigInteger.valueOf(minValue))) < 0;
BigInteger.valueOf(maxValue).subtract(BigInteger.valueOf(minValue))) < 0;
}
private static long getDefaultMinValue(Long startValue, long increment) {
......@@ -248,26 +248,26 @@ public class Sequence extends SchemaObjectBase {
boolean needsFlush = false;
long retVal;
long flushValueWithMargin = -1;
synchronized(this) {
if ((increment > 0 && value >= valueWithMargin) ||
(increment < 0 && value <= valueWithMargin)) {
valueWithMargin += increment * cacheSize;
flushValueWithMargin = valueWithMargin;
needsFlush = true;
}
if ((increment > 0 && value > maxValue) ||
(increment < 0 && value < minValue)) {
if (cycle) {
value = increment > 0 ? minValue : maxValue;
valueWithMargin = value + (increment * cacheSize);
flushValueWithMargin = valueWithMargin;
needsFlush = true;
} else {
throw DbException.get(ErrorCode.SEQUENCE_EXHAUSTED, getName());
}
}
retVal = value;
value += increment;
synchronized (this) {
if ((increment > 0 && value >= valueWithMargin) ||
(increment < 0 && value <= valueWithMargin)) {
valueWithMargin += increment * cacheSize;
flushValueWithMargin = valueWithMargin;
needsFlush = true;
}
if ((increment > 0 && value > maxValue) ||
(increment < 0 && value < minValue)) {
if (cycle) {
value = increment > 0 ? minValue : maxValue;
valueWithMargin = value + (increment * cacheSize);
flushValueWithMargin = valueWithMargin;
needsFlush = true;
} else {
throw DbException.get(ErrorCode.SEQUENCE_EXHAUSTED, getName());
}
}
retVal = value;
value += increment;
}
if (needsFlush) {
flush(session, flushValueWithMargin);
......@@ -308,15 +308,15 @@ public class Sequence extends SchemaObjectBase {
}
private void flushInternal(Session session, long flushValueWithMargin) {
final boolean metaWasLocked = database.lockMeta(session);
synchronized (this) {
if (flushValueWithMargin == lastFlushValueWithMargin) {
if (!metaWasLocked) {
database.unlockMeta(session);
}
return;
}
}
final boolean metaWasLocked = database.lockMeta(session);
synchronized (this) {
if (flushValueWithMargin == lastFlushValueWithMargin) {
if (!metaWasLocked) {
database.unlockMeta(session);
}
return;
}
}
// just for this case, use the value with the margin for the script
long realValue = value;
try {
......@@ -327,12 +327,12 @@ public class Sequence extends SchemaObjectBase {
} finally {
value = realValue;
}
synchronized (this) {
lastFlushValueWithMargin = flushValueWithMargin;
}
if (!metaWasLocked) {
database.unlockMeta(session);
}
synchronized (this) {
lastFlushValueWithMargin = flushValueWithMargin;
}
if (!metaWasLocked) {
database.unlockMeta(session);
}
}
/**
......
......@@ -266,11 +266,11 @@ public class Column {
* @return the new or converted value
*/
public Value validateConvertUpdateSequence(Session session, Value value) {
// take a local copy of defaultExpression to avoid holding the lock while calling getValue
final Expression localDefaultExpression;
synchronized (this) {
localDefaultExpression = defaultExpression;
}
// take a local copy of defaultExpression to avoid holding the lock while calling getValue
final Expression localDefaultExpression;
synchronized (this) {
localDefaultExpression = defaultExpression;
}
if (value == null) {
if (localDefaultExpression == null) {
value = ValueNull.INSTANCE;
......
......@@ -23,128 +23,131 @@ import org.h2.mvstore.db.MVTable;
* Detects deadlocks between threads. Prints out data in the same format as the CTRL-BREAK handler,
* but includes information about table locks.
*/
public class ThreadDeadlockDetector
{
private static String INDENT = " ";
private final ThreadMXBean tmbean;
private final Timer threadCheck = new Timer("ThreadDeadlockDetector", true/* isDaemon */);
private static ThreadDeadlockDetector detector = null;
public synchronized static void init() {
if (detector == null) {
detector = new ThreadDeadlockDetector();
}
}
private ThreadDeadlockDetector() {
this.tmbean = ManagementFactory.getThreadMXBean();
threadCheck.schedule(new TimerTask() {
@Override
public void run() {
checkForDeadlocks();
}
}, 10/*delay(ms)*/, 10000/*period(ms)*/);
}
/**
* Checks if any threads are deadlocked. If any, print the thread dump information.
*/
private void checkForDeadlocks() {
long[] tids = tmbean.findDeadlockedThreads();
if (tids == null) {
return;
}
final StringWriter stringWriter = new StringWriter();
final PrintWriter print = new PrintWriter(stringWriter);
print.println("ThreadDeadlockDetector - deadlock found :");
final ThreadInfo[] infos = tmbean.getThreadInfo(tids, true, true);
final HashMap<Long,String> mvtableWaitingForLockMap =
MVTable.WAITING_FOR_LOCK.getSnapshotOfAllThreads();
final HashMap<Long,ArrayList<String>> mvtableExclusiveLocksMap =
MVTable.EXCLUSIVE_LOCKS.getSnapshotOfAllThreads();
final HashMap<Long,ArrayList<String>> mvtableSharedLocksMap =
MVTable.SHARED_LOCKS.getSnapshotOfAllThreads();
for (ThreadInfo ti : infos) {
printThreadInfo(print, ti);
printLockInfo(print, ti.getLockedSynchronizers(),
mvtableWaitingForLockMap.get(ti.getThreadId()),
mvtableExclusiveLocksMap.get(ti.getThreadId()),
mvtableSharedLocksMap.get(ti.getThreadId()));
}
print.flush();
// Dump it to system.out in one block, so it doesn't get mixed up with other stuff when we're
// using a logging subsystem.
System.out.println(stringWriter.getBuffer());
}
private static void printThreadInfo(PrintWriter print, ThreadInfo ti) {
// print thread information
printThread(print, ti);
// print stack trace with locks
StackTraceElement[] stacktrace = ti.getStackTrace();
MonitorInfo[] monitors = ti.getLockedMonitors();
for (int i = 0; i < stacktrace.length; i++) {
StackTraceElement ste = stacktrace[i];
print.println(INDENT + "at " + ste.toString());
for (MonitorInfo mi : monitors) {
if (mi.getLockedStackDepth() == i) {
print.println(INDENT + " - locked " + mi);
}
}
}
print.println();
}
private static void printThread(PrintWriter print, ThreadInfo ti) {
print.print("\"" + ti.getThreadName() + "\"" + " Id="
+ ti.getThreadId() + " in " + ti.getThreadState());
if (ti.getLockName() != null) {
print.append(" on lock=" + ti.getLockName());
}
if (ti.isSuspended()) {
print.append(" (suspended)");
}
if (ti.isInNative()) {
print.append(" (running in native)");
}
print.println();
if (ti.getLockOwnerName() != null) {
print.println(INDENT + " owned by " + ti.getLockOwnerName() + " Id="
+ ti.getLockOwnerId());
}
}
private static void printLockInfo(PrintWriter print, LockInfo[] locks,
String mvtableWaitingForLock,
ArrayList<String> mvtableExclusiveLocks,
ArrayList<String> mvtableSharedLocksMap) {
print.println(INDENT + "Locked synchronizers: count = " + locks.length);
for (LockInfo li : locks) {
print.println(INDENT + " - " + li);
}
if (mvtableWaitingForLock != null) {
print.println(INDENT + "Waiting for table: " + mvtableWaitingForLock);
}
if (mvtableExclusiveLocks != null) {
print.println(INDENT + "Exclusive table locks: count = " + mvtableExclusiveLocks.size());
for (String name : mvtableExclusiveLocks) {
print.println(INDENT + " - " + name);
}
}
if (mvtableSharedLocksMap != null) {
print.println(INDENT + "Shared table locks: count = " + mvtableSharedLocksMap.size());
for (String name : mvtableSharedLocksMap) {
print.println(INDENT + " - " + name);
}
}
print.println();
}
public class ThreadDeadlockDetector {
private static final String INDENT = " ";
private static ThreadDeadlockDetector detector;
private final ThreadMXBean tmbean;
// a daemon thread
private final Timer threadCheck = new Timer("ThreadDeadlockDetector", true);
private ThreadDeadlockDetector() {
this.tmbean = ManagementFactory.getThreadMXBean();
// delay: 10 ms
// period: 10000 ms (100 seconds)
threadCheck.schedule(new TimerTask() {
@Override
public void run() {
checkForDeadlocks();
}
}, 10, 10000);
}
public static synchronized void init() {
if (detector == null) {
detector = new ThreadDeadlockDetector();
}
}
/**
* Checks if any threads are deadlocked. If any, print the thread dump information.
*/
void checkForDeadlocks() {
long[] tids = tmbean.findDeadlockedThreads();
if (tids == null) {
return;
}
final StringWriter stringWriter = new StringWriter();
final PrintWriter print = new PrintWriter(stringWriter);
print.println("ThreadDeadlockDetector - deadlock found :");
final ThreadInfo[] infos = tmbean.getThreadInfo(tids, true, true);
final HashMap<Long, String> mvtableWaitingForLockMap =
MVTable.WAITING_FOR_LOCK.getSnapshotOfAllThreads();
final HashMap<Long, ArrayList<String>> mvtableExclusiveLocksMap =
MVTable.EXCLUSIVE_LOCKS.getSnapshotOfAllThreads();
final HashMap<Long, ArrayList<String>> mvtableSharedLocksMap =
MVTable.SHARED_LOCKS.getSnapshotOfAllThreads();
for (ThreadInfo ti : infos) {
printThreadInfo(print, ti);
printLockInfo(print, ti.getLockedSynchronizers(),
mvtableWaitingForLockMap.get(ti.getThreadId()),
mvtableExclusiveLocksMap.get(ti.getThreadId()),
mvtableSharedLocksMap.get(ti.getThreadId()));
}
print.flush();
// Dump it to system.out in one block, so it doesn't get mixed up with other stuff when we're
// using a logging subsystem.
System.out.println(stringWriter.getBuffer());
}
private static void printThreadInfo(PrintWriter print, ThreadInfo ti) {
// print thread information
printThread(print, ti);
// print stack trace with locks
StackTraceElement[] stacktrace = ti.getStackTrace();
MonitorInfo[] monitors = ti.getLockedMonitors();
for (int i = 0; i < stacktrace.length; i++) {
StackTraceElement ste = stacktrace[i];
print.println(INDENT + "at " + ste.toString());
for (MonitorInfo mi : monitors) {
if (mi.getLockedStackDepth() == i) {
print.println(INDENT + " - locked " + mi);
}
}
}
print.println();
}
private static void printThread(PrintWriter print, ThreadInfo ti) {
print.print("\"" + ti.getThreadName() + "\"" + " Id="
+ ti.getThreadId() + " in " + ti.getThreadState());
if (ti.getLockName() != null) {
print.append(" on lock=" + ti.getLockName());
}
if (ti.isSuspended()) {
print.append(" (suspended)");
}
if (ti.isInNative()) {
print.append(" (running in native)");
}
print.println();
if (ti.getLockOwnerName() != null) {
print.println(INDENT + " owned by " + ti.getLockOwnerName() + " Id="
+ ti.getLockOwnerId());
}
}
private static void printLockInfo(PrintWriter print, LockInfo[] locks,
String mvtableWaitingForLock,
ArrayList<String> mvtableExclusiveLocks,
ArrayList<String> mvtableSharedLocksMap) {
print.println(INDENT + "Locked synchronizers: count = " + locks.length);
for (LockInfo li : locks) {
print.println(INDENT + " - " + li);
}
if (mvtableWaitingForLock != null) {
print.println(INDENT + "Waiting for table: " + mvtableWaitingForLock);
}
if (mvtableExclusiveLocks != null) {
print.println(INDENT + "Exclusive table locks: count = " + mvtableExclusiveLocks.size());
for (String name : mvtableExclusiveLocks) {
print.println(INDENT + " - " + name);
}
}
if (mvtableSharedLocksMap != null) {
print.println(INDENT + "Shared table locks: count = " + mvtableSharedLocksMap.size());
for (String name : mvtableSharedLocksMap) {
print.println(INDENT + " - " + name);
}
}
print.println();
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论