|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +// Some portions generated by Codex |
| 12 | +package org.eclipse.rdf4j.http.client; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +import java.lang.reflect.Constructor; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.atomic.AtomicLong; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | + |
| 28 | +import ch.qos.logback.classic.Level; |
| 29 | +import ch.qos.logback.classic.Logger; |
| 30 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 31 | +import ch.qos.logback.core.read.ListAppender; |
| 32 | + |
| 33 | +class QueryCircuitBreakerLoggingTest { |
| 34 | + |
| 35 | + private static final Constructor<QueryCircuitBreaker.Configuration> CONFIGURATION_CONSTRUCTOR = configurationCtor(); |
| 36 | + private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(QueryCircuitBreaker.class); |
| 37 | + private static final String ENTRY_THROTTLE_WARNING = "Query circuit breaker is throttling new queries at entry"; |
| 38 | + private static final String RUNNING_THROTTLE_WARNING = "Query circuit breaker is throttling running queries"; |
| 39 | + |
| 40 | + private ListAppender<ILoggingEvent> listAppender; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void attachAppender() { |
| 44 | + listAppender = new ListAppender<>(); |
| 45 | + listAppender.start(); |
| 46 | + LOGGER.addAppender(listAppender); |
| 47 | + } |
| 48 | + |
| 49 | + @AfterEach |
| 50 | + void detachAppender() { |
| 51 | + LOGGER.detachAppender(listAppender); |
| 52 | + listAppender.stop(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void shouldWarnWhenThrottlingNewQueriesAtEntryOncePerMinute() { |
| 57 | + Fixture fixture = new Fixture(); |
| 58 | + QueryCircuitBreaker breaker = fixture.breaker(configuration(true, 100, 200, 300, 400, 300, 200, 25, 10, 1000, |
| 59 | + 7), 1000); |
| 60 | + List<QueryCircuitBreakerHandle> handles = new ArrayList<>(); |
| 61 | + |
| 62 | + try { |
| 63 | + fixture.freeMemoryMb.set(350); |
| 64 | + QueryCircuitBreakerHandle warnHandle = registerHandle(breaker, handles, "warn-entry-1"); |
| 65 | + breaker.beforeExecution(warnHandle); |
| 66 | + |
| 67 | + assertEquals(1, warningCount(ENTRY_THROTTLE_WARNING)); |
| 68 | + assertTrue(lastWarning(ENTRY_THROTTLE_WARNING).contains("action=delay")); |
| 69 | + |
| 70 | + QueryCircuitBreakerHandle secondWarnHandle = registerHandle(breaker, handles, "warn-entry-2"); |
| 71 | + breaker.beforeExecution(secondWarnHandle); |
| 72 | + assertEquals(1, warningCount(ENTRY_THROTTLE_WARNING)); |
| 73 | + |
| 74 | + fixture.clock.addAndGet(60_000); |
| 75 | + QueryCircuitBreakerHandle thirdWarnHandle = registerHandle(breaker, handles, "warn-entry-3"); |
| 76 | + breaker.beforeExecution(thirdWarnHandle); |
| 77 | + assertEquals(2, warningCount(ENTRY_THROTTLE_WARNING)); |
| 78 | + |
| 79 | + fixture.clock.addAndGet(60_000); |
| 80 | + fixture.freeMemoryMb.set(250); |
| 81 | + QueryCircuitBreakerHandle highHandle = registerHandle(breaker, handles, "high-entry"); |
| 82 | + QueryCircuitBreaker.CircuitBreakerException exception = assertThrows( |
| 83 | + QueryCircuitBreaker.CircuitBreakerException.class, () -> breaker.beforeExecution(highHandle)); |
| 84 | + |
| 85 | + assertTrue(exception.isRejected()); |
| 86 | + assertEquals(3, warningCount(ENTRY_THROTTLE_WARNING)); |
| 87 | + assertTrue(lastWarning(ENTRY_THROTTLE_WARNING).contains("action=reject")); |
| 88 | + } finally { |
| 89 | + handles.forEach(breaker::complete); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void shouldWarnWhenThrottlingRunningQueriesOncePerMinute() { |
| 95 | + Fixture fixture = new Fixture(); |
| 96 | + QueryCircuitBreaker breaker = fixture.breaker(configuration(true, 100, 200, 300, 400, 300, 200, 25, 10, 1000, |
| 97 | + 7), 1000); |
| 98 | + QueryCircuitBreakerHandle handle = breaker.register(QueryCircuitBreakerHandle.Source.SERVER, "repo", |
| 99 | + "running-query"); |
| 100 | + |
| 101 | + try { |
| 102 | + fixture.freeMemoryMb.set(250); |
| 103 | + breaker.checkpoint(handle, "JOIN"); |
| 104 | + assertEquals(1, warningCount(RUNNING_THROTTLE_WARNING)); |
| 105 | + |
| 106 | + breaker.checkpoint(handle, "JOIN"); |
| 107 | + assertEquals(1, warningCount(RUNNING_THROTTLE_WARNING)); |
| 108 | + |
| 109 | + fixture.clock.addAndGet(60_000); |
| 110 | + breaker.checkpoint(handle, "JOIN"); |
| 111 | + assertEquals(2, warningCount(RUNNING_THROTTLE_WARNING)); |
| 112 | + } finally { |
| 113 | + breaker.complete(handle); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private int warningCount(String messageFragment) { |
| 118 | + return (int) listAppender.list.stream() |
| 119 | + .filter(event -> event.getLevel() == Level.WARN) |
| 120 | + .map(ILoggingEvent::getFormattedMessage) |
| 121 | + .filter(message -> message.contains(messageFragment)) |
| 122 | + .count(); |
| 123 | + } |
| 124 | + |
| 125 | + private String lastWarning(String messageFragment) { |
| 126 | + return listAppender.list.stream() |
| 127 | + .filter(event -> event.getLevel() == Level.WARN) |
| 128 | + .map(ILoggingEvent::getFormattedMessage) |
| 129 | + .filter(message -> message.contains(messageFragment)) |
| 130 | + .reduce((first, second) -> second) |
| 131 | + .orElseThrow(); |
| 132 | + } |
| 133 | + |
| 134 | + private QueryCircuitBreakerHandle registerHandle(QueryCircuitBreaker breaker, |
| 135 | + List<QueryCircuitBreakerHandle> handles, String queryText) { |
| 136 | + QueryCircuitBreakerHandle handle = breaker.register(QueryCircuitBreakerHandle.Source.SERVER, "repo", queryText); |
| 137 | + handles.add(handle); |
| 138 | + return handle; |
| 139 | + } |
| 140 | + |
| 141 | + private static QueryCircuitBreaker.Configuration configuration(boolean enabled, int warnGcMs, int highGcMs, |
| 142 | + int criticalGcMs, int warnFreeMb, int highFreeMb, int criticalFreeMb, int warnAdmissionDelayMs, |
| 143 | + int checkpointDelayMs, int cancelCooldownMs, int retryAfterSeconds) { |
| 144 | + try { |
| 145 | + return CONFIGURATION_CONSTRUCTOR.newInstance(enabled, warnGcMs, highGcMs, criticalGcMs, warnFreeMb, |
| 146 | + highFreeMb, criticalFreeMb, warnAdmissionDelayMs, checkpointDelayMs, cancelCooldownMs, |
| 147 | + retryAfterSeconds); |
| 148 | + } catch (Exception e) { |
| 149 | + throw new IllegalStateException("Unable to construct query breaker configuration", e); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static Constructor<QueryCircuitBreaker.Configuration> configurationCtor() { |
| 154 | + try { |
| 155 | + Constructor<QueryCircuitBreaker.Configuration> constructor = QueryCircuitBreaker.Configuration.class |
| 156 | + .getDeclaredConstructor(boolean.class, int.class, int.class, int.class, int.class, int.class, |
| 157 | + int.class, int.class, int.class, int.class, int.class); |
| 158 | + constructor.setAccessible(true); |
| 159 | + return constructor; |
| 160 | + } catch (Exception e) { |
| 161 | + throw new IllegalStateException("Unable to access query breaker configuration constructor", e); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static final class Fixture { |
| 166 | + private final AtomicLong clock = new AtomicLong(); |
| 167 | + private final AtomicLong freeMemoryMb = new AtomicLong(1024); |
| 168 | + private final QueryPressureMonitor monitor = new QueryPressureMonitor(freeMemoryMb::get, clock::get, false); |
| 169 | + private final List<Long> sleeps = new ArrayList<>(); |
| 170 | + |
| 171 | + private QueryCircuitBreaker breaker(QueryCircuitBreaker.Configuration configuration, long recoveryCooldownMs) { |
| 172 | + return new QueryCircuitBreaker(monitor, () -> configuration, clock::get, sleeps::add, |
| 173 | + recoveryCooldownMs); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments