zookeeper icon indicating copy to clipboard operation
zookeeper copied to clipboard

ZOOKEEPER-4910: fix error when no dep dropwizard java.lang.NoClassDefFoundError: com/…

Open songxiaosheng opened this issue 9 months ago • 2 comments

fix follow error

Exception in thread "ZooKeeper Server Starter" java.lang.NoClassDefFoundError: com/codahale/metrics/Reservoir
	at org.apache.zookeeper.metrics.impl.DefaultMetricsProvider$DefaultMetricsContext.lambda$getSummary$2(DefaultMetricsProvider.java:126)
	at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1713)
	at org.apache.zookeeper.metrics.impl.DefaultMetricsProvider$DefaultMetricsContext.getSummary(DefaultMetricsProvider.java:122)
	at org.apache.zookeeper.server.ServerMetrics.<init>(ServerMetrics.java:74)
	at org.apache.zookeeper.server.ServerMetrics.<clinit>(ServerMetrics.java:44)
	at org.apache.zookeeper.server.ZooKeeperServerMain.runFromConfig(ZooKeeperServerMain.java:133)
	at org.apache.dubbo.springboot.demo.provider.EmbeddedZooKeeper$ServerRunnable.run(EmbeddedZooKeeper.java:249)
	at java.base/java.lang.Thread.run(Thread.java:1575)
Caused by: java.lang.ClassNotFoundException: com.codahale.metrics.Reservoir
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:528)
	... 8 

songxiaosheng avatar Mar 24 '25 14:03 songxiaosheng

@tisonkun PATL

songxiaosheng avatar Mar 24 '25 14:03 songxiaosheng

Hello @songxiaosheng . Thank you for the patch, but I'm not sure we'll be able to commit this.

In #925 , we intentionally slimmed down pom.xml so that clients do not import dependencies that are only needed on the server side. Part of this included marking io.dropwizard.metrics:metrics-core as provided. This is only used on the ZooKeeper server side. If we commit this change, it's possible that actual failures in metrics initialization could leave a server running in production but not producing metrics.

From the sample stack trace, it looks like this is a use case for running ZooKeeper "embedded" as part of a demo application. In Apache Curator's tests, they handle this by explicitly adding their own dependency on io.dropwizard.metrics:metrics-core. Perhaps your demo application could do the same thing.

https://github.com/apache/curator/blob/apache-curator-5.8.0/curator-test/pom.xml#L45-L48

On a side note, you might also be interested in ZooKeeperServerEmbedded.

thanks, i found it in ZooKeeper "embedded"

songxiaosheng avatar Mar 25 '25 04:03 songxiaosheng

@cnauroth I am actually running into the same issue and would probably propose a different fix. The issue I see is that there are references to the dropwizard metrics classes used even when the following is configured for the embedded ZooKeeper server:

zookeeperConfig.put("metricsProvider.className", NullMetricsProvider.class.getName());

The reason seems to be that a static field intended for tests forces an instance of the DefaultMetricsProvider to be created whether an embedder wants it or not, here: https://github.com/apache/zookeeper/blame/6c5f788ee3e58e09d369339a8128bc07cbbc6d03/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerMetrics.java#L46

shartte avatar Jul 15 '25 13:07 shartte