-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove license state listeners on closables (#36308)
We have a few places where we register license state listeners on transient components (i.e., resources that can be open and closed during the lifecycle of the server). In one case (the opt-out query cache) we were never removing the registered listener, effectively a terrible memory leak. In another case, we were not un-registered the listener that we registered, since we were not referencing the same instance of Runnable. This commit does two things: - introduces a marker interface LicenseStateListener so that it is easier to identify these listeners in the codebase and avoid classes that need to register a license state listener from having to implement Runnable which carries a different semantic meaning than we want here - fixes the two places where we are currently leaking license state listeners
- Loading branch information
1 parent
fe265ae
commit 159a78a
Showing
6 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseStateListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.license; | ||
|
||
import org.elasticsearch.Version; | ||
|
||
/** | ||
* Marker interface for callbacks that are invoked when the license state changes. | ||
*/ | ||
@FunctionalInterface | ||
public interface LicenseStateListener { | ||
|
||
/** | ||
* Callback when the license state changes. See {@link XPackLicenseState#update(License.OperationMode, boolean, Version)}. | ||
*/ | ||
void licenseStateChanged(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...g/src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.monitoring.exporter.local; | ||
|
||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.xpack.monitoring.cleaner.CleanerService; | ||
import org.elasticsearch.xpack.monitoring.exporter.Exporter; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
|
||
public class LocalExporterTests extends ESTestCase { | ||
|
||
public void testLocalExporterRemovesListenersOnClose() throws Exception { | ||
final ClusterService clusterService = mock(ClusterService.class); | ||
final XPackLicenseState licenseState = mock(XPackLicenseState.class); | ||
final Exporter.Config config = new Exporter.Config("name", "type", Settings.EMPTY, clusterService, licenseState); | ||
final CleanerService cleanerService = mock(CleanerService.class); | ||
final LocalExporter exporter = new LocalExporter(config, mock(Client.class), cleanerService); | ||
verify(clusterService).addListener(exporter); | ||
verify(cleanerService).add(exporter); | ||
verify(licenseState).addListener(exporter); | ||
exporter.close(); | ||
verify(clusterService).removeListener(exporter); | ||
verify(cleanerService).remove(exporter); | ||
verify(licenseState).removeListener(exporter); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters