Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#4459] Fix unchecked call to a original type member #4506

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.eventmesh.common.utils;

import java.util.HashSet;
import java.util.Set;

import lombok.experimental.UtilityClass;

@UtilityClass
public class TypeUtils {

public static <T> Set<T> castSet(Object obj, Class<T> clazz) {
Set<T> result = new HashSet<>();
if (obj instanceof Set<?>) {
for (Object o : (Set<?>) obj) {
result.add(clazz.cast(o));
}
return result;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.eventmesh.common.utils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TypeUtilsTest {

@Test
public void testCastSet() {
Set<String> set = new HashSet<>(Arrays.asList("1", "2", "3"));
Set<String> newSet = TypeUtils.castSet(set, String.class);
for (String s : set) {
Assertions.assertTrue(newSet.contains(s));
}
Assertions.assertEquals(set.size(), newSet.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static void export(final String meterName, final GrpcSummaryMetrics summa
final Meter meter = GlobalMeterProvider.getMeter(meterName);

paramPairs.forEach(
(metricInfo, getMetric) -> observeOfValue(meter, METRICS_GRPC_PREFIX + metricInfo[0], metricInfo[1], GRPC, summaryMetrics, getMetric));
(metricInfo, getMetric) -> observeOfValue(meter, METRICS_GRPC_PREFIX + metricInfo[0], metricInfo[1],
GRPC, summaryMetrics, getMetric, GrpcSummaryMetrics.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ public class PrometheusHttpExporter {

public void export(String name, HttpSummaryMetrics summaryMetrics) {
Meter meter = GlobalMeterProvider.getMeter(name);
paramPairs.forEach((metricInfo, getMetric) -> observeOfValue(meter, metricInfo[0], metricInfo[1], HTTP, summaryMetrics, getMetric));
paramPairs.forEach((metricInfo, getMetric) -> observeOfValue(meter, metricInfo[0], metricInfo[1],
HTTP, summaryMetrics, getMetric, HttpSummaryMetrics.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class PrometheusTcpExporter {
public void export(final String meterName, final TcpSummaryMetrics summaryMetrics) {
final Meter meter = GlobalMeterProvider.getMeter(meterName);
paramPairs.forEach(
(metricInfo, getMetric) -> observeOfValue(meter, METRICS_TCP_PREFIX + metricInfo[0], metricInfo[1], TCP, summaryMetrics, getMetric));
(metricInfo, getMetric) -> observeOfValue(meter, METRICS_TCP_PREFIX + metricInfo[0], metricInfo[1],
TCP, summaryMetrics, getMetric, TcpSummaryMetrics.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ public class PrometheusExporterUtils {
* @param getMetric
*/
@SneakyThrows
public static void observeOfValue(Meter meter, String metricName, String metricDesc, String protocol,
Metric summaryMetrics, Function getMetric) {
public static <T extends Metric> void observeOfValue(Meter meter, String metricName, String metricDesc, String protocol,
Metric summaryMetrics, Function<T, Number> getMetric, Class<T> clazz) {
Method method = getMetric.getClass().getMethod("apply", Object.class);
Class metricType = (Class) method.getGenericReturnType();
Class<?> metricType = (Class<?>) method.getGenericReturnType();
if (metricType == Long.class) {
meter.longValueObserverBuilder(metricName)
.setDescription(metricDesc)
.setUnit(protocol)
.setUpdater(result -> result.observe((long) getMetric.apply(summaryMetrics), Labels.empty()))
.setUpdater(result -> result.observe((long) getMetric.apply(clazz.cast(summaryMetrics)), Labels.empty()))
.build();
} else if (metricType == Double.class) {
meter.doubleValueObserverBuilder(metricName)
.setDescription(metricDesc)
.setUnit(protocol)
.setUpdater(result -> result.observe((double) getMetric.apply(summaryMetrics), Labels.empty()))
.setUpdater(result -> result.observe((double) getMetric.apply(clazz.cast(summaryMetrics)), Labels.empty()))
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ default boolean rejectRequest() {
return false;
}

default <T extends Header, E extends Body> void completeResponse(HttpCommand req, AsyncContext asyncContext,
default <T extends Header, E extends Body> void completeResponse(HttpCommand req, AsyncContext<HttpCommand> asyncContext,
T respHeader, EventMeshRetCode emCode,
String msg, Class<E> clazz) {
try {
Method method = clazz.getMethod("buildBody", Integer.class, String.class);
Object o = method.invoke(null, emCode.getRetCode(),
StringUtils.isNotBlank(msg) ? msg : emCode.getErrMsg());
HttpCommand response = req.createHttpCommandResponse(respHeader, (E) o);
HttpCommand response = req.createHttpCommandResponse(respHeader, (Body) o);
asyncContext.onComplete(response);
} catch (Exception e) {
log.error("response failed", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.eventmesh.api.exception.AclException;
import org.apache.eventmesh.common.config.CommonConfiguration;
import org.apache.eventmesh.common.utils.ConfigurationContextUtil;
import org.apache.eventmesh.common.utils.TypeUtils;

import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -136,14 +137,15 @@ public static boolean authAccess(AclProperties aclProperties) {

String topic = aclProperties.getTopic();

Set<String> groupTopics = (Set<String>) aclProperties.getExtendedField("topics");
Object topics = aclProperties.getExtendedField("topics");
Comment on lines 139 to +140
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This modification can have impacts on code readability and it is recommended to add some comments if there is no better solution.


if (groupTopics.contains(topic)) {
return true;
} else {
if (!(topics instanceof Set)) {
return false;
}

Set<String> groupTopics = TypeUtils.castSet(topics, String.class);

return groupTopics.contains(topic);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public void unsubscribe(String topic) {
}
}

@SuppressWarnings("deprecation")
public void updateOffset(List<CloudEvent> cloudEvents, AbstractContext context) {
ConsumeMessageService consumeMessageService = rocketmqPushConsumer
.getDefaultMQPushConsumerImpl().getConsumeMessageService();
Expand Down
Loading