Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.2' into provider-payload
Browse files Browse the repository at this point in the history
* origin/3.2: (22 commits)
  Remove logic to simplify ConstraintViolationException to ValidationException. (apache#11883)
  Bump spring-boot-dependencies from 2.7.9 to 2.7.10 (apache#11904)
  Bump spring-boot.version from 2.7.9 to 2.7.10 (apache#11905)
  Bump spring-boot-starter-test from 2.7.9 to 2.7.10 (apache#11906)
  Bump commons-compress from 1.22 to 1.23.0 (apache#11907)
  Bump spring-boot-maven-plugin from 2.7.9 to 2.7.10 (apache#11908)
  dubbo-starter  import metrics dep (apache#11921)
  Add histogram (apache#11632)
  Add metrics for configcenter (apache#11602)
  Fix/dependency cycle (apache#11919)
  bugfix && add testcase (apache#11918)
  adding the reject task indicator for thread pool burying point(apache#11706) (apache#11916)
  Revert "optimize performance. decode in user thread (apache#11879)" (apache#11917)
  Reject if response do not match any request (apache#11882)
  triple server batch flush (apache#11723)
  optimize performance. decode in user thread (apache#11879)
  Code opt (apache#11857)
  Bump maven-release-plugin from 3.0.0-M7 to 3.0.0 (apache#11877)
  support exception process when  service not found (apache#11088)
  Bump maven-core from 3.9.0 to 3.9.1 (apache#11869)
  ...

# Conflicts:
#	dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcInvocation.java
  • Loading branch information
mytang0 committed Mar 27, 2023
2 parents 8c84a97 + 35751cb commit 8e99fe4
Show file tree
Hide file tree
Showing 95 changed files with 2,269 additions and 415 deletions.
11 changes: 11 additions & 0 deletions dubbo-cluster/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,16 @@
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-default</artifactId>
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-integration-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* limitations under the License.
*/

package org.apache.dubbo.metrics.filter;
package org.apache.dubbo.rpc.cluster.filter.support;


import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
Expand All @@ -29,12 +30,13 @@
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ScopeModelAware;


import java.util.Optional;

import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;

@Activate(group = CONSUMER)
@Activate(group = CONSUMER,onClass = "org.apache.dubbo.metrics.collector.DefaultMetricsCollector")
public class MetricsClusterFilter implements ClusterFilter, BaseFilter.Listener, ScopeModelAware {

private DefaultMetricsCollector collector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.metrics.observation;
package org.apache.dubbo.rpc.cluster.filter.support;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;

import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metrics.observation.DefaultDubboClientObservationConvention;
import org.apache.dubbo.metrics.observation.DubboClientContext;
import org.apache.dubbo.metrics.observation.DubboClientObservationConvention;
import org.apache.dubbo.metrics.observation.DubboObservation;
import org.apache.dubbo.rpc.BaseFilter;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ available=org.apache.dubbo.rpc.cluster.support.AvailableCluster
mergeable=org.apache.dubbo.rpc.cluster.support.MergeableCluster
broadcast=org.apache.dubbo.rpc.cluster.support.BroadcastCluster
zone-aware=org.apache.dubbo.rpc.cluster.support.registry.ZoneAwareCluster
observationsender=org.apache.dubbo.metrics.observation.ObservationSenderFilter
metricsClusterFilter=org.apache.dubbo.metrics.filter.MetricsClusterFilter

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.dubbo.rpc.cluster.filter;

import io.micrometer.tracing.test.SampleTestRunner;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.BaseFilter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.AfterEach;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

abstract class AbstractObservationFilterTest extends SampleTestRunner {

ApplicationModel applicationModel;
RpcInvocation invocation;

BaseFilter filter;

Invoker<?> invoker = mock(Invoker.class);

static final String INTERFACE_NAME = "org.apache.dubbo.MockInterface";
static final String METHOD_NAME = "mockMethod";
static final String GROUP = "mockGroup";
static final String VERSION = "1.0.0";

@AfterEach
public void teardown() {
if (applicationModel != null) {
applicationModel.destroy();
}
}

abstract BaseFilter createFilter(ApplicationModel applicationModel);

void setupConfig() {
ApplicationConfig config = new ApplicationConfig();
config.setName("MockObservations");

applicationModel = ApplicationModel.defaultModel();
applicationModel.getApplicationConfigManager().setApplication(config);

invocation = new RpcInvocation(new MockInvocation());
invocation.addInvokedInvoker(invoker);

applicationModel.getBeanFactory().registerBean(getObservationRegistry());

filter = createFilter(applicationModel);

given(invoker.invoke(invocation)).willReturn(new AppResponse("success"));

initParam();
}

private void initParam() {
invocation.setTargetServiceUniqueName(GROUP + "/" + INTERFACE_NAME + ":" + VERSION);
invocation.setMethodName(METHOD_NAME);
invocation.setParameterTypes(new Class[] {String.class});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ protected Result doInvoke(Invocation invocation) {
};

Invoker<?> invokerAfterBuild = defaultFilterChainBuilder.buildInvokerChain(invokerWithoutFilter, REFERENCE_FILTER_KEY, CONSUMER);
Assertions.assertTrue(invokerAfterBuild instanceof AbstractInvoker);

// verify that if LogFilter is configured, LogFilter should exist in the filter chain
URL urlWithFilter = URL.valueOf("injvm://127.0.0.1/DemoService")
Expand All @@ -64,8 +63,6 @@ protected Result doInvoke(Invocation invocation) {
};
invokerAfterBuild = defaultFilterChainBuilder.buildInvokerChain(invokerWithFilter, REFERENCE_FILTER_KEY, CONSUMER);
Assertions.assertTrue(invokerAfterBuild instanceof FilterChainBuilder.CallbackRegistrationInvoker);
Assertions.assertEquals(1, ((FilterChainBuilder.CallbackRegistrationInvoker<?, ?>) invokerAfterBuild).filters.size());

}

@Test
Expand All @@ -84,7 +81,7 @@ protected Result doInvoke(Invocation invocation) {
};

Invoker<?> invokerAfterBuild = defaultFilterChainBuilder.buildInvokerChain(invokerWithoutFilter, REFERENCE_FILTER_KEY, CONSUMER);
Assertions.assertTrue(invokerAfterBuild instanceof AbstractInvoker);
// Assertions.assertTrue(invokerAfterBuild instanceof AbstractInvoker);

// verify that if LogFilter is configured, LogFilter should exist in the filter chain
URL urlWithFilter = URL.valueOf("dubbo://127.0.0.1:20880/DemoService")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/*
* 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.dubbo.rpc.cluster.filter;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
import org.apache.dubbo.metrics.filter.MetricsFilter;
import org.apache.dubbo.metrics.model.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.cluster.filter.support.MetricsClusterFilter;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

class MetricsClusterFilterTest {

private ApplicationModel applicationModel;
private MetricsFilter filter;
private MetricsClusterFilter metricsClusterFilter;
private DefaultMetricsCollector collector;
private RpcInvocation invocation;
private final Invoker<?> invoker = mock(Invoker.class);

private static final String INTERFACE_NAME = "org.apache.dubbo.MockInterface";
private static final String METHOD_NAME = "mockMethod";
private static final String GROUP = "mockGroup";
private static final String VERSION = "1.0.0";
private String side;

private AtomicBoolean initApplication = new AtomicBoolean(false);


@BeforeEach
public void setup() {
ApplicationConfig config = new ApplicationConfig();
config.setName("MockMetrics");
//RpcContext.getContext().setAttachment("MockMetrics","MockMetrics");

applicationModel = ApplicationModel.defaultModel();
applicationModel.getApplicationConfigManager().setApplication(config);

invocation = new RpcInvocation();
filter = new MetricsFilter();

collector = applicationModel.getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class);
if(!initApplication.get()) {
collector.collectApplication(applicationModel);
initApplication.set(true);
}
filter.setApplicationModel(applicationModel);
side = CommonConstants.CONSUMER;
invocation.setInvoker(new TestMetricsInvoker(side));
RpcContext.getServiceContext().setUrl(URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side=" + side));

metricsClusterFilter = new MetricsClusterFilter();
metricsClusterFilter.setApplicationModel(applicationModel);
}

@AfterEach
public void teardown() {
applicationModel.destroy();
}

@Test
public void testNoProvider(){
testClusterFilterError(RpcException.FORBIDDEN_EXCEPTION,
MetricsKey.METRIC_REQUESTS_SERVICE_UNAVAILABLE_FAILED.getNameByType(CommonConstants.CONSUMER));
}

private void testClusterFilterError(int errorCode,String name){
collector.setCollectEnabled(true);
given(invoker.invoke(invocation)).willThrow(new RpcException(errorCode));
initParam();

Long count = 1L;

for (int i = 0; i < count; i++) {
try {
metricsClusterFilter.invoke(invoker, invocation);
} catch (Exception e) {
Assertions.assertTrue(e instanceof RpcException);
metricsClusterFilter.onError(e, invoker, invocation);
}
}
Map<String, MetricSample> metricsMap = getMetricsMap();
Assertions.assertTrue(metricsMap.containsKey(name));

MetricSample sample = metricsMap.get(name);

Assertions.assertSame(((GaugeMetricSample) sample).applyAsLong(), count);
teardown();
}



private void initParam() {
invocation.setTargetServiceUniqueName(GROUP + "/" + INTERFACE_NAME + ":" + VERSION);
invocation.setMethodName(METHOD_NAME);
invocation.setParameterTypes(new Class[]{String.class});
}

private Map<String, MetricSample> getMetricsMap() {
List<MetricSample> samples = collector.collect();
List<MetricSample> samples1 = new ArrayList<>();
for (MetricSample sample : samples) {
if (sample.getName().contains("dubbo.thread.pool")) {
continue;
}
samples1.add(sample);
}
return samples1.stream().collect(Collectors.toMap(MetricSample::getName, Function.identity()));
}

public class TestMetricsInvoker implements Invoker {

private String side;

public TestMetricsInvoker(String side) {
this.side = side;
}

@Override
public Class getInterface() {
return null;
}

@Override
public Result invoke(Invocation invocation) throws RpcException {
return null;
}

@Override
public URL getUrl() {
return URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&side="+side);
}

@Override
public boolean isAvailable() {
return true;
}

@Override
public void destroy() {

}
}
}
Loading

0 comments on commit 8e99fe4

Please sign in to comment.