Skip to content

Commit

Permalink
Add some test cases and optimizations (apache#10481)
Browse files Browse the repository at this point in the history
* Add some test cases and optimizations

* Rollback

* Update ClassGenerator.java
BurningCN authored Aug 24, 2022
1 parent c326da7 commit f7e87c1
Showing 38 changed files with 396 additions and 256 deletions.
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@
import static org.apache.dubbo.rpc.cluster.Constants.OVERRIDE_PROVIDERS_KEY;

/**
* AbstractOverrideConfigurator
* AbstractConfigurator
*/
public abstract class AbstractConfigurator implements Configurator {

Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@

/**
* AbsentConfigurator
*
*/
public class AbsentConfigurator extends AbstractConfigurator {

Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@

/**
* OverrideConfigurator
*
*/
public class OverrideConfigurator extends AbstractConfigurator {

Original file line number Diff line number Diff line change
@@ -19,9 +19,8 @@
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.rpc.model.ScopeModelAware;

public class DefaultGovernanceRuleRepositoryImpl implements GovernanceRuleRepository, ScopeModelAware {
public class DefaultGovernanceRuleRepositoryImpl implements GovernanceRuleRepository {

private ModuleModel moduleModel;

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.cluster.configurator.absent.AbsentConfigurator;
import org.apache.dubbo.rpc.cluster.configurator.override.OverrideConfigurator;
import org.apache.dubbo.rpc.cluster.configurator.parser.ConfigParser;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* {@link Configurator}
*/
public class ConfiguratorTest {

@Test
public void test() throws Exception {

Optional<List<Configurator>> emptyOptional = Configurator.toConfigurators(Collections.emptyList());
Assertions.assertEquals(Optional.empty(), emptyOptional);

String configData = "[\"override://0.0.0.0/com.xx.Service?category=configurators&timeout=6666&disabled=true&dynamic=false&enabled=true&group=dubbo&priority=2&version=1.0\"" +
", \"absent://0.0.0.0/com.xx.Service?category=configurators&timeout=6666&disabled=true&dynamic=false&enabled=true&group=dubbo&priority=1&version=1.0\" ]";
List<URL> urls = ConfigParser.parseConfigurators(configData);
Optional<List<Configurator>> optionalList = Configurator.toConfigurators(urls);
Assertions.assertTrue(optionalList.isPresent());
List<Configurator> configurators = optionalList.get();
Assertions.assertEquals(configurators.size(), 2);
// The hosts of AbsentConfigurator and OverrideConfigurator are equal, but the priority of OverrideConfigurator is higher
Assertions.assertTrue(configurators.get(0) instanceof AbsentConfigurator);
Assertions.assertTrue(configurators.get(1) instanceof OverrideConfigurator);
}

}
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ public final class ClassGenerator {

private static final AtomicLong CLASS_NAME_COUNTER = new AtomicLong(0);
private static final String SIMPLE_NAME_TAG = "<init>";
private static final Map<ClassLoader, ClassPool> POOL_MAP = new ConcurrentHashMap<ClassLoader, ClassPool>(); //ClassLoader - ClassPool
private static final Map<ClassLoader, ClassPool> POOL_MAP = new ConcurrentHashMap<>(); //ClassLoader - ClassPool
private ClassPool mPool;
private CtClass mCtc;
private String mClassName;
@@ -131,7 +131,7 @@ public ClassGenerator setClassName(String name) {

public ClassGenerator addInterface(String cn) {
if (mInterfaces == null) {
mInterfaces = new HashSet<String>();
mInterfaces = new HashSet<>();
}
mInterfaces.add(cn);
return this;
@@ -153,7 +153,7 @@ public ClassGenerator setSuperClass(Class<?> cl) {

public ClassGenerator addField(String code) {
if (mFields == null) {
mFields = new ArrayList<String>();
mFields = new ArrayList<>();
}
mFields.add(code);
return this;
@@ -177,7 +177,7 @@ public ClassGenerator addField(String name, int mod, Class<?> type, String def)

public ClassGenerator addMethod(String code) {
if (mMethods == null) {
mMethods = new ArrayList<String>();
mMethods = new ArrayList<>();
}
mMethods.add(code);
return this;
@@ -192,12 +192,14 @@ public ClassGenerator addMethod(String name, int mod, Class<?> rt, Class<?>[] pt
StringBuilder sb = new StringBuilder();
sb.append(modifier(mod)).append(' ').append(ReflectUtils.getName(rt)).append(' ').append(name);
sb.append('(');
for (int i = 0; i < pts.length; i++) {
if (i > 0) {
sb.append(',');
if (ArrayUtils.isNotEmpty(pts)) {
for (int i = 0; i < pts.length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append(ReflectUtils.getName(pts[i]));
sb.append(" arg").append(i);
}
sb.append(ReflectUtils.getName(pts[i]));
sb.append(" arg").append(i);
}
sb.append(')');
if (ArrayUtils.isNotEmpty(ets)) {
@@ -222,15 +224,15 @@ public ClassGenerator addMethod(String name, Method m) {
String desc = name + ReflectUtils.getDescWithoutMethodName(m);
addMethod(':' + desc);
if (mCopyMethods == null) {
mCopyMethods = new ConcurrentHashMap<String, Method>(8);
mCopyMethods = new ConcurrentHashMap<>(8);
}
mCopyMethods.put(desc, m);
return this;
}

public ClassGenerator addConstructor(String code) {
if (mConstructors == null) {
mConstructors = new LinkedList<String>();
mConstructors = new LinkedList<>();
}
mConstructors.add(code);
return this;
@@ -269,7 +271,7 @@ public ClassGenerator addConstructor(Constructor<?> c) {
String desc = ReflectUtils.getDesc(c);
addConstructor(":" + desc);
if (mCopyConstructors == null) {
mCopyConstructors = new ConcurrentHashMap<String, Constructor<?>>(4);
mCopyConstructors = new ConcurrentHashMap<>(4);
}
mCopyConstructors.put(desc, c);
return this;
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@
/**
* Proxy.
*/

public class Proxy {
public static final InvocationHandler THROW_UNSUPPORTED_INVOKER = new InvocationHandler() {
@Override
Original file line number Diff line number Diff line change
@@ -293,6 +293,7 @@ private static Wrapper makeWrapper(Class<?> c) {
throw new RuntimeException(e.getMessage(), e);
} finally {
cc.release();
pts.clear();
ms.clear();
mns.clear();
dmns.clear();
Original file line number Diff line number Diff line change
@@ -88,10 +88,11 @@ public T remove() {
public T remove(Consumer<? super T> action) {
// release
T value = reference.getAndSet(null);
if (value != null && action != null) {
if (value != null) {
if (action != null) {
action.accept(value);
} else if (value instanceof Disposable) {
}
if (value instanceof Disposable) {
((Disposable) value).destroy();
}
}
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ default String getString(String key, String defaultValue) {
}

default int getInt(String key) {
Integer i = this.getInteger(key, (Integer) null);
Integer i = this.getInteger(key, null);
if (i != null) {
return i;
} else {
@@ -58,7 +58,7 @@ default int getInt(String key) {
}

default int getInt(String key, int defaultValue) {
Integer i = this.getInteger(key, (Integer) null);
Integer i = this.getInteger(key, null);
return i == null ? defaultValue : i;
}

Original file line number Diff line number Diff line change
@@ -171,7 +171,7 @@ public void updateAppConfigMap(Map<String, String> map) {
* All configurations will be converged into a data bus - URL, and then drive the subsequent process.
* <p>
* At present, there are many configuration sources, including AbstractConfig (API, XML, annotation), - D, config center, etc.
* This methood helps us t filter out the most priority values from various configuration sources.
* This method helps us t filter out the most priority values from various configuration sources.
*
* @param config
* @param prefix
Original file line number Diff line number Diff line change
@@ -126,9 +126,6 @@ public void setDynamicConfiguration(DynamicConfiguration dynamicConfiguration) {
public void destroy() throws IllegalStateException {
super.destroy();
this.orderedPropertiesConfiguration = null;
this.globalConfiguration = null;
this.dynamicGlobalConfiguration = null;
this.dynamicConfiguration = null;
}

@Override
@@ -224,8 +221,5 @@ public String getLocalMigrationRule() {
public void refreshClassLoaders() {
orderedPropertiesConfiguration.refresh();
applicationDelegate.refreshClassLoaders();
this.globalConfiguration = null;
this.globalConfigurationMaps = null;
this.dynamicGlobalConfiguration = null;
}
}
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
import java.util.Properties;
import java.util.Set;

public class OrderedPropertiesConfiguration implements Configuration{
public class OrderedPropertiesConfiguration implements Configuration {
private Properties properties;
private ModuleModel moduleModel;

Original file line number Diff line number Diff line change
@@ -20,10 +20,7 @@
import java.util.Map;

/**
* FIXME: is this really necessary? PropertiesConfiguration should have already covered this:
*
* @See ConfigUtils#getProperty(String)
* @see PropertiesConfiguration
* Configuration from system properties
*/
public class SystemConfiguration implements Configuration {

Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
package org.apache.dubbo.common.lang;

import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.resource.Disposable;
import org.apache.dubbo.rpc.model.ApplicationModel;

import java.util.Collection;
@@ -31,7 +32,7 @@
*
* @since 2.7.5
*/
public class ShutdownHookCallbacks {
public class ShutdownHookCallbacks implements Disposable {

private final List<ShutdownHookCallback> callbacks = new LinkedList<>();

@@ -56,7 +57,7 @@ public Collection<ShutdownHookCallback> getCallbacks() {
}
}

public void clear() {
public void destroy() {
synchronized (this) {
callbacks.clear();
}
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ public GlobalResourceInitializer(Callable<T> initializer) {
super(initializer);
}

public GlobalResourceInitializer(Callable initializer, Consumer<T> disposeAction) {
public GlobalResourceInitializer(Callable<T> initializer, Consumer<T> disposeAction) {
super(initializer);
this.disposeAction = disposeAction;
}
@@ -59,7 +59,4 @@ protected T initialize() {
return value;
}

public interface DestroyHandler<T> {
void dispose(GlobalResourceInitializer<T> initializer);
}
}
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
import java.util.concurrent.Executors;

/**
* Global resources repository between all framework models.
* Global resource repository between all framework models.
* It will be destroyed only after all framework model is destroyed.
*/
public class GlobalResourcesRepository {
@@ -59,16 +59,22 @@ public static GlobalResourcesRepository getInstance() {
* @param disposable
*/
public static void registerGlobalDisposable(Disposable disposable) {
synchronized (GlobalResourcesRepository.class) {
if (!globalReusedDisposables.contains(disposable)) {
globalReusedDisposables.add(disposable);
if (!globalReusedDisposables.contains(disposable)) {
synchronized (GlobalResourcesRepository.class) {
if (!globalReusedDisposables.contains(disposable)) {
globalReusedDisposables.add(disposable);
}
}
}
}

public void removeGlobalDisposable(Disposable disposable) {
synchronized (GlobalResourcesRepository.class) {
this.globalReusedDisposables.remove(disposable);
if (globalReusedDisposables.contains(disposable)) {
synchronized (GlobalResourcesRepository.class) {
if (globalReusedDisposables.contains(disposable)) {
this.globalReusedDisposables.remove(disposable);
}
}
}
}

@@ -125,14 +131,24 @@ public synchronized void destroy() {
* Register a one-off disposable, the disposable is removed automatically on first shutdown.
* @param disposable
*/
public synchronized void registerDisposable(Disposable disposable) {
public void registerDisposable(Disposable disposable) {
if (!oneoffDisposables.contains(disposable)) {
oneoffDisposables.add(disposable);
synchronized (this) {
if (!oneoffDisposables.contains(disposable)) {
oneoffDisposables.add(disposable);
}
}
}
}

public synchronized void removeDisposable(Disposable disposable) {
this.oneoffDisposables.remove(disposable);
public void removeDisposable(Disposable disposable) {
if (oneoffDisposables.contains(disposable)) {
synchronized (this) {
if (oneoffDisposables.contains(disposable)) {
oneoffDisposables.remove(disposable);
}
}
}
}


Loading
Oops, something went wrong.

0 comments on commit f7e87c1

Please sign in to comment.