-
Notifications
You must be signed in to change notification settings - Fork 29.4k
[SPARK-59168][CORE] Avoid NoSuchElementException on missing keys in KVStore reads and writes #58466
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
base: master
Are you sure you want to change the base?
Changes from all commits
b7ed268
71ecbd3
f7a120d
db615c0
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 |
|---|---|---|
|
|
@@ -131,24 +131,16 @@ public RocksDB(File path, KVStoreSerializer serializer) throws Exception { | |
| db().put(STORE_VERSION_KEY, serializer.serialize(STORE_VERSION)); | ||
| } | ||
|
|
||
| Map<String, byte[]> aliases; | ||
| try { | ||
| aliases = get(TYPE_ALIASES_KEY, TypeAliases.class).aliases; | ||
| } catch (NoSuchElementException e) { | ||
| aliases = new HashMap<>(); | ||
| } | ||
| TypeAliases aliasesValue = getOrNull(TYPE_ALIASES_KEY, TypeAliases.class); | ||
| Map<String, byte[]> aliases = aliasesValue != null ? aliasesValue.aliases : new HashMap<>(); | ||
| typeAliases = new ConcurrentHashMap<>(aliases); | ||
|
|
||
| iteratorTracker = new ConcurrentLinkedQueue<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public <T> T getMetadata(Class<T> klass) throws Exception { | ||
| try { | ||
| return get(METADATA_KEY, klass); | ||
| } catch (NoSuchElementException nsee) { | ||
| return null; | ||
| } | ||
| return getOrNull(METADATA_KEY, klass); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -161,11 +153,21 @@ public void setMetadata(Object value) throws Exception { | |
| } | ||
|
|
||
| <T> T get(byte[] key, Class<T> klass) throws Exception { | ||
| byte[] data = db().get(key); | ||
| if (data == null) { | ||
| T value = getOrNull(key, klass); | ||
| if (value == null) { | ||
| throw new NoSuchElementException(new String(key, UTF_8)); | ||
| } | ||
| return serializer.deserialize(data, klass); | ||
| return value; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value for the given key, or {@code null} if the key is not present, so that | ||
| * callers where a missing key is expected do not pay the cost of throwing and filling in an | ||
| * exception stack trace. | ||
| */ | ||
| <T> T getOrNull(byte[] key, Class<T> klass) throws Exception { | ||
| byte[] data = db().get(key); | ||
| return data != null ? serializer.deserialize(data, klass) : null; | ||
| } | ||
|
|
||
| private void put(byte[] key, Object value) throws Exception { | ||
|
|
@@ -239,12 +241,7 @@ private void updateBatch( | |
| Class<?> klass, | ||
| RocksDBTypeInfo.Index naturalIndex, | ||
| Collection<RocksDBTypeInfo.Index> indices) throws Exception { | ||
| Object existing; | ||
| try { | ||
| existing = get(naturalIndex.entityKey(null, value), klass); | ||
| } catch (NoSuchElementException e) { | ||
| existing = null; | ||
| } | ||
| Object existing = getOrNull(naturalIndex.entityKey(null, value), klass); | ||
|
|
||
| PrefixCache cache = new PrefixCache(value); | ||
| byte[] naturalKey = naturalIndex.toKey(naturalIndex.getValue(value)); | ||
|
|
@@ -261,9 +258,8 @@ public void delete(Class<?> type, Object naturalKey) throws Exception { | |
| RocksDBTypeInfo ti = getTypeInfo(type); | ||
| byte[] key = ti.naturalIndex().start(null, naturalKey); | ||
| synchronized (ti) { | ||
| byte[] data = db().get(key); | ||
| if (data != null) { | ||
| Object existing = serializer.deserialize(data, type); | ||
| Object existing = getOrNull(key, type); | ||
| if (existing != null) { | ||
| PrefixCache cache = new PrefixCache(existing); | ||
| byte[] keyBytes = ti.naturalIndex().toKey(ti.naturalIndex().getValue(existing)); | ||
| for (RocksDBTypeInfo.Index idx : ti.indices()) { | ||
|
|
@@ -272,8 +268,6 @@ public void delete(Class<?> type, Object naturalKey) throws Exception { | |
| db().write(writeOptions, writeBatch); | ||
|
Member
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. Same as
Contributor
Author
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. Same, done in 1330280. |
||
| } | ||
| } | ||
| } catch (NoSuchElementException nse) { | ||
| // Ignore. | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,32 @@ public void testObjectWriteReadDelete() throws Exception { | |
| assertEquals(0, countKeys(t.getClass())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetOrNullMissingKey() throws Exception { | ||
|
Member
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. Only the assertThrows(NoSuchElementException.class, () -> db.read(CustomType1.class, "missing"));
Contributor
Author
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. Done in 1330280: only the |
||
| // getOrNull() returns null for a missing key so expected misses (e.g. the write path | ||
| // looking up an existing entry) skip the cost of building an exception, while read() | ||
| // still surfaces a missing key as NoSuchElementException. | ||
| byte[] missingKey = db.getTypeInfo(CustomType1.class).naturalIndex().start(null, "missing"); | ||
| assertNull(db.getOrNull(missingKey, CustomType1.class)); | ||
| assertThrows(NoSuchElementException.class, () -> db.read(CustomType1.class, "missing")); | ||
|
|
||
| CustomType1 t = createCustomType1(1); | ||
| db.write(t); | ||
| assertEquals(t, db.read(CustomType1.class, t.key)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDeleteEdgeCases() throws Exception { | ||
|
Member
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. Just noting for the record: all three cases short-circuit at the pre-existing
Contributor
Author
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. Kept as-is; it now documents the same no-op contract the corrected |
||
| // Never-written type: type info is created on the fly, lookup misses, nothing happens. | ||
| db.delete(CustomType1.class, "missing"); | ||
| assertEquals(0L, db.count(CustomType1.class)); | ||
|
|
||
| // Never-written key of a written type. | ||
| db.write(createCustomType1(1)); | ||
| db.delete(CustomType1.class, "missing"); | ||
| assertEquals(1L, db.count(CustomType1.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMultipleObjectWriteReadDelete() throws Exception { | ||
| CustomType1 t1 = createCustomType1(1); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,6 +95,32 @@ public void testObjectWriteReadDelete() throws Exception { | |
| assertEquals(0, countKeys(t.getClass())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetOrNullMissingKey() throws Exception { | ||
|
Member
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. Same as
Contributor
Author
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. Same, done in 1330280. |
||
| // getOrNull() returns null for a missing key so expected misses (e.g. the write path | ||
| // looking up an existing entry) skip the cost of building an exception, while read() | ||
| // still surfaces a missing key as NoSuchElementException. | ||
| byte[] missingKey = db.getTypeInfo(CustomType1.class).naturalIndex().start(null, "missing"); | ||
| assertNull(db.getOrNull(missingKey, CustomType1.class)); | ||
| assertThrows(NoSuchElementException.class, () -> db.read(CustomType1.class, "missing")); | ||
|
|
||
| CustomType1 t = createCustomType1(1); | ||
| db.write(t); | ||
| assertEquals(t, db.read(CustomType1.class, t.key)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDeleteEdgeCases() throws Exception { | ||
| // Never-written type: type info is created on the fly, lookup misses, nothing happens. | ||
| db.delete(CustomType1.class, "missing"); | ||
| assertEquals(0L, db.count(CustomType1.class)); | ||
|
|
||
| // Never-written key of a written type. | ||
| db.write(createCustomType1(1)); | ||
| db.delete(CustomType1.class, "missing"); | ||
| assertEquals(1L, db.count(CustomType1.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMultipleObjectWriteReadDelete() throws Exception { | ||
| CustomType1 t1 = createCustomType1(1); | ||
|
|
||
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.
delete()(a few lines above, atbyte[] data = db().get(key); if (data != null) { Object existing = serializer.deserialize(data, type); ... }) is exactly the body of the newgetOrNull. Since this PR already touchesdelete(), how aboutso the read-or-null sequence has a single home per store? It is behavior-identical (same
synchronized (ti)block; theClass<?>capture already compiles the same way inupdateBatch).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.
Done in 1330280:
Object existing = getOrNull(key, type).