Skip to content
Open
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 @@ -16,15 +16,17 @@

package org.springframework.cloud.gateway.support;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.core.style.ToStringCreator;

public abstract class AbstractStatefulConfigurable<C> extends AbstractConfigurable<C>
implements StatefulConfigurable<C> {

private Map<String, C> config = new HashMap<>();
// Written while routes are built, which may run on several threads at once, and
// read on every request (e.g. RedisRateLimiter#isAllowed).
private final Map<String, C> config = new ConcurrentHashMap<>();

protected AbstractStatefulConfigurable(Class<C> configClass) {
super(configClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2013-present the original author or authors.
*
* Licensed 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 org.springframework.cloud.gateway.support;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.IntStream;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link AbstractStatefulConfigurable}.
*
* <p>
* The per-route config map is written while routes are built, which can happen on
* several threads at once (for example a custom refresh overlapping the refresh on
* startup). Concurrent writes must not drop entries: a missing entry makes
* {@code RedisRateLimiter#isAllowed} throw for every request on that route until the
* next refresh.
*
* @author Jaeyeob Jung
*/
public class AbstractStatefulConfigurableTests {

private static final int THREADS = 8;

private static final int ROUNDS = 500;

private static final int ROUTES = 25;

@Test
public void concurrentWritesKeepEveryRouteConfig() throws Exception {
List<String> routeIds = IntStream.range(0, ROUTES).mapToObj(i -> "route-" + i).toList();
ExecutorService executor = Executors.newFixedThreadPool(THREADS);
try {
for (int round = 0; round < ROUNDS; round++) {
TestConfigurable configurable = new TestConfigurable();
CyclicBarrier startTogether = new CyclicBarrier(THREADS);
// every thread writes the same route ids into an initially empty map
Callable<Void> populate = () -> {
startTogether.await();
for (String routeId : routeIds) {
configurable.getConfig().put(routeId, new Object());
}
return null;
};
List<Future<Void>> results = executor.invokeAll(Collections.nCopies(THREADS, populate));
for (Future<Void> result : results) {
result.get();
}
assertThat(configurable.getConfig()).as("round %d", round).containsOnlyKeys(routeIds);
}
}
finally {
executor.shutdownNow();
}
}

static class TestConfigurable extends AbstractStatefulConfigurable<Object> {

TestConfigurable() {
super(Object.class);
}

}

}
Loading