Skip to content

Commit 8fb68bd

Browse files
committed
Remove AtomicBoolean and update customMetadataChanged
1 parent f3dc2f8 commit 8fb68bd

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

modules/ingest-geoip/src/main/java/org/elasticsearch/ingest/geoip/GeoIpDownloaderTaskExecutor.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import java.util.Objects;
5454
import java.util.Set;
5555
import java.util.concurrent.ConcurrentHashMap;
56-
import java.util.concurrent.atomic.AtomicBoolean;
5756

5857
import static org.elasticsearch.ingest.geoip.GeoIpDownloader.DATABASES_INDEX;
5958
import static org.elasticsearch.ingest.geoip.GeoIpDownloader.GEOIP_DOWNLOADER;
@@ -105,7 +104,7 @@ public final class GeoIpDownloaderTaskExecutor extends PersistentTasksExecutor<G
105104
private volatile boolean eagerDownload;
106105

107106
private final ConcurrentHashMap<ProjectId, Boolean> atLeastOneGeoipProcessorByProject = new ConcurrentHashMap<>();
108-
private final ConcurrentHashMap<ProjectId, AtomicBoolean> taskIsBootstrappedByProject = new ConcurrentHashMap<>();
107+
private final ConcurrentHashMap<ProjectId, Boolean> taskIsBootstrappedByProject = new ConcurrentHashMap<>();
109108
private final ConcurrentHashMap<ProjectId, GeoIpDownloader> tasks = new ConcurrentHashMap<>();
110109
private final ProjectResolver projectResolver;
111110

@@ -239,22 +238,23 @@ public void clusterChanged(ClusterChangedEvent event) {
239238

240239
projectResolver.executeOnProject(projectId, () -> {
241240
// bootstrap task once iff it is not already bootstrapped
242-
AtomicBoolean taskIsBootstrapped = taskIsBootstrappedByProject.computeIfAbsent(projectId, k -> new AtomicBoolean(false));
243-
if (taskIsBootstrapped.getAndSet(true) == false) {
241+
boolean taskIsBootstrapped = taskIsBootstrappedByProject.computeIfAbsent(projectId, k -> false);
242+
if (taskIsBootstrapped != true) {
243+
taskIsBootstrappedByProject.put(projectId, true);
244244
this.taskIsBootstrappedByProject.computeIfAbsent(
245245
projectId,
246-
k -> new AtomicBoolean(hasAtLeastOneGeoipProcessor(projectMetadata))
246+
k -> hasAtLeastOneGeoipProcessor(projectMetadata)
247247
);
248248
if (ENABLED_SETTING.get(event.state().getMetadata().settings(), settings)) {
249249
logger.debug("Bootstrapping geoip downloader task for project [{}]", projectId);
250-
startTask(() -> taskIsBootstrapped.set(false));
250+
startTask(() -> taskIsBootstrappedByProject.put(projectId, false));
251251
} else {
252252
logger.debug("Stopping geoip downloader task for project [{}]", projectId);
253-
stopTask(() -> taskIsBootstrapped.set(false));
253+
stopTask(() -> taskIsBootstrappedByProject.put(projectId, false));
254254
}
255255
}
256256

257-
boolean hasIngestPipelineChanges = event.changedCustomProjectMetadataSet(projectId).contains(IngestMetadata.TYPE);
257+
boolean hasIngestPipelineChanges = event.customMetadataChanged(projectId, IngestMetadata.TYPE);
258258
boolean hasIndicesChanges = false;
259259
boolean projectExisted = event.previousState().metadata().hasProject(projectId);
260260
if (projectExisted) {

server/src/main/java/org/elasticsearch/cluster/ClusterChangedEvent.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,22 @@ public Set<String> changedCustomProjectMetadataSet() {
164164
}
165165

166166
/**
167-
* Returns a set of custom meta data types when any custom metadata for the project metadata has changed
168-
* between the previous cluster state and the new cluster state. custom metadata types are
169-
* returned iff they have been added, updated or removed between the previous and the current state
167+
* Checks whether custom metadata type for a project has changed between the previous cluster state
168+
* and the new cluster state. Custom metadata types are considered changed iff they have been added,
169+
* updated or removed between the previous and the current state
170170
*/
171-
public Set<String> changedCustomProjectMetadataSet(ProjectId projectId) {
172-
Set<String> result = new HashSet<>();
171+
public boolean customMetadataChanged(ProjectId projectId, String customMetadataType) {
172+
Set<String> changed = new HashSet<>();
173173
ProjectMetadata project = state.metadata().projects().get(projectId);
174174
ProjectMetadata previousProject = previousState.metadata().projects().get(projectId);
175175
if (previousProject != null && project != null) {
176-
result.addAll(changedCustoms(project.customs(), previousProject.customs()));
176+
changed.addAll(changedCustoms(project.customs(), previousProject.customs()));
177177
} else if (previousProject != null) {
178-
result.addAll(previousProject.customs().keySet());
178+
changed.addAll(previousProject.customs().keySet());
179179
} else if (project != null) {
180-
result.addAll(project.customs().keySet());
180+
changed.addAll(project.customs().keySet());
181181
}
182-
return result;
182+
return changed.contains(customMetadataType);
183183
}
184184

185185
private <C extends Metadata.MetadataCustom<C>> Set<String> changedCustoms(

server/src/test/java/org/elasticsearch/cluster/ClusterChangedEventTests.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,9 @@ public void testChangedCustomProjectMetadataSet() {
502502
ClusterState newState = ClusterState.builder(originalState).build();
503503
ClusterChangedEvent event = new ClusterChangedEvent("_na_", newState, originalState);
504504
// existing project
505-
assertTrue(event.changedCustomProjectMetadataSet(project1.id()).isEmpty());
505+
assertFalse(event.customMetadataChanged(project1.id(), custom1.getWriteableName()));
506506
// non-existing project
507-
assertTrue(event.changedCustomProjectMetadataSet(project2.id()).isEmpty());
507+
assertFalse(event.customMetadataChanged(project2.id(), custom2.getWriteableName()));
508508
}
509509

510510
// Add custom to existing project
@@ -513,7 +513,7 @@ public void testChangedCustomProjectMetadataSet() {
513513
.putProjectMetadata(ProjectMetadata.builder(project1).putCustom(custom2.getWriteableName(), custom2).build())
514514
.build();
515515
ClusterChangedEvent event = new ClusterChangedEvent("_na_", newState, originalState);
516-
assertEquals(Set.of(custom2.getWriteableName()), event.changedCustomProjectMetadataSet(project1.id()));
516+
assertTrue(event.customMetadataChanged(project1.id(), custom2.getWriteableName()));
517517
}
518518

519519
// Remove custom from existing project
@@ -522,7 +522,7 @@ public void testChangedCustomProjectMetadataSet() {
522522
.putProjectMetadata(ProjectMetadata.builder(project1).removeCustom(custom1.getWriteableName()).build())
523523
.build();
524524
ClusterChangedEvent event = new ClusterChangedEvent("_na_", newState, originalState);
525-
assertEquals(Set.of(custom1.getWriteableName()), event.changedCustomProjectMetadataSet(project1.id()));
525+
assertTrue(event.customMetadataChanged(project1.id(), custom1.getWriteableName()));
526526
}
527527

528528
// Add new project with custom
@@ -531,9 +531,10 @@ public void testChangedCustomProjectMetadataSet() {
531531
.putProjectMetadata(ProjectMetadata.builder(project2).build())
532532
.build();
533533
ClusterChangedEvent event = new ClusterChangedEvent("_na_", newState, originalState);
534-
assertEquals(Set.of(IndexGraveyard.TYPE, custom2.getWriteableName()), event.changedCustomProjectMetadataSet(project2.id()));
534+
assertTrue(event.customMetadataChanged(project2.id(), IndexGraveyard.TYPE));
535+
assertTrue(event.customMetadataChanged(project2.id(), custom2.getWriteableName()));
535536
// No change to other project
536-
assertTrue(event.changedCustomProjectMetadataSet(project1.id()).isEmpty());
537+
assertFalse(event.customMetadataChanged(project1.id(), custom1.getWriteableName()));
537538
}
538539

539540
// remove project
@@ -544,9 +545,10 @@ public void testChangedCustomProjectMetadataSet() {
544545
// project2 is removed
545546
ClusterState newState = originalState;
546547
ClusterChangedEvent event = new ClusterChangedEvent("_na_", newState, oldState);
547-
assertEquals(Set.of(IndexGraveyard.TYPE, custom2.getWriteableName()), event.changedCustomProjectMetadataSet(project2.id()));
548+
assertTrue(event.customMetadataChanged(project2.id(), IndexGraveyard.TYPE));
549+
assertTrue(event.customMetadataChanged(project2.id(), custom2.getWriteableName()));
548550
// No change to other project
549-
assertTrue(event.changedCustomProjectMetadataSet(project1.id()).isEmpty());
551+
assertFalse(event.customMetadataChanged(project1.id(), custom1.getWriteableName()));
550552
}
551553

552554
// add custom to project1 + remove project2
@@ -558,8 +560,9 @@ public void testChangedCustomProjectMetadataSet() {
558560
.putProjectMetadata(ProjectMetadata.builder(project1).putCustom(custom2.getWriteableName(), custom2).build())
559561
.build();
560562
ClusterChangedEvent event = new ClusterChangedEvent("_na_", newState, oldState);
561-
assertEquals(Set.of(IndexGraveyard.TYPE, custom2.getWriteableName()), event.changedCustomProjectMetadataSet(project2.id()));
562-
assertEquals(Set.of(custom2.getWriteableName()), event.changedCustomProjectMetadataSet(project1.id()));
563+
assertTrue(event.customMetadataChanged(project2.id(), IndexGraveyard.TYPE));
564+
assertTrue(event.customMetadataChanged(project2.id(), custom2.getWriteableName()));
565+
assertTrue(event.customMetadataChanged(project1.id(), custom2.getWriteableName()));
563566
}
564567
}
565568

0 commit comments

Comments
 (0)