|
39 | 39 | import java.util.HashMap; |
40 | 40 | import java.util.List; |
41 | 41 | import java.util.Map; |
42 | | -import java.util.Objects; |
43 | 42 |
|
44 | 43 | /** A CustomPostgresSchema similar to PostgresSchema with customization. */ |
45 | 44 | public class CustomPostgresSchema { |
@@ -82,7 +81,7 @@ public Map<TableId, TableChange> getTableSchema(List<TableId> tableIds) { |
82 | 81 |
|
83 | 82 | if (!unMatchTableIds.isEmpty()) { |
84 | 83 | try { |
85 | | - readTableSchema(tableIds); |
| 84 | + readTableSchema(unMatchTableIds); |
86 | 85 | } catch (SQLException e) { |
87 | 86 | throw new FlinkRuntimeException("Failed to read table schema", e); |
88 | 87 | } |
@@ -119,26 +118,37 @@ private List<TableChange> readTableSchema(List<TableId> tableIds) throws SQLExce |
119 | 118 | throw new FlinkRuntimeException("Failed to read schema", e); |
120 | 119 | } |
121 | 120 |
|
122 | | - for (TableId tableId : tableIds) { |
123 | | - Table table = Objects.requireNonNull(tables.forTable(tableId)); |
124 | | - // set the events to populate proper sourceInfo into offsetContext |
125 | | - offsetContext.event(tableId, Instant.now()); |
126 | | - |
127 | | - // TODO: check whether we always set isFromSnapshot = true |
| 121 | + // Cache all tables discovered by readSchema to avoid redundant full scans on future calls. |
| 122 | + for (TableId discoveredId : tables.tableIds()) { |
| 123 | + if (this.schemasByTableId.containsKey(discoveredId)) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + Table table = tables.forTable(discoveredId); |
| 127 | + if (table == null) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + offsetContext.event(discoveredId, Instant.now()); |
128 | 131 | SchemaChangeEvent schemaChangeEvent = |
129 | 132 | SchemaChangeEvent.ofCreate( |
130 | 133 | partition, |
131 | 134 | offsetContext, |
132 | 135 | dbzConfig.databaseName(), |
133 | | - tableId.schema(), |
| 136 | + discoveredId.schema(), |
134 | 137 | null, |
135 | 138 | table, |
136 | 139 | true); |
137 | | - |
138 | 140 | for (TableChanges.TableChange tableChange : schemaChangeEvent.getTableChanges()) { |
139 | | - this.schemasByTableId.put(tableId, tableChange); |
| 141 | + this.schemasByTableId.put(discoveredId, tableChange); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + for (TableId tableId : tableIds) { |
| 146 | + TableChange cached = this.schemasByTableId.get(tableId); |
| 147 | + if (cached == null) { |
| 148 | + throw new FlinkRuntimeException( |
| 149 | + String.format("Failed to read table schema of table %s", tableId)); |
140 | 150 | } |
141 | | - tableChanges.add(this.schemasByTableId.get(tableId)); |
| 151 | + tableChanges.add(cached); |
142 | 152 | } |
143 | 153 | return tableChanges; |
144 | 154 | } |
|
0 commit comments