fury icon indicating copy to clipboard operation
fury copied to clipboard

[Java] Observability for ThreadPoolFory

Open theigl opened this issue 3 months ago • 10 comments

Feature Request

It would be great if it was possible to monitor ThreadPoolFory in some way.

Our production profiler shows that more Fory instances are created over time than I would expect, but we currently have no way to see what is going on. At the very least, ThreadPoolFory should have debug logs when new instances are created and for which ClassLoader. Ideally, we would have additional statistics for the pool: the current size, the peak size etc.

Is your feature request related to a problem? Please describe

No response

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

The only log output currently generated that can detect a new Fory instance being created is this quite unhelpful one:

org.apache.fory.Fory : Created new fory org.apache.fory.Fory@50e383c

This can definitely be improved by implementing a meaningful toString implementation that contains the name of the instance if set, potentially the class-loader, etc.

theigl avatar Aug 28 '25 09:08 theigl

Sounds great, would you like to create a pr?

chaokunyang avatar Aug 28 '25 10:08 chaokunyang

I don't have time to work on full observability at the moment, but I might be able to create a PR to improve the current log output.

theigl avatar Aug 28 '25 10:08 theigl

is this issue to someone? if not could you please assign it to me! My background - I am currently a working professional and i have the knowledge of the techstack used and would like to contribute...

JaskaranSingh-01 avatar Sep 08 '25 18:09 JaskaranSingh-01

I'm not planning on working on this at the moment. What I'm currently doing is reflecting into ThreadPoolFory and creating the following metrics:

final ForyPooledObjectFactory objectFactory = (ForyPooledObjectFactory) ReflectionUtil.readField(fory, "foryPooledObjectFactory");
Metrics.gauge("fory.cache.numIdle", Tags.of("name", name), objectFactory, value -> {
    final ClassLoaderForyPooled c = value.getPooledCache();
    return ((BlockingQueue<?>) ReflectionUtil.readField(c, "idleCacheQueue")).size();
});
Metrics.gauge("fory.cache.numActive", Tags.of("name", name), objectFactory, value -> {
    final ClassLoaderForyPooled c = value.getPooledCache();
    return ((AtomicInteger) ReflectionUtil.readField(c, "activeCacheNumber")).get();
});
Metrics.gauge("fory.cache.numAll", Tags.of("name", name), objectFactory, value -> {
    final ClassLoaderForyPooled c = value.getPooledCache();
    return ((Map<?, ?>) ReflectionUtil.readField(c, "allFory")).size();
});

Since numAll is always the same as numActive it should be enough to expose metrics for numIdle and numActive. Ideally, ThreadPoolFory would expose a method like getPoolStats() that gives the user access to these metrics.

@chaokunyang What do you think?

theigl avatar Sep 09 '25 08:09 theigl

Yes, expose a getPoolStats API would be great. @JaskaranSingh-01 Would you like to work on this?

chaokunyang avatar Sep 09 '25 17:09 chaokunyang

yes, that would be great!! how do i get started?

JaskaranSingh-01 avatar Sep 12 '25 05:09 JaskaranSingh-01

yes, that would be great!! how do i get started?

@JaskaranSingh-01 You can follow https://github.com/apache/fory/blob/main/CONTRIBUTING.md and submit a PR

chaokunyang avatar Sep 12 '25 08:09 chaokunyang

Hi @chaokunyang , I’d like to work on this issue. Could you please assign it to me? Thank you!

souravbhunia07 avatar Oct 11 '25 15:10 souravbhunia07

Hi @chaokunyang , I’d like to work on this issue. Could you please assign it to me? Thank you!

Great! Assigned to you

chaokunyang avatar Oct 11 '25 15:10 chaokunyang

Thanks a lot for assigning this to me! I’ll get started and keep you updated on my progress.

souravbhunia07 avatar Oct 11 '25 15:10 souravbhunia07