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

Avoid memory leak when configuring all job count metrics with false #645

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,17 @@
!PrometheusConfiguration.get().isCountSuccessfulBuilds() &&
!PrometheusConfiguration.get().isCountUnstableBuilds();

BuildCompletionListener listener = BuildCompletionListener.getInstance();

if (ignoreBuildMetrics) {
listener.unregister();

Check warning on line 94 in src/main/java/org/jenkinsci/plugins/prometheus/JobCollector.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 94 is not covered by tests
return samples;
}

// Below metrics use labelNameArray which might include the optional labels
// of "parameters" or "status"
summary = factory.createRunCollector(CollectorType.BUILD_DURATION_SUMMARY, labelNameArray, null);
BuildCompletionListener listener = BuildCompletionListener.getInstance();


// Counter manager acts as a DB to retrieve any counters that are already in memory instead of reinitializing
// them with each iteration of collect.
var manager = CounterManager.getManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ protected BuildCompletionListener(){
lock = new ReentrantLock();
}

@Override
public void unregister() {
super.unregister();
try {
lock.lock();
this.runStack.clear();
} finally {
lock.unlock();
}
}

/*
* Extension tells Jenkins to register this class as a RunListener and to use
* this method in order to retrieve an instance of the class. It is a singleton,
Expand Down Expand Up @@ -111,4 +122,8 @@ public void close() {
}
};
}

List<Run<?, ?>> getRunStack() {
return runStack;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jenkinsci.plugins.prometheus.collectors.builds;

import hudson.model.Run;
import hudson.model.TaskListener;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

class BuildCompletionListenerTest {

@Test
@Issue("#643")
void unregisterClearsRunStack() {
Run<?,?> mock = mock(Run.class);
TaskListener taskListener = mock(TaskListener.class);

BuildCompletionListener sut = BuildCompletionListener.getInstance();

sut.onCompleted(mock, taskListener);

assertEquals(1, sut.getRunStack().size());

sut.unregister();

assertEquals(0, sut.getRunStack().size(), "Unregister should clear the list. Otherwise a memory leak can occur.");

}
}
Loading