Project

General

Profile

1 6398 cjones
/**
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$'
9
 *     '$Date$'
10
 * '$Revision$'
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 6399 leinfelder
package edu.ucsb.nceas.metacat.dataone.hazelcast;
28 6398 cjones
29 6401 cjones
import java.io.File;
30
31 6398 cjones
import org.apache.log4j.Logger;
32
import org.dataone.service.types.v1.Identifier;
33
import org.dataone.service.types.v1.SystemMetadata;
34
35 6401 cjones
import com.hazelcast.config.Config;
36 6398 cjones
import com.hazelcast.core.Hazelcast;
37 6401 cjones
import com.hazelcast.core.HazelcastInstance;
38 6398 cjones
import com.hazelcast.core.IMap;
39 6407 cjones
import com.hazelcast.core.IdGenerator;
40 6398 cjones
import com.hazelcast.core.InstanceEvent;
41
import com.hazelcast.core.InstanceListener;
42
43 6401 cjones
import edu.ucsb.nceas.metacat.properties.PropertyService;
44 6398 cjones
import edu.ucsb.nceas.metacat.shared.BaseService;
45
import edu.ucsb.nceas.metacat.shared.ServiceException;
46 6401 cjones
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
47 6398 cjones
/**
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 6401 cjones
  private static HazelcastService hzService = null;
58 6398 cjones
59 6401 cjones
  /* The Hazelcast instance that is a cluster member */
60
  private HazelcastInstance hzMember;
61 6398 cjones
62 6401 cjones
  /* The Hazelcast configuration */
63
  private Config hzConfig;
64 6398 cjones
65 6407 cjones
  /* 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 6398 cjones
  /*
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 6401 cjones
      init();
82 6398 cjones
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 6401 cjones
    if ( hzService == null ) {
100 6398 cjones
101 6401 cjones
      hzService = new HazelcastService();
102 6398 cjones
103
    }
104 6401 cjones
    return hzService;
105 6398 cjones
  }
106
107
  /**
108 6401 cjones
   * Initializes the Hazelcast service
109 6398 cjones
   */
110 6401 cjones
  public void init() throws ServiceException {
111 6398 cjones
112
    logMetacat.debug("HazelcastService.doRefresh() called.");
113
114 6401 cjones
    try {
115 6407 cjones
      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 6401 cjones
125
    } catch (PropertyNotFoundException e) {
126 6407 cjones
      String msg = "Couldn't find the Hazelcast configuration file.";
127
      logMetacat.error(msg);
128
      throw new ServiceException(msg);
129 6398 cjones
130 6401 cjones
    } catch (IllegalStateException e) {
131
132 6407 cjones
      String msg = "This instance of Hazelcast has already been created.";
133
      logMetacat.error(msg);
134
      throw new ServiceException(msg);
135 6401 cjones
136
    }
137
138 6398 cjones
    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 6401 cjones
    // TODO: Determine the consequences of restarting the Hazelcast instance
149 6407 cjones
    // 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 6398 cjones
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 6407 cjones
  @Override
169 6398 cjones
  public void instanceCreated(InstanceEvent event) {
170 6407 cjones
    logMetacat.info("New Hazelcast instance created: " +
171
      event.getInstance().getId() + ", " +
172
      event.getInstance().getInstanceType());
173
174 6398 cjones
  }
175
176 6407 cjones
  @Override
177 6398 cjones
  public void instanceDestroyed(InstanceEvent event) {
178 6407 cjones
    logMetacat.info("Hazelcast instance removed: " +
179
        event.getInstance().getId() + ", " +
180
        event.getInstance().getInstanceType());
181
182 6398 cjones
  }
183
184 6407 cjones
185
  /**
186
   * Refresh the Hazelcast service by restarting it
187
   */
188
  @Override
189 6401 cjones
  protected void doRefresh() throws ServiceException {
190
191 6407 cjones
    // TODO: verify that the correct config file is still used
192 6401 cjones
    Hazelcast.getLifecycleService().restart();
193 6407 cjones
194 6401 cjones
  }
195
196 6398 cjones
}