Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: Implements a service for managing a Hazelcast cluster member
4
 *  Copyright: 2011 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Christopher Jones
7
 * 
8
 *   '$Author: leinfelder $'
9
 *     '$Date: 2013-06-25 13:17:52 -0700 (Tue, 25 Jun 2013) $'
10
 * '$Revision: 7827 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.dataone.hazelcast;
28

    
29
import java.io.FileNotFoundException;
30
import java.sql.SQLException;
31
import java.util.HashSet;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Set;
35
import java.util.concurrent.ExecutorService;
36
import java.util.concurrent.Executors;
37
import java.util.concurrent.locks.Lock;
38

    
39
import org.apache.log4j.Logger;
40
import org.dataone.service.exceptions.InvalidSystemMetadata;
41
import org.dataone.service.types.v1.Identifier;
42
import org.dataone.service.types.v1.SystemMetadata;
43

    
44
import com.hazelcast.config.Config;
45
import com.hazelcast.config.FileSystemXmlConfig;
46
import com.hazelcast.core.EntryEvent;
47
import com.hazelcast.core.EntryListener;
48
import com.hazelcast.core.Hazelcast;
49
import com.hazelcast.core.HazelcastInstance;
50
import com.hazelcast.core.ILock;
51
import com.hazelcast.core.IMap;
52
import com.hazelcast.core.ISet;
53
import com.hazelcast.core.ItemEvent;
54
import com.hazelcast.core.ItemListener;
55
import com.hazelcast.core.LifecycleEvent;
56
import com.hazelcast.core.LifecycleListener;
57
import com.hazelcast.core.Member;
58
import com.hazelcast.core.MembershipEvent;
59
import com.hazelcast.core.MembershipListener;
60
import com.hazelcast.partition.Partition;
61
import com.hazelcast.partition.PartitionService;
62

    
63
import edu.ucsb.nceas.metacat.IdentifierManager;
64
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
65
import edu.ucsb.nceas.metacat.properties.PropertyService;
66
import edu.ucsb.nceas.metacat.shared.BaseService;
67
import edu.ucsb.nceas.metacat.shared.ServiceException;
68
import edu.ucsb.nceas.metacat.util.DocumentUtil;
69
import edu.ucsb.nceas.utilities.FileUtil;
70
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
71
/**
72
 * The Hazelcast service enables Metacat as a Hazelcast cluster member
73
 */
74
public class HazelcastService extends BaseService
75
  implements EntryListener<Identifier, SystemMetadata>, MembershipListener, LifecycleListener, ItemListener<Identifier> {
76
  
77
  private static final String SINCE_PROPERTY = "dateSysMetadataModified";
78

    
79
  private static final String MISSING_PID_PREFIX = "missing-";
80

    
81
/* The instance of the logging class */
82
  private static Logger logMetacat = Logger.getLogger(HazelcastService.class);
83
  
84
  /* The singleton instance of the hazelcast service */
85
  private static HazelcastService hzService = null;
86
  
87
  /* The Hazelcast configuration */
88
  private Config hzConfig;
89
  
90
  /* The name of the system metadata map */
91
  private String systemMetadataMap;
92
  
93
  /* The Hazelcast distributed system metadata map */
94
  private IMap<Identifier, SystemMetadata> systemMetadata;
95
  
96
  /* The name of the identifiers set */
97
  private String identifiersSet;
98
  
99
  /* The Hazelcast distributed identifiers set */
100
  private ISet<Identifier> identifiers;
101
  
102
  /* The Hazelcast distributed missing identifiers set */
103
  private ISet<Identifier> missingIdentifiers;
104
  
105
  /* The Hazelcast distributed index queue */
106
  private String hzIndexQueue;
107
  private ISet<SystemMetadata> indexQueue;
108

    
109
  private HazelcastInstance hzInstance;
110
      
111
  /*
112
   * Constructor: Creates an instance of the hazelcast service. Since
113
   * this uses a singleton pattern, use getInstance() to gain the instance.
114
   */
115
  private HazelcastService() {
116
    
117
    super();
118
    _serviceName="HazelcastService";
119
    
120
    try {
121
      init();
122
      
123
    } catch (ServiceException se) {
124
      logMetacat.error("There was a problem creating the HazelcastService. " +
125
                       "The error message was: " + se.getMessage());
126
      
127
    }
128
    
129
  }
130
  
131
  /**
132
   *  Get the instance of the HazelcastService that has been instantiated,
133
   *  or instantiate one if it has not been already.
134
   *
135
   * @return hazelcastService - The instance of the hazelcast service
136
   */
137
  public static HazelcastService getInstance(){
138
    
139
    if ( hzService == null ) {
140
      
141
      hzService = new HazelcastService();
142
      
143
    }
144
    return hzService;
145
  }
146
  
147
  /**
148
   * Initializes the Hazelcast service
149
   */
150
  public void init() throws ServiceException {
151
    
152
    logMetacat.debug("HazelcastService.init() called.");
153
    
154
	String configFileName = null;
155
	try {
156
		configFileName = PropertyService.getProperty("dataone.hazelcast.configFilePath");
157
		hzConfig = new FileSystemXmlConfig(configFileName);
158
	} catch (Exception e) {
159
		configFileName = PropertyService.CONFIG_FILE_DIR + FileUtil.getFS() + "hazelcast.xml";
160
		logMetacat.warn("Custom Hazelcast configuration not defined, using default: " + configFileName);
161
		// make sure we have the config
162
		try {
163
			hzConfig = new FileSystemXmlConfig(configFileName);
164
		} catch (FileNotFoundException e1) {
165
			String msg = e.getMessage();
166
			logMetacat.error(msg);
167
			throw new ServiceException(msg);
168
		}
169
	}
170

    
171
	this.hzInstance = Hazelcast.newHazelcastInstance(hzConfig);
172
  
173
  	logMetacat.debug("Initialized hzInstance");
174

    
175
    // Get configuration properties on instantiation
176
    try {
177
      systemMetadataMap = 
178
        PropertyService.getProperty("dataone.hazelcast.storageCluster.systemMetadataMap");
179
      identifiersSet = PropertyService.getProperty("dataone.hazelcast.storageCluster.identifiersSet");
180

    
181
      // Get a reference to the shared system metadata map as a cluster member
182
      // NOTE: this loads the map from the backing store and can take a long time for large collections
183
      systemMetadata = this.hzInstance.getMap(systemMetadataMap);
184
      
185
      logMetacat.debug("Initialized systemMetadata");
186

    
187
      // Get a reference to the shared identifiers set as a cluster member
188
      // NOTE: this takes a long time to complete
189
      logMetacat.warn("Retrieving hzIdentifiers from Hazelcast");
190
      identifiers = this.hzInstance.getSet(identifiersSet);
191
      logMetacat.warn("Retrieved hzIdentifiers from Hazelcast");
192
      
193
      // for publishing the "PIDs Wanted" list
194
      missingIdentifiers = this.hzInstance.getSet("hzMissingIdentifiersSet");
195
      
196
      missingIdentifiers.addItemListener(this, true);
197
      
198
      hzIndexQueue = PropertyService.getProperty("index.hazelcast.indexqueue");
199
      indexQueue = this.hzInstance.getSet(hzIndexQueue);
200

    
201
      // Listen for changes to the system metadata map
202
      systemMetadata.addEntryListener(this, true);
203
      
204
      // Listen for members added/removed
205
      hzInstance.getCluster().addMembershipListener(this);
206
      
207
      // Listen for lifecycle state changes
208
      hzInstance.getLifecycleService().addLifecycleListener(this);
209
      
210
    } catch (PropertyNotFoundException e) {
211

    
212
      String msg = "Couldn't find Hazelcast properties for the DataONE clusters. " +
213
        "The error message was: " + e.getMessage();
214
      logMetacat.error(msg);
215
      
216
    }
217
    
218
    // make sure we have all metadata locally
219
    try {
220
    	// synch on restart
221
        resynchInThread();
222
	} catch (Exception e) {
223
		String msg = "Problem resynchronizing system metadata. " + e.getMessage();
224
		logMetacat.error(msg, e);
225
	}
226
        
227
  }
228
  
229
  /**
230
   * Get the system metadata map
231
   * 
232
   * @return systemMetadata - the hazelcast map of system metadata
233
   * @param identifier - the identifier of the object as a string
234
   */
235
  public IMap<Identifier,SystemMetadata> getSystemMetadataMap() {
236
	  return systemMetadata;
237
  }
238
  
239
  /**
240
   * Get the identifiers set
241
   * @return identifiers - the set of unique DataONE identifiers in the cluster
242
   */
243
  public ISet<Identifier> getIdentifiers() {
244
      return identifiers;
245
      
246
  }
247

    
248
  /**
249
   * Get the index queue
250
   * @return the set of SystemMetadata to be indexed
251
   */
252
  public ISet<SystemMetadata> getIndexQueue() {
253
      return indexQueue;
254
      
255
  }
256
  
257
  /**
258
   * When Metacat changes the underlying store, we need to refresh the
259
   * in-memory representation of it.
260
   * @param guid
261
   */
262
  public void refreshSystemMetadataEntry(String guid) {
263
	Identifier identifier = new Identifier();
264
	identifier.setValue(guid);
265
	// force hazelcast to update system metadata in memory from the store
266
	HazelcastService.getInstance().getSystemMetadataMap().evict(identifier);
267
  }
268

    
269
  public Lock getLock(String identifier) {
270
    
271
    Lock lock = null;
272
    
273
    try {
274
        lock = getInstance().getHazelcastInstance().getLock(identifier);
275
        
276
    } catch (RuntimeException e) {
277
        logMetacat.info("Couldn't get a lock for identifier " + 
278
            identifier + " !!");
279
    }
280
    return lock;
281
      
282
  }
283
  
284
  /**
285
   * Get the DataONE hazelcast node map
286
   * @return nodes - the hazelcast map of nodes
287
   */
288
//  public IMap<NodeReference, Node> getNodesMap() {
289
//	  return nodes;
290
//  }
291
  
292
  /**
293
   * Indicate whether or not this service is refreshable.
294
   *
295
   * @return refreshable - the boolean refreshable status
296
   */
297
  public boolean refreshable() {
298
    // TODO: Determine the consequences of restarting the Hazelcast instance
299
    // Set this to true if it's okay to drop from the cluster, lose the maps,
300
    // and start back up again
301
    return false;
302
    
303
  }
304
  
305
  /**
306
   * Stop the HazelcastService. When stopped, the service will no longer
307
   * respond to requests.
308
   */
309
  public void stop() throws ServiceException {
310
    
311
	  this.hzInstance.getLifecycleService().shutdown();
312
    
313
  }
314

    
315
  public HazelcastInstance getHazelcastInstance() {
316
      return this.hzInstance;
317
      
318
  }
319
  
320
  /**
321
   * Refresh the Hazelcast service by restarting it
322
   */
323
  @Override
324
  protected void doRefresh() throws ServiceException {
325

    
326
    // TODO: verify that the correct config file is still used
327
	  this.hzInstance.getLifecycleService().restart();
328
    
329
  }
330
  
331
  /**
332
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
333
	 * added events in the hzSystemMetadata map. Evaluate the entry and create
334
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
335
	 * 
336
	 * @param event - The EntryEvent that occurred
337
	 */
338
	@Override
339
	public void entryAdded(EntryEvent<Identifier, SystemMetadata> event) {
340
	  
341
	  logMetacat.info("SystemMetadata entry added event on identifier " + 
342
	      event.getKey().getValue());
343
		// handle as update - that method will create if necessary
344
		entryUpdated(event);
345

    
346
	}
347

    
348
	/**
349
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
350
	 * evicted events in the hzSystemMetadata map.  Evaluate the entry and create
351
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
352
	 * 
353
	 * @param event - The EntryEvent that occurred
354
	 */
355
	@Override
356
	public void entryEvicted(EntryEvent<Identifier, SystemMetadata> event) {
357

    
358
      logMetacat.info("SystemMetadata entry evicted event on identifier " + 
359
          event.getKey().getValue());
360
      
361
	    // ensure identifiers are listed in the hzIdentifiers set
362
      if ( !identifiers.contains(event.getKey()) ) {
363
          identifiers.add(event.getKey());
364
      }
365
	  
366
	}
367
	
368
	/**
369
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
370
	 * removed events in the hzSystemMetadata map.  Evaluate the entry and create
371
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
372
	 * 
373
	 * @param event - The EntryEvent that occurred
374
	 */
375
	@Override
376
	public void entryRemoved(EntryEvent<Identifier, SystemMetadata> event) {
377
		
378
    logMetacat.info("SystemMetadata entry removed event on identifier " + 
379
        event.getKey().getValue());
380

    
381
	  // we typically don't remove objects in Metacat, but can remove System Metadata
382
		IdentifierManager.getInstance().deleteSystemMetadata(event.getValue().getIdentifier().getValue());
383

    
384
    // keep the hzIdentifiers set in sync with the systemmetadata table
385
    if ( identifiers.contains(event.getKey()) ) {
386
        identifiers.remove(event.getKey());
387
        
388
    }
389

    
390
	}
391
	
392
	/**
393
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
394
	 * updated events in the hzSystemMetadata map.  Evaluate the entry and create
395
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
396
	 * 
397
	 * @param event - The EntryEvent that occurred
398
	 */
399
	@Override
400
	public void entryUpdated(EntryEvent<Identifier, SystemMetadata> event) {
401

    
402
		logMetacat.debug("Entry added/updated to System Metadata map: " + event.getKey().getValue());
403
		PartitionService partitionService = this.hzInstance.getPartitionService();
404
		Partition partition = partitionService.getPartition(event.getKey());
405
		Member ownerMember = partition.getOwner();
406
		SystemMetadata sysmeta = event.getValue();
407
		if (!ownerMember.localMember()) {
408
			if (sysmeta == null) {
409
				logMetacat.warn("No SystemMetadata provided in the event, getting from shared map: " + event.getKey().getValue());
410
				sysmeta = getSystemMetadataMap().get(event.getKey());
411
				if (sysmeta == null) {
412
					// this is a problem
413
					logMetacat.error("Could not find SystemMetadata in shared map for: " + event.getKey().getValue());
414
					// TODO: should probably return at this point since the save will fail
415
				}
416
			}
417
			// need to pull the entry into the local store
418
			saveLocally(event.getValue());
419
		}
420

    
421
		// ensure identifiers are listed in the hzIdentifiers set
422
		if (!identifiers.contains(event.getKey())) {
423
			identifiers.add(event.getKey());
424
		}
425

    
426
	}
427
	
428
	/**
429
	 * Save SystemMetadata to local store if needed
430
	 * @param sm
431
	 */
432
	private void saveLocally(SystemMetadata sm) {
433
		logMetacat.debug("Saving entry locally: " + sm.getIdentifier().getValue());
434
		try {
435

    
436
			IdentifierManager.getInstance().insertOrUpdateSystemMetadata(sm);
437

    
438
		} catch (McdbDocNotFoundException e) {
439
			logMetacat.error("Could not save System Metadata to local store.", e);
440
			
441
		} catch (SQLException e) {
442
	      logMetacat.error("Could not save System Metadata to local store.", e);
443
	      
444
	    } catch (InvalidSystemMetadata e) {
445
	        logMetacat.error("Could not save System Metadata to local store.", e);
446
	        
447
	    }
448
	}
449
	
450
	/**
451
	 * Checks the local backing store for missing SystemMetadata,
452
	 * retrieves those entries from the shared map if they exist,
453
	 * and saves them locally.
454
	 */
455
	private void synchronizeLocalStore() {
456
		List<String> localIds = IdentifierManager.getInstance().getLocalIdsWithNoSystemMetadata(true, -1);
457
		if (localIds != null) {
458
			logMetacat.debug("Member missing SystemMetadata entries, count = " + localIds.size());
459
			for (String localId: localIds) {
460
				logMetacat.debug("Processing system metadata for localId: " + localId);
461
				try {
462
					String docid = DocumentUtil.getSmartDocId(localId);
463
					int rev = DocumentUtil.getRevisionFromAccessionNumber(localId);
464
					String guid = IdentifierManager.getInstance().getGUID(docid, rev);
465
					logMetacat.debug("Found mapped guid: " + guid);
466
					Identifier pid = new Identifier();
467
					pid.setValue(guid);
468
					SystemMetadata sm = systemMetadata.get(pid);
469
					logMetacat.debug("Found shared system metadata for guid: " + guid);
470
					saveLocally(sm);
471
					logMetacat.debug("Saved shared system metadata locally for guid: " + guid);
472
				} catch (Exception e) {
473
					logMetacat.error("Could not save shared SystemMetadata entry locally, localId: " + localId, e);
474
				}
475
			}
476
		}
477
	}
478
	
479
	
480
	/**
481
	 * Make sure we have a copy of every entry in the shared map.
482
	 * We use lazy loading and therefore the CNs may not all be in sync when one
483
	 * comes back online after an extended period of being offline
484
	 * This method loops through the entries that a FULLY UP-TO-DATE CN has
485
	 * and makes sure each one is present on the shared map.
486
	 * It is meant to overcome a HZ weakness wherein ownership of a key results in 
487
	 * null values where the owner does not have a complete backing store.
488
	 * This will be an expensive routine and should be run in a background process so that
489
	 * the server can continue to service other requests during the synch
490
	 * @throws Exception
491
	 */
492
	private void resynchToRemote() {
493
		
494
		// the local identifiers not already present in the shared map
495
		Set<Identifier> localIdKeys = loadAllKeys();
496
		
497
		//  the PIDs missing locally
498
		Set<Identifier> missingIdKeys = new HashSet<Identifier>();
499
				
500
		// only contribute PIDs that are not already shared
501
		Iterator<Identifier> idIter = identifiers.iterator();
502
		int processedCount = 0;
503
		while (idIter.hasNext()) {
504
			Identifier pid = idIter.next();
505
			if (localIdKeys.contains(pid)) {
506
				logMetacat.debug("Shared pid is already in local identifier set: " + pid.getValue());
507
				localIdKeys.remove(pid);
508
			} else {
509
				// we don't have this locally, so we should try to get it
510
				missingIdKeys.add(pid);
511
			}
512
			processedCount++;
513
		}
514
		logMetacat.warn("processedCount (identifiers from iterator): " + processedCount);
515

    
516
		logMetacat.warn("local pid count not yet shared: " + localIdKeys.size() + ", shared pid count: " + identifiers.size());
517

    
518
		//identifiers.addAll(idKeys);
519
		logMetacat.warn("Loading missing local keys into hzIdentifiers");
520
		for (Identifier key: localIdKeys) {
521
			if (!identifiers.contains(key)) {
522
				logMetacat.debug("Adding missing hzIdentifiers key: " + key.getValue());
523
				identifiers.add(key);
524
			}
525
		}
526
		logMetacat.warn("Initialized identifiers with missing local keys");
527
		
528
		logMetacat.warn("Processing missing SystemMetadata for missing pid count: " + missingIdKeys.size());
529
		
530
		// loop through all the missing PIDs to find any null (missing) SM that needs to be resynched
531
		Iterator<Identifier> missingPids = missingIdKeys.iterator();
532
		while (missingPids.hasNext()) {
533
			Identifier pid = missingPids.next();
534
			// publish that we need this SM entry
535
			logMetacat.debug("Publishing missing pid to wanted list: " + pid.getValue());
536
			missingIdentifiers.add(pid);
537
		}
538
		
539
	}
540
	
541
	public void resynchInThread() {
542
		logMetacat.debug("launching system metadata resynch in a thread");
543
		ExecutorService executor = Executors.newSingleThreadExecutor();
544
		executor.execute(new Runnable() {
545
			@Override
546
			public void run() {
547
				try {
548
					// this is a push mechanism
549
					resynchToRemote();
550
				} catch (Exception e) {
551
					logMetacat.error("Error in resynchInThread: " + e.getMessage(), e);
552
				}
553
			}
554
		});
555
		executor.shutdown();
556
	}
557

    
558
	/**
559
	 * When there is missing SystemMetadata on the local member,
560
	 * we retrieve it from the shared map and add it to the local
561
	 * backing store for safe keeping.
562
	 */
563
	@Override
564
	public void memberAdded(MembershipEvent event) {
565
		Member member = event.getMember();
566
		logMetacat.debug("Member added to cluster: " + member.getInetSocketAddress());
567
		boolean isLocal = member.localMember();
568
		if (isLocal) {
569
			logMetacat.debug("Member islocal: " + member.getInetSocketAddress());
570
			synchronizeLocalStore();
571
		}
572
	}
573

    
574
	@Override
575
	public void memberRemoved(MembershipEvent event) {
576
		// TODO Auto-generated method stub
577
		
578
	}
579

    
580
	/**
581
	 * In cases where this cluster is paused, we want to 
582
	 * check that the local store accurately reflects the shared 
583
	 * SystemMetadata map
584
	 * @param event
585
	 */
586
	@Override
587
	public void stateChanged(LifecycleEvent event) {
588
		logMetacat.debug("HZ LifecycleEvent.state: " + event.getState());
589
		if (event.getState().equals(LifecycleEvent.LifecycleState.RESUMED)) {
590
			logMetacat.debug("HZ LifecycleEvent.state is RESUMED, calling synchronizeLocalStore()");
591
			synchronizeLocalStore();
592
		}
593
	}
594

    
595
	/**
596
	 * Load all System Metadata keys from the backing store
597
	 * @return set of pids
598
	 */
599
	private Set<Identifier> loadAllKeys() {
600

    
601
		Set<Identifier> pids = new HashSet<Identifier>();
602
		
603
		try {
604
			
605
			// ALTERNATIVE 1: this has more overhead than just looking at the GUIDs
606
//			ObjectList ol = IdentifierManager.getInstance().querySystemMetadata(
607
//					null, //startTime, 
608
//					null, //endTime, 
609
//					null, //objectFormatId, 
610
//					false, //replicaStatus, 
611
//					0, //start, 
612
//					-1 //count
613
//					);
614
//			for (ObjectInfo o: ol.getObjectInfoList()) {
615
//				Identifier pid = o.getIdentifier();
616
//				if ( !pids.contains(pid) ) {
617
//					pids.add(pid);
618
//				}				
619
//			}
620
			
621
			// ALTERNATIVE method: look up all the Identifiers from the table
622
			List<String> guids = IdentifierManager.getInstance().getAllSystemMetadataGUIDs();
623
			logMetacat.warn("Local SystemMetadata pid count: " + guids.size());
624
			for (String guid: guids){
625
				Identifier pid = new Identifier();
626
				pid.setValue(guid);
627
				pids.add(pid);
628
			}
629
			
630
		} catch (Exception e) {
631
			throw new RuntimeException(e.getMessage(), e);
632
			
633
		}
634
		
635
		return pids;
636
	}
637

    
638
	/**
639
	 * Respond to itemAdded events on the hzMissingIdentifiers Set.  Uses a
640
	 * distributed ILock to try to prevent multiple put calls on hzSystemMetadata
641
	 * 
642
	 * @param pid   the identifier of the event
643
	 */
644
	@Override
645
	public void itemAdded(ItemEvent<Identifier> event) {
646
		
647
		Identifier pid = (Identifier) event.getItem();
648
		// publish the SM for the pid if we have it locally
649
		logMetacat.debug("Responding to itemAdded for pid: " + pid.getValue());
650
		
651
		// lock this event, only if we have a local copy to contribute
652
		ILock lock = null;
653
		try {
654
			// look up the local copy of the SM
655
			SystemMetadata sm = IdentifierManager.getInstance().getSystemMetadata(pid.getValue());
656
			if (sm != null) {
657
				lock = hzInstance.getLock(MISSING_PID_PREFIX + pid.getValue());
658
				
659
				if ( lock.tryLock() ) {
660
			        // "publish" the system metadata to the shared map since it showed up on the missing queue
661
			        logMetacat.debug("Adding SystemMetadata to shared map for pid: " + pid.getValue());
662
			        systemMetadata.put(pid, sm);
663
			        
664
			        // remove the entry since we processed it
665
			        missingIdentifiers.remove(pid);
666
			      
667
				  } else {
668
				      logMetacat.debug(MISSING_PID_PREFIX + pid.getValue() + " was already locked. Skipping.");
669
				  }
670
			} else {
671
				// can't help here
672
				logMetacat.warn("Local system metadata not found for pid: " + pid.getValue());
673
			}
674
		} catch (Exception e) {
675
			logMetacat.error("Error looking up missing system metadata for pid: " + pid.getValue());
676
		} finally {
677
			if ( lock != null ) {
678
				lock.unlock();
679
			}
680
        }
681
	}
682

    
683
	/**
684
   * Respond to itemRemoved events on the hzMissingIdentifiers Set
685
   * 
686
   * @param pid   the identifier of the event
687
   */
688
	@Override
689
	public void itemRemoved(ItemEvent<Identifier> event) {
690
		// do nothing since someone probably handled the wanted PID
691
		
692
	}
693

    
694
}
(1-1/3)