Revision 6118
Added by ben leinfelder over 13 years ago
test/edu/ucsb/nceas/metacattest/ReplicationServerListTest.java | ||
---|---|---|
39 | 39 |
|
40 | 40 |
import java.io.*; |
41 | 41 |
import java.net.*; |
42 |
import java.util.Date; |
|
42 | 43 |
|
43 | 44 |
/** |
44 | 45 |
* A JUnit test for testing Step class processing |
... | ... | |
269 | 270 |
*/ |
270 | 271 |
public void testGetLastCheckedDate() |
271 | 272 |
{ |
272 |
String lastCheckedDate = serverList.getLastCheckedDate("dev");
|
|
273 |
Date lastCheckedDate = serverList.getLastCheckedDate("dev");
|
|
273 | 274 |
assertTrue(lastCheckedDate.equals("0001-01-01 BC")); |
274 | 275 |
} |
275 | 276 |
|
src/edu/ucsb/nceas/metacat/IdentifierManager.java | ||
---|---|---|
51 | 51 |
import org.dataone.service.types.Subject; |
52 | 52 |
import org.dataone.service.types.SystemMetadata; |
53 | 53 |
|
54 |
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile; |
|
55 | 54 |
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface; |
56 | 55 |
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessAccess; |
57 | 56 |
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO; |
... | ... | |
690 | 689 |
return ids; |
691 | 690 |
} |
692 | 691 |
|
692 |
/** |
|
693 |
* returns a list of system metadata-only guids since the given date |
|
694 |
* @return a list of system ids in metacat that do not correspond to objects |
|
695 |
* TODO: need to check which server they are on |
|
696 |
*/ |
|
697 |
public List<String> getUpdatedSystemMetadataIds(Date since) |
|
698 |
throws Exception |
|
699 |
{ |
|
700 |
List<String> ids = new Vector<String>(); |
|
701 |
String sql = |
|
702 |
"select guid from " + TYPE_SYSTEM_METADATA + |
|
703 |
" where guid not in " + |
|
704 |
" (select guid from " + TYPE_IDENTIFIER + ") " + |
|
705 |
" and date_modified > ?"; |
|
706 |
DBConnection dbConn = null; |
|
707 |
int serialNumber = -1; |
|
708 |
try |
|
709 |
{ |
|
710 |
// Get a database connection from the pool |
|
711 |
dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getUpdatedSystemMetadataIds"); |
|
712 |
serialNumber = dbConn.getCheckOutSerialNumber(); |
|
693 | 713 |
|
714 |
// Execute the insert statement |
|
715 |
PreparedStatement stmt = dbConn.prepareStatement(sql); |
|
716 |
stmt.setDate(1, new java.sql.Date(since.getTime())); |
|
717 |
ResultSet rs = stmt.executeQuery(); |
|
718 |
while (rs.next()) |
|
719 |
{ |
|
720 |
String guid = rs.getString(1); |
|
721 |
ids.add(guid); |
|
722 |
} |
|
723 |
stmt.close(); |
|
724 |
} |
|
725 |
catch (SQLException e) |
|
726 |
{ |
|
727 |
logMetacat.error("Error while looking up the updated guids: " |
|
728 |
+ e.getMessage()); |
|
729 |
} |
|
730 |
finally |
|
731 |
{ |
|
732 |
// Return database connection to the pool |
|
733 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
|
734 |
} |
|
735 |
return ids; |
|
736 |
} |
|
694 | 737 |
|
738 |
|
|
739 |
|
|
695 | 740 |
/** |
696 | 741 |
* Determine if an identifier exists already, returning true if so. |
697 | 742 |
* |
src/edu/ucsb/nceas/metacat/replication/ReplicationServer.java | ||
---|---|---|
26 | 26 |
|
27 | 27 |
package edu.ucsb.nceas.metacat.replication; |
28 | 28 |
|
29 |
import java.util.Date; |
|
30 |
|
|
29 | 31 |
/** |
30 | 32 |
* A class express a entry in xml_replication. It include server name, |
31 | 33 |
* lastChechedDate, replication or not, dataReplication or not, hub or not |
... | ... | |
34 | 36 |
public class ReplicationServer |
35 | 37 |
{ |
36 | 38 |
private String serverName = null; //server name |
37 |
private String lastCheckedDate = null; //string of last
|
|
39 |
private Date lastCheckedDate = null; //string of last
|
|
38 | 40 |
private boolean replication = false; //replciate xml document or not |
39 | 41 |
private boolean dataReplication = false; //replciate data file or not |
40 | 42 |
//it is relative to replcation |
... | ... | |
75 | 77 |
/** |
76 | 78 |
* Get last checked date |
77 | 79 |
*/ |
78 |
public String getLastCheckedDate()
|
|
80 |
public Date getLastCheckedDate()
|
|
79 | 81 |
{ |
80 | 82 |
return this.lastCheckedDate; |
81 | 83 |
}//getLastCheckedDate |
... | ... | |
84 | 86 |
* Set a string as last checked date |
85 | 87 |
* @Param myLastCheckedDate, the string will set to object's lastCheckedDate |
86 | 88 |
*/ |
87 |
public void setLastCheckedDate(String myLastCheckedDate)
|
|
89 |
public void setLastCheckedDate(Date myLastCheckedDate)
|
|
88 | 90 |
{ |
89 | 91 |
this.lastCheckedDate = myLastCheckedDate; |
90 | 92 |
}//setLastCheckedDate |
src/edu/ucsb/nceas/metacat/replication/ReplMessageHandler.java | ||
---|---|---|
42 | 42 |
{ |
43 | 43 |
private Vector<String> indivUpdate = new Vector<String>(); |
44 | 44 |
private Vector<Vector<String>> updates = new Vector<Vector<String>>(); |
45 |
private Vector<String> indivSystemMetadata = new Vector<String>(); |
|
46 |
private Vector<Vector<String>> systemMetadataEntries = new Vector<Vector<String>>(); |
|
45 | 47 |
private Vector<String> indivDelete = new Vector<String>(); |
46 | 48 |
private Vector<Vector<String>> deletes = new Vector<Vector<String>>(); |
47 | 49 |
private Vector<String> indivRevision = new Vector<String>(); |
... | ... | |
51 | 53 |
private boolean update = false; |
52 | 54 |
private boolean delete = false; |
53 | 55 |
private boolean revision = false; |
56 |
private boolean systemMetadata = false; |
|
54 | 57 |
String currentTag = new String(); |
55 | 58 |
StringBuffer textBuffer = null; |
56 | 59 |
|
... | ... | |
81 | 84 |
indivRevision = new Vector<String>(); |
82 | 85 |
revision = true; |
83 | 86 |
} |
87 |
else if (localName.equals("updatedSystemMetadata")) |
|
88 |
{ |
|
89 |
indivSystemMetadata = new Vector<String>(); |
|
90 |
systemMetadata = true; |
|
91 |
} |
|
84 | 92 |
|
85 | 93 |
} |
86 | 94 |
|
... | ... | |
102 | 110 |
{ |
103 | 111 |
indivRevision.add(textBuffer.toString()); |
104 | 112 |
} |
113 |
else if (currentTag.equals("guid") && systemMetadata) |
|
114 |
{ |
|
115 |
indivSystemMetadata.add(textBuffer.toString()); |
|
116 |
indivSystemMetadata.add(server); |
|
117 |
} |
|
105 | 118 |
|
106 | 119 |
if(currentTag.equals("rev") && update) |
107 | 120 |
{ |
... | ... | |
171 | 184 |
revisions.add(new Vector<String>(indivRevision)); |
172 | 185 |
revision = false; |
173 | 186 |
} |
187 |
else if (localName.equals("updatedSystemMetadata")) |
|
188 |
{ |
|
189 |
systemMetadataEntries.add(new Vector<String>(indivSystemMetadata)); |
|
190 |
systemMetadata = false; |
|
191 |
} |
|
174 | 192 |
} |
175 | 193 |
|
176 | 194 |
/** |
... | ... | |
196 | 214 |
return revisions; |
197 | 215 |
} |
198 | 216 |
|
217 |
public Vector<Vector<String>> getSystemMetadataVect() |
|
218 |
{ |
|
219 |
return systemMetadataEntries; |
|
220 |
} |
|
221 |
|
|
199 | 222 |
/** |
200 | 223 |
* Gets the server name |
201 | 224 |
* @return |
src/edu/ucsb/nceas/metacat/replication/ReplicationHandler.java | ||
---|---|---|
281 | 281 |
REVINSERTNUMBER = 1; |
282 | 282 |
REVERRORNUMBER = 1; |
283 | 283 |
|
284 |
// handle system metadata |
|
285 |
Vector<Vector<String>> systemMetadataList = message.getSystemMetadataVect(); |
|
286 |
for(int k = 0; k < systemMetadataList.size(); k++) { |
|
287 |
Vector<String> w = systemMetadataList.elementAt(k); |
|
288 |
String guid = (String) w.elementAt(0); |
|
289 |
String remoteserver = (String) w.elementAt(1); |
|
290 |
try { |
|
291 |
handleSystemMetadata(remoteserver, guid); |
|
292 |
} |
|
293 |
catch (Exception ee) { |
|
294 |
logMetacat.error("Error replicating system metedata for guid: " + guid, ee); |
|
295 |
continue; |
|
296 |
} |
|
297 |
} |
|
298 |
|
|
284 | 299 |
totalServerListParseTime += (System.currentTimeMillis() - startServerListParseTime); |
285 | 300 |
}//for response |
286 | 301 |
|
... | ... | |
796 | 811 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber); |
797 | 812 |
}//finally |
798 | 813 |
} |
814 |
|
|
815 |
/** |
|
816 |
* Handle replicate system metadata |
|
817 |
* |
|
818 |
* @param remoteserver |
|
819 |
* @param guid |
|
820 |
* @throws HandlerException |
|
821 |
*/ |
|
822 |
private void handleSystemMetadata(String remoteserver, String guid) |
|
823 |
throws HandlerException { |
|
824 |
try { |
|
799 | 825 |
|
826 |
// Try get the system metadata from remote server |
|
827 |
String sysMetaURLStr = "https://" + remoteserver + "?server=" |
|
828 |
+ MetacatUtil.getLocalReplicationServerName() |
|
829 |
+ "&action=getsystemmetadata&guid=" + guid; |
|
830 |
sysMetaURLStr = MetacatUtil.replaceWhiteSpaceForURL(sysMetaURLStr); |
|
831 |
URL sysMetaUrl = new URL(sysMetaURLStr); |
|
832 |
logReplication.info("ReplicationHandler.handleSystemMetadata - Sending message: " |
|
833 |
+ sysMetaUrl.toString()); |
|
834 |
String systemMetadataXML = ReplicationService.getURLContent(sysMetaUrl); |
|
800 | 835 |
|
836 |
logReplication.info("ReplicationHandler.handleSystemMetadata - guid in repl: " + guid); |
|
801 | 837 |
|
838 |
// process system metadata |
|
839 |
if (systemMetadataXML != null) { |
|
840 |
SystemMetadata sysMeta = (SystemMetadata) ServiceTypeUtil |
|
841 |
.deserializeServiceType(SystemMetadata.class, |
|
842 |
new ByteArrayInputStream(systemMetadataXML |
|
843 |
.getBytes("UTF-8"))); |
|
844 |
// String guid = sysMeta.getIdentifier().getValue(); |
|
845 |
if (!IdentifierManager.getInstance().identifierExists(guid)) { |
|
846 |
logReplication.debug("Creating system metadata for guid: " + guid); |
|
847 |
IdentifierManager.getInstance().createSystemMetadata(sysMeta); |
|
848 |
} |
|
849 |
IdentifierManager.getInstance().updateSystemMetadata(sysMeta); |
|
850 |
} |
|
851 |
|
|
852 |
logReplication.info("ReplicationHandler.handleSystemMetadata - Successfully replicated system metadata for guid: " |
|
853 |
+ guid); |
|
854 |
|
|
855 |
String ip = getIpFromURL(sysMetaUrl); |
|
856 |
EventLog.getInstance().log(ip, ReplicationService.REPLICATIONUSER, guid, "systemMetadata"); |
|
857 |
|
|
858 |
} catch (Exception e) { |
|
859 |
logMetacat.error("ReplicationHandler.handleSystemMetadata - " |
|
860 |
+ ReplicationService.METACAT_REPL_ERROR_MSG); |
|
861 |
logReplication |
|
862 |
.error("ReplicationHandler.handleSystemMetadata - Failed to write system metadata " |
|
863 |
+ guid + " into db because " + e.getMessage()); |
|
864 |
throw new HandlerException( |
|
865 |
"ReplicationHandler.handleSystemMetadata - generic exception " |
|
866 |
+ "writing Replication: " + e.getMessage()); |
|
867 |
} |
|
868 |
|
|
869 |
} |
|
870 |
|
|
802 | 871 |
/** |
803 | 872 |
* updates xml_catalog with entries from other servers. |
804 | 873 |
*/ |
src/edu/ucsb/nceas/metacat/replication/ReplicationService.java | ||
---|---|---|
49 | 49 |
import java.util.Date; |
50 | 50 |
import java.util.Enumeration; |
51 | 51 |
import java.util.Hashtable; |
52 |
import java.util.List; |
|
52 | 53 |
import java.util.Timer; |
53 | 54 |
import java.util.Vector; |
54 | 55 |
|
... | ... | |
1010 | 1011 |
} |
1011 | 1012 |
|
1012 | 1013 |
} |
1014 |
|
|
1015 |
/** |
|
1016 |
* Sends System Metadata as XML |
|
1017 |
*/ |
|
1018 |
protected static void handleGetSystemMetadataRequest( |
|
1019 |
Hashtable<String, String[]> params, HttpServletResponse response) { |
|
1020 |
String guid = ((String[]) (params.get("guid")))[0]; |
|
1021 |
String systemMetadataXML = null; |
|
1022 |
try { |
|
1023 |
|
|
1024 |
// serialize the System Metadata as XML |
|
1025 |
SystemMetadata systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid); |
|
1026 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
1027 |
ServiceTypeUtil.serializeServiceType(SystemMetadata.class, systemMetadata, baos); |
|
1028 |
systemMetadataXML = baos.toString("UTF-8"); |
|
1029 |
|
|
1030 |
// get a writer for sending back to response |
|
1031 |
response.setContentType("text/xml"); |
|
1032 |
Writer out = response.getWriter(); |
|
1033 |
out.write(systemMetadataXML); |
|
1034 |
out.close(); |
|
1013 | 1035 |
|
1036 |
} catch (Exception e) { |
|
1037 |
String msg = "ReplicationService.handleGetSystemMetadataRequest for guid: " + guid + " : " + e.getMessage(); |
|
1038 |
logMetacat.error(msg); |
|
1039 |
logReplication.error(msg); |
|
1040 |
} |
|
1041 |
|
|
1042 |
} |
|
1043 |
|
|
1014 | 1044 |
/** |
1015 | 1045 |
* Sends a datafile to a remote host |
1016 | 1046 |
*/ |
... | ... | |
1432 | 1462 |
// add revision doc list |
1433 | 1463 |
doclist.append(prepareRevisionDoc(dbConn, revisionSql.toString(), |
1434 | 1464 |
replicateData)); |
1465 |
|
|
1466 |
// add the system metadata entries |
|
1467 |
Date since = new Date(System.currentTimeMillis()); |
|
1468 |
since = serverList.getLastCheckedDate(server); |
|
1469 |
List<String> systemMetadataEntries = |
|
1470 |
IdentifierManager.getInstance().getUpdatedSystemMetadataIds(since); |
|
1471 |
for (int i = 0; i < systemMetadataEntries.size(); i++) { |
|
1472 |
String guid = systemMetadataEntries.get(i); |
|
1473 |
doclist.append("<updatedSystemMetadata>"); |
|
1474 |
doclist.append("<guid>"); |
|
1475 |
doclist.append(guid); |
|
1476 |
doclist.append("</guid>"); |
|
1477 |
doclist.append("</updatedSystemMetadata>"); |
|
1478 |
} |
|
1435 | 1479 |
|
1436 | 1480 |
doclist.append("</updates></replication>"); |
1437 | 1481 |
logReplication.info("ReplicationService.handleUpdateRequest - doclist: " + doclist.toString()); |
src/edu/ucsb/nceas/metacat/replication/ReplicationServerList.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package edu.ucsb.nceas.metacat.replication; |
29 | 29 |
|
30 |
import java.sql.PreparedStatement; |
|
31 |
import java.sql.ResultSet; |
|
32 |
import java.sql.SQLException; |
|
33 |
import java.util.Date; |
|
34 |
import java.util.Vector; |
|
35 |
|
|
36 |
import org.apache.log4j.Logger; |
|
37 |
|
|
30 | 38 |
import edu.ucsb.nceas.metacat.database.DBConnection; |
31 | 39 |
import edu.ucsb.nceas.metacat.database.DBConnectionPool; |
32 | 40 |
import edu.ucsb.nceas.metacat.database.DatabaseService; |
33 | 41 |
|
34 |
import java.util.Vector; |
|
35 |
import java.sql.*; |
|
36 |
|
|
37 |
import org.apache.log4j.Logger; |
|
38 |
|
|
39 | 42 |
/** |
40 | 43 |
* A class represent a replication server list in xml_replcation table |
41 | 44 |
*/ |
... | ... | |
95 | 98 |
String serverName = rs.getString(1); |
96 | 99 |
logMetacat.info("ServerName: "+serverName); |
97 | 100 |
//Last check date |
98 |
String lastChecked = rs.getString(2);
|
|
101 |
Date lastChecked = rs.getDate(2);
|
|
99 | 102 |
logMetacat.info("Last checked time: "+lastChecked); |
100 | 103 |
//Replication value |
101 | 104 |
int replication = rs.getInt(3); |
... | ... | |
313 | 316 |
* @param givenServerName, the server's name which we want to get last checked |
314 | 317 |
* out date |
315 | 318 |
*/ |
316 |
public synchronized String getLastCheckedDate(String givenServerName)
|
|
319 |
public synchronized Date getLastCheckedDate(String givenServerName)
|
|
317 | 320 |
{ |
318 | 321 |
int index = -1;//Variable to store the index |
319 | 322 |
ReplicationServer server = null;//Variable for replication server |
src/edu/ucsb/nceas/metacat/replication/ReplicationServlet.java | ||
---|---|---|
207 | 207 |
ReplicationService.handleGetLockRequest(params, response); |
208 | 208 |
} else if (action.equals("getdocumentinfo")) { |
209 | 209 |
ReplicationService.handleGetDocumentInfoRequest(params, response); |
210 |
} else if (action.equals("getsystemmetadata")) { |
|
211 |
ReplicationService.handleGetSystemMetadataRequest(params, response); |
|
210 | 212 |
} else if (action.equals("gettime")) { |
211 | 213 |
ReplicationService.handleGetTimeRequest(params, response); |
212 | 214 |
} else if (action.equals("getcatalog")) { |
Also available in: Unified diff
handle timed replication of system metadata. there are still a few outstanding issues:
-track server location of system metadata-only entries
-replication policy flag for system metadata-only entries?
-locking for replicated entries?
-forced replication of entries