Commit b1028da3 by 胡青青

feat: [none] 添加key的solt日志,分析倾斜原因

parent 8dc866b2
...@@ -9,6 +9,8 @@ import org.aspectj.lang.annotation.Aspect; ...@@ -9,6 +9,8 @@ import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import redis.clients.util.JedisClusterCRC16;
import redis.clients.util.SafeEncoder;
import java.io.*; import java.io.*;
import java.util.Map; import java.util.Map;
...@@ -74,7 +76,13 @@ public class JedisClusterAspect { ...@@ -74,7 +76,13 @@ public class JedisClusterAspect {
private LongAdder llenLongAdder = new LongAdder(); private LongAdder llenLongAdder = new LongAdder();
private LongAdder missllenLongAdder = new LongAdder(); private LongAdder missllenLongAdder = new LongAdder();
private Map<String, ConcurrentHashMap<String, AtomicInteger>> solfDistributedMap = new ConcurrentHashMap<>();
public JedisClusterAspect() { public JedisClusterAspect() {
solfDistributedMap.put("firstNode", new ConcurrentHashMap<String, AtomicInteger>());
solfDistributedMap.put("secondNode", new ConcurrentHashMap<String, AtomicInteger>());
solfDistributedMap.put("thirdNode", new ConcurrentHashMap<String, AtomicInteger>());
solfDistributedMap.put("fourthNode", new ConcurrentHashMap<String, AtomicInteger>());
new PringLogThread().start(); new PringLogThread().start();
} }
...@@ -94,11 +102,11 @@ public class JedisClusterAspect { ...@@ -94,11 +102,11 @@ public class JedisClusterAspect {
while (true) { while (true) {
BufferedWriter out = null; BufferedWriter out = null;
try { try {
Thread.sleep(60 * 60 * 1000); Thread.sleep( 5*60 * 1000);
tempMap.clear(); tempMap.clear();
tempMap.putAll(missKeyMap); tempMap.putAll(missKeyMap);
missKeyMap.clear(); missKeyMap.clear();
StringBuffer logStr = new StringBuffer("\n"); StringBuffer logStr = new StringBuffer();
logStr.append("get调用:" + allGetLongAdder.intValue() + "次,未命中:" + missGetLongAdder.intValue() + "次"); logStr.append("get调用:" + allGetLongAdder.intValue() + "次,未命中:" + missGetLongAdder.intValue() + "次");
logStr.append("\n"); logStr.append("\n");
logStr.append("rpop调用:" + allRpopLongAdder.intValue() + "次,未命中:" + missRpopLongAdder.intValue() + "次"); logStr.append("rpop调用:" + allRpopLongAdder.intValue() + "次,未命中:" + missRpopLongAdder.intValue() + "次");
...@@ -126,13 +134,20 @@ public class JedisClusterAspect { ...@@ -126,13 +134,20 @@ public class JedisClusterAspect {
logStr.append("ttl调用:" + ttlLongAdder.intValue() + "次,未命中:" + misshttlLongAdder.intValue() + "次"); logStr.append("ttl调用:" + ttlLongAdder.intValue() + "次,未命中:" + misshttlLongAdder.intValue() + "次");
logStr.append("\n"); logStr.append("\n");
logStr.append("zrank调用:" + zrankLongAdder.intValue() + "次,未命中:" + misszrankLongAdder.intValue() + "次"); logStr.append("zrank调用:" + zrankLongAdder.intValue() + "次,未命中:" + misszrankLongAdder.intValue() + "次");
logStr.append("\n");
logStr.append("getSetR调用:" + getSetRLongAdder.intValue() + "次,未命中:" + missgetSetRLongAdder.intValue() + "次"); logStr.append("getSetR调用:" + getSetRLongAdder.intValue() + "次,未命中:" + missgetSetRLongAdder.intValue() + "次");
logStr.append("\n"); logStr.append("\n");
logStr.append("setnx调用:" + setnxLongAdder.intValue() + "次,未命中:" + missSetnxLongAdder.intValue() + "次"); logStr.append("setnx调用:" + setnxLongAdder.intValue() + "次,未命中:" + missSetnxLongAdder.intValue() + "次");
logStr.append("\n"); logStr.append("\n");
logStr.append("hmget调用:" + hmgetLongAdder.intValue() + "次,未命中:" + misshmgetLongAdder.intValue() + "次"); logStr.append("hmget调用:" + hmgetLongAdder.intValue() + "次,未命中:" + misshmgetLongAdder.intValue() + "次");
logStr.append("\n"); logStr.append("\n");
logStr.append("\n"); for (Map.Entry<String, ConcurrentHashMap<String, AtomicInteger>> entry : solfDistributedMap.entrySet()) {
for (Map.Entry<String, AtomicInteger> childEntry : entry.getValue().entrySet()) {
logStr.append(entry.getKey() + "节点上 key:" + childEntry.getKey());
logStr.append(",调用次数:" + childEntry.getValue());
logStr.append("\n");
}
}
String fileName = "redisOperate" + DateUtils.getYmdHmsTime() + ".log"; String fileName = "redisOperate" + DateUtils.getYmdHmsTime() + ".log";
out = new BufferedWriter(new FileWriter(path + fileName)); out = new BufferedWriter(new FileWriter(path + fileName));
out.write(logStr.toString()); out.write(logStr.toString());
...@@ -161,6 +176,7 @@ public class JedisClusterAspect { ...@@ -161,6 +176,7 @@ public class JedisClusterAspect {
switch (methodName) { switch (methodName) {
case "get": case "get":
allGetLongAdder.add(1); allGetLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
missGetLongAdder.add(1); missGetLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -168,6 +184,7 @@ public class JedisClusterAspect { ...@@ -168,6 +184,7 @@ public class JedisClusterAspect {
break; break;
case "rpop": case "rpop":
allRpopLongAdder.add(1); allRpopLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
missRpopLongAdder.add(1); missRpopLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -175,6 +192,7 @@ public class JedisClusterAspect { ...@@ -175,6 +192,7 @@ public class JedisClusterAspect {
break; break;
case "hgetAll": case "hgetAll":
allHgetAllLongAdder.add(1); allHgetAllLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
missHgetAllLongAdder.add(1); missHgetAllLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -182,6 +200,7 @@ public class JedisClusterAspect { ...@@ -182,6 +200,7 @@ public class JedisClusterAspect {
break; break;
case "getSet": case "getSet":
getSetLongAdder.add(1); getSetLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
missGetSetLongAdder.add(1); missGetSetLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -189,6 +208,7 @@ public class JedisClusterAspect { ...@@ -189,6 +208,7 @@ public class JedisClusterAspect {
break; break;
case "getSetR": case "getSetR":
getSetRLongAdder.add(1); getSetRLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
missgetSetRLongAdder.add(1); missgetSetRLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -196,6 +216,7 @@ public class JedisClusterAspect { ...@@ -196,6 +216,7 @@ public class JedisClusterAspect {
break; break;
case "setnx": case "setnx":
setnxLongAdder.add(1); setnxLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
missSetnxLongAdder.add(1); missSetnxLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -203,6 +224,7 @@ public class JedisClusterAspect { ...@@ -203,6 +224,7 @@ public class JedisClusterAspect {
break; break;
case "hmget": case "hmget":
hmgetLongAdder.add(1); hmgetLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misshmgetLongAdder.add(1); misshmgetLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -210,6 +232,7 @@ public class JedisClusterAspect { ...@@ -210,6 +232,7 @@ public class JedisClusterAspect {
break; break;
case "hget": case "hget":
hgetLongAdder.add(1); hgetLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misshgetLongAdder.add(1); misshgetLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -217,6 +240,7 @@ public class JedisClusterAspect { ...@@ -217,6 +240,7 @@ public class JedisClusterAspect {
break; break;
case "hlen": case "hlen":
hlenLongAdder.add(1); hlenLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misshlenLongAdder.add(1); misshlenLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -224,6 +248,7 @@ public class JedisClusterAspect { ...@@ -224,6 +248,7 @@ public class JedisClusterAspect {
break; break;
case "lrange": case "lrange":
lrangeLongAdder.add(1); lrangeLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misslrangeLongAdder.add(1); misslrangeLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -231,6 +256,7 @@ public class JedisClusterAspect { ...@@ -231,6 +256,7 @@ public class JedisClusterAspect {
break; break;
case "llen": case "llen":
llenLongAdder.add(1); llenLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
missllenLongAdder.add(1); missllenLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -238,6 +264,7 @@ public class JedisClusterAspect { ...@@ -238,6 +264,7 @@ public class JedisClusterAspect {
break; break;
case "hkeys": case "hkeys":
hkeysLongAdder.add(1); hkeysLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misshkeysLongAdder.add(1); misshkeysLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -245,6 +272,7 @@ public class JedisClusterAspect { ...@@ -245,6 +272,7 @@ public class JedisClusterAspect {
break; break;
case "lpop": case "lpop":
lpopLongAdder.add(1); lpopLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misslpopLongAdder.add(1); misslpopLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -252,6 +280,7 @@ public class JedisClusterAspect { ...@@ -252,6 +280,7 @@ public class JedisClusterAspect {
break; break;
case "zcard": case "zcard":
zcardLongAdder.add(1); zcardLongAdder.add(1);
setSoltDistributed(key);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misszcardLongAdder.add(1); misszcardLongAdder.add(1);
addMissCount(missKeyMap, key); addMissCount(missKeyMap, key);
...@@ -265,6 +294,7 @@ public class JedisClusterAspect { ...@@ -265,6 +294,7 @@ public class JedisClusterAspect {
} }
break; break;
case "ttl": case "ttl":
setSoltDistributed(key);
ttlLongAdder.add(1); ttlLongAdder.add(1);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misshttlLongAdder.add(1); misshttlLongAdder.add(1);
...@@ -272,6 +302,7 @@ public class JedisClusterAspect { ...@@ -272,6 +302,7 @@ public class JedisClusterAspect {
} }
break; break;
case "zrank": case "zrank":
setSoltDistributed(key);
zrankLongAdder.add(1); zrankLongAdder.add(1);
if (result == null || StringUtil.isEmpty(result.toString())) { if (result == null || StringUtil.isEmpty(result.toString())) {
misszrankLongAdder.add(1); misszrankLongAdder.add(1);
...@@ -283,6 +314,38 @@ public class JedisClusterAspect { ...@@ -283,6 +314,38 @@ public class JedisClusterAspect {
} }
} }
/**
* 计算key的分布
*
* @param key
*/
private void setSoltDistributed(String key) {
String[] keys = key.split(":");
if (keys.length < 2) {
return;
}
String keyPrefix = key.substring(0, key.lastIndexOf(":"));
int slot = JedisClusterCRC16.getSlot(SafeEncoder.encode(key));
String mapkey = "";
if (slot >= 0 && slot <= 4095) {
mapkey = "firstNode";
} else if (slot >= 4096 && slot <= 8191) {
mapkey = "secondNode";
} else if (slot >= 8192 && slot <= 12287) {
mapkey = "thirdNode";
} else if (slot > 12288 && slot <= 16383) {
mapkey = "fourthNode";
}
ConcurrentHashMap<String, AtomicInteger> map = solfDistributedMap.get(mapkey);
AtomicInteger missCount = map.get(keyPrefix);
if (missCount == null) {
missCount = new AtomicInteger(1);
} else {
missCount.getAndIncrement();
}
map.put(keyPrefix, missCount);
}
private static String getResourceBasePath() { private static String getResourceBasePath() {
// 获取跟目录 // 获取跟目录
File path = null; File path = null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment