Improve stability of Cassandra API calls (commands are checked)
[proteocache.git] / datadb / compbio / cassandra / CassandraRemover.java
1 package compbio.cassandra;
2
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.List;
8
9 import com.datastax.driver.core.ResultSet;
10 import com.datastax.driver.core.Row;
11 import compbio.cassandra.readers.CassandraReader;
12
13 /**
14  * The class removes jobs from the cassandra database. 4 different strategies
15  * are possiable: 1. remove 1 job with given job ID 2. remove jobs launched from
16  * an IP 3. remove jobs with particular protein sequence 4. remove jobs launched
17  * within a time range (date1, data2)
18  * 
19  * @author Alexander Sherstnev
20  * @author Natasha Sherstneva
21  * @version 1.0
22  * @since Nov 2013
23  */
24 public class CassandraRemover extends CassandraReader {
25         static SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
26
27         /**
28          * private method for real deleting one job
29          * 
30          * @param jobid
31          *            job ID
32          * @param date
33          *            job execution date
34          * 
35          * @return nothing
36          */
37         private int RemoveJob(String jobid, long date) {
38
39                 if (date < 0L) {
40                         log.error("CassandraRemover error: job " + jobid + " with date " + date
41                                         + " can not be deleted in JobDateInfo. Daily statistics is inconsistent");
42                         return 0;
43                 }
44
45                 String status = FindStatus(jobid);
46                 String com1 = "DELETE FROM ProteinLog WHERE JobID = '" + jobid + "';";
47                 System.out.println("Command: " + com1);
48                 CassandraQuery(com1);
49
50                 String com2 = "UPDATE jpredarchive SET finalstatus = 'DELETED'  WHERE JobID = '" + jobid + "' ;";
51                 System.out.println("Command: " + com2);
52                 CassandraQuery(com2);
53
54                 String com3 = "SELECT * FROM JobDateInfo WHERE jobday = " + date + ";";
55                 System.out.println("Command: " + com3);
56                 ResultSet results = CassandraQuery(com3);
57                 if (results.isExhausted()) {
58                         log.error("CassandraRemover error: job " + jobid + " with date " + date
59                                         + " can not be deleted in JobDateInfo. Daily statistics is inconsistent");
60                         return 0;
61                 }
62                 Row row = results.one();
63                 long njobs = row.getLong("Total") - 1;
64                 if (status.equals("OK")) {
65                         long njobsOK = row.getLong("TotalOK") - 1;
66                         String com4 = "DELETE FROM ProteinRow WHERE JobID = '" + jobid + "';";
67                         System.out.println("Command: " + com4);
68                         CassandraQuery(com4);
69
70                         String com5 = "DELETE FROM ProteinData WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
71                         System.out.println("Command: " + com5);
72                         CassandraQuery(com5);
73                         UpdateJobDateInfo(date, "TotalOK", njobsOK, njobs);
74                 } else {
75                         String com6 = "DELETE FROM FailLog WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
76                         System.out.println("Command: " + com6);
77                         CassandraQuery(com6);
78                         if (status.equals("STOPPED")) {
79                                 long njobsStopped = row.getLong("TotalStopped") - 1;
80                                 UpdateJobDateInfo(date, "TotalStopped", njobsStopped, njobs);
81                         } else if (status.equals("ERROR")) {
82                                 long njobsError = row.getLong("TotalError") - 1;
83                                 UpdateJobDateInfo(date, "TotalError", njobsError, njobs);
84                         } else if (status.equals("TIMEDOUT")) {
85                                 long njobsTimeOut = row.getLong("TotalTimeOut") - 1;
86                                 UpdateJobDateInfo(date, "TotalTimeOut", njobsTimeOut, njobs);
87                         }
88                 }
89                 System.out.println("Job " + jobid + " removed...");
90                 return 1;
91         }
92
93         /**
94          * update a pariticular column in the JobDateInfo table
95          * 
96          * @param jobid
97          *            job ID
98          * 
99          * @return nothing
100          * 
101          */
102         private void UpdateJobDateInfo(long date, String ColumnName, long totalcol, long total) {
103                 String com = "UPDATE JobDateInfo SET " + ColumnName + " = " + totalcol + ", Total = " + total + " WHERE jobday = " + date + ";";
104                 System.out.println("Command: " + com);
105                 CassandraQuery(com);
106         }
107
108         /**
109          * external method for deleting job with given job ID (strategy 1)
110          * 
111          * @param jobid
112          *            job ID
113          * 
114          * @return a number of deleted jobs
115          * 
116          */
117         public int RemoveJobById(String jobid) {
118                 if (jobid == null)
119                         return 0;
120                 long date = FindJobDate(jobid);
121                 return RemoveJob(jobid, date);
122         }
123
124         /**
125          * external method for deleting jobs within a time range (strategy 4)
126          * 
127          * @param date1
128          *            starting date
129          * 
130          * @param date2
131          *            ending date
132          * 
133          * @return a number of deleted jobs
134          * 
135          */
136         public int RemoveJobByDate(String date1, String date2) {
137                 if (date1 == null || date2 == null)
138                         return 0;
139
140                 int njobs = 0;
141                 Long dateBegin = convertDate(date1);
142                 Long dateEnd = convertDate(date2);
143                 Calendar start = Calendar.getInstance();
144                 start.setTime(new Date(dateBegin));
145                 Calendar end = Calendar.getInstance();
146                 end.setTime(new Date(dateEnd));
147
148                 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
149                         String com1 = "SELECT JobID FROM ProteinData WHERE jobtime = " + date.getTime() + ";";
150                         System.out.println("Command: " + com1);
151                         ResultSet results = CassandraQuery(com1);
152                         if (!results.isExhausted()) {
153                                 List<Row> rows = results.all();
154                                 for (Row r : rows) {
155                                         String jobid = r.getString("JobID");
156                                         if (jobid != null) {
157                                                 njobs += RemoveJob(jobid, date.getTime());
158                                         }
159                                 }
160                         }
161
162                         String com2 = "SELECT JobID FROM FailLog WHERE jobtime = " + date.getTime() + ";";
163                         ResultSet resultsfail = CassandraQuery(com2);
164                         if (!resultsfail.isExhausted()) {
165                                 List<Row> rows = resultsfail.all();
166                                 for (Row r : rows) {
167                                         String jobid = r.getString("JobID");
168                                         if (jobid != null) {
169                                                 njobs += RemoveJob(jobid, date.getTime());
170                                         }
171                                 }
172                         }
173                 }
174                 return njobs;
175         }
176
177         /**
178          * external method for deleting jobs launched from a particular IP (strategy
179          * 2)
180          * 
181          * @param ip
182          *            the IP
183          * 
184          * @return a number of deleted jobs
185          * 
186          */
187         public int RemoveJobByIp(String ip) {
188                 int njobs = 0;
189                 if (ip == null)
190                         return 0;
191                 String com = "SELECT databegin, JobID FROM ProteinLog WHERE ip = '" + ip + "';";
192                 ResultSet results = CassandraQuery(com);
193                 if (!results.isExhausted()) {
194                         List<Row> rows = results.all();
195                         for (Row r : rows) {
196                                 Long date = convertDate(r.getString("databegin"));
197                                 String jobid = r.getString("JobID");
198                                 if (date != null || jobid != null) {
199                                         njobs += RemoveJob(jobid, date);
200                                 }
201                         }
202                 }
203                 return njobs;
204         }
205
206         /**
207          * external method for deleting jobs with a protein sequence (strategy 3)
208          * 
209          * @param sequence
210          *            the sequence
211          * 
212          * @return a number of deleted jobs
213          * 
214          */
215         public int RemoveJobBySequence(String sequence) {
216                 int njobs = 0;
217                 if (sequence == null)
218                         return 0;
219                 String com = "SELECT JobID FROM ProteinRow WHERE Protein = '" + sequence + "';";
220                 ResultSet results = CassandraQuery(com);
221                 if (!results.isExhausted()) {
222                         List<Row> rows = results.all();
223                         for (Row r : rows) {
224                                 String jobid = r.getString("JobID");
225                                 long date = FindJobDate(jobid);
226                                 njobs += RemoveJob(jobid, date);
227                         }
228                 }
229                 return njobs;
230         }
231
232         private long FindJobDate(String jobid) {
233                 String com = "SELECT databegin FROM ProteinLog WHERE JobID = '" + jobid + "';";
234                 ResultSet results = CassandraQuery(com);
235                 if (!results.isExhausted()) {
236                         return convertDate(results.one().getString("databegin"));
237                 }
238                 return -1L;
239         }
240
241         private String FindStatus(String jobid) {
242                 String com = "SELECT FinalStatus FROM ProteinLog WHERE JobID = '" + jobid + "';";
243                 System.out.println("Command: " + com);
244                 ResultSet results = CassandraQuery(com);
245                 if (!results.isExhausted()) {
246                         return results.one().getString("FinalStatus");
247                 }
248                 return "UNKNOWN";
249         }
250
251         protected long convertDate(String d) {
252                 try {
253                         if (null != d) {
254                                 Date startdate = dateformatter.parse(d);
255                                 return startdate.getTime();
256                         }
257                 } catch (ParseException e) {
258                         e.printStackTrace();
259                 }
260                 return 0L;
261         }
262
263 }