Skip to content

Commit ff2d266

Browse files
committed
Merge remote-tracking branch 'origin/etcm-50/fix-db-closing' into phase/etc_forks
2 parents e0041e9 + 5ff25ac commit ff2d266

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/main/scala/io/iohk/ethereum/db/dataSource/RocksDbDataSource.scala

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class RocksDbDataSource(
2222

2323
private val logger = LoggerFactory.getLogger("rocks-db")
2424

25+
@volatile
26+
private var isClosed = false
27+
2528
/**
2629
* This function obtains the associated value to a key, if there exists one.
2730
*
@@ -30,6 +33,7 @@ class RocksDbDataSource(
3033
* @return the value associated with the passed key.
3134
*/
3235
override def get(namespace: Namespace, key: Key): Option[Value] = {
36+
assureNotClosed()
3337
RocksDbDataSource.dbLock.readLock().lock()
3438
try {
3539
Option(db.get(handles(namespace), readOptions, key.toArray))
@@ -51,6 +55,7 @@ class RocksDbDataSource(
5155
* @return the value associated with the passed key.
5256
*/
5357
override def getOptimized(key: Array[Byte]): Option[Array[Byte]] = {
58+
assureNotClosed()
5459
RocksDbDataSource.dbLock.readLock().lock()
5560
try {
5661
Option(db.get(readOptions, key))
@@ -73,6 +78,7 @@ class RocksDbDataSource(
7378
* @return the new DataSource after the removals and insertions were done.
7479
*/
7580
override def update(namespace: Namespace, toRemove: Seq[Key], toUpsert: Seq[(Key, Value)]): DataSource = {
81+
assureNotClosed()
7682
RocksDbDataSource.dbLock.readLock().lock()
7783
try {
7884
withResources(new WriteOptions()){ writeOptions =>
@@ -104,6 +110,7 @@ class RocksDbDataSource(
104110
* @return the new DataSource after the removals and insertions were done.
105111
*/
106112
override def updateOptimized(toRemove: Seq[Array[Byte]], toUpsert: Seq[(Array[Byte], Array[Byte])]): DataSource = {
113+
assureNotClosed()
107114
RocksDbDataSource.dbLock.readLock().lock()
108115
try {
109116
withResources(new WriteOptions()){ writeOptions =>
@@ -132,12 +139,16 @@ class RocksDbDataSource(
132139
override def clear: DataSource = {
133140
destroy()
134141
logger.debug(s"About to create new DataSource for path: ${ rocksDbConfig.path }")
135-
val (newDb, handles, readOptions, dbOptions, cfOptions) = RocksDbDataSource.createDB(rocksDbConfig, nameSpaces)
142+
val (newDb, handles, readOptions, dbOptions, cfOptions) = RocksDbDataSource.createDB(rocksDbConfig, nameSpaces.tail)
143+
144+
assert(nameSpaces.size == handles.size)
145+
136146
this.db = newDb
137147
this.readOptions = readOptions
138-
this.handles = nameSpaces.zip(handles).toMap
148+
this.handles = nameSpaces.zip(handles.toList).toMap
139149
this.dbOptions = dbOptions
140150
this.cfOptions = cfOptions
151+
this.isClosed = false
141152
this
142153
}
143154

@@ -146,6 +157,8 @@ class RocksDbDataSource(
146157
*/
147158
override def close(): Unit = {
148159
logger.debug(s"About to close DataSource in path: ${ rocksDbConfig.path }")
160+
assureNotClosed()
161+
isClosed = true
149162
RocksDbDataSource.dbLock.writeLock().lock()
150163
try {
151164
// There is specific order for closing rocksdb with column families descibed in
@@ -172,7 +185,9 @@ class RocksDbDataSource(
172185
*/
173186
override def destroy(): Unit = {
174187
try {
175-
close()
188+
if (!isClosed) {
189+
close()
190+
}
176191
} finally {
177192
import rocksDbConfig._
178193

@@ -198,6 +213,13 @@ class RocksDbDataSource(
198213
options.close()
199214
}
200215
}
216+
217+
private def assureNotClosed(): Unit = {
218+
if (isClosed) {
219+
throw new IllegalStateException(s"This ${getClass.getSimpleName} has been closed")
220+
}
221+
}
222+
201223
}
202224

203225
trait RocksDbConfig {
@@ -273,6 +295,8 @@ object RocksDbDataSource {
273295
val (db, handles, readOptions, dbOptions, cfOptions) = createDB(rocksDbConfig, namespaces)
274296
assert(allNameSpaces.size == handles.size)
275297
val handlesMap = allNameSpaces.zip(handles.toList).toMap
298+
//This assert ensures that we do not have duplicated namespaces
299+
assert(handlesMap.size == handles.size)
276300
new RocksDbDataSource(db, rocksDbConfig, readOptions, dbOptions, cfOptions, allNameSpaces, handlesMap)
277301
}
278302
}

src/main/scala/io/iohk/ethereum/db/storage/KeyValueStorage.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ object Namespaces {
5050
val AppStateNamespace: IndexedSeq[Byte] = IndexedSeq[Byte]('s'.toByte)
5151
val KnownNodesNamespace: IndexedSeq[Byte] = IndexedSeq[Byte]('k'.toByte)
5252
val HeightsNamespace: IndexedSeq[Byte] = IndexedSeq[Byte]('i'.toByte)
53-
val FastSyncStateNamespace: IndexedSeq[Byte] = IndexedSeq[Byte]('h'.toByte)
53+
val FastSyncStateNamespace: IndexedSeq[Byte] = IndexedSeq[Byte]('f'.toByte)
5454
val TransactionMappingNamespace: IndexedSeq[Byte] = IndexedSeq[Byte]('l'.toByte)
5555

5656
val nsSeq = Seq(

0 commit comments

Comments
 (0)