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: cjones $'
9
 *     '$Date: 2012-11-10 15:34:51 -0800 (Sat, 10 Nov 2012) $'
10
 * '$Revision: 7421 $'
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.Node;
43
import org.dataone.service.types.v1.NodeReference;
44
import org.dataone.service.types.v1.SystemMetadata;
45

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

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

    
81
  private static final String MISSING_PID_PREFIX = "missing-";
82

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

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

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

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

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

    
185
      // Get a reference to the shared identifiers set as a cluster member
186
      // NOTE: this takes a long time to complete
187
      logMetacat.warn("Retrieving hzIdentifiers from Hazelcast");
188
      identifiers = this.hzInstance.getSet(identifiersSet);
189
      logMetacat.warn("Retrieved hzIdentifiers from Hazelcast");
190
      
191
      // for publishing the "PIDs Wanted" list
192
      missingIdentifiers = this.hzInstance.getSet("hzMissingIdentifiersSet");
193
      
194
      missingIdentifiers.addItemListener(this, true);
195
      
196
      // Listen for changes to the system metadata map
197
      systemMetadata.addEntryListener(this, true);
198
      
199
      // Listen for members added/removed
200
      hzInstance.getCluster().addMembershipListener(this);
201
      
202
      // Listen for lifecycle state changes
203
      hzInstance.getLifecycleService().addLifecycleListener(this);
204
      
205
    } catch (PropertyNotFoundException e) {
206

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

    
243
  /**
244
   * When Metacat changes the underlying store, we need to refresh the
245
   * in-memory representation of it.
246
   * @param guid
247
   */
248
  public void refreshSystemMetadataEntry(String guid) {
249
	Identifier identifier = new Identifier();
250
	identifier.setValue(guid);
251
	// force hazelcast to update system metadata in memory from the store
252
	HazelcastService.getInstance().getSystemMetadataMap().evict(identifier);
253
	
254
  }
255

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

    
302
  public HazelcastInstance getHazelcastInstance() {
303
      return this.hzInstance;
304
      
305
  }
306
  
307
  /**
308
   * Refresh the Hazelcast service by restarting it
309
   */
310
  @Override
311
  protected void doRefresh() throws ServiceException {
312

    
313
    // TODO: verify that the correct config file is still used
314
	  this.hzInstance.getLifecycleService().restart();
315
    
316
  }
317
  
318
  /**
319
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
320
	 * added events in the hzSystemMetadata map. Evaluate the entry and create
321
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
322
	 * 
323
	 * @param event - The EntryEvent that occurred
324
	 */
325
	@Override
326
	public void entryAdded(EntryEvent<Identifier, SystemMetadata> event) {
327
	  
328
	  logMetacat.info("SystemMetadata entry added event on identifier " + 
329
	      event.getKey().getValue());
330
		// handle as update - that method will create if necessary
331
		entryUpdated(event);
332

    
333
	}
334

    
335
	/**
336
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
337
	 * evicted events in the hzSystemMetadata map.  Evaluate the entry and create
338
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
339
	 * 
340
	 * @param event - The EntryEvent that occurred
341
	 */
342
	@Override
343
	public void entryEvicted(EntryEvent<Identifier, SystemMetadata> event) {
344

    
345
      logMetacat.info("SystemMetadata entry evicted event on identifier " + 
346
          event.getKey().getValue());
347
      
348
	    // ensure identifiers are listed in the hzIdentifiers set
349
      if ( !identifiers.contains(event.getKey()) ) {
350
          identifiers.add(event.getKey());
351
      }
352
	  
353
	}
354
	
355
	/**
356
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
357
	 * removed events in the hzSystemMetadata map.  Evaluate the entry and create
358
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
359
	 * 
360
	 * @param event - The EntryEvent that occurred
361
	 */
362
	@Override
363
	public void entryRemoved(EntryEvent<Identifier, SystemMetadata> event) {
364
		
365
    logMetacat.info("SystemMetadata entry removed event on identifier " + 
366
        event.getKey().getValue());
367

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

    
371
    // keep the hzIdentifiers set in sync with the systemmetadata table
372
    if ( identifiers.contains(event.getKey()) ) {
373
        identifiers.remove(event.getKey());
374
        
375
    }
376

    
377
	}
378
	
379
	/**
380
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
381
	 * updated events in the hzSystemMetadata map.  Evaluate the entry and create
382
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
383
	 * 
384
	 * @param event - The EntryEvent that occurred
385
	 */
386
	@Override
387
	public void entryUpdated(EntryEvent<Identifier, SystemMetadata> event) {
388

    
389
		logMetacat.debug("Entry added/updated to System Metadata map: " + event.getKey().getValue());
390
		PartitionService partitionService = this.hzInstance.getPartitionService();
391
		Partition partition = partitionService.getPartition(event.getKey());
392
		Member ownerMember = partition.getOwner();
393
		SystemMetadata sysmeta = event.getValue();
394
		if (!ownerMember.localMember()) {
395
			if (sysmeta == null) {
396
				logMetacat.warn("No SystemMetadata provided in the event, getting from shared map: " + event.getKey().getValue());
397
				sysmeta = getSystemMetadataMap().get(event.getKey());
398
				if (sysmeta == null) {
399
					// this is a problem
400
					logMetacat.error("Could not find SystemMetadata in shared map for: " + event.getKey().getValue());
401
					// TODO: should probably return at this point since the save will fail
402
				}
403
			}
404
			// need to pull the entry into the local store
405
			saveLocally(event.getValue());
406
		}
407

    
408
		// ensure identifiers are listed in the hzIdentifiers set
409
		if (!identifiers.contains(event.getKey())) {
410
			identifiers.add(event.getKey());
411
		}
412

    
413
	}
414
	
415
	/**
416
	 * Save SystemMetadata to local store if needed
417
	 * @param sm
418
	 */
419
	private void saveLocally(SystemMetadata sm) {
420
		logMetacat.debug("Saving entry locally: " + sm.getIdentifier().getValue());
421
		try {
422

    
423
			IdentifierManager.getInstance().insertOrUpdateSystemMetadata(sm);
424

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

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

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

    
545
	/**
546
	 * When there is missing SystemMetadata on the local member,
547
	 * we retrieve it from the shared map and add it to the local
548
	 * backing store for safe keeping.
549
	 */
550
	@Override
551
	public void memberAdded(MembershipEvent event) {
552
		Member member = event.getMember();
553
		logMetacat.debug("Member added to cluster: " + member.getInetSocketAddress());
554
		boolean isLocal = member.localMember();
555
		if (isLocal) {
556
			logMetacat.debug("Member islocal: " + member.getInetSocketAddress());
557
			synchronizeLocalStore();
558
		}
559
	}
560

    
561
	@Override
562
	public void memberRemoved(MembershipEvent event) {
563
		// TODO Auto-generated method stub
564
		
565
	}
566

    
567
	/**
568
	 * In cases where this cluster is paused, we want to 
569
	 * check that the local store accurately reflects the shared 
570
	 * SystemMetadata map
571
	 * @param event
572
	 */
573
	@Override
574
	public void stateChanged(LifecycleEvent event) {
575
		logMetacat.debug("HZ LifecycleEvent.state: " + event.getState());
576
		if (event.getState().equals(LifecycleEvent.LifecycleState.RESUMED)) {
577
			logMetacat.debug("HZ LifecycleEvent.state is RESUMED, calling synchronizeLocalStore()");
578
			synchronizeLocalStore();
579
		}
580
	}
581

    
582
	/**
583
	 * Load all System Metadata keys from the backing store
584
	 * @return set of pids
585
	 */
586
	private Set<Identifier> loadAllKeys() {
587

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

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

    
670
	/**
671
   * Respond to itemRemoved events on the hzMissingIdentifiers Set
672
   * 
673
   * @param pid   the identifier of the event
674
   */
675
	@Override
676
	public void itemRemoved(ItemEvent<Identifier> event) {
677
		// do nothing since someone probably handled the wanted PID
678
		
679
	}
680

    
681
}
(1-1/3)