Project

General

Profile

« Previous | Next » 

Revision 7519

include xml_revisions.
do not allow removal of server_location = 1 documents (these are not replicas).
https://redmine.dataone.org/issues/3539

View differences:

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
            

Also available in: Unified diff