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: leinfelder $'
|
9
|
* '$Date: 2011-09-19 15:26:28 -0700 (Mon, 19 Sep 2011) $'
|
10
|
* '$Revision: 6459 $'
|
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.util.Collection;
|
30
|
import java.util.Date;
|
31
|
|
32
|
import org.apache.log4j.Logger;
|
33
|
import org.dataone.service.types.v1.Identifier;
|
34
|
import org.dataone.service.types.v1.Node;
|
35
|
import org.dataone.service.types.v1.NodeReference;
|
36
|
import org.dataone.service.types.v1.SystemMetadata;
|
37
|
|
38
|
import com.hazelcast.client.HazelcastClient;
|
39
|
import com.hazelcast.config.Config;
|
40
|
import com.hazelcast.config.FileSystemXmlConfig;
|
41
|
import com.hazelcast.core.EntryEvent;
|
42
|
import com.hazelcast.core.EntryListener;
|
43
|
import com.hazelcast.core.Hazelcast;
|
44
|
import com.hazelcast.core.IMap;
|
45
|
import com.hazelcast.core.InstanceEvent;
|
46
|
import com.hazelcast.core.InstanceListener;
|
47
|
import com.hazelcast.core.Member;
|
48
|
import com.hazelcast.partition.Partition;
|
49
|
import com.hazelcast.partition.PartitionService;
|
50
|
import com.hazelcast.query.EntryObject;
|
51
|
import com.hazelcast.query.Predicate;
|
52
|
import com.hazelcast.query.PredicateBuilder;
|
53
|
|
54
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
55
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
56
|
import edu.ucsb.nceas.metacat.dataone.D1NodeService;
|
57
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
58
|
import edu.ucsb.nceas.metacat.shared.BaseService;
|
59
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
60
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
61
|
/**
|
62
|
* The Hazelcast service enables Metacat as a Hazelcast cluster member
|
63
|
*/
|
64
|
public class HazelcastService extends BaseService
|
65
|
implements InstanceListener, EntryListener<Identifier, SystemMetadata> {
|
66
|
|
67
|
private static final String SINCE_PROPERTY = "dateSysMetaModified";
|
68
|
|
69
|
/* The instance of the logging class */
|
70
|
private static Logger logMetacat = Logger.getLogger(HazelcastService.class);
|
71
|
|
72
|
/* The singleton instance of the hazelcast service */
|
73
|
private static HazelcastService hzService = null;
|
74
|
|
75
|
/* The Hazelcast configuration */
|
76
|
private Config hzConfig;
|
77
|
|
78
|
/* The instance of the Hazelcast client */
|
79
|
private HazelcastClient hzClient;
|
80
|
|
81
|
/* The name of the DataONE Hazelcast cluster group */
|
82
|
private String groupName;
|
83
|
|
84
|
/* The name of the DataONE Hazelcast cluster password */
|
85
|
private String groupPassword;
|
86
|
|
87
|
/* The name of the DataONE Hazelcast cluster IP addresses */
|
88
|
private String addressList;
|
89
|
|
90
|
/* The name of the node map */
|
91
|
private String nodeMap;
|
92
|
|
93
|
/* The name of the system metadata map */
|
94
|
private String systemMetadataMap;
|
95
|
|
96
|
/* The Hazelcast distributed task id generator namespace */
|
97
|
private String taskIds;
|
98
|
|
99
|
/* The Hazelcast distributed system metadata map */
|
100
|
private IMap<NodeReference, Node> nodes;
|
101
|
|
102
|
/* The Hazelcast distributed system metadata map */
|
103
|
private IMap<Identifier, SystemMetadata> systemMetadata;
|
104
|
|
105
|
/* The Hazelcast distributed pending replication tasks map*/
|
106
|
private IMap<String, CNReplicationTask> pendingReplicationTasks;
|
107
|
|
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.doRefresh() called.");
|
151
|
|
152
|
try {
|
153
|
String configFileName = PropertyService.getProperty("dataone.hazelcast.configFilePath");
|
154
|
// System.setProperty("hazelcast.config", configFileName);
|
155
|
Config config = new FileSystemXmlConfig(configFileName);
|
156
|
Hazelcast.init(config);
|
157
|
} catch (Exception e) {
|
158
|
String msg = e.getMessage();
|
159
|
logMetacat.error(msg);
|
160
|
throw new ServiceException(msg);
|
161
|
}
|
162
|
|
163
|
// Get configuration properties on instantiation
|
164
|
try {
|
165
|
groupName =
|
166
|
PropertyService.getProperty("dataone.hazelcast.processCluster.groupName");
|
167
|
groupPassword =
|
168
|
PropertyService.getProperty("dataone.hazelcast.processCluster.password");
|
169
|
addressList =
|
170
|
PropertyService.getProperty("dataone.hazelcast.processCluster.instances");
|
171
|
nodeMap =
|
172
|
PropertyService.getProperty("dataone.hazelcast.processCluster.nodesMap");
|
173
|
systemMetadataMap =
|
174
|
PropertyService.getProperty("dataone.hazelcast.storageCluster.systemMetadataMap");
|
175
|
|
176
|
// Become a DataONE-process cluster client
|
177
|
//TODO: where should this be?
|
178
|
// String[] addresses = addressList.split(",");
|
179
|
// hzClient =
|
180
|
// HazelcastClient.newHazelcastClient(this.groupName, this.groupPassword, addresses);
|
181
|
// nodes = hzClient.getMap(nodeMap);
|
182
|
// pendingReplicationTasks = hzClient.getMap(pendingTasksQueue);
|
183
|
|
184
|
// Get a reference to the shared system metadata map as a cluster member
|
185
|
systemMetadata = Hazelcast.getMap(systemMetadataMap);
|
186
|
|
187
|
// Listen for changes to the system metadata map
|
188
|
systemMetadata.addEntryListener(this, true);
|
189
|
|
190
|
} catch (PropertyNotFoundException e) {
|
191
|
|
192
|
String msg = "Couldn't find Hazelcast properties for the DataONE clusters. " +
|
193
|
"The error message was: " + e.getMessage();
|
194
|
logMetacat.error(msg);
|
195
|
|
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
|
}
|
207
|
|
208
|
}
|
209
|
|
210
|
public IMap<Identifier,SystemMetadata> getSystemMetadataMap() {
|
211
|
return systemMetadata;
|
212
|
}
|
213
|
|
214
|
public IMap<String,CNReplicationTask> getPendingReplicationTasks() {
|
215
|
return pendingReplicationTasks;
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
* Indicate whether or not this service is refreshable.
|
220
|
*
|
221
|
* @return refreshable - the boolean refreshable status
|
222
|
*/
|
223
|
public boolean refreshable() {
|
224
|
// TODO: Determine the consequences of restarting the Hazelcast instance
|
225
|
// Set this to true if it's okay to drop from the cluster, lose the maps,
|
226
|
// and start back up again
|
227
|
return false;
|
228
|
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Stop the HazelcastService. When stopped, the service will no longer
|
233
|
* respond to requests.
|
234
|
*/
|
235
|
public void stop() throws ServiceException {
|
236
|
|
237
|
Hazelcast.getLifecycleService().shutdown();
|
238
|
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Listen for new Hazelcast member events
|
243
|
*/
|
244
|
@Override
|
245
|
public void instanceCreated(InstanceEvent event) {
|
246
|
logMetacat.info("New Hazelcast instance created: " +
|
247
|
event.getInstance().getId() + ", " +
|
248
|
event.getInstance().getInstanceType());
|
249
|
|
250
|
}
|
251
|
|
252
|
@Override
|
253
|
public void instanceDestroyed(InstanceEvent event) {
|
254
|
logMetacat.info("Hazelcast instance removed: " +
|
255
|
event.getInstance().getId() + ", " +
|
256
|
event.getInstance().getInstanceType());
|
257
|
|
258
|
}
|
259
|
|
260
|
/**
|
261
|
* Refresh the Hazelcast service by restarting it
|
262
|
*/
|
263
|
@Override
|
264
|
protected void doRefresh() throws ServiceException {
|
265
|
|
266
|
// TODO: verify that the correct config file is still used
|
267
|
Hazelcast.getLifecycleService().restart();
|
268
|
|
269
|
}
|
270
|
|
271
|
/**
|
272
|
* Implement the EntryListener interface for Hazelcast, reponding to entry
|
273
|
* added events in the hzSystemMetadata map. Evaluate the entry and create
|
274
|
* CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
|
275
|
*
|
276
|
* @param event - The EntryEvent that occurred
|
277
|
*/
|
278
|
@Override
|
279
|
public void entryAdded(EntryEvent<Identifier, SystemMetadata> event) {
|
280
|
// handle as update - that method will create if necessary
|
281
|
entryUpdated(event);
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* Implement the EntryListener interface for Hazelcast, reponding to entry
|
286
|
* evicted events in the hzSystemMetadata map. Evaluate the entry and create
|
287
|
* CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
|
288
|
*
|
289
|
* @param event - The EntryEvent that occurred
|
290
|
*/
|
291
|
@Override
|
292
|
public void entryEvicted(EntryEvent<Identifier, SystemMetadata> event) {
|
293
|
// nothing to do, entries are still in the backing store
|
294
|
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* Implement the EntryListener interface for Hazelcast, reponding to entry
|
299
|
* removed events in the hzSystemMetadata map. Evaluate the entry and create
|
300
|
* CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
|
301
|
*
|
302
|
* @param event - The EntryEvent that occurred
|
303
|
*/
|
304
|
@Override
|
305
|
public void entryRemoved(EntryEvent<Identifier, SystemMetadata> event) {
|
306
|
// we don't remove objects
|
307
|
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* Implement the EntryListener interface for Hazelcast, reponding to entry
|
312
|
* updated events in the hzSystemMetadata map. Evaluate the entry and create
|
313
|
* CNReplicationTasks as appropriate (for DATA, METADATA, RESOURCE)
|
314
|
*
|
315
|
* @param event - The EntryEvent that occurred
|
316
|
*/
|
317
|
@Override
|
318
|
public void entryUpdated(EntryEvent<Identifier, SystemMetadata> event) {
|
319
|
|
320
|
logMetacat.debug("Entry added/updated to System Metadata map: " + event.getKey().getValue());
|
321
|
PartitionService partitionService = Hazelcast.getPartitionService();
|
322
|
Partition partition = partitionService.getPartition(event.getKey());
|
323
|
Member ownerMember = partition.getOwner();
|
324
|
if (!ownerMember.localMember()) {
|
325
|
// need to pull the entry into the local store
|
326
|
saveLocally(event.getValue());
|
327
|
}
|
328
|
|
329
|
// TODO evaluate the type of system metadata change, decide if it
|
330
|
// warrants a replication event, what type (DATA, METADATA, RESOURCE),
|
331
|
// iteratively lock the PID, create and submit the tasks, and expect a
|
332
|
// result back. Deal with exceptions.
|
333
|
boolean isMetadata = D1NodeService.isScienceMetadata(event.getValue());
|
334
|
// TODO: do we need to do anything explicit here?
|
335
|
|
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
|
}
|
381
|
|
382
|
}
|