stargate.drivers.hazelcast.HazelcastCoreDriver.java Source code

Java tutorial

Introduction

Here is the source code for stargate.drivers.hazelcast.HazelcastCoreDriver.java

Source

/*
 * The MIT License
 *
 * Copyright 2015 iychoi.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package stargate.drivers.hazelcast;

import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MapStoreConfig;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.ReplicatedMapConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.Member;
import com.hazelcast.core.ReplicatedMap;
import stargate.drivers.hazelcast.datastore.HazelcastDataStoreDriver;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import stargate.commons.drivers.ADriver;
import stargate.commons.drivers.ADriverConfiguration;
import stargate.commons.drivers.DriverNotInstantiatedException;
import stargate.server.service.StargateService;
import stargate.server.service.StargateServiceConfiguration;
import stargate.server.temporalstorage.TemporalStorageManager;

/**
 *
 * @author iychoi
 */
public class HazelcastCoreDriver extends ADriver {

    private static final Log LOG = LogFactory.getLog(HazelcastDataStoreDriver.class);

    private static final String HAZELCAST_GROUP_NAME_PREFIX = "Stargate_";
    public static final String HAZELCAST_PERSISTENT_MAP_PREFIX = "PERSISTENT_";

    private static HazelcastCoreDriver instance;

    private HazelcastCoreDriverConfiguration config;
    private HazelcastInstance hazelcastInstance;

    private PersistentStoreFactory persistentStoreFactory;

    public static HazelcastCoreDriver getInstance() throws DriverNotInstantiatedException {
        synchronized (HazelcastCoreDriver.class) {
            if (instance == null) {
                throw new DriverNotInstantiatedException("HazelcastDriverGroup is not instantiated");
            }
            return instance;
        }
    }

    public HazelcastCoreDriver(ADriverConfiguration config) {
        if (config == null) {
            throw new IllegalArgumentException("config is null");
        }

        if (!(config instanceof HazelcastCoreDriverConfiguration)) {
            throw new IllegalArgumentException("config is not an instance of HazelcastDriverGroupConfiguration");
        }

        if (instance != null) {
            throw new InstantiationError("cannot instantiate HazelcastDriverGroup class. Instance already exists");
        }

        this.config = (HazelcastCoreDriverConfiguration) config;

        instance = this;
    }

    public HazelcastCoreDriver(HazelcastCoreDriverConfiguration config) {
        if (config == null) {
            throw new IllegalArgumentException("config is null");
        }

        if (instance != null) {
            throw new InstantiationError("cannot instantiate HazelcastDriverGroup class. Instance already exists");
        }

        this.config = config;

        instance = this;
    }

    @Override
    public synchronized void startDriver() throws IOException {
        Config hazelcastConfig = makeHazelcastTCPConfig();
        this.hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig);
    }

    @Override
    public synchronized void stopDriver() throws IOException {
        this.hazelcastInstance.shutdown();
    }

    public StargateService getStargateService() throws Exception {
        if (this.service instanceof StargateService) {
            return (StargateService) this.service;
        } else {
            throw new Exception("service object is not instance of StargateService");
        }
    }

    private Config makeDefaultHazelcastConfig() throws Exception {
        Config config = new Config();
        if (this.config.getServiceName() == null || this.config.getServiceName().isEmpty()) {
            StargateServiceConfiguration serviceConf = getStargateService().getConfiguration();
            config.getGroupConfig().setName(HAZELCAST_GROUP_NAME_PREFIX + serviceConf.getServiceName());
        } else {
            config.getGroupConfig().setName(HAZELCAST_GROUP_NAME_PREFIX + this.config.getServiceName());
        }

        NetworkConfig network = config.getNetworkConfig();
        network.setPort(this.config.getPort());
        network.setPortAutoIncrement(true);
        network.setPortCount(100);

        this.persistentStoreFactory = new PersistentStoreFactory();

        MapStoreConfig mapStoreConfig = new MapStoreConfig();
        mapStoreConfig.setFactoryImplementation(this.persistentStoreFactory);
        mapStoreConfig.setWriteDelaySeconds(0);

        MapConfig persistentMapConfig = new MapConfig();
        persistentMapConfig.setName(HAZELCAST_PERSISTENT_MAP_PREFIX + "*");
        persistentMapConfig.setBackupCount(2);
        persistentMapConfig.getMaxSizeConfig().setSize(0);
        persistentMapConfig.setTimeToLiveSeconds(0);
        persistentMapConfig.setReadBackupData(true);
        persistentMapConfig.setMapStoreConfig(mapStoreConfig);

        MapConfig mapConfig = new MapConfig();
        mapConfig.setName("*");
        mapConfig.setBackupCount(2);
        mapConfig.getMaxSizeConfig().setSize(0);
        mapConfig.setTimeToLiveSeconds(0);
        mapConfig.setReadBackupData(true);
        mapConfig.setMapStoreConfig(mapStoreConfig);

        config.addMapConfig(mapConfig);

        ReplicatedMapConfig rmapConfig = new ReplicatedMapConfig();
        rmapConfig.setName("*");

        config.addReplicatedMapConfig(rmapConfig);

        return config;
    }

    private Config makeHazelcastTCPConfig() throws IOException {
        try {
            Config config = makeDefaultHazelcastConfig();
            config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
            config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
            if (!this.config.isLeaderHost()) {
                for (String knownHostAddr : this.config.getKnownHostAddr()) {
                    config.getNetworkConfig().getJoin().getTcpIpConfig()
                            .addMember(knownHostAddr + ":" + this.config.getPort());
                }
            }

            config.getNetworkConfig().getJoin().getTcpIpConfig()
                    .addMember(this.config.getMyHostAddr() + ":" + this.config.getPort());

            return config;
        } catch (Exception ex) {
            throw new IOException(ex);
        }
    }

    @Override
    public String getDriverName() {
        return "HazelcastDriverGroup";
    }

    private synchronized void ensureTemporalStorageManagerLoaded() {
        if (this.persistentStoreFactory.getTemporalStorageManager() == null) {
            try {
                StargateService service = getStargateService();
                TemporalStorageManager temporalStorageManager = service.getTemporalStorageManagerNoException();
                this.persistentStoreFactory.setTemporalStorageManager(temporalStorageManager);
            } catch (Exception ex) {
                LOG.error("unable to get a service", ex);
            }
        }
    }

    public synchronized IMap<String, Object> getMap(String name) {
        return this.hazelcastInstance.getMap(name);
    }

    public synchronized IMap<String, Object> getPersistentMap(String name) {
        ensureTemporalStorageManagerLoaded();
        return this.hazelcastInstance.getMap(HAZELCAST_PERSISTENT_MAP_PREFIX + name);
    }

    public synchronized ReplicatedMap<String, Object> getReplicatedMap(String name) {
        return this.hazelcastInstance.getReplicatedMap(name);
    }

    public synchronized boolean isLeader() {
        Member member = this.hazelcastInstance.getCluster().getMembers().iterator().next();
        return member.localMember();
    }
}