Permalink
Browse files

change threadLocalRandom;fix index overflow;check serialize in url ca…

…nServer method;avoiding dns lookup
  • Loading branch information...
1 parent 8939d4c commit 98f3145db0af31effc878bfdeb2846677ce75913 @rayzhang0603 rayzhang0603 committed Nov 17, 2016
View
@@ -3,6 +3,7 @@
*/logs/
*/*/logs/
*/target/
+bin/
# maven ignore
target/
@@ -20,6 +20,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
import com.weibo.api.motan.core.extension.SpiMeta;
import com.weibo.api.motan.rpc.Referer;
@@ -43,14 +44,13 @@
*/
@SpiMeta(name = "activeWeight")
public class ActiveWeightLoadBalance<T> extends AbstractLoadBalance<T> {
- private static Random random = new Random();
@Override
protected Referer<T> doSelect(Request request) {
List<Referer<T>> referers = getReferers();
int refererSize = referers.size();
- int startIndex = random.nextInt(refererSize);
+ int startIndex = ThreadLocalRandom.current().nextInt(refererSize);
int currentCursor = 0;
int currentAvailableCursor = 0;
@@ -83,7 +83,7 @@ protected void doSelectToHolder(Request request, List<Referer<T>> refersHolder)
List<Referer<T>> referers = getReferers();
int refererSize = referers.size();
- int startIndex = random.nextInt(refererSize);
+ int startIndex = ThreadLocalRandom.current().nextInt(refererSize);
int currentCursor = 0;
int currentAvailableCursor = 0;
@@ -21,6 +21,8 @@
import com.weibo.api.motan.rpc.Request;
import com.weibo.api.motan.util.CollectionUtil;
import com.weibo.api.motan.util.LoggerUtil;
+import com.weibo.api.motan.util.MathUtil;
+
import org.apache.commons.lang3.StringUtils;
import java.util.*;
@@ -200,7 +202,7 @@ public void setWeightString(String weightString) {
String group = randomKeyList.get(ThreadLocalRandom.current().nextInt(randomKeySize));
AtomicInteger ai = cursors.get(group);
List<Referer<T>> referers = groupReferers.get(group);
- return referers.get(ai.getAndIncrement() % referers.size());
+ return referers.get(MathUtil.getPositive(ai.getAndIncrement()) % referers.size());
}
// 求最大公约数
@@ -25,6 +25,7 @@
import com.weibo.api.motan.core.extension.SpiMeta;
import com.weibo.api.motan.rpc.Referer;
import com.weibo.api.motan.rpc.Request;
+import com.weibo.api.motan.util.MathUtil;
/**
*
@@ -82,11 +83,13 @@ protected void doSelectToHolder(Request request, List<Referer<T>> refersHolder)
}
private int getHash(Request request) {
+ int hashcode;
if (request.getArguments() == null || request.getArguments().length == 0) {
- return 0x7fffffff & request.hashCode();
+ hashcode = request.hashCode();
} else {
- return 0x7fffffff & Arrays.hashCode(request.getArguments());
+ hashcode = Arrays.hashCode(request.getArguments());
}
+ return MathUtil.getPositive(hashcode);
}
@@ -23,6 +23,7 @@
import com.weibo.api.motan.util.NetUtils;
import java.util.*;
+import java.util.concurrent.ThreadLocalRandom;
/**
* "本地服务优先" 负载均衡
@@ -42,7 +43,6 @@
@SpiMeta(name = "localFirst")
public class LocalFirstLoadBalance<T> extends AbstractLoadBalance<T> {
public static final int MAX_REFERER_COUNT = 10;
- private static Random random = new Random();
public static long ipToLong(final String addr) {
final String[] addressBytes = addr.split("\\.");
@@ -107,7 +107,7 @@ protected void doSelectToHolder(Request request, List<Referer<T>> refersHolder)
}
int refererSize = referers.size();
- int startIndex = random.nextInt(refererSize);
+ int startIndex = ThreadLocalRandom.current().nextInt(refererSize);
int currentCursor = 0;
int currentAvailableCursor = 0;
@@ -17,6 +17,7 @@
package com.weibo.api.motan.cluster.loadbalance;
import java.util.List;
+import java.util.concurrent.ThreadLocalRandom;
import com.weibo.api.motan.core.extension.SpiMeta;
import com.weibo.api.motan.rpc.Referer;
@@ -36,7 +37,7 @@
protected Referer<T> doSelect(Request request) {
List<Referer<T>> referers = getReferers();
- int idx = (int) (Math.random() * referers.size());
+ int idx = (int) (ThreadLocalRandom.current().nextDouble() * referers.size());
for (int i = 0; i < referers.size(); i++) {
Referer<T> ref = referers.get((i + idx) % referers.size());
if (ref.isAvailable()) {
@@ -50,7 +51,7 @@
protected void doSelectToHolder(Request request, List<Referer<T>> refersHolder) {
List<Referer<T>> referers = getReferers();
- int idx = (int) (Math.random() * referers.size());
+ int idx = (int) (ThreadLocalRandom.current().nextDouble() * referers.size());
for (int i = 0; i < referers.size(); i++) {
Referer<T> referer = referers.get((i + idx) % referers.size());
if (referer.isAvailable()) {
@@ -22,6 +22,7 @@
import com.weibo.api.motan.core.extension.SpiMeta;
import com.weibo.api.motan.rpc.Referer;
import com.weibo.api.motan.rpc.Request;
+import com.weibo.api.motan.util.MathUtil;
/**
*
@@ -65,6 +66,6 @@ protected void doSelectToHolder(Request request, List<Referer<T>> refersHolder)
// get positive int
private int getNextPositive() {
- return 0x7fffffff & idx.incrementAndGet();
+ return MathUtil.getPositive(idx.incrementAndGet());
}
}
@@ -30,6 +30,8 @@
*/
@SpiMeta(name = "statistic")
public class AccessStatisticFilter implements Filter {
+ protected static Application RPC_SERVICES = new Application(ApplicationInfo.STATISTIC, "rpc_service");
+
@Override
public Response filter(Caller<?> caller, Request request) {
long start = System.currentTimeMillis();
@@ -68,7 +70,7 @@ public Response filter(Caller<?> caller, Request request) {
String statName =
caller.getUrl().getProtocol() + MotanConstants.PROTOCOL_SEPARATOR + MotanFrameworkUtil.getGroupMethodString(request);
if (caller instanceof Provider) {
- application = new Application(ApplicationInfo.STATISTIC, "rpc_service");
+ application = RPC_SERVICES;
StatsUtil.accessStatistic(statName, application, end, end - start, bizProcessTime, accessStatus);
}
application = ApplicationInfo.getApplication(caller.getUrl());
@@ -1,17 +1,15 @@
/*
- * Copyright 2009-2016 Weibo, Inc.
- *
- * Licensed 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.
+ * Copyright 2009-2016 Weibo, Inc.
+ *
+ * Licensed 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 com.weibo.api.motan.filter;
@@ -23,13 +21,15 @@
import com.weibo.api.motan.exception.MotanFrameworkException;
import com.weibo.api.motan.rpc.*;
import com.weibo.api.motan.util.ReflectUtil;
+
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;
/**
@@ -135,7 +135,8 @@ public Response filter(Caller<?> caller, Request request) {
return response;
}
- private Object invoke(Object clz, Method method, Object[] args, MockInfo info) throws InterruptedException, InvocationTargetException, IllegalAccessException {
+ private Object invoke(Object clz, Method method, Object[] args, MockInfo info) throws InterruptedException, InvocationTargetException,
+ IllegalAccessException {
info.callNum.addAndGet(1);
@@ -151,7 +152,7 @@ private long caclSleepTime(MockInfo info) {
long sleepTime;
- int n = new Random().nextInt(1000);
+ int n = ThreadLocalRandom.current().nextInt(1000);
long delta = (long) (rMean - info.mean + 1);
if (n < 900) {
@@ -175,7 +176,7 @@ private long caclSleepTime(MockInfo info) {
while (info.errorRate * rate < 1) {
rate *= 10;
}
- if (new Random().nextInt(rate) == 1) {
+ if (ThreadLocalRandom.current().nextInt(rate) == 1) {
throw new RuntimeException();
}
}
@@ -397,7 +397,12 @@ public boolean canServe(URL refUrl) {
if (!version.equals(refVersion)) {
return false;
}
-
+ // check serialize
+ String serialize = getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue());
+ String refSerialize = refUrl.getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue());
+ if (!serialize.equals(refSerialize)) {
+ return false;
+ }
// 由于需要提供跨group访问rpc的能力,所以不再验证group是否一致。
return true;
}
@@ -53,7 +53,7 @@
protected ConcurrentMap<String, AtomicInteger> requestCounters = new ConcurrentHashMap<String, AtomicInteger>();
protected AtomicInteger totalCounter = new AtomicInteger(0);
- protected Object lock = new Object();
+
public ProviderProtectedMessageRouter() {
super();
@@ -73,11 +73,8 @@ protected Response call(Request request, Provider<?> provider) {
try {
int requestCounter = 0, totalCounter = 0;
- synchronized (lock) {
- requestCounter = incrRequestCounter(requestKey);
- totalCounter = incrTotalCounter();
- }
-
+ requestCounter = incrRequestCounter(requestKey);
+ totalCounter = incrTotalCounter();
if (isAllowRequest(requestCounter, totalCounter, maxThread, request)) {
return super.call(request, provider);
} else {
@@ -86,10 +83,8 @@ protected Response call(Request request, Provider<?> provider) {
}
} finally {
- synchronized (lock) {
- decrTotalCounter();
- decrRequestCounter(requestKey);
- }
+ decrTotalCounter();
+ decrRequestCounter(requestKey);
}
}
@@ -31,4 +31,13 @@ public static int parseInt(String intStr, int defaultValue) {
return defaultValue;
}
}
+
+ /**
+ * return positive int value of originValue
+ * @param originValue
+ * @return positive int
+ */
+ public static int getPositive(int originValue){
+ return 0x7fffffff & originValue;
+ }
}
@@ -175,14 +175,17 @@ public static boolean isValidAddress(InetAddress address) {
String name = address.getHostAddress();
return (name != null && !ANYHOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name).matches());
}
-
+ //return ip to avoid lookup dns
public static String getHostName(SocketAddress socketAddress) {
if (socketAddress == null) {
return null;
}
if (socketAddress instanceof InetSocketAddress) {
- return ((InetSocketAddress) socketAddress).getHostName();
+ InetAddress addr = ((InetSocketAddress) socketAddress).getAddress();
+ if(addr != null){
+ return addr.getHostAddress();
+ }
}
return null;

0 comments on commit 98f3145

Please sign in to comment.