diff --git a/polaris-discovery/polaris-discovery-client/src/main/java/com/tencent/polaris/discovery/client/util/Validator.java b/polaris-discovery/polaris-discovery-client/src/main/java/com/tencent/polaris/discovery/client/util/Validator.java index 5d2f20645..c686db364 100644 --- a/polaris-discovery/polaris-discovery-client/src/main/java/com/tencent/polaris/discovery/client/util/Validator.java +++ b/polaris-discovery/polaris-discovery-client/src/main/java/com/tencent/polaris/discovery/client/util/Validator.java @@ -24,25 +24,13 @@ import com.tencent.polaris.api.pojo.RetStatus; import com.tencent.polaris.api.pojo.ServiceEventKey; import com.tencent.polaris.api.pojo.ServiceKey; -import com.tencent.polaris.api.rpc.BaseEntity; -import com.tencent.polaris.api.rpc.GetAllInstancesRequest; -import com.tencent.polaris.api.rpc.GetHealthyInstancesRequest; -import com.tencent.polaris.api.rpc.GetInstancesRequest; -import com.tencent.polaris.api.rpc.GetOneInstanceRequest; -import com.tencent.polaris.api.rpc.GetResourcesRequest; -import com.tencent.polaris.api.rpc.GetServiceContractRequest; -import com.tencent.polaris.api.rpc.GetServiceRuleRequest; -import com.tencent.polaris.api.rpc.InstanceDeregisterRequest; -import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest; -import com.tencent.polaris.api.rpc.InstanceRegisterRequest; -import com.tencent.polaris.api.rpc.ServiceCallResult; -import com.tencent.polaris.api.rpc.UnWatchServiceRequest; -import com.tencent.polaris.api.rpc.WatchServiceRequest; +import com.tencent.polaris.api.rpc.*; import com.tencent.polaris.api.utils.CollectionUtils; import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.client.util.CommonValidator; +import com.tencent.polaris.logging.LoggerFactory; +import org.slf4j.Logger; -import java.util.Objects; import java.util.Set; /** @@ -53,6 +41,8 @@ */ public class Validator { + private static final Logger LOG = LoggerFactory.getLogger(Validator.class); + private static final int MAX_PORT = 65536; /** @@ -164,7 +154,7 @@ public static void validateServiceCallResult(ServiceCallResult serviceCallResult throw new PolarisException(ErrorCode.API_INVALID_ARGUMENT, "delay can not be less than 0"); } if (!RetStatus.RetReject.equals(serviceCallResult.getRetStatus())) { - validateHostPort(serviceCallResult.getHost(), serviceCallResult.getPort()); + validateServiceCallResultHostPort(serviceCallResult.getHost(), serviceCallResult.getPort()); } } @@ -187,6 +177,29 @@ private static void validateHostPort(String host, Integer port) throws PolarisEx } } + /** + * 校验服务请求端口信息。某些场景下端口号为0,例如Dubbo的本地调用。 + * + * @param host + * @param port + * @throws PolarisException + */ + private static void validateServiceCallResultHostPort(String host, Integer port) throws PolarisException { + if (StringUtils.isBlank(host)) { + throw new PolarisException(ErrorCode.API_INVALID_ARGUMENT, "host can not be blank"); + } + if (port == null) { + throw new PolarisException(ErrorCode.API_INVALID_ARGUMENT, "port can not be null"); + } + if (port == 0) { + LOG.warn("port is 0 with host {}. Please check if it meets expectations.", host); + } + if (port < 0 || port >= MAX_PORT) { + throw new PolarisException( + ErrorCode.API_INVALID_ARGUMENT, "port value should be in range [0, 65536)."); + } + } + /** * 校验实例注册请求 * diff --git a/polaris-discovery/polaris-discovery-client/src/test/java/com/tencent/polaris/discovery/client/util/ValidatorTest.java b/polaris-discovery/polaris-discovery-client/src/test/java/com/tencent/polaris/discovery/client/util/ValidatorTest.java new file mode 100644 index 000000000..4712f7159 --- /dev/null +++ b/polaris-discovery/polaris-discovery-client/src/test/java/com/tencent/polaris/discovery/client/util/ValidatorTest.java @@ -0,0 +1,24 @@ +/* + * Tencent is pleased to support the open source community by making polaris-java available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.polaris.discovery.client.util; + +/** + * @author Haotian Zhang + */ +public class ValidatorTest { +} diff --git a/polaris-plugins/polaris-plugins-observability/event-tsf/src/main/java/com/tencent/polaris/plugins/event/tsf/TsfEventReporter.java b/polaris-plugins/polaris-plugins-observability/event-tsf/src/main/java/com/tencent/polaris/plugins/event/tsf/TsfEventReporter.java index 6ad848fc5..e93d076a6 100644 --- a/polaris-plugins/polaris-plugins-observability/event-tsf/src/main/java/com/tencent/polaris/plugins/event/tsf/TsfEventReporter.java +++ b/polaris-plugins/polaris-plugins-observability/event-tsf/src/main/java/com/tencent/polaris/plugins/event/tsf/TsfEventReporter.java @@ -29,9 +29,9 @@ import com.tencent.polaris.api.plugin.common.PluginTypes; import com.tencent.polaris.api.plugin.compose.Extensions; import com.tencent.polaris.api.plugin.event.BaseEvent; +import com.tencent.polaris.api.plugin.event.EventConstants; import com.tencent.polaris.api.plugin.event.EventReporter; import com.tencent.polaris.api.plugin.event.FlowEvent; -import com.tencent.polaris.api.pojo.ServiceEventKey; import com.tencent.polaris.api.utils.CollectionUtils; import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.api.utils.ThreadPoolUtils; @@ -102,10 +102,9 @@ public boolean isEnabled() { @Override public boolean reportEvent(BaseEvent baseEvent) { if (baseEvent instanceof FlowEvent) { - if (baseEvent.getEventType().equals(ServiceEventKey.EventType.CIRCUIT_BREAKING)) { + if (baseEvent.getEventType().equals(EventConstants.EventType.CIRCUIT_BREAKING)) { return reportV1Event((FlowEvent) baseEvent); - } - else if (baseEvent.getEventType().equals(ServiceEventKey.EventType.RATE_LIMITING)) { + } else if (baseEvent.getEventType().equals(EventConstants.EventType.RATE_LIMITING)) { return reportReportEvent((FlowEvent) baseEvent); } }