diff --git a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/Version.java b/rds-proxy/proxy/src/main/java/com/tongtech/proxy/Version.java index 61b1abbe815113149752e959ec349a1eb1d0c824..f84bd6a340363a41c5c5570e32bb66cd2050d33c 100644 --- a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/Version.java +++ b/rds-proxy/proxy/src/main/java/com/tongtech/proxy/Version.java @@ -3,5 +3,5 @@ package com.tongtech.proxy; public class Version { public static final String Version ="2.2.1.4"; public static final String ProductName = "TongRDS Proxy"; - public static final String BuildTime = "2023-12-29 16:57:54"; + public static final String BuildTime = "2024-02-01 14:24:59"; } \ No newline at end of file diff --git a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Base16.java b/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Base16.java deleted file mode 100644 index 7e875eab789a321faa15012ac6f6b07b4bf7f3b5..0000000000000000000000000000000000000000 --- a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Base16.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.tongtech.proxy.core.utils; - -public class Base16 { - /** - * byte数组和String转换 - */ - private static final char[] CODE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - - public static String encode(byte[] b) { - if (b == null) { - return null; - } - StringBuilder buf = new StringBuilder(b.length * 2); - for (byte b1 : b) { - buf.append(CODE[(b1 & 0xf0) >> 4]); - buf.append(CODE[b1 & 0x0f]); - } - return buf.toString(); - } - - public static byte[] decode(String s) { - if (s == null) { - return null; - } - byte[] buf = new byte[s.length() >> 1]; - for (int i = 0; i < s.length() - 1; i += 2) { - char c = s.charAt(i); - int i1 = 0; - if (c >= '0' && c <= '9') { - i1 = c - '0'; - } else if (c >= 'a' && c <= 'f') { - i1 = c - 'a' + 10; - } else if (c >= 'A' && c <= 'F') { - i1 = c - 'A' + 10; - } - - c = s.charAt(i + 1); - int i2 = 0; - if (c >= '0' && c <= '9') { - i2 = c - '0'; - } else if (c >= 'a' && c <= 'f') { - i2 = c - 'a' + 10; - } else if (c >= 'A' && c <= 'F') { - i2 = c - 'A' + 10; - } - buf[i >> 1] = (byte) ((i1 << 4) | i2); - } - return buf; - } -} diff --git a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Base64.java b/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Base64.java deleted file mode 100644 index 89d873796c2a46e41abd6d03ad3e9748544f0186..0000000000000000000000000000000000000000 --- a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Base64.java +++ /dev/null @@ -1,304 +0,0 @@ -/** - * 自有算法的BASE64转换(非标准算法),经过自有算法转换的字符串,在比较大小时不改变原来的顺序 - * 转换举例: - * 0 : A+** - * 1 : AE** - * 10 : AH+* - * 11 : AH2* - * 2 : AU** - * 3 : Ak** - * 4 : B+** - * 5 : BE** - * a : ME** - * aa : MK2* - * aaa : MK3V - * aab : MK3W - * aac: MK3X - * b : MU** - * bb : Ma6* - * bbb : Ma7W - * c : Mk** - * cc : MqA* - * ccc : MqBX - * 16789 : AHMrC1Y* - */ -package com.tongtech.proxy.core.utils; - - -import java.nio.charset.StandardCharsets; - -public class Base64 { - private static final char PADDING = '*'; - - private static final byte[] encodingTable = { - (byte) '+', (byte) '/', - - /* 2 */ - (byte) '0', (byte) '1', (byte) '2', - (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', - (byte) '8', (byte) '9', - - /* 12 */ - (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', - (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', - (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', - (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', - (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', - (byte) 'Z', - - /* 38 */ - (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', - (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', - (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', - (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', - (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', - (byte) 'y', (byte) 'z', - - }; - private static final byte[] decodingTable; - - static { - decodingTable = new byte[128]; - - for (int i = 0; i < 128; i++) { - decodingTable[i] = (byte) -1; - } - - decodingTable['+'] = 0; - decodingTable['/'] = 1; - - for (int i = '0'; i <= '9'; i++) { - decodingTable[i] = (byte) (i - '0' + 2); - } - - for (int i = 'A'; i <= 'Z'; i++) { - decodingTable[i] = (byte) (i - 'A' + 12); - } - - for (int i = 'a'; i <= 'z'; i++) { - decodingTable[i] = (byte) (i - 'a' + 38); - } - } - - /** - * 将输入的字节数组转换成可打印的Base64字符串 - * - * @param data - * @param offset - * @param len - * @return - */ - public static String encode(byte[] data, int offset, int len) { - byte[] bytes; - - int modulus = len % 3; - - if (modulus == 0) { - bytes = new byte[(4 * len) / 3]; - } else { - bytes = new byte[4 * ((len / 3) + 1)]; - } - - int dataLength = (len - modulus); - int a1; - int a2; - int a3; - - for (int i = offset, j = 0; i < dataLength + offset; i += 3, j += 4) { - a1 = data[i] & 0xff; - a2 = data[i + 1] & 0xff; - a3 = data[i + 2] & 0xff; - - bytes[j] = encodingTable[(a1 >>> 2) & 0x3f]; - bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f]; - bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f]; - bytes[j + 3] = encodingTable[a3 & 0x3f]; - } - - int b1; - int b2; - int b3; - int d1; - int d2; - - switch (modulus) { - case 0: /* nothing left to do */ - break; - - case 1: - d1 = data[offset + len - 1] & 0xff; - b1 = (d1 >>> 2) & 0x3f; - b2 = (d1 << 4) & 0x3f; - - bytes[bytes.length - 4] = encodingTable[b1]; - bytes[bytes.length - 3] = encodingTable[b2]; - bytes[bytes.length - 2] = PADDING;//(byte) '='; - bytes[bytes.length - 1] = PADDING;//(byte) '='; - - break; - - case 2: - d1 = data[offset + len - 2] & 0xff; - d2 = data[offset + len - 1] & 0xff; - - b1 = (d1 >>> 2) & 0x3f; - b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f; - b3 = (d2 << 2) & 0x3f; - - bytes[bytes.length - 4] = encodingTable[b1]; - bytes[bytes.length - 3] = encodingTable[b2]; - bytes[bytes.length - 2] = encodingTable[b3]; - bytes[bytes.length - 1] = PADDING;// (byte) '='; - - break; - } - - return new String(bytes); - } - - /** - * 将Base64的字符串恢复回真正的字节数据数据 - * - * @param data - * @return - */ - public static byte[] decode(String data) { - return decode(data.getBytes()); - } - - private static byte[] decode(byte[] data) { - byte[] bytes; - byte b1; - byte b2; - byte b3; - byte b4; - - if (data == null) { - return null; - } else if (data.length == 0) { - return data; - } else { - try { - if (data[data.length - 2] == PADDING/* '=' */) { - bytes = new byte[(((data.length / 4) - 1) * 3) + 1]; - } else if (data[data.length - 1] == PADDING/* '=' */) { - bytes = new byte[(((data.length / 4) - 1) * 3) + 2]; - } else { - bytes = new byte[((data.length / 4) * 3)]; - } - - for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) { - b1 = decodingTable[data[i]]; - b2 = decodingTable[data[i + 1]]; - b3 = decodingTable[data[i + 2]]; - b4 = decodingTable[data[i + 3]]; - - bytes[j] = (byte) ((b1 << 2) | (b2 >> 4)); - bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2)); - bytes[j + 2] = (byte) ((b3 << 6) | b4); - } - - if (data[data.length - 2] == PADDING/* '=' */) { - b1 = decodingTable[data[data.length - 4]]; - b2 = decodingTable[data[data.length - 3]]; - - bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4)); - } else if (data[data.length - 1] == PADDING/* '=' */) { - b1 = decodingTable[data[data.length - 4]]; - b2 = decodingTable[data[data.length - 3]]; - b3 = decodingTable[data[data.length - 2]]; - - bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4)); - bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2)); - } else { - b1 = decodingTable[data[data.length - 4]]; - b2 = decodingTable[data[data.length - 3]]; - b3 = decodingTable[data[data.length - 2]]; - b4 = decodingTable[data[data.length - 1]]; - - bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4)); - bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2)); - bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4); - } - } catch (Throwable t) { - bytes = null; - } - return bytes; - } - } - - /** - * 计算BASE64编码的字符串的实际字节数 - * - * @param str - * @return - */ - public static int lengthAsBase64(String str) { - if (str == null | str.length() < 4) { - return 0; - } - - int len; - int length = str.length(); - if (str.charAt(length - 2) == PADDING/* '=' */) { - len = (((length / 4) - 1) * 3) + 1; - } else if (str.charAt(length - 1) == PADDING/* '=' */) { - len = (((length / 4) - 1) * 3) + 2; - } else { - len = ((length / 4) * 3); - } - return len; - } - -// private static byte[] discardNonBase64Bytes(byte[] data) { -// byte[] temp = new byte[data.length]; -// int bytesCopied = 0; -// -// for (int i = 0; i < data.length; i++) { -// if (isValidBase64Byte(data[i])) { -// temp[bytesCopied++] = data[i]; -// } -// } -// -// byte[] newData = new byte[bytesCopied]; -// -// System.arraycopy(temp, 0, newData, 0, bytesCopied); -// -// return newData; -// } -// -// private static String discardNonBase64Chars(String data) { -// StringBuffer sb = new StringBuffer(); -// -// int length = data.length(); -// -// for (int i = 0; i < length; i++) { -// if (isValidBase64Byte((byte) (data.charAt(i)))) { -// sb.append(data.charAt(i)); -// } -// } -// -// return sb.toString(); -// } - -// private static boolean isValidBase64Byte(byte b) { -// if (b == '=') { -// return true; -// } else if ((b < 0) || (b >= 128)) { -// return false; -// } else if (decodingTable[b] == -1) { -// return false; -// } -// -// return true; -// } - - public static void main(String[] args) { - String data = ""; - byte[] bs = data.getBytes(StandardCharsets.UTF_8); - String result = Base64.encode(bs, 0, bs.length); - System.out.println(result); - System.out.println(); - System.out.println(new String(Base64.decode(result))); - } -} diff --git a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Pxx.java b/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Pxx.java deleted file mode 100644 index 114c2289859cd49b2b0362a91988f9223c55cd3b..0000000000000000000000000000000000000000 --- a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/Pxx.java +++ /dev/null @@ -1,212 +0,0 @@ -package com.tongtech.proxy.core.utils; - -/** - * 百分位数监控指标计算类。例如P90表示包含90%的值,即总样本中从少到多计数第90%个样本的值, - * 如需要计算一组样本内P90、P99和P99.9的值,可定义: - * Pxx pxx = new Pxx(9000, 9900, 9990); - * pxx.set(long)依次输入各样本值 - * pxx.get()返回上述样本区间内的P90、P99、P99.9、总样本数、总样本时间共5个长整数的数组,同时并清空累计值。 - */ -public class Pxx { - //private static final ILog logger = LogManager.getLogger(Pxx.class); - private static final Log logger = ProxyConfig.getServerLog(); - - private final static int MAX_PERCENT = 10000; - - private final static long[] DEVISIONS = new long[]{2, 4, 7, 10, 15, 23, 34, 51, 76, 114 - , 171, 256, 384, 577, 865, 1298, 2596, 5192, 10384, 32768 - , 131072, 524288, 2097152}; - - private final static int BLOCKS = DEVISIONS.length; - - private final int[] datas = new int[BLOCKS * 2]; - - private final int[] caculateData = new int[BLOCKS]; - - private final int[] indicators; - - private volatile long totalTimes = 0; - private volatile long totalTime = 0; - - public Pxx() { - // P90 - this(9000); - } - - /** - * for excample: Pxx pxx = new Pxx(9000, 9900, 9990); - * and call pxx.set(duration); many times - * then call pxx.get() will obtain long[]{P90, P99, P99.9} - * - * @param indicators the indicators - */ - public Pxx(int... indicators) { - if (indicators == null || indicators.length == 0) { - throw new IllegalArgumentException("null argument"); - } - - for (int i = 0; i < indicators.length; ++i) { - if (indicators[i] >= MAX_PERCENT) { - throw new IllegalArgumentException("the max value of indicator is " + MAX_PERCENT); - } else if (indicators[i] < (MAX_PERCENT >> 2)) { - throw new IllegalArgumentException("indicator " + indicators[i] + " is too small. the max value of indicator is " - + MAX_PERCENT + ", the indicator must be greater or equal than " + (MAX_PERCENT >> 2)); - } - } - - this.indicators = indicators; - for (int i = 0; i < datas.length; ++i) { - datas[i] = 0; - } - - //logger.debug("New Pxx created for {} indicators", indicators.length); - if (logger.isDebug()) { - logger.debugLog("Pxx::() New Pxx created for " + indicators.length + " indicators"); - } - } - - public void set(long duration) { - int start = 0; - int stop = BLOCKS - 1; - while (start != stop) { - int cur = (start + stop) >> 1; - if (duration >= DEVISIONS[cur]) { - // Here cur must be less than BLOCKS - 1 - start = cur + 1; - } else if (cur > 0 && duration < DEVISIONS[cur - 1]) { - stop = cur - 1; - } else { - start = stop = cur; - } - } - synchronized (datas) { - ++datas[start]; - ++totalTimes; - totalTime += duration; - } - } - - public long[] get() { - long[] ret = new long[indicators.length + 2]; - synchronized (caculateData) { - synchronized (datas) { - System.arraycopy(datas, 0, caculateData, 0, BLOCKS); - System.arraycopy(datas, BLOCKS, datas, 0, BLOCKS); - - ret[ret.length - 2] = totalTimes; - totalTimes = 0; - ret[ret.length - 1] = totalTime; - totalTime = 0; - } - -// if (logger.isDebugEnable()) { -// StringBuilder buf = new StringBuilder("Duration distribution: [ "); -// for (int i = 0; i < BLOCKS; ++i) { -// buf.append(caculateData[i]).append(' '); -// } -// buf.append(']'); -// logger.debug(buf.toString()); -// } - if (logger.isDebug()) { - StringBuilder buf = new StringBuilder(64); - for (int i = 0; i < BLOCKS; ++i) { - buf.append(caculateData[i]).append(' '); - } -// buf.append(']'); - logger.debugLog("Pxx::get() Duration distribution: [ {}]",buf); - } - - long total = ret[ret.length - 2]; - for (int i = 0; i < indicators.length; ++i) { - if (total > 0) { - long abnormal = (MAX_PERCENT - indicators[i]) * total / MAX_PERCENT; - ret[i] = getPxx(Math.max(1, abnormal)); - } else { - ret[i] = 0; - } - } - } - return ret; - } - - private long getPxx(long slown) { - int i = caculateData.length - 1; - int slows = 0; - for (; i >= 0; --i) { - if (slows + caculateData[i] >= slown) { - break; - } - slows += caculateData[i]; - } - - if (i < 0) { - return 0; - } else { - long min = 0; - if (i > 0) { - min = DEVISIONS[i - 1]; - } - long max = DEVISIONS[DEVISIONS.length - 1]; - if (i < DEVISIONS.length - 1) { - max = DEVISIONS[i]; - } - long left = caculateData[i] - (slown - slows); - return (max - min) * left / caculateData[i] + min; - } - } - - public static void main(String[] args) { - Pxx pxx = new Pxx(5000, 9000, 9900, 9990); - pxx.set(0); - pxx.set(10); - pxx.set(38); - pxx.set(27); - pxx.set(83); - pxx.set(42); - pxx.set(31); - pxx.set(51); - pxx.set(47); - pxx.set(71); - pxx.set(129000000l); - pxx.set(82); - pxx.set(286); - pxx.set(21); - pxx.set(6); - pxx.set(15); - pxx.set(25); - pxx.set(34); - pxx.set(62); - pxx.set(34); - pxx.set(75); - pxx.set(598); - pxx.set(19); - pxx.set(24); - pxx.set(35); - pxx.set(48); - pxx.set(68); - pxx.set(93); - pxx.set(152); - pxx.set(29); - pxx.set(67); - pxx.set(63); - pxx.set(70); - - StringBuilder buf = new StringBuilder(); - buf.append("[ "); - for (long l : pxx.get()) { - buf.append(l).append(' '); - } - buf.append(']'); - System.out.println(buf); - - buf.setLength(0); - buf.append("[ "); - for (int i = 0; i < pxx.caculateData.length; ++i) { - long min = i > 0 ? DEVISIONS[i - 1] : 0; - long l = pxx.caculateData[i]; - buf.append(l).append('(').append(min).append('-').append(DEVISIONS[i] - 1).append(") "); - } - buf.append(']'); - System.out.println(buf); - } -} diff --git a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/StableVector.java b/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/StableVector.java deleted file mode 100644 index 0be66609e15c6418b21c9cc71d03a8ecb7e5d445..0000000000000000000000000000000000000000 --- a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/StableVector.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.tongtech.proxy.core.utils; - - -public class StableVector { - - private final int Datas[]; - - private final int Capacity; - - private int Size = 0; - - public StableVector(int capacity) { - Datas = new int[capacity]; - Capacity = capacity; - } - - public synchronized void add(int i) throws TooManyRowsException { - if (Size < Capacity) { - Datas[Size++] = i; - } else { - throw new TooManyRowsException("data is too large"); - } - } - - public synchronized int get(int p) throws IllegalArgumentException { - if (p >= 0 && p < Size) { - return Datas[p]; - } else { - throw new IllegalArgumentException("input position is error"); - } - } - - public synchronized void clear() { - Size = 0; - } - - public synchronized int size() { - return Size; - } - - public int capacity() { - return Datas.length; - } -} diff --git a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/TooManyRowsException.java b/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/TooManyRowsException.java deleted file mode 100644 index cb82f90efefb7ed391dcf788ce6590971470ae3d..0000000000000000000000000000000000000000 --- a/rds-proxy/proxy/src/main/java/com/tongtech/proxy/core/utils/TooManyRowsException.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.tongtech.proxy.core.utils; - -public class TooManyRowsException extends IllegalArgumentException { - - /** - * - */ - private static final long serialVersionUID = -2999165613121463589L; - - public TooManyRowsException(String msg){ - super(msg); - } -}