-
Notifications
You must be signed in to change notification settings - Fork 13.8k
[FLINK-6210] close RocksDB in ListViaMergeSpeedMiniBenchmark && ListV… #3652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,57 +56,68 @@ public static void main(String[] args) throws Exception { | |
|
|
||
| final String key = "key"; | ||
| final String value = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ7890654321"; | ||
|
|
||
| try { | ||
|
|
||
| final byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); | ||
| final byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8); | ||
| final byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); | ||
| final byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| final byte[] keyTemplate = Arrays.copyOf(keyBytes, keyBytes.length + 4); | ||
| final byte[] keyTemplate = Arrays.copyOf(keyBytes, keyBytes.length + 4); | ||
|
|
||
| final Unsafe unsafe = MemoryUtils.UNSAFE; | ||
| final long offset = unsafe.arrayBaseOffset(byte[].class) + keyTemplate.length - 4; | ||
| final Unsafe unsafe = MemoryUtils.UNSAFE; | ||
| final long offset = unsafe.arrayBaseOffset(byte[].class) + keyTemplate.length - 4; | ||
|
|
||
| final int num = 50000; | ||
| System.out.println("begin insert"); | ||
| final int num = 50000; | ||
| System.out.println("begin insert"); | ||
|
|
||
| final long beginInsert = System.nanoTime(); | ||
| for (int i = 0; i < num; i++) { | ||
| unsafe.putInt(keyTemplate, offset, i); | ||
| rocksDB.put(write_options, keyTemplate, valueBytes); | ||
| } | ||
| final long endInsert = System.nanoTime(); | ||
| System.out.println("end insert - duration: " + ((endInsert - beginInsert) / 1_000_000) + " ms"); | ||
|
|
||
| final byte[] resultHolder = new byte[num * valueBytes.length]; | ||
|
|
||
| final long beginGet = System.nanoTime(); | ||
|
|
||
| final RocksIterator iterator = rocksDB.newIterator(); | ||
| int pos = 0; | ||
|
|
||
| // seek to start | ||
| unsafe.putInt(keyTemplate, offset, 0); | ||
| iterator.seek(keyTemplate); | ||
|
|
||
| // mark end | ||
| unsafe.putInt(keyTemplate, offset, -1); | ||
|
|
||
| // iterate | ||
| while (iterator.isValid()) { | ||
| byte[] currKey = iterator.key(); | ||
| if (samePrefix(keyBytes, currKey)) { | ||
| byte[] currValue = iterator.value(); | ||
| System.arraycopy(currValue, 0, resultHolder, pos, currValue.length); | ||
| pos += currValue.length; | ||
| iterator.next(); | ||
| final long beginInsert = System.nanoTime(); | ||
| for (int i = 0; i < num; i++) { | ||
| unsafe.putInt(keyTemplate, offset, i); | ||
| rocksDB.put(write_options, keyTemplate, valueBytes); | ||
| } | ||
| else { | ||
| break; | ||
| final long endInsert = System.nanoTime(); | ||
| System.out.println("end insert - duration: " + ((endInsert - beginInsert) / 1_000_000) + " ms"); | ||
|
|
||
| final byte[] resultHolder = new byte[num * valueBytes.length]; | ||
|
|
||
| final long beginGet = System.nanoTime(); | ||
|
|
||
| final RocksIterator iterator = rocksDB.newIterator(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The iterator should be closed once it's not used. So it's better to use try-with-resources here. |
||
| int pos = 0; | ||
|
|
||
| try { | ||
| // seek to start | ||
| unsafe.putInt(keyTemplate, offset, 0); | ||
| iterator.seek(keyTemplate); | ||
|
|
||
| // mark end | ||
| unsafe.putInt(keyTemplate, offset, -1); | ||
|
|
||
| // iterate | ||
| while (iterator.isValid()) { | ||
| byte[] currKey = iterator.key(); | ||
| if (samePrefix(keyBytes, currKey)) { | ||
| byte[] currValue = iterator.value(); | ||
| System.arraycopy(currValue, 0, resultHolder, pos, currValue.length); | ||
| pos += currValue.length; | ||
| iterator.next(); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| }finally { | ||
| iterator.close(); | ||
| } | ||
| } | ||
|
|
||
| final long endGet = System.nanoTime(); | ||
| final long endGet = System.nanoTime(); | ||
|
|
||
| System.out.println("end get - duration: " + ((endGet - beginGet) / 1_000_000) + " ms"); | ||
| System.out.println("end get - duration: " + ((endGet - beginGet) / 1_000_000) + " ms"); | ||
| } finally { | ||
| rocksDB.close(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with the problem mentioned above, all RocksDB objects should be closed once they are not used. |
||
| options.close(); | ||
| write_options.close(); | ||
| FileUtils.deleteDirectory(rocksDir); | ||
| } | ||
| } | ||
|
|
||
| private static boolean samePrefix(byte[] prefix, byte[] key) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Besides
RocksDB,OptionsandWriteOptionsshould also be closed here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to delete the RocksDB's directory here.