Skip to content

Commit 277e9a8

Browse files
mukul1987nandakumar131
authored andcommitted
Opening of rocksDB in datanode fails with "No locks available"
Signed-off-by: Nanda kumar <[email protected]>
1 parent 21de9af commit 277e9a8

File tree

3 files changed

+145
-25
lines changed

3 files changed

+145
-25
lines changed

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/ContainerCache.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public void shutdownCache() {
7777
while (iterator.hasNext()) {
7878
iterator.next();
7979
ReferenceCountedDB db = (ReferenceCountedDB) iterator.getValue();
80-
db.setEvicted(true);
80+
Preconditions.checkArgument(db.cleanup(), "refCount:",
81+
db.getReferenceCount());
8182
}
8283
// reset the cache
8384
cache.clear();
@@ -92,14 +93,9 @@ public void shutdownCache() {
9293
@Override
9394
protected boolean removeLRU(LinkEntry entry) {
9495
ReferenceCountedDB db = (ReferenceCountedDB) entry.getValue();
95-
String dbFile = (String)entry.getKey();
9696
lock.lock();
9797
try {
98-
db.setEvicted(false);
99-
return true;
100-
} catch (Exception e) {
101-
LOG.error("Eviction for db:{} failed", dbFile, e);
102-
return false;
98+
return db.cleanup();
10399
} finally {
104100
lock.unlock();
105101
}
@@ -156,8 +152,8 @@ public void removeDB(String containerDBPath) {
156152
try {
157153
ReferenceCountedDB db = (ReferenceCountedDB)this.get(containerDBPath);
158154
if (db != null) {
159-
// marking it as evicted will close the db as well.
160-
db.setEvicted(true);
155+
Preconditions.checkArgument(db.cleanup(), "refCount:",
156+
db.getReferenceCount());
161157
}
162158
this.remove(containerDBPath);
163159
} finally {

hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/ReferenceCountedDB.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.slf4j.LoggerFactory;
2525

2626
import java.io.Closeable;
27-
import java.util.concurrent.atomic.AtomicBoolean;
2827
import java.util.concurrent.atomic.AtomicInteger;
2928

3029
/**
@@ -38,17 +37,19 @@ public class ReferenceCountedDB implements Closeable {
3837
private static final Logger LOG =
3938
LoggerFactory.getLogger(ReferenceCountedDB.class);
4039
private final AtomicInteger referenceCount;
41-
private final AtomicBoolean isEvicted;
4240
private final MetadataStore store;
4341
private final String containerDBPath;
4442

4543
public ReferenceCountedDB(MetadataStore store, String containerDBPath) {
4644
this.referenceCount = new AtomicInteger(0);
47-
this.isEvicted = new AtomicBoolean(false);
4845
this.store = store;
4946
this.containerDBPath = containerDBPath;
5047
}
5148

49+
public long getReferenceCount() {
50+
return referenceCount.get();
51+
}
52+
5253
public void incrementReference() {
5354
this.referenceCount.incrementAndGet();
5455
if (LOG.isDebugEnabled()) {
@@ -59,35 +60,30 @@ public void incrementReference() {
5960
}
6061

6162
public void decrementReference() {
62-
this.referenceCount.decrementAndGet();
63+
int refCount = this.referenceCount.decrementAndGet();
64+
Preconditions.checkArgument(refCount >= 0, "refCount:", refCount);
6365
if (LOG.isDebugEnabled()) {
6466
LOG.debug("DecRef {} to refCnt {} \n", containerDBPath,
6567
referenceCount.get());
6668
new Exception().printStackTrace();
6769
}
68-
cleanup();
69-
}
70-
71-
public void setEvicted(boolean checkNoReferences) {
72-
Preconditions.checkState(!checkNoReferences ||
73-
(referenceCount.get() == 0),
74-
"checkNoReferences:%b, referencount:%d, dbPath:%s",
75-
checkNoReferences, referenceCount.get(), containerDBPath);
76-
isEvicted.set(true);
77-
cleanup();
7870
}
7971

80-
private void cleanup() {
81-
if (referenceCount.get() == 0 && isEvicted.get() && store != null) {
72+
public boolean cleanup() {
73+
if (referenceCount.get() == 0 && store != null) {
8274
if (LOG.isDebugEnabled()) {
8375
LOG.debug("Close {} refCnt {}", containerDBPath,
8476
referenceCount.get());
8577
}
8678
try {
8779
store.close();
80+
return true;
8881
} catch (Exception e) {
8982
LOG.error("Error closing DB. Container: " + containerDBPath, e);
83+
return false;
9084
}
85+
} else {
86+
return false;
9187
}
9288
}
9389

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.hadoop.ozone.container.common;
20+
21+
import org.apache.hadoop.fs.FileSystemTestHelper;
22+
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
23+
import org.apache.hadoop.ozone.OzoneConfigKeys;
24+
import org.apache.hadoop.ozone.container.common.utils.ContainerCache;
25+
import org.apache.hadoop.ozone.container.common.utils.ReferenceCountedDB;
26+
import org.apache.hadoop.utils.MetadataStore;
27+
import org.apache.hadoop.utils.MetadataStoreBuilder;
28+
import org.junit.Assert;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.junit.rules.ExpectedException;
32+
33+
import java.io.File;
34+
35+
36+
/**
37+
* Test ContainerCache with evictions.
38+
*/
39+
public class TestContainerCache {
40+
private static String testRoot = new FileSystemTestHelper().getTestRootDir();
41+
42+
@Rule
43+
public ExpectedException thrown = ExpectedException.none();
44+
45+
private void createContainerDB(OzoneConfiguration conf, File dbFile)
46+
throws Exception {
47+
MetadataStore store = MetadataStoreBuilder.newBuilder().setConf(conf)
48+
.setCreateIfMissing(true).setDbFile(dbFile).build();
49+
50+
// we close since the SCM pre-creates containers.
51+
// we will open and put Db handle into a cache when keys are being created
52+
// in a container.
53+
54+
store.close();
55+
}
56+
57+
@Test
58+
public void testContainerCacheEviction() throws Exception {
59+
File root = new File(testRoot);
60+
root.mkdirs();
61+
62+
OzoneConfiguration conf = new OzoneConfiguration();
63+
conf.setInt(OzoneConfigKeys.OZONE_CONTAINER_CACHE_SIZE, 2);
64+
65+
ContainerCache cache = ContainerCache.getInstance(conf);
66+
File containerDir1 = new File(root, "cont1");
67+
File containerDir2 = new File(root, "cont2");
68+
File containerDir3 = new File(root, "cont3");
69+
File containerDir4 = new File(root, "cont4");
70+
71+
72+
createContainerDB(conf, containerDir1);
73+
createContainerDB(conf, containerDir2);
74+
createContainerDB(conf, containerDir3);
75+
createContainerDB(conf, containerDir4);
76+
77+
// Get 2 references out of the same db and verify the objects are same.
78+
ReferenceCountedDB db1 = cache.getDB(1, "RocksDB",
79+
containerDir1.getPath(), conf);
80+
Assert.assertEquals(1, db1.getReferenceCount());
81+
ReferenceCountedDB db2 = cache.getDB(1, "RocksDB",
82+
containerDir1.getPath(), conf);
83+
Assert.assertEquals(2, db2.getReferenceCount());
84+
Assert.assertEquals(2, db1.getReferenceCount());
85+
Assert.assertEquals(db1, db2);
86+
87+
// add one more references to ContainerCache.
88+
ReferenceCountedDB db3 = cache.getDB(2, "RocksDB",
89+
containerDir2.getPath(), conf);
90+
Assert.assertEquals(1, db3.getReferenceCount());
91+
92+
// and close the reference
93+
db3.close();
94+
Assert.assertEquals(0, db3.getReferenceCount());
95+
96+
Assert.assertTrue(cache.isFull());
97+
98+
// add one more reference to ContainerCache and verify that it will not
99+
// evict the least recent entry as it has reference.
100+
ReferenceCountedDB db4 = cache.getDB(3, "RocksDB",
101+
containerDir3.getPath(), conf);
102+
Assert.assertEquals(1, db4.getReferenceCount());
103+
104+
Assert.assertEquals(2, cache.size());
105+
Assert.assertNotNull(cache.get(containerDir1.getPath()));
106+
Assert.assertNull(cache.get(containerDir2.getPath()));
107+
108+
// Now close both the references for container1
109+
db1.close();
110+
db2.close();
111+
Assert.assertEquals(0, db1.getReferenceCount());
112+
Assert.assertEquals(0, db2.getReferenceCount());
113+
114+
115+
// The reference count for container1 is 0 but it is not evicted.
116+
ReferenceCountedDB db5 = cache.getDB(1, "RocksDB",
117+
containerDir1.getPath(), conf);
118+
Assert.assertEquals(1, db5.getReferenceCount());
119+
Assert.assertEquals(db1, db5);
120+
db5.close();
121+
db4.close();
122+
123+
124+
// Decrementing reference count below zero should fail.
125+
thrown.expect(IllegalArgumentException.class);
126+
db5.close();
127+
}
128+
}

0 commit comments

Comments
 (0)