Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep track of HTTPServer instances #40

Merged
merged 3 commits into from
Aug 21, 2024
Merged

Conversation

mimaison
Copy link
Contributor

An attempt at addressing #34 by keeping track of all HTTPServer instances started so they can be properly closed whenever possible. You can have multiple HTTPServer instances when several Kafka components are started in the same JVM, for example in Kafka Connect or Kafka Streams.

If multiple components attempt to create servers on the same listener, the same server will be reused. When all using components are shutdown, the server can also be closed.

@@ -57,7 +56,7 @@ public void configure(Map<String, ?> map) {
config = new PrometheusMetricsReporterConfig(map, registry);
collector = new KafkaMetricsCollector();
// Add JVM metrics
JvmMetrics.builder().register(registry);
JvmMetrics.builder().register();
Copy link
Contributor Author

@mimaison mimaison Aug 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method without an argument is idempotent and registers the JVM metrics onto the default metrics registry, which is what we use at runtime. This allow multiple components to run this code without causing exceptions as the metrics are only added once: http://prometheus.github.io/client_java/api/io/prometheus/metrics/instrumentation/jvm/JvmMetrics.Builder.html#register()

It would be nice to pass a registry and still have the idempotent behavior if it's the default registry. I may open a PR on https://github.com/prometheus/client_java to do that. In the meantime, we have to use to method without arguments.

@mimaison mimaison added this to the 0.1.0 milestone Aug 20, 2024
Copy link
Member

@scholzj scholzj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits. LGTM otherwise.

import java.util.concurrent.atomic.AtomicInteger;

/**
* Static class to keep track of all the HTTP servers started by all the Kafka components in a JVM.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is static on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, "static" is not the right term. I meant it as a utility. But I guess we can just say "Class to keep track ..."

* @return A ServerCounter instance
* @throws IOException if the HTTP server does not exist and cannot be started
*/
public synchronized static ServerCounter get(Listener listener, PrometheusRegistry registry) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be named something like getOrCreate if it is not a simple getter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I've renamed it to getOrCreate()

Signed-off-by: Mickael Maison <[email protected]>
Copy link
Member

@scholzj scholzj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

LOG.info("HTTP server already started");
return Optional.empty();
HttpServers.ServerCounter server = HttpServers.getOrCreate(listener, registry);
LOG.info("HTTP server started on listener http://{}:{}", listener.host, server.port());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log line is misleading as the server isn't necessarily being started here.

.port(listener.port)
.registry(registry)
.buildAndStart();
this.listener = listener;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a log statement for starting the server.

private synchronized boolean close() {
int remaining = count.decrementAndGet();
if (remaining == 0) {
server.close();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and log on shutdown

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I checked that the port is returned (and valid) after close() is called.

* @return A ServerCounter instance
* @throws IOException if the HTTP server does not exist and cannot be started
*/
public synchronized static ServerCounter getOrCreate(Listener listener, PrometheusRegistry registry) throws IOException {
Copy link

@k-wall k-wall Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API is a bit weak. It has you believe that you could attach different registries, but it actually ignore it.
I see that both Yammer and Kafka paths pass use the same registry (the default one) so given our use of the class, there's no issue.

I don't really have an alternative suggestion here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the registry used to be hard coded to the default registry as it's what all components have to use. But because there isn't a mechanism to clear a registry, we allowed passing it in for tests.

Signed-off-by: Mickael Maison <[email protected]>
Copy link

@k-wall k-wall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mimaison mimaison merged commit dab0fda into strimzi:main Aug 21, 2024
7 checks passed
@mimaison mimaison deleted the issue-34 branch August 21, 2024 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants