use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ClusterSpiTest method testCall.
public void testCall() {
initCluster(true);
final Request request = mockRequest();
final Response response = mockery.mock(Response.class);
final URL url = mockURL();
mockery.checking(new Expectations() {
{
for (Referer<IHello> ref : referers) {
atLeast(0).of(ref).call(request);
will(returnValue(response));
atLeast(0).of(ref).isAvailable();
will(returnValue(true));
atLeast(0).of(ref).getUrl();
will(returnValue(url));
atLeast(1).of(ref).destroy();
}
}
});
clusterSpi.call(request);
clusterSpi.destroy();
try {
clusterSpi.call(request);
fail("Should not run to here!");
} catch (Exception e) {
assertTrue(e instanceof MotanServiceException);
}
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ClusterTest method setUp.
@Override
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
super.setUp();
HaStrategy<IHello> ha = new FailoverHaStrategy<IHello>();
LoadBalance<IHello> lb = new RandomLoadBalance<IHello>();
referers = new ArrayList<Referer<IHello>>();
referers.add(mockery.mock(Referer.class, "ref1"));
referers.add(mockery.mock(Referer.class, "ref2"));
cluster.setUrl(new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName()));
cluster.setHaStrategy(ha);
cluster.setLoadBalance(lb);
cluster.onRefresh(referers);
cluster.init();
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class RandomLoadBalanceTest method testSelectToHolder.
public void testSelectToHolder() {
final Request request = mockery.mock(Request.class);
mockery.checking(new Expectations() {
{
atLeast(1).of(request).getArguments();
will(returnValue(new Object[] { 1, 2, 3 }));
atLeast(0).of(request).getParamtersDesc();
will(returnValue("void_"));
}
});
for (int i = 0; i < 10; i++) {
List<Referer<IHello>> refHolder = new ArrayList<Referer<IHello>>();
randomLoadBalance.selectToHolder(request, refHolder);
assertEquals(refHolder.size(), (referers.size() - falseCount));
}
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ClusterSupport method notify.
/**
* <pre>
* 1 notify的执行需要串行
* 2 notify通知都是全量通知,在设入新的referer后,cluster需要把不再使用的referer进行回收,避免资源泄漏;
* 3 如果该registry对应的referer数量为0,而没有其他可用的referers,那就忽略该次通知;
* 4 此处对protoco进行decorator处理,当前为增加filters
* </pre>
*/
@Override
public synchronized void notify(URL registryUrl, List<URL> urls) {
if (CollectionUtil.isEmpty(urls)) {
onRegistryEmpty(registryUrl);
LoggerUtil.warn("ClusterSupport config change notify, urls is empty: registry={} service={} urls=[]", registryUrl.getUri(), url.getIdentity());
return;
}
LoggerUtil.info("ClusterSupport config change notify: registry={} service={} urls={}", registryUrl.getUri(), url.getIdentity(), getIdentities(urls));
// 通知都是全量通知,在设入新的referer后,cluster内部需要把不再使用的referer进行回收,避免资源泄漏
// ////////////////////////////////////////////////////////////////////////////////
// 判断urls中是否包含权重信息,并通知loadbalance。
processWeights(urls);
List<Referer<T>> newReferers = new ArrayList<Referer<T>>();
for (URL u : urls) {
if (!u.canServe(url)) {
continue;
}
Referer<T> referer = getExistingReferer(u, registryReferers.get(registryUrl));
if (referer == null) {
// careful u: serverURL, refererURL的配置会被serverURL的配置覆盖
URL refererURL = u.createCopy();
mergeClientConfigs(refererURL);
referer = protocol.refer(interfaceClass, refererURL, u);
}
if (referer != null) {
newReferers.add(referer);
}
}
if (CollectionUtil.isEmpty(newReferers)) {
onRegistryEmpty(registryUrl);
return;
}
// 此处不销毁referers,由cluster进行销毁
registryReferers.put(registryUrl, newReferers);
refreshCluster();
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class FailoverHaStrategy method call.
@Override
public Response call(Request request, LoadBalance<T> loadBalance) {
List<Referer<T>> referers = selectReferers(request, loadBalance);
if (referers.isEmpty()) {
throw new MotanServiceException(String.format("FailoverHaStrategy No referers for request:%s, loadbalance:%s", request, loadBalance));
}
URL refUrl = referers.get(0).getUrl();
// 先使用method的配置
int tryCount = refUrl.getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.retries.getName(), URLParamType.retries.getIntValue());
// 如果有问题,则设置为不重试
if (tryCount < 0) {
tryCount = 0;
}
for (int i = 0; i <= tryCount; i++) {
Referer<T> refer = referers.get(i % referers.size());
try {
request.setRetries(i);
return refer.call(request);
} catch (RuntimeException e) {
// 对于业务异常,直接抛出
if (ExceptionUtil.isBizException(e)) {
throw e;
} else if (i >= tryCount) {
throw e;
}
LoggerUtil.warn(String.format("FailoverHaStrategy Call false for request:%s error=%s", request, e.getMessage()));
}
}
throw new MotanFrameworkException("FailoverHaStrategy.call should not come here!");
}
Aggregations