|
| 1 | +/* |
| 2 | + * Copyright 2024 OceanBase. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.oceanbase.test; |
| 18 | + |
| 19 | +import com.oceanbase.clogproxy.client.LogProxyClient; |
| 20 | +import com.oceanbase.clogproxy.client.config.ClientConf; |
| 21 | +import com.oceanbase.clogproxy.client.config.ObReaderConfig; |
| 22 | +import com.oceanbase.clogproxy.client.exception.LogProxyClientException; |
| 23 | +import com.oceanbase.clogproxy.client.listener.RecordListener; |
| 24 | +import com.oceanbase.oms.logmessage.DataMessage; |
| 25 | +import com.oceanbase.oms.logmessage.LogMessage; |
| 26 | +import java.sql.Connection; |
| 27 | +import java.sql.Driver; |
| 28 | +import java.sql.SQLException; |
| 29 | +import java.sql.Statement; |
| 30 | +import java.time.Duration; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Properties; |
| 33 | +import java.util.concurrent.BlockingQueue; |
| 34 | +import java.util.concurrent.CountDownLatch; |
| 35 | +import java.util.concurrent.LinkedBlockingQueue; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 38 | +import java.util.stream.Collectors; |
| 39 | +import java.util.stream.Stream; |
| 40 | +import org.junit.jupiter.api.Assertions; |
| 41 | +import org.junit.jupiter.params.ParameterizedTest; |
| 42 | +import org.junit.jupiter.params.provider.Arguments; |
| 43 | +import org.junit.jupiter.params.provider.MethodSource; |
| 44 | + |
| 45 | +public class LogProxyCETest { |
| 46 | + |
| 47 | + private static final org.slf4j.Logger LOG = |
| 48 | + org.slf4j.LoggerFactory.getLogger(LogProxyCETest.class); |
| 49 | + |
| 50 | + static Stream<Arguments> testLogProxyCEArgs() { |
| 51 | + // non-null env vars |
| 52 | + String observerIP = Utils.getNonEmptyEnv("observer_ip"); |
| 53 | + String logProxyIP = Utils.getNonEmptyEnv("oblogproxy_ip"); |
| 54 | + String logProxyPort = Utils.getNonEmptyEnv("oblogproxy_port"); |
| 55 | + String username = Utils.getNonEmptyEnv("username"); |
| 56 | + String password = Utils.getNonEmptyEnv("password"); |
| 57 | + |
| 58 | + return Stream.of( |
| 59 | + Arguments.of(observerIP, logProxyIP, "2983", username, password), |
| 60 | + Arguments.of(observerIP, "127.0.0.1", logProxyPort, username, password)); |
| 61 | + } |
| 62 | + |
| 63 | + @ParameterizedTest |
| 64 | + @MethodSource("testLogProxyCEArgs") |
| 65 | + public void testLogProxyCE( |
| 66 | + String observerIP, |
| 67 | + String logProxyIP, |
| 68 | + String logProxyPort, |
| 69 | + String username, |
| 70 | + String password) |
| 71 | + throws InterruptedException { |
| 72 | + |
| 73 | + LOG.info( |
| 74 | + "Testing with args: [observerIP: {}, logProxyIP: {}, logProxyPort: {}, username: {}, password: {}]", |
| 75 | + observerIP, |
| 76 | + logProxyIP, |
| 77 | + logProxyPort, |
| 78 | + username, |
| 79 | + password); |
| 80 | + |
| 81 | + ObReaderConfig obReaderConfig = new ObReaderConfig(); |
| 82 | + |
| 83 | + Properties props = new Properties(); |
| 84 | + props.setProperty("user", username); |
| 85 | + props.setProperty("password", password); |
| 86 | + |
| 87 | + Driver driver = Utils.getDriver(false); |
| 88 | + try (Connection conn = driver.connect("jdbc:mysql://" + observerIP + ":2881/test", props)) { |
| 89 | + obReaderConfig.setRsList(Utils.getRSList(conn)); |
| 90 | + obReaderConfig.setTableWhiteList(Utils.getTenantName(conn) + ".*.*"); |
| 91 | + } catch (SQLException e) { |
| 92 | + Assertions.fail(e); |
| 93 | + } |
| 94 | + |
| 95 | + obReaderConfig.setUsername(username); |
| 96 | + obReaderConfig.setPassword(password); |
| 97 | + obReaderConfig.setStartTimestamp(0L); |
| 98 | + obReaderConfig.setWorkingMode("memory"); |
| 99 | + |
| 100 | + ClientConf clientConf = |
| 101 | + ClientConf.builder() |
| 102 | + .transferQueueSize(1000) |
| 103 | + .connectTimeoutMs((int) Duration.ofSeconds(30).toMillis()) |
| 104 | + .maxReconnectTimes(0) |
| 105 | + .ignoreUnknownRecordType(true) |
| 106 | + .build(); |
| 107 | + |
| 108 | + LogProxyClient client = |
| 109 | + new LogProxyClient( |
| 110 | + logProxyIP, Integer.parseInt(logProxyPort), obReaderConfig, clientConf); |
| 111 | + |
| 112 | + BlockingQueue<LogMessage> messageQueue = new LinkedBlockingQueue<>(2); |
| 113 | + AtomicBoolean started = new AtomicBoolean(false); |
| 114 | + CountDownLatch latch = new CountDownLatch(1); |
| 115 | + |
| 116 | + client.addListener( |
| 117 | + new RecordListener() { |
| 118 | + @Override |
| 119 | + public void notify(LogMessage message) { |
| 120 | + switch (message.getOpt()) { |
| 121 | + case HEARTBEAT: |
| 122 | + LOG.info( |
| 123 | + "Received heartbeat with checkpoint {}", |
| 124 | + message.getCheckpoint()); |
| 125 | + if (started.compareAndSet(false, true)) { |
| 126 | + latch.countDown(); |
| 127 | + } |
| 128 | + break; |
| 129 | + case BEGIN: |
| 130 | + LOG.info("Received transaction begin: {}", message); |
| 131 | + break; |
| 132 | + case COMMIT: |
| 133 | + LOG.info("Received transaction commit: {}", message); |
| 134 | + break; |
| 135 | + case INSERT: |
| 136 | + case UPDATE: |
| 137 | + case DELETE: |
| 138 | + case DDL: |
| 139 | + try { |
| 140 | + messageQueue.put(message); |
| 141 | + } catch (InterruptedException e) { |
| 142 | + throw new RuntimeException("Failed to add message to queue", e); |
| 143 | + } |
| 144 | + break; |
| 145 | + default: |
| 146 | + throw new IllegalArgumentException( |
| 147 | + "Unsupported log message type: " + message.getOpt()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void onException(LogProxyClientException e) { |
| 153 | + LOG.error(e.toString()); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + client.start(); |
| 158 | + |
| 159 | + if (!latch.await(30, TimeUnit.SECONDS)) { |
| 160 | + Assertions.fail("Timeout to receive heartbeat message"); |
| 161 | + } |
| 162 | + |
| 163 | + String ddl = "CREATE TABLE t_product (id INT(10) PRIMARY KEY, name VARCHAR(20))"; |
| 164 | + try (Connection conn = driver.connect("jdbc:mysql://" + observerIP + ":2881/test", props); |
| 165 | + Statement statement = conn.createStatement()) { |
| 166 | + statement.execute(ddl); |
| 167 | + statement.execute("INSERT INTO t_product VALUES (1, 'meat')"); |
| 168 | + } catch (SQLException e) { |
| 169 | + Assertions.fail(e); |
| 170 | + } |
| 171 | + |
| 172 | + while (messageQueue.size() < 2) { |
| 173 | + Thread.sleep(1000); |
| 174 | + } |
| 175 | + |
| 176 | + LogMessage first = messageQueue.take(); |
| 177 | + Assertions.assertEquals(DataMessage.Record.Type.DDL, first.getOpt()); |
| 178 | + Assertions.assertEquals(ddl, first.getFieldList().get(0).getValue().toString()); |
| 179 | + |
| 180 | + LogMessage second = messageQueue.take(); |
| 181 | + Assertions.assertEquals(DataMessage.Record.Type.INSERT, second.getOpt()); |
| 182 | + Assertions.assertEquals("t_product", second.getTableName()); |
| 183 | + |
| 184 | + Map<String, String> fieldMap = |
| 185 | + second.getFieldList().stream() |
| 186 | + .filter(f -> !f.isPrev()) |
| 187 | + .collect( |
| 188 | + Collectors.toMap( |
| 189 | + DataMessage.Record.Field::getFieldname, |
| 190 | + f -> f.getValue().toString())); |
| 191 | + Assertions.assertEquals(2, fieldMap.size()); |
| 192 | + Assertions.assertEquals("1", fieldMap.get("id")); |
| 193 | + Assertions.assertEquals("meat", fieldMap.get("name")); |
| 194 | + |
| 195 | + try (Connection conn = driver.connect("jdbc:mysql://" + observerIP + ":2881/test", props); |
| 196 | + Statement statement = conn.createStatement()) { |
| 197 | + statement.execute("DROP TABLE t_product"); |
| 198 | + } catch (SQLException e) { |
| 199 | + Assertions.fail(e); |
| 200 | + } |
| 201 | + |
| 202 | + client.stop(); |
| 203 | + } |
| 204 | +} |
0 commit comments