|
| 1 | +/*+***************************************************************************** |
| 2 | + * ___ _ ____ ____ |
| 3 | + * / _ \ _ _ ___ ___| |_| _ \| __ ) |
| 4 | + * | | | | | | |/ _ \/ __| __| | | | _ \ |
| 5 | + * | |_| | |_| | __/\__ \ |_| |_| | |_) | |
| 6 | + * \__\_\\__,_|\___||___/\__|____/|____/ |
| 7 | + * |
| 8 | + * Copyright (c) 2014-2019 Appsicle |
| 9 | + * Copyright (c) 2019-2026 QuestDB |
| 10 | + * |
| 11 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | + * you may not use this file except in compliance with the License. |
| 13 | + * You may obtain a copy of the License at |
| 14 | + * |
| 15 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | + * |
| 17 | + * Unless required by applicable law or agreed to in writing, software |
| 18 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | + * See the License for the specific language governing permissions and |
| 21 | + * limitations under the License. |
| 22 | + * |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +package io.questdb.client.test.cutlass.qwp.client.sf.cursor; |
| 26 | + |
| 27 | +import io.questdb.client.cutlass.http.client.WebSocketClient; |
| 28 | +import io.questdb.client.cutlass.http.client.WebSocketClientFactory; |
| 29 | +import io.questdb.client.cutlass.qwp.client.QwpAuthFailedException; |
| 30 | +import io.questdb.client.cutlass.qwp.client.QwpDurableAckMismatchException; |
| 31 | +import io.questdb.client.cutlass.qwp.client.sf.cursor.CursorSendEngine; |
| 32 | +import io.questdb.client.cutlass.qwp.client.sf.cursor.CursorWebSocketSendLoop; |
| 33 | +import io.questdb.client.std.MemoryTag; |
| 34 | +import io.questdb.client.std.Unsafe; |
| 35 | +import io.questdb.client.test.cutlass.qwp.websocket.TestWebSocketServer; |
| 36 | +import io.questdb.client.test.tools.TestUtils; |
| 37 | +import org.junit.Assert; |
| 38 | +import org.junit.Rule; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.rules.TemporaryFolder; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.nio.ByteBuffer; |
| 44 | +import java.nio.ByteOrder; |
| 45 | +import java.nio.charset.StandardCharsets; |
| 46 | +import java.util.IdentityHashMap; |
| 47 | +import java.util.Map; |
| 48 | +import java.util.concurrent.TimeUnit; |
| 49 | +import java.util.concurrent.atomic.AtomicInteger; |
| 50 | + |
| 51 | +public class CursorWebSocketSendLoopForegroundReconnectPolicyTest { |
| 52 | + private static final long SEGMENT_SIZE_BYTES = 16_384L; |
| 53 | + |
| 54 | + @Rule |
| 55 | + public final TemporaryFolder sfDir = TemporaryFolder.builder().assureDeletion().build(); |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testPostStartAuthFailureRetriesUntilCredentialsRecover() throws Exception { |
| 59 | + assertForegroundRecovers(false, |
| 60 | + () -> new QwpAuthFailedException(401, "localhost", 1)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testPostStartDurableAckMismatchRetriesUntilCapabilityRecovers() throws Exception { |
| 65 | + assertForegroundRecovers(true, |
| 66 | + () -> new QwpDurableAckMismatchException("localhost", 1, "primary")); |
| 67 | + } |
| 68 | + |
| 69 | + private void assertForegroundRecovers(boolean durableAck, FailureSupplier failureSupplier) throws Exception { |
| 70 | + TestUtils.assertMemoryLeak(() -> { |
| 71 | + DropFirstConnectionHandler handler = new DropFirstConnectionHandler(durableAck); |
| 72 | + try (TestWebSocketServer server = new TestWebSocketServer(handler, true); |
| 73 | + CursorSendEngine engine = new CursorSendEngine( |
| 74 | + sfDir.newFolder().getAbsolutePath(), SEGMENT_SIZE_BYTES)) { |
| 75 | + server.start(); |
| 76 | + Assert.assertTrue(server.awaitStart(5, TimeUnit.SECONDS)); |
| 77 | + |
| 78 | + WebSocketClient initialClient = connect(server.getPort(), durableAck); |
| 79 | + ScriptedFactory factory = new ScriptedFactory( |
| 80 | + server.getPort(), durableAck, failureSupplier); |
| 81 | + CursorWebSocketSendLoop loop = new CursorWebSocketSendLoop( |
| 82 | + initialClient, |
| 83 | + engine, |
| 84 | + 0L, |
| 85 | + CursorWebSocketSendLoop.DEFAULT_PARK_NANOS, |
| 86 | + factory, |
| 87 | + 100L, |
| 88 | + 1L, |
| 89 | + 4L, |
| 90 | + durableAck, |
| 91 | + durableAck ? 10L : 0L, |
| 92 | + CursorWebSocketSendLoop.DEFAULT_MAX_HEAD_FRAME_REJECTIONS, |
| 93 | + 0L, |
| 94 | + 0L, |
| 95 | + CursorWebSocketSendLoop.ReconnectPolicy.FOREGROUND); |
| 96 | + try { |
| 97 | + appendFrame(engine, (byte) 1); |
| 98 | + loop.start(); |
| 99 | + |
| 100 | + await(() -> loop.getTotalReconnects() >= 1L |
| 101 | + || loop.getTerminalError() != null, |
| 102 | + "foreground reconnect did not complete"); |
| 103 | + Assert.assertNull("post-start endpoint-policy failures must not stop the producer", |
| 104 | + loop.getTerminalError()); |
| 105 | + Assert.assertTrue("the reconnect loop must retry both scripted failures", |
| 106 | + factory.attempts() >= 3); |
| 107 | + |
| 108 | + long target = appendFrame(engine, (byte) 2); |
| 109 | + await(() -> engine.ackedFsn() >= target || loop.getTerminalError() != null, |
| 110 | + "foreground sender did not deliver after the policy failure cleared"); |
| 111 | + Assert.assertNull(loop.getTerminalError()); |
| 112 | + Assert.assertEquals(target, engine.ackedFsn()); |
| 113 | + } finally { |
| 114 | + loop.close(); |
| 115 | + initialClient.close(); |
| 116 | + } |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + private static long appendFrame(CursorSendEngine engine, byte marker) { |
| 122 | + long buffer = Unsafe.malloc(16, MemoryTag.NATIVE_DEFAULT); |
| 123 | + try { |
| 124 | + for (int i = 0; i < 16; i++) { |
| 125 | + Unsafe.getUnsafe().putByte(buffer + i, marker); |
| 126 | + } |
| 127 | + return engine.appendBlocking(buffer, 16); |
| 128 | + } finally { |
| 129 | + Unsafe.free(buffer, 16, MemoryTag.NATIVE_DEFAULT); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static void await(Condition condition, String failureMessage) throws Exception { |
| 134 | + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); |
| 135 | + while (!condition.isTrue()) { |
| 136 | + if (System.nanoTime() >= deadline) { |
| 137 | + Assert.fail(failureMessage); |
| 138 | + } |
| 139 | + Thread.sleep(1L); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static WebSocketClient connect(int port, boolean durableAck) throws Exception { |
| 144 | + WebSocketClient client = WebSocketClientFactory.newPlainTextInstance(); |
| 145 | + try { |
| 146 | + client.setQwpMaxVersion(1); |
| 147 | + client.setQwpRequestDurableAck(durableAck); |
| 148 | + client.setConnectTimeout(5_000); |
| 149 | + client.connect("localhost", port); |
| 150 | + client.upgrade("/write/v4", 5_000, null); |
| 151 | + return client; |
| 152 | + } catch (Throwable e) { |
| 153 | + client.close(); |
| 154 | + throw e; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @FunctionalInterface |
| 159 | + private interface Condition { |
| 160 | + boolean isTrue() throws Exception; |
| 161 | + } |
| 162 | + |
| 163 | + @FunctionalInterface |
| 164 | + private interface FailureSupplier { |
| 165 | + Exception get(); |
| 166 | + } |
| 167 | + |
| 168 | + private static final class ScriptedFactory implements CursorWebSocketSendLoop.ReconnectFactory { |
| 169 | + private final AtomicInteger attempts = new AtomicInteger(); |
| 170 | + private final boolean durableAck; |
| 171 | + private final FailureSupplier failureSupplier; |
| 172 | + private final int port; |
| 173 | + |
| 174 | + private ScriptedFactory(int port, boolean durableAck, FailureSupplier failureSupplier) { |
| 175 | + this.port = port; |
| 176 | + this.durableAck = durableAck; |
| 177 | + this.failureSupplier = failureSupplier; |
| 178 | + } |
| 179 | + |
| 180 | + int attempts() { |
| 181 | + return attempts.get(); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public WebSocketClient reconnect() throws Exception { |
| 186 | + if (attempts.incrementAndGet() <= 2) { |
| 187 | + throw failureSupplier.get(); |
| 188 | + } |
| 189 | + return connect(port, durableAck); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private static final class DropFirstConnectionHandler |
| 194 | + implements TestWebSocketServer.WebSocketServerHandler { |
| 195 | + private static final String TABLE = "trades"; |
| 196 | + private final boolean durableAck; |
| 197 | + private final Map<TestWebSocketServer.ClientHandler, long[]> wireSeqByConnection = |
| 198 | + new IdentityHashMap<>(); |
| 199 | + private TestWebSocketServer.ClientHandler firstConnection; |
| 200 | + |
| 201 | + private DropFirstConnectionHandler(boolean durableAck) { |
| 202 | + this.durableAck = durableAck; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public synchronized void onBinaryMessage(TestWebSocketServer.ClientHandler client, byte[] data) { |
| 207 | + long[] sequence = wireSeqByConnection.get(client); |
| 208 | + if (sequence == null) { |
| 209 | + sequence = new long[1]; |
| 210 | + wireSeqByConnection.put(client, sequence); |
| 211 | + if (firstConnection == null) { |
| 212 | + firstConnection = client; |
| 213 | + } |
| 214 | + } |
| 215 | + long wireSeq = sequence[0]++; |
| 216 | + try { |
| 217 | + client.sendBinary(okFrame(wireSeq)); |
| 218 | + if (durableAck) { |
| 219 | + client.sendBinary(durableAckFrame(wireSeq)); |
| 220 | + } |
| 221 | + if (client == firstConnection) { |
| 222 | + client.close(); |
| 223 | + } |
| 224 | + } catch (IOException ignored) { |
| 225 | + // The deliberate close may race the acknowledgement write. |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + private static byte[] durableAckFrame(long seqTxn) { |
| 230 | + byte[] name = TABLE.getBytes(StandardCharsets.UTF_8); |
| 231 | + ByteBuffer bb = ByteBuffer.allocate(1 + 2 + 2 + name.length + 8) |
| 232 | + .order(ByteOrder.LITTLE_ENDIAN); |
| 233 | + bb.put((byte) 0x02); |
| 234 | + bb.putShort((short) 1); |
| 235 | + bb.putShort((short) name.length); |
| 236 | + bb.put(name); |
| 237 | + bb.putLong(seqTxn); |
| 238 | + return bb.array(); |
| 239 | + } |
| 240 | + |
| 241 | + private static byte[] okFrame(long wireSeq) { |
| 242 | + byte[] name = TABLE.getBytes(StandardCharsets.UTF_8); |
| 243 | + ByteBuffer bb = ByteBuffer.allocate(1 + 8 + 2 + 2 + name.length + 8) |
| 244 | + .order(ByteOrder.LITTLE_ENDIAN); |
| 245 | + bb.put((byte) 0x00); |
| 246 | + bb.putLong(wireSeq); |
| 247 | + bb.putShort((short) 1); |
| 248 | + bb.putShort((short) name.length); |
| 249 | + bb.put(name); |
| 250 | + bb.putLong(wireSeq); |
| 251 | + return bb.array(); |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments