shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

java.lang.NullPointerException: Cannot invoke "org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler.close()" because "this.proxyBackendHandler" is null

Open wsm12138 opened this issue 3 years ago • 4 comments

Bug Report

Which version of ShardingSphere did you use?

f306248c8d1d7e4b54ffec60eb4a125eb0f52119

Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

ShardingSphere-Proxy

Expected behavior

have no error

Actual behavior

[ERROR] 2022-12-21 18:42:07.195 [ShardingSphere-Command-183] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: Cannot invoke "org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler.close()" because "this.proxyBackendHandler" is null
        at org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute.MySQLComStmtExecuteExecutor.close(MySQLComStmtExecuteExecutor.java:130)
        at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.executeCommand(CommandExecutorTask.java:124)
        at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.run(CommandExecutorTask.java:78)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
[ERROR] 2022-12-21 18:42:07.195 [ShardingSphere-Command-75] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: Cannot invoke "org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler.close()" because "this.proxyBackendHandler" is null
        at org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute.MySQLComStmtExecuteExecutor.close(MySQLComStmtExecuteExecutor.java:130)
        at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.executeCommand(CommandExecutorTask.java:124)
        at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.run(CommandExecutorTask.java:78)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
[ERROR] 2022-12-21 18:42:07.195 [ShardingSphere-Command-164] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: Cannot invoke "org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler.close()" because "this.proxyBackendHandler" is null
        at org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute.MySQLComStmtExecuteExecutor.close(MySQLComStmtExecuteExecutor.java:130)
        at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.executeCommand(CommandExecutorTask.java:124)
        at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.run(CommandExecutorTask.java:78)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
[ERROR] 2022-12-21 18:42:07.195 [ShardingSphere-Command-157] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: Cannot invoke "org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler.close()" because "this.proxyBackendHandler" is null
        at org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute.MySQLComStmtExecuteExecutor.close(MySQLComStmtExecuteExecutor.java:130)

[ERROR] 2022-12-21 18:42:07.195 [ShardingSphere-Command-164] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: Cannot invoke "org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler.close()" because "this.proxyBackendHandler" is null
        at org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute.MySQLComStmtExecuteExecutor.close(MySQLComStmtExecuteExecutor.java:130)
        at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.executeCommand(CommandExecutorTask.java:124)
        at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.run(CommandExecutorTask.java:78)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
[ERROR] 2022-12-21 18:42:07.195 [ShardingSphere-Command-157] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: Cannot invoke "org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler.close()" because "this.proxyBackendHandler" is null
        at org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.execute.MySQLComStmtExecuteExecutor.close(MySQLComStmtExecuteExecutor.java:130)

...
[ERROR] 2022-12-21 18:42:07.199 [ShardingSphere-Command-192] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.199 [ShardingSphere-Command-161] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.199 [ShardingSphere-Command-155] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.199 [ShardingSphere-Command-75] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.199 [ShardingSphere-Command-75] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.199 [ShardingSphere-Command-192] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.199 [ShardingSphere-Command-75] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.199 [ShardingSphere-Command-147] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.200 [ShardingSphere-Command-189] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.200 [ShardingSphere-Command-154] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.202 [ShardingSphere-Command-189] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.202 [ShardingSphere-Command-163] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.202 [ShardingSphere-Command-155] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.202 [ShardingSphere-Command-196] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.202 [ShardingSphere-Command-161] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.203 [ShardingSphere-Command-151] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.203 [ShardingSphere-Command-196] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.203 [ShardingSphere-Command-189] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.203 [ShardingSphere-Command-195] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null
[ERROR] 2022-12-21 18:42:07.203 [ShardingSphere-Command-132] o.a.s.p.f.c.CommandExecutorTask - Exception occur:
java.lang.NullPointerException: null

Reason analyze (If you can)

can't

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

  1. start proxy
  2. config proxy
databaseName: sharding_db
dataSources:
  ds_0:
    password: 
    url: jdbc:mysql://192.168.10.23:13333/test?serverTimezone=UTC&useSSL=false
    username: root
    minPoolSize: 1
    connectionTimeoutMilliseconds: 30000
    maxLifetimeMilliseconds: 2100000
    readOnly: false
    idleTimeoutMilliseconds: 30000
    maxPoolSize: 50
rules:
  1. use sysbench threads=40
sysbench oltp_read_write --mysql-host=192.168.10.23 --mysql-port=6666 --mysql-user=root --mysql-password=root --mysql-db=sharding_db --tables=1 --table-size=1000 --report-interval=2 --time=1600 --threads=40 --max-requests=0 --mysql-ignore-errors=all --rand-type=uniform --range_selects=off --auto_inc=off run
  1. proxy execute show processlist; count 40 image

  2. proxy execute alter resource

ALTER STORAGE UNIT ds_0 (
    URL="jdbc:mysql://192.168.10.23:13333/test?serverTimezone=UTC&useSSL=false",
    USER="root",
    PASSWORD="",
    PROPERTIES("maximumPoolSize"="10","idleTimeout"="30000")
);
  1. proxy execute show processlist; count 40 image

  2. cat stdout.log

image
  1. ALTER STORAGE UNIT ds_0 ( URL="jdbc:mysql://192.168.10.23:13333/test?serverTimezone=UTC&useSSL=false", USER="root", PASSWORD="", PROPERTIES("maximumPoolSize"="50","idleTimeout"="30000") ); image

  2. cat stdout.log image

sysbench resoult

image image

Example codes for reproduce this issue (such as a github link).

wsm12138 avatar Dec 21 '22 11:12 wsm12138

If the ALTER STORAGE UNIT will close DataSource and reopen a new one, then this operation is not recommended to be executed in busy time, which would be devastating for a business. Maybe we could do some enhancements such as:

  • avoid the DistSQL to be execute in busy time.
  • Or we could only change the field maximumPoolSize of the origin data source, which is allowed in HikariCP.

TeslaCN avatar Dec 26 '22 02:12 TeslaCN

@TeslaCN @RaigorJiang I tried taking a stab at the issue. I think it is because of the delayed initialisation of the proxyBackendHandler in the MySQLComStmtExecuteExecutor. The proxyBackendHandler variable is initialised only in the execute() method and if any exception is thrown before the initialisation, the close method will be called in the CommandExecutorTask. We might have to check for the existence of the proxyHandler instance before invoking close.

vikiuvb avatar Jan 13 '23 11:01 vikiuvb

@TeslaCN @RaigorJiang I tried taking a stab at the issue. I think it is because of the delayed initialisation of the proxyBackendHandler in the MySQLComStmtExecuteExecutor. The proxyBackendHandler variable is initialised only in the execute() method and if any exception is thrown before the initialisation, the close method will be called in the CommandExecutorTask. We might have to check for the existence of the proxyHandler instance before invoking close.

I don't think this is the root cause of the error. But it could be an enhancement. Would you like to submit a PR to eliminate the NPE?

TeslaCN avatar Jan 16 '23 01:01 TeslaCN

maybe we can only change the field maximumPoolSize of the origin data source

@RaigorJiang

wsm12138 avatar Aug 12 '24 02:08 wsm12138