20
20
import java .util .ArrayList ;
21
21
import java .util .Collection ;
22
22
23
+ import org .slf4j .Logger ;
24
+ import org .slf4j .LoggerFactory ;
25
+
23
26
import com .datastax .cdm .feature .TrackRun ;
24
27
import com .datastax .cdm .feature .TrackRun .RUN_TYPE ;
28
+ import com .datastax .cdm .job .RunNotStartedException ;
25
29
import com .datastax .cdm .job .SplitPartitions ;
26
30
import com .datastax .cdm .job .SplitPartitions .Partition ;
27
31
import com .datastax .oss .driver .api .core .CqlSession ;
28
32
import com .datastax .oss .driver .api .core .cql .BoundStatement ;
29
33
import com .datastax .oss .driver .api .core .cql .ResultSet ;
34
+ import com .datastax .oss .driver .api .core .cql .Row ;
30
35
31
36
public class TargetUpsertRunDetailsStatement {
32
37
private CqlSession session ;
@@ -36,45 +41,71 @@ public class TargetUpsertRunDetailsStatement {
36
41
private long prevRunId ;
37
42
private BoundStatement boundInitInfoStatement ;
38
43
private BoundStatement boundInitStatement ;
39
- private BoundStatement boundUpdateInfoStatement ;
44
+ private BoundStatement boundEndInfoStatement ;
40
45
private BoundStatement boundUpdateStatement ;
41
46
private BoundStatement boundUpdateStartStatement ;
47
+ private BoundStatement boundSelectInfoStatement ;
42
48
private BoundStatement boundSelectStatement ;
43
49
50
+ public Logger logger = LoggerFactory .getLogger (this .getClass ().getName ());
51
+
44
52
public TargetUpsertRunDetailsStatement (CqlSession session , String keyspaceTable ) {
45
53
this .session = session ;
46
54
String [] ksTab = keyspaceTable .split ("\\ ." );
55
+ if (ksTab .length != 2 ) {
56
+ throw new RuntimeException ("Invalid keyspace.table format: " + keyspaceTable );
57
+ }
47
58
this .keyspaceName = ksTab [0 ];
48
59
this .tableName = ksTab [1 ];
49
60
String cdmKsTabInfo = this .keyspaceName + ".cdm_run_info" ;
50
61
String cdmKsTabDetails = this .keyspaceName + ".cdm_run_details" ;
51
62
52
63
this .session .execute ("create table if not exists " + cdmKsTabInfo
53
- + " (table_name text, run_id bigint, run_type text, prev_run_id bigint, start_time timestamp, end_time timestamp, run_info text, primary key (table_name, run_id))" );
54
- this .session .execute ("create table if not exists " + cdmKsTabDetails
55
- + " (table_name text, run_id bigint, start_time timestamp, token_min bigint, token_max bigint, status text, primary key ((table_name, run_id), token_min))" );
64
+ + " (table_name text, run_id bigint, run_type text, prev_run_id bigint, start_time timestamp, end_time timestamp, run_info text, status text, primary key (table_name, run_id))" );
65
+
66
+ // TODO: Remove this code block after a few releases, its only added for backward compatibility
67
+ try {
68
+ this .session .execute ("alter table " + cdmKsTabInfo + " add status text" );
69
+ } catch (Exception e ) {
70
+ // ignore if column already exists
71
+ logger .trace ("Column 'status' already exists in table {}" , cdmKsTabInfo );
72
+ }
56
73
57
74
boundInitInfoStatement = bindStatement ("INSERT INTO " + cdmKsTabInfo
58
- + " (table_name, run_id, run_type, prev_run_id, start_time) VALUES (?, ?, ?, ?, dateof(now()))" );
75
+ + " (table_name, run_id, run_type, prev_run_id, start_time, status ) VALUES (?, ?, ?, ?, dateof(now()), ? )" );
59
76
boundInitStatement = bindStatement ("INSERT INTO " + cdmKsTabDetails
60
77
+ " (table_name, run_id, token_min, token_max, status) VALUES (?, ?, ?, ?, ?)" );
61
- boundUpdateInfoStatement = bindStatement ("UPDATE " + cdmKsTabInfo
62
- + " SET end_time = dateof(now()), run_info = ? WHERE table_name = ? AND run_id = ?" );
78
+ boundEndInfoStatement = bindStatement ("UPDATE " + cdmKsTabInfo
79
+ + " SET end_time = dateof(now()), run_info = ?, status = ? WHERE table_name = ? AND run_id = ?" );
63
80
boundUpdateStatement = bindStatement (
64
81
"UPDATE " + cdmKsTabDetails + " SET status = ? WHERE table_name = ? AND run_id = ? AND token_min = ?" );
65
82
boundUpdateStartStatement = bindStatement ("UPDATE " + cdmKsTabDetails
66
83
+ " SET start_time = dateof(now()), status = ? WHERE table_name = ? AND run_id = ? AND token_min = ?" );
84
+ boundSelectInfoStatement = bindStatement (
85
+ "SELECT status FROM " + cdmKsTabInfo + " WHERE table_name = ? AND run_id = ?" );
67
86
boundSelectStatement = bindStatement ("SELECT token_min, token_max FROM " + cdmKsTabDetails
68
87
+ " WHERE table_name = ? AND run_id = ? and status in ('NOT_STARTED', 'STARTED', 'FAIL', 'DIFF') ALLOW FILTERING" );
69
88
}
70
89
71
- public Collection <SplitPartitions .Partition > getPendingPartitions (long prevRunId ) {
90
+ public Collection <SplitPartitions .Partition > getPendingPartitions (long prevRunId ) throws RunNotStartedException {
72
91
this .prevRunId = prevRunId ;
92
+ final Collection <SplitPartitions .Partition > pendingParts = new ArrayList <SplitPartitions .Partition >();
73
93
if (prevRunId == 0 ) {
74
- return new ArrayList <SplitPartitions .Partition >();
94
+ return pendingParts ;
95
+ }
96
+
97
+ ResultSet rsInfo = session
98
+ .execute (boundSelectInfoStatement .setString ("table_name" , tableName ).setLong ("run_id" , prevRunId ));
99
+ Row cdmRunStatus = rsInfo .one ();
100
+ if (cdmRunStatus == null ) {
101
+ return pendingParts ;
102
+ } else {
103
+ String status = cdmRunStatus .getString ("status" );
104
+ if (TrackRun .RUN_STATUS .NOT_STARTED .toString ().equals (status )) {
105
+ throw new RunNotStartedException ("Run not started for run_id: " + prevRunId );
106
+ }
75
107
}
76
108
77
- final Collection <SplitPartitions .Partition > pendingParts = new ArrayList <SplitPartitions .Partition >();
78
109
ResultSet rs = session
79
110
.execute (boundSelectStatement .setString ("table_name" , tableName ).setLong ("run_id" , prevRunId ));
80
111
rs .forEach (row -> {
@@ -89,8 +120,12 @@ public Collection<SplitPartitions.Partition> getPendingPartitions(long prevRunId
89
120
public long initCdmRun (Collection <SplitPartitions .Partition > parts , RUN_TYPE runType ) {
90
121
runId = System .currentTimeMillis ();
91
122
session .execute (boundInitInfoStatement .setString ("table_name" , tableName ).setLong ("run_id" , runId )
92
- .setString ("run_type" , runType .toString ()).setLong ("prev_run_id" , prevRunId ));
123
+ .setString ("run_type" , runType .toString ()).setLong ("prev_run_id" , prevRunId )
124
+ .setString ("status" , TrackRun .RUN_STATUS .NOT_STARTED .toString ()));
93
125
parts .forEach (part -> initCdmRun (part ));
126
+ session .execute (boundInitInfoStatement .setString ("table_name" , tableName ).setLong ("run_id" , runId )
127
+ .setString ("run_type" , runType .toString ()).setLong ("prev_run_id" , prevRunId )
128
+ .setString ("status" , TrackRun .RUN_STATUS .STARTED .toString ()));
94
129
return runId ;
95
130
}
96
131
@@ -101,9 +136,9 @@ private void initCdmRun(Partition partition) {
101
136
.setString ("status" , TrackRun .RUN_STATUS .NOT_STARTED .toString ()));
102
137
}
103
138
104
- public void updateCdmRunInfo (String runInfo ) {
105
- session .execute (boundUpdateInfoStatement .setString ("table_name" , tableName ).setLong ("run_id" , runId )
106
- .setString ("run_info" , runInfo ));
139
+ public void endCdmRun (String runInfo ) {
140
+ session .execute (boundEndInfoStatement .setString ("table_name" , tableName ).setLong ("run_id" , runId )
141
+ .setString ("run_info" , runInfo ). setString ( "status" , TrackRun . RUN_STATUS . ENDED . toString ()) );
107
142
}
108
143
109
144
public void updateCdmRun (BigInteger min , TrackRun .RUN_STATUS status ) {
0 commit comments