Skip to content

Commit

Permalink
[3.0] Multi instance - multi module config/deploy and spring support (#…
Browse files Browse the repository at this point in the history
…8789)

* Add spring support for multi instance

* Fix testMultiModuleApplication

* polish ApplicationModel and so on

* destroy application when all module are removed

* Extract ApplicationDeployer and ModuleDeployer, add ModuleConfigManager, add spring support for module manage

* Fix ut

* Initialize SpringExtensionInjector

* Fix DubboHealthIndicatorTest

* Fix SpringExtensionInjector avoid using not completed Spring ApplicationContext

* Prepare application instance before refer services, fix SpringXmlConfigTest

* App index starts from 1 in each FrameworkModel

* Add ConfigScopeModelInitializer, Refactor DubboShutdownHook

* Register bean with class is change to getOrRegisterBean

* Fix ShutdownTelnetTest

* Fix missing dependency

* Fix not found ApplicationModel bean in spring context

* Add module deploy and reload test

* Put deployer to scope model attributes

* Add getDeployer() to ApplicationModel/ModuleModel
  • Loading branch information
kylixs authored Sep 14, 2021
1 parent 675aead commit 5c2161a
Show file tree
Hide file tree
Showing 163 changed files with 4,275 additions and 2,965 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster.router.tag;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
Expand All @@ -41,6 +36,11 @@
import org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule;
import org.apache.dubbo.rpc.cluster.router.tag.model.TagRuleParser;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.TAG_KEY;
import static org.apache.dubbo.rpc.Constants.FORCE_USE_TAG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
*/
package org.apache.dubbo.rpc.cluster.router.tag;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
Expand All @@ -31,6 +26,12 @@
import org.apache.dubbo.rpc.cluster.router.state.AbstractStateRouter;
import org.apache.dubbo.rpc.cluster.router.state.BitList;
import org.apache.dubbo.rpc.cluster.router.state.RouterCache;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static org.apache.dubbo.common.constants.CommonConstants.TAG_KEY;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.dubbo.rpc.cluster.merger;

import org.apache.dubbo.rpc.model.ApplicationModel;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance;
import org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.rpc.model.ApplicationModel;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.common;

import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.common.lang.ShutdownHookCallbacks;
import org.apache.dubbo.common.status.reporter.FrameworkStatusReportService;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
Expand All @@ -32,6 +33,7 @@ public void initializeFrameworkModel(FrameworkModel frameworkModel) {
@Override
public void initializeApplicationModel(ApplicationModel applicationModel) {
ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
beanFactory.registerBean(ShutdownHookCallbacks.class);
beanFactory.registerBean(FrameworkStatusReportService.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,22 @@ private void initInstantiationStrategy() {
}

public <T> T registerBean(Class<T> bean) throws ScopeBeanException {
return this.registerBean(null, bean);
return this.getOrRegisterBean(null, bean);
}

public <T> T registerBean(String name, Class<T> clazz) throws ScopeBeanException {
return getOrRegisterBean(name, clazz);
}

private <T> T createAndRegisterBean(String name, Class<T> clazz) {
T instance = getBean(name, clazz);
if (instance != null) {
throw new ScopeBeanException("already exists bean with same name and type, name=" + name + ", type=" + clazz.getName());
}
try {
instance = instantiationStrategy.instantiate(clazz);
} catch (Throwable e) {
throw new ScopeBeanException("create bean instance failed, type=" + clazz.getName());
throw new ScopeBeanException("create bean instance failed, type=" + clazz.getName(), e);
}
registerBean(name, instance);
return instance;
Expand All @@ -101,23 +105,23 @@ public void registerBean(String name, Object bean) {
registeredBeanInfos.add(new BeanInfo(name, bean));
}

public <T> T registerBeanIfAbsent(Class<T> type) {
return registerBeanIfAbsent(null, type);
public <T> T getOrRegisterBean(Class<T> type) {
return getOrRegisterBean(null, type);
}

public <T> T registerBeanIfAbsent(String name, Class<T> type) {
public <T> T getOrRegisterBean(String name, Class<T> type) {
T bean = getBean(name, type);
if (bean == null) {
bean = registerBean(name, type);
bean = createAndRegisterBean(name, type);
}
return bean;
}

public <T> T registerBeanIfAbsent(Class<T> type, Function<? super Class<T>, ? extends T> mappingFunction) {
return registerBeanIfAbsent(null, type, mappingFunction);
public <T> T getOrRegisterBean(Class<T> type, Function<? super Class<T>, ? extends T> mappingFunction) {
return getOrRegisterBean(null, type, mappingFunction);
}

public <T> T registerBeanIfAbsent(String name, Class<T> type, Function<? super Class<T>, ? extends T> mappingFunction) {
public <T> T getOrRegisterBean(String name, Class<T> type, Function<? super Class<T>, ? extends T> mappingFunction) {
T bean = getBean(name, type);
if (bean == null) {
//TODO add lock for type
Expand Down Expand Up @@ -177,16 +181,23 @@ private <T> T getBeanInternal(String name, Class<T> type) {
return null;
}
List<BeanInfo> candidates = null;
BeanInfo firstCandidate = null;
for (BeanInfo beanInfo : registeredBeanInfos) {
// if required bean type is same class/superclass/interface of the registered bean
if (type.isAssignableFrom(beanInfo.instance.getClass())) {
if (StringUtils.isEquals(beanInfo.name, name)) {
return (T) beanInfo.instance;
} else {
if (candidates == null) {
candidates = new ArrayList<>();
// optimize for only one matched bean
if (firstCandidate == null) {
firstCandidate = beanInfo;
} else {
if (candidates == null) {
candidates = new ArrayList<>();
candidates.add(firstCandidate);
}
candidates.add(beanInfo);
}
candidates.add(beanInfo);
}
}
}
Expand All @@ -199,6 +210,8 @@ private <T> T getBeanInternal(String name, Class<T> type) {
List<String> candidateBeanNames = candidates.stream().map(beanInfo -> beanInfo.name).collect(Collectors.toList());
throw new ScopeBeanException("expected single matching bean but found " + candidates.size() + " candidates for type [" + type.getName() + "]: " + candidateBeanNames);
}
} else if (firstCandidate != null) {
return (T) firstCandidate.instance;
}
return null;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ public <T> T instantiate(Class<T> type) throws ReflectiveOperationException {
List<Constructor> matchedConstructors = new ArrayList<>();
Constructor<?>[] declaredConstructors = type.getConstructors();
for (Constructor<?> constructor : declaredConstructors) {
for (Class<?> parameterType : constructor.getParameterTypes()) {
if (!isSupportedConstructorParameterType(parameterType)) {
break;
}
if (isMatched(constructor)) {
matchedConstructors.add(constructor);
}
matchedConstructors.add(constructor);
}
if (matchedConstructors.size() > 1) {
throw new IllegalArgumentException("Expect only one but found " +
Expand All @@ -86,6 +83,15 @@ public <T> T instantiate(Class<T> type) throws ReflectiveOperationException {
return (T) constructor.newInstance(args);
}

private boolean isMatched(Constructor<?> constructor) {
for (Class<?> parameterType : constructor.getParameterTypes()) {
if (!isSupportedConstructorParameterType(parameterType)) {
return false;
}
}
return true;
}

private boolean isSupportedConstructorParameterType(Class<?> parameterType) {
return ScopeModel.class.isAssignableFrom(parameterType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
*/
package org.apache.dubbo.common.bytecode;

import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
Expand All @@ -29,6 +25,9 @@
import javassist.CtNewConstructor;
import javassist.CtNewMethod;
import javassist.NotFoundException;
import org.apache.dubbo.common.utils.ArrayUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.common.config;

import org.apache.dubbo.config.ReferenceConfigBase;

import java.util.List;

public interface ReferenceCache {
@SuppressWarnings("unchecked")
<T> T get(ReferenceConfigBase<T> referenceConfig);

@SuppressWarnings("unchecked")
<T> T get(String key, Class<T> type);

@SuppressWarnings("unchecked")
<T> T get(String key);

@SuppressWarnings("unchecked")
<T> List<T> getAll(Class<T> type);

@SuppressWarnings("unchecked")
<T> T get(Class<T> type);

void destroy(String key, Class<?> type);

void destroy(Class<?> type);

<T> void destroy(ReferenceConfigBase<T> referenceConfig);

void destroyAll();
}
Loading

0 comments on commit 5c2161a

Please sign in to comment.