1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class that gets Accession Number, check for uniqueness
|
4
|
* and register it into db
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Jivka Bojilova, Matt Jones
|
8
|
*
|
9
|
* '$Author: leinfelder $'
|
10
|
* '$Date: 2011-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
|
11
|
* '$Revision: 6595 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
package edu.ucsb.nceas.metacat.index;
|
28
|
|
29
|
import java.io.FileInputStream;
|
30
|
import java.io.FileNotFoundException;
|
31
|
import java.io.InputStream;
|
32
|
|
33
|
import org.apache.commons.logging.Log;
|
34
|
import org.apache.commons.logging.LogFactory;
|
35
|
import org.dataone.configuration.Settings;
|
36
|
import org.dataone.service.exceptions.ServiceFailure;
|
37
|
import org.dataone.service.types.v1.Identifier;
|
38
|
import org.dataone.service.types.v1.SystemMetadata;
|
39
|
|
40
|
import com.hazelcast.client.ClientConfig;
|
41
|
import com.hazelcast.client.HazelcastClient;
|
42
|
import com.hazelcast.config.Config;
|
43
|
import com.hazelcast.config.FileSystemXmlConfig;
|
44
|
import com.hazelcast.core.IMap;
|
45
|
import com.hazelcast.core.ISet;
|
46
|
|
47
|
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
|
48
|
|
49
|
|
50
|
/**
|
51
|
* A factory to get distributed maps from the haszel cast client.
|
52
|
* @author tao
|
53
|
*
|
54
|
*/
|
55
|
public class DistributedMapsFactory {
|
56
|
|
57
|
private static final String IDENTIFIERSETNAME = "hzIdentifiers";
|
58
|
private static Log log = LogFactory.getLog(DistributedMapsFactory.class);
|
59
|
|
60
|
private static HazelcastClient hzClient = null;
|
61
|
private static String hzSystemMetadata = null;
|
62
|
private static String hzObjectPath = null;
|
63
|
private static String hzIndexQueue = null;
|
64
|
private static int waitingTime = IndexGenerator.WAITTIME;
|
65
|
private static int maxAttempts = IndexGenerator.MAXWAITNUMBER;
|
66
|
private static IMap<Identifier, SystemMetadata> systemMetadataMap = null;
|
67
|
private static IMap<Identifier, String> objectPathMap = null;
|
68
|
private static ISet<SystemMetadata> indexQueue = null;
|
69
|
/* The name of the identifiers set */
|
70
|
private static String identifiersSetName = IDENTIFIERSETNAME;
|
71
|
/* The Hazelcast distributed identifiers set */
|
72
|
private static ISet<Identifier> identifiersSet = null;
|
73
|
|
74
|
// for sending index events to metacat
|
75
|
private static String hzIndexEventMap = null;
|
76
|
private static IMap<Identifier, IndexEvent> indexEventMap = null;
|
77
|
|
78
|
/**
|
79
|
* Start the hazel cast client
|
80
|
*/
|
81
|
private static void startHazelCastClient() throws FileNotFoundException, ServiceFailure{
|
82
|
|
83
|
try {
|
84
|
waitingTime = Settings.getConfiguration().getInt(IndexGenerator.WAITIMEPOPERTYNAME);
|
85
|
maxAttempts = Settings.getConfiguration().getInt(IndexGenerator.MAXATTEMPTSPROPERTYNAME);
|
86
|
} catch (Exception e) {
|
87
|
log.warn("DistributedMapFactory.startHazelCastClient - couldn't read the waiting time or maxattempts from the metacat.properties file since : "+e.getMessage()+". Default values will be used");
|
88
|
waitingTime = IndexGenerator.WAITTIME;
|
89
|
maxAttempts = IndexGenerator.MAXWAITNUMBER;
|
90
|
}
|
91
|
try {
|
92
|
identifiersSetName = Settings.getConfiguration().getString("dataone.hazelcast.storageCluster.identifiersSet");
|
93
|
} catch (Exception e) {
|
94
|
log.warn("DistributedMapFactory.startHazelCastClient - couldn't read the name of the identifiersSet from the metacat.properties file since : "+e.getMessage()+". Default values will be used");
|
95
|
identifiersSetName = IDENTIFIERSETNAME;
|
96
|
}
|
97
|
|
98
|
// the index queue name to listen to
|
99
|
hzIndexQueue = Settings.getConfiguration().getString("index.hazelcast.indexqueue");
|
100
|
|
101
|
// the index event map name to send events to
|
102
|
hzIndexEventMap = Settings.getConfiguration().getString("index.hazelcast.indexeventmap");
|
103
|
|
104
|
// get config values
|
105
|
hzSystemMetadata = Settings.getConfiguration().getString(
|
106
|
"dataone.hazelcast.storageCluster.systemMetadataMap");
|
107
|
hzObjectPath = Settings.getConfiguration().getString(
|
108
|
"dataone.hazelcast.storageCluster.objectPathMap");
|
109
|
String configFileName = Settings.getConfiguration().getString(
|
110
|
"dataone.hazelcast.configFilePath");;
|
111
|
Config hzConfig = null;
|
112
|
try {
|
113
|
hzConfig = new FileSystemXmlConfig(configFileName);
|
114
|
} catch (FileNotFoundException e) {
|
115
|
log.error("could not load hazelcast configuration file from: " + configFileName, e);
|
116
|
throw e;
|
117
|
}
|
118
|
|
119
|
String hzGroupName = hzConfig.getGroupConfig().getName();
|
120
|
String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
|
121
|
String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
|
122
|
|
123
|
log.info("starting index entry listener...");
|
124
|
log.info("System Metadata value: " + hzSystemMetadata);
|
125
|
log.info("Object path value: " + hzObjectPath);
|
126
|
log.info("Group Name: " + hzGroupName);
|
127
|
log.info("Group Password: " + "*****"); // don't show value
|
128
|
log.info("HZ Address: " + hzAddress);
|
129
|
|
130
|
// connect to the HZ cluster
|
131
|
ClientConfig cc = new ClientConfig();
|
132
|
cc.getGroupConfig().setName(hzGroupName);
|
133
|
cc.getGroupConfig().setPassword(hzGroupPassword);
|
134
|
cc.addAddress(hzAddress);
|
135
|
|
136
|
int times = 0;
|
137
|
while(true) {
|
138
|
//System.out.println("here ==================");
|
139
|
try {
|
140
|
hzClient = HazelcastClient.newHazelcastClient(cc);
|
141
|
break;
|
142
|
} catch (Exception e) {
|
143
|
if(times <= maxAttempts) {
|
144
|
log.warn("DistributedMapFactory.startHazelCastClient - the hazelcast service is not ready : "
|
145
|
+e.getMessage()+"\nWe will try to access it "+waitingTime/1000+" seconds later ");
|
146
|
try {
|
147
|
Thread.sleep(waitingTime);
|
148
|
} catch (Exception ee) {
|
149
|
log.warn("DistributedMapFactory.startHazelCastClient - the thread can't sleep for "+waitingTime/1000+" seconds to wait the hazelcast service");
|
150
|
}
|
151
|
|
152
|
} else {
|
153
|
throw new ServiceFailure("0000", "DistributedMapFactory.startHazelCastClient - the hazelcast service is not ready even though Metacat-index wailted for "+
|
154
|
maxAttempts*waitingTime/1000+" seconds. We can't get the system metadata from it and the building index can't happen this time");
|
155
|
}
|
156
|
}
|
157
|
times++;
|
158
|
//System.out.println("here ==================2");
|
159
|
}
|
160
|
|
161
|
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Get the system metadata map
|
166
|
* @return
|
167
|
* @throws FileNotFoundException
|
168
|
* @throws ServiceFailure
|
169
|
*/
|
170
|
public static IMap<Identifier, SystemMetadata> getSystemMetadataMap() throws FileNotFoundException, ServiceFailure {
|
171
|
if(hzClient == null) {
|
172
|
startHazelCastClient();
|
173
|
}
|
174
|
systemMetadataMap = hzClient.getMap(hzSystemMetadata);
|
175
|
return systemMetadataMap;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Get the distributed object path map from the haszel cast client.
|
180
|
* @return
|
181
|
* @throws FileNotFoundException
|
182
|
* @throws ServiceFailure
|
183
|
*/
|
184
|
public static IMap<Identifier, String> getObjectPathMap() throws FileNotFoundException, ServiceFailure {
|
185
|
if(hzClient == null) {
|
186
|
startHazelCastClient();
|
187
|
}
|
188
|
objectPathMap = hzClient.getMap(hzObjectPath);
|
189
|
return objectPathMap;
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Get the SystemMetadata for the specified id. The null will be returned if there is no SystemMetadata found for this id
|
194
|
* @param id the specified id.
|
195
|
* @return the SystemMetadata for the id
|
196
|
* @throws FileNotFoundException
|
197
|
* @throws ServiceFailure
|
198
|
*/
|
199
|
public static SystemMetadata getSystemMetadata(String id) throws FileNotFoundException, ServiceFailure {
|
200
|
if(systemMetadataMap == null) {
|
201
|
getSystemMetadataMap();
|
202
|
}
|
203
|
SystemMetadata metadata = null;
|
204
|
if(systemMetadataMap != null && id != null) {
|
205
|
Identifier identifier = new Identifier();
|
206
|
identifier.setValue(id);
|
207
|
metadata = systemMetadataMap.get(identifier);
|
208
|
}
|
209
|
return metadata;
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Get the DataObject for the specified id. The null will be returned if not data object is found.
|
214
|
* @param id the specified id
|
215
|
* @return the InputStream of the data object for the specified id.
|
216
|
* @throws FileNotFoundException
|
217
|
* @throws ServiceFailure
|
218
|
*/
|
219
|
public static InputStream getDataObject(String id) throws FileNotFoundException, ServiceFailure {
|
220
|
if(objectPathMap == null) {
|
221
|
getObjectPathMap();
|
222
|
}
|
223
|
InputStream data = null;
|
224
|
if(objectPathMap != null && id != null) {
|
225
|
Identifier identifier = new Identifier();
|
226
|
identifier.setValue(id);
|
227
|
String objectPath = objectPathMap.get(identifier);
|
228
|
if(objectPath != null) {
|
229
|
data = new FileInputStream(objectPath);
|
230
|
}
|
231
|
}
|
232
|
return data;
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* Get the identifiers set in the hazelcast client. The null may be returned if we can't find the set.
|
237
|
* @return the identifiersSet
|
238
|
* @throws FileNotFoundException
|
239
|
* @throws ServiceFailure
|
240
|
*/
|
241
|
public static ISet<Identifier> getIdentifiersSet() throws FileNotFoundException, ServiceFailure {
|
242
|
if(hzClient== null) {
|
243
|
startHazelCastClient();
|
244
|
}
|
245
|
identifiersSet= hzClient.getSet(identifiersSetName);
|
246
|
return identifiersSet;
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* Get the indexQueue set from hazelcast client
|
251
|
* @return the indexQueue
|
252
|
* @throws FileNotFoundException
|
253
|
* @throws ServiceFailure
|
254
|
*/
|
255
|
public static ISet<SystemMetadata> getIndexQueue() throws FileNotFoundException, ServiceFailure {
|
256
|
if(hzClient== null) {
|
257
|
startHazelCastClient();
|
258
|
}
|
259
|
indexQueue = hzClient.getSet(hzIndexQueue);
|
260
|
return indexQueue;
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* Get the index event map
|
265
|
* @return the index event map for writing/reading events
|
266
|
* @throws FileNotFoundException
|
267
|
* @throws ServiceFailure
|
268
|
*/
|
269
|
public static IMap<Identifier, IndexEvent> getIndexEventMap() throws FileNotFoundException, ServiceFailure {
|
270
|
if(hzClient == null) {
|
271
|
startHazelCastClient();
|
272
|
}
|
273
|
indexEventMap = hzClient.getMap(hzIndexEventMap);
|
274
|
return indexEventMap;
|
275
|
}
|
276
|
|
277
|
}
|