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: tao $'
9
 *     '$Date: 2015-02-12 15:32:36 -0800 (Thu, 12 Feb 2015) $'
10
 * '$Revision: 9116 $'
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.text.DateFormat;
32
import java.text.SimpleDateFormat;
33
import java.util.Calendar;
34
import java.util.HashSet;
35
import java.util.Iterator;
36
import java.util.List;
37
import java.util.Set;
38
import java.util.concurrent.ExecutorService;
39
import java.util.concurrent.Executors;
40
import java.util.concurrent.locks.Lock;
41

    
42
import org.apache.log4j.Logger;
43
import org.dataone.service.exceptions.InvalidSystemMetadata;
44
import org.dataone.service.types.v1.Identifier;
45
import org.dataone.service.types.v2.SystemMetadata;
46

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

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

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

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

    
178
	this.hzInstance = Hazelcast.newHazelcastInstance(hzConfig);
179
  
180
  	logMetacat.debug("Initialized hzInstance");
181

    
182
    // Get configuration properties on instantiation
183
    try {
184
      systemMetadataMap = 
185
        PropertyService.getProperty("dataone.hazelcast.storageCluster.systemMetadataMap");
186
      identifiersSet = PropertyService.getProperty("dataone.hazelcast.storageCluster.identifiersSet");
187

    
188
      // Get a reference to the shared system metadata map as a cluster member
189
      // NOTE: this loads the map from the backing store and can take a long time for large collections
190
      systemMetadata = this.hzInstance.getMap(systemMetadataMap);
191
      
192
      logMetacat.debug("Initialized systemMetadata");
193

    
194
      // Get a reference to the shared identifiers set as a cluster member
195
      // NOTE: this takes a long time to complete
196
      logMetacat.warn("Retrieving hzIdentifiers from Hazelcast");
197
      identifiers = this.hzInstance.getSet(identifiersSet);
198
      logMetacat.warn("Retrieved hzIdentifiers from Hazelcast");
199
      
200
      // for publishing the "PIDs Wanted" list
201
      missingIdentifiers = this.hzInstance.getSet("hzMissingIdentifiersSet");
202
      
203
      missingIdentifiers.addItemListener(this, true);
204

    
205
      // for index tasks
206
      hzIndexQueue = PropertyService.getProperty("index.hazelcast.indexqueue");
207
      indexQueue = this.hzInstance.getMap(hzIndexQueue);
208

    
209
      // for index events (failures)
210
      hzIndexEventMap = PropertyService.getProperty("index.hazelcast.indexeventmap");
211
      indexEventMap = this.hzInstance.getMap(hzIndexEventMap);
212
      
213
      // Listen for changes to the system metadata map
214
      systemMetadata.addEntryListener(this, true);
215
      
216
      // Listen for members added/removed
217
      hzInstance.getCluster().addMembershipListener(this);
218
      
219
      // Listen for lifecycle state changes
220
      hzInstance.getLifecycleService().addLifecycleListener(this);
221
      
222
    } catch (PropertyNotFoundException e) {
223

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

    
260
  /**
261
   * Get the index queue
262
   * @return the set of SystemMetadata to be indexed
263
   */
264
  public IMap<Identifier, IndexTask> getIndexQueue() {
265
      return indexQueue;
266
  }
267
  
268
  /**
269
   * Get the index event map
270
   * @return indexEventMap - the hazelcast map of index events
271
   */
272
  public IMap<Identifier, IndexEvent> getIndexEventMap() {
273
	  return indexEventMap;
274
  }
275
  
276
  /**
277
   * When Metacat changes the underlying store, we need to refresh the
278
   * in-memory representation of it.
279
   * @param guid
280
   */
281
  public void refreshSystemMetadataEntry(String guid) {
282
	Identifier identifier = new Identifier();
283
	identifier.setValue(guid);
284
	// force hazelcast to update system metadata in memory from the store
285
	HazelcastService.getInstance().getSystemMetadataMap().evict(identifier);
286
  }
287

    
288
  public Lock getLock(String identifier) {
289
    
290
    Lock lock = null;
291
    
292
    try {
293
        lock = getInstance().getHazelcastInstance().getLock(identifier);
294
        
295
    } catch (RuntimeException e) {
296
        logMetacat.info("Couldn't get a lock for identifier " + 
297
            identifier + " !!");
298
    }
299
    return lock;
300
      
301
  }
302
  
303
  /**
304
   * Get the DataONE hazelcast node map
305
   * @return nodes - the hazelcast map of nodes
306
   */
307
//  public IMap<NodeReference, Node> getNodesMap() {
308
//	  return nodes;
309
//  }
310
  
311
  /**
312
   * Indicate whether or not this service is refreshable.
313
   *
314
   * @return refreshable - the boolean refreshable status
315
   */
316
  public boolean refreshable() {
317
    // TODO: Determine the consequences of restarting the Hazelcast instance
318
    // Set this to true if it's okay to drop from the cluster, lose the maps,
319
    // and start back up again
320
    return false;
321
    
322
  }
323
  
324
  /**
325
   * Stop the HazelcastService. When stopped, the service will no longer
326
   * respond to requests.
327
   */
328
  public void stop() throws ServiceException {
329
    
330
	  this.hzInstance.getLifecycleService().shutdown();
331
    
332
  }
333

    
334
  public HazelcastInstance getHazelcastInstance() {
335
      return this.hzInstance;
336
      
337
  }
338
  
339
  /**
340
   * Refresh the Hazelcast service by restarting it
341
   */
342
  @Override
343
  protected void doRefresh() throws ServiceException {
344

    
345
    // TODO: verify that the correct config file is still used
346
	  this.hzInstance.getLifecycleService().restart();
347
    
348
  }
349
  
350
  /**
351
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
352
	 * added events in the hzSystemMetadata map. Evaluate the entry and create
353
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
354
	 * 
355
	 * @param event - The EntryEvent that occurred
356
	 */
357
	@Override
358
	public void entryAdded(EntryEvent<Identifier, SystemMetadata> event) {
359
	  
360
	  logMetacat.info("SystemMetadata entry added event on identifier " + 
361
	      event.getKey().getValue());
362
		// handle as update - that method will create if necessary
363
		entryUpdated(event);
364

    
365
	}
366

    
367
	/**
368
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
369
	 * evicted events in the hzSystemMetadata map.  Evaluate the entry and create
370
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
371
	 * 
372
	 * @param event - The EntryEvent that occurred
373
	 */
374
	@Override
375
	public void entryEvicted(EntryEvent<Identifier, SystemMetadata> event) {
376

    
377
      logMetacat.info("SystemMetadata entry evicted event on identifier " + 
378
          event.getKey().getValue());
379
      
380
	    // ensure identifiers are listed in the hzIdentifiers set
381
      if ( !identifiers.contains(event.getKey()) ) {
382
          identifiers.add(event.getKey());
383
      }
384
	  
385
	}
386
	
387
	/**
388
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
389
	 * removed events in the hzSystemMetadata map.  Evaluate the entry and create
390
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
391
	 * 
392
	 * @param event - The EntryEvent that occurred
393
	 */
394
	@Override
395
	public void entryRemoved(EntryEvent<Identifier, SystemMetadata> event) {
396
		
397
    logMetacat.info("SystemMetadata entry removed event on identifier " + 
398
        event.getKey().getValue());
399

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

    
403
    // keep the hzIdentifiers set in sync with the systemmetadata table
404
    if ( identifiers.contains(event.getKey()) ) {
405
        identifiers.remove(event.getKey());
406
        
407
    }
408

    
409
	}
410
	
411
	/**
412
	 * Implement the EntryListener interface for Hazelcast, reponding to entry
413
	 * updated events in the hzSystemMetadata map.  Evaluate the entry and create
414
	 * CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
415
	 * 
416
	 * @param event - The EntryEvent that occurred
417
	 */
418
	@Override
419
	public void entryUpdated(EntryEvent<Identifier, SystemMetadata> event) {
420

    
421
		logMetacat.debug("Entry added/updated to System Metadata map: " + event.getKey().getValue());
422
		PartitionService partitionService = this.hzInstance.getPartitionService();
423
		Partition partition = partitionService.getPartition(event.getKey());
424
		Member ownerMember = partition.getOwner();
425
		SystemMetadata sysmeta = event.getValue();
426
		if (!ownerMember.localMember()) {
427
			if (sysmeta == null) {
428
				logMetacat.warn("No SystemMetadata provided in the event, getting from shared map: " + event.getKey().getValue());
429
				sysmeta = getSystemMetadataMap().get(event.getKey());
430
				if (sysmeta == null) {
431
					// this is a problem
432
					logMetacat.error("Could not find SystemMetadata in shared map for: " + event.getKey().getValue());
433
					// TODO: should probably return at this point since the save will fail
434
				}
435
			}
436
			// need to pull the entry into the local store
437
			saveLocally(event.getValue());
438
		}
439

    
440
		// ensure identifiers are listed in the hzIdentifiers set
441
		if (!identifiers.contains(event.getKey())) {
442
			identifiers.add(event.getKey());
443
		}
444

    
445
	}
446
	
447
	/**
448
	 * Save SystemMetadata to local store if needed
449
	 * @param sm
450
	 */
451
	private void saveLocally(SystemMetadata sm) {
452
		logMetacat.debug("Saving entry locally: " + sm.getIdentifier().getValue());
453
		try {
454

    
455
			IdentifierManager.getInstance().insertOrUpdateSystemMetadata(sm);
456

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

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

    
537
		//identifiers.addAll(idKeys);
538
		logMetacat.warn("Loading missing local keys into hzIdentifiers");
539
		for (Identifier key: localIdKeys) {
540
			if (!identifiers.contains(key)) {
541
				logMetacat.debug("Adding missing hzIdentifiers key: " + key.getValue());
542
				identifiers.add(key);
543
			}
544
		}
545
		logMetacat.warn("Initialized identifiers with missing local keys");
546
		
547
		logMetacat.warn("Processing missing SystemMetadata for missing pid count: " + missingIdKeys.size());
548
		
549
		// loop through all the missing PIDs to find any null (missing) SM that needs to be resynched
550
		Iterator<Identifier> missingPids = missingIdKeys.iterator();
551
		while (missingPids.hasNext()) {
552
			Identifier pid = missingPids.next();
553
			// publish that we need this SM entry
554
			logMetacat.debug("Publishing missing pid to wanted list: " + pid.getValue());
555
			missingIdentifiers.add(pid);
556
		}
557
		
558
	}
559
	
560
	public void resynchInThread() {
561
		logMetacat.debug("launching system metadata resynch in a thread");
562
		ExecutorService executor = Executors.newSingleThreadExecutor();
563
		executor.execute(new Runnable() {
564
			@Override
565
			public void run() {
566
				try {
567
					// this is a push mechanism
568
				    DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss aaa");
569
				    System.out.println(dateFormat.format(Calendar.getInstance().getTime())+" Start the hazelcast synchronization");
570
				    logMetacat.warn("Start the hazelcast synchronization");
571
					resynchToRemote();
572
					System.out.println(dateFormat.format(Calendar.getInstance().getTime())+" End the hazelcast synchronization");
573
					logMetacat.warn("End the hazelcast synchronization");
574
				} catch (Exception e) {
575
					logMetacat.error("Error in resynchInThread: " + e.getMessage(), e);
576
				}
577
			}
578
		});
579
		executor.shutdown();
580
	}
581

    
582
	/**
583
	 * When there is missing SystemMetadata on the local member,
584
	 * we retrieve it from the shared map and add it to the local
585
	 * backing store for safe keeping.
586
	 */
587
	@Override
588
	public void memberAdded(MembershipEvent event) {
589
		Member member = event.getMember();
590
		logMetacat.debug("Member added to cluster: " + member.getInetSocketAddress());
591
		boolean isLocal = member.localMember();
592
		if (isLocal) {
593
			logMetacat.debug("Member islocal: " + member.getInetSocketAddress());
594
			synchronizeLocalStore();
595
		}
596
	}
597

    
598
	@Override
599
	public void memberRemoved(MembershipEvent event) {
600
		// TODO Auto-generated method stub
601
		
602
	}
603

    
604
	/**
605
	 * In cases where this cluster is paused, we want to 
606
	 * check that the local store accurately reflects the shared 
607
	 * SystemMetadata map
608
	 * @param event
609
	 */
610
	@Override
611
	public void stateChanged(LifecycleEvent event) {
612
		logMetacat.debug("HZ LifecycleEvent.state: " + event.getState());
613
		if (event.getState().equals(LifecycleEvent.LifecycleState.RESUMED)) {
614
			logMetacat.debug("HZ LifecycleEvent.state is RESUMED, calling synchronizeLocalStore()");
615
			synchronizeLocalStore();
616
		}
617
	}
618

    
619
	/**
620
	 * Load all System Metadata keys from the backing store
621
	 * @return set of pids
622
	 */
623
	private Set<Identifier> loadAllKeys() {
624

    
625
		Set<Identifier> pids = new HashSet<Identifier>();
626
		
627
		try {
628
			
629
			// ALTERNATIVE 1: this has more overhead than just looking at the GUIDs
630
//			ObjectList ol = IdentifierManager.getInstance().querySystemMetadata(
631
//					null, //startTime, 
632
//					null, //endTime, 
633
//					null, //objectFormatId, 
634
//					false, //replicaStatus, 
635
//					0, //start, 
636
//					-1 //count
637
//					);
638
//			for (ObjectInfo o: ol.getObjectInfoList()) {
639
//				Identifier pid = o.getIdentifier();
640
//				if ( !pids.contains(pid) ) {
641
//					pids.add(pid);
642
//				}				
643
//			}
644
			
645
			// ALTERNATIVE method: look up all the Identifiers from the table
646
			List<String> guids = IdentifierManager.getInstance().getAllSystemMetadataGUIDs();
647
			logMetacat.warn("Local SystemMetadata pid count: " + guids.size());
648
			for (String guid: guids){
649
				Identifier pid = new Identifier();
650
				pid.setValue(guid);
651
				pids.add(pid);
652
			}
653
			
654
		} catch (Exception e) {
655
			throw new RuntimeException(e.getMessage(), e);
656
			
657
		}
658
		
659
		return pids;
660
	}
661

    
662
	/**
663
	 * Respond to itemAdded events on the hzMissingIdentifiers Set.  Uses a
664
	 * distributed ILock to try to prevent multiple put calls on hzSystemMetadata
665
	 * 
666
	 * @param pid   the identifier of the event
667
	 */
668
	@Override
669
	public void itemAdded(ItemEvent<Identifier> event) {
670
		
671
		Identifier pid = (Identifier) event.getItem();
672
		// publish the SM for the pid if we have it locally
673
		logMetacat.debug("Responding to itemAdded for pid: " + pid.getValue());
674
		
675
		// lock this event, only if we have a local copy to contribute
676
		ILock lock = null;
677
		try {
678
			// look up the local copy of the SM
679
			SystemMetadata sm = IdentifierManager.getInstance().getSystemMetadata(pid.getValue());
680
			if (sm != null) {
681
				lock = hzInstance.getLock(MISSING_PID_PREFIX + pid.getValue());
682
				
683
				if ( lock.tryLock() ) {
684
			        // "publish" the system metadata to the shared map since it showed up on the missing queue
685
			        logMetacat.debug("Adding SystemMetadata to shared map for pid: " + pid.getValue());
686
			        systemMetadata.put(pid, sm);
687
			        
688
			        // remove the entry since we processed it
689
			        missingIdentifiers.remove(pid);
690
			      
691
				  } else {
692
				      logMetacat.debug(MISSING_PID_PREFIX + pid.getValue() + " was already locked. Skipping.");
693
				  }
694
			} else {
695
				// can't help here
696
				logMetacat.warn("Local system metadata not found for pid: " + pid.getValue());
697
			}
698
		} catch (Exception e) {
699
			logMetacat.error("Error looking up missing system metadata for pid: " + pid.getValue());
700
		} finally {
701
			if ( lock != null ) {
702
				lock.unlock();
703
			}
704
        }
705
	}
706

    
707
	/**
708
   * Respond to itemRemoved events on the hzMissingIdentifiers Set
709
   * 
710
   * @param pid   the identifier of the event
711
   */
712
	@Override
713
	public void itemRemoved(ItemEvent<Identifier> event) {
714
		// do nothing since someone probably handled the wanted PID
715
		
716
	}
717

    
718
}
(1-1/3)