Repository: alibaba/Sentinel

Issue #1724: NPE when update Gateway Flow Rule before the api has been requested once
Created: 2020-09-10T10:05:23Z
Issue Description
Type: bug report
Describe what happened (or what feature you want)
When my program update the rule whith :
GatewayRuleManager.loadRules(new HashSet<>(FLOW_RULES.values()));
if there has one or more Api has not been requested ,
then there the api resource has not related metric.
that will throws a NPE:
java.lang.NullPointerException: null
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager$GatewayRulePropertyListener.applyToConvertedParamMap(GatewayRuleManager.java:230) ~[sentinel-api-gateway-adapter-common-1.8.0.jar:na]
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager$GatewayRulePropertyListener.applyGatewayRuleInternal(GatewayRuleManager.java:200) ~[sentinel-api-gateway-adapter-common-1.8.0.jar:na]
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager$GatewayRulePropertyListener.configUpdate(GatewayRuleManager.java:118) ~[sentinel-api-gateway-adapter-common-1.8.0.jar:na]
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager$GatewayRulePropertyListener.configUpdate(GatewayRuleManager.java:114) ~[sentinel-api-gateway-adapter-common-1.8.0.jar:na]
	at com.alibaba.csp.sentinel.property.DynamicSentinelProperty.updateValue(DynamicSentinelProperty.java:57) ~[sentinel-core-1.8.0.jar:1.8.0]
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager.loadRules(GatewayRuleManager.java:78) ~[sentinel-api-gateway-adapter-common-1.8.0.jar:na]
	at ****.java:50) ~[classes/:na]
	at ****.java:102) ~[classes/:na]
	at ****.java:52) ~[classes/:na]
	at sun.reflect.GeneratedMethodAccessor81.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_201]
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [na:1.8.0_201]
	at java.util.concurrent.FutureTask.run(FutureTask.java) [na:1.8.0_201]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_201]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_201]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_201]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_201]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]

Because there has not metric for this resource , but the method:
com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager.GatewayRulePropertyListener private void applyToConvertedParamMap(@NotNull Set<ParamFlowRule> paramFlowRules)
try to clear that with :
ParameterMetricStorage.getParamMetricForResource(resource).clearForRule(rule);
in com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager.GatewayRulePropertyListener
(233 line in com/alibaba/csp/sentinel/adapter/gateway/common/rule/GatewayRuleManager.java)
Describe what you expected to happen
How to reproduce it (as minimally and precisely as possible)

init a api resource with gateway rule manager
update that before that has been requested

Tell us your environment
Sentinel Version : 1.8
Anything else we need to know?

Duplicate issue #1809: sentinel-dashboard-1.8版本网关控制台修改流控规则失败
Created: 2020-10-21T13:05:40Z
环境：
<java.version>1.8</java.version>
<springboot.version>2.1.1.RELEASE</springboot.version>
<springcloud.version>Greenwich.RELEASE</springcloud.version>
pom：
<dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.8.0</version>
        </dependency>
gateway启动参数：
java -Dspring.profiles.active=local  -Dcsp.sentinel.app.type=1 -Dcsp.sentinel.dashboard.server=localhost:8098 -Dproject.name=zjyz-gateway -Dcsp.sentinel.heartbeat.client.ip=192.168.8.42
-jar zjyz-gateway.jar
现象：
修改网关流控规则失败
网关报错信息：
java.lang.NullPointerException
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager$GatewayRulePropertyListener.applyToConvertedParamMap(GatewayRuleManager.java:230)
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager$GatewayRulePropertyListener.applyGatewayRuleInternal(GatewayRuleManager.java:200)
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager$GatewayRulePropertyListener.configUpdate(GatewayRuleManager.java:118)
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager$GatewayRulePropertyListener.configUpdate(GatewayRuleManager.java:114)
	at com.alibaba.csp.sentinel.property.DynamicSentinelProperty.updateValue(DynamicSentinelProperty.java:57)
	at com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager.loadRules(GatewayRuleManager.java:78)
	at com.alibaba.csp.sentinel.adapter.gateway.common.command.UpdateGatewayRuleCommandHandler.handle(UpdateGatewayRuleCommandHandler.java:59)
	at com.alibaba.csp.sentinel.transport.command.http.HttpEventTask.run(HttpEventTask.java:103)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
	at java.util.concurrent.FutureTask.run(FutureTask.java)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

通过代码跟踪发现ParameterMetricStorage中的metricsMap为空：

Closing comment (2020-10-21T16:50:04Z): Maybe related to #1724