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: cjones $'
|
9
|
* '$Date: 2011-09-11 18:12:50 -0700 (Sun, 11 Sep 2011) $'
|
10
|
* '$Revision: 6407 $'
|
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.File;
|
30
|
|
31
|
import org.apache.log4j.Logger;
|
32
|
import org.dataone.service.types.v1.Identifier;
|
33
|
import org.dataone.service.types.v1.SystemMetadata;
|
34
|
|
35
|
import com.hazelcast.config.Config;
|
36
|
import com.hazelcast.core.Hazelcast;
|
37
|
import com.hazelcast.core.HazelcastInstance;
|
38
|
import com.hazelcast.core.IMap;
|
39
|
import com.hazelcast.core.IdGenerator;
|
40
|
import com.hazelcast.core.InstanceEvent;
|
41
|
import com.hazelcast.core.InstanceListener;
|
42
|
|
43
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
44
|
import edu.ucsb.nceas.metacat.shared.BaseService;
|
45
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
46
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
47
|
/**
|
48
|
* The Hazelcast service enables Metacat as a Hazelcast cluster member
|
49
|
*/
|
50
|
public class HazelcastService extends BaseService
|
51
|
implements InstanceListener {
|
52
|
|
53
|
/* The instance of the logging class */
|
54
|
private static Logger logMetacat = Logger.getLogger(HazelcastService.class);
|
55
|
|
56
|
/* The singleton instance of the hazelcast service */
|
57
|
private static HazelcastService hzService = null;
|
58
|
|
59
|
/* The Hazelcast instance that is a cluster member */
|
60
|
private HazelcastInstance hzMember;
|
61
|
|
62
|
/* The Hazelcast configuration */
|
63
|
private Config hzConfig;
|
64
|
|
65
|
/* The task id namespace (for generating cluster-wide unqique task ids */
|
66
|
private String taskIds;
|
67
|
|
68
|
/* The Hazelcast Id Genrator used to generate unique task ids */
|
69
|
IdGenerator idGenerator;
|
70
|
|
71
|
/*
|
72
|
* Constructor: Creates an instance of the hazelcast service. Since
|
73
|
* this uses a singleton pattern, use getInstance() to gain the instance.
|
74
|
*/
|
75
|
private HazelcastService() {
|
76
|
|
77
|
super();
|
78
|
_serviceName="HazelcastService";
|
79
|
|
80
|
try {
|
81
|
init();
|
82
|
|
83
|
} catch (ServiceException se) {
|
84
|
logMetacat.debug("There was a problem creating the HazelcastService. " +
|
85
|
"The error message was: " + se.getMessage());
|
86
|
|
87
|
}
|
88
|
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Get the instance of the HazelcastService that has been instantiated,
|
93
|
* or instantiate one if it has not been already.
|
94
|
*
|
95
|
* @return hazelcastService - The instance of the hazelcast service
|
96
|
*/
|
97
|
public static HazelcastService getInstance(){
|
98
|
|
99
|
if ( hzService == null ) {
|
100
|
|
101
|
hzService = new HazelcastService();
|
102
|
|
103
|
}
|
104
|
return hzService;
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Initializes the Hazelcast service
|
109
|
*/
|
110
|
public void init() throws ServiceException {
|
111
|
|
112
|
logMetacat.debug("HazelcastService.doRefresh() called.");
|
113
|
|
114
|
try {
|
115
|
File hzConfigFile =
|
116
|
new File(PropertyService.getProperty("dataone.hazelcast.configFilePath"));
|
117
|
hzConfig = new Config();
|
118
|
hzConfig.setConfigurationFile(hzConfigFile);
|
119
|
hzMember = Hazelcast.init(hzConfig);
|
120
|
taskIds =
|
121
|
PropertyService.getProperty("dataone.hazelcast.storageCluster.tasksIdNamespace");
|
122
|
|
123
|
idGenerator = hzMember.getIdGenerator(taskIds);
|
124
|
|
125
|
} catch (PropertyNotFoundException e) {
|
126
|
String msg = "Couldn't find the Hazelcast configuration file.";
|
127
|
logMetacat.error(msg);
|
128
|
throw new ServiceException(msg);
|
129
|
|
130
|
} catch (IllegalStateException e) {
|
131
|
|
132
|
String msg = "This instance of Hazelcast has already been created.";
|
133
|
logMetacat.error(msg);
|
134
|
throw new ServiceException(msg);
|
135
|
|
136
|
}
|
137
|
|
138
|
return;
|
139
|
|
140
|
}
|
141
|
|
142
|
/**
|
143
|
* Indicate whether or not this service is refreshable.
|
144
|
*
|
145
|
* @return refreshable - the boolean refreshable status
|
146
|
*/
|
147
|
public boolean refreshable() {
|
148
|
// TODO: Determine the consequences of restarting the Hazelcast instance
|
149
|
// Set this to true if it's okay to drop from the cluster, lose the maps,
|
150
|
// and start back up again
|
151
|
return false;
|
152
|
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Stop the HazelcastService. When stopped, the service will no longer
|
157
|
* respond to requests.
|
158
|
*/
|
159
|
public void stop() throws ServiceException {
|
160
|
|
161
|
Hazelcast.getLifecycleService().shutdown();
|
162
|
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* Listen for new Hazelcast member events
|
167
|
*/
|
168
|
@Override
|
169
|
public void instanceCreated(InstanceEvent event) {
|
170
|
logMetacat.info("New Hazelcast instance created: " +
|
171
|
event.getInstance().getId() + ", " +
|
172
|
event.getInstance().getInstanceType());
|
173
|
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public void instanceDestroyed(InstanceEvent event) {
|
178
|
logMetacat.info("Hazelcast instance removed: " +
|
179
|
event.getInstance().getId() + ", " +
|
180
|
event.getInstance().getInstanceType());
|
181
|
|
182
|
}
|
183
|
|
184
|
|
185
|
/**
|
186
|
* Refresh the Hazelcast service by restarting it
|
187
|
*/
|
188
|
@Override
|
189
|
protected void doRefresh() throws ServiceException {
|
190
|
|
191
|
// TODO: verify that the correct config file is still used
|
192
|
Hazelcast.getLifecycleService().restart();
|
193
|
|
194
|
}
|
195
|
|
196
|
}
|