blob: 62563644b65591b25bc1b256de1e784f71c3335d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yuta HIGUCHI41f2ec02014-10-27 09:54:43 -070016package org.onlab.onos.store.hz;
tomdc66b382014-09-22 17:05:47 -070017
tom1a2908c2014-09-23 16:37:39 -070018import com.hazelcast.config.Config;
19import com.hazelcast.config.FileSystemXmlConfig;
Yuta HIGUCHI48ee9922014-11-11 09:19:13 -080020import com.hazelcast.config.MapConfig;
tomdc66b382014-09-22 17:05:47 -070021import com.hazelcast.core.Hazelcast;
22import com.hazelcast.core.HazelcastInstance;
Yuta HIGUCHI497c8842014-09-25 14:23:34 -070023
tomdc66b382014-09-22 17:05:47 -070024import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Component;
26import org.apache.felix.scr.annotations.Deactivate;
27import org.apache.felix.scr.annotations.Service;
tomdc66b382014-09-22 17:05:47 -070028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
tom1a2908c2014-09-23 16:37:39 -070031import java.io.FileNotFoundException;
tom0872a172014-09-23 11:24:26 -070032
tomdc66b382014-09-22 17:05:47 -070033/**
34 * Auxiliary bootstrap of distributed store.
35 */
36@Component(immediate = true)
37@Service
38public class StoreManager implements StoreService {
39
Yuta HIGUCHIb4139d82014-09-23 18:41:33 -070040 protected static final String HAZELCAST_XML_FILE = "etc/hazelcast.xml";
tom1a2908c2014-09-23 16:37:39 -070041
tomdc66b382014-09-22 17:05:47 -070042 private final Logger log = LoggerFactory.getLogger(getClass());
43
tom85ff08b2014-09-22 17:14:18 -070044 protected HazelcastInstance instance;
tomdc66b382014-09-22 17:05:47 -070045
46 @Activate
47 public void activate() {
tom1a2908c2014-09-23 16:37:39 -070048 try {
49 Config config = new FileSystemXmlConfig(HAZELCAST_XML_FILE);
Yuta HIGUCHI48ee9922014-11-11 09:19:13 -080050
51 MapConfig roles = config.getMapConfig("nodeRoles");
52 roles.setAsyncBackupCount(MapConfig.MAX_BACKUP_COUNT - roles.getBackupCount());
53
54 MapConfig terms = config.getMapConfig("terms");
55 terms.setAsyncBackupCount(MapConfig.MAX_BACKUP_COUNT - terms.getBackupCount());
56
tom1a2908c2014-09-23 16:37:39 -070057 instance = Hazelcast.newHazelcastInstance(config);
tom1a2908c2014-09-23 16:37:39 -070058 log.info("Started");
59 } catch (FileNotFoundException e) {
60 log.error("Unable to configure Hazelcast", e);
61 }
tomdc66b382014-09-22 17:05:47 -070062 }
63
64 @Deactivate
65 public void deactivate() {
tom0872a172014-09-23 11:24:26 -070066 instance.shutdown();
tomdc66b382014-09-22 17:05:47 -070067 log.info("Stopped");
68 }
69
70 @Override
71 public HazelcastInstance getHazelcastInstance() {
72 return instance;
73 }
tom0872a172014-09-23 11:24:26 -070074
tomdc66b382014-09-22 17:05:47 -070075}