|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.polaris.service.events.listeners.inmemory; |
| 21 | + |
| 22 | +import static org.apache.polaris.core.entity.PolarisEvent.ResourceType.CATALOG; |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.awaitility.Awaitility.await; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.Mockito.doReturn; |
| 27 | +import static org.mockito.Mockito.doThrow; |
| 28 | +import static org.mockito.Mockito.reset; |
| 29 | + |
| 30 | +import com.google.common.collect.ImmutableMap; |
| 31 | +import io.netty.channel.EventLoopGroup; |
| 32 | +import io.quarkus.netty.MainEventLoopGroup; |
| 33 | +import io.quarkus.test.junit.QuarkusTest; |
| 34 | +import io.quarkus.test.junit.QuarkusTestProfile; |
| 35 | +import io.quarkus.test.junit.TestProfile; |
| 36 | +import io.quarkus.test.junit.mockito.InjectSpy; |
| 37 | +import io.smallrye.common.annotation.Identifier; |
| 38 | +import jakarta.enterprise.inject.Instance; |
| 39 | +import jakarta.inject.Inject; |
| 40 | +import java.sql.Connection; |
| 41 | +import java.sql.ResultSet; |
| 42 | +import java.sql.Statement; |
| 43 | +import java.time.Duration; |
| 44 | +import java.util.Map; |
| 45 | +import java.util.UUID; |
| 46 | +import javax.sql.DataSource; |
| 47 | +import org.apache.polaris.core.entity.PolarisEvent; |
| 48 | +import org.apache.polaris.core.persistence.MetaStoreManagerFactory; |
| 49 | +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; |
| 50 | +import org.junit.jupiter.api.AfterEach; |
| 51 | +import org.junit.jupiter.api.Test; |
| 52 | +import org.junit.jupiter.api.TestInstance; |
| 53 | +import org.mockito.Mockito; |
| 54 | + |
| 55 | +@QuarkusTest |
| 56 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 57 | +@TestProfile(InMemoryEventProducerTest.Profile.class) |
| 58 | +class InMemoryEventProducerTest { |
| 59 | + |
| 60 | + public static class Profile implements QuarkusTestProfile { |
| 61 | + |
| 62 | + @Override |
| 63 | + public Map<String, String> getConfigOverrides() { |
| 64 | + return ImmutableMap.<String, String>builder() |
| 65 | + .put("polaris.realm-context.realms", "test1,test2") |
| 66 | + .put("polaris.persistence.type", "relational-jdbc") |
| 67 | + .put("polaris.persistence.auto-bootstrap-types", "relational-jdbc") |
| 68 | + .put("quarkus.datasource.db-kind", "h2") |
| 69 | + .put( |
| 70 | + "quarkus.datasource.jdbc.url", |
| 71 | + "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE") |
| 72 | + .put("polaris.event-listener.type", "persistence-in-memory") |
| 73 | + .put("polaris.event-listener.persistence-in-memory-buffer.buffer-time", "5s") |
| 74 | + .put("polaris.event-listener.persistence-in-memory-buffer.max-buffer-size", "10") |
| 75 | + .put( |
| 76 | + "quarkus.fault-tolerance.\"org.apache.polaris.service.events.listeners.inmemory.InMemoryEventListener/flush\".retry.max-retries", |
| 77 | + "1") |
| 78 | + .put( |
| 79 | + "quarkus.fault-tolerance.\"org.apache.polaris.service.events.listeners.inmemory.InMemoryEventListener/flush\".retry.delay", |
| 80 | + "10") |
| 81 | + .build(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // A delay shorter than the full 5s buffer timeout |
| 86 | + private static final Duration SHORT_DELAY = Duration.ofSeconds(4); |
| 87 | + |
| 88 | + // A delay longer than the full 5s buffer timeout |
| 89 | + public static final Duration LONG_DELAY = Duration.ofSeconds(8); |
| 90 | + |
| 91 | + @Inject |
| 92 | + @Identifier("persistence-in-memory") |
| 93 | + InMemoryEventListener producer; |
| 94 | + |
| 95 | + @InjectSpy |
| 96 | + @Identifier("relational-jdbc") |
| 97 | + @SuppressWarnings("CdiInjectionPointsInspection") |
| 98 | + MetaStoreManagerFactory metaStoreManagerFactory; |
| 99 | + |
| 100 | + @Inject |
| 101 | + @MainEventLoopGroup |
| 102 | + @SuppressWarnings("CdiInjectionPointsInspection") |
| 103 | + EventLoopGroup eventLoopGroup; |
| 104 | + |
| 105 | + @Inject Instance<DataSource> dataSource; |
| 106 | + |
| 107 | + @Test |
| 108 | + void testFlushOnSize() { |
| 109 | + sendAsync("test1", 10); |
| 110 | + sendAsync("test2", 10); |
| 111 | + assertRows("test1", 10, SHORT_DELAY); |
| 112 | + assertRows("test2", 10, SHORT_DELAY); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void testFlushOnTimeout() { |
| 117 | + sendAsync("test1", 5); |
| 118 | + sendAsync("test2", 5); |
| 119 | + assertRows("test1", 5, LONG_DELAY); |
| 120 | + assertRows("test2", 5, LONG_DELAY); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testFlushOnShutdown() { |
| 125 | + producer.processEvent("test1", event()); |
| 126 | + producer.processEvent("test2", event()); |
| 127 | + producer.shutdown(); |
| 128 | + assertRows("test1", 1, SHORT_DELAY); |
| 129 | + assertRows("test2", 1, SHORT_DELAY); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testFailureRecovery() { |
| 134 | + var manager = Mockito.mock(PolarisMetaStoreManager.class); |
| 135 | + doReturn(manager).when(metaStoreManagerFactory).getOrCreateMetaStoreManager(any()); |
| 136 | + RuntimeException error = new RuntimeException("error"); |
| 137 | + doThrow(error) |
| 138 | + .doThrow(error) // first batch will give up after 2 attempts |
| 139 | + .doThrow(error) |
| 140 | + .doCallRealMethod() // second batch will succeed on the 2nd attempt |
| 141 | + .when(manager) |
| 142 | + .writeEvents(any(), any()); |
| 143 | + sendAsync("test1", 20); |
| 144 | + assertRows("test1", 10, SHORT_DELAY); |
| 145 | + } |
| 146 | + |
| 147 | + @AfterEach |
| 148 | + void clearEvents() throws Exception { |
| 149 | + reset(metaStoreManagerFactory); |
| 150 | + producer.shutdown(); |
| 151 | + try (Connection connection = dataSource.get().getConnection(); |
| 152 | + Statement statement = connection.createStatement()) { |
| 153 | + statement.execute("DELETE FROM polaris_schema.events"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private void sendAsync(String realmId, int n) { |
| 158 | + for (int i = 0; i < n; i++) { |
| 159 | + eventLoopGroup.next().execute(() -> producer.processEvent(realmId, event())); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private void assertRows(String realmId, int expected, Duration timeout) { |
| 164 | + String query = "SELECT COUNT(*) FROM polaris_schema.events WHERE realm_id = '" + realmId + "'"; |
| 165 | + await() |
| 166 | + .atMost(timeout) |
| 167 | + .untilAsserted( |
| 168 | + () -> { |
| 169 | + try (Connection connection = dataSource.get().getConnection(); |
| 170 | + Statement statement = connection.createStatement(); |
| 171 | + ResultSet rs = statement.executeQuery(query)) { |
| 172 | + rs.next(); |
| 173 | + assertThat(rs.getInt(1)).isEqualTo(expected); |
| 174 | + } |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + private static PolarisEvent event() { |
| 179 | + String id = UUID.randomUUID().toString(); |
| 180 | + return new PolarisEvent("test", id, null, "test", 0, null, CATALOG, "test"); |
| 181 | + } |
| 182 | +} |
0 commit comments