17
17
import java .util .ArrayList ;
18
18
import java .util .Collection ;
19
19
import java .util .concurrent .CompletionStage ;
20
- import java .util .concurrent .atomic .AtomicLong ;
21
20
22
21
public class CopyJobSession extends AbstractJobSession <SplitPartitions .Partition > {
23
22
24
- private static CopyJobSession copyJobSession ;
25
23
private final PKFactory pkFactory ;
26
24
private final boolean isCounterTable ;
27
25
private final Integer fetchSize ;
28
26
private final Integer batchSize ;
29
27
public Logger logger = LoggerFactory .getLogger (this .getClass ().getName ());
30
- protected AtomicLong readCounter = new AtomicLong (0 );
31
- protected AtomicLong skippedCounter = new AtomicLong (0 );
32
- protected AtomicLong writeCounter = new AtomicLong (0 );
33
- protected AtomicLong errorCounter = new AtomicLong (0 );
34
28
private TargetUpsertStatement targetUpsertStatement ;
35
29
private TargetSelectByPKStatement targetSelectByPKStatement ;
36
30
37
31
protected CopyJobSession (CqlSession originSession , CqlSession targetSession , SparkConf sc ) {
38
32
super (originSession , targetSession , sc );
33
+ this .jobCounter .setRegisteredTypes (JobCounter .CounterType .READ , JobCounter .CounterType .WRITE , JobCounter .CounterType .SKIPPED , JobCounter .CounterType .ERROR , JobCounter .CounterType .UNFLUSHED );
39
34
40
35
pkFactory = this .originSession .getPKFactory ();
41
36
isCounterTable = this .originSession .getCqlTable ().isCounterTable ();
@@ -60,11 +55,8 @@ public void getDataAndInsert(BigInteger min, BigInteger max) {
60
55
int maxAttempts = maxRetries + 1 ;
61
56
String guardrailCheck ;
62
57
for (int attempts = 1 ; attempts <= maxAttempts && !done ; attempts ++) {
63
- long readCnt = 0 ;
64
- long flushedWriteCnt = 0 ;
65
- long skipCnt = 0 ;
66
- long errCnt = 0 ;
67
- long unflushedWrites = 0 ;
58
+ jobCounter .threadReset ();
59
+
68
60
try {
69
61
OriginSelectByPartitionRangeStatement originSelectByPartitionRangeStatement = this .originSession .getOriginSelectByPartitionRangeStatement ();
70
62
targetUpsertStatement = this .targetSession .getTargetUpsertStatement ();
@@ -74,14 +66,11 @@ public void getDataAndInsert(BigInteger min, BigInteger max) {
74
66
75
67
for (Row originRow : resultSet ) {
76
68
rateLimiterOrigin .acquire (1 );
77
- readCnt ++;
78
- if (readCnt % printStatsAfter == 0 ) {
79
- printCounts (false );
80
- }
69
+ jobCounter .threadIncrement (JobCounter .CounterType .READ );
81
70
82
71
Record record = new Record (pkFactory .getTargetPK (originRow ), originRow , null );
83
72
if (originSelectByPartitionRangeStatement .shouldFilterRecord (record )) {
84
- skipCnt ++ ;
73
+ jobCounter . threadIncrement ( JobCounter . CounterType . SKIPPED ) ;
85
74
continue ;
86
75
}
87
76
@@ -90,66 +79,48 @@ public void getDataAndInsert(BigInteger min, BigInteger max) {
90
79
guardrailCheck = guardrailFeature .guardrailChecks (r );
91
80
if (guardrailCheck != null && guardrailCheck != Guardrail .CLEAN_CHECK ) {
92
81
logger .error ("Guardrails failed for PrimaryKey {}; {}" , r .getPk (), guardrailCheck );
93
- skipCnt ++ ;
82
+ jobCounter . threadIncrement ( JobCounter . CounterType . SKIPPED ) ;
94
83
continue ;
95
84
}
96
85
}
97
86
98
87
BoundStatement boundUpsert = bind (r );
99
88
if (null == boundUpsert ) {
100
- skipCnt ++ ; // TODO: this previously skipped, why not errCnt?
89
+ jobCounter . threadIncrement ( JobCounter . CounterType . SKIPPED ) ; // TODO: this previously skipped, why not errCnt?
101
90
continue ;
102
91
}
103
92
104
93
rateLimiterTarget .acquire (1 );
105
94
batch = writeAsync (batch , writeResults , boundUpsert );
106
- unflushedWrites ++ ;
95
+ jobCounter . threadIncrement ( JobCounter . CounterType . UNFLUSHED ) ;
107
96
108
- if (unflushedWrites > fetchSize ) {
97
+ if (jobCounter . getCount ( JobCounter . CounterType . UNFLUSHED ) > fetchSize ) {
109
98
flushAndClearWrites (batch , writeResults );
110
- flushedWriteCnt += unflushedWrites ;
111
- unflushedWrites = 0 ;
99
+ jobCounter . threadIncrement ( JobCounter . CounterType . WRITE , jobCounter . getCount ( JobCounter . CounterType . UNFLUSHED )) ;
100
+ jobCounter . threadReset ( JobCounter . CounterType . UNFLUSHED ) ;
112
101
}
113
102
}
114
103
}
115
104
116
105
flushAndClearWrites (batch , writeResults );
117
- flushedWriteCnt += unflushedWrites ;
118
-
119
- readCounter .addAndGet (readCnt );
120
- writeCounter .addAndGet (flushedWriteCnt );
121
- skippedCounter .addAndGet (skipCnt );
106
+ jobCounter .threadIncrement (JobCounter .CounterType .WRITE , jobCounter .getCount (JobCounter .CounterType .UNFLUSHED ));
107
+ jobCounter .threadReset (JobCounter .CounterType .UNFLUSHED );
122
108
done = true ;
123
109
124
110
} catch (Exception e ) {
125
111
if (attempts == maxAttempts ) {
126
- readCounter .addAndGet (readCnt );
127
- writeCounter .addAndGet (flushedWriteCnt );
128
- skippedCounter .addAndGet (skipCnt );
129
- errorCounter .addAndGet (readCnt - flushedWriteCnt - skipCnt );
112
+ jobCounter .threadIncrement (JobCounter .CounterType .ERROR , jobCounter .getCount (JobCounter .CounterType .READ ) - jobCounter .getCount (JobCounter .CounterType .WRITE ) - jobCounter .getCount (JobCounter .CounterType .SKIPPED ));
130
113
logFailedPartitionsInFile (partitionFile , min , max );
131
114
}
132
115
logger .error ("Error occurred during Attempt#: {}" , attempts , e );
133
116
logger .error ("Error with PartitionRange -- ThreadID: {} Processing min: {} max: {} -- Attempt# {}" ,
134
117
Thread .currentThread ().getId (), min , max , attempts );
135
- logger .error ("Error stats Read#: {}, Wrote#: {}, Skipped#: {}, Error#: {}" , readCnt , flushedWriteCnt , skipCnt , (readCnt - flushedWriteCnt - skipCnt ));
118
+ logger .error ("Error stats " + jobCounter .getThreadCounters (false ));
119
+ }
120
+ finally {
121
+ jobCounter .globalIncrement ();
122
+ printCounts (false );
136
123
}
137
- }
138
- }
139
-
140
- @ Override
141
- public synchronized void printCounts (boolean isFinal ) {
142
- String msg = "ThreadID: " + Thread .currentThread ().getId ();
143
- if (isFinal ) {
144
- msg += " Final" ;
145
- logger .info ("################################################################################################" );
146
- }
147
- logger .info ("{} Read Record Count: {}" , msg , readCounter .get ());
148
- logger .info ("{} Skipped Record Count: {}" , msg , skippedCounter .get ());
149
- logger .info ("{} Write Record Count: {}" , msg , writeCounter .get ());
150
- logger .info ("{} Error Record Count: {}" , msg , errorCounter .get ());
151
- if (isFinal ) {
152
- logger .info ("################################################################################################" );
153
124
}
154
125
}
155
126
0 commit comments