Revision 2608
Added by Jing Tao over 18 years ago
ReplicationHandler.java | ||
---|---|---|
62 | 62 |
//PrintWriter out; |
63 | 63 |
private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter; |
64 | 64 |
|
65 |
|
|
66 |
|
|
65 | 67 |
public ReplicationHandler() |
66 | 68 |
{ |
67 | 69 |
//this.out = o; |
... | ... | |
129 | 131 |
String update; |
130 | 132 |
ReplMessageHandler message = new ReplMessageHandler(); |
131 | 133 |
Vector responses = new Vector(); |
132 |
boolean flag=false; // If a document need to update |
|
133 |
boolean dataFile=false; |
|
134 |
String action = new String(); |
|
135 | 134 |
XMLReader parser; |
136 | 135 |
URL u; |
137 | 136 |
|
... | ... | |
230 | 229 |
MetaCatUtil.debugMessage("Update vector size: "+ updateList.size(), 40); |
231 | 230 |
MetaCatUtil.debugMessage("Delete vector size: "+ deleteList.size(),40); |
232 | 231 |
// go though every element in updated document vector |
233 |
for(int j=0; j<updateList.size(); j++) |
|
234 |
{ |
|
235 |
//initial dataFile is false |
|
236 |
dataFile=false; |
|
237 |
//w is information for one document, information contain |
|
238 |
//docid, rev, server or datafile. |
|
239 |
Vector w = new Vector((Vector)(updateList.elementAt(j))); |
|
240 |
//Check if the vector w contain "datafile" |
|
241 |
//If it has, this document is data file |
|
242 |
if (w.contains((String)util.getOption("datafileflag"))) |
|
243 |
{ |
|
244 |
dataFile=true; |
|
245 |
} |
|
246 |
//System.out.println("w: " + w.toString()); |
|
247 |
// Get docid |
|
248 |
String docid = (String)w.elementAt(0); |
|
249 |
MetaCatUtil.debugMessage("docid: " + docid, 40); |
|
250 |
// Get revision number |
|
251 |
int rev = Integer.parseInt((String)w.elementAt(1)); |
|
252 |
MetaCatUtil.debugMessage("rev: " + rev, 40); |
|
253 |
// Get remote server name (it is may not be doc homeserver because |
|
254 |
// the new hub feature |
|
255 |
String remoteServer = (String)w.elementAt(2); |
|
256 |
remoteServer = remoteServer.trim(); |
|
257 |
|
|
258 |
|
|
259 |
// compare the update rev and local rev to see what need happen |
|
260 |
int localrev = -1; |
|
261 |
try |
|
262 |
{ |
|
263 |
localrev = DocumentImpl.getLatestRevisionNumber(docid); |
|
264 |
} |
|
265 |
catch (SQLException e) |
|
266 |
{ |
|
267 |
MetaCatUtil.debugMessage("Local rev for docid "+ docid + " could not "+ |
|
268 |
" be found because " + e.getMessage(), 45); |
|
269 |
MetacatReplication.replErrorLog("Docid "+ docid + " could not be "+ |
|
270 |
"written because error happend to find it's local revision"); |
|
271 |
continue; |
|
272 |
} |
|
273 |
MetaCatUtil.debugMessage("Local rev for docid "+ docid + " is "+ |
|
274 |
localrev, 45); |
|
275 |
|
|
276 |
//check the revs for an update because this document is in the |
|
277 |
//local DB, it might be out of date. |
|
278 |
if (localrev == -1) |
|
279 |
{ |
|
280 |
//insert this document as new because it is not in the local DB |
|
281 |
action = "INSERT"; |
|
282 |
flag = true; |
|
283 |
} |
|
284 |
else |
|
285 |
{ |
|
286 |
if(localrev == rev) |
|
287 |
{ |
|
288 |
// Local meatacat has the same rev to remote host, don't need |
|
289 |
// update and flag set false |
|
290 |
flag = false; |
|
291 |
} |
|
292 |
else if(localrev < rev) |
|
293 |
{ |
|
294 |
//this document needs to be updated so send an read request |
|
295 |
action = "UPDATE"; |
|
296 |
flag = true; |
|
297 |
} |
|
298 |
} |
|
299 |
|
|
300 |
// this is non-data file |
|
301 |
if(flag && !dataFile) |
|
302 |
{ |
|
303 |
try |
|
304 |
{ |
|
305 |
handleSingleXMLDocument(remoteServer, action, docid); |
|
306 |
} |
|
307 |
catch(Exception e) |
|
308 |
{ |
|
309 |
// skip this document |
|
310 |
continue; |
|
311 |
} |
|
312 |
}//if for non-data file |
|
313 |
|
|
314 |
// this is for data file |
|
315 |
if(flag && dataFile) |
|
316 |
{ |
|
317 |
try |
|
318 |
{ |
|
319 |
handleSingleDataFile(remoteServer, action, docid); |
|
320 |
} |
|
321 |
catch(Exception e) |
|
322 |
{ |
|
323 |
// skip this datafile |
|
324 |
continue; |
|
325 |
} |
|
326 |
|
|
327 |
}//for datafile |
|
328 |
}//for update docs |
|
329 |
|
|
232 |
handleDocList(updateList, DocumentImpl.DOCUMENTTABLE); |
|
330 | 233 |
//handle deleted docs |
331 | 234 |
for(int k=0; k<deleteList.size(); k++) |
332 | 235 |
{ //delete the deleted documents; |
... | ... | |
341 | 244 |
continue; |
342 | 245 |
} |
343 | 246 |
}//for delete docs |
247 |
|
|
248 |
// handle replicate doc in xml_revision |
|
249 |
Vector revisionList = new Vector(message.getRevisionsVect()); |
|
250 |
handleDocList(revisionList, DocumentImpl.REVISIONTABLE); |
|
344 | 251 |
}//for response |
345 | 252 |
|
346 | 253 |
//updated last_checked |
... | ... | |
362 | 269 |
|
363 | 270 |
/* Handle replicate single xml document*/ |
364 | 271 |
private void handleSingleXMLDocument(String remoteserver, String actions, |
365 |
String docId) |
|
272 |
String docId, String tableName)
|
|
366 | 273 |
throws Exception |
367 | 274 |
{ |
368 | 275 |
DBConnection dbConn = null; |
... | ... | |
438 | 345 |
(String)docinfoHash.get("user_owner"), |
439 | 346 |
null, /* null for groups[] */ |
440 | 347 |
docHomeServer, |
441 |
remoteserver);
|
|
348 |
remoteserver, tableName, true);// true is for time replication
|
|
442 | 349 |
MetaCatUtil.debugMessage("Successfully replicated doc " + accnum, 35); |
443 | 350 |
MetacatReplication.replLog("wrote doc " + accnum + " from " + |
444 | 351 |
remoteserver); |
... | ... | |
463 | 370 |
|
464 | 371 |
/* Handle replicate single xml document*/ |
465 | 372 |
private void handleSingleDataFile(String remoteserver, String actions, |
466 |
String docId) |
|
373 |
String docId, String tableName)
|
|
467 | 374 |
throws Exception |
468 | 375 |
{ |
469 | 376 |
MetaCatUtil.debugMessage("Try to replicate data file: "+docId, 40); |
... | ... | |
515 | 422 |
if ( input != null) |
516 | 423 |
{ |
517 | 424 |
DocumentImpl.writeDataFileInReplication(input, |
518 |
datafilePath, |
|
519 |
docName,docType, |
|
520 |
accnum, user, |
|
521 |
docHomeServer, |
|
522 |
remoteserver); |
|
425 |
datafilePath, |
|
426 |
docName,docType, |
|
427 |
accnum, user, |
|
428 |
docHomeServer, |
|
429 |
remoteserver, |
|
430 |
tableName, |
|
431 |
true);//true means timed replication |
|
523 | 432 |
MetaCatUtil.debugMessage("Successfully to write datafile " + docId, 30); |
524 | 433 |
MetacatReplication.replLog("wrote datafile " + accnum + " from " + |
525 | 434 |
remoteserver); |
... | ... | |
1001 | 910 |
System.out.println("Time is " + s); |
1002 | 911 |
return s; |
1003 | 912 |
} |
913 |
|
|
914 |
|
|
915 |
/* |
|
916 |
* This method will go through the docid list both in xml_Documents table |
|
917 |
* and in xml_revisions table |
|
918 |
* @author tao |
|
919 |
*/ |
|
920 |
private void handleDocList(Vector docList, String tableName) |
|
921 |
{ |
|
922 |
boolean dataFile=false; |
|
923 |
for(int j=0; j<docList.size(); j++) |
|
924 |
{ |
|
925 |
//initial dataFile is false |
|
926 |
dataFile=false; |
|
927 |
//w is information for one document, information contain |
|
928 |
//docid, rev, server or datafile. |
|
929 |
Vector w = new Vector((Vector)(docList.elementAt(j))); |
|
930 |
//Check if the vector w contain "datafile" |
|
931 |
//If it has, this document is data file |
|
932 |
if (w.contains((String)util.getOption("datafileflag"))) |
|
933 |
{ |
|
934 |
dataFile=true; |
|
935 |
} |
|
936 |
//System.out.println("w: " + w.toString()); |
|
937 |
// Get docid |
|
938 |
String docid = (String)w.elementAt(0); |
|
939 |
MetaCatUtil.debugMessage("docid: " + docid, 40); |
|
940 |
// Get revision number |
|
941 |
int rev = Integer.parseInt((String)w.elementAt(1)); |
|
942 |
MetaCatUtil.debugMessage("rev: " + rev, 40); |
|
943 |
// Get remote server name (it is may not be doc homeserver because |
|
944 |
// the new hub feature |
|
945 |
String remoteServer = (String)w.elementAt(2); |
|
946 |
remoteServer = remoteServer.trim(); |
|
947 |
|
|
948 |
try |
|
949 |
{ |
|
950 |
if (tableName.equals(DocumentImpl.DOCUMENTTABLE)) |
|
951 |
{ |
|
952 |
handleDocInXMLDocuments(docid, rev, remoteServer, dataFile); |
|
953 |
} |
|
954 |
else if (tableName.equals(DocumentImpl.REVISIONTABLE)) |
|
955 |
{ |
|
956 |
handleDocInXMLRevisions(docid, rev, remoteServer, dataFile); |
|
957 |
} |
|
958 |
else |
|
959 |
{ |
|
960 |
continue; |
|
961 |
} |
|
962 |
|
|
963 |
} |
|
964 |
catch (Exception e) |
|
965 |
{ |
|
966 |
MetaCatUtil.debugMessage("error to handle update doc in "+ |
|
967 |
tableName +" in time replication"+e.getMessage(), 30); |
|
968 |
continue; |
|
969 |
} |
|
970 |
|
|
971 |
}//for update docs |
|
972 |
|
|
973 |
} |
|
974 |
|
|
975 |
/* |
|
976 |
* This method will handle doc in xml_documents table. |
|
977 |
*/ |
|
978 |
private void handleDocInXMLDocuments(String docid, int rev, String remoteServer, boolean dataFile) |
|
979 |
throws Exception |
|
980 |
{ |
|
981 |
// compare the update rev and local rev to see what need happen |
|
982 |
int localrev = -1; |
|
983 |
String action = null; |
|
984 |
boolean flag = false; |
|
985 |
try |
|
986 |
{ |
|
987 |
localrev = DocumentImpl.getLatestRevisionNumber(docid); |
|
988 |
} |
|
989 |
catch (SQLException e) |
|
990 |
{ |
|
991 |
MetaCatUtil.debugMessage("Local rev for docid "+ docid + " could not "+ |
|
992 |
" be found because " + e.getMessage(), 45); |
|
993 |
MetacatReplication.replErrorLog("Docid "+ docid + " could not be "+ |
|
994 |
"written because error happend to find it's local revision"); |
|
995 |
throw new Exception (e.getMessage()); |
|
996 |
} |
|
997 |
MetaCatUtil.debugMessage("Local rev for docid "+ docid + " is "+ |
|
998 |
localrev, 45); |
|
999 |
|
|
1000 |
//check the revs for an update because this document is in the |
|
1001 |
//local DB, it might be out of date. |
|
1002 |
if (localrev == -1) |
|
1003 |
{ |
|
1004 |
//insert this document as new because it is not in the local DB |
|
1005 |
action = "INSERT"; |
|
1006 |
flag = true; |
|
1007 |
} |
|
1008 |
else |
|
1009 |
{ |
|
1010 |
if(localrev == rev) |
|
1011 |
{ |
|
1012 |
// Local meatacat has the same rev to remote host, don't need |
|
1013 |
// update and flag set false |
|
1014 |
flag = false; |
|
1015 |
} |
|
1016 |
else if(localrev < rev) |
|
1017 |
{ |
|
1018 |
//this document needs to be updated so send an read request |
|
1019 |
action = "UPDATE"; |
|
1020 |
flag = true; |
|
1021 |
} |
|
1022 |
} |
|
1023 |
|
|
1024 |
// this is non-data file |
|
1025 |
if(flag && !dataFile) |
|
1026 |
{ |
|
1027 |
try |
|
1028 |
{ |
|
1029 |
handleSingleXMLDocument(remoteServer, action, docid, DocumentImpl.DOCUMENTTABLE); |
|
1030 |
} |
|
1031 |
catch(Exception e) |
|
1032 |
{ |
|
1033 |
// skip this document |
|
1034 |
throw e; |
|
1035 |
} |
|
1036 |
}//if for non-data file |
|
1037 |
|
|
1038 |
// this is for data file |
|
1039 |
if(flag && dataFile) |
|
1040 |
{ |
|
1041 |
try |
|
1042 |
{ |
|
1043 |
handleSingleDataFile(remoteServer, action, docid, DocumentImpl.DOCUMENTTABLE); |
|
1044 |
} |
|
1045 |
catch(Exception e) |
|
1046 |
{ |
|
1047 |
// skip this datafile |
|
1048 |
throw e; |
|
1049 |
} |
|
1050 |
|
|
1051 |
}//for datafile |
|
1052 |
} |
|
1053 |
|
|
1054 |
/* |
|
1055 |
* This method will handle doc in xml_documents table. |
|
1056 |
*/ |
|
1057 |
private void handleDocInXMLRevisions(String docid, int rev, String remoteServer, boolean dataFile) |
|
1058 |
throws Exception |
|
1059 |
{ |
|
1060 |
// compare the update rev and local rev to see what need happen |
|
1061 |
Vector localrev = null; |
|
1062 |
String action = "INSERT"; |
|
1063 |
boolean flag = false; |
|
1064 |
try |
|
1065 |
{ |
|
1066 |
localrev = DBUtil.getRevListFromRevisionTable(docid); |
|
1067 |
} |
|
1068 |
catch (SQLException e) |
|
1069 |
{ |
|
1070 |
MetaCatUtil.debugMessage("Local rev for docid "+ docid + " could not "+ |
|
1071 |
" be found because " + e.getMessage(), 45); |
|
1072 |
MetacatReplication.replErrorLog("Docid "+ docid + " could not be "+ |
|
1073 |
"written because error happend to find it's local revision"); |
|
1074 |
throw new Exception (e.getMessage()); |
|
1075 |
} |
|
1076 |
MetaCatUtil.debugMessage("rev list in xml_revision table for docid "+ docid + " is "+ |
|
1077 |
localrev.toString(), 45); |
|
1078 |
|
|
1079 |
// if the rev is not in the xml_revision, we need insert it |
|
1080 |
if (!localrev.contains(new Integer(rev))) |
|
1081 |
{ |
|
1082 |
flag = true; |
|
1083 |
} |
|
1084 |
|
|
1085 |
// this is non-data file |
|
1086 |
if(flag && !dataFile) |
|
1087 |
{ |
|
1088 |
try |
|
1089 |
{ |
|
1090 |
handleSingleXMLDocument(remoteServer, action, docid, DocumentImpl.REVISIONTABLE); |
|
1091 |
} |
|
1092 |
catch(Exception e) |
|
1093 |
{ |
|
1094 |
// skip this document |
|
1095 |
throw e; |
|
1096 |
} |
|
1097 |
}//if for non-data file |
|
1098 |
|
|
1099 |
// this is for data file |
|
1100 |
if(flag && dataFile) |
|
1101 |
{ |
|
1102 |
try |
|
1103 |
{ |
|
1104 |
handleSingleDataFile(remoteServer, action, docid, DocumentImpl.REVISIONTABLE); |
|
1105 |
} |
|
1106 |
catch(Exception e) |
|
1107 |
{ |
|
1108 |
// skip this datafile |
|
1109 |
throw e; |
|
1110 |
} |
|
1111 |
|
|
1112 |
}//for datafile |
|
1113 |
} |
|
1114 |
|
|
1004 | 1115 |
} |
1005 | 1116 |
|
Also available in: Unified diff
Add code to handle deleted document replication.