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
import java.util.List;
24
import java.util.Map;
25

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

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

    
40
import edu.ucsb.nceas.metacat.common.index.IndexTask;
41
import edu.ucsb.nceas.metacat.common.index.event.IndexEvent;
42

    
43

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

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

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

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