Project

General

Profile

« Previous | Next » 

Revision 6455

move CNReplicationTask to the hazelcast package

View differences:

src/edu/ucsb/nceas/metacat/dataone/CNReplicationTask.java
1
/**
2
 * This work was created by participants in the DataONE project, and is
3
 * jointly copyrighted by participating institutions in DataONE. For
4
 * more information on DataONE, see our web site at http://dataone.org.
5
 *
6
 *   Copyright ${year}
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

  
21
package edu.ucsb.nceas.metacat.dataone;
22

  
23
import java.io.InputStream;
24
import java.io.Serializable;
25
import java.util.concurrent.Callable;
26

  
27
import org.dataone.client.D1Client;
28
import org.dataone.configuration.Settings;
29
import org.dataone.service.exceptions.NotFound;
30
import org.dataone.service.types.v1.Identifier;
31
import org.dataone.service.types.v1.Node;
32
import org.dataone.service.types.v1.Permission;
33
import org.dataone.service.types.v1.SystemMetadata;
34

  
35
import com.hazelcast.core.Hazelcast;
36
import com.hazelcast.core.IMap;
37

  
38
import edu.ucsb.nceas.metacat.IdentifierManager;
39
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
40

  
41
/**
42
 * A single CN replication task to be executed by the CN Replication Service. This 
43
 * applies to replication of system metadata, science metadata, and resource maps 
44
 * across CNs.
45
 * 
46
 * The task is built when a change occurs in the hzSystemMetadata map in the
47
 * storage cluster initiated by Metacat for a given PID. If the change involves
48
 * only modifications to system metadata, those changes will occur in the shared
49
 * map and in the backing store of the member that owns the map entry. Other 
50
 * members must be updated using this task.  Likewise, changes to the system
51
 * metadata map that involve science metadata creation or changes will be
52
 * distributed to all members via this task.  The same is for OAI-ORE resource maps.
53
 * 
54
 * @author cjones
55
 *
56
 */
57
public class CNReplicationTask implements Serializable, Callable<Identifier> {
58

  
59
  /* The identifier of this task */
60
  private String taskid;
61
  
62
  /* The identifier of the object to replicate */
63
  private Identifier pid;
64
  
65
  /* The object format type stated in the system metadata (DATA/METADATA/RESOURCE) */
66
  private String formatType;
67
  
68
  /* The target Node object */
69
  private Node targetNode;
70
  
71
  /* The originating Node object */
72
  private Node originatingNode;
73

  
74
  /* The subject of the target node, extracted from the Node object */
75
  private String targetNodeSubject;
76
  
77
  /* The subject of the originating node, extracted from the Node object */
78
  private String originatingNodeSubject;
79
  
80
  /* The permission to be executed (in this case, always 'replicate') */
81
  String permission;
82

  
83
  /**
84
   * Constructor - create an empty replication task instance
85
   */
86
  public CNReplicationTask() {
87
  }
88

  
89
  /**
90
   * Constructor - create a replication task instance
91
   * 
92
   * @param taskid
93
   * @param pid
94
   * @param targetNode
95
   */
96
  public CNReplicationTask(String taskid, Identifier pid, String formatType,
97
    Node originatingNode, Node targetNode,
98
    Permission replicatePermission) {
99
    
100
    this.taskid = taskid;
101
    this.pid = pid;
102
    this.formatType = formatType;
103
    this.originatingNode = originatingNode;
104
    this.targetNode = targetNode;
105
    this.originatingNodeSubject = originatingNode.getSubject(0).getValue();
106
    this.targetNodeSubject = targetNode.getSubject(0).getValue();
107
    this.permission = replicatePermission.name();
108
    
109
  }
110

  
111
  /**
112
   * Get the task identifier for this task
113
   * @return the taskid
114
   */
115
  public String getTaskid() {
116
    return taskid;
117
  }
118

  
119
  /**
120
   * Set the task identifier for this task
121
   * @param taskid the taskid to set
122
   */
123
  public void setTaskid(String taskid) {
124
    this.taskid = taskid;
125
  }
126

  
127
  /**
128
   * Get the object identifier to be replicated
129
   * @return the pid
130
   */
131
  public Identifier getPid() {
132
    return pid;
133
  }
134

  
135
  /**
136
   * Set the object identifier to be replicated
137
   * @param pid the pid to set
138
   */
139
  public void setPid(Identifier pid) {
140
    this.pid = pid;
141
  }
142

  
143

  
144
  /**
145
   * Get the format type of the object to be replicated
146
   * @return the pid
147
   */
148
  public String getFormatType() {
149
    return this.formatType;
150
    
151
  }
152

  
153
  /**
154
   * Set the format type of the object to be replicated
155
   * @param pid the pid to set
156
   */
157
  public void setFormatType(String formatType) {
158
    this.formatType = formatType;
159
    
160
  }
161

  
162
  /**
163
   * Get the target node
164
   * @return the targetNode
165
   */
166
  public Node getTargetNode() {
167
    return targetNode;
168
  }
169

  
170
  /**
171
   * Set the target node
172
   * @param targetNode the targetNode to set
173
   */
174
  public void setTargetNode(Node targetNode) {
175
    this.targetNode = targetNode;
176
  }
177

  
178
  /**
179
   * Get the originating node
180
   * @return the originatingNode
181
   */
182
  public Node getOriginatingNode() {
183
    return originatingNode;
184
  }
185

  
186
  /**
187
   * Set the originating node
188
   * @param originatingNode the originatingNode to set
189
   */
190
  public void setOriginatingNode(Node originatingNode) {
191
    this.originatingNode = originatingNode;
192
  }
193

  
194
  /**
195
   * For the given Replication task, return the Subject listed in the target
196
   * node.  Usually used in authorizing a replication event.
197
   * 
198
   * @return subject - the subject listed in the target Node object as a string
199
   */
200
  public String getTargetNodeSubject() {
201
    
202
    return this.targetNodeSubject;
203
    
204
  }
205
  
206
  /**
207
   * Set the target node subject identifying the node
208
   * @param subject the targetNode subject
209
   */
210
  public void setTargetNodeSubject(String subject) {
211
    this.targetNodeSubject = subject;
212
  }
213
  
214
  /**
215
   * For the given Replication task, return the Subject listed in the target
216
   * node.  Usually used in authorizing a replication event.
217
   * 
218
   * @return subject - the subject listed in the target Node object as a string
219
   */
220
  public String getOriginatingNodeSubject() {
221
    
222
    return this.originatingNodeSubject;
223
    
224
  }
225
  
226
  /**
227
   * Set the target node subject identifying the node
228
   * @param subject the targetNode subject
229
   */
230
  public void setOriginatingNodeSubject(String subject) {
231
    this.originatingNodeSubject = subject;
232
  }
233
  
234
  /**
235
   * Get the permission being allowed for this task
236
   * 
237
   * @return subject - the subject listed in the target Node object
238
   */
239
  public String getPermission() {
240
    return this.permission;
241
    
242
  }
243
  
244
  /**
245
   * Set the permission being allowed for this task
246
   * @param subject the targetNode subject
247
   */
248
  public void setPermission(Permission permission) {
249
    this.permission = permission.name();
250
    
251
  }
252
 
253
  /**
254
   * Implement the Callable interface, providing code that initiates replication.
255
   * 
256
   * @return pid - the identifier of the replicated object upon success
257
   */
258
  public Identifier call() {
259
		
260
	// Get the D1 Hazelcast configuration parameters
261
	String hzSystemMetadata = 
262
		Settings.getConfiguration().getString("dataone.hazelcast.systemMetadata");
263
	
264
	// get the system metadata for the pid	
265
	IMap<Identifier, SystemMetadata> sysMetaMap = Hazelcast.getMap(hzSystemMetadata);
266
	
267
	// get the systemMetadata
268
	SystemMetadata sm = sysMetaMap.get(pid);
269
	
270
	
271
  	
272
  	// only system metadata - no data replicated
273
  	boolean isData = formatType.equals("??");
274
  	if (!isData) {
275
  		// TODO: get the science metadata/ORE from somewhere
276
  		InputStream sciMetaORE = null;
277
		try {
278
			sciMetaORE = CNodeService.getInstance().get(null, pid);
279
		} catch (NotFound nf) {
280
			try {
281
				sciMetaORE = D1Client.getCN().get(null, pid);
282
				// save it locally
283
		  		CNodeService.getInstance().create(null, pid, sciMetaORE, sm);
284
			} catch (Exception e) {
285
				// TODO Auto-generated catch block
286
				e.printStackTrace();
287
			}
288
		} catch (Exception e) {
289
			e.printStackTrace();
290
		}
291
  	} else {
292
  		// just system metadata
293
  		try {
294
  			if (!IdentifierManager.getInstance().identifierExists(pid.getValue())) {
295
  				IdentifierManager.getInstance().createSystemMetadata(sm);
296
  			} else {
297
  				IdentifierManager.getInstance().updateSystemMetadata(sm);
298
  			}  
299
  	  	} catch (McdbDocNotFoundException e) {
300
  			// TODO Auto-generated catch block
301
  			e.printStackTrace();
302
  			return null;
303
  		}
304
  	}
305
  	
306
    return pid;
307
  }
308

  
309
}
310 0

  
src/edu/ucsb/nceas/metacat/dataone/CNodeService.java
63 63
import edu.ucsb.nceas.metacat.EventLog;
64 64
import edu.ucsb.nceas.metacat.IdentifierManager;
65 65
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
66
import edu.ucsb.nceas.metacat.dataone.hazelcast.CNReplicationTask;
66 67
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
67
import edu.ucsb.nceas.metacat.replication.ForceReplicationSystemMetadataHandler;
68 68

  
69 69
/**
70 70
 * Represents Metacat's implementation of the DataONE Coordinating Node 
src/edu/ucsb/nceas/metacat/dataone/hazelcast/HazelcastService.java
47 47

  
48 48
import edu.ucsb.nceas.metacat.IdentifierManager;
49 49
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
50
import edu.ucsb.nceas.metacat.dataone.CNReplicationTask;
51 50
import edu.ucsb.nceas.metacat.dataone.D1NodeService;
52 51
import edu.ucsb.nceas.metacat.properties.PropertyService;
53 52
import edu.ucsb.nceas.metacat.shared.BaseService;
src/edu/ucsb/nceas/metacat/dataone/hazelcast/CNReplicationTask.java
1
/**
2
 * This work was created by participants in the DataONE project, and is
3
 * jointly copyrighted by participating institutions in DataONE. For
4
 * more information on DataONE, see our web site at http://dataone.org.
5
 *
6
 *   Copyright ${year}
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

  
21
package edu.ucsb.nceas.metacat.dataone.hazelcast;
22

  
23
import java.io.InputStream;
24
import java.io.Serializable;
25
import java.util.concurrent.Callable;
26

  
27
import org.dataone.client.D1Client;
28
import org.dataone.configuration.Settings;
29
import org.dataone.service.exceptions.NotFound;
30
import org.dataone.service.types.v1.Identifier;
31
import org.dataone.service.types.v1.Node;
32
import org.dataone.service.types.v1.Permission;
33
import org.dataone.service.types.v1.SystemMetadata;
34

  
35
import com.hazelcast.core.Hazelcast;
36
import com.hazelcast.core.IMap;
37

  
38
import edu.ucsb.nceas.metacat.IdentifierManager;
39
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
40
import edu.ucsb.nceas.metacat.dataone.CNodeService;
41

  
42
/**
43
 * A single CN replication task to be executed by the CN Replication Service. This 
44
 * applies to replication of system metadata, science metadata, and resource maps 
45
 * across CNs.
46
 * 
47
 * The task is built when a change occurs in the hzSystemMetadata map in the
48
 * storage cluster initiated by Metacat for a given PID. If the change involves
49
 * only modifications to system metadata, those changes will occur in the shared
50
 * map and in the backing store of the member that owns the map entry. Other 
51
 * members must be updated using this task.  Likewise, changes to the system
52
 * metadata map that involve science metadata creation or changes will be
53
 * distributed to all members via this task.  The same is for OAI-ORE resource maps.
54
 * 
55
 * @author cjones
56
 *
57
 */
58
public class CNReplicationTask implements Serializable, Callable<Identifier> {
59

  
60
  /* The identifier of this task */
61
  private String taskid;
62
  
63
  /* The identifier of the object to replicate */
64
  private Identifier pid;
65
  
66
  /* The object format type stated in the system metadata (DATA/METADATA/RESOURCE) */
67
  private String formatType;
68
  
69
  /* The target Node object */
70
  private Node targetNode;
71
  
72
  /* The originating Node object */
73
  private Node originatingNode;
74

  
75
  /* The subject of the target node, extracted from the Node object */
76
  private String targetNodeSubject;
77
  
78
  /* The subject of the originating node, extracted from the Node object */
79
  private String originatingNodeSubject;
80
  
81
  /* The permission to be executed (in this case, always 'replicate') */
82
  String permission;
83

  
84
  /**
85
   * Constructor - create an empty replication task instance
86
   */
87
  public CNReplicationTask() {
88
  }
89

  
90
  /**
91
   * Constructor - create a replication task instance
92
   * 
93
   * @param taskid
94
   * @param pid
95
   * @param targetNode
96
   */
97
  public CNReplicationTask(String taskid, Identifier pid, String formatType,
98
    Node originatingNode, Node targetNode,
99
    Permission replicatePermission) {
100
    
101
    this.taskid = taskid;
102
    this.pid = pid;
103
    this.formatType = formatType;
104
    this.originatingNode = originatingNode;
105
    this.targetNode = targetNode;
106
    this.originatingNodeSubject = originatingNode.getSubject(0).getValue();
107
    this.targetNodeSubject = targetNode.getSubject(0).getValue();
108
    this.permission = replicatePermission.name();
109
    
110
  }
111

  
112
  /**
113
   * Get the task identifier for this task
114
   * @return the taskid
115
   */
116
  public String getTaskid() {
117
    return taskid;
118
  }
119

  
120
  /**
121
   * Set the task identifier for this task
122
   * @param taskid the taskid to set
123
   */
124
  public void setTaskid(String taskid) {
125
    this.taskid = taskid;
126
  }
127

  
128
  /**
129
   * Get the object identifier to be replicated
130
   * @return the pid
131
   */
132
  public Identifier getPid() {
133
    return pid;
134
  }
135

  
136
  /**
137
   * Set the object identifier to be replicated
138
   * @param pid the pid to set
139
   */
140
  public void setPid(Identifier pid) {
141
    this.pid = pid;
142
  }
143

  
144

  
145
  /**
146
   * Get the format type of the object to be replicated
147
   * @return the pid
148
   */
149
  public String getFormatType() {
150
    return this.formatType;
151
    
152
  }
153

  
154
  /**
155
   * Set the format type of the object to be replicated
156
   * @param pid the pid to set
157
   */
158
  public void setFormatType(String formatType) {
159
    this.formatType = formatType;
160
    
161
  }
162

  
163
  /**
164
   * Get the target node
165
   * @return the targetNode
166
   */
167
  public Node getTargetNode() {
168
    return targetNode;
169
  }
170

  
171
  /**
172
   * Set the target node
173
   * @param targetNode the targetNode to set
174
   */
175
  public void setTargetNode(Node targetNode) {
176
    this.targetNode = targetNode;
177
  }
178

  
179
  /**
180
   * Get the originating node
181
   * @return the originatingNode
182
   */
183
  public Node getOriginatingNode() {
184
    return originatingNode;
185
  }
186

  
187
  /**
188
   * Set the originating node
189
   * @param originatingNode the originatingNode to set
190
   */
191
  public void setOriginatingNode(Node originatingNode) {
192
    this.originatingNode = originatingNode;
193
  }
194

  
195
  /**
196
   * For the given Replication task, return the Subject listed in the target
197
   * node.  Usually used in authorizing a replication event.
198
   * 
199
   * @return subject - the subject listed in the target Node object as a string
200
   */
201
  public String getTargetNodeSubject() {
202
    
203
    return this.targetNodeSubject;
204
    
205
  }
206
  
207
  /**
208
   * Set the target node subject identifying the node
209
   * @param subject the targetNode subject
210
   */
211
  public void setTargetNodeSubject(String subject) {
212
    this.targetNodeSubject = subject;
213
  }
214
  
215
  /**
216
   * For the given Replication task, return the Subject listed in the target
217
   * node.  Usually used in authorizing a replication event.
218
   * 
219
   * @return subject - the subject listed in the target Node object as a string
220
   */
221
  public String getOriginatingNodeSubject() {
222
    
223
    return this.originatingNodeSubject;
224
    
225
  }
226
  
227
  /**
228
   * Set the target node subject identifying the node
229
   * @param subject the targetNode subject
230
   */
231
  public void setOriginatingNodeSubject(String subject) {
232
    this.originatingNodeSubject = subject;
233
  }
234
  
235
  /**
236
   * Get the permission being allowed for this task
237
   * 
238
   * @return subject - the subject listed in the target Node object
239
   */
240
  public String getPermission() {
241
    return this.permission;
242
    
243
  }
244
  
245
  /**
246
   * Set the permission being allowed for this task
247
   * @param subject the targetNode subject
248
   */
249
  public void setPermission(Permission permission) {
250
    this.permission = permission.name();
251
    
252
  }
253
 
254
  /**
255
   * Implement the Callable interface, providing code that initiates replication.
256
   * 
257
   * @return pid - the identifier of the replicated object upon success
258
   */
259
  public Identifier call() {
260
		
261
	// Get the D1 Hazelcast configuration parameters
262
	String hzSystemMetadata = 
263
		Settings.getConfiguration().getString("dataone.hazelcast.systemMetadata");
264
	
265
	// get the system metadata for the pid	
266
	IMap<Identifier, SystemMetadata> sysMetaMap = Hazelcast.getMap(hzSystemMetadata);
267
	
268
	// get the systemMetadata
269
	SystemMetadata sm = sysMetaMap.get(pid);
270
	
271
	
272
  	
273
  	// only system metadata - no data replicated
274
  	boolean isData = formatType.equals("??");
275
  	if (!isData) {
276
  		// TODO: get the science metadata/ORE from somewhere
277
  		InputStream sciMetaORE = null;
278
		try {
279
			sciMetaORE = CNodeService.getInstance().get(null, pid);
280
		} catch (NotFound nf) {
281
			try {
282
				sciMetaORE = D1Client.getCN().get(null, pid);
283
				// save it locally
284
		  		CNodeService.getInstance().create(null, pid, sciMetaORE, sm);
285
			} catch (Exception e) {
286
				// TODO Auto-generated catch block
287
				e.printStackTrace();
288
			}
289
		} catch (Exception e) {
290
			e.printStackTrace();
291
		}
292
  	} else {
293
  		// just system metadata
294
  		try {
295
  			if (!IdentifierManager.getInstance().identifierExists(pid.getValue())) {
296
  				IdentifierManager.getInstance().createSystemMetadata(sm);
297
  			} else {
298
  				IdentifierManager.getInstance().updateSystemMetadata(sm);
299
  			}  
300
  	  	} catch (McdbDocNotFoundException e) {
301
  			// TODO Auto-generated catch block
302
  			e.printStackTrace();
303
  			return null;
304
  		}
305
  	}
306
  	
307
    return pid;
308
  }
309

  
310
}
0 311

  

Also available in: Unified diff