maksaska commented on code in PR #311: URL: https://github.com/apache/ignite-extensions/pull/311#discussion_r2217360739
########## modules/cdc-ext/src/main/java/org/apache/ignite/cdc/postgresql/IgniteToPostgreSqlCdcApplier.java: ########## @@ -0,0 +1,661 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.cdc.postgresql; + +import java.math.BigDecimal; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import javax.sql.DataSource; +import org.apache.ignite.IgniteException; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.cache.CacheEntryVersion; +import org.apache.ignite.cache.QueryEntity; +import org.apache.ignite.cdc.CdcCacheEvent; +import org.apache.ignite.cdc.CdcEvent; +import org.apache.ignite.internal.util.typedef.F; + +import static org.apache.ignite.internal.processors.cache.GridCacheUtils.UNDEFINED_CACHE_ID; + +/** */ +public class IgniteToPostgreSqlCdcApplier { + /** */ + public static final String DFLT_SQL_TYPE = "OTHER"; + + /** */ + public static final Map<String, String> JAVA_TO_SQL_TYPES; + + static { + Map<String, String> map = new HashMap<>(); + + map.put("java.lang.String", "VARCHAR"); + map.put("java.lang.Integer", "INT"); + map.put("int", "INT"); + map.put("java.lang.Long", "BIGINT"); + map.put("long", "BIGINT"); + map.put("java.lang.Boolean", "BOOLEAN"); + map.put("boolean", "BOOLEAN"); + map.put("java.lang.Double", "DOUBLE PRECISION"); + map.put("double", "DOUBLE PRECISION"); + map.put("java.lang.Float", "REAL"); + map.put("float", "REAL"); + map.put("java.math.BigDecimal", "DECIMAL"); + map.put("java.lang.Short", "SMALLINT"); + map.put("short", "SMALLINT"); + map.put("java.lang.Byte", "SMALLINT"); + map.put("byte", "SMALLINT"); + map.put("java.util.UUID", "UUID"); + map.put("[B", "BYTEA"); + map.put("java.lang.Object", "OTHER"); + + JAVA_TO_SQL_TYPES = Collections.unmodifiableMap(map); + } + + /** */ + private final DataSource dataSrc; + + /** */ + private final boolean autoCommit; + + /** */ + private final long maxBatchSize; + + /** */ + private final IgniteLogger log; + + /** */ + private final Map<Integer, String> cacheIdToUpsertQry = new HashMap<>(); + + /** */ + private final Map<Integer, String> cacheIdToDeleteQry = new HashMap<>(); + + /** */ + private final Map<Integer, Set<String>> cacheIdToPrimaryKeys = new HashMap<>(); + + /** */ + private final Map<Integer, Set<String>> cacheIdToFields = new HashMap<>(); + + /** */ + private final Set<Object> curKeys = new HashSet<>(); + + /** + * @param dataSrc {@link DataSource} - connection pool to PostgreSql + * @param autoCommit - autoCommit flag for batch execution + * @param maxBatchSize the maximum number of CDC events to include in a single batch + * @param log the {@link IgniteLogger} instance used for logging CDC processing events + */ + public IgniteToPostgreSqlCdcApplier(DataSource dataSrc, boolean autoCommit, long maxBatchSize, IgniteLogger log) { + this.dataSrc = dataSrc; + this.autoCommit = autoCommit; + this.maxBatchSize = maxBatchSize; + this.log = log; + } + + /** + * @param evts an {@link Iterator} of {@link CdcEvent} objects to be applied + * @return the total number of events successfully batched and executed + */ + public long applyEvents(Iterator<CdcEvent> evts) { + try (Connection conn = dataSrc.getConnection()) { + conn.setAutoCommit(autoCommit); + + long res = applyEvents(conn, evts); + + conn.commit(); + + return res; + } + catch (Throwable e) { + log.error(e.getMessage(), e); + + throw new IgniteException("CDC failure", e); + } + } + + /** + * @param conn connection to PostgreSql + * @param evts an {@link Iterator} of {@link CdcEvent} objects to be applied + * @return the total number of events successfully batched and executed + */ + private long applyEvents(Connection conn, Iterator<CdcEvent> evts) { + long evtsApplied = 0; + + int currCacheId = UNDEFINED_CACHE_ID; + boolean prevOpIsDelete = false; + + PreparedStatement curPrepStmt = null; + CdcEvent evt; + + while (evts.hasNext()) { + evt = evts.next(); + + if (log.isDebugEnabled()) + log.debug("Event received [evt=" + evt + ']'); + + if (currCacheId != evt.cacheId() || prevOpIsDelete ^ (evt.value() == null)) { + if (curPrepStmt != null) + evtsApplied += executeBatch(curPrepStmt); + + currCacheId = evt.cacheId(); + prevOpIsDelete = evt.value() == null; + + curPrepStmt = prepareStatement(conn, evt); + } + + if (curKeys.size() >= maxBatchSize || curKeys.contains(evt.key())) + evtsApplied += executeBatch(curPrepStmt); + + addEvent(curPrepStmt, evt); + } + + if (currCacheId != UNDEFINED_CACHE_ID) + evtsApplied += executeBatch(curPrepStmt); + + return evtsApplied; + } + + /** + * @param curPrepStmt {@link PreparedStatement} + * @return the total number of batches successfully executed. One CdcEvent - one batch. + */ + private int executeBatch(PreparedStatement curPrepStmt) { + try { + curKeys.clear(); + + if (log.isDebugEnabled()) + log.debug("Applying batch " + curPrepStmt.toString()); + + if (!curPrepStmt.isClosed()) + return curPrepStmt.executeBatch().length; + + throw new IgniteException("Tried to execute on closed prepared statement!"); + } + catch (SQLException e) { + log.error(e.getMessage(), e); + + throw new IgniteException(e); + } + } + + /** + * @param conn connection to PostgreSql + * @param evt {@link CdcEvent} + * @return relevant {@link PreparedStatement} + */ + private PreparedStatement prepareStatement(Connection conn, CdcEvent evt) { + String sqlQry; + + if (evt.value() == null) + sqlQry = cacheIdToDeleteQry.get(evt.cacheId()); + else + sqlQry = cacheIdToUpsertQry.get(evt.cacheId()); + + if (sqlQry == null) + throw new IgniteException("No SQL query is found for cacheId=" + evt.cacheId()); + + if (log.isDebugEnabled()) + log.debug("Statement updated [cacheId=" + evt.cacheId() + ", sqlQry=" + sqlQry + ']'); + + try { + return conn.prepareStatement(sqlQry); + } + catch (SQLException e) { + log.error(e.getMessage(), e); + + throw new IgniteException(e); + } + } + + /** + * @param curPrepStmt {@link PreparedStatement} + * @param evt {@link CdcEvent} + */ + private void addEvent(PreparedStatement curPrepStmt, CdcEvent evt) { + try { + if (evt.value() == null) + addEvent(curPrepStmt, evt, true); + else { + int idx = addEvent(curPrepStmt, evt, false); + + curPrepStmt.setBytes(idx, encodeVersion(evt.version())); Review Comment: Yes, indeed ########## modules/cdc-ext/src/main/java/org/apache/ignite/cdc/postgresql/IgniteToPostgreSqlCdcApplier.java: ########## @@ -0,0 +1,661 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.cdc.postgresql; + +import java.math.BigDecimal; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import javax.sql.DataSource; +import org.apache.ignite.IgniteException; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.cache.CacheEntryVersion; +import org.apache.ignite.cache.QueryEntity; +import org.apache.ignite.cdc.CdcCacheEvent; +import org.apache.ignite.cdc.CdcEvent; +import org.apache.ignite.internal.util.typedef.F; + +import static org.apache.ignite.internal.processors.cache.GridCacheUtils.UNDEFINED_CACHE_ID; + +/** */ +public class IgniteToPostgreSqlCdcApplier { + /** */ + public static final String DFLT_SQL_TYPE = "OTHER"; + + /** */ + public static final Map<String, String> JAVA_TO_SQL_TYPES; + + static { + Map<String, String> map = new HashMap<>(); + + map.put("java.lang.String", "VARCHAR"); + map.put("java.lang.Integer", "INT"); + map.put("int", "INT"); + map.put("java.lang.Long", "BIGINT"); + map.put("long", "BIGINT"); + map.put("java.lang.Boolean", "BOOLEAN"); + map.put("boolean", "BOOLEAN"); + map.put("java.lang.Double", "DOUBLE PRECISION"); + map.put("double", "DOUBLE PRECISION"); + map.put("java.lang.Float", "REAL"); + map.put("float", "REAL"); + map.put("java.math.BigDecimal", "DECIMAL"); + map.put("java.lang.Short", "SMALLINT"); + map.put("short", "SMALLINT"); + map.put("java.lang.Byte", "SMALLINT"); + map.put("byte", "SMALLINT"); + map.put("java.util.UUID", "UUID"); + map.put("[B", "BYTEA"); + map.put("java.lang.Object", "OTHER"); + + JAVA_TO_SQL_TYPES = Collections.unmodifiableMap(map); + } + + /** */ + private final DataSource dataSrc; + + /** */ + private final boolean autoCommit; + + /** */ + private final long maxBatchSize; + + /** */ + private final IgniteLogger log; + + /** */ + private final Map<Integer, String> cacheIdToUpsertQry = new HashMap<>(); + + /** */ + private final Map<Integer, String> cacheIdToDeleteQry = new HashMap<>(); + + /** */ + private final Map<Integer, Set<String>> cacheIdToPrimaryKeys = new HashMap<>(); + + /** */ + private final Map<Integer, Set<String>> cacheIdToFields = new HashMap<>(); + + /** */ + private final Set<Object> curKeys = new HashSet<>(); + + /** + * @param dataSrc {@link DataSource} - connection pool to PostgreSql + * @param autoCommit - autoCommit flag for batch execution + * @param maxBatchSize the maximum number of CDC events to include in a single batch + * @param log the {@link IgniteLogger} instance used for logging CDC processing events + */ + public IgniteToPostgreSqlCdcApplier(DataSource dataSrc, boolean autoCommit, long maxBatchSize, IgniteLogger log) { + this.dataSrc = dataSrc; + this.autoCommit = autoCommit; + this.maxBatchSize = maxBatchSize; + this.log = log; + } + + /** + * @param evts an {@link Iterator} of {@link CdcEvent} objects to be applied + * @return the total number of events successfully batched and executed + */ + public long applyEvents(Iterator<CdcEvent> evts) { + try (Connection conn = dataSrc.getConnection()) { + conn.setAutoCommit(autoCommit); + + long res = applyEvents(conn, evts); + + conn.commit(); + + return res; + } + catch (Throwable e) { + log.error(e.getMessage(), e); + + throw new IgniteException("CDC failure", e); + } + } + + /** + * @param conn connection to PostgreSql + * @param evts an {@link Iterator} of {@link CdcEvent} objects to be applied + * @return the total number of events successfully batched and executed + */ + private long applyEvents(Connection conn, Iterator<CdcEvent> evts) { + long evtsApplied = 0; + + int currCacheId = UNDEFINED_CACHE_ID; + boolean prevOpIsDelete = false; + + PreparedStatement curPrepStmt = null; + CdcEvent evt; + + while (evts.hasNext()) { + evt = evts.next(); + + if (log.isDebugEnabled()) + log.debug("Event received [evt=" + evt + ']'); + + if (currCacheId != evt.cacheId() || prevOpIsDelete ^ (evt.value() == null)) { + if (curPrepStmt != null) + evtsApplied += executeBatch(curPrepStmt); + + currCacheId = evt.cacheId(); + prevOpIsDelete = evt.value() == null; + + curPrepStmt = prepareStatement(conn, evt); + } + + if (curKeys.size() >= maxBatchSize || curKeys.contains(evt.key())) + evtsApplied += executeBatch(curPrepStmt); + + addEvent(curPrepStmt, evt); + } + + if (currCacheId != UNDEFINED_CACHE_ID) + evtsApplied += executeBatch(curPrepStmt); + + return evtsApplied; + } + + /** + * @param curPrepStmt {@link PreparedStatement} + * @return the total number of batches successfully executed. One CdcEvent - one batch. + */ + private int executeBatch(PreparedStatement curPrepStmt) { + try { + curKeys.clear(); + + if (log.isDebugEnabled()) + log.debug("Applying batch " + curPrepStmt.toString()); + + if (!curPrepStmt.isClosed()) + return curPrepStmt.executeBatch().length; + + throw new IgniteException("Tried to execute on closed prepared statement!"); + } + catch (SQLException e) { + log.error(e.getMessage(), e); + + throw new IgniteException(e); + } + } + + /** + * @param conn connection to PostgreSql + * @param evt {@link CdcEvent} + * @return relevant {@link PreparedStatement} + */ + private PreparedStatement prepareStatement(Connection conn, CdcEvent evt) { + String sqlQry; + + if (evt.value() == null) + sqlQry = cacheIdToDeleteQry.get(evt.cacheId()); + else + sqlQry = cacheIdToUpsertQry.get(evt.cacheId()); + + if (sqlQry == null) + throw new IgniteException("No SQL query is found for cacheId=" + evt.cacheId()); + + if (log.isDebugEnabled()) + log.debug("Statement updated [cacheId=" + evt.cacheId() + ", sqlQry=" + sqlQry + ']'); + + try { + return conn.prepareStatement(sqlQry); + } + catch (SQLException e) { + log.error(e.getMessage(), e); + + throw new IgniteException(e); + } + } + + /** + * @param curPrepStmt {@link PreparedStatement} + * @param evt {@link CdcEvent} + */ + private void addEvent(PreparedStatement curPrepStmt, CdcEvent evt) { + try { + if (evt.value() == null) + addEvent(curPrepStmt, evt, true); + else { + int idx = addEvent(curPrepStmt, evt, false); + + curPrepStmt.setBytes(idx, encodeVersion(evt.version())); + } + + curPrepStmt.addBatch(); + } + catch (Throwable e) { + log.error(e.getMessage(), e); + + throw new IgniteException(e); + } + } + + /** + * @param curPrepStmt {@link PreparedStatement} + * @param evt {@link CdcEvent} + * @param isDelete - flag that indicate delete sql statement usage + * @return number of filled values + */ + private int addEvent(PreparedStatement curPrepStmt, CdcEvent evt, boolean isDelete) throws SQLException { + Iterator<String> itFields = isDelete ? + cacheIdToPrimaryKeys.get(evt.cacheId()).iterator() : + cacheIdToFields.get(evt.cacheId()).iterator(); + + String field; + + BinaryObject keyObj = (evt.key() instanceof BinaryObject) ? (BinaryObject)evt.key() : null; + BinaryObject valObj = (evt.value() instanceof BinaryObject) ? (BinaryObject)evt.value() : null; + + int idx = 1; + Object obj; + + while (itFields.hasNext()) { + field = itFields.next(); + + if (cacheIdToPrimaryKeys.get(evt.cacheId()).contains(field)) + if (keyObj != null) + obj = keyObj.field(field); + else + obj = evt.key(); + else + if (valObj != null) + obj = valObj.field(field); + else + obj = evt.value(); + + addObject(curPrepStmt, idx, obj); + + idx++; + } + + return idx; + } + + /** + * Sets a value in the PreparedStatement at the given index using the appropriate setter + * based on the runtime type of the object. + * @param curPrepStmt {@link PreparedStatement} + * @param idx value index in {@link PreparedStatement} + * @param obj value + */ + private void addObject(PreparedStatement curPrepStmt, int idx, Object obj) throws SQLException { + if (obj == null) { + curPrepStmt.setObject(idx, null); + + return; + } + + if (obj instanceof String) + curPrepStmt.setString(idx, (String)obj); + else if (obj instanceof Integer) + curPrepStmt.setInt(idx, (Integer)obj); + else if (obj instanceof Long) + curPrepStmt.setLong(idx, (Long)obj); + else if (obj instanceof Short) + curPrepStmt.setShort(idx, (Short)obj); + else if (obj instanceof Byte) + curPrepStmt.setByte(idx, (Byte)obj); + else if (obj instanceof Boolean) + curPrepStmt.setBoolean(idx, (Boolean)obj); + else if (obj instanceof Float) + curPrepStmt.setFloat(idx, (Float)obj); + else if (obj instanceof Double) + curPrepStmt.setDouble(idx, (Double)obj); + else if (obj instanceof BigDecimal) + curPrepStmt.setBigDecimal(idx, (BigDecimal)obj); + else if (obj instanceof UUID) + curPrepStmt.setObject(idx, obj, Types.OTHER); // PostgreSQL expects UUID as OTHER + else if (obj instanceof byte[]) + curPrepStmt.setBytes(idx, (byte[])obj); + else + curPrepStmt.setObject(idx, obj); + } + + /** + * @param evts an {@link Iterator} of {@link CdcCacheEvent} objects to apply + * @param createTables tables creation flag. If true - attempt to create tables will be made. + * @return Number of applied events. + */ + public long applyCacheEvents(Iterator<CdcCacheEvent> evts, boolean createTables) { + CdcCacheEvent evt; + QueryEntity entity; + + long cnt = 0; + + while (evts.hasNext()) { + evt = evts.next(); + + if (evt.queryEntities().size() != 1) + throw new IgniteException("There should be exactly 1 QueryEntity for cacheId: " + evt.cacheId()); + + entity = evt.queryEntities().iterator().next(); + + if (createTables) + createTableIfNotExists(entity); + + cacheIdToUpsertQry.put(evt.cacheId(), getUpsertSqlQry(entity)); + + cacheIdToDeleteQry.put(evt.cacheId(), getDeleteSqlQry(entity)); + + cacheIdToPrimaryKeys.put(evt.cacheId(), getPrimaryKeys(entity)); + + cacheIdToFields.put(evt.cacheId(), entity.getFields().keySet()); + + if (log.isInfoEnabled()) + log.info("Cache table created [tableName=" + entity.getTableName() + + ", columns=" + entity.getFields().keySet() + ']'); + + cnt++; + } + + return cnt; + } + + /** + * @param entity the {@link QueryEntity} describing the table schema to create + */ + private void createTableIfNotExists(QueryEntity entity) { + String createSqlStmt = getCreateTableSqlStatement(entity); + + try (Connection conn = dataSrc.getConnection(); Statement stmt = conn.createStatement()) { + stmt.execute(createSqlStmt); + } + catch (SQLException e) { + log.error(e.getMessage(), e); + + throw new IgniteException(e); + } + } + + /** + * Generates the SQL statement for creating a table. + * + * @param entity QueryEntity instance describing the cache structure. + * @return SQL statement for creating a table. + */ + private String getCreateTableSqlStatement(QueryEntity entity) { + StringBuilder ddl = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(entity.getTableName()).append(" ("); + + addFieldsAndTypes(entity, ddl); + + ddl.append(", version BYTEA NOT NULL"); + + ddl.append(", PRIMARY KEY ("); + + addPrimaryKeys(entity, ddl); + + ddl.append(')').append(')'); + + return ddl.toString(); + } + + /** + * Constructs DDL-compatible SQL fragment listing fields along with their mapped SQL types. + * + * @param entity QueryEntity instance describing the cache structure. + * @param sql Target StringBuilder where the result will be appended. + */ + private void addFieldsAndTypes(QueryEntity entity, StringBuilder sql) { + Iterator<Map.Entry<String, String>> iter = entity.getFields().entrySet().iterator(); + + Map.Entry<String, String> field; + String type; + + Integer precision; + Integer scale; + + while (iter.hasNext()) { + field = iter.next(); + type = JAVA_TO_SQL_TYPES.getOrDefault(field.getValue(), DFLT_SQL_TYPE); + + sql.append(field.getKey()).append(" ").append(type); + + precision = entity.getFieldsPrecision().get(field.getKey()); + scale = entity.getFieldsScale().get(field.getKey()); + + if (precision != null && precision > 0) { + if (type.equals("VARCHAR") || type.equals("TIME") || type.equals("TIMESTAMP") || Review Comment: Done! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org