Project

General

Profile

« Previous | Next » 

Revision 6567

Modify CNodeService's updateSystemMetadata(), setReplicationStatus(), setReplicationPolicy(), and setOwner() with support for SystemMetadata's serialVersion field. Other methods still pending an update. Use the hzSystemMetadata map for all system metadata reads using a lock on the pid in order to get the very latest version.

View differences:

src/edu/ucsb/nceas/metacat/dataone/CNodeService.java
23 23

  
24 24
package edu.ucsb.nceas.metacat.dataone;
25 25

  
26
import java.math.BigInteger;
27
import java.util.Calendar;
26 28
import java.util.Date;
27 29
import java.util.List;
28 30
import java.util.Set;
......
116 118
   */
117 119
  @Override
118 120
  public boolean setReplicationPolicy(Session session, Identifier pid,
119
    ReplicationPolicy policy) 
120
    throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, InvalidRequest, InvalidToken {
121

  
122
    // get the subject
123
    Subject subject = session.getSubject();
124
    // get the system metadata
125
    String guid = pid.getValue();
126
    
127
    // are we allowed to do this?
128
    if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
129
      throw new NotAuthorized("4881", Permission.CHANGE_PERMISSION + " not allowed by " + subject.getValue() + " on " + guid);  
130
    }
131
    
132
    SystemMetadata systemMetadata = null;
133
    try {
134
      systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
135
    } catch (McdbDocNotFoundException e) {
136
      throw new NotFound("4884", "No record found for: " + guid);
137
    }
121
      ReplicationPolicy policy) 
122
      throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, 
123
      InvalidRequest, InvalidToken {
124
      
125
      // get the subject
126
      Subject subject = session.getSubject();
127
      
128
      // are we allowed to do this?
129
      if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
130
        throw new NotAuthorized("4881", Permission.CHANGE_PERMISSION + 
131
            " not allowed by " + subject.getValue() + " on " + pid.getValue());  
132
      }
133
      
134
      SystemMetadata systemMetadata = null;
135
      try {
136
          HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
137
          systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
138 138
        
139
    // set the new policy
140
    systemMetadata.setReplicationPolicy(policy);
139
      } catch (Exception e) { // Catch is generic since HZ throws RuntimeException
140
        throw new NotFound("4884", "No record found for: " + pid.getValue());
141
        
142
      }
143
          
144
      // set the new policy
145
      systemMetadata.setReplicationPolicy(policy);
146
      
147
      // update the metadata
148
      try {
149
        systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
150
        systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
151
	      HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
152
	      HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
153
	      
154
      } catch (Exception e) {
155
		      throw new ServiceFailure("4882", e.getMessage());
156
		  
157
	    } finally {
158
	        HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
159
	        
160
	    }
141 161
    
142
    // update the metadata
143
    try {
144
	    HazelcastService.getInstance().getSystemMetadataMap().lock(systemMetadata.getIdentifier());
145
	    HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
146
	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
147
    } catch (Exception e) {
148
		throw new ServiceFailure("4882", e.getMessage());
149
	} finally {
150
	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
151
	}
152
    
153
    return true;
162
      return true;
154 163
  }
155 164

  
156 165
  /**
......
178 187

  
179 188
    // get the subject
180 189
    Subject subject = session.getSubject();
181
    // get the system metadata
182
    String guid = pid.getValue();
183 190
    
184 191
    // are we allowed to do this?
185 192
    if (!isAuthorized(session, pid, Permission.WRITE)) {
186
      throw new NotAuthorized("4720", Permission.WRITE + " not allowed by " + subject.getValue() + " on " + guid);  
193
      throw new NotAuthorized("4720", Permission.WRITE + " not allowed by " + 
194
          subject.getValue() + " on " + pid.getValue());  
187 195
    }
188 196
    
189 197
    SystemMetadata systemMetadata = null;
190
    try {
191
      systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
192
    } catch (McdbDocNotFoundException e) {
193
      throw new NotFound("4740", "No record found for: " + guid);
198
    try {      
199
        HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
200
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
201

  
202
    } catch (Exception e) { // Catch is generic since HZ throws RuntimeException
203
      throw new NotFound("4740", "No record found for: " + pid.getValue() +
204
          " : " + e.getMessage());
205
      
194 206
    }
195 207
        
196
    // set the status for each replica
197
    // TODO: should this method select a certain replica?
208
    // set the status for the replica
198 209
    List<Replica> replicas = systemMetadata.getReplicaList();
199 210
    for (Replica replica: replicas) {
200
      replica.setReplicationStatus(status);
211
        if (replica.getReplicaMemberNode().getValue().equals(targetNode.getValue())) {
212
            replica.setReplicationStatus(status);
213
            
214
        }
201 215
    }
202 216
    
203 217
    // [re]set the list -- redundant?
......
205 219
    
206 220
    // update the metadata
207 221
    try {
208
	    HazelcastService.getInstance().getSystemMetadataMap().lock(systemMetadata.getIdentifier());
209
	    HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
210
	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
222
        systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
223
        systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
224
	      HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
225
	      HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
226
	    
211 227
    } catch (Exception e) {
212
		throw new ServiceFailure("4700", e.getMessage());
213
	} finally {
214
	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
215
	}
228
		    throw new ServiceFailure("4700", e.getMessage());
229
		
230
	  } finally {
231
	      HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
232
	      
233
	  }
216 234
	
217 235
    return true;
218 236
  }
......
575 593
   */
576 594
  @Override
577 595
  public boolean updateSystemMetadata(Session session, Identifier guid,
578
    SystemMetadata sysmeta) 
579
    throws NotImplemented, NotAuthorized, ServiceFailure, InvalidRequest, 
580
    InvalidSystemMetadata, NotFound {
596
      SystemMetadata sysmeta) 
597
      throws NotImplemented, NotAuthorized, ServiceFailure, InvalidRequest, 
598
      InvalidSystemMetadata, NotFound {
581 599

  
582
    // TODO: control who can call this?
583
        if (session == null) {
584
            //TODO: many of the thrown exceptions do not use the correct error codes
585
            //check these against the docs and correct them
586
            throw new NotAuthorized("4861", "No Session - could not authorize for update." +
587
                    "  If you are not logged in, please do so and retry the request.");
588
        }
589
        
590
        // verify that guid == SystemMetadata.getIdentifier()
591
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
592
        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
593
            throw new InvalidRequest("4863", 
594
                "GUID in method call (" + guid.getValue() + ") does not match GUID in system metadata (" +
595
                sysmeta.getIdentifier().getValue() + ").");
596
        }
600
      // TODO: control who can call this?
601
      if (session == null) {
602
          //TODO: many of the thrown exceptions do not use the correct error codes
603
          //check these against the docs and correct them
604
          throw new NotAuthorized("4861", "No Session - could not authorize for update." +
605
                  "  If you are not logged in, please do so and retry the request.");
606
      }
607
      
608
      // verify that guid == SystemMetadata.getIdentifier()
609
      logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + 
610
          sysmeta.getIdentifier().getValue());
611
      if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
612
          throw new InvalidRequest("4863", 
613
              "GUID in method call (" + guid.getValue() + 
614
              ") does not match GUID in system metadata (" +
615
              sysmeta.getIdentifier().getValue() + ").");
616
      }
597 617

  
598
        logMetacat.debug("Checking if identifier exists...");
599
        // Check that the identifier exists
600
        if (!IdentifierManager.getInstance().identifierExists(guid.getValue())) {
601
            throw new NotFound("000", 
602
                "GUID does not exist");
603
        }
618
      logMetacat.debug("Checking if identifier exists...");
619
      // Check that the identifier exists
620
      if (!IdentifierManager.getInstance().identifierExists(guid.getValue())) {
621
          throw new NotFound("000", 
622
              "GUID does not exist");
623
      }
604 624

  
605
        // update the system metadata into the object store
606
        logMetacat.debug("Starting to update SystemMetadata...");
607
        sysmeta.setDateSysMetadataModified(new Date());
608
        
609
        // update system metadata
610
        try {
625
      // update the system metadata into the object store
626
      logMetacat.debug("Starting to update SystemMetadata...");
627
      
628
      // update system metadata
629
      try {
611 630
    	    HazelcastService.getInstance().getSystemMetadataMap().lock(sysmeta.getIdentifier());
612
    	    HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
631
          SystemMetadata currentSysMeta = 
632
              HazelcastService.getInstance().getSystemMetadataMap().get(sysmeta.getIdentifier());
633
          sysmeta.setSerialVersion(currentSysMeta.getSerialVersion().add(BigInteger.ONE));
634
          sysmeta.setDateSysMetadataModified(Calendar.getInstance().getTime());
635
          HazelcastService.getInstance().getSystemMetadataMap().put(sysmeta.getIdentifier(), sysmeta);
613 636
    	    HazelcastService.getInstance().getSystemMetadataMap().unlock(sysmeta.getIdentifier());
614
        } catch (Exception e) {
615
    		throw new ServiceFailure("4852", e.getMessage());
637
    	    
638
      } catch (Exception e) {
639
    	throw new ServiceFailure("4852", e.getMessage());
640
    	
616 641
    	} finally {
617 642
    	    HazelcastService.getInstance().getSystemMetadataMap().unlock(sysmeta.getIdentifier());
643
    	  
618 644
    	}
619
        
620
        logMetacat.debug("Returning from updateSystemMetadata");
621
        EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), session.getSubject().getValue(), guid.getValue(), "updateSystemMetadata");
622
        return true;
645
      
646
      logMetacat.debug("Returning from updateSystemMetadata");
647
      EventLog.getInstance().log(request.getRemoteAddr(), 
648
                                 request.getHeader("User-Agent"), 
649
                                 session.getSubject().getValue(), 
650
                                 guid.getValue(), "updateSystemMetadata");
651
      return true;
623 652
  }
624 653
  
625 654
  /**
......
713 742
    
714 743
    // get the subject
715 744
    Subject subject = session.getSubject();
716
    // get the system metadata
717
    String guid = pid.getValue();
718 745
    
719 746
    // are we allowed to do this?
720 747
    if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
721
      throw new NotAuthorized("4440", "not allowed by " + subject.getValue() + " on " + guid);  
748
      throw new NotAuthorized("4440", "not allowed by " + subject.getValue() + " on " + pid.getValue());  
722 749
    }
723 750
    
724 751
    SystemMetadata systemMetadata = null;
725 752
    try {
726
      systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
727
    } catch (McdbDocNotFoundException e) {
728
      throw new NotFound("4460", "No record found for: " + guid);
753
        HazelcastService.getInstance().getSystemMetadataMap().lock(pid);
754
        systemMetadata = HazelcastService.getInstance().getSystemMetadataMap().get(pid);
755
        
756
    } catch (Exception e) { // Catch is generic since HZ throws RuntimeException
757
        throw new NotFound("4460", "No record found for: " + pid.getValue());
758
        
729 759
    }
730 760
        
731 761
    // set the new rights holder
......
733 763
    
734 764
    // update the metadata
735 765
    try {
736
	    HazelcastService.getInstance().getSystemMetadataMap().lock(systemMetadata.getIdentifier());
737
	    HazelcastService.getInstance().getSystemMetadataMap().put(systemMetadata.getIdentifier(), systemMetadata);
738
	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
766
        systemMetadata.setSerialVersion(systemMetadata.getSerialVersion().add(BigInteger.ONE));
767
        systemMetadata.setDateSysMetadataModified(Calendar.getInstance().getTime());
768
	      HazelcastService.getInstance().getSystemMetadataMap().put(pid, systemMetadata);
769
	      HazelcastService.getInstance().getSystemMetadataMap().unlock(pid);
770
	      
739 771
    } catch (Exception e) {
740 772
		throw new ServiceFailure("4490", e.getMessage());
741
	} finally {
742
	    HazelcastService.getInstance().getSystemMetadataMap().unlock(systemMetadata.getIdentifier());
743
	}
773
		
774
	  } finally {
775
	      HazelcastService.getInstance().getSystemMetadataMap().unlock(pid);
776
	  }
744 777
    
745 778
    return pid;
746 779
  }

Also available in: Unified diff