Project

General

Profile

« Previous | Next » 

Revision 6459

lookup latest system metadata update date for use in synchronizing CN-CN when an offline nodes comes back online

View differences:

src/edu/ucsb/nceas/metacat/dataone/hazelcast/HazelcastService.java
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
}
src/edu/ucsb/nceas/metacat/IdentifierManager.java
770 770
        return ids;
771 771
    }
772 772
    
773
    /**
774
     * returns a list of system metadata-only guids since the given date
775
     * @return a list of system ids in metacat that do not correspond to objects
776
     * TODO: need to check which server they are on
777
     */
778
    public Date getLastModifiedDate() throws Exception {
779
        Date maxDate = null;
773 780

  
781
        List<String> ids = new Vector<String>();
782
        String sql = 
783
        	"select max(date_modified) from " + TYPE_SYSTEM_METADATA;
784
        DBConnection dbConn = null;
785
        int serialNumber = -1;
786
        try 
787
        {
788
            // Get a database connection from the pool
789
            dbConn = DBConnectionPool.getDBConnection("IdentifierManager.getLastModifiedDate");
790
            serialNumber = dbConn.getCheckOutSerialNumber();
791

  
792
            // Execute the insert statement
793
            PreparedStatement stmt = dbConn.prepareStatement(sql);
794
            ResultSet rs = stmt.executeQuery();
795
            if (rs.next()) {
796
            	maxDate = rs.getDate(1);
797
            } 
798
            stmt.close();
799
        } 
800
        catch (SQLException e) 
801
        {
802
            logMetacat.error("Error while looking up the latest update date: " 
803
                    + e.getMessage());
804
        } 
805
        finally 
806
        {
807
            // Return database connection to the pool
808
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
809
        }
810
        return maxDate;
811
    }
812

  
774 813
    
775 814
    /**
776 815
     * Determine if an identifier exists already, returning true if so.

Also available in: Unified diff