Skip to content

Commit bb5aa56

Browse files
committed
Tests
1 parent 2d7df13 commit bb5aa56

1 file changed

Lines changed: 220 additions & 0 deletions

File tree

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under both the GPLv2 (found in the
3+
// COPYING file in the root directory) and Apache 2.0 License
4+
// (found in the LICENSE.Apache file in the root directory).
5+
6+
package org.rocksdb;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
10+
11+
import org.junit.ClassRule;
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.junit.rules.TemporaryFolder;
15+
16+
public class WriteBufferManagerTest {
17+
@ClassRule
18+
public static final RocksNativeLibraryResource ROCKS_NATIVE_LIBRARY_RESOURCE =
19+
new RocksNativeLibraryResource();
20+
21+
@Rule public TemporaryFolder dbFolder = new TemporaryFolder();
22+
23+
@Test
24+
public void construct() throws RocksDBException {
25+
try (final Cache cache = new LRUCache(1024 * 1024);
26+
final WriteBufferManager wbm = new WriteBufferManager(2000L, cache)) {
27+
assertThat(wbm).isNotNull();
28+
}
29+
}
30+
31+
@Test
32+
public void constructWithAllowStall() throws RocksDBException {
33+
try (final Cache cache = new LRUCache(1024 * 1024);
34+
final WriteBufferManager wbm = new WriteBufferManager(2000L, cache, true)) {
35+
assertThat(wbm).isNotNull();
36+
assertThat(wbm.allowStall()).isTrue();
37+
}
38+
}
39+
40+
@Test
41+
public void enabled() throws RocksDBException {
42+
try (final Cache cache = new LRUCache(1024 * 1024);
43+
final WriteBufferManager wbm = new WriteBufferManager(2000L, cache)) {
44+
assertThat(wbm.enabled()).isTrue();
45+
}
46+
}
47+
48+
@Test
49+
public void enabledWithZeroSize() throws RocksDBException {
50+
try (final Cache cache = new LRUCache(1024 * 1024);
51+
final WriteBufferManager wbm = new WriteBufferManager(0L, cache)) {
52+
assertThat(wbm.enabled()).isFalse();
53+
}
54+
}
55+
56+
@Test
57+
public void costToCache() throws RocksDBException {
58+
try (final Cache cache = new LRUCache(1024 * 1024);
59+
final WriteBufferManager wbm = new WriteBufferManager(2000L, cache)) {
60+
assertThat(wbm.costToCache()).isTrue();
61+
}
62+
}
63+
64+
@Test
65+
public void memoryUsage() throws RocksDBException {
66+
try (final Cache cache = new LRUCache(1024 * 1024);
67+
final WriteBufferManager wbm = new WriteBufferManager(10 * 1024 * 1024, cache)) {
68+
assertThat(wbm.memoryUsage()).isEqualTo(0L);
69+
}
70+
}
71+
72+
@Test
73+
public void mutableMemtableMemoryUsage() throws RocksDBException {
74+
try (final Cache cache = new LRUCache(1024 * 1024);
75+
final WriteBufferManager wbm = new WriteBufferManager(10 * 1024 * 1024, cache)) {
76+
assertThat(wbm.mutableMemtableMemoryUsage()).isEqualTo(0L);
77+
}
78+
}
79+
80+
@Test
81+
public void dummyEntriesInCacheUsage() throws RocksDBException {
82+
try (final Cache cache = new LRUCache(1024 * 1024);
83+
final WriteBufferManager wbm = new WriteBufferManager(10 * 1024 * 1024, cache)) {
84+
assertThat(wbm.dummyEntriesInCacheUsage()).isEqualTo(0L);
85+
}
86+
}
87+
88+
@Test
89+
public void bufferSize() throws RocksDBException {
90+
final long expectedSize = 2000L;
91+
try (final Cache cache = new LRUCache(1024 * 1024);
92+
final WriteBufferManager wbm = new WriteBufferManager(expectedSize, cache)) {
93+
assertThat(wbm.bufferSize()).isEqualTo(expectedSize);
94+
}
95+
}
96+
97+
@Test
98+
public void setBufferSize() throws RocksDBException {
99+
final long initialSize = 2000L;
100+
final long newSize = 4000L;
101+
try (final Cache cache = new LRUCache(1024 * 1024);
102+
final WriteBufferManager wbm = new WriteBufferManager(initialSize, cache)) {
103+
assertThat(wbm.bufferSize()).isEqualTo(initialSize);
104+
wbm.setBufferSize(newSize);
105+
assertThat(wbm.bufferSize()).isEqualTo(newSize);
106+
}
107+
}
108+
109+
@Test
110+
public void setBufferSizeInvalid() throws RocksDBException {
111+
try (final Cache cache = new LRUCache(1024 * 1024);
112+
final WriteBufferManager wbm = new WriteBufferManager(2000L, cache)) {
113+
assertThatThrownBy(() -> wbm.setBufferSize(0L))
114+
.isInstanceOf(IllegalArgumentException.class)
115+
.hasMessageContaining("Buffer size must be greater than 0");
116+
assertThatThrownBy(() -> wbm.setBufferSize(-100L))
117+
.isInstanceOf(IllegalArgumentException.class)
118+
.hasMessageContaining("Buffer size must be greater than 0");
119+
}
120+
}
121+
122+
@Test
123+
public void setAllowStall() throws RocksDBException {
124+
try (final Cache cache = new LRUCache(1024 * 1024);
125+
final WriteBufferManager wbm = new WriteBufferManager(2000L, cache, false)) {
126+
assertThat(wbm.allowStall()).isFalse();
127+
wbm.setAllowStall(true);
128+
assertThat(wbm.allowStall()).isTrue();
129+
wbm.setAllowStall(false);
130+
assertThat(wbm.allowStall()).isFalse();
131+
}
132+
}
133+
134+
@Test
135+
public void isStallActive() throws RocksDBException {
136+
try (final Cache cache = new LRUCache(1024 * 1024);
137+
final WriteBufferManager wbm = new WriteBufferManager(2000L, cache)) {
138+
assertThat(wbm.isStallActive()).isFalse();
139+
}
140+
}
141+
142+
@Test
143+
public void isStallThresholdExceeded() throws RocksDBException {
144+
try (final Cache cache = new LRUCache(1024 * 1024);
145+
final WriteBufferManager wbm = new WriteBufferManager(2000L, cache)) {
146+
assertThat(wbm.isStallThresholdExceeded()).isFalse();
147+
}
148+
}
149+
150+
@Test
151+
public void integrationWithDB() throws RocksDBException {
152+
try (final Cache cache = new LRUCache(8 * 1024 * 1024);
153+
final WriteBufferManager wbm = new WriteBufferManager(8 * 1024 * 1024, cache, false);
154+
final Options options = new Options()
155+
.setCreateIfMissing(true)
156+
.setWriteBufferSize(1024 * 1024)
157+
.setWriteBufferManager(wbm);
158+
final RocksDB db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath())) {
159+
160+
// Memory usage may be non-zero due to memtable initialization
161+
final long initialMemory = wbm.memoryUsage();
162+
final long initialMutable = wbm.mutableMemtableMemoryUsage();
163+
164+
// Write some data
165+
final byte[] value = new byte[1024];
166+
for (int i = 0; i < 100; i++) {
167+
db.put(("testkey" + i).getBytes(), value);
168+
}
169+
170+
// Memory usage should increase or stay the same
171+
assertThat(wbm.memoryUsage()).isGreaterThanOrEqualTo(initialMemory);
172+
assertThat(wbm.mutableMemtableMemoryUsage()).isGreaterThanOrEqualTo(initialMutable);
173+
174+
// Should not exceed buffer size
175+
assertThat(wbm.memoryUsage()).isLessThanOrEqualTo(wbm.bufferSize());
176+
}
177+
}
178+
179+
@Test
180+
public void multipleColumnFamilies() throws RocksDBException {
181+
try (final Cache cache = new LRUCache(16 * 1024 * 1024);
182+
final WriteBufferManager wbm = new WriteBufferManager(16 * 1024 * 1024, cache);
183+
final DBOptions dbOptions = new DBOptions()
184+
.setCreateIfMissing(true)
185+
.setCreateMissingColumnFamilies(true)
186+
.setWriteBufferManager(wbm);
187+
final ColumnFamilyOptions cfOpts = new ColumnFamilyOptions()
188+
.setWriteBufferSize(1024 * 1024)) {
189+
190+
final java.util.List<ColumnFamilyDescriptor> cfDescriptors = new java.util.ArrayList<>();
191+
cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, cfOpts));
192+
cfDescriptors.add(new ColumnFamilyDescriptor("cf1".getBytes(), cfOpts));
193+
194+
final java.util.List<ColumnFamilyHandle> cfHandles = new java.util.ArrayList<>();
195+
196+
try (final RocksDB db = RocksDB.open(dbOptions, dbFolder.getRoot().getAbsolutePath(),
197+
cfDescriptors, cfHandles)) {
198+
199+
// Memory is shared across column families
200+
assertThat(wbm.enabled()).isTrue();
201+
assertThat(wbm.bufferSize()).isEqualTo(16 * 1024 * 1024);
202+
203+
// Write to both column families
204+
final byte[] value = new byte[1024];
205+
for (int i = 0; i < 50; i++) {
206+
db.put(cfHandles.get(0), ("key_default_" + i).getBytes(), value);
207+
db.put(cfHandles.get(1), ("key_cf1_" + i).getBytes(), value);
208+
}
209+
210+
// Total memory should be tracked
211+
assertThat(wbm.memoryUsage()).isGreaterThan(0L);
212+
213+
} finally {
214+
for (final ColumnFamilyHandle handle : cfHandles) {
215+
handle.close();
216+
}
217+
}
218+
}
219+
}
220+
}

0 commit comments

Comments
 (0)