Revision 7098
Added by ben leinfelder over 12 years ago
src/edu/ucsb/nceas/metacat/dataone/hazelcast/HazelcastService.java | ||
---|---|---|
30 | 30 |
import java.sql.SQLException; |
31 | 31 |
import java.util.Collection; |
32 | 32 |
import java.util.Date; |
33 |
import java.util.List; |
|
33 | 34 |
import java.util.concurrent.locks.Lock; |
34 | 35 |
|
35 | 36 |
import org.apache.log4j.Logger; |
... | ... | |
47 | 48 |
import com.hazelcast.core.HazelcastInstance; |
48 | 49 |
import com.hazelcast.core.IMap; |
49 | 50 |
import com.hazelcast.core.ISet; |
50 |
import com.hazelcast.core.InstanceEvent; |
|
51 |
import com.hazelcast.core.InstanceListener; |
|
52 | 51 |
import com.hazelcast.core.Member; |
52 |
import com.hazelcast.core.MembershipEvent; |
|
53 |
import com.hazelcast.core.MembershipListener; |
|
53 | 54 |
import com.hazelcast.partition.Partition; |
54 | 55 |
import com.hazelcast.partition.PartitionService; |
55 | 56 |
import com.hazelcast.query.EntryObject; |
... | ... | |
62 | 63 |
import edu.ucsb.nceas.metacat.properties.PropertyService; |
63 | 64 |
import edu.ucsb.nceas.metacat.shared.BaseService; |
64 | 65 |
import edu.ucsb.nceas.metacat.shared.ServiceException; |
66 |
import edu.ucsb.nceas.metacat.util.DocumentUtil; |
|
65 | 67 |
import edu.ucsb.nceas.utilities.FileUtil; |
66 | 68 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
67 | 69 |
/** |
68 | 70 |
* The Hazelcast service enables Metacat as a Hazelcast cluster member |
69 | 71 |
*/ |
70 | 72 |
public class HazelcastService extends BaseService |
71 |
implements InstanceListener, EntryListener<Identifier, SystemMetadata> {
|
|
73 |
implements EntryListener<Identifier, SystemMetadata>, MembershipListener {
|
|
72 | 74 |
|
73 | 75 |
private static final String SINCE_PROPERTY = "dateSysMetadataModified"; |
74 | 76 |
|
... | ... | |
209 | 211 |
// Listen for changes to the system metadata map |
210 | 212 |
systemMetadata.addEntryListener(this, true); |
211 | 213 |
|
214 |
// Listen for members added/removed |
|
215 |
hzInstance.getCluster().addMembershipListener(this); |
|
216 |
|
|
212 | 217 |
} catch (PropertyNotFoundException e) { |
213 | 218 |
|
214 | 219 |
String msg = "Couldn't find Hazelcast properties for the DataONE clusters. " + |
... | ... | |
308 | 313 |
|
309 | 314 |
} |
310 | 315 |
|
311 |
/** |
|
312 |
* Listen for new Hazelcast member events |
|
313 |
*/ |
|
314 |
@Override |
|
315 |
public void instanceCreated(InstanceEvent event) { |
|
316 |
logMetacat.info("New Hazelcast instance created: " + |
|
317 |
event.getInstance().getId() + ", " + |
|
318 |
event.getInstance().getInstanceType()); |
|
319 |
|
|
320 |
} |
|
321 |
|
|
322 |
@Override |
|
323 |
public void instanceDestroyed(InstanceEvent event) { |
|
324 |
logMetacat.info("Hazelcast instance removed: " + |
|
325 |
event.getInstance().getId() + ", " + |
|
326 |
event.getInstance().getInstanceType()); |
|
327 |
|
|
328 |
} |
|
329 |
|
|
330 | 316 |
public HazelcastInstance getHazelcastInstance() { |
331 | 317 |
return this.hzInstance; |
332 | 318 |
|
... | ... | |
493 | 479 |
} |
494 | 480 |
} |
495 | 481 |
|
482 |
/** |
|
483 |
* When there is missing SystemMetadata on the local member, |
|
484 |
* we retrieve it from the shared map and add it to the local |
|
485 |
* backing store for safe keeping. |
|
486 |
*/ |
|
487 |
@Override |
|
488 |
public void memberAdded(MembershipEvent event) { |
|
489 |
Member member = event.getMember(); |
|
490 |
logMetacat.debug("Member added to cluster: " + member.getInetSocketAddress()); |
|
491 |
boolean isLocal = member.localMember(); |
|
492 |
if (isLocal) { |
|
493 |
logMetacat.debug("Member islocal: " + member.getInetSocketAddress()); |
|
494 |
List<String> localIds = IdentifierManager.getInstance().getLocalIdsWithNoSystemMetadata(true); |
|
495 |
if (localIds != null) { |
|
496 |
logMetacat.debug("Member missing SystemMetadata entries, count = " + localIds.size()); |
|
497 |
for (String localId: localIds) { |
|
498 |
logMetacat.debug("Processing system metadata for localId: " + localId); |
|
499 |
try { |
|
500 |
String docid = DocumentUtil.getSmartDocId(localId); |
|
501 |
int rev = DocumentUtil.getRevisionFromAccessionNumber(localId); |
|
502 |
String guid = IdentifierManager.getInstance().getGUID(docid, rev); |
|
503 |
logMetacat.debug("Found mapped guid: " + guid); |
|
504 |
Identifier pid = new Identifier(); |
|
505 |
pid.setValue(guid); |
|
506 |
SystemMetadata sm = systemMetadata.get(pid); |
|
507 |
logMetacat.debug("Found shared system metadata for guid: " + guid); |
|
508 |
saveLocally(sm); |
|
509 |
logMetacat.debug("Saved shared system metadata locally for guid: " + guid); |
|
510 |
} catch (Exception e) { |
|
511 |
logMetacat.error("Could not save shared SystemMetadata entry locally, localId: " + localId, e); |
|
512 |
} |
|
513 |
} |
|
514 |
} |
|
515 |
} |
|
516 |
} |
|
517 |
|
|
518 |
@Override |
|
519 |
public void memberRemoved(MembershipEvent event) { |
|
520 |
// TODO Auto-generated method stub |
|
521 |
|
|
522 |
} |
|
523 |
|
|
496 | 524 |
} |
Also available in: Unified diff
-use MembershipListener to keep new members' backing store for system metadata synchronized with the shared system metadata map.
-remove the unused InstanceListener interface