Revision 7519
Added by ben leinfelder almost 12 years ago
src/edu/ucsb/nceas/metacat/admin/upgrade/RemoveInvalidReplicas.java | ||
---|---|---|
71 | 71 |
|
72 | 72 |
private boolean dryRun = false; |
73 | 73 |
|
74 |
private int serverLocation = 0; |
|
75 |
|
|
76 |
public int getServerLocation() { |
|
77 |
return serverLocation; |
|
78 |
} |
|
79 |
|
|
80 |
public void setServerLocation(int serverLocation) { |
|
81 |
this.serverLocation = serverLocation; |
|
82 |
} |
|
83 |
|
|
74 | 84 |
public boolean upgrade() throws AdminException { |
75 | 85 |
|
76 | 86 |
boolean success = true; |
77 | 87 |
|
88 |
// do not allow this. ever. |
|
89 |
if (serverLocation == 1) { |
|
90 |
throw new AdminException("This is a DESTRUCTIVE action. Cannot remove original objects from home server: " + serverLocation); |
|
91 |
} |
|
92 |
|
|
78 | 93 |
Connection sqlca = null; |
79 | 94 |
PreparedStatement pstmt = null; |
80 | 95 |
|
... | ... | |
82 | 97 |
|
83 | 98 |
log.debug("dryRun: " + dryRun); |
84 | 99 |
|
85 |
|
|
86 | 100 |
// get the properties |
87 | 101 |
driver = PropertyService.getProperty("database.driver"); |
88 | 102 |
url = PropertyService.getProperty("database.connectionURI"); |
... | ... | |
99 | 113 |
List<String> invalidReplicas = new ArrayList<String>(); |
100 | 114 |
pstmt = sqlca.prepareStatement( |
101 | 115 |
"SELECT guid " + |
102 |
"FROM identifier id, xml_documents xml, access_log log " +
|
|
116 |
"FROM xml_documents xml, identifier id, access_log log " +
|
|
103 | 117 |
"WHERE id.docid = xml.docid " + |
104 | 118 |
"AND id.rev = xml.rev " + |
105 | 119 |
"AND log.docid = id.docid || '.' || id.rev " + |
106 |
"AND xml.server_location != '1' " + |
|
107 |
"AND log.event = ?"); |
|
108 |
pstmt.setString(1, Event.SYNCHRONIZATION_FAILED.xmlValue()); |
|
120 |
"AND xml.server_location = ? " + |
|
121 |
"AND log.event = ? " + |
|
122 |
"UNION " + |
|
123 |
"SELECT guid " + |
|
124 |
"FROM xml_revisions xml, identifier id, access_log log " + |
|
125 |
"WHERE id.docid = xml.docid " + |
|
126 |
"AND id.rev = xml.rev " + |
|
127 |
"AND log.docid = id.docid || '.' || id.rev " + |
|
128 |
"AND xml.server_location = ? " + |
|
129 |
"AND log.event = ? "); |
|
130 |
pstmt.setInt(1, serverLocation); |
|
131 |
pstmt.setString(2, Event.SYNCHRONIZATION_FAILED.xmlValue()); |
|
132 |
pstmt.setInt(3, serverLocation); |
|
133 |
pstmt.setString(4, Event.SYNCHRONIZATION_FAILED.xmlValue()); |
|
109 | 134 |
log.debug("Finding invalid (failed replicas with SQL: " + pstmt.toString()); |
110 | 135 |
ResultSet rs = pstmt.executeQuery(); |
111 | 136 |
while (rs.next()) { |
... | ... | |
134 | 159 |
try { |
135 | 160 |
if (!dryRun) { |
136 | 161 |
MNodeService.getInstance(request).delete(session, pid); |
162 |
log.debug("Deleted invalid replica object: " + pid.getValue()); |
|
163 |
|
|
164 |
// remove SM from database |
|
165 |
IdentifierManager.getInstance().deleteSystemMetadata(identifier); |
|
166 |
log.debug("Deleted invalid replica SystemMetadata for: " + pid.getValue()); |
|
167 |
|
|
168 |
// remove the identifier from the database |
|
169 |
if (IdentifierManager.getInstance().mappingExists(identifier)) { |
|
170 |
String localId = IdentifierManager.getInstance().getLocalId(identifier); |
|
171 |
IdentifierManager.getInstance().removeMapping(identifier, localId); |
|
172 |
log.debug("Removed localId mapping: " + localId); |
|
173 |
} else { |
|
174 |
// remove from the identifier table manually |
|
175 |
pstmt.setString(1, identifier); |
|
176 |
int count = pstmt.executeUpdate(); |
|
177 |
log.debug("Removed identifier entry with SQL: " + pstmt.toString()); |
|
178 |
|
|
179 |
// warn if we saw something unexpected |
|
180 |
if (count <= 0) { |
|
181 |
log.warn("Delete returned unexpected count for pid: " + identifier); |
|
182 |
} |
|
183 |
} |
|
184 |
|
|
185 |
// purge from Hz map |
|
186 |
HazelcastService.getInstance().getSystemMetadataMap().evict(pid); |
|
187 |
log.debug("Evicted identifier from HZ map: " + pid.getValue()); |
|
188 |
|
|
137 | 189 |
} |
138 | 190 |
} catch (Exception e) { |
139 | 191 |
log.error("Could not delete invalid replica: " + identifier, e); |
140 | 192 |
continue; |
141 | 193 |
} |
142 |
|
|
143 |
|
|
144 |
if (!dryRun) { |
|
145 |
// remove SM |
|
146 |
IdentifierManager.getInstance().deleteSystemMetadata(identifier); |
|
147 |
|
|
148 |
// remove the identifier |
|
149 |
if (IdentifierManager.getInstance().mappingExists(identifier)) { |
|
150 |
String localId = IdentifierManager.getInstance().getLocalId(identifier); |
|
151 |
IdentifierManager.getInstance().removeMapping(identifier, localId ); |
|
152 |
} |
|
153 |
|
|
154 |
// purge from Hz |
|
155 |
HazelcastService.getInstance().getSystemMetadataMap().evict(pid); |
|
156 |
} |
|
157 |
|
|
158 |
// remove from the identifier table |
|
159 |
pstmt.setString(1, identifier); |
|
160 |
if (!dryRun) { |
|
161 |
int count = pstmt.executeUpdate(); |
|
162 |
// warn if we saw something unexpected |
|
163 |
if (count <= 0) { |
|
164 |
log.warn("Delete returned unexpected count for pid: " + identifier); |
|
165 |
} |
|
166 |
} |
|
167 | 194 |
} |
168 | 195 |
|
169 | 196 |
} catch (Exception e) { |
... | ... | |
187 | 214 |
return success; |
188 | 215 |
} |
189 | 216 |
|
190 |
public static void main(String [] ags){ |
|
217 |
public static void main(String [] args){
|
|
191 | 218 |
|
192 | 219 |
try { |
193 | 220 |
// set up the properties based on the test/deployed configuration of the workspace |
... | ... | |
196 | 223 |
testProperties.load(); |
197 | 224 |
String metacatContextDir = testProperties.getProperty("metacat.contextDir"); |
198 | 225 |
PropertyService.getInstance(metacatContextDir + "/WEB-INF"); |
226 |
|
|
199 | 227 |
// now run it |
200 | 228 |
RemoveInvalidReplicas upgrader = new RemoveInvalidReplicas(); |
229 |
if (args.length > 0) { |
|
230 |
String serverLocation = args[0]; |
|
231 |
upgrader.setServerLocation(Integer.parseInt(serverLocation)); |
|
232 |
} |
|
201 | 233 |
//upgrader.dryRun = true; |
202 | 234 |
upgrader.upgrade(); |
203 | 235 |
|
src/edu/ucsb/nceas/metacat/replication/ReplicationService.java | ||
---|---|---|
78 | 78 |
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException; |
79 | 79 |
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile; |
80 | 80 |
import edu.ucsb.nceas.metacat.accesscontrol.PermOrderException; |
81 |
import edu.ucsb.nceas.metacat.admin.upgrade.RemoveInvalidReplicas; |
|
81 | 82 |
import edu.ucsb.nceas.metacat.admin.upgrade.dataone.GenerateORE; |
82 | 83 |
import edu.ucsb.nceas.metacat.admin.upgrade.dataone.GenerateSystemMetadata; |
83 | 84 |
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException; |
... | ... | |
402 | 403 |
gore.upgrade(); |
403 | 404 |
out.write("Generated ORE maps for server " + serverid); |
404 | 405 |
|
406 |
} else if (subaction.equals("removeinvalidreplicas")) { |
|
407 |
RemoveInvalidReplicas rir = new RemoveInvalidReplicas(); |
|
408 |
int serverLocation = -1; |
|
409 |
String serverid = ((String[]) params.get("serverid"))[0]; |
|
410 |
serverLocation = Integer.parseInt(serverid); |
|
411 |
rir.setServerLocation(serverLocation ); |
|
412 |
rir.upgrade(); |
|
413 |
out.write("Removed invalid replicas for server " + serverid); |
|
414 |
|
|
405 | 415 |
} else { |
406 | 416 |
|
407 | 417 |
out.write("<error>Unkonwn subaction</error>"); |
... | ... | |
425 | 435 |
if (showGenerateSystemMetadata) { |
426 | 436 |
out.write("<td><b>System Metadata</b></td>"); |
427 | 437 |
out.write("<td><b>ORE Maps</b></td>"); |
438 |
out.write("<td><b>Invalid Replicas</b></td>"); |
|
428 | 439 |
} |
429 | 440 |
out.write("</tr>"); |
430 | 441 |
|
... | ... | |
433 | 444 |
ResultSet rs = pstmt.getResultSet(); |
434 | 445 |
boolean tablehasrows = rs.next(); |
435 | 446 |
while (tablehasrows) { |
447 |
String serverId = rs.getString(1); |
|
436 | 448 |
out.write("<tr><td>" + rs.getString(2) + "</td><td>"); |
437 | 449 |
out.write(rs.getString(3) + "</td><td>"); |
438 | 450 |
out.write(rs.getString(4) + "</td><td>"); |
... | ... | |
441 | 453 |
if (showGenerateSystemMetadata) { |
442 | 454 |
// for SM |
443 | 455 |
out.write("<td><form action='" + request.getContextPath() + "/admin'>"); |
444 |
out.write("<input name='serverid' type='hidden' value='" + rs.getString(1) + "'/>");
|
|
456 |
out.write("<input name='serverid' type='hidden' value='" + serverId + "'/>");
|
|
445 | 457 |
out.write("<input name='configureType' type='hidden' value='replication'/>"); |
446 | 458 |
out.write("<input name='action' type='hidden' value='servercontrol'/>"); |
447 | 459 |
out.write("<input name='subaction' type='hidden' value='generatesystemmetadata'/>"); |
... | ... | |
450 | 462 |
|
451 | 463 |
// for ORE maps |
452 | 464 |
out.write("<td><form action='" + request.getContextPath() + "/admin'>"); |
453 |
out.write("<input name='serverid' type='hidden' value='" + rs.getString(1) + "'/>");
|
|
465 |
out.write("<input name='serverid' type='hidden' value='" + serverId + "'/>");
|
|
454 | 466 |
out.write("<input name='configureType' type='hidden' value='replication'/>"); |
455 | 467 |
out.write("<input name='action' type='hidden' value='servercontrol'/>"); |
456 | 468 |
out.write("<input name='subaction' type='hidden' value='generateore'/>"); |
457 | 469 |
out.write("<input type='submit' value='Generate ORE'/>"); |
458 | 470 |
out.write("</form></td>"); |
471 |
|
|
472 |
// for invalid replicas |
|
473 |
out.write("<td><form action='" + request.getContextPath() + "/admin'>"); |
|
474 |
out.write("<input name='serverid' type='hidden' value='" + serverId + "'/>"); |
|
475 |
out.write("<input name='configureType' type='hidden' value='replication'/>"); |
|
476 |
out.write("<input name='action' type='hidden' value='servercontrol'/>"); |
|
477 |
out.write("<input name='subaction' type='hidden' value='removeinvalidreplicas'/>"); |
|
478 |
String disabled = ""; |
|
479 |
if (Integer.valueOf(serverId) == 1) { |
|
480 |
disabled = "disabled='true'"; |
|
481 |
} |
|
482 |
out.write("<input type='submit' value='Remove Invalid Replicas' " + disabled + " />"); |
|
483 |
out.write("</form></td>"); |
|
459 | 484 |
} |
460 | 485 |
out.write("</tr>"); |
461 | 486 |
|
Also available in: Unified diff
include xml_revisions.
do not allow removal of server_location = 1 documents (these are not replicas).
https://redmine.dataone.org/issues/3539