Skip to content

Commit

Permalink
introduce ReentrantShortLock
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhee17 committed Apr 26, 2023
1 parent e7b5f63 commit 1c12c96
Show file tree
Hide file tree
Showing 24 changed files with 79 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import com.google.common.annotations.VisibleForTesting;

import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

import io.netty.channel.EventLoop;

abstract class AbstractEventLoopState {
Expand All @@ -35,7 +37,7 @@ static AbstractEventLoopState of(List<EventLoop> eventLoops, int maxNumEventLoop
return new HeapBasedEventLoopState(eventLoops, maxNumEventLoops, scheduler);
}

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();
private final List<EventLoop> eventLoops;
private final DefaultEventLoopScheduler scheduler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.ReleasableHolder;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
Expand All @@ -58,7 +59,7 @@ final class DefaultEventLoopScheduler implements EventLoopScheduler {

static final int DEFAULT_MAX_NUM_EVENT_LOOPS = 1;

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();

private final List<EventLoop> eventLoops;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.linecorp.armeria.common.Cookies;
import com.linecorp.armeria.common.Scheme;
import com.linecorp.armeria.common.SessionProtocol;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

import io.netty.util.NetUtil;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
Expand All @@ -60,7 +61,7 @@ final class DefaultCookieJar implements CookieJar {
this.cookiePolicy = cookiePolicy;
store = new Object2LongOpenHashMap<>();
filter = new HashMap<>();
lock = new ReentrantLock();
lock = new ReentrantShortLock();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.linecorp.armeria.common.util.AsyncCloseableSupport;
import com.linecorp.armeria.common.util.EventLoopCheckingFuture;
import com.linecorp.armeria.common.util.ListenableAsyncCloseable;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

/**
* A dynamic {@link EndpointGroup}. The list of {@link Endpoint}s can be updated dynamically.
Expand All @@ -66,7 +67,7 @@ public static DynamicEndpointGroupBuilder builder() {
private final EndpointSelectionStrategy selectionStrategy;
private final AtomicReference<EndpointSelector> selector = new AtomicReference<>();
private volatile List<Endpoint> endpoints = UNINITIALIZED_ENDPOINTS;
private final Lock endpointsLock = new ReentrantLock();
private final Lock endpointsLock = new ReentrantShortLock();

private final CompletableFuture<List<Endpoint>> initialEndpointsFuture = new InitialEndpointsFuture();
private final AsyncCloseableSupport closeable = AsyncCloseableSupport.of(this::closeAsync);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.common.base.MoreObjects;

import com.linecorp.armeria.client.endpoint.FileWatcherRunnable.FileWatchEvent;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

/**
* A registry which wraps a {@link WatchService} and allows paths to be registered.
Expand Down Expand Up @@ -133,7 +134,7 @@ void close() throws IOException {

private final Map<FileSystem, FileSystemWatchContext> fileSystemWatchServiceMap =
new HashMap<>();
private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();

/**
* Registers a {@code filePath} and {@code callback} to the {@link WatchService}. When the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import java.util.function.Supplier;

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

/**
* A restartable thread utility class.
*/
final class RestartableThread {

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();

@Nullable
private Thread thread;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

/**
* This selector selects an {@link Endpoint} using random and the weight of the {@link Endpoint}. If there are
Expand All @@ -37,7 +38,7 @@
*/
final class WeightedRandomDistributionEndpointSelector {

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();
private final List<Entry> allEntries;
@GuardedBy("lock")
private final List<Entry> currentEntries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.AsyncCloseable;
import com.linecorp.armeria.common.util.EventLoopCheckingFuture;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

import io.netty.channel.EventLoopGroup;
import io.netty.util.concurrent.Future;
Expand All @@ -54,7 +55,7 @@ final class DefaultHealthCheckerContext
private final Endpoint endpoint;
private final SessionProtocol protocol;
private final ClientOptions clientOptions;
private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();

/**
* Keeps the {@link Future}s which were scheduled via this {@link ScheduledExecutorService}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.metric.MeterIdPrefix;
import com.linecorp.armeria.common.util.AsyncCloseable;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

import io.micrometer.core.instrument.binder.MeterBinder;

Expand Down Expand Up @@ -110,7 +111,7 @@ public static HealthCheckedEndpointGroupBuilder builder(EndpointGroup delegate,
@VisibleForTesting
final HealthCheckStrategy healthCheckStrategy;

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();
@GuardedBy("lock")
private final Deque<HealthCheckContextGroup> contextGroupChain = new ArrayDeque<>(4);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.linecorp.armeria.common.util.AsyncCloseable;
import com.linecorp.armeria.common.util.AsyncCloseableSupport;
import com.linecorp.armeria.common.util.TimeoutMode;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;
import com.linecorp.armeria.unsafe.PooledObjects;

import io.netty.util.AsciiString;
Expand All @@ -60,7 +61,7 @@ final class HttpHealthChecker implements AsyncCloseable {

private static final AsciiString ARMERIA_LPHC = HttpHeaderNames.of("armeria-lphc");

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();
private final HealthCheckerContext ctx;
private final WebClient webClient;
private final String authority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.util.IdentityHashStrategy;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenCustomHashSet;

Expand All @@ -40,7 +41,7 @@ public abstract class AbstractListenable<T> implements Listenable<T> {
private final Set<Consumer<? super T>> updateListeners =
new ObjectLinkedOpenCustomHashSet<>(IdentityHashStrategy.of());

private final ReentrantLock reentrantLock = new ReentrantLock();
private final ReentrantLock reentrantLock = new ReentrantShortLock();

/**
* Notify the new value changes to the listeners added via {@link #addListener(Consumer)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.common.collect.ImmutableSet;

import com.linecorp.armeria.client.ClientOption;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

/**
* A configuration option.
Expand Down Expand Up @@ -236,7 +237,7 @@ private static final class Pool {

private final Class<?> type;
private final BiMap<String, AbstractOption<?, ?, ?>> options;
private final ReentrantLock reentrantLock = new ReentrantLock();
private final ReentrantLock reentrantLock = new ReentrantShortLock();

Pool(Class<?> type) {
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import com.linecorp.armeria.common.Flags;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

/**
* Represents an exception that is a composite of one or more other exceptions. A {@code CompositeException}
Expand Down Expand Up @@ -81,7 +82,7 @@ public final class CompositeException extends RuntimeException {
@Nullable
private Throwable cause;

private final ReentrantLock reentrantLock = new ReentrantLock();
private final ReentrantLock reentrantLock = new ReentrantShortLock();

/**
* Constructs a CompositeException with the given array of Throwables as the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.annotation.UnstableApi;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

/**
* A utility class for adding a task with an {@link AutoCloseable} on shutdown.
Expand All @@ -46,7 +47,7 @@ public final class ShutdownHooks {
private static final Map<AutoCloseable, Queue<Runnable>> autoCloseableOnShutdownTasks =
new LinkedHashMap<>();

private static final ReentrantLock reentrantLock = new ReentrantLock();
private static final ReentrantLock reentrantLock = new ReentrantShortLock();

private static final ThreadFactory THREAD_FACTORY = ThreadFactories
.builder("armeria-shutdown-hook")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.slf4j.LoggerFactory;

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

/**
* Provides asynchronous start-stop life cycle support.
Expand Down Expand Up @@ -63,7 +64,7 @@ enum State {
*/
private UnmodifiableFuture<?> future = completedFuture(null);

private final ReentrantLock reentrantLock = new ReentrantLock();
private final ReentrantLock reentrantLock = new ReentrantShortLock();

/**
* Creates a new instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.linecorp.armeria.common.DependencyInjector;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

public final class ReflectiveDependencyInjector implements DependencyInjector {

Expand All @@ -57,7 +58,7 @@ public static <T> T create(Class<? extends T> type, @Nullable Map<Class<?>, Obje
return instance;
}

private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();

private final Map<Class<?>, Object> instances = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.metric.MeterIdPrefix;
import com.linecorp.armeria.common.util.Ticker;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

import io.micrometer.core.instrument.MeterRegistry;

Expand Down Expand Up @@ -86,7 +87,7 @@ private static final class CaffeineMetrics {

private final MeterRegistry parent;
private final MeterIdPrefix idPrefix;
private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();
@GuardedBy("lock")
private final List<CacheReference> cacheRefs = new ArrayList<>(2);
private final AtomicBoolean hasLoadingCache = new AtomicBoolean();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class MinifiedBouncyCastleProvider extends Provider implements Conf

private static final String PROVIDER_NAME = "ArmeriaBC";

private static final ReentrantLock lock = new ReentrantLock();
private static final ReentrantLock lock = new ReentrantShortLock();

private static final Map<ASN1ObjectIdentifier, AsymmetricKeyInfoConverter> keyInfoConverters =
new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2023 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.linecorp.armeria.internal.common.util;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantShortLock extends ReentrantLock {

@Override
public void lock() {
super.lock();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import com.linecorp.armeria.common.DependencyInjector;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;
import com.linecorp.armeria.internal.server.annotation.AnnotatedValueResolver.NoAnnotatedParameterException;
import com.linecorp.armeria.internal.server.annotation.AnnotatedValueResolver.RequestObjectResolver;
import com.linecorp.armeria.server.annotation.RequestConverter;
Expand All @@ -63,7 +64,7 @@
final class AnnotatedBeanFactoryRegistry {
private static final Logger logger = LoggerFactory.getLogger(AnnotatedBeanFactoryRegistry.class);

private static final ReentrantLock lock = new ReentrantLock();
private static final ReentrantLock lock = new ReentrantShortLock();

private static final ClassValue<AnnotatedBeanFactories> factories =
new ClassValue<AnnotatedBeanFactories>() {
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/linecorp/armeria/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import com.linecorp.armeria.common.util.Version;
import com.linecorp.armeria.internal.common.RequestTargetCache;
import com.linecorp.armeria.internal.common.util.ChannelUtil;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;

import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -114,7 +115,7 @@ public static ServerBuilder builder() {
private final UpdatableServerConfig config;
private final StartStopSupport<Void, Void, Void, ServerListener> startStop;
private final Set<ServerChannel> serverChannels = new NonBlockingHashSet<>();
private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();
@GuardedBy("lock")
private final Map<InetSocketAddress, ServerPort> activePorts = new LinkedHashMap<>();
private final ConnectionLimitingHandler connectionLimitingHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.TimeoutMode;
import com.linecorp.armeria.internal.common.ArmeriaHttpUtil;
import com.linecorp.armeria.internal.common.util.ReentrantShortLock;
import com.linecorp.armeria.server.HttpService;
import com.linecorp.armeria.server.HttpStatusException;
import com.linecorp.armeria.server.RequestTimeoutException;
Expand Down Expand Up @@ -137,7 +138,7 @@ public static HealthCheckServiceBuilder builder() {
private final long maxLongPollingTimeoutMillis;
private final double longPollingTimeoutJitterRate;
private final long pingIntervalMillis;
private final ReentrantLock lock = new ReentrantLock();
private final ReentrantLock lock = new ReentrantShortLock();
@Nullable
private final Consumer<HealthChecker> healthCheckerListener;
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.linecorp.armeria.testing.junit5.common.EventLoopExtension;

class JacksonUtilTest {

@RegisterExtension
static EventLoopExtension eventLoop = new EventLoopExtension();

@Test
void shouldSerializeJdk8TypesWithDefaultMapper() throws JsonProcessingException {
final ObjectMapper objectMapper = JacksonUtil.newDefaultObjectMapper();
Expand Down
Loading

0 comments on commit 1c12c96

Please sign in to comment.