Skip to content

Commit

Permalink
Fix hazelcast serialization problem
Browse files Browse the repository at this point in the history
closes #1429
  • Loading branch information
srempfer committed Nov 10, 2020
1 parent fd58e6d commit 5d04ee9
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ language: java
dist: xenial
jdk: openjdk8

services:
- docker

env:
- TRAVIS_NODE_VERSION="lts/*"

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<hazelcast-tests.version>3.12.8</hazelcast-tests.version>
<findbugs-jsr305.version>3.0.2</findbugs-jsr305.version>
<awaitility.version>4.0.3</awaitility.version>
<testcontainers.version>1.14.3</testcontainers.version>

<!-- plugin versions -->
<build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version>
Expand Down
12 changes: 12 additions & 0 deletions spring-boot-admin-build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
Expand Down
15 changes: 15 additions & 0 deletions spring-boot-admin-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,20 @@
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ private Registration(String name, @Nullable String managementUrl, String healthU
this.healthUrl = healthUrl;
this.serviceUrl = serviceUrl;
this.source = source;
this.metadata = new LinkedHashMap<>(metadata);
this.metadata = new LinkedHashMap<>();
metadata.forEach((key, value) -> this.metadata.put(key, value));
}

public static Registration.Builder create(String name, String healthUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public abstract class AbstractEventStoreTest {

private final InstanceId id = InstanceId.of("id");

private final Registration registration = Registration.create("foo", "http://health").build();
private final Registration registration = Registration.create("foo", "http://health").metadata("test", "dummy")
.build();

protected abstract InstanceEventStore createStore(int maxLogSizePerAggregate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class HazelcastEventStoreTest extends AbstractEventStoreTest {
@Override
protected InstanceEventStore createStore(int maxLogSizePerAggregate) {
HazelcastInstance hazelcast = new TestHazelcastInstanceFactory(1).newHazelcastInstance();
return new HazelcastEventStore(maxLogSizePerAggregate, hazelcast.getMap("testList"));
return new HazelcastEventStore(maxLogSizePerAggregate,
hazelcast.getMap("testList" + System.currentTimeMillis()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2014-2018 the original author or authors.
*
* 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 de.codecentric.boot.admin.server.eventstore;

import java.util.List;

import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastClientFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.values.InstanceId;

@Testcontainers
public class HazelcastEventStoreWithClientConfigTest extends AbstractEventStoreTest {

@Container
private static GenericContainer hazelcastServer = new GenericContainer("hazelcast/hazelcast:3.12.8")
.withExposedPorts(5701);

private final HazelcastInstance hazelcast;

public HazelcastEventStoreWithClientConfigTest() {
this.hazelcast = createHazelcastInstance();
}

@Override
protected InstanceEventStore createStore(int maxLogSizePerAggregate) {
IMap<InstanceId, List<InstanceEvent>> eventLog = hazelcast.getMap("testList" + System.currentTimeMillis());
return new HazelcastEventStore(maxLogSizePerAggregate, eventLog);
}

private HazelcastInstance createHazelcastInstance() {
String address = hazelcastServer.getContainerIpAddress() + ":" + hazelcastServer.getMappedPort(5701);

ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().addAddress(address);

return (new HazelcastClientFactory(clientConfig)).getHazelcastInstance();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2014-2018 the original author or authors.
*
* 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 de.codecentric.boot.admin.server.eventstore;

import java.util.List;

import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MergePolicyConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.map.merge.PutIfAbsentMapMergePolicy;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory;

import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.values.InstanceId;

import static de.codecentric.boot.admin.server.config.AdminServerHazelcastAutoConfiguration.DEFAULT_NAME_EVENT_STORE_MAP;
import static de.codecentric.boot.admin.server.config.AdminServerHazelcastAutoConfiguration.DEFAULT_NAME_SENT_NOTIFICATIONS_MAP;
import static java.util.Collections.singletonList;

public class HazelcastEventStoreWithServerConfigTest extends AbstractEventStoreTest {

private final HazelcastInstance hazelcast;

public HazelcastEventStoreWithServerConfigTest() {
this.hazelcast = createHazelcastInstance();
}

@Override
protected InstanceEventStore createStore(int maxLogSizePerAggregate) {
IMap<InstanceId, List<InstanceEvent>> eventLogs = hazelcast.getMap("testList" + System.currentTimeMillis());
return new HazelcastEventStore(maxLogSizePerAggregate, eventLogs);
}

private HazelcastInstance createHazelcastInstance() {
Config config = createHazelcastConfig();
return (new HazelcastInstanceFactory(config)).getHazelcastInstance();
}

// config from sample project
private Config createHazelcastConfig() {
// This map is used to store the events.
// It should be configured to reliably hold all the data,
// Spring Boot Admin will compact the events, if there are too many
MapConfig eventStoreMap = new MapConfig(DEFAULT_NAME_EVENT_STORE_MAP).setInMemoryFormat(InMemoryFormat.OBJECT)
.setBackupCount(1).setEvictionPolicy(EvictionPolicy.NONE)
.setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));

// This map is used to deduplicate the notifications.
// If data in this map gets lost it should not be a big issue as it will atmost
// lead to
// the same notification to be sent by multiple instances
MapConfig sentNotificationsMap = new MapConfig(DEFAULT_NAME_SENT_NOTIFICATIONS_MAP)
.setInMemoryFormat(InMemoryFormat.OBJECT).setBackupCount(1).setEvictionPolicy(EvictionPolicy.LRU)
.setMergePolicyConfig(new MergePolicyConfig(PutIfAbsentMapMergePolicy.class.getName(), 100));

Config config = new Config();
config.addMapConfig(eventStoreMap);
config.addMapConfig(sentNotificationsMap);
config.setProperty("hazelcast.jmx", "true");

// WARNING: This setups a local cluster, you change it to fit your needs.
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = config.getNetworkConfig().getJoin().getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setMembers(singletonList("127.0.0.1"));
return config;
}

}

0 comments on commit 5d04ee9

Please sign in to comment.