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

4.0.11: Convert ConcurrentHashMap which does service loading to HashMap with reentrant lock #8991

Merged
merged 1 commit into from
Jul 16, 2024
Merged
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,12 +17,15 @@
package io.helidon.common.configurable;

import java.lang.System.Logger.Level;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -50,7 +53,8 @@ class ObserverManager {
private static final LazyValue<List<ExecutorServiceSupplierObserver>> OBSERVERS = LazyValue
.create(ObserverManager::loadObservers);

private static final Map<Supplier<? extends ExecutorService>, SupplierInfo> SUPPLIERS = new ConcurrentHashMap<>();
private static final Map<Supplier<? extends ExecutorService>, SupplierInfo> SUPPLIERS = new HashMap<>();
private static final ReadWriteLock SUPPLIERS_LOCK = new ReentrantReadWriteLock();

// A given supplier category can have multiple suppliers, so keep track of the next available index by category.
private static final Map<String, AtomicInteger> SUPPLIER_CATEGORY_NEXT_INDEX_VALUES = new ConcurrentHashMap<>();
Expand All @@ -71,13 +75,18 @@ private ObserverManager() {
static void registerSupplier(Supplier<? extends ExecutorService> supplier,
String supplierCategory,
String executorServiceCategory) {
int supplierIndex = SUPPLIER_CATEGORY_NEXT_INDEX_VALUES.computeIfAbsent(supplierCategory, key -> new AtomicInteger())
.getAndIncrement();
SUPPLIERS.computeIfAbsent(supplier,
s -> SupplierInfo.create(s,
executorServiceCategory,
supplierCategory,
supplierIndex));
SUPPLIERS_LOCK.writeLock().lock();
try {
int supplierIndex = SUPPLIER_CATEGORY_NEXT_INDEX_VALUES.computeIfAbsent(supplierCategory, key -> new AtomicInteger())
.getAndIncrement();
SUPPLIERS.computeIfAbsent(supplier,
s -> SupplierInfo.create(s,
executorServiceCategory,
supplierCategory,
supplierIndex));
} finally {
SUPPLIERS_LOCK.writeLock().unlock();
}
}

/**
Expand All @@ -90,7 +99,13 @@ static void registerSupplier(Supplier<? extends ExecutorService> supplier,
* @throws IllegalStateException if the supplier has not previously registered itself
*/
static <E extends ExecutorService> E registerExecutorService(Supplier<E> supplier, E executorService) {
SupplierInfo supplierInfo = SUPPLIERS.get(supplier);
SUPPLIERS_LOCK.readLock().lock();
SupplierInfo supplierInfo;
try {
supplierInfo = SUPPLIERS.get(supplier);
} finally {
SUPPLIERS_LOCK.readLock().unlock();
}
if (supplierInfo == null) {
throw new IllegalStateException("Attempt to register an executor service to an unregistered supplier");
}
Expand Down