Project

General

Profile

1
/**
2
 *  Copyright: 2013 Regents of the University of California and the
3
 *             National Center for Ecological Analysis and Synthesis
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 */
18
package edu.ucsb.nceas.metacat.index;
19

    
20
import java.io.FileInputStream;
21
import java.io.FileNotFoundException;
22
import java.io.InputStream;
23

    
24
import org.apache.commons.logging.Log;
25
import org.apache.commons.logging.LogFactory;
26
import org.dataone.configuration.Settings;
27
import org.dataone.service.exceptions.ServiceFailure;
28
import org.dataone.service.types.v1.Identifier;
29
import org.dataone.service.types.v2.SystemMetadata;
30

    
31
import com.hazelcast.client.ClientConfig;
32
import com.hazelcast.client.HazelcastClient;
33
import com.hazelcast.config.Config;
34
import com.hazelcast.config.FileSystemXmlConfig;
35
import com.hazelcast.core.IMap;
36
import com.hazelcast.core.ISet;
37

    
38
import edu.ucsb.nceas.metacat.common.index.IndexTask;
39
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
40

    
41

    
42
/**
43
 * A factory to get distributed maps from the haszel cast client.
44
 * @author tao
45
 *
46
 */
47
public class DistributedMapsFactory {
48
    
49
    private static final String IDENTIFIERSETNAME = "hzIdentifiers";
50
    private static Log log = LogFactory.getLog(DistributedMapsFactory.class);
51
    
52
    private static HazelcastClient hzClient = null;
53
    private static String hzSystemMetadata = null;
54
    private static String hzObjectPath = null;
55
    private static String hzIndexQueue = null;
56
    private static int waitingTime = IndexGeneratorTimerTask.WAITTIME;
57
    private static int maxAttempts = IndexGeneratorTimerTask.MAXWAITNUMBER;
58
    private static IMap<Identifier, SystemMetadata> systemMetadataMap = null;
59
    private static IMap<Identifier, String> objectPathMap = null;
60
    private static IMap<Identifier, IndexTask> indexQueue = null;
61
    /* The name of the identifiers set */
62
    private static String identifiersSetName = IDENTIFIERSETNAME;
63
    /* The Hazelcast distributed identifiers set */
64
    private static ISet<Identifier> identifiersSet = null;
65
    
66
    // for sending index events to metacat 
67
    private static String hzIndexEventMap = null;
68
    private static IMap<Identifier, IndexEvent> indexEventMap = null;
69
    
70
    /**
71
     * Start the hazel cast client
72
     */
73
    private static void startHazelCastClient() throws FileNotFoundException, ServiceFailure{
74
        
75
        try {
76
            waitingTime = Settings.getConfiguration().getInt(IndexGeneratorTimerTask.WAITIMEPOPERTYNAME);
77
            maxAttempts = Settings.getConfiguration().getInt(IndexGeneratorTimerTask.MAXATTEMPTSPROPERTYNAME);
78
        } catch (Exception e) {
79
            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");
80
            waitingTime = IndexGeneratorTimerTask.WAITTIME;
81
            maxAttempts = IndexGeneratorTimerTask.MAXWAITNUMBER;
82
        }
83
        try {
84
            identifiersSetName = Settings.getConfiguration().getString("dataone.hazelcast.storageCluster.identifiersSet");
85
        } catch (Exception e) {
86
            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");
87
            identifiersSetName = IDENTIFIERSETNAME;
88
        }
89
        
90
        // the index queue name to listen to
91
        hzIndexQueue = Settings.getConfiguration().getString("index.hazelcast.indexqueue");        
92
        
93
        // the index event map name to send events to
94
        hzIndexEventMap = Settings.getConfiguration().getString("index.hazelcast.indexeventmap");
95
        
96
        // get config values
97
        hzSystemMetadata = Settings.getConfiguration().getString(
98
                "dataone.hazelcast.storageCluster.systemMetadataMap");
99
        hzObjectPath = Settings.getConfiguration().getString(
100
                "dataone.hazelcast.storageCluster.objectPathMap");
101
        String configFileName = Settings.getConfiguration().getString(
102
                "dataone.hazelcast.configFilePath");;
103
        Config hzConfig = null;
104
        try {
105
            hzConfig = new FileSystemXmlConfig(configFileName);
106
        } catch (FileNotFoundException e) {
107
            log.error("could not load hazelcast configuration file from: " + configFileName, e);
108
            throw e;
109
        }
110
        
111
        String hzGroupName = hzConfig.getGroupConfig().getName();
112
        String hzGroupPassword = hzConfig.getGroupConfig().getPassword();
113
        String hzAddress = hzConfig.getNetworkConfig().getInterfaces().getInterfaces().iterator().next() + ":" + hzConfig.getNetworkConfig().getPort();
114

    
115
        log.info("starting index entry listener...");
116
        log.info("System Metadata value: " + hzSystemMetadata);
117
        log.info("Object path value: " + hzObjectPath);
118
        log.info("Group Name: " + hzGroupName);
119
        log.info("Group Password: " + "*****"); // don't show value
120
        log.info("HZ Address: " + hzAddress);
121

    
122
        // connect to the HZ cluster
123
        ClientConfig cc = new ClientConfig();
124
        cc.getGroupConfig().setName(hzGroupName);
125
        cc.getGroupConfig().setPassword(hzGroupPassword);
126
        cc.addAddress(hzAddress);
127
        
128
        int times = 0;
129
        while(true) {
130
            //System.out.println("here ==================");
131
            try {
132
                hzClient = HazelcastClient.newHazelcastClient(cc);
133
                break;
134
            } catch (Exception e) {
135
                    if(times <= maxAttempts) {
136
                        log.warn("DistributedMapFactory.startHazelCastClient - the hazelcast service is not ready : "
137
                                         +e.getMessage()+"\nWe will try to access it "+waitingTime/1000+" seconds later ");
138
                        try {
139
                            Thread.sleep(waitingTime);
140
                        } catch (Exception ee) {
141
                            log.warn("DistributedMapFactory.startHazelCastClient - the thread can't sleep for "+waitingTime/1000+" seconds to wait the hazelcast service");
142
                        }
143
                       
144
                    } else {
145
                        throw new ServiceFailure("0000", "DistributedMapFactory.startHazelCastClient - the hazelcast service is not ready even though Metacat-index wailted for "+
146
                                        maxAttempts*waitingTime/1000+" seconds. We can't get the system metadata from it and the building index can't happen this time");
147
                    }
148
             }
149
             times++;
150
             //System.out.println("here ==================2");
151
        }
152
        
153

    
154
    }
155
    
156
    /**
157
     * Get the system metadata map
158
     * @return
159
     * @throws FileNotFoundException
160
     * @throws ServiceFailure
161
     */
162
    public static IMap<Identifier, SystemMetadata> getSystemMetadataMap() throws FileNotFoundException, ServiceFailure {
163
        if(hzClient == null) {
164
            startHazelCastClient();
165
        }
166
        systemMetadataMap = hzClient.getMap(hzSystemMetadata);
167
        return systemMetadataMap;
168
    }
169
    
170
    /**
171
     * Get the distributed object path map from the haszel cast client.
172
     * @return
173
     * @throws FileNotFoundException
174
     * @throws ServiceFailure
175
     */
176
    public static IMap<Identifier, String> getObjectPathMap() throws FileNotFoundException, ServiceFailure {
177
        if(hzClient == null) {
178
            startHazelCastClient();
179
        }
180
        objectPathMap = hzClient.getMap(hzObjectPath);
181
        return objectPathMap;
182
    }
183
    
184
    /**
185
     * Get the SystemMetadata for the specified id. The null will be returned if there is no SystemMetadata found for this id
186
     * @param id the specified id.
187
     * @return the SystemMetadata for the id
188
     * @throws FileNotFoundException
189
     * @throws ServiceFailure
190
     */
191
    public static SystemMetadata getSystemMetadata(String id) throws FileNotFoundException, ServiceFailure {
192
        if(systemMetadataMap == null) {
193
            getSystemMetadataMap();
194
        }
195
        SystemMetadata metadata = null;
196
        if(systemMetadataMap != null && id != null) {
197
            Identifier identifier = new Identifier();
198
            identifier.setValue(id);
199
            metadata = systemMetadataMap.get(identifier);
200
        }
201
        return metadata;
202
    }
203
    
204
    /**
205
     * Get the DataObject for the specified id. The null will be returned if not data object is found.
206
     * @param id the specified id
207
     * @return the InputStream of the data object for the specified id.
208
     * @throws FileNotFoundException
209
     * @throws ServiceFailure
210
     */
211
    public static InputStream getDataObject(String id) throws FileNotFoundException, ServiceFailure {
212
        if(objectPathMap == null) {
213
            getObjectPathMap();
214
        }
215
        InputStream data = null;
216
        if(objectPathMap != null && id != null) {
217
            Identifier identifier = new Identifier();
218
            identifier.setValue(id);
219
            String objectPath = objectPathMap.get(identifier);
220
            if(objectPath != null) {
221
                data = new FileInputStream(objectPath);
222
            }
223
        }
224
        return data;
225
    }
226
    
227
    /**
228
     * Get the identifiers set in the hazelcast client. The null may be returned if we can't find the set.
229
     * @return the identifiersSet
230
     * @throws FileNotFoundException
231
     * @throws ServiceFailure
232
     */
233
    public static ISet<Identifier> getIdentifiersSet() throws FileNotFoundException, ServiceFailure {
234
        if(hzClient== null) {
235
            startHazelCastClient();
236
        }
237
        identifiersSet= hzClient.getSet(identifiersSetName);
238
        return identifiersSet;
239
    }
240
    
241
    /**
242
     * Get the indexQueue set from hazelcast client
243
     * @return the indexQueue
244
     * @throws FileNotFoundException
245
     * @throws ServiceFailure
246
     */
247
    public static IMap<Identifier, IndexTask> getIndexQueue() throws FileNotFoundException, ServiceFailure {
248
        if(hzClient== null) {
249
            startHazelCastClient();
250
        }
251
        indexQueue = hzClient.getMap(hzIndexQueue);
252
        return indexQueue;
253
    }
254
    
255
    /**
256
     * Get the index event map
257
     * @return the index event map for writing/reading events
258
     * @throws FileNotFoundException
259
     * @throws ServiceFailure
260
     */
261
    public static IMap<Identifier, IndexEvent> getIndexEventMap() throws FileNotFoundException, ServiceFailure {
262
        if(hzClient == null) {
263
            startHazelCastClient();
264
        }
265
        indexEventMap = hzClient.getMap(hzIndexEventMap);
266
        return indexEventMap;
267
    }
268
    
269
}
(2-2/6)