@ Override public void dispose ( ) { if ( this . disposed ) { return ; } super . dispose ( ) ; // This call will block until all clients that still acquire access to the RocksDB instance have released it, // so that we cannot release the native resources while clients are still working with it in parallel. rocksDBResourceGuard . close ( ) ; // IMPORTANT: null reference to signal potential async checkpoint workers that the db was disposed, as // working on the disposed object results in SEGFAULTS. if ( db != null ) { IOUtils . closeQuietly ( writeBatchWrapper ) ; // Metric collection occurs on a background thread. When this method returns // it is guaranteed that thr RocksDB reference has been invalidated // and no more metric collection will be attempted against the database. if ( nativeMetricMonitor != null ) { nativeMetricMonitor . close ( ) ; } List < ColumnFamilyOptions > columnFamilyOptions = new ArrayList <> ( kvStateInformation . values ( ) . size ( ) ) ; // RocksDB's native memory management requires that *all* CFs (including default) are closed before the // DB is closed. See: // https://github.com/facebook/rocksdb/wiki/RocksJava-Basics#opening-a-database-with-column-families // Start with default CF ... RocksDBOperationUtils . addColumnFamilyOptionsToCloseLater ( columnFamilyOptions , defaultColumnFamily ) ; IOUtils . closeQuietly ( defaultColumnFamily ) ; // ... continue with the ones created by Flink... for ( RocksDbKvStateInfo kvStateInfo : kvStateInformation . values ( ) ) { RocksDBOperationUtils . addColumnFamilyOptionsToCloseLater ( columnFamilyOptions , kvStateInfo . columnFamilyHandle ) ; IOUtils . closeQuietly ( kvStateInfo . columnFamilyHandle ) ; } // ... and finally close the DB instance ... IOUtils . closeQuietly ( db ) ; columnFamilyOptions . forEach ( IOUtils :: closeQuietly ) ; IOUtils . closeQuietly ( dbOptions ) ; IOUtils . closeQuietly ( writeOptions ) ; ttlCompactFiltersManager . disposeAndClearRegisteredCompactionFactories ( ) ; kvStateInformation . clear ( ) ; cleanInstanceBasePath ( ) ; } this . disposed = true ; }
Should only be called by one thread and only after all accesses to the DB happened .