Skip to content

Tune the databases closure #5428

Closed
@halibobo1205

Description

@halibobo1205

Rationale

Databases are basic, so they are always loaded first and closed last to avoid using unloaded or closed databases.
The spring dependency injection ensures that the databases are always loaded first.
However, the databases are closed by the ChainBaseManager.closeAllStore, there may be some threads accessing the closed database at kill -15.

Implementation

Do you have ideas regarding the implementation of this feature?

Database beans's shutdown changed to be managed by the Spring IoC container, not by ChainBaseManager.
As documented in Bean.destroyMethod:

public abstract String destroyMethod
The optional name of a method to call on the bean instance upon closing the application context, for example a close() method on a JDBC DataSource implementation, or a Hibernate SessionFactory object. The method must have no arguments but may throw any exception.
As a convenience to the user, the container will attempt to infer a destroy method against an object returned from the @bean method. For example, given an @bean method returning an Apache Commons DBCP BasicDataSource, the container will notice the close() method available on that object and automatically register it as the destroyMethod. This 'destroy method inference' is currently limited to detecting only public, no-arg methods named 'close' or 'shutdown'. The method may be declared at any level of the inheritance hierarchy and will be detected regardless of the return type of the @bean method (i.e., detection occurs reflectively against the bean instance itself at creation time).
To disable destroy method inference for a particular @bean, specify an empty string as the value, e.g. @bean(destroyMethod=""). Note that the DisposableBean callback interface will nevertheless get detected and the corresponding destroy method invoked: In other words, destroyMethod="" only affects custom close/shutdown methods and Closeable/AutoCloseable declared close methods.
Note: Only invoked on beans whose lifecycle is under the full control of the factory, which is always the case for singletons but not guaranteed for any other scope.
See Also:
DisposableBean, ConfigurableApplicationContext.close()
Default:
"(inferred)"

According to DisposableBeanAdapter.inferDestroyMethodIfNecessary:

If the current value of the given beanDefinition's "destroyMethodName" property is AbstractBeanDefinition.INFER_METHOD, then attempt to infer a destroy method. Candidate methods are currently limited to public, no-arg methods named "close" or "shutdown" (whether declared locally or inherited). The given BeanDefinition's "destroyMethodName" is updated to be null if no such method is found, otherwise set to the name of the inferred method. This constant serves as the default for the @bean#destroyMethod attribute and the value of the constant may also be used in XML within the or attributes.
Also processes the java.io.Closeable and AutoCloseable interfaces, reflectively calling the "close" method on implementing beans as well.

A list of known beans that trigger the destroyMethod.

bean type trigger type note
1 checkTmpStore db AutoCloseable close db
2 revokingDatabase revoking db "(inferred)"-> close() close thread pool
3 accountStateStoreTrie db AutoCloseable close db
4 balanceTraceStore db AutoCloseable close db
5 accountTraceStore db AutoCloseable close db
6 dynamicPropertiesStore db AutoCloseable close db
7 accountStore db AutoCloseable close db
8 accountAssetStore db AutoCloseable close db
9 blockStore db AutoCloseable close db
10 witnessStore db AutoCloseable close db
11 assetIssueStore db AutoCloseable close db
12 assetIssueV2Store db AutoCloseable close db
13 blockIndexStore db AutoCloseable close db
14 accountIdIndexStore db AutoCloseable close db
15 accountIndexStore db AutoCloseable close db
16 witnessScheduleStore db AutoCloseable close db
17 votesStore db AutoCloseable close db
18 proposalStore db AutoCloseable close db
19 exchangeStore db AutoCloseable close db
20 exchangeV2Store db AutoCloseable close db
21 marketAccountStore db AutoCloseable close db
22 marketOrderStore db AutoCloseable close db
23 marketPairPriceToOrderStore db AutoCloseable close db
24 marketPairToPriceStore db AutoCloseable close db
25 abiStore db AutoCloseable close db
26 codeStore db AutoCloseable close db
27 contractStore db AutoCloseable close db
28 contractStateStore db AutoCloseable close db
29 delegatedResourceStore db AutoCloseable close db
30 delegatedResourceAccountIndexStore db AutoCloseable close db
31 storageRowStore db AutoCloseable close db
32 nullifierStore db AutoCloseable close db
33 ZKProofStore db AutoCloseable close db
34 incrementalMerkleTreeStore db AutoCloseable close db
35 delegationStore db AutoCloseable close db
36 khaosDatabase db AutoCloseable close db
37 commonStore db AutoCloseable close db
38 transactionStore db AutoCloseable close db
39 transactionRetStore db AutoCloseable close db
40 recentBlockStore db AutoCloseable close db
41 recentTransactionStore db AutoCloseable close db
42 transactionHistoryStore db AutoCloseable close db
43 commonDataBase db AutoCloseable close db
44 pbftSignDataStore db AutoCloseable close db
45 treeBlockIndexStore db AutoCloseable close db
46 sectionBloomStore db AutoCloseable close db
47 pbftMessageHandle pbft message Common Annotations @PreDestroy close timer
48 transactionCache db AutoCloseable close db
49 tronNetDelegate block sync Common Annotations @PreDestroy close thread

Are you willing to implement this feature?

Yes

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions