|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.jdbc.source; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.common.metrics.MetricsContext; |
| 21 | +import org.apache.seatunnel.api.event.EventListener; |
| 22 | +import org.apache.seatunnel.api.source.SourceEvent; |
| 23 | +import org.apache.seatunnel.api.source.SourceSplitEnumerator; |
| 24 | +import org.apache.seatunnel.api.table.catalog.CatalogTable; |
| 25 | +import org.apache.seatunnel.api.table.catalog.TableIdentifier; |
| 26 | +import org.apache.seatunnel.api.table.catalog.TablePath; |
| 27 | +import org.apache.seatunnel.api.table.catalog.TableSchema; |
| 28 | +import org.apache.seatunnel.connectors.seatunnel.jdbc.config.JdbcConnectionConfig; |
| 29 | +import org.apache.seatunnel.connectors.seatunnel.jdbc.config.JdbcSourceConfig; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.Assertions; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Set; |
| 41 | +import java.util.concurrent.ConcurrentHashMap; |
| 42 | +import java.util.concurrent.atomic.AtomicInteger; |
| 43 | + |
| 44 | +class JdbcSourceSplitEnumeratorTest { |
| 45 | + |
| 46 | + @Test |
| 47 | + void testRunSignalsNoMoreSplitsOnce() throws Exception { |
| 48 | + int parallelism = 1; |
| 49 | + TablePath tablePath = TablePath.of("db", "schema", "table"); |
| 50 | + |
| 51 | + Map<TablePath, JdbcSourceTable> tables = new HashMap<>(); |
| 52 | + tables.put(tablePath, createJdbcSourceTable(tablePath)); |
| 53 | + |
| 54 | + List<Integer> assignTargets = new ArrayList<>(); |
| 55 | + Set<Integer> noMoreSplitsReaders = new HashSet<>(); |
| 56 | + AtomicInteger noMoreSplitsCallCount = new AtomicInteger(); |
| 57 | + |
| 58 | + SourceSplitEnumerator.Context<JdbcSourceSplit> context = |
| 59 | + new SourceSplitEnumerator.Context<JdbcSourceSplit>() { |
| 60 | + @Override |
| 61 | + public int currentParallelism() { |
| 62 | + return parallelism; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Set<Integer> registeredReaders() { |
| 67 | + return Collections.singleton(0); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void assignSplit(int subtaskId, List<JdbcSourceSplit> splits) { |
| 72 | + assignTargets.add(subtaskId); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void signalNoMoreSplits(int subtask) { |
| 77 | + noMoreSplitsCallCount.incrementAndGet(); |
| 78 | + noMoreSplitsReaders.add(subtask); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void sendEventToSourceReader(int subtaskId, SourceEvent event) {} |
| 83 | + |
| 84 | + @Override |
| 85 | + public MetricsContext getMetricsContext() { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public EventListener getEventListener() { |
| 91 | + return null; |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + JdbcSourceConfig sourceConfig = |
| 96 | + JdbcSourceConfig.builder() |
| 97 | + .jdbcConnectionConfig( |
| 98 | + JdbcConnectionConfig.builder() |
| 99 | + .url("jdbc:generic://localhost:0/test") |
| 100 | + .driverName("org.example.Driver") |
| 101 | + .build()) |
| 102 | + .build(); |
| 103 | + |
| 104 | + JdbcSourceSplitEnumerator enumerator = |
| 105 | + new JdbcSourceSplitEnumerator(context, sourceConfig, tables, null); |
| 106 | + |
| 107 | + enumerator.open(); |
| 108 | + enumerator.run(); |
| 109 | + |
| 110 | + Assertions.assertEquals(Collections.singletonList(0), assignTargets); |
| 111 | + Assertions.assertEquals(Collections.singleton(0), noMoreSplitsReaders); |
| 112 | + Assertions.assertEquals(1, noMoreSplitsCallCount.get()); |
| 113 | + |
| 114 | + // NoMoreSplitsEvent is only sent once at the end of run(). |
| 115 | + enumerator.addSplitsBack(Collections.emptyList(), 0); |
| 116 | + enumerator.registerReader(0); |
| 117 | + |
| 118 | + Assertions.assertEquals(1, noMoreSplitsCallCount.get()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void testRunSignalsNoMoreSplitsForAllRegisteredReadersWithHighParallelism() throws Exception { |
| 123 | + int parallelism = 8; |
| 124 | + |
| 125 | + Set<Integer> registeredReaders = new HashSet<>(); |
| 126 | + for (int i = 0; i < parallelism; i++) { |
| 127 | + registeredReaders.add(i); |
| 128 | + } |
| 129 | + |
| 130 | + Map<TablePath, JdbcSourceTable> tables = new HashMap<>(); |
| 131 | + for (int i = 0; i < 3; i++) { |
| 132 | + TablePath tablePath = TablePath.of("db", "schema", "table_" + i); |
| 133 | + tables.put(tablePath, createJdbcSourceTable(tablePath)); |
| 134 | + } |
| 135 | + |
| 136 | + Map<String, Integer> assignedSplitOwners = new HashMap<>(); |
| 137 | + Set<Integer> noMoreSplitsReaders = ConcurrentHashMap.newKeySet(); |
| 138 | + AtomicInteger noMoreSplitsCallCount = new AtomicInteger(); |
| 139 | + |
| 140 | + SourceSplitEnumerator.Context<JdbcSourceSplit> context = |
| 141 | + new SourceSplitEnumerator.Context<JdbcSourceSplit>() { |
| 142 | + @Override |
| 143 | + public int currentParallelism() { |
| 144 | + return parallelism; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Set<Integer> registeredReaders() { |
| 149 | + return new HashSet<>(registeredReaders); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void assignSplit(int subtaskId, List<JdbcSourceSplit> splits) { |
| 154 | + for (JdbcSourceSplit split : splits) { |
| 155 | + assignedSplitOwners.put(split.splitId(), subtaskId); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void signalNoMoreSplits(int subtask) { |
| 161 | + noMoreSplitsCallCount.incrementAndGet(); |
| 162 | + noMoreSplitsReaders.add(subtask); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void sendEventToSourceReader(int subtaskId, SourceEvent event) {} |
| 167 | + |
| 168 | + @Override |
| 169 | + public MetricsContext getMetricsContext() { |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public EventListener getEventListener() { |
| 175 | + return null; |
| 176 | + } |
| 177 | + }; |
| 178 | + |
| 179 | + JdbcSourceConfig sourceConfig = |
| 180 | + JdbcSourceConfig.builder() |
| 181 | + .jdbcConnectionConfig( |
| 182 | + JdbcConnectionConfig.builder() |
| 183 | + .url("jdbc:generic://localhost:0/test") |
| 184 | + .driverName("org.example.Driver") |
| 185 | + .build()) |
| 186 | + .build(); |
| 187 | + |
| 188 | + JdbcSourceSplitEnumerator enumerator = |
| 189 | + new JdbcSourceSplitEnumerator(context, sourceConfig, tables, null); |
| 190 | + |
| 191 | + enumerator.open(); |
| 192 | + enumerator.run(); |
| 193 | + |
| 194 | + Assertions.assertEquals(tables.size(), assignedSplitOwners.size()); |
| 195 | + assignedSplitOwners.forEach( |
| 196 | + (splitId, owner) -> { |
| 197 | + int expectedOwner = (splitId.hashCode() & Integer.MAX_VALUE) % parallelism; |
| 198 | + Assertions.assertEquals(expectedOwner, owner); |
| 199 | + }); |
| 200 | + |
| 201 | + Assertions.assertEquals(registeredReaders, noMoreSplitsReaders); |
| 202 | + Assertions.assertEquals(parallelism, noMoreSplitsCallCount.get()); |
| 203 | + Assertions.assertEquals(0, enumerator.currentUnassignedSplitSize()); |
| 204 | + } |
| 205 | + |
| 206 | + private JdbcSourceTable createJdbcSourceTable(TablePath tablePath) { |
| 207 | + TableIdentifier tableId = TableIdentifier.of("default", tablePath); |
| 208 | + TableSchema tableSchema = TableSchema.builder().columns(Collections.emptyList()).build(); |
| 209 | + CatalogTable catalogTable = |
| 210 | + CatalogTable.of( |
| 211 | + tableId, tableSchema, Collections.emptyMap(), Collections.emptyList(), ""); |
| 212 | + return JdbcSourceTable.builder().tablePath(tablePath).catalogTable(catalogTable).build(); |
| 213 | + } |
| 214 | +} |
0 commit comments