Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
45be4dac
提交
45be4dac
authored
6 年前
作者:
Andrei Tokar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
convert MVStore.state into AtomicInteger, eliminate race on close()
上级
b55aaa2b
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
66 行增加
和
60 行删除
+66
-60
MVStore.java
h2/src/main/org/h2/mvstore/MVStore.java
+66
-60
没有找到文件。
h2/src/main/org/h2/mvstore/MVStore.java
浏览文件 @
45be4dac
...
@@ -148,23 +148,24 @@ public class MVStore implements AutoCloseable {
...
@@ -148,23 +148,24 @@ public class MVStore implements AutoCloseable {
private
static
final
int
MARKED_FREE
=
10_000_000
;
private
static
final
int
MARKED_FREE
=
10_000_000
;
/**
/**
* Stor
ag
e is open.
* Store is open.
*/
*/
private
static
final
int
STATE_OPEN
=
0
;
private
static
final
int
STATE_OPEN
=
0
;
/**
/**
* Storage is stopping now. Background writer thread finishes its work
* Store is about to close now, but is still operational.
* during this process.
* Outstanding store operation by background writer or other thread may be in progress.
* New updates must not be initiated, unless they are part of a closing procedure itself.
*/
*/
private
static
final
int
STATE_STOPPING
=
1
;
private
static
final
int
STATE_STOPPING
=
1
;
/**
/**
* Stor
age is closing now
.
* Stor
e is closing now, and any operation on it may fail
.
*/
*/
private
static
final
int
STATE_CLOSING
=
2
;
private
static
final
int
STATE_CLOSING
=
2
;
/**
/**
* Stor
ag
e is closed.
* Store is closed.
*/
*/
private
static
final
int
STATE_CLOSED
=
3
;
private
static
final
int
STATE_CLOSED
=
3
;
...
@@ -183,7 +184,7 @@ public class MVStore implements AutoCloseable {
...
@@ -183,7 +184,7 @@ public class MVStore implements AutoCloseable {
private
volatile
boolean
reuseSpace
=
true
;
private
volatile
boolean
reuseSpace
=
true
;
private
volatile
int
state
;
private
final
AtomicInteger
state
=
new
AtomicInteger
()
;
private
final
FileStore
fileStore
;
private
final
FileStore
fileStore
;
...
@@ -435,7 +436,7 @@ public class MVStore implements AutoCloseable {
...
@@ -435,7 +436,7 @@ public class MVStore implements AutoCloseable {
}
}
private
void
panic
(
IllegalStateException
e
)
{
private
void
panic
(
IllegalStateException
e
)
{
if
(
state
==
STATE_OPEN
)
{
if
(
isOpen
()
)
{
handleException
(
e
);
handleException
(
e
);
panicException
=
e
;
panicException
=
e
;
closeImmediately
();
closeImmediately
();
...
@@ -939,27 +940,13 @@ public class MVStore implements AutoCloseable {
...
@@ -939,27 +940,13 @@ public class MVStore implements AutoCloseable {
*/
*/
@Override
@Override
public
void
close
()
{
public
void
close
()
{
if
(
isClosed
())
{
return
;
}
FileStore
f
=
fileStore
;
if
(
f
!=
null
&&
!
f
.
isReadOnly
())
{
stopBackgroundThread
();
for
(
MVMap
<?,
?>
map
:
maps
.
values
())
{
if
(
map
.
isClosed
())
{
if
(
meta
.
remove
(
MVMap
.
getMapRootKey
(
map
.
getId
()))
!=
null
)
{
markMetaChanged
();
}
}
}
commit
();
}
closeStore
(
true
);
closeStore
(
true
);
}
}
/**
/**
* Close the file and the store, without writing anything. This will stop
* Close the file and the store, without writing anything.
* the background thread. This method ignores all errors.
* This will try to stop the background thread (without waiting for it).
* This method ignores all errors.
*/
*/
public
void
closeImmediately
()
{
public
void
closeImmediately
()
{
try
{
try
{
...
@@ -969,43 +956,54 @@ public class MVStore implements AutoCloseable {
...
@@ -969,43 +956,54 @@ public class MVStore implements AutoCloseable {
}
}
}
}
private
void
closeStore
(
boolean
shrinkIfPossible
)
{
private
void
closeStore
(
boolean
normalShutdown
)
{
if
(
isClosed
())
{
while
(!
isClosed
())
{
return
;
if
(
state
.
compareAndSet
(
STATE_OPEN
,
STATE_STOPPING
))
{
}
state
=
STATE_STOPPING
;
try
{
stopBackgroundThread
();
state
=
STATE_CLOSING
;
storeLock
.
lock
();
try
{
try
{
try
{
if
(
fileStore
!=
null
&&
shrinkIfPossible
)
{
stopBackgroundThread
();
shrinkFileIfPossible
(
0
);
storeLock
.
lock
();
}
try
{
// release memory early - this is important when called
try
{
// because of out of memory
if
(
normalShutdown
&&
fileStore
!=
null
&&
!
fileStore
.
isReadOnly
())
{
if
(
cache
!=
null
)
{
for
(
MVMap
<?,
?>
map
:
maps
.
values
())
{
cache
.
clear
();
if
(
map
.
isClosed
())
{
}
if
(
meta
.
remove
(
MVMap
.
getMapRootKey
(
map
.
getId
()))
!=
null
)
{
if
(
cacheChunkRef
!=
null
)
{
markMetaChanged
();
cacheChunkRef
.
clear
();
}
}
}
for
(
MVMap
<?,
?>
m
:
new
ArrayList
<>(
maps
.
values
()))
{
}
m
.
close
();
commit
();
shrinkFileIfPossible
(
0
);
}
state
.
set
(
STATE_CLOSING
);
// release memory early - this is important when called
// because of out of memory
if
(
cache
!=
null
)
{
cache
.
clear
();
}
if
(
cacheChunkRef
!=
null
)
{
cacheChunkRef
.
clear
();
}
for
(
MVMap
<?,
?>
m
:
new
ArrayList
<>(
maps
.
values
()))
{
m
.
close
();
}
chunks
.
clear
();
maps
.
clear
();
}
finally
{
if
(
fileStore
!=
null
&&
!
fileStoreIsProvided
)
{
fileStore
.
close
();
}
}
}
finally
{
storeLock
.
unlock
();
}
}
chunks
.
clear
();
maps
.
clear
();
}
finally
{
}
finally
{
if
(
fileStore
!=
null
&&
!
fileStoreIsProvided
)
{
state
.
set
(
STATE_CLOSED
);
fileStore
.
close
();
}
}
}
}
finally
{
storeLock
.
unlock
();
}
}
}
finally
{
state
=
STATE_CLOSED
;
}
}
}
}
...
@@ -2805,12 +2803,20 @@ public class MVStore implements AutoCloseable {
...
@@ -2805,12 +2803,20 @@ public class MVStore implements AutoCloseable {
}
}
}
}
private
boolean
isOpen
()
{
return
state
.
get
()
==
STATE_OPEN
;
}
/**
* Determine that store is open, or wait for it to be closed (by other thread)
* @return true if store is open, false otherwise
*/
public
boolean
isClosed
()
{
public
boolean
isClosed
()
{
if
(
state
==
STATE_OPEN
)
{
if
(
isOpen
()
)
{
return
false
;
return
false
;
}
}
int
millis
=
1
;
int
millis
=
1
;
while
(
state
!=
STATE_CLOSED
)
{
while
(
state
.
get
()
!=
STATE_CLOSED
)
{
/*
/*
* We need to wait for completion of close procedure. This is
* We need to wait for completion of close procedure. This is
* required because otherwise database may be closed too early while
* required because otherwise database may be closed too early while
...
@@ -2829,7 +2835,7 @@ public class MVStore implements AutoCloseable {
...
@@ -2829,7 +2835,7 @@ public class MVStore implements AutoCloseable {
}
}
private
boolean
isOpenOrStopping
()
{
private
boolean
isOpenOrStopping
()
{
return
state
<=
STATE_STOPPING
;
return
state
.
get
()
<=
STATE_STOPPING
;
}
}
private
void
stopBackgroundThread
()
{
private
void
stopBackgroundThread
()
{
...
@@ -2874,7 +2880,7 @@ public class MVStore implements AutoCloseable {
...
@@ -2874,7 +2880,7 @@ public class MVStore implements AutoCloseable {
}
}
stopBackgroundThread
();
stopBackgroundThread
();
// start the background thread if needed
// start the background thread if needed
if
(
millis
>
0
&&
state
==
STATE_OPEN
)
{
if
(
millis
>
0
&&
isOpen
()
)
{
int
sleep
=
Math
.
max
(
1
,
millis
/
10
);
int
sleep
=
Math
.
max
(
1
,
millis
/
10
);
BackgroundWriterThread
t
=
BackgroundWriterThread
t
=
new
BackgroundWriterThread
(
this
,
sleep
,
new
BackgroundWriterThread
(
this
,
sleep
,
...
...
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论