[SPARK-36667][SS][TEST] Close resources properly in StateStoreSuite/RocksDBStateStoreSuite

### What changes were proposed in this pull request?

This PR proposes to ensure StateStoreProvider instances are properly closed for each test in StateStoreSuite/RocksDBStateStoreSuite.

### Why are the changes needed?

While this doesn't break the test, this is a bad practice and may possibly make nasty problems in the future.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Existing UTs

Closes #33916 from HeartSaVioR/SPARK-36667.

Authored-by: Jungtaek Lim <kabhwan.opensource@gmail.com>
Signed-off-by: Liang-Chi Hsieh <viirya@gmail.com>
This commit is contained in:
Jungtaek Lim 2021-09-06 17:40:03 -07:00 committed by Liang-Chi Hsieh
parent 0ab0cb108d
commit 093c2080fe
2 changed files with 521 additions and 465 deletions

View file

@ -36,23 +36,34 @@ import org.apache.spark.util.Utils
class RocksDBStateStoreSuite extends StateStoreSuiteBase[RocksDBStateStoreProvider]
with BeforeAndAfter {
before {
StateStore.stop()
require(!StateStore.isMaintenanceRunning)
}
after {
StateStore.stop()
require(!StateStore.isMaintenanceRunning)
}
import StateStoreTestsHelper._
test("version encoding") {
import RocksDBStateStoreProvider._
val provider = newStoreProvider()
val store = provider.getStore(0)
val keyRow = dataToKeyRow("a", 0)
val valueRow = dataToValueRow(1)
store.put(keyRow, valueRow)
val iter = provider.rocksDB.iterator()
assert(iter.hasNext)
val kv = iter.next()
tryWithProviderResource(newStoreProvider()) { provider =>
val store = provider.getStore(0)
val keyRow = dataToKeyRow("a", 0)
val valueRow = dataToValueRow(1)
store.put(keyRow, valueRow)
val iter = provider.rocksDB.iterator()
assert(iter.hasNext)
val kv = iter.next()
// Verify the version encoded in first byte of the key and value byte arrays
assert(Platform.getByte(kv.key, Platform.BYTE_ARRAY_OFFSET) === STATE_ENCODING_VERSION)
assert(Platform.getByte(kv.value, Platform.BYTE_ARRAY_OFFSET) === STATE_ENCODING_VERSION)
// Verify the version encoded in first byte of the key and value byte arrays
assert(Platform.getByte(kv.key, Platform.BYTE_ARRAY_OFFSET) === STATE_ENCODING_VERSION)
assert(Platform.getByte(kv.value, Platform.BYTE_ARRAY_OFFSET) === STATE_ENCODING_VERSION)
}
}
test("RocksDB confs are passed correctly from SparkSession to db instance") {
@ -100,19 +111,20 @@ class RocksDBStateStoreSuite extends StateStoreSuiteBase[RocksDBStateStoreProvid
metricPair.get._2
}
val provider = newStoreProvider()
val store = provider.getStore(0)
// Verify state after updating
put(store, "a", 0, 1)
assert(get(store, "a", 0) === Some(1))
assert(store.commit() === 1)
assert(store.hasCommitted)
val storeMetrics = store.metrics
assert(storeMetrics.numKeys === 1)
assert(getCustomMetric(storeMetrics, CUSTOM_METRIC_FILES_COPIED) > 0L)
assert(getCustomMetric(storeMetrics, CUSTOM_METRIC_FILES_REUSED) == 0L)
assert(getCustomMetric(storeMetrics, CUSTOM_METRIC_BYTES_COPIED) > 0L)
assert(getCustomMetric(storeMetrics, CUSTOM_METRIC_ZIP_FILE_BYTES_UNCOMPRESSED) > 0L)
tryWithProviderResource(newStoreProvider()) { provider =>
val store = provider.getStore(0)
// Verify state after updating
put(store, "a", 0, 1)
assert(get(store, "a", 0) === Some(1))
assert(store.commit() === 1)
assert(store.hasCommitted)
val storeMetrics = store.metrics
assert(storeMetrics.numKeys === 1)
assert(getCustomMetric(storeMetrics, CUSTOM_METRIC_FILES_COPIED) > 0L)
assert(getCustomMetric(storeMetrics, CUSTOM_METRIC_FILES_REUSED) == 0L)
assert(getCustomMetric(storeMetrics, CUSTOM_METRIC_BYTES_COPIED) > 0L)
assert(getCustomMetric(storeMetrics, CUSTOM_METRIC_ZIP_FILE_BYTES_UNCOMPRESSED) > 0L)
}
}
override def newStoreProvider(): RocksDBStateStoreProvider = {
@ -145,9 +157,10 @@ class RocksDBStateStoreSuite extends StateStoreSuiteBase[RocksDBStateStoreProvid
override def getData(
provider: RocksDBStateStoreProvider,
version: Int = -1): Set[((String, Int), Int)] = {
val reloadedProvider = newStoreProvider(provider.stateStoreId)
val versionToRead = if (version < 0) reloadedProvider.latestVersion else version
reloadedProvider.getStore(versionToRead).iterator().map(rowPairToDataPair).toSet
tryWithProviderResource(newStoreProvider(provider.stateStoreId)) { reloadedProvider =>
val versionToRead = if (version < 0) reloadedProvider.latestVersion else version
reloadedProvider.getStore(versionToRead).iterator().map(rowPairToDataPair).toSet
}
}
override def newStoreProvider(