From 0b611020176ffd40f4ceb41971d7a425b7d36834 Mon Sep 17 00:00:00 2001 From: "1162223173@qq.com" <1162223173@qq.com> Date: Sat, 6 Jan 2018 23:15:52 +0800 Subject: [PATCH 1/9] update SpringBeanUtil.java etc --- .../java/com/weibo/api/motan/switcher/LocalSwitcherService.java | 2 +- .../api/motan/config/springsupport/util/SpringBeanUtil.java | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/switcher/LocalSwitcherService.java b/motan-core/src/main/java/com/weibo/api/motan/switcher/LocalSwitcherService.java index af65619b8..9365178b0 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/switcher/LocalSwitcherService.java +++ b/motan-core/src/main/java/com/weibo/api/motan/switcher/LocalSwitcherService.java @@ -36,7 +36,7 @@ public class LocalSwitcherService implements SwitcherService { private static ConcurrentMap switchers = new ConcurrentHashMap(); - private Map> listenerMap = new ConcurrentHashMap(); + private Map> listenerMap = new ConcurrentHashMap<>(); @Override public Switcher getSwitcher(String name) { diff --git a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/util/SpringBeanUtil.java b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/util/SpringBeanUtil.java index fe7f63bb8..d94d86817 100644 --- a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/util/SpringBeanUtil.java +++ b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/util/SpringBeanUtil.java @@ -1,6 +1,5 @@ package com.weibo.api.motan.config.springsupport.util; -import com.weibo.api.motan.config.ProtocolConfig; import org.springframework.beans.factory.BeanFactory; import java.util.ArrayList; From bc33c2768ff80ea369bcd82660738fc75e682d5d Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 10 Jan 2018 18:52:30 +0800 Subject: [PATCH 2/9] support multi serialize in simpleSerialization --- .../motan/serialize/SimpleSerialization.java | 56 ++++++++++--------- .../serialize/SimpleSerializationTest.java | 34 ++++++++++- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/serialize/SimpleSerialization.java b/motan-core/src/main/java/com/weibo/api/motan/serialize/SimpleSerialization.java index 3d913e999..20a6acfa7 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/serialize/SimpleSerialization.java +++ b/motan-core/src/main/java/com/weibo/api/motan/serialize/SimpleSerialization.java @@ -40,6 +40,14 @@ public class SimpleSerialization implements Serialization { @Override public byte[] serialize(Object obj) throws IOException { GrowableByteBuffer buffer = new GrowableByteBuffer(4096); + serialize(obj, buffer); + buffer.flip(); + byte[] result = new byte[buffer.remaining()]; + buffer.get(result); + return result; + } + + private void serialize(Object obj, GrowableByteBuffer buffer) throws IOException { if (obj != null) { if (obj instanceof String) { buffer.put((byte) 1); @@ -48,8 +56,9 @@ public byte[] serialize(Object obj) throws IOException { buffer.put(b); } else if (obj instanceof Map) { buffer.put((byte) 2); + int pos = buffer.position(); int size = 0; - buffer.position(5); + buffer.position(pos + 4); for (Entry entry : ((Map) obj).entrySet()) { if (entry.getKey() != null && entry.getValue() != null && (entry.getKey() instanceof String) && (entry.getValue() instanceof String)) { @@ -57,10 +66,10 @@ public byte[] serialize(Object obj) throws IOException { size += putString(buffer, (String) entry.getValue()); } } - buffer.position(1); + buffer.position(pos); buffer.putInt(size); - buffer.position(5 + size); - } else if(obj instanceof byte[]){ + buffer.position(pos + size + 4); + } else if (obj instanceof byte[]) { buffer.put((byte) 3); byte[] b = (byte[]) obj; buffer.putInt(b.length); @@ -71,15 +80,15 @@ public byte[] serialize(Object obj) throws IOException { } else { buffer.put((byte) 0); } - buffer.flip(); - byte[] result = new byte[buffer.remaining()]; - buffer.get(result); - return result; } @Override public T deserialize(byte[] bytes, Class clz) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(bytes); + return deserialize(buffer, clz); + } + + private T deserialize(ByteBuffer buffer, Class clz) throws IOException { byte type = buffer.get(); switch (type) { case 0: @@ -111,6 +120,7 @@ public T deserialize(byte[] bytes, Class clz) throws IOException { key = getString(buffer); } } + buffer.limit(buffer.capacity()); return (T) map; } else { throw new MotanServiceException("SimpleSerialization not support type:" + clz); @@ -125,30 +135,24 @@ public T deserialize(byte[] bytes, Class clz) throws IOException { @Override public byte[] serializeMulti(Object[] data) throws IOException { - if (data.length == 1) { - return serialize(data[0]); + GrowableByteBuffer buffer = new GrowableByteBuffer(4096); + for (Object o : data) { + serialize(o, buffer); } - //TODO mulit param support - throw new MotanServiceException("SimpleSerialization not support serialize multi Object"); + buffer.flip(); + byte[] result = new byte[buffer.remaining()]; + buffer.get(result); + return result; } @Override public Object[] deserializeMulti(byte[] data, Class[] classes) throws IOException { - if (classes.length == 1) { - return new Object[]{deserialize(data, classes[0])}; - } else { - StringBuilder sb = new StringBuilder(128); - sb.append("["); - for (Class c : classes) { - sb.append(c.getName()).append(","); - } - if (sb.length() > 1) { - sb.deleteCharAt(sb.length() - 1); - } - sb.append("]"); - throw new MotanServiceException("SimpleSerialization not support deserialize multi Object of " + classes); + ByteBuffer buffer = ByteBuffer.wrap(data); + Object[] result = new Object[classes.length]; + for (int i = 0; i < classes.length; i++) { + result[i] = deserialize(buffer, classes[i]); } - + return result; } @Override diff --git a/motan-core/src/test/java/com/weibo/api/motan/serialize/SimpleSerializationTest.java b/motan-core/src/test/java/com/weibo/api/motan/serialize/SimpleSerializationTest.java index 2790a9c94..ef0c6e183 100644 --- a/motan-core/src/test/java/com/weibo/api/motan/serialize/SimpleSerializationTest.java +++ b/motan-core/src/test/java/com/weibo/api/motan/serialize/SimpleSerializationTest.java @@ -54,7 +54,7 @@ public void serialize() throws Exception { assertEquals(entry.getValue(), m2.get(entry.getKey())); } - byte[] bytes = new byte[]{2,34,12,24}; + byte[] bytes = new byte[]{2, 34, 12, 24}; b = serialization.serialize(bytes); assertNotNull(b); assertTrue(b.length > 0); @@ -67,5 +67,37 @@ public void serialize() throws Exception { } } + @Test + public void testSerializeMulti() throws Exception { + SimpleSerialization serialization = new SimpleSerialization(); + Object[] objects = new Object[3]; + objects[0] = "teststring"; + Map map = new HashMap(); + map.put("name", "ray"); + map.put("code", "xxx"); + objects[1] = map; + byte[] bytes = new byte[]{2, 34, 12, 24}; + objects[2] = bytes; + + byte[] b = serialization.serializeMulti(objects); + assertNotNull(b); + assertTrue(b.length > 0); + + Object[] result = serialization.deserializeMulti(b, new Class[]{String.class, Map.class, byte[].class}); + assertEquals(3, result.length); + assertTrue(result[0] instanceof String); + assertEquals(result[0], objects[0]); + assertTrue(result[1] instanceof Map); + Map map2 = (Map) result[1]; + for (Map.Entry entry : map.entrySet()) { + assertEquals(entry.getValue(), map2.get(entry.getKey())); + } + assertTrue(result[2] instanceof byte[]); + + byte[] nbytes = (byte[]) result[2]; + for (int i = 0; i < nbytes.length; i++) { + assertEquals(nbytes[i], bytes[i]); + } + } } \ No newline at end of file From 7db1047e7e6c29195e179641b77efd401c6d20c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BE=BD?= Date: Thu, 11 Jan 2018 11:34:18 +0800 Subject: [PATCH 3/9] fix NPE in #470 --- .../com/weibo/api/motan/registry/consul/ConsulRegistry.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/motan-registry-consul/src/main/java/com/weibo/api/motan/registry/consul/ConsulRegistry.java b/motan-registry-consul/src/main/java/com/weibo/api/motan/registry/consul/ConsulRegistry.java index 9a174bbfd..be881f6e6 100644 --- a/motan-registry-consul/src/main/java/com/weibo/api/motan/registry/consul/ConsulRegistry.java +++ b/motan-registry-consul/src/main/java/com/weibo/api/motan/registry/consul/ConsulRegistry.java @@ -212,13 +212,13 @@ protected String discoverCommand(URL url) { } private ConcurrentHashMap> lookupServiceUpdate(String group) { + ConcurrentHashMap> groupUrls = new ConcurrentHashMap>(); Long lastConsulIndexId = lookupGroupServices.get(group) == null ? 0 : lookupGroupServices.get(group); ConsulResponse> response = lookupConsulService(group, lastConsulIndexId); if (response != null) { List services = response.getValue(); if (services != null && !services.isEmpty() && response.getConsulIndex() > lastConsulIndexId) { - ConcurrentHashMap> groupUrls = new ConcurrentHashMap>(); for (ConsulService service : services) { try { URL url = ConsulUtils.buildUrl(service); @@ -239,7 +239,7 @@ private ConcurrentHashMap> lookupServiceUpdate(String group) { LoggerUtil.info(group + " no need update, lastIndex:" + lastConsulIndexId); } } - return null; + return groupUrls; } private String lookupCommandUpdate(String group) { From 88170d107c94b96d21cf4f33c3ddddf97beba1af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BE=BD?= Date: Thu, 11 Jan 2018 13:38:39 +0800 Subject: [PATCH 4/9] fix #564 --- .../com/weibo/api/motan/proxy/spi/JdkProxyFactory.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/proxy/spi/JdkProxyFactory.java b/motan-core/src/main/java/com/weibo/api/motan/proxy/spi/JdkProxyFactory.java index 4c3db04cd..79479b799 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/proxy/spi/JdkProxyFactory.java +++ b/motan-core/src/main/java/com/weibo/api/motan/proxy/spi/JdkProxyFactory.java @@ -16,12 +16,12 @@ package com.weibo.api.motan.proxy.spi; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Proxy; - import com.weibo.api.motan.core.extension.SpiMeta; import com.weibo.api.motan.proxy.ProxyFactory; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; + /** * jdk proxy * @@ -31,9 +31,10 @@ @SpiMeta(name = "jdk") public class JdkProxyFactory implements ProxyFactory { + @Override @SuppressWarnings("unchecked") public T getProxy(Class clz, InvocationHandler invocationHandler) { - return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[] {clz}, invocationHandler); + return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[]{clz}, invocationHandler); } } From 5036491ccc466b6494720b0b340438a6fdaf2f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BE=BD?= Date: Tue, 16 Jan 2018 17:17:48 +0800 Subject: [PATCH 5/9] fix #470 --- .../support/command/CommandFailbackRegistry.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/registry/support/command/CommandFailbackRegistry.java b/motan-core/src/main/java/com/weibo/api/motan/registry/support/command/CommandFailbackRegistry.java index ca96537b2..a8fa88104 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/registry/support/command/CommandFailbackRegistry.java +++ b/motan-core/src/main/java/com/weibo/api/motan/registry/support/command/CommandFailbackRegistry.java @@ -16,16 +16,15 @@ package com.weibo.api.motan.registry.support.command; -import java.util.HashMap; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.commons.lang3.StringUtils; - import com.weibo.api.motan.registry.NotifyListener; import com.weibo.api.motan.registry.support.FailbackRegistry; import com.weibo.api.motan.rpc.URL; import com.weibo.api.motan.util.LoggerUtil; +import org.apache.commons.lang3.StringUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; public abstract class CommandFailbackRegistry extends FailbackRegistry { @@ -93,7 +92,7 @@ protected List doDiscover(URL url) { finalResult = discoverService(urlCopy); } - LoggerUtil.info("CommandFailbackRegistry discover size: " + finalResult.size() + ", result:" + finalResult.toString()); + LoggerUtil.info("CommandFailbackRegistry discover size: " + (finalResult == null ? "0" : finalResult.size())); return finalResult; } From 25be47c7acb503d5031f8fd74db7980c2d1c97d8 Mon Sep 17 00:00:00 2001 From: luominggang Date: Wed, 21 Mar 2018 15:18:53 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E4=BF=AE=E5=A4=8DNettyClient=E5=9C=A8?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E8=BF=9E=E6=8E=A5=E5=BC=82=E5=B8=B8=E6=97=B6?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E8=BF=9E=E6=8E=A5=E6=B3=84?= =?UTF-8?q?=E9=9C=B2=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../motan/transport/netty/NettyChannel.java | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannel.java b/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannel.java index 0683a45b6..9ec771f83 100644 --- a/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannel.java +++ b/motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannel.java @@ -111,8 +111,9 @@ public synchronized boolean open() { return true; } + ChannelFuture channelFuture = null; try { - ChannelFuture channleFuture = nettyClient.getBootstrap().connect( + channelFuture = nettyClient.getBootstrap().connect( new InetSocketAddress(nettyClient.getUrl().getHost(), nettyClient.getUrl().getPort())); long start = System.currentTimeMillis(); @@ -123,11 +124,11 @@ public synchronized boolean open() { MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR); } // 不去依赖于connectTimeout - boolean result = channleFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS); - boolean success = channleFuture.isSuccess(); + boolean result = channelFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS); + boolean success = channelFuture.isSuccess(); if (result && success) { - channel = channleFuture.getChannel(); + channel = channelFuture.getChannel(); if (channel.getLocalAddress() != null && channel.getLocalAddress() instanceof InetSocketAddress) { localAddress = (InetSocketAddress) channel.getLocalAddress(); } @@ -136,22 +137,25 @@ public synchronized boolean open() { return true; } boolean connected = false; - if(channleFuture.getChannel() != null){ - connected = channleFuture.getChannel().isConnected(); + if(channelFuture.getChannel() != null){ + connected = channelFuture.getChannel().isConnected(); } - if (channleFuture.getCause() != null) { - channleFuture.cancel(); + if (channelFuture.getCause() != null) { + channelFuture.cancel(); throw new MotanServiceException("NettyChannel failed to connect to server, url: " - + nettyClient.getUrl().getUri()+ ", result: " + result + ", success: " + success + ", connected: " + connected, channleFuture.getCause()); + + nettyClient.getUrl().getUri()+ ", result: " + result + ", success: " + success + ", connected: " + connected, channelFuture.getCause()); } else { - channleFuture.cancel(); + channelFuture.cancel(); throw new MotanServiceException("NettyChannel connect to server timeout url: " + nettyClient.getUrl().getUri() + ", cost: " + (System.currentTimeMillis() - start) + ", result: " + result + ", success: " + success + ", connected: " + connected); } } catch (MotanServiceException e) { throw e; } catch (Exception e) { + if (channelFuture != null) { + channelFuture.getChannel().close(); + } throw new MotanServiceException("NettyChannel failed to connect to server, url: " + nettyClient.getUrl().getUri(), e); } finally { From f6b558ee0b1788b93756025cf18a110d8d8de5ff Mon Sep 17 00:00:00 2001 From: ladd Date: Wed, 21 Mar 2018 15:33:56 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=8B=BC?= =?UTF-8?q?=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/weibo/motan/benchmark/BenchmarkService.java | 2 +- .../java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/motan-benchmark/motan-benchmark-api/src/main/java/com/weibo/motan/benchmark/BenchmarkService.java b/motan-benchmark/motan-benchmark-api/src/main/java/com/weibo/motan/benchmark/BenchmarkService.java index 90a0f11f7..bdd80a90c 100644 --- a/motan-benchmark/motan-benchmark-api/src/main/java/com/weibo/motan/benchmark/BenchmarkService.java +++ b/motan-benchmark/motan-benchmark-api/src/main/java/com/weibo/motan/benchmark/BenchmarkService.java @@ -26,6 +26,6 @@ public interface BenchmarkService { public Map getUserTypes(List uids); - public long[] getLastStausIds(long[] uids); + public long[] getLastStatusIds(long[] uids); } diff --git a/motan-benchmark/motan-benchmark-server/src/main/java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java b/motan-benchmark/motan-benchmark-server/src/main/java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java index f299d4193..980fea028 100644 --- a/motan-benchmark/motan-benchmark-server/src/main/java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java +++ b/motan-benchmark/motan-benchmark-server/src/main/java/com/weibo/motan/benchmark/BenchmarkServiceImpl.java @@ -36,7 +36,7 @@ public Map getUserTypes(List uids) { } @Override - public long[] getLastStausIds(long[] uids) { + public long[] getLastStatusIds(long[] uids) { return new long[0]; } } From 0e4e72d5ab1700a29764621792e350f5d921b685 Mon Sep 17 00:00:00 2001 From: ladd Date: Wed, 21 Mar 2018 19:03:01 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=8B=BC?= =?UTF-8?q?=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/weibo/api/motan/codec/AbstractCodec.java | 6 +++--- .../com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/codec/AbstractCodec.java b/motan-core/src/main/java/com/weibo/api/motan/codec/AbstractCodec.java index 019867151..2582959c7 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/codec/AbstractCodec.java +++ b/motan-core/src/main/java/com/weibo/api/motan/codec/AbstractCodec.java @@ -70,7 +70,7 @@ public ObjectInput createInput(InputStream in) { } } - protected static synchronized void initAllSerialziation() { + protected static synchronized void initAllSerialization() { if (serializations == null) { serializations = new ConcurrentHashMap(); try { @@ -89,9 +89,9 @@ protected static synchronized void initAllSerialziation() { } } - protected Serialization getSerializaiontByNum(int serializationNum) { + protected Serialization getSerializationByNum(int serializationNum) { if (serializations == null) { - initAllSerialziation(); + initAllSerialization(); } String name = serializations.get(serializationNum); Serialization s = null; diff --git a/motan-core/src/main/java/com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java b/motan-core/src/main/java/com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java index 9bd35d955..a77b38a8f 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java +++ b/motan-core/src/main/java/com/weibo/api/motan/protocol/v2motan/MotanV2Codec.java @@ -54,7 +54,7 @@ public class MotanV2Codec extends AbstractCodec { static { - initAllSerialziation(); + initAllSerialization(); } @Override @@ -233,7 +233,7 @@ public Object decode(Channel channel, String remoteIp, byte[] data) throws IOExc body = ByteUtil.unGzip(body); } //默认自适应序列化 - Serialization serialization = getSerializaiontByNum(header.getSerialize()); + Serialization serialization = getSerializationByNum(header.getSerialize()); obj = new DeserializableObject(serialization, body); } if (header.isRequest()) { From ca4e85da1eb750a157cc42f54474c543ed60c1b7 Mon Sep 17 00:00:00 2001 From: ladd Date: Thu, 22 Mar 2018 11:21:59 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E9=92=88=E5=AF=B9MathUtil=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E4=BA=86=E4=B8=80=E4=BA=9B=E5=AE=8C=E5=96=84=EF=BC=9A?= =?UTF-8?q?=201.=09=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=202.=09=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=203.=20=09=E5=AF=B9getPositive=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E8=BF=9B=E8=A1=8C=E4=BA=86=E9=87=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?=EF=BC=88=E5=9B=A0=E4=B8=BA=E5=85=B6=E5=AE=9E=E7=8E=B0=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E4=B9=9F=E4=BC=9A=E8=BF=94=E5=9B=9E0?= =?UTF-8?q?=EF=BC=8C=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=BAgetNonNegative?= =?UTF-8?q?=EF=BC=89=EF=BC=8C=E5=B9=B6=E9=80=9A=E8=BF=87=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E4=BF=AE=E6=94=B9=E4=BA=86=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E8=AF=A5=E6=96=B9=E6=B3=95=E7=9A=84=E7=9B=B8=E5=85=B3=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfigurableWeightLoadBalance.java | 2 +- .../ConsistentHashLoadBalance.java | 2 +- .../loadbalance/RoundRobinLoadBalance.java | 10 ++--- .../transport/AbstractSharedPoolClient.java | 2 +- .../com/weibo/api/motan/util/MathUtil.java | 22 ++++++++- .../weibo/api/motan/util/MathUtilTest.java | 45 +++++++++++++++++++ 6 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 motan-core/src/test/java/com/weibo/api/motan/util/MathUtilTest.java diff --git a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConfigurableWeightLoadBalance.java b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConfigurableWeightLoadBalance.java index 9b64e58bc..6fd2e123f 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConfigurableWeightLoadBalance.java +++ b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConfigurableWeightLoadBalance.java @@ -202,7 +202,7 @@ Referer next() { String group = randomKeyList.get(ThreadLocalRandom.current().nextInt(randomKeySize)); AtomicInteger ai = cursors.get(group); List> referers = groupReferers.get(group); - return referers.get(MathUtil.getPositive(ai.getAndIncrement()) % referers.size()); + return referers.get(MathUtil.getNonNegative(ai.getAndIncrement()) % referers.size()); } // 求最大公约数 diff --git a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConsistentHashLoadBalance.java b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConsistentHashLoadBalance.java index 24ce21d2a..f57a3f85c 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConsistentHashLoadBalance.java +++ b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/ConsistentHashLoadBalance.java @@ -89,7 +89,7 @@ private int getHash(Request request) { } else { hashcode = Arrays.hashCode(request.getArguments()); } - return MathUtil.getPositive(hashcode); + return MathUtil.getNonNegative(hashcode); } diff --git a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/RoundRobinLoadBalance.java b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/RoundRobinLoadBalance.java index ecb3f0b6a..ef8723a93 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/RoundRobinLoadBalance.java +++ b/motan-core/src/main/java/com/weibo/api/motan/cluster/loadbalance/RoundRobinLoadBalance.java @@ -40,7 +40,7 @@ public class RoundRobinLoadBalance extends AbstractLoadBalance { protected Referer doSelect(Request request) { List> referers = getReferers(); - int index = getNextPositive(); + int index = getNextNonNegative(); for (int i = 0; i < referers.size(); i++) { Referer ref = referers.get((i + index) % referers.size()); if (ref.isAvailable()) { @@ -54,7 +54,7 @@ protected Referer doSelect(Request request) { protected void doSelectToHolder(Request request, List> refersHolder) { List> referers = getReferers(); - int index = getNextPositive(); + int index = getNextNonNegative(); for (int i = 0, count = 0; i < referers.size() && count < MAX_REFERER_COUNT; i++) { Referer referer = referers.get((i + index) % referers.size()); if (referer.isAvailable()) { @@ -64,8 +64,8 @@ protected void doSelectToHolder(Request request, List> refersHolder) } } - // get positive int - private int getNextPositive() { - return MathUtil.getPositive(idx.incrementAndGet()); + // get non-negative int + private int getNextNonNegative() { + return MathUtil.getNonNegative(idx.incrementAndGet()); } } diff --git a/motan-core/src/main/java/com/weibo/api/motan/transport/AbstractSharedPoolClient.java b/motan-core/src/main/java/com/weibo/api/motan/transport/AbstractSharedPoolClient.java index 05f801281..c26597877 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/transport/AbstractSharedPoolClient.java +++ b/motan-core/src/main/java/com/weibo/api/motan/transport/AbstractSharedPoolClient.java @@ -84,7 +84,7 @@ private void createConnections() { } protected Channel getChannel() throws MotanServiceException { - int index = MathUtil.getPositive(idx.getAndIncrement()); + int index = MathUtil.getNonNegative(idx.getAndIncrement()); Channel channel; for (int i = index; i < connections + index; i++) { diff --git a/motan-core/src/main/java/com/weibo/api/motan/util/MathUtil.java b/motan-core/src/main/java/com/weibo/api/motan/util/MathUtil.java index 173143adf..2b118f2bd 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/util/MathUtil.java +++ b/motan-core/src/main/java/com/weibo/api/motan/util/MathUtil.java @@ -23,6 +23,14 @@ public class MathUtil { + + /** + * 针对int类型字符串进行解析,如果存在格式错误,则返回默认值(defaultValue) + * Parse intStr, return defaultValue when numberFormatException occurs + * @param intStr + * @param defaultValue + * @return + */ public static int parseInt(String intStr, int defaultValue) { try { return Integer.parseInt(intStr); @@ -32,6 +40,13 @@ public static int parseInt(String intStr, int defaultValue) { } } + /** + * 针对long类型字符串进行解析,如果存在格式错误,则返回默认值(defaultValue) + * Parse longStr, return defaultValue when numberFormatException occurs + * @param longStr + * @param defaultValue + * @return + */ public static long parseLong(String longStr, long defaultValue){ try { return Long.parseLong(longStr); @@ -41,11 +56,14 @@ public static long parseLong(String longStr, long defaultValue){ } /** - * return positive int value of originValue + * 通过二进制位操作将originValue转化为非负数: + * 0和正数返回本身 + * 负数通过二进制首位取反转化为正数或0(Integer.MIN_VALUE将转换为0) + * return non-negative int value of originValue * @param originValue * @return positive int */ - public static int getPositive(int originValue){ + public static int getNonNegative(int originValue){ return 0x7fffffff & originValue; } } diff --git a/motan-core/src/test/java/com/weibo/api/motan/util/MathUtilTest.java b/motan-core/src/test/java/com/weibo/api/motan/util/MathUtilTest.java new file mode 100644 index 000000000..a1327e25c --- /dev/null +++ b/motan-core/src/test/java/com/weibo/api/motan/util/MathUtilTest.java @@ -0,0 +1,45 @@ +package com.weibo.api.motan.util; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class MathUtilTest { + + @Test + public void parseInt() { + + assertTrue(MathUtil.parseInt("-1",0)==-1); + assertTrue(MathUtil.parseInt("0",-1)==0); + assertTrue(MathUtil.parseInt("1",0)==1); + + assertTrue(MathUtil.parseInt("",0)==0); + assertTrue(MathUtil.parseInt(null,0)==0); + assertTrue(MathUtil.parseInt("Invalid Int String",0)==0); + + } + + @Test + public void parseLong() { + + assertTrue(MathUtil.parseLong("-1",0)==-1); + assertTrue(MathUtil.parseLong("0",-1)==0); + assertTrue(MathUtil.parseLong("1",0)==1); + + assertTrue(MathUtil.parseLong("",0)==0); + assertTrue(MathUtil.parseLong(null,0)==0); + assertTrue(MathUtil.parseLong("Invalid Int String",0)==0); + } + + @Test + public void getNonNegative() { + + assertTrue(MathUtil.getNonNegative(Integer.MIN_VALUE)==0); + assertTrue(MathUtil.getNonNegative(-1)>0); + + assertTrue(MathUtil.getNonNegative(0)==0); + assertTrue(MathUtil.getNonNegative(1)==1); + assertTrue(MathUtil.getNonNegative(Integer.MAX_VALUE)==Integer.MAX_VALUE); + + } +} \ No newline at end of file