Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
9b92121d
提交
9b92121d
authored
7月 08, 2014
作者:
noelgrandin@gmail.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Issue 567: h2 hangs for a long time then (sometimes) recovers.
Introduce a queue when doing table locking to prevent session starvation.
上级
d20f5855
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
105 行增加
和
91 行删除
+105
-91
changelog.html
h2/src/docsrc/html/changelog.html
+2
-0
MVTable.java
h2/src/main/org/h2/mvstore/db/MVTable.java
+53
-47
RegularTable.java
h2/src/main/org/h2/table/RegularTable.java
+50
-44
没有找到文件。
h2/src/docsrc/html/changelog.html
浏览文件 @
9b92121d
...
@@ -29,6 +29,8 @@ Change Log
...
@@ -29,6 +29,8 @@ Change Log
and which servers that are available. (patch from Nikolaj Fogh)
and which servers that are available. (patch from Nikolaj Fogh)
</li><li>
Fix bug in changing encrypted DB password that kept the file handle
</li><li>
Fix bug in changing encrypted DB password that kept the file handle
open when the wrong password was supplied. (test case from Jens Hohmuth).
open when the wrong password was supplied. (test case from Jens Hohmuth).
</li><li>
Issue 567: h2 hangs for a long time then (sometimes) recovers.
Introduce a queue when doing table locking to prevent session starvation.
</li></ul>
</li></ul>
<h2>
Version 1.4.179 Beta (2014-06-23)
</h2>
<h2>
Version 1.4.179 Beta (2014-06-23)
</h2>
...
...
h2/src/main/org/h2/mvstore/db/MVTable.java
浏览文件 @
9b92121d
...
@@ -5,12 +5,12 @@
...
@@ -5,12 +5,12 @@
*/
*/
package
org
.
h2
.
mvstore
.
db
;
package
org
.
h2
.
mvstore
.
db
;
import
java.util.ArrayDeque
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.Comparator
;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.Set
;
import
org.h2.api.DatabaseEventListener
;
import
org.h2.api.DatabaseEventListener
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
import
org.h2.command.ddl.Analyze
;
import
org.h2.command.ddl.Analyze
;
...
@@ -48,10 +48,14 @@ import org.h2.value.Value;
...
@@ -48,10 +48,14 @@ import org.h2.value.Value;
public
class
MVTable
extends
TableBase
{
public
class
MVTable
extends
TableBase
{
private
MVPrimaryIndex
primaryIndex
;
private
MVPrimaryIndex
primaryIndex
;
private
ArrayList
<
Index
>
indexes
=
New
.
arrayList
();
private
final
ArrayList
<
Index
>
indexes
=
New
.
arrayList
();
private
long
lastModificationId
;
private
long
lastModificationId
;
private
volatile
Session
lockExclusiveSession
;
private
volatile
Session
lockExclusiveSession
;
private
HashSet
<
Session
>
lockSharedSessions
=
New
.
hashSet
();
private
final
HashSet
<
Session
>
lockSharedSessions
=
New
.
hashSet
();
/**
* FIFO queue to prevent starvation, since Java's synchronized locking is biased.
*/
private
final
ArrayDeque
<
Session
>
waitingSessions
=
new
ArrayDeque
<
Session
>();
private
final
Trace
traceLock
;
private
final
Trace
traceLock
;
private
int
changesSinceAnalyze
;
private
int
changesSinceAnalyze
;
private
int
nextAnalyze
;
private
int
nextAnalyze
;
...
@@ -60,13 +64,6 @@ public class MVTable extends TableBase {
...
@@ -60,13 +64,6 @@ public class MVTable extends TableBase {
private
final
TransactionStore
store
;
private
final
TransactionStore
store
;
/**
* True if one thread ever was waiting to lock this table. This is to avoid
* calling notifyAll if no session was ever waiting to lock this table. If
* set, the flag stays. In theory, it could be reset, however not sure when.
*/
private
boolean
waitForLock
;
public
MVTable
(
CreateTableData
data
,
MVTableEngine
.
Store
store
)
{
public
MVTable
(
CreateTableData
data
,
MVTableEngine
.
Store
store
)
{
super
(
data
);
super
(
data
);
nextAnalyze
=
database
.
getSettings
().
analyzeAuto
;
nextAnalyze
=
database
.
getSettings
().
analyzeAuto
;
...
@@ -125,51 +122,25 @@ public class MVTable extends TableBase {
...
@@ -125,51 +122,25 @@ public class MVTable extends TableBase {
return
;
return
;
}
}
session
.
setWaitForLock
(
this
,
Thread
.
currentThread
());
session
.
setWaitForLock
(
this
,
Thread
.
currentThread
());
waitingSessions
.
addLast
(
session
);
try
{
try
{
doLock
(
session
,
lockMode
,
exclusive
);
doLock
1
(
session
,
lockMode
,
exclusive
);
}
finally
{
}
finally
{
session
.
setWaitForLock
(
null
,
null
);
session
.
setWaitForLock
(
null
,
null
);
waitingSessions
.
remove
(
session
);
}
}
}
}
}
}
private
void
doLock
(
Session
session
,
int
lockMode
,
boolean
exclusive
)
{
private
void
doLock
1
(
Session
session
,
int
lockMode
,
boolean
exclusive
)
{
traceLock
(
session
,
exclusive
,
"requesting for"
);
traceLock
(
session
,
exclusive
,
"requesting for"
);
// don't get the current time unless necessary
// don't get the current time unless necessary
long
max
=
0
;
long
max
=
0
;
boolean
checkDeadlock
=
false
;
boolean
checkDeadlock
=
false
;
while
(
true
)
{
while
(
true
)
{
if
(
exclusive
)
{
// if I'm the next one in the queue
if
(
lockExclusiveSession
==
null
)
{
if
(
waitingSessions
.
getFirst
()
==
session
)
{
if
(
lockSharedSessions
.
isEmpty
())
{
if
(
doLock2
(
session
,
lockMode
,
exclusive
))
{
traceLock
(
session
,
exclusive
,
"added for"
);
session
.
addLock
(
this
);
lockExclusiveSession
=
session
;
return
;
}
else
if
(
lockSharedSessions
.
size
()
==
1
&&
lockSharedSessions
.
contains
(
session
))
{
traceLock
(
session
,
exclusive
,
"add (upgraded) for "
);
lockExclusiveSession
=
session
;
return
;
}
}
}
else
{
if
(
lockExclusiveSession
==
null
)
{
if
(
lockMode
==
Constants
.
LOCK_MODE_READ_COMMITTED
)
{
if
(!
database
.
isMultiThreaded
()
&&
!
database
.
isMultiVersion
())
{
// READ_COMMITTED: a read lock is acquired,
// but released immediately after the operation
// is complete.
// When allowing only one thread, no lock is
// required.
// Row level locks work like read committed.
return
;
}
}
if
(!
lockSharedSessions
.
contains
(
session
))
{
traceLock
(
session
,
exclusive
,
"ok"
);
session
.
addLock
(
this
);
lockSharedSessions
.
add
(
session
);
}
return
;
return
;
}
}
}
}
...
@@ -210,7 +181,6 @@ public class MVTable extends TableBase {
...
@@ -210,7 +181,6 @@ public class MVTable extends TableBase {
if
(
sleep
==
0
)
{
if
(
sleep
==
0
)
{
sleep
=
1
;
sleep
=
1
;
}
}
waitForLock
=
true
;
database
.
wait
(
sleep
);
database
.
wait
(
sleep
);
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
// ignore
// ignore
...
@@ -218,6 +188,44 @@ public class MVTable extends TableBase {
...
@@ -218,6 +188,44 @@ public class MVTable extends TableBase {
}
}
}
}
private
boolean
doLock2
(
Session
session
,
int
lockMode
,
boolean
exclusive
)
{
if
(
exclusive
)
{
if
(
lockExclusiveSession
==
null
)
{
if
(
lockSharedSessions
.
isEmpty
())
{
traceLock
(
session
,
exclusive
,
"added for"
);
session
.
addLock
(
this
);
lockExclusiveSession
=
session
;
return
true
;
}
else
if
(
lockSharedSessions
.
size
()
==
1
&&
lockSharedSessions
.
contains
(
session
))
{
traceLock
(
session
,
exclusive
,
"add (upgraded) for "
);
lockExclusiveSession
=
session
;
return
true
;
}
}
}
else
{
if
(
lockExclusiveSession
==
null
)
{
if
(
lockMode
==
Constants
.
LOCK_MODE_READ_COMMITTED
)
{
if
(!
database
.
isMultiThreaded
()
&&
!
database
.
isMultiVersion
())
{
// READ_COMMITTED: a read lock is acquired,
// but released immediately after the operation
// is complete.
// When allowing only one thread, no lock is
// required.
// Row level locks work like read committed.
return
true
;
}
}
if
(!
lockSharedSessions
.
contains
(
session
))
{
traceLock
(
session
,
exclusive
,
"ok"
);
session
.
addLock
(
this
);
lockSharedSessions
.
add
(
session
);
}
return
true
;
}
}
return
false
;
}
private
static
String
getDeadlockDetails
(
ArrayList
<
Session
>
sessions
)
{
private
static
String
getDeadlockDetails
(
ArrayList
<
Session
>
sessions
)
{
// We add the thread details here to make it easier for customers to
// We add the thread details here to make it easier for customers to
// match up these error messages with their own logs.
// match up these error messages with their own logs.
...
@@ -326,10 +334,8 @@ public class MVTable extends TableBase {
...
@@ -326,10 +334,8 @@ public class MVTable extends TableBase {
if
(
lockSharedSessions
.
size
()
>
0
)
{
if
(
lockSharedSessions
.
size
()
>
0
)
{
lockSharedSessions
.
remove
(
s
);
lockSharedSessions
.
remove
(
s
);
}
}
// TODO lock: maybe we need we fifo-queue to make sure nobody
// starves. check what other databases do
synchronized
(
database
)
{
synchronized
(
database
)
{
if
(
database
.
getSessionCount
()
>
1
&&
waitForLock
)
{
if
(
!
waitingSessions
.
isEmpty
()
)
{
database
.
notifyAll
();
database
.
notifyAll
();
}
}
}
}
...
...
h2/src/main/org/h2/table/RegularTable.java
浏览文件 @
9b92121d
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
*/
*/
package
org
.
h2
.
table
;
package
org
.
h2
.
table
;
import
java.util.ArrayDeque
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.Comparator
;
import
java.util.Comparator
;
...
@@ -54,6 +55,10 @@ public class RegularTable extends TableBase {
...
@@ -54,6 +55,10 @@ public class RegularTable extends TableBase {
private
long
rowCount
;
private
long
rowCount
;
private
volatile
Session
lockExclusiveSession
;
private
volatile
Session
lockExclusiveSession
;
private
HashSet
<
Session
>
lockSharedSessions
=
New
.
hashSet
();
private
HashSet
<
Session
>
lockSharedSessions
=
New
.
hashSet
();
/**
* FIFO queue to prevent starvation, since Java's synchronized locking is biased.
*/
private
final
ArrayDeque
<
Session
>
waitingSessions
=
new
ArrayDeque
<
Session
>();
private
final
Trace
traceLock
;
private
final
Trace
traceLock
;
private
final
ArrayList
<
Index
>
indexes
=
New
.
arrayList
();
private
final
ArrayList
<
Index
>
indexes
=
New
.
arrayList
();
private
long
lastModificationId
;
private
long
lastModificationId
;
...
@@ -63,13 +68,6 @@ public class RegularTable extends TableBase {
...
@@ -63,13 +68,6 @@ public class RegularTable extends TableBase {
private
int
nextAnalyze
;
private
int
nextAnalyze
;
private
Column
rowIdColumn
;
private
Column
rowIdColumn
;
/**
* True if one thread ever was waiting to lock this table. This is to avoid
* calling notifyAll if no session was ever waiting to lock this table. If
* set, the flag stays. In theory, it could be reset, however not sure when.
*/
private
boolean
waitForLock
;
public
RegularTable
(
CreateTableData
data
)
{
public
RegularTable
(
CreateTableData
data
)
{
super
(
data
);
super
(
data
);
nextAnalyze
=
database
.
getSettings
().
analyzeAuto
;
nextAnalyze
=
database
.
getSettings
().
analyzeAuto
;
...
@@ -463,51 +461,25 @@ public class RegularTable extends TableBase {
...
@@ -463,51 +461,25 @@ public class RegularTable extends TableBase {
return
;
return
;
}
}
session
.
setWaitForLock
(
this
,
Thread
.
currentThread
());
session
.
setWaitForLock
(
this
,
Thread
.
currentThread
());
waitingSessions
.
addLast
(
session
);
try
{
try
{
doLock
(
session
,
lockMode
,
exclusive
);
doLock
1
(
session
,
lockMode
,
exclusive
);
}
finally
{
}
finally
{
session
.
setWaitForLock
(
null
,
null
);
session
.
setWaitForLock
(
null
,
null
);
waitingSessions
.
remove
(
session
);
}
}
}
}
}
}
private
void
doLock
(
Session
session
,
int
lockMode
,
boolean
exclusive
)
{
private
void
doLock
1
(
Session
session
,
int
lockMode
,
boolean
exclusive
)
{
traceLock
(
session
,
exclusive
,
"requesting for"
);
traceLock
(
session
,
exclusive
,
"requesting for"
);
// don't get the current time unless necessary
// don't get the current time unless necessary
long
max
=
0
;
long
max
=
0
;
boolean
checkDeadlock
=
false
;
boolean
checkDeadlock
=
false
;
while
(
true
)
{
while
(
true
)
{
if
(
exclusive
)
{
// if I'm the next one in the queue
if
(
lockExclusiveSession
==
null
)
{
if
(
waitingSessions
.
getFirst
()
==
session
)
{
if
(
lockSharedSessions
.
isEmpty
())
{
if
(
doLock2
(
session
,
lockMode
,
exclusive
))
{
traceLock
(
session
,
exclusive
,
"added for"
);
session
.
addLock
(
this
);
lockExclusiveSession
=
session
;
return
;
}
else
if
(
lockSharedSessions
.
size
()
==
1
&&
lockSharedSessions
.
contains
(
session
))
{
traceLock
(
session
,
exclusive
,
"add (upgraded) for "
);
lockExclusiveSession
=
session
;
return
;
}
}
}
else
{
if
(
lockExclusiveSession
==
null
)
{
if
(
lockMode
==
Constants
.
LOCK_MODE_READ_COMMITTED
)
{
if
(!
database
.
isMultiThreaded
()
&&
!
database
.
isMultiVersion
())
{
// READ_COMMITTED: a read lock is acquired,
// but released immediately after the operation
// is complete.
// When allowing only one thread, no lock is
// required.
// Row level locks work like read committed.
return
;
}
}
if
(!
lockSharedSessions
.
contains
(
session
))
{
traceLock
(
session
,
exclusive
,
"ok"
);
session
.
addLock
(
this
);
lockSharedSessions
.
add
(
session
);
}
return
;
return
;
}
}
}
}
...
@@ -545,7 +517,6 @@ public class RegularTable extends TableBase {
...
@@ -545,7 +517,6 @@ public class RegularTable extends TableBase {
if
(
sleep
==
0
)
{
if
(
sleep
==
0
)
{
sleep
=
1
;
sleep
=
1
;
}
}
waitForLock
=
true
;
database
.
wait
(
sleep
);
database
.
wait
(
sleep
);
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
// ignore
// ignore
...
@@ -553,6 +524,43 @@ public class RegularTable extends TableBase {
...
@@ -553,6 +524,43 @@ public class RegularTable extends TableBase {
}
}
}
}
private
boolean
doLock2
(
Session
session
,
int
lockMode
,
boolean
exclusive
)
{
if
(
exclusive
)
{
if
(
lockExclusiveSession
==
null
)
{
if
(
lockSharedSessions
.
isEmpty
())
{
traceLock
(
session
,
exclusive
,
"added for"
);
session
.
addLock
(
this
);
lockExclusiveSession
=
session
;
return
true
;
}
else
if
(
lockSharedSessions
.
size
()
==
1
&&
lockSharedSessions
.
contains
(
session
))
{
traceLock
(
session
,
exclusive
,
"add (upgraded) for "
);
lockExclusiveSession
=
session
;
return
true
;
}
}
}
else
{
if
(
lockExclusiveSession
==
null
)
{
if
(
lockMode
==
Constants
.
LOCK_MODE_READ_COMMITTED
)
{
if
(!
database
.
isMultiThreaded
()
&&
!
database
.
isMultiVersion
())
{
// READ_COMMITTED: a read lock is acquired,
// but released immediately after the operation
// is complete.
// When allowing only one thread, no lock is
// required.
// Row level locks work like read committed.
return
true
;
}
}
if
(!
lockSharedSessions
.
contains
(
session
))
{
traceLock
(
session
,
exclusive
,
"ok"
);
session
.
addLock
(
this
);
lockSharedSessions
.
add
(
session
);
}
return
true
;
}
}
return
false
;
}
private
static
String
getDeadlockDetails
(
ArrayList
<
Session
>
sessions
)
{
private
static
String
getDeadlockDetails
(
ArrayList
<
Session
>
sessions
)
{
// We add the thread details here to make it easier for customers to
// We add the thread details here to make it easier for customers to
// match up these error messages with their own logs.
// match up these error messages with their own logs.
...
@@ -655,10 +663,8 @@ public class RegularTable extends TableBase {
...
@@ -655,10 +663,8 @@ public class RegularTable extends TableBase {
if
(
lockSharedSessions
.
size
()
>
0
)
{
if
(
lockSharedSessions
.
size
()
>
0
)
{
lockSharedSessions
.
remove
(
s
);
lockSharedSessions
.
remove
(
s
);
}
}
// TODO lock: maybe we need we fifo-queue to make sure nobody
// starves. check what other databases do
synchronized
(
database
)
{
synchronized
(
database
)
{
if
(
database
.
getSessionCount
()
>
1
&&
waitForLock
)
{
if
(
!
waitingSessions
.
isEmpty
()
)
{
database
.
notifyAll
();
database
.
notifyAll
();
}
}
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论