Commit 61b81882 authored by Matija Obreza's avatar Matija Obreza

Merged HZ configuration

parent 4d729385
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,23 +12,34 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.spring.config;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.ExecutorConfig;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizeConfig;
import com.hazelcast.config.MaxSizeConfig.MaxSizePolicy;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ILock;
import com.hazelcast.core.ManagedContext;
import com.hazelcast.hibernate.HazelcastCacheRegionFactory;
import com.hazelcast.spring.context.SpringManagedContext;
import org.apache.commons.lang.StringUtils;
import org.genesys2.spring.hazelcast.HazelcastCacheRegionFactoryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -36,11 +47,9 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@EnableCaching
@Import({ HazelcastConfigCacher.class })
public abstract class HazelcastConfig {
protected final Logger LOG = LoggerFactory.getLogger(HazelcastConfig.class);
......@@ -80,6 +89,101 @@ public abstract class HazelcastConfig {
public ManagedContext managedContext() {
return new SpringManagedContext();
}
@Value("${hazelcast.members}")
private List<String> hazelcastMembers;
@Bean
public HazelcastInstance hazelcast(ManagedContext managedContext) {
Config cfg = new Config();
cfg.setManagedContext(managedContext);
cfg.setInstanceName(instanceName);
GroupConfig groupConfig = cfg.getGroupConfig();
groupConfig.setName(name);
groupConfig.setPassword(password);
cfg.setProperty("hazelcast.merge.first.run.delay.seconds", "5");
cfg.setProperty("hazelcast.merge.next.run.delay.seconds", "5");
cfg.setProperty("hazelcast.logging.type", "log4j");
cfg.setProperty("hazelcast.icmp.enabled", "true");
NetworkConfig network = cfg.getNetworkConfig();
network.setPort(hazelPort);
network.setPortAutoIncrement(true);
if (StringUtils.isNotBlank(hazelPublicAddress)) {
System.out.println("Hazelcast: Using public address " + hazelPublicAddress);
try {
InetAddress ipAddress = InetAddress.getByName(hazelPublicAddress);
LOG.warn("Public ip={} siteLocal={}", ipAddress, ipAddress.isSiteLocalAddress());
network.setPublicAddress(ipAddress.getHostAddress());
} catch (UnknownHostException e) {
LOG.warn("Could not resolve {}, error {}", hazelPublicAddress, e.getMessage());
network.setPublicAddress(hazelPublicAddress);
}
}
if (StringUtils.isNotBlank(hazelInterfaces)) {
for (String hazelInterface : hazelInterfaces.split(",")) {
String trimmed = hazelInterface.trim();
if (StringUtils.isNotBlank(trimmed)) {
System.out.println("Hazelcast: Adding single interface " + trimmed);
try {
InetAddress ipAddress = InetAddress.getByName(hazelPublicAddress);
LOG.warn("Interface ip={} ={}", ipAddress, ipAddress.isSiteLocalAddress());
network.getInterfaces().addInterface(ipAddress.toString());
} catch (UnknownHostException e) {
LOG.warn("Could not resolve {}, error {}", hazelPublicAddress, e.getMessage());
network.getInterfaces().addInterface(trimmed);
}
}
}
}
JoinConfig join = network.getJoin();
if (!hasConfiguredMembers()) {
LOG.warn("Enabling Hazelcast multicast, members={}", hazelcastMembers);
join.getMulticastConfig().setEnabled(true);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(false);
} else {
join.getMulticastConfig().setEnabled(false);
LOG.warn("Enabling TCP/IP discovery for members={}", hazelcastMembers);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setConnectionTimeoutSeconds(20);
List<String> hzMembers=new ArrayList<>();
for (String hazelcastMember : hazelcastMembers) {
String trimmed = hazelcastMember.trim();
if (StringUtils.isNotBlank(trimmed)) {
System.out.println("Hazelcast: Adding member " + trimmed);
try {
InetAddress ipAddress = InetAddress.getByName(trimmed);
LOG.warn("Interface ip={} ={}", ipAddress, ipAddress.isSiteLocalAddress());
hzMembers.add(ipAddress.getHostAddress());
} catch (UnknownHostException e) {
LOG.warn("Could not resolve {}, error {}", trimmed, e.getMessage());
hzMembers.add(trimmed);
}
}
}
tcpIpConfig.setMembers(hzMembers);
}
configureHazelcast(cfg);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
return instance;
}
private boolean hasConfiguredMembers() {
LOG.debug("HZ members={}", hazelcastMembers);
return this.hazelcastMembers != null && this.hazelcastMembers.size() != 0 && this.hazelcastMembers.get(0).length() > 0;
}
@Bean
public ILock taxonomyUpdateLock(HazelcastInstance hazelcast) {
......
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.spring.config;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import com.hazelcast.config.Config;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ManagedContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile({ "cacher", "dev" })
public class HazelcastConfigCacher extends HazelcastConfig {
@Value("${hazelcast.members}")
private List<String> hazelcastMembers;
@Bean
public HazelcastInstance hazelcast(ManagedContext managedContext) {
Config cfg = new Config();
cfg.setManagedContext(managedContext);
cfg.setInstanceName(instanceName);
GroupConfig groupConfig = cfg.getGroupConfig();
groupConfig.setName(name);
groupConfig.setPassword(password);
cfg.setProperty("hazelcast.merge.first.run.delay.seconds", "5");
cfg.setProperty("hazelcast.merge.next.run.delay.seconds", "5");
cfg.setProperty("hazelcast.logging.type", "log4j");
cfg.setProperty("hazelcast.icmp.enabled", "true");
NetworkConfig network = cfg.getNetworkConfig();
network.setPort(hazelPort);
network.setPortAutoIncrement(true);
if (StringUtils.isNotBlank(hazelPublicAddress)) {
System.out.println("Hazelcast: Using public address " + hazelPublicAddress);
try {
InetAddress ipAddress = InetAddress.getByName(hazelPublicAddress);
LOG.warn("Public ip={} siteLocal={}", ipAddress, ipAddress.isSiteLocalAddress());
network.setPublicAddress(ipAddress.getHostAddress());
} catch (UnknownHostException e) {
LOG.warn("Could not resolve {}, error {}", hazelPublicAddress, e.getMessage());
network.setPublicAddress(hazelPublicAddress);
}
}
if (StringUtils.isNotBlank(hazelInterfaces)) {
for (String hazelInterface : hazelInterfaces.split(",")) {
String trimmed = hazelInterface.trim();
if (StringUtils.isNotBlank(trimmed)) {
System.out.println("Hazelcast: Adding single interface " + trimmed);
try {
InetAddress ipAddress = InetAddress.getByName(hazelPublicAddress);
LOG.warn("Interface ip={} ={}", ipAddress, ipAddress.isSiteLocalAddress());
network.getInterfaces().addInterface(ipAddress.toString());
} catch (UnknownHostException e) {
LOG.warn("Could not resolve {}, error {}", hazelPublicAddress, e.getMessage());
network.getInterfaces().addInterface(trimmed);
}
}
}
}
JoinConfig join = network.getJoin();
if (!hasConfiguredMembers()) {
LOG.warn("Enabling Hazelcast multicast, members={}", hazelcastMembers);
join.getMulticastConfig().setEnabled(true);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(false);
} else {
join.getMulticastConfig().setEnabled(false);
LOG.warn("Enabling TCP/IP discovery for members={}", hazelcastMembers);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setConnectionTimeoutSeconds(20);
List<String> hzMembers=new ArrayList<>();
for (String hazelcastMember : hazelcastMembers) {
String trimmed = hazelcastMember.trim();
if (StringUtils.isNotBlank(trimmed)) {
System.out.println("Hazelcast: Adding member " + trimmed);
try {
InetAddress ipAddress = InetAddress.getByName(trimmed);
LOG.warn("Interface ip={} ={}", ipAddress, ipAddress.isSiteLocalAddress());
hzMembers.add(ipAddress.getHostAddress());
} catch (UnknownHostException e) {
LOG.warn("Could not resolve {}, error {}", trimmed, e.getMessage());
hzMembers.add(trimmed);
}
}
}
tcpIpConfig.setMembers(hzMembers);
}
configureHazelcast(cfg);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
return instance;
}
private boolean hasConfiguredMembers() {
LOG.debug("HZ members={}", hazelcastMembers);
return this.hazelcastMembers != null && this.hazelcastMembers.size() != 0 && this.hazelcastMembers.get(0).length() > 0;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment