26 |
26 |
|
27 |
27 |
package edu.ucsb.nceas.metacat.dataone.hazelcast;
|
28 |
28 |
|
|
29 |
import java.util.Collection;
|
|
30 |
import java.util.Date;
|
|
31 |
|
29 |
32 |
import org.apache.log4j.Logger;
|
30 |
33 |
import org.dataone.service.types.v1.Identifier;
|
31 |
34 |
import org.dataone.service.types.v1.Node;
|
... | ... | |
44 |
47 |
import com.hazelcast.core.Member;
|
45 |
48 |
import com.hazelcast.partition.Partition;
|
46 |
49 |
import com.hazelcast.partition.PartitionService;
|
|
50 |
import com.hazelcast.query.EntryObject;
|
|
51 |
import com.hazelcast.query.Predicate;
|
|
52 |
import com.hazelcast.query.PredicateBuilder;
|
47 |
53 |
|
48 |
54 |
import edu.ucsb.nceas.metacat.IdentifierManager;
|
49 |
55 |
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
... | ... | |
58 |
64 |
public class HazelcastService extends BaseService
|
59 |
65 |
implements InstanceListener, EntryListener<Identifier, SystemMetadata> {
|
60 |
66 |
|
61 |
|
/* The instance of the logging class */
|
|
67 |
private static final String SINCE_PROPERTY = "dateSysMetaModified";
|
|
68 |
|
|
69 |
/* The instance of the logging class */
|
62 |
70 |
private static Logger logMetacat = Logger.getLogger(HazelcastService.class);
|
63 |
71 |
|
64 |
72 |
/* The singleton instance of the hazelcast service */
|
... | ... | |
186 |
194 |
logMetacat.error(msg);
|
187 |
195 |
|
188 |
196 |
}
|
|
197 |
|
|
198 |
// make sure we have all metadata locally
|
|
199 |
try {
|
|
200 |
// add index for resynch() method
|
|
201 |
systemMetadata.addIndex(SINCE_PROPERTY, true);
|
|
202 |
resynch();
|
|
203 |
} catch (Exception e) {
|
|
204 |
String msg = "Problem synchronizing system metadata. " + e.getMessage();
|
|
205 |
logMetacat.error(msg, e);
|
|
206 |
}
|
189 |
207 |
|
190 |
208 |
}
|
191 |
209 |
|
... | ... | |
305 |
323 |
Member ownerMember = partition.getOwner();
|
306 |
324 |
if (!ownerMember.localMember()) {
|
307 |
325 |
// need to pull the entry into the local store
|
308 |
|
logMetacat.debug("Saving entry locally: " + event.getKey().getValue());
|
309 |
|
try {
|
310 |
|
if (!IdentifierManager.getInstance().systemMetadataExists(event.getKey().getValue())) {
|
311 |
|
IdentifierManager.getInstance().createSystemMetadata(event.getValue());
|
312 |
|
} else {
|
313 |
|
IdentifierManager.getInstance().updateSystemMetadata(event.getValue());
|
314 |
|
}
|
315 |
|
} catch (McdbDocNotFoundException e) {
|
316 |
|
logMetacat.error(
|
317 |
|
"Could not save System Metadata to local store.", e);
|
318 |
|
}
|
|
326 |
saveLocally(event.getValue());
|
319 |
327 |
}
|
320 |
328 |
|
321 |
329 |
// TODO evaluate the type of system metadata change, decide if it
|
... | ... | |
326 |
334 |
// TODO: do we need to do anything explicit here?
|
327 |
335 |
|
328 |
336 |
}
|
|
337 |
|
|
338 |
/**
|
|
339 |
* Save SystemMetadata to local store if needed
|
|
340 |
* @param sm
|
|
341 |
*/
|
|
342 |
private void saveLocally(SystemMetadata sm) {
|
|
343 |
logMetacat.debug("Saving entry locally: " + sm.getIdentifier().getValue());
|
|
344 |
try {
|
|
345 |
if (!IdentifierManager.getInstance().systemMetadataExists(sm.getIdentifier().getValue())) {
|
|
346 |
IdentifierManager.getInstance().createSystemMetadata(sm);
|
|
347 |
} else {
|
|
348 |
IdentifierManager.getInstance().updateSystemMetadata(sm);
|
|
349 |
}
|
|
350 |
} catch (McdbDocNotFoundException e) {
|
|
351 |
logMetacat.error(
|
|
352 |
"Could not save System Metadata to local store.", e);
|
|
353 |
}
|
|
354 |
}
|
|
355 |
|
|
356 |
public void resynch() throws Exception {
|
|
357 |
|
|
358 |
// get the CN that is online
|
|
359 |
// TODO: do we even need to use a specific CN?
|
|
360 |
// All the System Metadata records should be available via the shared map
|
|
361 |
// NodeList allNodes = CNodeService.getInstance().listNodes();
|
|
362 |
// Node onlineCN = null;
|
|
363 |
// for (Node node: allNodes.getNodeList()) {
|
|
364 |
// if (node.getType().equals(NodeType.CN)) {
|
|
365 |
// if (node.getState().equals(NodeState.UP)) {
|
|
366 |
// onlineCN = node;
|
|
367 |
// break;
|
|
368 |
// }
|
|
369 |
// }
|
|
370 |
// }
|
|
371 |
|
|
372 |
// get the list of items that have changed since X
|
|
373 |
Date since = IdentifierManager.getInstance().getLastModifiedDate();
|
|
374 |
EntryObject e = new PredicateBuilder().getEntryObject();
|
|
375 |
Predicate predicate = e.get(SINCE_PROPERTY).greaterEqual(since);
|
|
376 |
Collection<SystemMetadata> updatedSystemMetadata = getSystemMetadataMap().values(predicate);
|
|
377 |
for (SystemMetadata sm: updatedSystemMetadata) {
|
|
378 |
saveLocally(sm);
|
|
379 |
}
|
|
380 |
}
|
329 |
381 |
|
330 |
382 |
}
|
lookup latest system metadata update date for use in synchronizing CN-CN when an offline nodes comes back online