fix(appconfigdata): consume configuration tokens only once (#7082)

This commit is contained in:
Jimmy Liu Jia authored and GitHub committed 2026-10-31 10:05:25 -04:00
1 parent 65b0881954
commit b3dad93daf
2 files changed
+104

No files matched your search

@@ -104,6 +104,15 @@ public class AppConfigDataService {
public ConfigurationData getLatestConfiguration(String token, String accept) {
ConfigurationSession session = sessionStore.get(token)
.orElseThrow(() -> new AwsException("BadRequestException", "Invalid configuration token", 400));
synchronized (session) {
if (!token.equals(session.getCurrentToken())) {
throw new AwsException("BadRequestException", "Invalid configuration token", 400);
}
return pollConfiguration(session, token, accept);
}
}
private ConfigurationData pollConfiguration(ConfigurationSession session, String token, String accept) {
appConfigService.getEnvironment(session.getApplicationId(), session.getEnvironmentId());
int pollInterval = normalizePollInterval(session.getRequiredMinimumPollIntervalInSeconds());
@@ -1,18 +1,113 @@
package io.github.hectorvent.floci.services.appconfig;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.services.appconfig.model.ConfigurationSession;
import io.github.hectorvent.floci.testing.InMemoryStorageFactory;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static io.github.hectorvent.floci.testing.ThreadStates.awaitBlockedOrDone;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
class AppConfigDataServiceTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
@Test
void concurrentPollsConsumeAConfigurationTokenOnlyOnce() throws Exception {
InMemoryStorageFactory factory = new InMemoryStorageFactory("000000000000");
AppConfigService appConfig = mock(AppConfigService.class);
AppConfigDataService service = new AppConfigDataService(factory, appConfig, MAPPER);
AccountAwareStorageBackend<ConfigurationSession> sessions = factory.create("appconfigdata",
"appconfigdata-sessions.json", new TypeReference<Map<String, ConfigurationSession>>() {});
ConfigurationSession session = new ConfigurationSession();
session.setApplicationId("app");
session.setEnvironmentId("env");
session.setConfigurationProfileId("profile");
session.setCurrentToken("initial");
sessions.put("initial", session);
ConfigurationSession otherSession = new ConfigurationSession();
otherSession.setApplicationId("app");
otherSession.setEnvironmentId("env");
otherSession.setConfigurationProfileId("profile");
otherSession.setCurrentToken("other");
sessions.put("other", otherSession);
CountDownLatch firstReadToken = new CountDownLatch(1);
CountDownLatch releaseFirst = new CountDownLatch(1);
AtomicBoolean first = new AtomicBoolean(true);
doAnswer(invocation -> {
if (first.compareAndSet(true, false)) {
firstReadToken.countDown();
assertTrue(releaseFirst.await(5, TimeUnit.SECONDS));
}
return null;
}).when(appConfig).getEnvironment("app", "env");
ExecutorService pool = Executors.newFixedThreadPool(3);
try {
Future<Object> firstCall = pool.submit(() -> pollOrError(service));
assertTrue(firstReadToken.await(5, TimeUnit.SECONDS));
AtomicReference<Thread> secondThread = new AtomicReference<>();
Future<Object> secondCall = pool.submit(() -> {
secondThread.set(Thread.currentThread());
return pollOrError(service);
});
awaitBlockedOrDone(secondThread, secondCall);
assertTrue(secondCall.isDone() || (secondThread.get() != null
&& secondThread.get().getState() == Thread.State.BLOCKED), "second poll overlaps the first");
Future<AppConfigDataService.ConfigurationData> otherCall = pool.submit(
() -> service.getLatestConfiguration("other"));
assertInstanceOf(AppConfigDataService.ConfigurationData.class, otherCall.get(5, TimeUnit.SECONDS));
releaseFirst.countDown();
List<Object> results = List.of(firstCall.get(5, TimeUnit.SECONDS), secondCall.get(5, TimeUnit.SECONDS));
assertEquals(1, results.stream().filter(AppConfigDataService.ConfigurationData.class::isInstance).count());
assertEquals(1, results.stream().filter(AwsException.class::isInstance).count());
AwsException rejected = (AwsException) results.stream().filter(AwsException.class::isInstance)
.findFirst().orElseThrow();
assertEquals("BadRequestException", rejected.getErrorCode());
assertEquals("Invalid configuration token", rejected.getMessage());
assertEquals(400, rejected.getHttpStatus());
AppConfigDataService.ConfigurationData accepted = (AppConfigDataService.ConfigurationData) results.stream()
.filter(AppConfigDataService.ConfigurationData.class::isInstance).findFirst().orElseThrow();
assertTrue(sessions.get("initial").isEmpty());
assertTrue(sessions.get(accepted.nextPollConfigurationToken()).isPresent());
assertEquals("BadRequestException", assertThrows(AwsException.class,
() -> service.getLatestConfiguration("initial")).getErrorCode());
} finally {
releaseFirst.countDown();
pool.shutdownNow();
}
}
private static Object pollOrError(AppConfigDataService service) {
try {
return service.getLatestConfiguration("initial");
} catch (AwsException e) {
return e;
}
}
@Test
void transformFeatureFlagsProducesRetrievalTimeValues() throws Exception {
byte[] deploymentContent = """