|
| 1 | +package datastax.astra.migrate; |
| 2 | + |
| 3 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 4 | +import com.datastax.oss.driver.api.core.cql.*; |
| 5 | +import com.datastax.oss.driver.shaded.guava.common.util.concurrent.RateLimiter; |
| 6 | +import org.apache.commons.lang.SerializationUtils; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.apache.spark.SparkConf; |
| 10 | + |
| 11 | +import java.io.Serializable; |
| 12 | +import java.math.BigInteger; |
| 13 | +import java.util.*; |
| 14 | +import java.util.concurrent.CompletionStage; |
| 15 | +import java.util.concurrent.atomic.AtomicLong; |
| 16 | + |
| 17 | +public class OriginCountJobSession extends BaseJobSession{ |
| 18 | + public Logger logger = LoggerFactory.getLogger(this.getClass().getName()); |
| 19 | + private static OriginCountJobSession originCountJobSession; |
| 20 | + protected AtomicLong readCounter = new AtomicLong(0); |
| 21 | + protected List<Integer> updateSelectMapping = new ArrayList<Integer>(); |
| 22 | + protected Boolean checkTableforColSize; |
| 23 | + protected String checkTableforselectCols; |
| 24 | + protected String filterColName; |
| 25 | + protected String filterColType; |
| 26 | + protected Integer filterColIndex; |
| 27 | + protected List<MigrateDataType> checkTableforColSizeTypes = new ArrayList<MigrateDataType>(); |
| 28 | + public static OriginCountJobSession getInstance(CqlSession sourceSession, SparkConf sparkConf) { |
| 29 | + if (originCountJobSession == null) { |
| 30 | + synchronized (OriginCountJobSession.class) { |
| 31 | + if (originCountJobSession == null) { |
| 32 | + originCountJobSession = new OriginCountJobSession(sourceSession, sparkConf); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return originCountJobSession; |
| 38 | + } |
| 39 | + |
| 40 | + protected OriginCountJobSession(CqlSession sourceSession, SparkConf sparkConf) { |
| 41 | + this.sourceSession = sourceSession; |
| 42 | + batchSize = new Integer(sparkConf.get("spark.batchSize", "1")); |
| 43 | + printStatsAfter = new Integer(sparkConf.get("spark.printStatsAfter", "100000")); |
| 44 | + if (printStatsAfter < 1) { |
| 45 | + printStatsAfter = 100000; |
| 46 | + } |
| 47 | + |
| 48 | + readLimiter = RateLimiter.create(new Integer(sparkConf.get("spark.readRateLimit", "20000"))); |
| 49 | + sourceKeyspaceTable = sparkConf.get("spark.source.keyspaceTable"); |
| 50 | + |
| 51 | + hasRandomPartitioner = Boolean.parseBoolean(sparkConf.get("spark.source.hasRandomPartitioner", "false")); |
| 52 | + isCounterTable = Boolean.parseBoolean(sparkConf.get("spark.source.counterTable", "false")); |
| 53 | + |
| 54 | + checkTableforColSize = Boolean.parseBoolean(sparkConf.get("spark.source.checkTableforColSize", "false")); |
| 55 | + checkTableforselectCols = sparkConf.get("spark.source.checkTableforColSize.cols"); |
| 56 | + checkTableforColSizeTypes = getTypes(sparkConf.get("spark.source.checkTableforColSize.cols.types")); |
| 57 | + filterColName = sparkConf.get("spark.source.FilterColumn"); |
| 58 | + filterColType = sparkConf.get("spark.source.FilterColumnType"); |
| 59 | + filterColIndex = Integer.parseInt(sparkConf.get("spark.source.FilterColumnIndex", "0")); |
| 60 | + |
| 61 | + String partionKey = sparkConf.get("spark.query.cols.partitionKey"); |
| 62 | + idColTypes = getTypes(sparkConf.get("spark.query.cols.id.types")); |
| 63 | + |
| 64 | + String selectCols = sparkConf.get("spark.query.cols.select"); |
| 65 | + String updateSelectMappingStr = sparkConf.get("spark.source.counterTable.update.select.index", "0"); |
| 66 | + for (String updateSelectIndex : updateSelectMappingStr.split(",")) { |
| 67 | + updateSelectMapping.add(Integer.parseInt(updateSelectIndex)); |
| 68 | + } |
| 69 | + String sourceSelectCondition = sparkConf.get("spark.query.cols.select.condition", ""); |
| 70 | + sourceSelectStatement = sourceSession.prepare( |
| 71 | + "select " + selectCols + " from " + sourceKeyspaceTable + " where token(" + partionKey.trim() |
| 72 | + + ") >= ? and token(" + partionKey.trim() + ") <= ? " + sourceSelectCondition + " ALLOW FILTERING"); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + public void getData(BigInteger min, BigInteger max) { |
| 77 | + logger.info("TreadID: " + Thread.currentThread().getId() + " Processing min: " + min + " max:" + max); |
| 78 | + int maxAttempts = maxRetries; |
| 79 | + for (int retryCount = 1; retryCount <= maxAttempts; retryCount++) { |
| 80 | + |
| 81 | + try { |
| 82 | + ResultSet resultSet = sourceSession.execute(sourceSelectStatement.bind(hasRandomPartitioner ? min : min.longValueExact(), hasRandomPartitioner ? max : max.longValueExact())); |
| 83 | + Collection<CompletionStage<AsyncResultSet>> writeResults = new ArrayList<CompletionStage<AsyncResultSet>>(); |
| 84 | + |
| 85 | + // cannot do batching if the writeFilter is greater than 0 or |
| 86 | + // maxWriteTimeStampFilter is less than max long |
| 87 | + // do not batch for counters as it adds latency & increases chance of discrepancy |
| 88 | + if (batchSize == 1 || writeTimeStampFilter || isCounterTable) { |
| 89 | + for (Row sourceRow : resultSet) { |
| 90 | + readLimiter.acquire(1); |
| 91 | + |
| 92 | + if(checkTableforColSize) { |
| 93 | + int rowColcnt = GetRowColumnLength(sourceRow, filterColType, filterColIndex); |
| 94 | + String result = ""; |
| 95 | + if (rowColcnt > 1024 * 1024 * 10) { |
| 96 | + for (int index = 0; index < checkTableforColSizeTypes.size(); index++) { |
| 97 | + MigrateDataType dataType = checkTableforColSizeTypes.get(index); |
| 98 | + Object colData = getData(dataType, index, sourceRow); |
| 99 | + String[] colName = checkTableforselectCols.split(","); |
| 100 | + result = result + " - " + colName[index] + " : " + colData; |
| 101 | + } |
| 102 | + logger.error("ThreadID: " + Thread.currentThread().getId() + result + " - " + filterColName + " length: " + rowColcnt); |
| 103 | + continue; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + } else { |
| 109 | + BatchStatement batchStatement = BatchStatement.newInstance(BatchType.UNLOGGED); |
| 110 | + for (Row sourceRow : resultSet) { |
| 111 | + readLimiter.acquire(1); |
| 112 | + writeLimiter.acquire(1); |
| 113 | + |
| 114 | + if(checkTableforColSize) { |
| 115 | + int rowColcnt = GetRowColumnLength(sourceRow, filterColType, filterColIndex); |
| 116 | + String result = ""; |
| 117 | + if (rowColcnt > 1024 * 1024 * 10) { |
| 118 | + for (int index = 0; index < checkTableforColSizeTypes.size(); index++) { |
| 119 | + MigrateDataType dataType = checkTableforColSizeTypes.get(index); |
| 120 | + Object colData = getData(dataType, index, sourceRow); |
| 121 | + String[] colName = checkTableforselectCols.split(","); |
| 122 | + result = result + " - " + colName[index] + " : " + colData; |
| 123 | + } |
| 124 | + logger.error("ThreadID: " + Thread.currentThread().getId() + result + " - " + filterColName + " length: " + rowColcnt); |
| 125 | + continue; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if (readCounter.incrementAndGet() % 1000 == 0) { |
| 130 | + logger.info("TreadID: " + Thread.currentThread().getId() + " Read Record Count: " + readCounter.get()); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + logger.info("TreadID: " + Thread.currentThread().getId() + " Final Read Record Count: " + readCounter.get()); |
| 138 | + retryCount = maxAttempts; |
| 139 | + } catch (Exception e) { |
| 140 | + logger.error("Error occurred retry#: " + retryCount, e); |
| 141 | + logger.error("Error with PartitionRange -- TreadID: " + Thread.currentThread().getId() + " Processing min: " + min + " max:" + max + " -- Retry# " + retryCount); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + private int GetRowColumnLength(Row sourceRow, String filterColType, Integer filterColIndex) { |
| 148 | + int i = 0; |
| 149 | + Object colData = getData(new MigrateDataType(filterColType), filterColIndex, sourceRow); |
| 150 | + byte[] colBytes = SerializationUtils.serialize((Serializable) colData); |
| 151 | + i = colBytes.length; |
| 152 | + if (i > 1024*1024*10) |
| 153 | + return i; |
| 154 | + return i; |
| 155 | + } |
| 156 | + |
| 157 | +} |
0 commit comments