Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
youfanx committed Aug 6, 2024
1 parent 1c2e497 commit 6cc68dd
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 56 deletions.
33 changes: 14 additions & 19 deletions rxlib/src/main/java/org/rx/core/Reflects.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ public Class<?> getClassTrace(int depth) {
}

//region class
// public static Class<?> getCallerClass() {
// try {
// Class<?> type = Class.forName("sun.reflect.Reflection");
// return invokeMethod(type, "getCallerClass");
// } catch (ClassNotFoundException e) {
// return CLASS_TRACER.getClassTrace(0);
// }
// }

// public static StackTraceElement[] getCallerStack() {
// //Throwable.class.getDeclaredMethod("getStackTraceElement", int.class) & Reflection.getCallerClass(2 + depth) java 11 not exist
// return new Throwable().getStackTrace();
// }

public static String getStackTrace(Thread t) {
StringBuilder buf = new StringBuilder();
for (StackTraceElement traceElement : t.getStackTrace()) {
Expand All @@ -118,25 +132,6 @@ public static String getStackTrace(Thread t) {
return buf.toString();
}

public static Linq<StackTraceElement> stackTrace(int takeCount) {
return Linq.from(new Throwable().getStackTrace()).skip(2).take(takeCount);
}

public static Class<?> getCallerClass() {
try {
Class<?> type = Class.forName("sun.reflect.Reflection");
return invokeMethod(type, "getCallerClass");
} catch (ClassNotFoundException e) {
// throw new RuntimeException(e);
return stackClass(0);
}
}

public static Class<?> stackClass(int depth) {
//Throwable.class.getDeclaredMethod("getStackTraceElement", int.class) & Reflection.getCallerClass(2 + depth) java 11 not exist
return SecurityManagerEx.INSTANCE.getClassTrace()[2 + depth];
}

public static InputStream getResource(String namePattern) {
InputStream in = getClassLoader().getResourceAsStream(namePattern);
if (in != null) {
Expand Down
3 changes: 3 additions & 0 deletions rxlib/src/main/java/org/rx/core/RxConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public interface ConfigNames {
String THREAD_POOL_HIGH_CPU_WATER_MARK = "app.threadPool.highCpuWaterMark";
String THREAD_POOL_REPLICAS = "app.threadPool.replicas";
String THREAD_POOL_TRACE_NAME = "app.threadPool.traceName";
String THREAD_POOL_SLOW_METHOD_SAMPLING_PERCENT = "app.threadPool.slowMethodSamplingPercent";
String THREAD_POOL_MAX_TRACE_DEPTH = "app.threadPool.maxTraceDepth";
String THREAD_POOL_SAMPLING_PERIOD = "app.threadPool.samplingPeriod";
String THREAD_POOL_SAMPLING_TIMES = "app.threadPool.samplingTimes";
Expand Down Expand Up @@ -109,6 +110,7 @@ public static class ThreadPoolConfig {
int replicas;
String traceName;
int maxTraceDepth;
int slowMethodSamplingPercent;

int cpuLoadWarningThreshold;
long samplingPeriod;
Expand Down Expand Up @@ -275,6 +277,7 @@ public void refreshFromSystemProperty() {
threadPool.replicas = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_REPLICAS, threadPool.replicas);
threadPool.traceName = SystemPropertyUtil.get(ConfigNames.THREAD_POOL_TRACE_NAME);
threadPool.maxTraceDepth = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_MAX_TRACE_DEPTH, threadPool.maxTraceDepth);
threadPool.slowMethodSamplingPercent = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_SLOW_METHOD_SAMPLING_PERCENT, threadPool.slowMethodSamplingPercent);
threadPool.cpuLoadWarningThreshold = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_CPU_LOAD_WARNING, threadPool.cpuLoadWarningThreshold);
threadPool.samplingPeriod = SystemPropertyUtil.getLong(ConfigNames.THREAD_POOL_SAMPLING_PERIOD, threadPool.samplingPeriod);
threadPool.samplingTimes = SystemPropertyUtil.getInt(ConfigNames.THREAD_POOL_SAMPLING_TIMES, threadPool.samplingTimes);
Expand Down
4 changes: 2 additions & 2 deletions rxlib/src/main/java/org/rx/core/Sys.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ public static <T> T deepClone(T obj) {

public static String fastCacheKey(String region, Object... args) {
if (region == null) {
region = Reflects.stackClass(1).getSimpleName();
region = Reflects.CLASS_TRACER.getClassTrace(1).getSimpleName();
}
if (!Arrays.isEmpty(args)) {
region += java.util.Arrays.hashCode(args);
Expand All @@ -527,7 +527,7 @@ public static String fastCacheKey(String region, Object... args) {

public static String cacheKey(String region, Object... args) {
if (region == null) {
region = Reflects.stackClass(1).getSimpleName();
region = Reflects.CLASS_TRACER.getClassTrace(1).getSimpleName();
}

StringBuilder buf = new StringBuilder();
Expand Down
2 changes: 1 addition & 1 deletion rxlib/src/main/java/org/rx/core/Tasks.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static <T> T awaitQuietly(Future<T> future, long millis) {
return future.get(millis, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
//catch +1 ?
TraceHandler.INSTANCE.log("awaitNow {} timeout", Reflects.stackClass(2).getName());
TraceHandler.INSTANCE.log("awaitNow {} timeout", Reflects.CLASS_TRACER.getClassTrace(2).getName());
} catch (Exception e) {
TraceHandler.INSTANCE.log(e);
}
Expand Down
17 changes: 11 additions & 6 deletions rxlib/src/main/java/org/rx/core/ThreadPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.concurrent.FastThreadLocalThread;
import io.netty.util.internal.InternalThreadLocalMap;
import io.netty.util.internal.ThreadLocalRandom;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -167,7 +168,7 @@ static <T> Task<T> as(Object fn) {
final Object id;
final InternalThreadLocalMap parent;
final String traceId;
final Class<?>[] caller;
final StackTraceElement[] stackTrace;

private Task(Func<T> fn, FlagsEnum<RunFlag> flags, Object id) {
if (flags == null) {
Expand All @@ -177,11 +178,10 @@ private Task(Func<T> fn, FlagsEnum<RunFlag> flags, Object id) {
if (conf.threadPool.traceName != null) {
flags.add(RunFlag.THREAD_TRACE);
}
if (conf.trace.slowMethodElapsedMicros > 0) {
//Reflects.getStackTrace(t)
caller = Reflects.CLASS_TRACER.getClassTrace();
if (conf.trace.slowMethodElapsedMicros > 0 && ThreadLocalRandom.current().nextInt(0, 100) < conf.threadPool.slowMethodSamplingPercent) {
stackTrace = new Throwable().getStackTrace();
} else {
caller = null;
stackTrace = null;
}

this.fn = fn;
Expand All @@ -205,7 +205,12 @@ public T call() {
throw e;
} finally {
Thread t = Thread.currentThread();
TraceHandler.INSTANCE.saveMethodTrace(t, caller != null ? caller[0] : ThreadPool.class, fn.getClass().getSimpleName() + Linq.from(caller).select(Class::getName).toJoinString(Constants.STACK_TRACE_FLAG), id == null ? null : new Object[]{id},
TraceHandler.INSTANCE.saveMethodTrace(t,
fn.getClass().getSimpleName(),
stackTrace != null
? "[" + Linq.from(stackTrace).select(StackTraceElement::toString).toJoinString(Constants.STACK_TRACE_FLAG) + "]"
: "Unknown",
id == null ? null : new Object[]{id},
r, ex, System.nanoTime() - s);
}
return r;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected ApplicationException(@NonNull Object errorCode, Object[] codeValues, T
}
this.codeValues = codeValues;
if (errorCode instanceof CharSequence) {
stacks = Reflects.stackTrace(8);
stacks = Linq.from(new Throwable().getStackTrace()).skip(1).take(8);
} else {
stacks = null;
}
Expand Down
49 changes: 23 additions & 26 deletions rxlib/src/main/java/org/rx/exception/TraceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.rx.bean.DateTime;
import org.rx.bean.ProceedEventArgs;
import org.rx.codec.CodecUtil;
import org.rx.core.Arrays;
import org.rx.core.StringBuilder;
import org.rx.core.*;
import org.rx.io.EntityDatabase;
Expand All @@ -20,10 +21,7 @@

import java.io.Serializable;
import java.sql.Time;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ScheduledFuture;

Expand All @@ -43,7 +41,7 @@ public static class ExceptionEntity implements Serializable {
@DbColumn(primaryKey = true)
long id;
ExceptionLevel level;
Queue<String> messages;
Queue<Map<String, Object>> messages;
String stackTrace;
int occurCount;

Expand Down Expand Up @@ -187,19 +185,21 @@ public void uncaughtException(Thread t, Throwable e) {
}

public void saveExceptionTrace(Thread t, String msg, Throwable e) {
queue.offer(new Object[]{t.getName(), msg, e});
queue.offer(new Object[]{t.getName(), DateTime.now(), msg, e});
}

void innerSave(String thread, String msg, Throwable e) {
void innerSave(String thread, DateTime now, String msg, Throwable e) {
RxConfig.TraceConfig conf = RxConfig.INSTANCE.getTrace();
String stackTrace = ExceptionUtils.getStackTrace(e);
String eMsg = Strings.EMPTY;
int eMsgFlag = stackTrace.indexOf(Constants.STACK_TRACE_FLAG);
String eMsg = stackTrace.substring(0, eMsgFlag);
stackTrace = stackTrace.substring(eMsgFlag + 2);
msg = msg == null ? eMsg : String.format("%s\n%s", eMsg, msg);
if (eMsgFlag != -1) {
eMsgFlag += 2;
eMsg = stackTrace.substring(0, eMsgFlag);
stackTrace = stackTrace.substring(eMsgFlag);
}

long pk = CodecUtil.hash64(stackTrace);
// Tasks.nextPool().runSerial(() -> {
EntityDatabase db = EntityDatabase.DEFAULT;
db.begin();
try {
Expand All @@ -215,29 +215,26 @@ void innerSave(String thread, String msg, Throwable e) {
entity.setMessages(new ConcurrentLinkedQueue<>());
entity.setStackTrace(stackTrace);
}
Queue<String> queue = entity.getMessages();
Queue<Map<String, Object>> queue = entity.getMessages();
if (queue.size() > conf.getErrorMessageSize()) {
queue.poll();
}
Map<String, Object> call = new HashMap<>(2);
StringBuilder b = new StringBuilder();
b.appendMessageFormat("{}\t{}", DateTime.now().toDateTimeString(), msg);
Map<String, String> ctxMap = Sys.getMDCCtxMap();
if (!ctxMap.isEmpty()) {
b.appendMessageFormat("\nMDC:\t{}", ctxMap);
}
queue.offer(b.toString());
b.appendMessageFormat("{}\t{}{}", now.toDateTimeString(), eMsg, msg);
call.put("message", b.toString());
call.put("MDC", Sys.getMDCCtxMap());
queue.offer(call);
entity.occurCount++;
entity.setAppName(RxConfig.INSTANCE.getId());
entity.setThreadName(thread);
entity.setModifyTime(DateTime.now());
entity.setModifyTime(now);
db.save(entity, doInsert);
db.commit();
} catch (Throwable ex) {
log.error("dbTrace", ex);
db.rollback();
}
// return null;
// }, pk);
}

public List<ExceptionEntity> queryExceptionTraces(Date startTime, Date endTime, ExceptionLevel level, String keyword,
Expand Down Expand Up @@ -271,20 +268,20 @@ public List<ExceptionEntity> queryExceptionTraces(Date startTime, Date endTime,
return db.findBy(q);
}

public void saveMethodTrace(Thread t, Class<?> declaringType, String methodName, Object[] parameters,
public void saveMethodTrace(Thread t, String declaringTypeName, String methodName, Object[] parameters,
Object returnValue, Throwable e, long elapsedMicros) {
queue.offer(new Object[]{t.getName(), declaringType, methodName, parameters, returnValue, e, elapsedMicros});
queue.offer(new Object[]{t.getName(), DateTime.now(), declaringTypeName, methodName, parameters, returnValue, e, elapsedMicros});
}

void innerSave(String thread, Class<?> declaringType, String methodName, Object[] parameters,
void innerSave(String thread, DateTime now, String declaringTypeName, String methodName, Object[] parameters,
Object returnValue, Throwable error, long elapsedNanos) {
RxConfig.TraceConfig conf = RxConfig.INSTANCE.getTrace();
long elapsedMicros;
if ((elapsedMicros = elapsedNanos / 1000L) < conf.getSlowMethodElapsedMicros()) {
return;
}

String fullName = String.format("%s.%s(%s)", declaringType.getName(), methodName, parameters == null ? 0 : parameters.length);
String fullName = String.format("%s.%s(%s)", declaringTypeName, methodName, parameters == null ? 0 : parameters.length);
long pk = CodecUtil.hash64(fullName);
// Tasks.nextPool().runSerial(() -> {
EntityDatabase db = EntityDatabase.DEFAULT;
Expand All @@ -310,7 +307,7 @@ void innerSave(String thread, Class<?> declaringType, String methodName, Object[
entity.occurCount++;
entity.setAppName(RxConfig.INSTANCE.getId());
entity.setThreadName(thread);
entity.setModifyTime(DateTime.now());
entity.setModifyTime(now);
db.save(entity, doInsert);
db.commit();
} catch (Throwable e) {
Expand Down
2 changes: 1 addition & 1 deletion rxlib/src/main/java/org/rx/spring/BaseInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
throw pe.getError();
}
} finally {
TraceHandler.INSTANCE.saveMethodTrace(Thread.currentThread(), pe.getDeclaringType(), signature.getName(), pe.getParameters(), pe.getReturnValue(), pe.getError(), pe.getElapsedNanos());
TraceHandler.INSTANCE.saveMethodTrace(Thread.currentThread(), pe.getDeclaringType().getName(), signature.getName(), pe.getParameters(), pe.getReturnValue(), pe.getError(), pe.getElapsedNanos());
onLog(signature, pe, paramSnapshot);
raiseEvent(onProceed, pe);
}
Expand Down
1 change: 1 addition & 0 deletions rxlib/src/main/resources/rx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ app:
highCpuWaterMark: 70
replicas: 2
maxTraceDepth: 5
slowMethodSamplingPercent: 1
cpuLoadWarningThreshold: 80
samplingPeriod: 3000
samplingTimes: 2
Expand Down

0 comments on commit 6cc68dd

Please sign in to comment.