@@ -76,16 +76,17 @@ public void getDataAndInsert(BigInteger min, BigInteger max) {
76
76
for (int retryCount = 1 ; retryCount <= maxAttempts ; retryCount ++) {
77
77
78
78
try {
79
- ResultSet resultSet = sourceSession .execute (sourceSelectStatement .bind (hasRandomPartitioner ? min : min .longValueExact (), hasRandomPartitioner ? max : max .longValueExact ()));
79
+ ResultSet resultSet = sourceSession .execute (sourceSelectStatement .bind (hasRandomPartitioner ? min : min .longValueExact (), hasRandomPartitioner ? max : max .longValueExact ()));
80
80
Collection <CompletionStage <AsyncResultSet >> writeResults = new ArrayList <CompletionStage <AsyncResultSet >>();
81
81
82
82
// cannot do batching if the writeFilter is greater than 0 or
83
83
// maxWriteTimeStampFilter is less than max long
84
- if (batchSize == 1 || writeTimeStampFilter ) {
84
+ // do not batch for counters as it adds latency & increases chance of discrepancy
85
+ if (batchSize == 1 || writeTimeStampFilter || isCounterTable ) {
85
86
for (Row sourceRow : resultSet ) {
86
87
readLimiter .acquire (1 );
87
88
88
- if (writeTimeStampFilter ) {
89
+ if (writeTimeStampFilter ) {
89
90
// only process rows greater than writeTimeStampFilter
90
91
Long sourceWriteTimeStamp = getLargestWriteTimeStamp (sourceRow );
91
92
if (sourceWriteTimeStamp < minWriteTimeStampFilter
@@ -99,22 +100,16 @@ public void getDataAndInsert(BigInteger min, BigInteger max) {
99
100
logger .info ("TreadID: " + Thread .currentThread ().getId () + " Read Record Count: "
100
101
+ readCounter .get ());
101
102
}
102
- if (minWriteTimeStampFilter > 0l ) {
103
- Row astraRow = null ;
104
- if (isCounterTable ) {
105
- ResultSet astraReadResultSet = astraSession
106
- .execute (selectFromAstra (astraSelectStatement , sourceRow ));
107
- astraRow = astraReadResultSet .one ();
108
- }
109
-
110
- CompletionStage <AsyncResultSet > astraWriteResultSet = astraSession
111
- .executeAsync (bindInsert (astraInsertStatement , sourceRow , astraRow ));
112
- writeResults .add (astraWriteResultSet );
113
- } else {
114
- CompletionStage <AsyncResultSet > astraWriteResultSet = astraSession
115
- .executeAsync (bindInsert (astraInsertStatement , sourceRow ));
116
- writeResults .add (astraWriteResultSet );
103
+ Row astraRow = null ;
104
+ if (isCounterTable ) {
105
+ ResultSet astraReadResultSet = astraSession
106
+ .execute (selectFromAstra (astraSelectStatement , sourceRow ));
107
+ astraRow = astraReadResultSet .one ();
117
108
}
109
+
110
+ CompletionStage <AsyncResultSet > astraWriteResultSet = astraSession
111
+ .executeAsync (bindInsert (astraInsertStatement , sourceRow , astraRow ));
112
+ writeResults .add (astraWriteResultSet );
118
113
if (writeResults .size () > 1000 ) {
119
114
iterateAndClearWriteResults (writeResults , 1 );
120
115
}
@@ -124,13 +119,13 @@ public void getDataAndInsert(BigInteger min, BigInteger max) {
124
119
iterateAndClearWriteResults (writeResults , 1 );
125
120
} else {
126
121
BatchStatement batchStatement = BatchStatement .newInstance (BatchType .UNLOGGED );
127
- for (Row row : resultSet ) {
122
+ for (Row sourceRow : resultSet ) {
128
123
readLimiter .acquire (1 );
129
124
writeLimiter .acquire (1 );
130
125
if (readCounter .incrementAndGet () % 1000 == 0 ) {
131
126
logger .info ("TreadID: " + Thread .currentThread ().getId () + " Read Record Count: " + readCounter .get ());
132
127
}
133
- batchStatement = batchStatement .add (bindInsert (astraInsertStatement , row ));
128
+ batchStatement = batchStatement .add (bindInsert (astraInsertStatement , sourceRow , null ));
134
129
135
130
// if batch threshold is met, send the writes and clear the batch
136
131
if (batchStatement .size () >= batchSize ) {
@@ -166,11 +161,9 @@ public void getDataAndInsert(BigInteger min, BigInteger max) {
166
161
}
167
162
}
168
163
169
-
170
-
171
164
}
172
165
173
- private void iterateAndClearWriteResults (Collection <CompletionStage <AsyncResultSet >> writeResults , int incrementBy ) throws Exception {
166
+ private void iterateAndClearWriteResults (Collection <CompletionStage <AsyncResultSet >> writeResults , int incrementBy ) throws Exception {
174
167
for (CompletionStage <AsyncResultSet > writeResult : writeResults ) {
175
168
//wait for the writes to complete for the batch. The Retry policy, if defined, should retry the write on timeouts.
176
169
writeResult .toCompletableFuture ().get ().one ();
@@ -181,41 +174,34 @@ private void iterateAndClearWriteResults(Collection<CompletionStage<AsyncResultS
181
174
writeResults .clear ();
182
175
}
183
176
184
- public BoundStatement bindInsert (PreparedStatement insertStatement , Row sourceRow ) {
185
- return bindInsert (insertStatement , sourceRow , null );
186
- }
187
-
188
177
public BoundStatement bindInsert (PreparedStatement insertStatement , Row sourceRow , Row astraRow ) {
189
- if ( isCounterTable ) {
178
+ BoundStatement boundInsertStatement = insertStatement . bind ();
190
179
191
- BoundStatement boundInsertStatement = insertStatement . bind ();
180
+ if ( isCounterTable ) {
192
181
for (int index = 0 ; index < insertColTypes .size (); index ++) {
193
182
MigrateDataType dataType = insertColTypes .get (index );
194
183
// compute the counter delta if reading from astra for the difference
195
184
if (astraRow != null && isCounterTable && index <= counterDeltaMaxIndex ) {
196
- boundInsertStatement = boundInsertStatement .set (index ,getCounterDelta (sourceRow .getLong (updateSelectMapping .get (index )), astraRow .getLong (updateSelectMapping .get (index ))),Long .class );
185
+ boundInsertStatement = boundInsertStatement .set (index , getCounterDelta (sourceRow .getLong (updateSelectMapping .get (index )), astraRow .getLong (updateSelectMapping .get (index ))), Long .class );
197
186
} else {
198
187
boundInsertStatement = boundInsertStatement .set (index , getData (dataType , updateSelectMapping .get (index ), sourceRow ), dataType .typeClass );
199
188
}
200
189
}
201
-
202
- return boundInsertStatement ;
203
-
204
190
} else {
205
- BoundStatement boundInsertStatement = insertStatement .bind ();
206
191
int index = 0 ;
207
192
for (index = 0 ; index < insertColTypes .size (); index ++) {
208
- MigrateDataType dataType = insertColTypes .get (index );
193
+ MigrateDataType dataTypeObj = insertColTypes .get (index );
194
+ Class dataType = dataTypeObj .typeClass ;
209
195
210
196
try {
211
- Object colData = getData (dataType , index , sourceRow );
212
- if (index < idColTypes .size () && colData == null && dataType . typeClass == String .class ){
213
- colData = "" ;
197
+ Object colData = getData (dataTypeObj , index , sourceRow );
198
+ if (index < idColTypes .size () && colData == null && dataType == String .class ) {
199
+ colData = "" ;
214
200
}
215
- boundInsertStatement = boundInsertStatement .set (index , colData , dataType . typeClass );
201
+ boundInsertStatement = boundInsertStatement .set (index , colData , dataType );
216
202
} catch (NullPointerException e ) {
217
203
// ignore the exception for map values being null
218
- if (dataType . typeClass != Map .class ) {
204
+ if (dataType != Map .class ) {
219
205
throw e ;
220
206
}
221
207
}
@@ -226,9 +212,9 @@ public BoundStatement bindInsert(PreparedStatement insertStatement, Row sourceRo
226
212
index ++;
227
213
boundInsertStatement = boundInsertStatement .set (index , getLargestWriteTimeStamp (sourceRow ), Long .class );
228
214
}
229
- return boundInsertStatement ;
230
-
231
215
}
216
+
217
+ return boundInsertStatement ;
232
218
}
233
219
234
220
public Long getCounterDelta (Long sourceRow , Long astraRow ) {
0 commit comments