From 4a51b129a399fdf4fb87adcae5813aa1a058f5b5 Mon Sep 17 00:00:00 2001 From: leventov Date: Thu, 6 Nov 2014 13:45:08 +0300 Subject: [PATCH] Many renames, API refactorings, classes/interfaces made private to net.openhft.chronicle.map package Conflicts: src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java --- README.md | 7 +- .../hash/ChannelProviderBuilder.java | 90 ----- .../chronicle/hash/ChronicleHashBuilder.java | 152 ++++----- .../hash/ChronicleHashInstanceBuilder.java | 57 ++++ .../hash/StatelessClientBuilder.java | 45 +++ .../hash/replication/AbstractReplication.java | 66 ++++ .../replication}/IdentifierListener.java | 2 +- .../hash/replication/ReplicationChannel.java | 35 ++ .../hash/replication/ReplicationHub.java | 82 +++++ .../hash/replication/SimpleReplication.java | 37 +++ .../TcpConfig.java} | 42 +-- .../{ => replication}/ThrottlingConfig.java | 2 +- .../hash/{ => replication}/TimeProvider.java | 3 +- .../UdpConfig.java} | 27 +- .../map/AbstractChannelReplicator.java | 2 +- .../map/AbstractChronicleMapBuilder.java | 224 ++++++------- .../{hash => map}/ChannelProvider.java | 307 +++++++++++------- .../chronicle/map/InstanceBuilder.java | 73 +++++ .../openhft/chronicle/map/NodeDiscovery.java | 15 +- .../net/openhft/chronicle/map/Replica.java | 2 +- .../chronicle/map/ReplicatedChronicleMap.java | 11 +- .../net/openhft/chronicle/map/Replicator.java | 2 +- .../openhft/chronicle/map/Replicators.java | 23 +- .../chronicle/map/StatelessChronicleMap.java | 10 +- .../StatelessMapBuilder.java} | 38 ++- .../openhft/chronicle/map/TcpReplicator.java | 14 +- .../chronicle/map/UdpChannelReplicator.java | 4 +- .../openhft/chronicle/map/UdpReplicator.java | 6 +- .../chronicle/map/VanillaChronicleMap.java | 4 +- .../chronicle/set/ChronicleSetBuilder.java | 48 +-- .../chronicle/set/InstanceBuilder.java | 62 ++++ .../chronicle/set/StatelessSetBuilder.java | 42 +++ src/test/java/eg/BigData.java | 2 +- .../chronicle/map/AcquireGetUsingMain.java | 4 +- .../net/openhft/chronicle/map/Builder.java | 6 +- .../chronicle/map/CHMLatencyTestMain.java | 2 +- .../net/openhft/chronicle/map/CHMTest5.java | 3 +- .../chronicle/map/ChannelReplicationTest.java | 38 +-- .../chronicle/map/ChronicleMapTest.java | 11 +- .../chronicle/map/DefaultValueTest.java | 6 +- .../chronicle/map/EntryCountMapTest.java | 5 +- ...tListenerTestWithTCPSocketReplication.java | 28 +- .../map/PostChannelCreationTest.java | 34 +- .../openhft/chronicle/map/SerializerTest.java | 4 +- .../chronicle/map/SingleMapChannelTest.java | 32 +- .../chronicle/map/StatelessClientTest.java | 48 ++- .../map/TCPSocketReplication4WayMapTest.java | 14 +- .../TCPSocketReplicationBootStrapTests.java | 20 +- .../map/TCPSocketReplicationIntValueTest.java | 14 +- .../map/TCPSocketReplicationTest.java | 5 +- ...CPSocketReplicationTest3VoidValueTest.java | 2 +- .../map/TCPSocketReplicationTest3way.java | 2 +- ...cketReplicationTest3wayPutReturnsNull.java | 8 +- ...CPSocketReplicationTestPostConnection.java | 18 +- .../map/TimeBasedReplicationTests.java | 90 ++--- .../map/TwoMapOnDifferentServers.java | 13 +- .../map/UDPSocketReplicationTest.java | 10 +- .../chronicle/map/example/ChannelExample.java | 59 ++-- .../map/example/DistributedSequenceMain.java | 3 - .../OpenJDKAndHashMapExamplesTest.java | 6 +- .../pingpong_latency/PingPongCASLeft.java | 7 - .../map/ipc/StateMachineTutorial.java | 5 - .../map/jrs166/map/ChronicleMapTest.java | 24 -- .../chronicle/map/jrs166/map/MapCheck.java | 5 +- .../jrs166/map/StatelessChronicleMapTest.java | 17 +- .../map/utility/ProcessInstanceLimiter.java | 3 +- .../SetForTCPSocketReplicationTest3way.java | 9 +- 67 files changed, 1241 insertions(+), 850 deletions(-) delete mode 100644 src/main/java/net/openhft/chronicle/hash/ChannelProviderBuilder.java create mode 100644 src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceBuilder.java create mode 100644 src/main/java/net/openhft/chronicle/hash/StatelessClientBuilder.java create mode 100644 src/main/java/net/openhft/chronicle/hash/replication/AbstractReplication.java rename src/main/java/net/openhft/chronicle/{map => hash/replication}/IdentifierListener.java (95%) create mode 100644 src/main/java/net/openhft/chronicle/hash/replication/ReplicationChannel.java create mode 100644 src/main/java/net/openhft/chronicle/hash/replication/ReplicationHub.java create mode 100644 src/main/java/net/openhft/chronicle/hash/replication/SimpleReplication.java rename src/main/java/net/openhft/chronicle/hash/{TcpReplicationConfig.java => replication/TcpConfig.java} (80%) rename src/main/java/net/openhft/chronicle/hash/{ => replication}/ThrottlingConfig.java (99%) rename src/main/java/net/openhft/chronicle/hash/{ => replication}/TimeProvider.java (95%) rename src/main/java/net/openhft/chronicle/hash/{UdpReplicationConfig.java => replication/UdpConfig.java} (78%) rename src/main/java/net/openhft/chronicle/{hash => map}/ChannelProvider.java (56%) create mode 100644 src/main/java/net/openhft/chronicle/map/InstanceBuilder.java rename src/main/java/net/openhft/chronicle/{hash/StatelessBuilder.java => map/StatelessMapBuilder.java} (59%) create mode 100644 src/main/java/net/openhft/chronicle/set/InstanceBuilder.java create mode 100644 src/main/java/net/openhft/chronicle/set/StatelessSetBuilder.java diff --git a/README.md b/README.md index 7b49b0e53..286fc0b6e 100644 --- a/README.md +++ b/README.md @@ -231,9 +231,6 @@ the instances of Chronicle Map on the same server. The name and location of the up to you. For the best performance on many unix systems we recommend using [tmpfs](http://en.wikipedia.org/wiki/Tmpfs). - - - ### Sharing Data Between Two or More Maps Since this file is memory mapped, if you were to create another instance of the Chronicle Map, pointing to the same file, both Chronicle Maps use this file as a common memory store, which they @@ -1026,7 +1023,7 @@ but in your example you should choose the host of the state-full server and the .statelessClient(new InetSocketAddress(, )) ``` -the ".statelessClient(..)" returns an instance of `StatelessClientConfig`, which has only a few +the ".statelessClient(..)" returns an instance of `StatelessClientBuilder`, which has only a few own configurations and `create()` method to create a new stateless client. method tells `ChronicleMap` that its going to build a stateless client. If you don’t add this line a normal state-full `ChronicleMap` will be created. For this example @@ -1665,4 +1662,4 @@ Or is there a lot of overhead for other functionality which i do not intend to u #### Answer You can share the data between processes on the same machine but this doesn't add overhead. -You can add replication between machines. But this uses extra classes which are not used in the simple case ie it is as if they were not there, only an option. \ No newline at end of file +You can add replication between machines. But this uses extra classes which are not used in the simple case ie it is as if they were not there, only an option. diff --git a/src/main/java/net/openhft/chronicle/hash/ChannelProviderBuilder.java b/src/main/java/net/openhft/chronicle/hash/ChannelProviderBuilder.java deleted file mode 100644 index d9a26b346..000000000 --- a/src/main/java/net/openhft/chronicle/hash/ChannelProviderBuilder.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2014 Higher Frequency Trading - * - * http://www.higherfrequencytrading.com - * - * 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 net.openhft.chronicle.hash; - -import net.openhft.chronicle.map.TcpReplicator; -import net.openhft.chronicle.map.UdpReplicator; - -import java.io.IOException; - -/** - * @author Rob Austin. - */ -public final class ChannelProviderBuilder { - - byte identifier; - int maxEntrySize = 1024; - int maxNumberOfChronicles = 128; - private UdpReplicationConfig udpReplicationConfig = null; - private TcpReplicationConfig tcpReplicationConfig = null; - - - - public int maxEntrySize() { - return maxEntrySize; - } - - public ChannelProviderBuilder maxEntrySize(int maxEntrySize) { - this.maxEntrySize = maxEntrySize; - return this; - } - - public ChannelProviderBuilder replicators(byte identifier, ReplicationConfig... replicationConfigs) { - this.identifier = identifier; - - - for (ReplicationConfig replicationConfig : replicationConfigs) { - - if (replicationConfig instanceof TcpReplicationConfig) { - this.tcpReplicationConfig = (TcpReplicationConfig) replicationConfig; - } else if (replicationConfig instanceof UdpReplicationConfig) { - this.udpReplicationConfig = (UdpReplicationConfig) replicationConfig; - } else - throw new UnsupportedOperationException(); - } - return this; - } - - public ChannelProviderBuilder maxNumberOfChronicles(int maxNumberOfChronicles) { - this.maxNumberOfChronicles = maxNumberOfChronicles; - return this; - } - - public ChannelProvider create() throws IOException { - final ChannelProvider replicatingCluster = new ChannelProvider(this); - if (tcpReplicationConfig != null) { - final TcpReplicator tcpReplicator = new TcpReplicator( - replicatingCluster.asReplica, - replicatingCluster.asEntryExternalizable, - tcpReplicationConfig, - maxEntrySize, null); - replicatingCluster.add(tcpReplicator); - } - - if (udpReplicationConfig != null) { - final UdpReplicator udpReplicator = - new UdpReplicator(replicatingCluster.asReplica, - replicatingCluster.asEntryExternalizable, - udpReplicationConfig, - maxEntrySize); - replicatingCluster.add(udpReplicator); - } - return replicatingCluster; - } -} diff --git a/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java index ab5624b35..59828e6b8 100644 --- a/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java @@ -18,6 +18,9 @@ package net.openhft.chronicle.hash; +import net.openhft.chronicle.hash.replication.SimpleReplication; +import net.openhft.chronicle.hash.replication.TcpConfig; +import net.openhft.chronicle.hash.replication.TimeProvider; import net.openhft.chronicle.map.*; import net.openhft.chronicle.set.ChronicleSet; import net.openhft.chronicle.set.ChronicleSetBuilder; @@ -32,6 +35,7 @@ import org.jetbrains.annotations.NotNull; import java.io.Externalizable; import java.io.File; import java.io.IOException; +import java.net.InetSocketAddress; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -224,67 +228,6 @@ public interface ChronicleHashBuilder - * the section about replication in ChronicleMap manual for more information. - * - *

Another way to establish replication is {@link #channel(ChannelProvider.ChronicleChannel)} - * method. - * - *

By default, hash containers, created by this builder doesn't replicate their data. - * - *

This method call overrides all previous replication configurations of this builder, made - * either by this method or {@link #channel(ChannelProvider.ChronicleChannel)} method. - * - * @param identifier the new {@linkplain Replica#identifier() identifier} of the containers, - * created by this builder - * @param replicators several replicators, used to sync the data across replicating nodes - * independently - * @return this builder back - * @see #channel(ChannelProvider.ChronicleChannel) - * @see #disableReplication() - */ - B replicators(byte identifier, ReplicationConfig... replicators); - - /** - * Configures replication of the hash containers, created by this builder, via so called - * "channels". See the - * section about Channels and ChannelProvider in ChronicleMap manual for more information. - * - *

Another way to establish replication if {@link #replicators(byte, ReplicationConfig[])} - * method. - * - *

By default, hash containers, created by this builder doesn't replicate their data. - * - *

This method call overrides all previous replication configurations of this builder, made - * either by means of this method or {@link #replicators(byte, ReplicationConfig[])} method - * calls. - * - * @param chronicleChannel the channel responsible for gathering updates of hash containers, - * created by this builder, and replicating them over network - * @return this builder object back - * @see #replicators(byte, ReplicationConfig[]) - * @see #disableReplication() - */ - B channel(ChannelProvider.ChronicleChannel chronicleChannel); - - /** - * Specifies that hash containers, created by this builder, shouldn't replicate their data over - * network. This method call clears any replication configurations, made via {@link - * #replicators(byte, ReplicationConfig[])} or {@link #channel(ChannelProvider.ChronicleChannel)} - * method call. - * - *

By default replication is not configured, so this method is useful only to ensure - * that hash containers, created by some inherited or passed builder, wouldn't replicate. I. e. - * in rare cases. - * - * @return this builder back - * @see #replicators(byte, ReplicationConfig[]) - * @see #channel(ChannelProvider.ChronicleChannel) - */ - B disableReplication(); - /** * Configures a time provider, used by hash containers, created by this builder, for needs of * replication consensus protocol (conflicting data updates resolution). @@ -293,8 +236,7 @@ public interface ChronicleHashBuilder + * the section about replication in ChronicleMap manual for more information. * - * @param statelessBuilder Stateless Client Config, {@code null} demotes no statelessBuilder + *

By default, hash containers, created by this builder doesn't replicate their data. + * + *

This method call overrides all previous replication configurations of this builder, made + * either by this method or {@link #replication(byte, TcpConfig)} shortcut method. + * + * @param replication the replication config * @return this builder back + * @see ChronicleHashInstanceBuilder#replicated(SimpleReplication) + * @see #replication(byte, TcpConfig) */ - B stateless(StatelessBuilder statelessBuilder); - + B replication(SimpleReplication replication); /** - * Specifies that hash containers us the specified file, if create is called and this is not set - * the map will be created in off-heap memory, and changes will not be persisted to disk. If a - * file is provided all changes to the map are persisted to disk (this is an operating system - * guarantee) independently from JVM process lifecycle. + * Shortcut for {@code replication(SimpleReplication.builder() + * .tcpTransportAndNetwork(tcpTransportAndNetwork).create(identifier))}. * - * @param file the file with existing hash container or a desired location of a new off-heap - * persisted hash container + * @param identifier the network-wide identifier of the containers, created by this builder + * @param tcpTransportAndNetwork configuration of tcp connection and network * @return this builder back - * @throws IOException If the file cannot be created and read. - * @see ChronicleHash#file() - * @see ChronicleHash#close() + * @see #replication(SimpleReplication) + * @see ChronicleHashInstanceBuilder#replicated(byte, TcpConfig) */ - B file(File file) throws IOException; + B replication(byte identifier, TcpConfig tcpTransportAndNetwork); + + StatelessClientBuilder statelessClient(InetSocketAddress remoteAddress); + + ChronicleHashInstanceBuilder instance(); /** * Creates a new hash container, storing it's data in off-heap memory, not mapped to any file. @@ -426,15 +367,42 @@ public interface ChronicleHashBuilderThis method is a shortcut for {@code instance().create()}. + * * @return a new off-heap hash container * @throws IOException if any IO error relates to off-heap memory allocation occurs - * @see #create() + * @see #createPersistedTo(File) + * @see #instance() */ C create() throws IOException; - - - - + /** + * Opens a hash container residing the specified file, or creates a new one if the file not yet + * exists and maps its off-heap memory to the file. All changes to the map are persisted to disk + * (this is an operating system guarantee) independently from JVM process lifecycle. + * + *

Multiple containers could give access to the same data simultaneously, either inside a + * single JVM or across processes. Access is synchronized correctly across all instances, i. e. + * hash container mapping the data from the first JVM isn't able to modify the data, + * concurrently accessed from the second JVM by another hash container instance, mapping the + * same data. + * + *

On container's {@link ChronicleHash#close() close()} the data isn't removed, it remains on + * disk and available to be opened again (given the same file name) or during different JVM + * run. + * + *

This method is shortcut for {@code instance().persistedTo(file).create()}. + * + * @param file the file with existing hash container or a desired location of a new off-heap + * persisted hash container + * @return a hash container mapped to the given file + * @throws IOException if any IO error, related to off-heap memory allocation or file mapping + * occurs + * @see ChronicleHash#file() + * @see ChronicleHash#close() + * @see #create() + * @see ChronicleHashInstanceBuilder#persistedTo(File) + */ + C createPersistedTo(File file) throws IOException; } diff --git a/src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceBuilder.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceBuilder.java new file mode 100644 index 000000000..64c046c33 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceBuilder.java @@ -0,0 +1,57 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.hash; + +import net.openhft.chronicle.hash.replication.ReplicationChannel; +import net.openhft.chronicle.hash.replication.SimpleReplication; +import net.openhft.chronicle.hash.replication.TcpConfig; + +import java.io.File; +import java.io.IOException; + +public interface ChronicleHashInstanceBuilder { + + ChronicleHashInstanceBuilder replicated(byte identifier, TcpConfig tcpTransportAndNetwork); + + ChronicleHashInstanceBuilder replicated(SimpleReplication replication); + + /** + * Configures replication of the hash containers, created by this builder, via so called + * "channels". See + * the + * section about Channels and ChannelProvider in ChronicleMap manual for more information. + * + *

Another way to establish replication is {@link #replicated(SimpleReplication)} method + * or it's shortcut: {@link #replicated(byte, TcpConfig)}. + * + *

By default, hash containers, created by this builder doesn't replicate their data. + * + *

This method call overrides all previous replication configurations of this builder, made + * either by means of this method, {@link #replicated(SimpleReplication)} + * or {@link #replicated(byte, TcpConfig)} method calls. + * + * @param channel the channel responsible for gathering updates of hash containers, + * created by this builder, and replicating them over network + * @return this builder object back + * @see #replicated(SimpleReplication) + */ + ChronicleHashInstanceBuilder replicatedViaChannel(ReplicationChannel channel); + + ChronicleHashInstanceBuilder persistedTo(File file); + + C create() throws IOException; +} diff --git a/src/main/java/net/openhft/chronicle/hash/StatelessClientBuilder.java b/src/main/java/net/openhft/chronicle/hash/StatelessClientBuilder.java new file mode 100644 index 000000000..0906fb72c --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/StatelessClientBuilder.java @@ -0,0 +1,45 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.hash; + +import net.openhft.chronicle.map.ChronicleMap; +import net.openhft.chronicle.set.ChronicleSet; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +/** + * Builder of stateless {@link ChronicleHash} implementation, this stateless implementation will be + * referred to as a stateless client as it + * will not hold any of its own data locally, the stateless client will perform Remote Procedure + * Calls ( RPC ) to another {@link ChronicleMap} or {@link ChronicleSet} which we will refer to + * as the server. The server will hold all your data, the server can not it’s self be a + * stateless client. Your stateless client must be connected to the server via TCP/IP. The + * stateless client will delegate all your method calls to the remote sever. The stateless + * client operations will block, in other words the stateless client will wait for the server to + * send a response before continuing to the next operation. The stateless client could be + * consider to be a ClientProxy to {@link ChronicleMap} or {@link ChronicleSet} running on + * another host + * + * @param the type of {@code ChronicleHash} accessed remotely, {@code ChronicleMap} or + * {@code ChronicleSet} + */ +public interface StatelessClientBuilder { + StatelessClientBuilder timeout(long timeout, TimeUnit units); + + C create() throws IOException; +} diff --git a/src/main/java/net/openhft/chronicle/hash/replication/AbstractReplication.java b/src/main/java/net/openhft/chronicle/hash/replication/AbstractReplication.java new file mode 100644 index 000000000..ac269165b --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/replication/AbstractReplication.java @@ -0,0 +1,66 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.hash.replication; + +import org.jetbrains.annotations.Nullable; + +class AbstractReplication { + protected final byte localIdentifier; + protected final UdpConfig udpConfig; + protected final TcpConfig tcpConfig; + + AbstractReplication(byte localIdentifier, Builder builder) { + udpConfig = builder.udpConfig; + this.localIdentifier = localIdentifier; + tcpConfig = builder.tcpConfig; + } + + public byte identifier() { + return localIdentifier; + } + + @Nullable + public TcpConfig tcpTransportAndNetwork() { + return tcpConfig; + } + + @Nullable + public UdpConfig udpTransport() { + return udpConfig; + } + + static abstract class Builder { + protected UdpConfig udpConfig = null; + protected TcpConfig tcpConfig = null; + + public B tcpTransportAndNetwork(TcpConfig tcpConfig) { + this.tcpConfig = tcpConfig; + return (B) this; + } + + public B udpTransport(UdpConfig udpConfig) { + this.udpConfig = udpConfig; + return (B) this; + } + + void check() { + if (udpConfig == null && tcpConfig == null) + throw new IllegalStateException( + "At least one transport method (TCP or UDP) should be configured"); + } + } +} diff --git a/src/main/java/net/openhft/chronicle/map/IdentifierListener.java b/src/main/java/net/openhft/chronicle/hash/replication/IdentifierListener.java similarity index 95% rename from src/main/java/net/openhft/chronicle/map/IdentifierListener.java rename to src/main/java/net/openhft/chronicle/hash/replication/IdentifierListener.java index d1b12f369..959f88d8e 100644 --- a/src/main/java/net/openhft/chronicle/map/IdentifierListener.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/IdentifierListener.java @@ -16,7 +16,7 @@ * limitations under the License. */ -package net.openhft.chronicle.map; +package net.openhft.chronicle.hash.replication; import java.net.SocketAddress; diff --git a/src/main/java/net/openhft/chronicle/hash/replication/ReplicationChannel.java b/src/main/java/net/openhft/chronicle/hash/replication/ReplicationChannel.java new file mode 100644 index 000000000..72527c251 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/replication/ReplicationChannel.java @@ -0,0 +1,35 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.hash.replication; + +public final class ReplicationChannel { + private ReplicationHub hub; + private final short channelId; + + ReplicationChannel(ReplicationHub hub, short channelId) { + this.hub = hub; + this.channelId = channelId; + } + + public ReplicationHub hub() { + return hub; + } + + public short channelId() { + return channelId; + } +} diff --git a/src/main/java/net/openhft/chronicle/hash/replication/ReplicationHub.java b/src/main/java/net/openhft/chronicle/hash/replication/ReplicationHub.java new file mode 100644 index 000000000..5e24837fe --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/replication/ReplicationHub.java @@ -0,0 +1,82 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.hash.replication; + +import net.openhft.chronicle.hash.ReplicationConfig; + +import java.io.IOException; + +public final class ReplicationHub extends AbstractReplication { + + public static Builder builder() { + return new Builder(); + } + + private final int maxEntrySize; + private final ReplicationChannel[] channels; + + private ReplicationHub(byte localIdentifier, Builder builder) { + super(localIdentifier, builder); + maxEntrySize = builder.maxEntrySize; + channels = new ReplicationChannel[builder.maxNumberOfChronicles]; + } + + public int maxEntrySize() { + return maxEntrySize; + } + + public int maxNumberOfChannels() { + return channels.length; + } + + public synchronized ReplicationChannel createChannel(short channelId) { + if (channelId < 0) + throw new IllegalArgumentException("channelId should be positive"); + if (channelId >= maxNumberOfChannels()) + throw new IllegalArgumentException("maxNumberOfChannels is configured (or defaulted) " + + "to " + maxNumberOfChannels() + ", channelId=" + channelId + " is requested"); + if (channels[channelId] != null) + throw new IllegalArgumentException("The requested channelId=" + channelId + + " is already in use"); + ReplicationChannel channel = new ReplicationChannel(this, channelId); + channels[channelId] = channel; + return channel; + } + + public static final class Builder extends AbstractReplication.Builder { + + private int maxEntrySize = 1024; + private int maxNumberOfChronicles = 128; + + private Builder() {} + + public Builder maxEntrySize(int maxEntrySize) { + this.maxEntrySize = maxEntrySize; + return this; + } + + public Builder maxNumberOfChannels(int maxNumberOfChannels) { + this.maxNumberOfChronicles = maxNumberOfChannels; + return this; + } + + public ReplicationHub create(byte identifier) throws IOException { + check(); + return new ReplicationHub(identifier, this); + } + } +} diff --git a/src/main/java/net/openhft/chronicle/hash/replication/SimpleReplication.java b/src/main/java/net/openhft/chronicle/hash/replication/SimpleReplication.java new file mode 100644 index 000000000..4e1031bee --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/replication/SimpleReplication.java @@ -0,0 +1,37 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.hash.replication; + +public final class SimpleReplication extends AbstractReplication { + + public static Builder builder() { + return new Builder(); + } + + SimpleReplication(byte localIdentifier, Builder builder) { + super(localIdentifier, builder); + } + + public static final class Builder extends AbstractReplication.Builder { + private Builder() {} + + public SimpleReplication create(byte identifier) { + check(); + return new SimpleReplication(identifier, this); + } + } +} diff --git a/src/main/java/net/openhft/chronicle/hash/TcpReplicationConfig.java b/src/main/java/net/openhft/chronicle/hash/replication/TcpConfig.java similarity index 80% rename from src/main/java/net/openhft/chronicle/hash/TcpReplicationConfig.java rename to src/main/java/net/openhft/chronicle/hash/replication/TcpConfig.java index 22e4aa6de..824cd606c 100644 --- a/src/main/java/net/openhft/chronicle/hash/TcpReplicationConfig.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/TcpConfig.java @@ -16,9 +16,9 @@ * limitations under the License. */ -package net.openhft.chronicle.hash; +package net.openhft.chronicle.hash.replication; -import net.openhft.chronicle.map.IdentifierListener; +import net.openhft.chronicle.hash.ReplicationConfig; import org.jetbrains.annotations.NotNull; @@ -33,7 +33,7 @@ import static java.util.Collections.unmodifiableSet; import static java.util.concurrent.TimeUnit.SECONDS; -public class TcpReplicationConfig implements ReplicationConfig { +public class TcpConfig implements ReplicationConfig { private static final int DEFAULT_PACKET_SIZE = 1024 * 8; private static final long DEFAULT_HEART_BEAT_INTERVAL = 20; @@ -46,17 +46,17 @@ public class TcpReplicationConfig implements ReplicationConfig { private ThrottlingConfig throttlingConfig = ThrottlingConfig.noThrottling(); private long heartBeatInterval = DEFAULT_HEART_BEAT_INTERVAL; private TimeUnit heartBeatIntervalUnit = DEFAULT_HEART_BEAT_INTERVAL_UNIT; - public IdentifierListener identifierListener; + private IdentifierListener identifierListener; - TcpReplicationConfig() { + TcpConfig() { } - public IdentifierListener identifierListener() { + public IdentifierListener nonUniqueIdentifierListener() { return identifierListener; } - public TcpReplicationConfig nonUniqueIdentifierListener(@NotNull final IdentifierListener + public TcpConfig nonUniqueIdentifierListener(@NotNull final IdentifierListener identifierListener) { this.identifierListener = identifierListener; return this; @@ -66,7 +66,7 @@ public class TcpReplicationConfig implements ReplicationConfig { return autoReconnectedUponDroppedConnection; } - public TcpReplicationConfig autoReconnectedUponDroppedConnection(boolean autoReconnectedUponDroppedConnection) { + public TcpConfig autoReconnectedUponDroppedConnection(boolean autoReconnectedUponDroppedConnection) { this.autoReconnectedUponDroppedConnection = autoReconnectedUponDroppedConnection; return this; } @@ -75,7 +75,7 @@ public class TcpReplicationConfig implements ReplicationConfig { return throttlingConfig; } - public TcpReplicationConfig throttlingConfig(ThrottlingConfig throttlingConfig) { + public TcpConfig throttlingConfig(ThrottlingConfig throttlingConfig) { this.throttlingConfig = throttlingConfig; return this; } @@ -88,7 +88,7 @@ public class TcpReplicationConfig implements ReplicationConfig { return serverPort; } - public TcpReplicationConfig serverPort(int serverPort) { + public TcpConfig serverPort(int serverPort) { this.serverPort = serverPort; return this; } @@ -97,7 +97,7 @@ public class TcpReplicationConfig implements ReplicationConfig { return endpoints; } - public TcpReplicationConfig endpoints(Set endpoints) { + public TcpConfig endpoints(Set endpoints) { for (final InetSocketAddress endpoint : endpoints) { if (endpoint.getPort() == serverPort && "localhost".equals(endpoint.getHostName())) @@ -113,11 +113,11 @@ public class TcpReplicationConfig implements ReplicationConfig { return packetSize; } - public static TcpReplicationConfig of(int serverPort, InetSocketAddress... endpoints) { + public static TcpConfig of(int serverPort, InetSocketAddress... endpoints) { return of(serverPort, Arrays.asList(endpoints)); } - public static TcpReplicationConfig of(int serverPort, Collection endpoints) { + public static TcpConfig of(int serverPort, Collection endpoints) { // at least in tests, we configure "receive-only" replication without endpoints. // TODO decide what to do with this case // if (endpoints.isEmpty()) @@ -128,25 +128,25 @@ public class TcpReplicationConfig implements ReplicationConfig { + " can not point to the same port as the server"); } - TcpReplicationConfig tcpReplicationConfig = new TcpReplicationConfig(); + TcpConfig tcpConfig = new TcpConfig(); - tcpReplicationConfig.serverPort(serverPort); - tcpReplicationConfig.endpoints = unmodifiableSet(new HashSet(endpoints)); + tcpConfig.serverPort(serverPort); + tcpConfig.endpoints = unmodifiableSet(new HashSet(endpoints)); - return tcpReplicationConfig; + return tcpConfig; } - public TcpReplicationConfig packetSize(int packetSize) { + public TcpConfig packetSize(int packetSize) { if (packetSize <= 0) throw new IllegalArgumentException(); this.packetSize = packetSize; return this; } - public TcpReplicationConfig throttlingConfig(long heartBeatInterval, TimeUnit heartBeatIntervalUnit) { + public TcpConfig throttlingConfig(long heartBeatInterval, TimeUnit heartBeatIntervalUnit) { this.heartBeatInterval = heartBeatInterval; this.heartBeatIntervalUnit = heartBeatIntervalUnit; return this; @@ -158,7 +158,7 @@ public class TcpReplicationConfig implements ReplicationConfig { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - TcpReplicationConfig that = (TcpReplicationConfig) o; + TcpConfig that = (TcpConfig) o; if (autoReconnectedUponDroppedConnection != that.autoReconnectedUponDroppedConnection) return false; if (heartBeatInterval != that.heartBeatInterval) return false; @@ -197,7 +197,7 @@ public class TcpReplicationConfig implements ReplicationConfig { '}'; } - public TcpReplicationConfig heartBeatInterval(long time, TimeUnit unit) { + public TcpConfig heartBeatInterval(long time, TimeUnit unit) { this.heartBeatInterval = time; this.heartBeatIntervalUnit = unit; return this; diff --git a/src/main/java/net/openhft/chronicle/hash/ThrottlingConfig.java b/src/main/java/net/openhft/chronicle/hash/replication/ThrottlingConfig.java similarity index 99% rename from src/main/java/net/openhft/chronicle/hash/ThrottlingConfig.java rename to src/main/java/net/openhft/chronicle/hash/replication/ThrottlingConfig.java index dbb466e2d..99aaf5903 100644 --- a/src/main/java/net/openhft/chronicle/hash/ThrottlingConfig.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/ThrottlingConfig.java @@ -16,7 +16,7 @@ * limitations under the License. */ -package net.openhft.chronicle.hash; +package net.openhft.chronicle.hash.replication; import java.util.concurrent.TimeUnit; diff --git a/src/main/java/net/openhft/chronicle/hash/TimeProvider.java b/src/main/java/net/openhft/chronicle/hash/replication/TimeProvider.java similarity index 95% rename from src/main/java/net/openhft/chronicle/hash/TimeProvider.java rename to src/main/java/net/openhft/chronicle/hash/replication/TimeProvider.java index bdf1f565c..8aad67c91 100644 --- a/src/main/java/net/openhft/chronicle/hash/TimeProvider.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/TimeProvider.java @@ -16,8 +16,9 @@ * limitations under the License. */ -package net.openhft.chronicle.hash; +package net.openhft.chronicle.hash.replication; +import net.openhft.chronicle.hash.ChronicleHashBuilder; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.set.ChronicleSet; diff --git a/src/main/java/net/openhft/chronicle/hash/UdpReplicationConfig.java b/src/main/java/net/openhft/chronicle/hash/replication/UdpConfig.java similarity index 78% rename from src/main/java/net/openhft/chronicle/hash/UdpReplicationConfig.java rename to src/main/java/net/openhft/chronicle/hash/replication/UdpConfig.java index 3e27e7b18..a46237c49 100644 --- a/src/main/java/net/openhft/chronicle/hash/UdpReplicationConfig.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/UdpConfig.java @@ -16,7 +16,9 @@ * limitations under the License. */ -package net.openhft.chronicle.hash; +package net.openhft.chronicle.hash.replication; + +import net.openhft.chronicle.hash.ReplicationConfig; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -24,13 +26,13 @@ import java.net.InetAddress; import java.net.NetworkInterface; -public final class UdpReplicationConfig implements ReplicationConfig { +public final class UdpConfig implements ReplicationConfig { private final InetAddress address; private final int port; private final NetworkInterface networkInterface; private final ThrottlingConfig throttlingConfig; - private UdpReplicationConfig( + private UdpConfig( InetAddress address, int port, NetworkInterface networkInterface, @@ -48,23 +50,22 @@ public final class UdpReplicationConfig implements ReplicationConfig { this.throttlingConfig = throttlingConfig; } - public static UdpReplicationConfig simple(@NotNull InetAddress address, int port) { + public static UdpConfig simple(@NotNull InetAddress address, int port) { if (address.isMulticastAddress()) throw new IllegalArgumentException(); return create(address, port, null, ThrottlingConfig.noThrottling()); } - public static UdpReplicationConfig multiCast(@NotNull InetAddress address, int port, - @NotNull NetworkInterface networkInterface) { + public static UdpConfig multiCast(@NotNull InetAddress address, int port, + @NotNull NetworkInterface networkInterface) { if (!address.isMulticastAddress() || networkInterface == null) throw new IllegalArgumentException(); return create(address, port, networkInterface, ThrottlingConfig.noThrottling()); } - static UdpReplicationConfig create(InetAddress address, int port, - NetworkInterface networkInterface, - ThrottlingConfig throttlingConfig) { - return new UdpReplicationConfig(address, port, networkInterface, throttlingConfig); + static UdpConfig create(InetAddress address, int port, NetworkInterface networkInterface, + ThrottlingConfig throttlingConfig) { + return new UdpConfig(address, port, networkInterface, throttlingConfig); } @NotNull @@ -101,8 +102,8 @@ public final class UdpReplicationConfig implements ReplicationConfig { if (o == this) { return true; } - if (o instanceof UdpReplicationConfig) { - UdpReplicationConfig that = (UdpReplicationConfig) o; + if (o instanceof UdpConfig) { + UdpConfig that = (UdpConfig) o; return (this.address.equals(that.address())) && (this.port == that.port()) && (this.networkInterface.equals(that.networkInterface())) @@ -125,7 +126,7 @@ public final class UdpReplicationConfig implements ReplicationConfig { return h; } - public UdpReplicationConfig throttlingConfig(@NotNull ThrottlingConfig throttlingConfig) { + public UdpConfig throttlingConfig(@NotNull ThrottlingConfig throttlingConfig) { ThrottlingConfig.checkMillisecondBucketInterval(throttlingConfig, "UDP"); return create(address(), port(), networkInterface(), throttlingConfig); } diff --git a/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java b/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java index e63616205..3c88d5c7e 100644 --- a/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.ThrottlingConfig; +import net.openhft.chronicle.hash.replication.ThrottlingConfig; import net.openhft.lang.io.ByteBufferBytes; import net.openhft.lang.io.Bytes; import net.openhft.lang.thread.NamedThreadFactory; diff --git a/src/main/java/net/openhft/chronicle/map/AbstractChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/AbstractChronicleMapBuilder.java index ca7d43f3d..9087dbf9e 100644 --- a/src/main/java/net/openhft/chronicle/map/AbstractChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/AbstractChronicleMapBuilder.java @@ -19,6 +19,7 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.*; +import net.openhft.chronicle.hash.replication.*; import net.openhft.chronicle.hash.serialization.MetaBytesInterop; import net.openhft.chronicle.hash.serialization.MetaBytesWriter; import net.openhft.chronicle.hash.serialization.MetaProvider; @@ -40,8 +41,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; +import java.net.InetSocketAddress; import java.nio.ByteBuffer; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -103,8 +107,6 @@ public abstract class AbstractChronicleMapBuilder keyBuilder; SerializationBuilder valueBuilder; - private Map, Replicator> replicators = new HashMap, Replicator>(); - // used when configuring the number of segments. private int minSegments = -1; private int actualSegments = -1; @@ -133,10 +135,8 @@ public abstract class AbstractChronicleMapBuilder defaultValueProvider = NullValueProvider.INSTANCE; private PrepareValueBytes prepareValueBytes = null; - private byte identifier = -1; - private StatelessBuilder statelessBuilder; - private File file; + private SimpleReplication simpleReplication = null; AbstractChronicleMapBuilder(Class keyClass, Class valueClass) { keyBuilder = new SerializationBuilder(keyClass, SerializationBuilder.Role.KEY); @@ -283,14 +283,14 @@ public abstract class AbstractChronicleMapBuilder 0) return entryAndValueAlignment().alignSize(entrySize); int size = metaDataBytes; int keySize = keySize(); size += keyBuilder.sizeMarshaller().sizeEncodingSize(keySize); size += keySize; - if (useReplication()) + if (replicated) size += ReplicatedChronicleMap.ADDITIONAL_ENTRY_BYTES; int valueSize = valueSize(); size += valueBuilder.sizeMarshaller().sizeEncodingSize(valueSize); @@ -559,43 +559,6 @@ public abstract class AbstractChronicleMapBuilder> statelessClient( + InetSocketAddress remoteAddress) { + return new StatelessMapBuilder<>(this.clone(), remoteAddress); } /** @@ -748,7 +705,7 @@ public abstract class AbstractChronicleMapBuilder defaultValueProvider() { if (defaultValueProvider != null) @@ -766,7 +723,43 @@ public abstract class AbstractChronicleMapBuilder(defaultValue, metaValueWriter, valueWriter); } - public ChronicleMap createWithFile(File file) throws IOException { + @Override + public B replication(SimpleReplication replication) { + this.simpleReplication = replication; + return self(); + } + + @Override + public B replication(byte identifier, TcpConfig tcpTransportAndNetwork) { + return replication(SimpleReplication.builder() + .tcpTransportAndNetwork(tcpTransportAndNetwork).create(identifier)); + } + + @Override + public ChronicleHashInstanceBuilder> instance() { + return new InstanceBuilder<>(this.clone(), simpleReplication, null, null); + } + + @Override + public ChronicleMap createPersistedTo(File file) throws IOException { + return createWithFile(file, simpleReplication, null); + } + + @Override + public ChronicleMap create() throws IOException { + return createWithoutFile(simpleReplication, null); + } + + ChronicleMap create(InstanceBuilder ib) throws IOException { + if (ib.file != null) { + return createWithFile(ib.file, ib.simpleReplication, ib.channel); + } else { + return createWithoutFile(ib.simpleReplication, ib.channel); + } + } + + ChronicleMap createWithFile(File file, SimpleReplication simpleReplication, + ReplicationChannel channel) throws IOException { for (int i = 0; i < 10; i++) { if (file.exists() && file.length() > 0) { FileInputStream fis = new FileInputStream(file); @@ -776,7 +769,7 @@ public abstract class AbstractChronicleMapBuilder) ois.readObject(); map.headerSize = roundUpMapHeaderSize(fis.getChannel().position()); map.createMappedStoreAndSegments(file); - return establishReplication(map); + return establishReplication(map, simpleReplication, channel); } catch (ClassNotFoundException e) { throw new IOException(e); } finally { @@ -796,7 +789,7 @@ public abstract class AbstractChronicleMapBuilder map = newMap(); + VanillaChronicleMap map = newMap(simpleReplication, channel); FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); @@ -809,54 +802,66 @@ public abstract class AbstractChronicleMapBuilder create() throws IOException { - - if (statelessBuilder != null) - return createStatelessMap(statelessBuilder); - - return (file != null) ? createWithFile(file) : createWithoutFile(); - } - - private ChronicleMap createWithoutFile() throws IOException { - VanillaChronicleMap map = newMap(); + /** + * Only for testing. + * + * @param identifier id + * @return map + */ + ChronicleMap createReplicated(byte identifier) throws IOException { + VanillaChronicleMap map = + new ReplicatedChronicleMap, + V, Object, MetaBytesWriter>(this, identifier); BytesStore bytesStore = new DirectStore(JDKObjectSerializer.INSTANCE, map.sizeInBytes(), true); map.createMappedStoreAndSegments(bytesStore); - return establishReplication(map); + return map; } - private ChronicleMap createStatelessMap(StatelessBuilder statelessBuilder) throws IOException { - preMapConstruction(); + private ChronicleMap createWithoutFile( + SimpleReplication simpleReplication, ReplicationChannel channel) throws IOException { + VanillaChronicleMap map = newMap(simpleReplication, channel); + BytesStore bytesStore = new DirectStore(JDKObjectSerializer.INSTANCE, + map.sizeInBytes(), true); + map.createMappedStoreAndSegments(bytesStore); + return establishReplication(map, simpleReplication, channel); + } + + ChronicleMap createStatelessMap(StatelessMapBuilder statelessBuilder) + throws IOException { + preMapConstruction(false); return new StatelessChronicleMap( statelessBuilder, this); } - private VanillaChronicleMap newMap() throws IOException { - preMapConstruction(); - if (useReplication()) { + private VanillaChronicleMap newMap( + SimpleReplication simpleReplication, ReplicationChannel channel) throws IOException { + boolean replicated = simpleReplication != null || channel != null; + preMapConstruction(replicated); + if (replicated) { + byte identifier; + if (simpleReplication != null) { + identifier = simpleReplication.identifier(); + } else { + identifier = channel.hub().identifier(); + } return new ReplicatedChronicleMap, - V, Object, MetaBytesWriter>(this); + V, Object, MetaBytesWriter>(this, identifier); } else { return new VanillaChronicleMap, V, Object, MetaBytesWriter>(this); } } - private boolean useReplication() { - return identifier != -1; - } - - void preMapConstruction() { + void preMapConstruction(boolean replicated) { keyBuilder.objectSerializer(objectSerializer()); valueBuilder.objectSerializer(objectSerializer()); - long maxSize = (long) entrySize() * figureBufferAllocationFactor(); + long maxSize = (long) entrySize(replicated) * figureBufferAllocationFactor(); keyBuilder.maxSize(maxSize); valueBuilder.maxSize(maxSize); @@ -866,32 +871,37 @@ public abstract class AbstractChronicleMapBuilder establishReplication(VanillaChronicleMap map) - throws IOException { + private ChronicleMap establishReplication( + VanillaChronicleMap map, SimpleReplication simpleReplication, + ReplicationChannel channel) throws IOException { if (map instanceof ReplicatedChronicleMap) { + if (simpleReplication != null && channel != null) { + throw new AssertionError("Only one non-null replication should be passed"); + } ReplicatedChronicleMap result = (ReplicatedChronicleMap) map; - for (Replicator replicator : replicators.values()) { - if (replicator instanceof ChannelProvider.ChronicleChannel) { - ChannelProvider.ChronicleChannel channel = - (ChannelProvider.ChronicleChannel) replicator; - if (entrySize() > channel.provider().maxEntrySize()) { - throw new IllegalArgumentException("During ChannelProviderBuilder setup, " + - "maxEntrySize=" + channel.provider().maxEntrySize() + - " was specified, but map with " + - "entrySize=" + entrySize() + " is attempted to apply" + - "to the replicator"); - } + List replicators = new ArrayList<>(2); + if (simpleReplication != null) { + if (simpleReplication.tcpTransportAndNetwork() != null) + replicators.add(Replicators.tcp(simpleReplication.tcpTransportAndNetwork())); + if (simpleReplication.udpTransport() != null) + replicators.add(Replicators.udp(simpleReplication.udpTransport())); + } else { + ReplicationHub hub = channel.hub(); + int entrySize = entrySize(true); + if (entrySize > hub.maxEntrySize()) { + throw new IllegalArgumentException("During ChannelProviderBuilder setup, " + + "maxEntrySize=" + hub.maxEntrySize() + + " was specified, but map with " + + "entrySize=" + entrySize + " is attempted to apply to the replicator"); } + ChannelProvider provider = ChannelProvider.getProvider(hub); + ChannelProvider.ChronicleChannel ch = provider.createChannel(channel.channelId()); + replicators.add(ch); + } + for (Replicator replicator : replicators) { Closeable token = replicator.applyTo(this, result, result, map); if (replicators.size() == 1 && token.getClass() == UdpReplicator.class) { - LOG.warn( - "MISSING TCP REPLICATION : The UdpReplicator only attempts to read data " + - "(it does not enforce or guarantee delivery), you should use" + - "the UdpReplicator if you have a large number of nodes, and you wish" + - "to receive the data before it becomes available on TCP/IP. Since data" + - "delivery is not guaranteed, it is recommended that you only use" + - "the UDP Replicator in conjunction with a TCP Replicator" - ); + LOG.warn(Replicators.ONLY_UDP_WARN_MESSAGE); } result.addCloseable(token); } @@ -906,11 +916,7 @@ public abstract class AbstractChronicleMapBuilder implMapping = new IdentityHashMap<>(); + static synchronized ChannelProvider getProvider(ReplicationHub hub) throws IOException { + ChannelProvider channelProvider = implMapping.get(hub); + if (channelProvider != null) + return channelProvider; + channelProvider = new ChannelProvider(hub); + TcpConfig tcpConfig = hub.tcpTransportAndNetwork(); + if (tcpConfig != null) { + final TcpReplicator tcpReplicator = new TcpReplicator( + channelProvider.asReplica, + channelProvider.asEntryExternalizable, + tcpConfig, hub.maxEntrySize(), null); + channelProvider.add(tcpReplicator); + } + + UdpConfig udpConfig = hub.udpTransport(); + if (udpConfig != null) { + final UdpReplicator udpReplicator = + new UdpReplicator( + channelProvider.asReplica, + channelProvider.asEntryExternalizable, + udpConfig, hub.maxEntrySize()); + channelProvider.add(udpReplicator); + if (tcpConfig == null) + LOG.warn(Replicators.ONLY_UDP_WARN_MESSAGE); + } + implMapping.put(hub, channelProvider); + return channelProvider; + } + private static final byte BOOTSTRAP_MESSAGE = 'B'; final EntryExternalizable asEntryExternalizable = new EntryExternalizable() { /** @@ -65,18 +96,28 @@ public final class ChannelProvider implements Closeable { @Override public void writeExternalEntry(@NotNull Bytes entry, @NotNull Bytes destination, int chronicleChannel) { - destination.writeStopBit(chronicleChannel); - channelEntryExternalizables[chronicleChannel] - .writeExternalEntry(entry, destination, chronicleChannel); + channelDataLock.readLock().lock(); + try { + destination.writeStopBit(chronicleChannel); + channelEntryExternalizables[chronicleChannel] + .writeExternalEntry(entry, destination, chronicleChannel); + } finally { + channelDataLock.readLock().unlock(); + } } @Override public void readExternalEntry(@NotNull Bytes source) { - final int chronicleId = (int) source.readStopBit(); - if (chronicleId < chronicleChannels.length) - channelEntryExternalizables[chronicleId].readExternalEntry(source); - else - LOG.info("skipped entry with chronicleId=" + chronicleId + ", "); + channelDataLock.readLock().lock(); + try { + final int chronicleId = (int) source.readStopBit(); + if (chronicleId < chronicleChannels.length) + channelEntryExternalizables[chronicleId].readExternalEntry(source); + else + LOG.info("skipped entry with chronicleId=" + chronicleId + ", "); + } finally { + channelDataLock.readLock().unlock(); + } } }; private final byte localIdentifier; @@ -90,53 +131,73 @@ public final class ChannelProvider implements Closeable { @Override public ModificationIterator acquireModificationIterator( final short remoteIdentifier, final ModificationNotifier notifier) { - final ModificationIterator result = modificationIterator.get(remoteIdentifier); - if (result != null) - return result; + channelDataLock.writeLock().lock(); + try { + final ModificationIterator result = modificationIterator.get(remoteIdentifier); + if (result != null) + return result; - final ModificationIterator result0 = new ModificationIterator() { + final ModificationIterator result0 = new ModificationIterator() { - @Override - public boolean hasNext() { - // old-style iteration to avoid 1) iterator object creation - // 2) ConcurrentModificationException - for (int i = 0, len = chronicleChannelList.size(); i < len; i++) { - final ModificationIterator modificationIterator = - chronicleChannelList.get(i) + @Override + public boolean hasNext() { + channelDataLock.readLock().lock(); + try { + // old-style iteration to avoid 1) iterator object creation + // 2) ConcurrentModificationException + for (int i = 0, len = chronicleChannelList.size(); i < len; i++) { + final ModificationIterator modificationIterator = + chronicleChannelList.get(i).acquireModificationIterator( + remoteIdentifier, notifier); + if (modificationIterator.hasNext()) + return true; + } + return false; + } finally { + channelDataLock.readLock().unlock(); + } + } + + @Override + public boolean nextEntry(@NotNull EntryCallback callback, + final int na) { + channelDataLock.readLock().lock(); + try { + for (int i = 0, len = chronicleChannelList.size(); i < len; i++) { + Replica chronicleChannel = chronicleChannelList.get(i); + final ModificationIterator modificationIterator = chronicleChannel .acquireModificationIterator(remoteIdentifier, notifier); - if (modificationIterator.hasNext()) - return true; + if (modificationIterator + .nextEntry(callback, chronicleChannelIds.get(i))) + return true; + } + return false; + } finally { + channelDataLock.readLock().unlock(); + } } - return false; - } - @Override - public boolean nextEntry(@NotNull EntryCallback callback, - final int na) { - for (int i = 0, len = chronicleChannelList.size(); i < len; i++) { - Replica chronicleChannel = chronicleChannelList.get(i); - final ModificationIterator modificationIterator = chronicleChannel - .acquireModificationIterator(remoteIdentifier, notifier); - if (modificationIterator.nextEntry(callback, chronicleChannelIds.get(i))) - return true; + @Override + public void dirtyEntries(long fromTimeStamp) { + channelDataLock.readLock().lock(); + try { + for (int i = 0, len = chronicleChannelList.size(); i < len; i++) { + chronicleChannelList.get(i) + .acquireModificationIterator(remoteIdentifier, notifier) + .dirtyEntries(fromTimeStamp); + notifier.onChange(); + } + } finally { + channelDataLock.readLock().unlock(); + } } - return false; - } - - @Override - public void dirtyEntries(long fromTimeStamp) { - for (int i = 0, len = chronicleChannelList.size(); i < len; i++) { - chronicleChannelList.get(i) - .acquireModificationIterator(remoteIdentifier, notifier) - .dirtyEntries(fromTimeStamp); - notifier.onChange(); - } - } - }; - - modificationIterator.set((int) remoteIdentifier, result0); - return result0; + }; + modificationIterator.set((int) remoteIdentifier, result0); + return result0; + } finally { + channelDataLock.writeLock().unlock(); + } } /** @@ -144,13 +205,19 @@ public final class ChannelProvider implements Closeable { */ @Override public long lastModificationTime(byte remoteIdentifier) { - long t = 0; - // not including the SystemQueue at index 0 - for (int i = 1, len = chronicleChannelList.size(); i < len; i++) { - t = (t == 0) ? chronicleChannels[i].lastModificationTime(remoteIdentifier) : - min(t, chronicleChannels[i].lastModificationTime(remoteIdentifier)); + channelDataLock.readLock().lock(); + try { + long t = 0; + // not including the SystemQueue at index 0 + for (int i = 1, len = chronicleChannelList.size(); i < len; i++) { + Replica channel = chronicleChannelList.get(i); + t = (t == 0) ? channel.lastModificationTime(remoteIdentifier) : + min(t, channel.lastModificationTime(remoteIdentifier)); + } + return t; + } finally { + channelDataLock.readLock().unlock(); } - return t; } @Override @@ -158,7 +225,13 @@ public final class ChannelProvider implements Closeable { ChannelProvider.this.close(); } }; + + private final ReplicationHub hub; private final int maxEntrySize; + + private final ReadWriteLock channelDataLock = new ReentrantReadWriteLock(); + + // start of channel data private final Replica[] chronicleChannels; private final List chronicleChannelList; private final List chronicleChannelIds; @@ -169,13 +242,18 @@ public final class ChannelProvider implements Closeable { newBitSet(systemModificationIterator.length()); private final AtomicReferenceArray modificationIterator = new AtomicReferenceArray(128); + // end of channel data + private final Set replicators = new CopyOnWriteArraySet(); - ChannelProvider(ChannelProviderBuilder builder) { - localIdentifier = builder.identifier; - maxEntrySize = builder.maxEntrySize; - chronicleChannels = new Replica[builder.maxNumberOfChronicles]; - channelEntryExternalizables = new EntryExternalizable[builder.maxNumberOfChronicles]; + private volatile boolean isClosed = false; + + private ChannelProvider(ReplicationHub hub) { + localIdentifier = hub.identifier(); + this.hub = hub; + maxEntrySize = hub.maxEntrySize(); + chronicleChannels = new Replica[hub.maxNumberOfChannels()]; + channelEntryExternalizables = new EntryExternalizable[hub.maxNumberOfChannels()]; chronicleChannelList = new ArrayList(); chronicleChannelIds = new ArrayList(); MessageHandler systemMessageHandler = new MessageHandler() { @@ -209,15 +287,6 @@ public final class ChannelProvider implements Closeable { return new SingleThreadedDirectBitSet(bytes); } - /** - * Returns a replicator, dedicated to the specified channel. Channel is basically just a number, that - * should correspond on different servers for instances of the same replicated map. Only one replicator - * per channel could be obtained from a single {@code ReplicatingChannel}. The returned replicator could - * be applied to a map at most once. - * - * @param channel number to create - * @return a replicator, dedicated to the specified channel - */ public ChronicleChannel createChannel(short channel) { return new ChronicleChannel(channel); } @@ -249,39 +318,49 @@ public final class ChannelProvider implements Closeable { private void add(short chronicleChannel, Replica replica, EntryExternalizable entryExternalizable) { - if (chronicleChannels[chronicleChannel] != null) { - throw new IllegalStateException("chronicleId=" + chronicleChannel + - " is already in use."); + channelDataLock.writeLock().lock(); + try { + if (chronicleChannels[chronicleChannel] != null) { + throw new IllegalStateException("chronicleId=" + chronicleChannel + + " is already in use."); + } + chronicleChannels[chronicleChannel] = replica; + chronicleChannelList.add(replica); + chronicleChannelIds.add((int) chronicleChannel); + channelEntryExternalizables[chronicleChannel] = entryExternalizable; + + if (chronicleChannel == 0) + return; + + for (int i = (int) systemModificationIteratorBitSet.nextSetBit(0); i > 0; + i = (int) systemModificationIteratorBitSet.nextSetBit(i + 1)) { + byte remoteIdentifier = (byte) i; + final long lastModificationTime = replica.lastModificationTime(remoteIdentifier); + final ByteBufferBytes message = + toBootstrapMessage(chronicleChannel, lastModificationTime); + systemModificationIterator.get(remoteIdentifier).addPayload(message); + } + } finally { + channelDataLock.writeLock().unlock(); } - chronicleChannels[chronicleChannel] = replica; - chronicleChannelList.add(replica); - chronicleChannelIds.add((int) chronicleChannel); - channelEntryExternalizables[chronicleChannel] = entryExternalizable; - - if (chronicleChannel == 0) - return; - - for (int i = (int) systemModificationIteratorBitSet.nextSetBit(0); i > 0; - i = (int) systemModificationIteratorBitSet.nextSetBit(i + 1)) { - byte remoteIdentifier = (byte) i; - final long lastModificationTime = replica.lastModificationTime(remoteIdentifier); - final ByteBufferBytes message = - toBootstrapMessage(chronicleChannel, lastModificationTime); - systemModificationIterator.get(remoteIdentifier).addPayload(message); - } - } @Override public void close() throws IOException { - for (Closeable replicator : replicators) { - replicator.close(); - } - // indexed reverse loop to avoid ConcurrentModificationException and remove elements from - // the end of the loop for correct iteration - for (int i = chronicleChannelList.size() - 1; i >= 0; i--) { - Replica chronicleChannel = chronicleChannelList.get(i); - chronicleChannel.close(); + if (isClosed) + return; + // to be synchronized with #getProvider() + synchronized (ChannelProvider.class) { + isClosed = true; + for (Closeable replicator : replicators) { + try { + replicator.close(); + } catch (IOException e) { + LOG.error("", e); + } + } + + implMapping.remove(hub); } } @@ -420,16 +499,22 @@ public final class ChannelProvider implements Closeable { return this; } - public ChannelProvider provider() { - return ChannelProvider.this; - } - @Override public void close() throws IOException { - chronicleChannelList.remove(chronicleChannels[chronicleChannel]); - chronicleChannelIds.remove(chronicleChannel); - chronicleChannels[chronicleChannel] = null; - channelEntryExternalizables[chronicleChannel] = null; + // synchronized on parent instance to prevent possible (but very unlikely) contention + // between close() calls on different channels + channelDataLock.writeLock().lock(); + try { + chronicleChannelList.remove(chronicleChannels[chronicleChannel]); + chronicleChannelIds.remove((Integer) (int) chronicleChannel); + chronicleChannels[chronicleChannel] = null; + channelEntryExternalizables[chronicleChannel] = null; + + if (chronicleChannelList.size() == 1) // i. e. only SystemQueue is left + ChannelProvider.this.close(); + } finally { + channelDataLock.writeLock().unlock(); + } } } diff --git a/src/main/java/net/openhft/chronicle/map/InstanceBuilder.java b/src/main/java/net/openhft/chronicle/map/InstanceBuilder.java new file mode 100644 index 000000000..02c2c1b23 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/map/InstanceBuilder.java @@ -0,0 +1,73 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.map; + +import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; +import net.openhft.chronicle.hash.replication.ReplicationChannel; +import net.openhft.chronicle.hash.replication.SimpleReplication; +import net.openhft.chronicle.hash.replication.TcpConfig; + +import java.io.File; +import java.io.IOException; + +final class InstanceBuilder + implements ChronicleHashInstanceBuilder> { + + final AbstractChronicleMapBuilder mapBuilder; + final SimpleReplication simpleReplication; + final ReplicationChannel channel; + final File file; + + InstanceBuilder(AbstractChronicleMapBuilder mapBuilder, + SimpleReplication simpleReplication, + ReplicationChannel channel, + File file) { + this.mapBuilder = mapBuilder; + this.simpleReplication = simpleReplication; + this.channel = channel; + this.file = file; + } + + @Override + public ChronicleHashInstanceBuilder> replicated( + byte identifier, TcpConfig tcpTransportAndNetwork) { + return replicated(SimpleReplication.builder() + .tcpTransportAndNetwork(tcpTransportAndNetwork).create(identifier)); + } + + @Override + public ChronicleHashInstanceBuilder> replicated( + SimpleReplication replication) { + return new InstanceBuilder<>(mapBuilder, replication, null, file); + } + + @Override + public ChronicleHashInstanceBuilder> replicatedViaChannel( + ReplicationChannel channel) { + return new InstanceBuilder<>(mapBuilder, null, channel, file); + } + + @Override + public ChronicleHashInstanceBuilder> persistedTo(File file) { + return new InstanceBuilder<>(mapBuilder, simpleReplication, channel, file); + } + + @Override + public ChronicleMap create() throws IOException { + return mapBuilder.create(this); + } +} diff --git a/src/main/java/net/openhft/chronicle/map/NodeDiscovery.java b/src/main/java/net/openhft/chronicle/map/NodeDiscovery.java index 16bb25b95..2a73a8c5c 100644 --- a/src/main/java/net/openhft/chronicle/map/NodeDiscovery.java +++ b/src/main/java/net/openhft/chronicle/map/NodeDiscovery.java @@ -18,8 +18,9 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; -import net.openhft.chronicle.hash.UdpReplicationConfig; +import net.openhft.chronicle.hash.replication.IdentifierListener; +import net.openhft.chronicle.hash.replication.TcpConfig; +import net.openhft.chronicle.hash.replication.UdpConfig; import net.openhft.lang.collection.ATSDirectBitSet; import net.openhft.lang.collection.DirectBitSet; import net.openhft.lang.io.ByteBufferBytes; @@ -41,7 +42,7 @@ import java.util.concurrent.atomic.AtomicReference; import static java.nio.ByteBuffer.allocateDirect; import static java.util.concurrent.TimeUnit.SECONDS; -import static net.openhft.chronicle.hash.UdpReplicationConfig.simple; +import static net.openhft.chronicle.hash.replication.UdpConfig.simple; import static net.openhft.chronicle.map.ConcurrentExpiryMap.getDefaultAddress; import static net.openhft.chronicle.map.DiscoveryNodeBytesMarshallable.ProposedNodes; import static net.openhft.chronicle.map.NodeDiscoveryBroadcaster.BOOTSTRAP_BYTES; @@ -55,7 +56,7 @@ import static net.openhft.chronicle.map.NodeDiscoveryBroadcaster.LOG; public class NodeDiscovery { private final AddressAndPort ourAddressAndPort; - private final UdpReplicationConfig udpConfig; + private final UdpConfig udpConfig; private final DiscoveryNodeBytesMarshallable discoveryNodeBytesMarshallable; private final AtomicReference nodeDiscoveryEventListenerAtomicReference = new AtomicReference(); @@ -220,7 +221,7 @@ public class NodeDiscovery { // add our identifier and host:port to the list of known identifiers knownNodes.add(ourAddressAndPort, identifier); - final TcpReplicationConfig tcpConfig = TcpReplicationConfig + final TcpConfig tcpConfig = TcpConfig .of(ourAddressAndPort.getPort(), toInetSocketArray(knownHostPorts)) .heartBeatInterval(1, SECONDS).nonUniqueIdentifierListener(identifierListener); @@ -231,7 +232,7 @@ public class NodeDiscovery { return ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000L) - .replicators(identifier, tcpConfig).create(); + .replication(identifier, tcpConfig).create(); } @@ -359,7 +360,7 @@ class NodeDiscoveryBroadcaster extends UdpChannelReplicator { * @throws java.io.IOException */ NodeDiscoveryBroadcaster( - final UdpReplicationConfig replicationConfig, + final UdpConfig replicationConfig, final int serializedEntrySize, final BytesMarshallable externalizable) throws IOException { diff --git a/src/main/java/net/openhft/chronicle/map/Replica.java b/src/main/java/net/openhft/chronicle/map/Replica.java index b4664a1c5..1041f7322 100644 --- a/src/main/java/net/openhft/chronicle/map/Replica.java +++ b/src/main/java/net/openhft/chronicle/map/Replica.java @@ -26,7 +26,7 @@ import java.io.Closeable; /** * @author Rob Austin. */ -public interface Replica extends Closeable { +interface Replica extends Closeable { /** * Provides the unique Identifier associated with this map instance.

An identifier is used to diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 9f4a2e0bc..6ca4a1bcc 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -19,7 +19,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TimeProvider; +import net.openhft.chronicle.hash.replication.TimeProvider; import net.openhft.chronicle.hash.serialization.BytesReader; import net.openhft.chronicle.hash.serialization.MetaBytesInterop; import net.openhft.chronicle.hash.serialization.MetaBytesWriter; @@ -79,7 +79,7 @@ import static net.openhft.lang.collection.DirectBitSet.NOT_FOUND; * @param the entries key type * @param the entries value type */ -class ReplicatedChronicleMap, +final class ReplicatedChronicleMap, V, VW, MVW extends MetaBytesWriter> extends VanillaChronicleMap implements ChronicleMap, @@ -99,12 +99,13 @@ class ReplicatedChronicleMap, private transient ModificationDelegator modificationDelegator; private transient long startOfModificationIterators; - public ReplicatedChronicleMap(@NotNull AbstractChronicleMapBuilder builder) + public ReplicatedChronicleMap(@NotNull AbstractChronicleMapBuilder builder, + byte identifier) throws IOException { super(builder); this.timeProvider = builder.timeProvider(); - this.localIdentifier = builder.identifier(); + this.localIdentifier = identifier; if (localIdentifier == -1) { throw new IllegalStateException("localIdentifier should not be -1"); @@ -642,8 +643,6 @@ class ReplicatedChronicleMap, V acquireWithoutLock(ThreadLocalCopies copies, MKI metaKeyInterop, KI keyInterop, K key, V usingValue, long hash2, boolean create, long timestamp) { - - long keySize = metaKeyInterop.size(keyInterop, key); MultiStoreBytes entry = acquireTmpBytes(); long offset = searchKey(keyInterop, metaKeyInterop, key, keySize, hash2, entry, diff --git a/src/main/java/net/openhft/chronicle/map/Replicator.java b/src/main/java/net/openhft/chronicle/map/Replicator.java index 7d84ae262..7580d519d 100644 --- a/src/main/java/net/openhft/chronicle/map/Replicator.java +++ b/src/main/java/net/openhft/chronicle/map/Replicator.java @@ -19,7 +19,7 @@ package net.openhft.chronicle.map; import java.io.Closeable; import java.io.IOException; -public abstract class Replicator { +abstract class Replicator { /** * A constructor for use in subclasses. diff --git a/src/main/java/net/openhft/chronicle/map/Replicators.java b/src/main/java/net/openhft/chronicle/map/Replicators.java index 6b97d0ee6..b62683c44 100644 --- a/src/main/java/net/openhft/chronicle/map/Replicators.java +++ b/src/main/java/net/openhft/chronicle/map/Replicators.java @@ -16,8 +16,8 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; -import net.openhft.chronicle.hash.UdpReplicationConfig; +import net.openhft.chronicle.hash.replication.TcpConfig; +import net.openhft.chronicle.hash.replication.UdpConfig; import org.jetbrains.annotations.NotNull; import java.io.Closeable; @@ -25,10 +25,18 @@ import java.io.IOException; final class Replicators { + static final String ONLY_UDP_WARN_MESSAGE = + "MISSING TCP REPLICATION : The UdpReplicator only attempts to read data " + + "(it does not enforce or guarantee delivery), you should use" + + "the UdpReplicator if you have a large number of nodes, and you wish" + + "to receive the data before it becomes available on TCP/IP. Since data" + + "delivery is not guaranteed, it is recommended that you only use" + + "the UDP Replicator in conjunction with a TCP Replicator"; + private Replicators() { } - static Replicator tcp(final TcpReplicationConfig replicationConfig) { + static Replicator tcp(final TcpConfig replicationConfig) { return new Replicator() { @Override @@ -42,16 +50,17 @@ final class Replicators { .keyBuilder, builder.valueBuilder); StatelessServerConnector statelessServer = new StatelessServerConnector - (keyValueSerializer, (VanillaChronicleMap) chronicleMap, builder.entrySize()); + (keyValueSerializer, (VanillaChronicleMap) chronicleMap, + builder.entrySize(true)); return new TcpReplicator(replica, entryExternalizable, replicationConfig, - builder.entrySize(), statelessServer); + builder.entrySize(true), statelessServer); } }; } static Replicator udp( - final UdpReplicationConfig replicationConfig) { + final UdpConfig replicationConfig) { return new Replicator() { @Override @@ -61,7 +70,7 @@ final class Replicators { final ChronicleMap chronicleMap) throws IOException { return new UdpReplicator(map, entryExternalizable, replicationConfig, - builder.entrySize()); + builder.entrySize(true)); } }; } diff --git a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java index 0eecd6e15..9866be621 100644 --- a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java @@ -19,7 +19,6 @@ package net.openhft.chronicle.map; import com.sun.jdi.connect.spi.ClosedConnectionException; -import net.openhft.chronicle.hash.StatelessBuilder; import net.openhft.chronicle.hash.exceptions.IORuntimeException; import net.openhft.chronicle.hash.exceptions.TimeoutRuntimeException; import net.openhft.lang.io.ByteBufferBytes; @@ -60,8 +59,8 @@ class StatelessChronicleMap implements ChronicleMap, Closeable { private final KeyValueSerializer keyValueSerializer; private volatile SocketChannel clientChannel; - private volatile CloseablesManager closeables; - private final StatelessBuilder builder; + private CloseablesManager closeables; + private final StatelessMapBuilder builder; private int maxEntrySize; private final Class kClass; private final Class vClass; @@ -97,15 +96,14 @@ class StatelessChronicleMap implements ChronicleMap, Closeable { private long transactionID; - StatelessChronicleMap(final StatelessBuilder builder, + StatelessChronicleMap(final StatelessMapBuilder builder, final AbstractChronicleMapBuilder chronicleMapBuilder) throws IOException { - this.builder = builder; this.keyValueSerializer = new KeyValueSerializer(chronicleMapBuilder.keyBuilder, chronicleMapBuilder.valueBuilder); this.putReturnsNull = chronicleMapBuilder.putReturnsNull(); this.removeReturnsNull = chronicleMapBuilder.removeReturnsNull(); - this.maxEntrySize = chronicleMapBuilder.entrySize(); + this.maxEntrySize = chronicleMapBuilder.entrySize(true); this.vClass = chronicleMapBuilder.valueBuilder.eClass; this.kClass = chronicleMapBuilder.keyBuilder.eClass; // this.putReturnsNull = putReturnsNull; diff --git a/src/main/java/net/openhft/chronicle/hash/StatelessBuilder.java b/src/main/java/net/openhft/chronicle/map/StatelessMapBuilder.java similarity index 59% rename from src/main/java/net/openhft/chronicle/hash/StatelessBuilder.java rename to src/main/java/net/openhft/chronicle/map/StatelessMapBuilder.java index 5434eb768..7d3b28ce8 100644 --- a/src/main/java/net/openhft/chronicle/hash/StatelessBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessMapBuilder.java @@ -16,37 +16,45 @@ * limitations under the License. */ -package net.openhft.chronicle.hash; +package net.openhft.chronicle.map; +import net.openhft.chronicle.hash.StatelessClientBuilder; + +import java.io.IOException; import java.net.InetSocketAddress; import java.util.concurrent.TimeUnit; /** * @author Rob Austin. */ -public class StatelessBuilder { +class StatelessMapBuilder implements StatelessClientBuilder> { + private final AbstractChronicleMapBuilder mapBuilder; + private final InetSocketAddress remoteAddress; private long timeoutMs = TimeUnit.SECONDS.toMillis(10); - private InetSocketAddress remoteAddress; + StatelessMapBuilder(AbstractChronicleMapBuilder mapBuilder, + InetSocketAddress remoteAddress) { + this.mapBuilder = mapBuilder; + this.remoteAddress = remoteAddress; + } public long timeoutMs() { return timeoutMs; } - public void timeout(long timeout, TimeUnit units) { - this.timeoutMs = units.toMillis(timeout); - } - - private StatelessBuilder(InetSocketAddress remoteAddress) { - this.remoteAddress = remoteAddress; - } - - public static StatelessBuilder remoteAddress(InetSocketAddress inetSocketAddress) { - return new StatelessBuilder(inetSocketAddress); - } - public InetSocketAddress remoteAddress() { return remoteAddress; } + + @Override + public StatelessMapBuilder timeout(long timeout, TimeUnit units) { + this.timeoutMs = units.toMillis(timeout); + return this; + } + + @Override + public ChronicleMap create() throws IOException { + return mapBuilder.createStatelessMap(this); + } } diff --git a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java index 1613ba88b..bab8a8b8d 100644 --- a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java @@ -18,8 +18,9 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; -import net.openhft.chronicle.hash.ThrottlingConfig; +import net.openhft.chronicle.hash.replication.IdentifierListener; +import net.openhft.chronicle.hash.replication.TcpConfig; +import net.openhft.chronicle.hash.replication.ThrottlingConfig; import net.openhft.lang.io.ByteBufferBytes; import net.openhft.lang.io.Bytes; import net.openhft.lang.threadlocal.ThreadLocalCopies; @@ -56,7 +57,7 @@ interface Work { * * @author Rob Austin. */ -public class TcpReplicator extends AbstractChannelReplicator implements Closeable { +final class TcpReplicator extends AbstractChannelReplicator implements Closeable { public static final int STATELESS_CLIENT = -127; public static final byte NOT_SET = (byte) HEARTBEAT.ordinal(); @@ -73,7 +74,7 @@ public class TcpReplicator extends AbstractChannelReplicator implements Closeabl private final byte localIdentifier; private final int maxEntrySizeBytes; private final Replica.EntryExternalizable externalizable; - private final TcpReplicationConfig replicationConfig; + private final TcpConfig replicationConfig; private final StatelessServerConnector statelessServerConnector; private long selectorTimeout; @@ -88,7 +89,7 @@ public class TcpReplicator extends AbstractChannelReplicator implements Closeabl */ public TcpReplicator(@NotNull final Replica replica, @NotNull final Replica.EntryExternalizable externalizable, - @NotNull final TcpReplicationConfig replicationConfig, + @NotNull final TcpConfig replicationConfig, final int maxEntrySizeBytes, @Nullable StatelessServerConnector statelessServerConnector) throws IOException { @@ -477,7 +478,8 @@ public class TcpReplicator extends AbstractChannelReplicator implements Closeabl // this can occur sometimes when if 2 or more remote node attempt to use node discovery at the // same time - final IdentifierListener identifierListener = replicationConfig.identifierListener; + final IdentifierListener identifierListener = + replicationConfig.nonUniqueIdentifierListener(); final SocketAddress remoteAddress = socketChannel.getRemoteAddress(); if ((identifierListener != null && !identifierListener.isIdentifierUnique(remoteIdentifier, diff --git a/src/main/java/net/openhft/chronicle/map/UdpChannelReplicator.java b/src/main/java/net/openhft/chronicle/map/UdpChannelReplicator.java index 337e48336..2896832d2 100644 --- a/src/main/java/net/openhft/chronicle/map/UdpChannelReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/UdpChannelReplicator.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.UdpReplicationConfig; +import net.openhft.chronicle.hash.replication.UdpConfig; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -69,7 +69,7 @@ class UdpChannelReplicator extends AbstractChannelReplicator implements Replica. * @param localIdentifier just used for logging * @throws IOException */ - UdpChannelReplicator(@NotNull final UdpReplicationConfig replicationConfig, + UdpChannelReplicator(@NotNull final UdpConfig replicationConfig, final int maxPayloadSize, final byte localIdentifier) throws IOException { diff --git a/src/main/java/net/openhft/chronicle/map/UdpReplicator.java b/src/main/java/net/openhft/chronicle/map/UdpReplicator.java index ed43075dd..504804982 100644 --- a/src/main/java/net/openhft/chronicle/map/UdpReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/UdpReplicator.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.UdpReplicationConfig; +import net.openhft.chronicle.hash.replication.UdpConfig; import net.openhft.lang.io.ByteBufferBytes; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -36,14 +36,14 @@ import java.nio.channels.DatagramChannel; * it becomes available on TCP/IP. In order to not miss data, UdpReplicator should be used in conjunction with * the TCP Replicator. */ -public class UdpReplicator extends UdpChannelReplicator implements Replica.ModificationNotifier, Closeable { +final class UdpReplicator extends UdpChannelReplicator implements Replica.ModificationNotifier, Closeable { private static final Logger LOG = LoggerFactory.getLogger(UdpReplicator.class.getName()); public UdpReplicator(@NotNull final Replica replica, @NotNull final Replica.EntryExternalizable entryExternalizable, - @NotNull final UdpReplicationConfig replicationConfig, + @NotNull final UdpConfig replicationConfig, final int serializedEntrySize) throws IOException { diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index cdc305845..d9ffc64e1 100644 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -129,8 +129,8 @@ class VanillaChronicleMap, lockTimeOutNS = builder.lockTimeOut(TimeUnit.NANOSECONDS); - // this.replicas = builder.replicas(); - this.entrySize = builder.entrySize(); + boolean replicated = getClass() == ReplicatedChronicleMap.class; + this.entrySize = builder.entrySize(replicated); this.alignment = builder.entryAndValueAlignment(); this.errorListener = builder.errorListener(); diff --git a/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java b/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java index e4c6bf44e..4d85e83bf 100644 --- a/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java +++ b/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java @@ -19,7 +19,9 @@ package net.openhft.chronicle.set; import net.openhft.chronicle.hash.*; -import net.openhft.chronicle.map.Alignment; +import net.openhft.chronicle.hash.replication.SimpleReplication; +import net.openhft.chronicle.hash.replication.TcpConfig; +import net.openhft.chronicle.hash.replication.TimeProvider; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.map.ChronicleMapBuilder; import net.openhft.lang.io.serialization.BytesMarshaller; @@ -30,6 +32,7 @@ import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; +import java.net.InetSocketAddress; import java.util.concurrent.TimeUnit; /** @@ -167,25 +170,6 @@ public class ChronicleSetBuilder return this; } - @Override - public ChronicleSetBuilder replicators(byte identifier, ReplicationConfig... replicators) { - chronicleMapBuilder.replicators(identifier, replicators); - return this; - } - - @Override - public ChronicleSetBuilder channel(ChannelProvider.ChronicleChannel chronicleChannel) { - chronicleMapBuilder.channel(chronicleChannel); - return this; - } - - @Override - public ChronicleSetBuilder disableReplication() { - chronicleMapBuilder.disableReplication(); - return this; - } - - @Override public String toString() { return " ChronicleSetBuilder{" + @@ -265,23 +249,39 @@ public class ChronicleSetBuilder } @Override - public ChronicleSetBuilder stateless(StatelessBuilder statelessBuilder) { - chronicleMapBuilder.stateless(statelessBuilder); + public ChronicleSetBuilder replication(SimpleReplication replication) { + chronicleMapBuilder.replication(replication); return this; } @Override - public ChronicleSetBuilder file(File file) throws IOException { - chronicleMapBuilder.file(file); + public ChronicleSetBuilder replication(byte identifier, TcpConfig tcpTransportAndNetwork) { + chronicleMapBuilder.replication(identifier, tcpTransportAndNetwork); return this; } + @Override + public StatelessClientBuilder> statelessClient( + InetSocketAddress remoteAddress) { + return new StatelessSetBuilder<>(chronicleMapBuilder.statelessClient(remoteAddress)); + } + + @Override + public ChronicleHashInstanceBuilder> instance() { + return new InstanceBuilder<>(chronicleMapBuilder.instance()); + } + @Override public ChronicleSet create() throws IOException { final ChronicleMap map = chronicleMapBuilder.create(); return new SetFromMap(map); } + @Override + public ChronicleSet createPersistedTo(File file) throws IOException { + ChronicleMap map = chronicleMapBuilder.createPersistedTo(file); + return new SetFromMap(map); + } } diff --git a/src/main/java/net/openhft/chronicle/set/InstanceBuilder.java b/src/main/java/net/openhft/chronicle/set/InstanceBuilder.java new file mode 100644 index 000000000..d12f582a2 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/set/InstanceBuilder.java @@ -0,0 +1,62 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.set; + +import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; +import net.openhft.chronicle.hash.replication.ReplicationChannel; +import net.openhft.chronicle.hash.replication.SimpleReplication; +import net.openhft.chronicle.hash.replication.TcpConfig; +import net.openhft.chronicle.map.ChronicleMap; + +import java.io.File; +import java.io.IOException; + +final class InstanceBuilder implements ChronicleHashInstanceBuilder> { + + private final ChronicleHashInstanceBuilder> mapBuilder; + + InstanceBuilder(ChronicleHashInstanceBuilder> mapBuilder) { + this.mapBuilder = mapBuilder; + } + + @Override + public ChronicleHashInstanceBuilder> replicated( + byte identifier, TcpConfig tcpTransportAndNetwork) { + return new InstanceBuilder<>(mapBuilder.replicated(identifier, tcpTransportAndNetwork)); + } + + @Override + public ChronicleHashInstanceBuilder> replicated(SimpleReplication replication) { + return new InstanceBuilder<>(mapBuilder.replicated(replication)); + } + + @Override + public ChronicleHashInstanceBuilder> replicatedViaChannel( + ReplicationChannel channel) { + return new InstanceBuilder<>(mapBuilder.replicatedViaChannel(channel)); + } + + @Override + public ChronicleHashInstanceBuilder> persistedTo(File file) { + return new InstanceBuilder<>(mapBuilder.persistedTo(file)); + } + + @Override + public ChronicleSet create() throws IOException { + return new SetFromMap<>(mapBuilder.create()); + } +} diff --git a/src/main/java/net/openhft/chronicle/set/StatelessSetBuilder.java b/src/main/java/net/openhft/chronicle/set/StatelessSetBuilder.java new file mode 100644 index 000000000..d67829fda --- /dev/null +++ b/src/main/java/net/openhft/chronicle/set/StatelessSetBuilder.java @@ -0,0 +1,42 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * 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 net.openhft.chronicle.set; + +import net.openhft.chronicle.hash.StatelessClientBuilder; +import net.openhft.chronicle.map.ChronicleMap; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +final class StatelessSetBuilder implements StatelessClientBuilder> { + + private final StatelessClientBuilder> mapClientBuilder; + + StatelessSetBuilder(StatelessClientBuilder> mapClientBuilder) { + this.mapClientBuilder = mapClientBuilder; + } + + @Override + public StatelessClientBuilder> timeout(long timeout, TimeUnit units) { + return new StatelessSetBuilder<>(mapClientBuilder.timeout(timeout, units)); + } + + @Override + public ChronicleSet create() throws IOException { + return new SetFromMap<>(mapClientBuilder.create()); + } +} diff --git a/src/test/java/eg/BigData.java b/src/test/java/eg/BigData.java index 745998a78..177b1059f 100644 --- a/src/test/java/eg/BigData.java +++ b/src/test/java/eg/BigData.java @@ -52,7 +52,7 @@ public class BigData { String chmPath = dir + "/testmap-" + Long.toString(System.nanoTime(), 36); new File(chmPath).deleteOnExit(); try { - theMap = builder.file(new File(chmPath)).create(); + theMap = builder.createPersistedTo(new File(chmPath)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/test/java/net/openhft/chronicle/map/AcquireGetUsingMain.java b/src/test/java/net/openhft/chronicle/map/AcquireGetUsingMain.java index befa4148a..fe6b10497 100644 --- a/src/test/java/net/openhft/chronicle/map/AcquireGetUsingMain.java +++ b/src/test/java/net/openhft/chronicle/map/AcquireGetUsingMain.java @@ -29,7 +29,9 @@ import static org.junit.Assert.fail; public class AcquireGetUsingMain { public static void main(String[] args) throws Exception { File file = new File(System.getProperty("java.io.tmpdir") + "/test1"); - ChronicleMap theSharedMap = OffHeapUpdatableChronicleMapBuilder.of(String.class, Data.class).file(file).create(); + ChronicleMap theSharedMap = + OffHeapUpdatableChronicleMapBuilder.of(String.class, Data.class) + .createPersistedTo(file); Data data = DataValueClasses.newDirectReference(Data.class); String processType = "testkey"; if (theSharedMap.getUsing(processType, data) == null) { diff --git a/src/test/java/net/openhft/chronicle/map/Builder.java b/src/test/java/net/openhft/chronicle/map/Builder.java index 1225a0fc9..a7e700f76 100644 --- a/src/test/java/net/openhft/chronicle/map/Builder.java +++ b/src/test/java/net/openhft/chronicle/map/Builder.java @@ -19,7 +19,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.TcpConfig; import java.io.File; import java.io.IOException; @@ -71,10 +71,10 @@ public class Builder { final byte identifier, final int serverPort, final InetSocketAddress... endpoints) throws IOException { - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(serverPort, endpoints) + TcpConfig tcpConfig = TcpConfig.of(serverPort, endpoints) .heartBeatInterval(1L, TimeUnit.SECONDS); return ChronicleMapBuilder.of(kClass, vClass) - .replicators(identifier, tcpConfig); + .replication(identifier, tcpConfig); } diff --git a/src/test/java/net/openhft/chronicle/map/CHMLatencyTestMain.java b/src/test/java/net/openhft/chronicle/map/CHMLatencyTestMain.java index e6711b4ca..6649c1d86 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMLatencyTestMain.java +++ b/src/test/java/net/openhft/chronicle/map/CHMLatencyTestMain.java @@ -79,7 +79,7 @@ public class CHMLatencyTestMain { OffHeapUpdatableChronicleMapBuilder.of(LongValue.class, LongValue.class) .entries(KEYS) .entrySize(16) - .createWithFile(file); + .createPersistedTo(file); // add keys LongValue key = DataValueClasses.newInstance(LongValue.class); diff --git a/src/test/java/net/openhft/chronicle/map/CHMTest5.java b/src/test/java/net/openhft/chronicle/map/CHMTest5.java index e631c45be..6979bc473 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMTest5.java +++ b/src/test/java/net/openhft/chronicle/map/CHMTest5.java @@ -44,7 +44,8 @@ public class CHMTest5 { String chmPath = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + "CHMTest5"; - ChronicleMap theSharedMap = builder.file(new File(chmPath)).create(); + ChronicleMap theSharedMap = + builder.createPersistedTo(new File(chmPath)); //Now get the shared data object, auto-creating if it's not there CHMTest5Data data = DataValueClasses.newDirectReference(CHMTest5Data.class); diff --git a/src/test/java/net/openhft/chronicle/map/ChannelReplicationTest.java b/src/test/java/net/openhft/chronicle/map/ChannelReplicationTest.java index c726da6eb..2214fab56 100644 --- a/src/test/java/net/openhft/chronicle/map/ChannelReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChannelReplicationTest.java @@ -19,9 +19,8 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.ChannelProvider; -import net.openhft.chronicle.hash.ChannelProviderBuilder; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.ReplicationHub; +import net.openhft.chronicle.hash.replication.TcpConfig; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -47,47 +46,46 @@ public class ChannelReplicationTest { private ChronicleMap map1b; private ChronicleMap map2b; - private ChannelProvider channelProviderA; - private ChannelProvider channelProviderB; + private ReplicationHub hubA; + private ReplicationHub hubB; @Before - public void setup() throws IOException { { - TcpReplicationConfig tcpConfig = TcpReplicationConfig + TcpConfig tcpConfig = TcpConfig .of(8086, new InetSocketAddress("localhost", 8087)) .heartBeatInterval(1, SECONDS); - channelProviderA = new ChannelProviderBuilder() - .replicators((byte) 1, tcpConfig) - .create(); + hubA = ReplicationHub.builder() + .tcpTransportAndNetwork(tcpConfig) + .create((byte) 1); map1a = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderA.createChannel((short) 1)).create(); + .instance().replicatedViaChannel(hubA.createChannel((short) 1)).create(); } { - TcpReplicationConfig tcpConfig = - TcpReplicationConfig.of(8087).heartBeatInterval(1, SECONDS); + TcpConfig tcpConfig = + TcpConfig.of(8087).heartBeatInterval(1, SECONDS); - channelProviderB = new ChannelProviderBuilder().replicators((byte) 2, tcpConfig) - .create(); + hubB = ReplicationHub.builder().tcpTransportAndNetwork(tcpConfig) + .create((byte) 2); map1b = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderB.createChannel((short) 1)).create(); + .instance().replicatedViaChannel(hubB.createChannel((short) 1)).create(); } } @After public void tearDown() throws InterruptedException { - for (final Closeable closeable : new Closeable[]{channelProviderA, channelProviderB}) { + for (final Closeable closeable : new Closeable[]{map1a, map1b, map2a, map2b}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } @@ -99,12 +97,12 @@ public class ChannelReplicationTest { map2b = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderB.createChannel((short) 2)).create(); + .instance().replicatedViaChannel(hubB.createChannel((short) 2)).create(); map2a = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderA.createChannel((short) 2)).create(); + .instance().replicatedViaChannel(hubA.createChannel((short) 2)).create(); map2a.put(1, "EXAMPLE-2"); map1a.put(1, "EXAMPLE-1"); diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java index 091b097f8..1ff16445c 100644 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java @@ -590,7 +590,7 @@ public class ChronicleMapTest { File tmpFile = File.createTempFile("testAcquirePerf", ".deleteme"); tmpFile.deleteOnExit(); - final ChronicleMap map = builder.createWithFile(tmpFile); + final ChronicleMap map = builder.createPersistedTo(tmpFile); int count = runs > 500 ? runs > 1200 ? 3 : 5 : 5; final int independence = Math.min(procs, runs > 500 ? 8 : 4); @@ -660,7 +660,7 @@ public class ChronicleMapTest { File tmpFile = File.createTempFile("testAcquirePerf", ".deleteme"); tmpFile.deleteOnExit(); - final ChronicleMap map = builder.createWithFile(tmpFile); + final ChronicleMap map = builder.createPersistedTo(tmpFile); int count = runs > 500 ? runs > 1200 ? 3 : 5 : 5; final int independence = Math.min(procs, runs > 500 ? 8 : 4); @@ -1229,10 +1229,9 @@ public class ChronicleMapTest { .entries(10) .minSegments(1) .entrySize(10); - builder.file(getPersistenceFile()); testOversizeEntriesPutRemoveReplace( (VanillaChronicleMap) - builder.create() + builder.createPersistedTo(getPersistenceFile()) ); } @@ -1334,7 +1333,7 @@ public class ChronicleMapTest { .entrySize(16); File tmpFile = File.createTempFile("testAcquireUsingLocked", ".deleteme"); tmpFile.deleteOnExit(); - final ChronicleMap map = builder.createWithFile(tmpFile); + final ChronicleMap map = builder.createPersistedTo(tmpFile); LongValue value = nativeLongValue(); @@ -1391,7 +1390,7 @@ public class ChronicleMapTest { .entrySize(40); File tmpFile = File.createTempFile("testAcquireUsingLocked", ".deleteme"); tmpFile.deleteOnExit(); - final ChronicleMap map = builder.createWithFile(tmpFile); + final ChronicleMap map = builder.createPersistedTo(tmpFile); LongValue value = DataValueClasses.newInstance(LongValue.class); diff --git a/src/test/java/net/openhft/chronicle/map/DefaultValueTest.java b/src/test/java/net/openhft/chronicle/map/DefaultValueTest.java index 249b017ad..96180ebc8 100644 --- a/src/test/java/net/openhft/chronicle/map/DefaultValueTest.java +++ b/src/test/java/net/openhft/chronicle/map/DefaultValueTest.java @@ -39,7 +39,7 @@ public class DefaultValueTest { defaultValue.add(42); try (ChronicleMap> map = ChronicleMapBuilder .of(String.class, (Class>) ((Class) List.class)) - .defaultValue(defaultValue).file(file).create()) { + .defaultValue(defaultValue).createPersistedTo(file)) { assertEquals(defaultValue, map.acquireUsing("a", null)); assertEquals(1, map.size()); @@ -49,8 +49,8 @@ public class DefaultValueTest { } try (ChronicleMap> map = ChronicleMapBuilder - .of(String.class, (Class>) ((Class) List.class)).file(file). - create()) { + .of(String.class, (Class>) ((Class) List.class)) + .createPersistedTo(file)) { assertEquals(defaultValue, map.acquireUsing("c", null)); } } finally { diff --git a/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java b/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java index c15ff7d53..d3cad018a 100644 --- a/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java @@ -55,9 +55,8 @@ public class EntryCountMapTest { .entries(entries) .minSegments(segments) .entrySize(entrySize) - .entryAndValueAlignment(alignment) - .file(getPersistenceFile()); - return mapBuilder.create(); + .entryAndValueAlignment(alignment); + return mapBuilder.createPersistedTo(getPersistenceFile()); } @Test diff --git a/src/test/java/net/openhft/chronicle/map/EventListenerTestWithTCPSocketReplication.java b/src/test/java/net/openhft/chronicle/map/EventListenerTestWithTCPSocketReplication.java index ff29d8ac3..9b0afb3d8 100644 --- a/src/test/java/net/openhft/chronicle/map/EventListenerTestWithTCPSocketReplication.java +++ b/src/test/java/net/openhft/chronicle/map/EventListenerTestWithTCPSocketReplication.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.TcpConfig; import net.openhft.lang.io.Bytes; import org.junit.After; import org.junit.Assert; @@ -48,7 +48,7 @@ public class EventListenerTestWithTCPSocketReplication { for (final Closeable closeable : new Closeable[]{map1, map2}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } @@ -85,22 +85,22 @@ public class EventListenerTestWithTCPSocketReplication { // we connect the maps via a TCP socket connection on port 8077 - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8076, new InetSocketAddress("localhost", 8077)); + TcpConfig tcpConfig = TcpConfig.of(8076, new InetSocketAddress("localhost", 8077)); ChronicleMapBuilder map1Builder = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000L) - .replicators((byte) 2, tcpConfig); + .replication((byte) 2, tcpConfig); map1 = map1Builder.create(); } // ---------- SERVER2 2 on the same server as ---------- { - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8077); + TcpConfig tcpConfig = TcpConfig.of(8077); ChronicleMapBuilder map2Builder = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000L) - .replicators((byte) 1, tcpConfig) + .replication((byte) 1, tcpConfig) .eventListener(eventListener); map2 = map2Builder.create(); @@ -158,11 +158,11 @@ public class EventListenerTestWithTCPSocketReplication { // we connect the maps via a TCP socket connection on port 8077 - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8076, new InetSocketAddress("localhost", 8077)); + TcpConfig tcpConfig = TcpConfig.of(8076, new InetSocketAddress("localhost", 8077)); ChronicleMapBuilder map1Builder = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000L) - .replicators((byte) 2, tcpConfig); + .replication((byte) 2, tcpConfig); map1 = map1Builder.create(); @@ -170,11 +170,11 @@ public class EventListenerTestWithTCPSocketReplication { // ---------- SERVER2 2 on the same server as ---------- { - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8077); + TcpConfig tcpConfig = TcpConfig.of(8077); ChronicleMapBuilder map2Builder = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000L) - .replicators((byte) 1, tcpConfig) + .replication((byte) 1, tcpConfig) .eventListener(eventListener); map2 = map2Builder.create(); @@ -233,11 +233,11 @@ public class EventListenerTestWithTCPSocketReplication { // we connect the maps via a TCP socket connection on port 8077 - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8076, new InetSocketAddress("localhost", 8077)); + TcpConfig tcpConfig = TcpConfig.of(8076, new InetSocketAddress("localhost", 8077)); ChronicleMapBuilder map1Builder = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000L) - .replicators((byte) 2, tcpConfig); + .replication((byte) 2, tcpConfig); map1 = map1Builder.create(); map1.put(5, "WILL_GET_OVERWRITEN"); @@ -245,11 +245,11 @@ public class EventListenerTestWithTCPSocketReplication { // ---------- SERVER2 2 on the same server as ---------- { - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8077); + TcpConfig tcpConfig = TcpConfig.of(8077); ChronicleMapBuilder map2Builder = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000L) - .replicators((byte) 1, tcpConfig) + .replication((byte) 1, tcpConfig) .eventListener(eventListener); map2 = map2Builder.create(); diff --git a/src/test/java/net/openhft/chronicle/map/PostChannelCreationTest.java b/src/test/java/net/openhft/chronicle/map/PostChannelCreationTest.java index dcd9890bc..f56d70eb1 100644 --- a/src/test/java/net/openhft/chronicle/map/PostChannelCreationTest.java +++ b/src/test/java/net/openhft/chronicle/map/PostChannelCreationTest.java @@ -19,9 +19,8 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.ChannelProvider; -import net.openhft.chronicle.hash.ChannelProviderBuilder; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.ReplicationHub; +import net.openhft.chronicle.hash.replication.TcpConfig; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -48,8 +47,8 @@ public class PostChannelCreationTest { private ChronicleMap map1b; private ChronicleMap map2b; - private ChannelProvider channelProviderA; - private ChannelProvider channelProviderB; + private ReplicationHub hubA; + private ReplicationHub hubB; public static File getPersistenceFile() { String TMP = System.getProperty("java.io.tmpdir"); @@ -60,44 +59,43 @@ public class PostChannelCreationTest { @Before - public void setup() throws IOException { { - final TcpReplicationConfig tcpConfig = TcpReplicationConfig + final TcpConfig tcpConfig = TcpConfig .of(8086, new InetSocketAddress("localhost", 8087)) .heartBeatInterval(1, SECONDS); - channelProviderA = new ChannelProviderBuilder() - .replicators((byte) 1, tcpConfig).create(); + hubA = ReplicationHub.builder().tcpTransportAndNetwork(tcpConfig) + .create((byte) 1); // this is how you add maps after the custer is created map1a = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderA.createChannel((short) 1)).create(); + .instance().replicatedViaChannel(hubA.createChannel((short) 1)).create(); map2a = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderA.createChannel((short) 2)).create(); + .instance().replicatedViaChannel(hubA.createChannel((short) 2)).create(); } { - final TcpReplicationConfig tcpConfig = TcpReplicationConfig + final TcpConfig tcpConfig = TcpConfig .of(8087, new InetSocketAddress("localhost", 8086)) .heartBeatInterval(1, SECONDS); - channelProviderB = new ChannelProviderBuilder() - .replicators((byte) 2, tcpConfig).create(); + hubB = ReplicationHub.builder().tcpTransportAndNetwork(tcpConfig) + .create((byte) 2); // this is how you add maps after the custer is created map1b = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderB.createChannel((short) 1)).create(); + .instance().replicatedViaChannel(hubB.createChannel((short) 1)).create(); map2b = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderB.createChannel((short) 2)).create(); + .instance().replicatedViaChannel(hubB.createChannel((short) 2)).create(); } @@ -106,10 +104,10 @@ public class PostChannelCreationTest { @After public void tearDown() throws InterruptedException { - for (final Closeable closeable : new Closeable[]{channelProviderA, channelProviderB}) { + for (final Closeable closeable : new Closeable[]{map1a, map1b, map2a, map2b}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/SerializerTest.java b/src/test/java/net/openhft/chronicle/map/SerializerTest.java index 5808d89b4..4bf4a05e7 100644 --- a/src/test/java/net/openhft/chronicle/map/SerializerTest.java +++ b/src/test/java/net/openhft/chronicle/map/SerializerTest.java @@ -73,7 +73,7 @@ public class SerializerTest { ByteBufferBytes in = out.slice(); ChronicleMapBuilder builder = ChronicleMapBuilder.of(Integer.class, valueClass); - builder.preMapConstruction(); + builder.preMapConstruction(false); Serializer v = new Serializer(builder.valueBuilder); @@ -96,7 +96,7 @@ public class SerializerTest { ByteBufferBytes in = out.slice(); ChronicleMapBuilder builder = ChronicleMapBuilder.of(clazz, Integer.class); - builder.preMapConstruction(); + builder.preMapConstruction(false); { Serializer v = new Serializer(builder.keyBuilder); diff --git a/src/test/java/net/openhft/chronicle/map/SingleMapChannelTest.java b/src/test/java/net/openhft/chronicle/map/SingleMapChannelTest.java index 8cbd1fe0b..00ee14308 100644 --- a/src/test/java/net/openhft/chronicle/map/SingleMapChannelTest.java +++ b/src/test/java/net/openhft/chronicle/map/SingleMapChannelTest.java @@ -19,9 +19,8 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.ChannelProvider; -import net.openhft.chronicle.hash.ChannelProviderBuilder; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.ReplicationHub; +import net.openhft.chronicle.hash.replication.TcpConfig; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -46,8 +45,8 @@ public class SingleMapChannelTest { private ChronicleMap map1b; - private ChannelProvider channelProviderA; - private ChannelProvider channelProviderB; + private ReplicationHub hubA; + private ReplicationHub hubB; public static File getPersistenceFile() { String TMP = System.getProperty("java.io.tmpdir"); @@ -61,43 +60,44 @@ public class SingleMapChannelTest { public void setup() throws IOException { { - final TcpReplicationConfig tcpReplicationConfig = TcpReplicationConfig + final TcpConfig tcpConfig = TcpConfig .of(8086, new InetSocketAddress("localhost", 8087)) .heartBeatInterval(1, SECONDS); int maxEntrySize = 1024; byte identifier= 1; - channelProviderA = new ChannelProviderBuilder() + hubA = ReplicationHub.builder() .maxEntrySize(maxEntrySize) - .replicators(identifier, tcpReplicationConfig).create(); + .tcpTransportAndNetwork(tcpConfig) + .create(identifier); // this is how you add maps after the custer is created map1a = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .channel(channelProviderA.createChannel((short) 1)).create(); + .instance().replicatedViaChannel(hubA.createChannel((short) 1)).create(); } { - final TcpReplicationConfig tcpReplicationConfig = TcpReplicationConfig + final TcpConfig tcpConfig = TcpConfig .of(8087, new InetSocketAddress("localhost", 8086)) .heartBeatInterval(1, SECONDS); - channelProviderB = new ChannelProviderBuilder() - .replicators((byte) 2, tcpReplicationConfig).create(); + hubB = ReplicationHub.builder().tcpTransportAndNetwork(tcpConfig).create((byte) 2); // this is how you add maps after the custer is created map1b = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .channel(channelProviderB.createChannel((short) 1)) - .entries(1000).create(); + .entries(1000) + .instance().replicatedViaChannel(hubB.createChannel((short) 1)) + .create(); } } @After public void tearDown() throws InterruptedException { - for (final Closeable closeable : new Closeable[]{channelProviderA, channelProviderB}) { + for (final Closeable closeable : new Closeable[]{map1a, map1b}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java b/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java index a479a9065..fae9889f9 100644 --- a/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java +++ b/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.TcpConfig; import org.junit.Assert; import org.junit.Test; @@ -29,8 +29,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -import static net.openhft.chronicle.hash.StatelessBuilder.remoteAddress; - /** * @author Rob Austin. @@ -52,13 +50,13 @@ public class StatelessClientTest { { statelessMap = ChronicleMapBuilder.of(Integer .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); } // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)).create(); + .replication((byte) 2, TcpConfig.of(8086)).create(); } @@ -95,13 +93,13 @@ public class StatelessClientTest { { statelessMap = ChronicleMapBuilder.of(Integer .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); } // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)).create(); + .replication((byte) 2, TcpConfig.of(8086)).create(); } @@ -136,13 +134,13 @@ public class StatelessClientTest { { statelessMap = ChronicleMapBuilder.of(Integer .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); } // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)).create(); + .replication((byte) 2, TcpConfig.of(8086)).create(); } @@ -175,7 +173,7 @@ public class StatelessClientTest { // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)).create(); + .replication((byte) 2, TcpConfig.of(8086)).create(); serverMap.put(10, "EXAMPLE-10"); } @@ -185,7 +183,7 @@ public class StatelessClientTest { { statelessMap = ChronicleMapBuilder.of(Integer .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); Assert.assertEquals("EXAMPLE-10", statelessMap.get(10)); Assert.assertEquals(1, statelessMap.size()); @@ -208,13 +206,13 @@ public class StatelessClientTest { { statelessMap = ChronicleMapBuilder.of(Integer .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); } // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)).create(); + .replication((byte) 2, TcpConfig.of(8086)).create(); serverMap.put(10, "EXAMPLE-10"); } @@ -240,13 +238,13 @@ public class StatelessClientTest { { statelessMap = ChronicleMapBuilder.of(Integer .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); } // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)).create(); + .replication((byte) 2, TcpConfig.of(8086)).create(); } @@ -283,13 +281,13 @@ public class StatelessClientTest { statelessMap = ChronicleMapBuilder.of(Integer .class, CharSequence.class) .putReturnsNull(true) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); } // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)) + .replication((byte) 2, TcpConfig.of(8086)) .putReturnsNull(true) .create(); } @@ -329,13 +327,13 @@ public class StatelessClientTest { .class, CharSequence.class) .putReturnsNull(true) .removeReturnsNull(true) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); } // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)) + .replication((byte) 2, TcpConfig.of(8086)) .putReturnsNull(true) .removeReturnsNull(true) .create(); @@ -368,13 +366,13 @@ public class StatelessClientTest { .class, CharSequence.class) .putReturnsNull(true) .removeReturnsNull(true) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); } // server { serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)) + .replication((byte) 2, TcpConfig.of(8086)) .putReturnsNull(true) .removeReturnsNull(true) .create(); @@ -408,18 +406,18 @@ public class StatelessClientTest { { statelessMap1 = ChronicleMapBuilder.of(Integer .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8086))).create(); + .statelessClient(new InetSocketAddress("localhost", 8086)).create(); statelessMap2 = ChronicleMapBuilder.of(Integer .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", 8077))).create(); + .statelessClient(new InetSocketAddress("localhost", 8077)).create(); } // server { serverMap1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8086)).create(); + .replication((byte) 2, TcpConfig.of(8086)).create(); serverMap2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(8077)).create(); + .replication((byte) 2, TcpConfig.of(8077)).create(); } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication4WayMapTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication4WayMapTest.java index 2eec55000..51e9f3831 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication4WayMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication4WayMapTest.java @@ -53,18 +53,6 @@ public class TCPSocketReplication4WayMapTest { } - - static ChronicleMap newTcpSocketShmIntValueString( - final byte identifier, - final int serverPort, - final InetSocketAddress... endpoints) throws IOException { - return newTcpSocketShmBuilder(IntValue.class, CharSequence.class, - identifier, serverPort, endpoints) - .entries(20000L) - .keyMarshaller(ByteableIntValueMarshaller.INSTANCE).create(); - } - - @Before public void setup() throws IOException { @@ -82,7 +70,7 @@ public class TCPSocketReplication4WayMapTest { for (final Closeable closeable : new Closeable[]{map1, map2, map3, map4}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTests.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTests.java index 7eb7f7d7c..963d0ed13 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTests.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTests.java @@ -51,7 +51,7 @@ public class TCPSocketReplicationBootStrapTests { newTcpSocketShmBuilder(Integer.class, CharSequence.class, (byte) 2, 8076, new InetSocketAddress("localhost", 8079)); final ChronicleMap map2a = - map2aBuilder.file(getPersistenceFile()).create(); + map2aBuilder.createPersistedTo(getPersistenceFile()); map2a.put(10, "EXAMPLE-10"); // this will be the last time that map1 go an update from map2 long lastModificationTime; @@ -69,16 +69,14 @@ public class TCPSocketReplicationBootStrapTests { // restart map 2 but don't doConnect it to map one final ChronicleMap map2b = ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .file(map2File) - .create(); + .createPersistedTo(map2File); // add data into it map2b.put(11, "ADDED WHEN DISCONNECTED TO MAP1"); map2b.close(); } // now restart map2a and doConnect it to map1, map1 should bootstrap the missing entry - map2aBuilder.file(map2File); - map2 = map2aBuilder.create(); + map2 = map2aBuilder.createPersistedTo(map2File); // add data into it waitTillEqual(5000); @@ -96,7 +94,7 @@ public class TCPSocketReplicationBootStrapTests { final ReplicatedChronicleMap map2a = (ReplicatedChronicleMap) - map2aBuilder.file(getPersistenceFile()).create(); + map2aBuilder.createPersistedTo(getPersistenceFile()); map2a.put(10, "EXAMPLE-10"); // this will be the last time that map1 go an update from map2 @@ -113,18 +111,16 @@ public class TCPSocketReplicationBootStrapTests { { // restart map 2 but don't doConnect it to map one - ChronicleMapBuilder.of(Integer.class, CharSequence.class).file(map2File); final ChronicleMap map2b = - ChronicleMapBuilder.of(Integer.class, CharSequence.class).file(map2File). - create(); + ChronicleMapBuilder.of(Integer.class, CharSequence.class) + .createPersistedTo(map2File); // add data into it map2b.put(11, "ADDED WHEN DISCONNECTED TO MAP1"); map2b.close(); } // now restart map2a and doConnect it to map1, map1 should bootstrap the missing entry - map2aBuilder.file(map2File); - map2 = map2aBuilder.create(); + map2 = map2aBuilder.createPersistedTo(map2File); // add data into it waitTillEqual(20000); @@ -139,7 +135,7 @@ public class TCPSocketReplicationBootStrapTests { for (final Closeable closeable : new Closeable[]{map1, map2}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java index 982d5ca78..93429dd23 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java @@ -34,7 +34,6 @@ import java.util.Random; import static net.openhft.chronicle.map.Builder.getPersistenceFile; import static net.openhft.chronicle.map.Builder.newTcpSocketShmBuilder; -import static net.openhft.chronicle.map.TCPSocketReplication4WayMapTest.newTcpSocketShmIntValueString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -57,9 +56,14 @@ public class TCPSocketReplicationIntValueTest { ((Byteable) value).bytes(new ByteBufferBytes(ByteBuffer.allocateDirect(4)), 0); map1Builder = newTcpSocketShmBuilder(IntValue.class, CharSequence.class, (byte) 1, 8076, new InetSocketAddress("localhost", 8077)); - map1Builder.keyMarshaller(ByteableIntValueMarshaller.INSTANCE).file(getPersistenceFile()); - map1 = map1Builder.keyMarshaller(ByteableIntValueMarshaller.INSTANCE).create(); - map2 = newTcpSocketShmIntValueString((byte) 2, 8077); + map1Builder.keyMarshaller(ByteableIntValueMarshaller.INSTANCE); + map1 = map1Builder.keyMarshaller(ByteableIntValueMarshaller.INSTANCE) + .createPersistedTo(getPersistenceFile()); + map2 = newTcpSocketShmBuilder(IntValue.class, CharSequence.class, + (byte) 2, 8077) + .entries(20000L) + .keyMarshaller(ByteableIntValueMarshaller.INSTANCE) + .create(); } @@ -69,7 +73,7 @@ public class TCPSocketReplicationIntValueTest { for (final Closeable closeable : new Closeable[]{map1, map2}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java index 7ba19246e..f8e8f7a52 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java @@ -50,8 +50,7 @@ public class TCPSocketReplicationTest { ChronicleMapBuilder map1Builder = newTcpSocketShmBuilder(Integer.class, CharSequence.class, (byte) 1, 8076, new InetSocketAddress("localhost", 8077)); - map1Builder.file(getPersistenceFile()); - map1 = map1Builder.create(); + map1 = map1Builder.createPersistedTo(getPersistenceFile()); map2 = newTcpSocketShmIntString((byte) 2, 8077); } @@ -61,7 +60,7 @@ public class TCPSocketReplicationTest { for (final Closeable closeable : new Closeable[]{map1, map2}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3VoidValueTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3VoidValueTest.java index 8f532bc9f..ab87b3d16 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3VoidValueTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3VoidValueTest.java @@ -55,7 +55,7 @@ public class TCPSocketReplicationTest3VoidValueTest { for (final Closeable closeable : new Closeable[]{map1, map2, map3}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3way.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3way.java index 28c8a5eb8..1c546d0a8 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3way.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3way.java @@ -56,7 +56,7 @@ public class TCPSocketReplicationTest3way { for (final Closeable closeable : new Closeable[]{map1, map2, map3}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3wayPutReturnsNull.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3wayPutReturnsNull.java index 4a2f2a52e..ce022aefd 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3wayPutReturnsNull.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest3wayPutReturnsNull.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.TcpConfig; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -47,11 +47,11 @@ public class TCPSocketReplicationTest3wayPutReturnsNull { final byte identifier, final int serverPort, final InetSocketAddress... endpoints) throws IOException { - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(serverPort, endpoints); + TcpConfig tcpConfig = TcpConfig.of(serverPort, endpoints); return (T) ChronicleMapBuilder.of(Integer.class, CharSequence.class) .putReturnsNull(true) - .replicators(identifier, tcpConfig).create(); + .replication(identifier, tcpConfig).create(); } @@ -69,7 +69,7 @@ public class TCPSocketReplicationTest3wayPutReturnsNull { for (final Closeable closeable : new Closeable[]{map1, map2, map3}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTestPostConnection.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTestPostConnection.java index c2656ddd5..2fe5cb61f 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTestPostConnection.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTestPostConnection.java @@ -79,23 +79,21 @@ public class TCPSocketReplicationTestPostConnection { ChronicleMapBuilder map2aBuilder = newTcpSocketShmBuilder(Integer.class, CharSequence.class, (byte) 2, 8077, new InetSocketAddress("localhost", 8076)); - map2aBuilder.file(Builder.getPersistenceFile()); - final ChronicleMap map2a = - map2aBuilder.create(); - map1 = TCPSocketReplication4WayMapTest.newTcpSocketShmIntString((byte) 1, 8076); + try (final ChronicleMap map2a = + map2aBuilder.createPersistedTo(Builder.getPersistenceFile())) { + map1 = TCPSocketReplication4WayMapTest.newTcpSocketShmIntString((byte) 1, 8076); - Thread.sleep(1); - map1.put(5, "EXAMPLE-2"); + Thread.sleep(1); + map1.put(5, "EXAMPLE-2"); - map2a.close(); + } Thread.sleep(1); map1.put(6, "EXAMPLE-1"); // recreate map2 with new unique file - map2aBuilder.file(Builder.getPersistenceFile()); - map2 = map2aBuilder.create(); + map2 = map2aBuilder.createPersistedTo(Builder.getPersistenceFile()); // allow time for the recompilation to resolve @@ -115,7 +113,7 @@ public class TCPSocketReplicationTestPostConnection { try { if (closeable != null) closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTests.java b/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTests.java index f35862472..e26b077bd 100644 --- a/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTests.java +++ b/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTests.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TimeProvider; +import net.openhft.chronicle.hash.replication.TimeProvider; import net.openhft.chronicle.map.jrs166.JSR166TestCase; import org.junit.Test; import org.mockito.Mockito; @@ -49,12 +49,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testIgnoreALatePut() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(10).replicators((byte) 1); ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).create(); + .entries(10) + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -80,11 +78,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testIgnoreALatePutIfAbsent() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(10).replicators((byte) 1); ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).create(); + .entries(10) + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -111,11 +108,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testIgnoreALateReplace() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(10).replicators((byte) 1); ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).create(); + .entries(10) + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -145,14 +141,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testIgnoreALateReplaceWithValue() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(10).replicators((byte) 1); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).file(getPersistenceFile()); ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).create(); + .entries(10) + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -179,13 +171,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testIgnoreALateRemoveWithValue() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class).replicators((byte) 1); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).file(getPersistenceFile()); ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).create(); + .entries(10) + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -211,13 +200,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testIgnoreALateRemove() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class).replicators((byte) 1); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).file(getPersistenceFile()); ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).create(); + .entries(10) + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -243,13 +229,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testIgnoreWithRemoteRemove() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class).replicators((byte) 1); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).file(getPersistenceFile()); ReplicatedChronicleMap map = (ReplicatedChronicleMap) ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).create(); + .entries(10) + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -276,13 +259,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class).replicators((byte) 1); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).file(getPersistenceFile()); ReplicatedChronicleMap map = (ReplicatedChronicleMap) ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 1) - .timeProvider(timeProvider).create(); + .entries(10) + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -309,16 +289,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testRemoveFollowedByLatePut() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(10) - .replicators((byte) 1) - .entries(10) - .timeProvider(timeProvider).file(getPersistenceFile()); ReplicatedChronicleMap map = (ReplicatedChronicleMap) ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) .entries(10) - .replicators((byte) 1) - .entries(10) - .timeProvider(timeProvider).create(); + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); @@ -343,16 +317,10 @@ public class TimeBasedReplicationTests extends JSR166TestCase { public void testPutRemovePut() throws IOException { final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(10).replicators((byte) 1); - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(10) - .entries(10) - .timeProvider(timeProvider).file(getPersistenceFile()); ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) .entries(10) - .entries(10) - .timeProvider(timeProvider).create(); + .timeProvider(timeProvider) + .createReplicated((byte) 1); current(timeProvider); diff --git a/src/test/java/net/openhft/chronicle/map/TwoMapOnDifferentServers.java b/src/test/java/net/openhft/chronicle/map/TwoMapOnDifferentServers.java index 6d64e8ae2..3c238584d 100644 --- a/src/test/java/net/openhft/chronicle/map/TwoMapOnDifferentServers.java +++ b/src/test/java/net/openhft/chronicle/map/TwoMapOnDifferentServers.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.TcpConfig; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -30,7 +30,7 @@ import java.net.InetSocketAddress; import static java.util.concurrent.TimeUnit.SECONDS; import static net.openhft.chronicle.map.Builder.getPersistenceFile; -import static net.openhft.chronicle.hash.TcpReplicationConfig.of; +import static net.openhft.chronicle.hash.replication.TcpConfig.of; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -46,19 +46,18 @@ public class TwoMapOnDifferentServers { @Before public void setup() throws IOException { - final TcpReplicationConfig tcpConfig = + final TcpConfig tcpConfig = of(8076, new InetSocketAddress("localhost", 8077)) .heartBeatInterval(1, SECONDS); - File persistenceFile = getPersistenceFile(); map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000) - .replicators((byte) 1, tcpConfig).create(); + .replication((byte) 1, tcpConfig).create(); map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000) - .replicators((byte) 2, of(8077).heartBeatInterval(1, SECONDS)).create(); + .replication((byte) 2, of(8077).heartBeatInterval(1, SECONDS)).create(); } @After @@ -67,7 +66,7 @@ public class TwoMapOnDifferentServers { for (final Closeable closeable : new Closeable[]{map1, map2}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java b/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java index 22887c0d8..68cb1d692 100644 --- a/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java @@ -18,7 +18,8 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.UdpReplicationConfig; +import net.openhft.chronicle.hash.replication.SimpleReplication; +import net.openhft.chronicle.hash.replication.UdpConfig; import org.junit.After; import org.junit.Before; @@ -40,12 +41,13 @@ public class UDPSocketReplicationTest { final int identifier, final int udpPort) throws IOException { - UdpReplicationConfig udpConfig = UdpReplicationConfig + UdpConfig udpConfig = UdpConfig .simple(Inet4Address.getByName("255.255.255.255"), udpPort); return ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(1000) - .replicators((byte) identifier, udpConfig).create(); + .replication(SimpleReplication.builder().udpTransport(udpConfig) + .create((byte) identifier)).create(); } @Before @@ -65,7 +67,7 @@ public class UDPSocketReplicationTest { for (final Closeable closeable : new Closeable[]{map2}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/test/java/net/openhft/chronicle/map/example/ChannelExample.java b/src/test/java/net/openhft/chronicle/map/example/ChannelExample.java index 404c74741..645b7defd 100644 --- a/src/test/java/net/openhft/chronicle/map/example/ChannelExample.java +++ b/src/test/java/net/openhft/chronicle/map/example/ChannelExample.java @@ -1,8 +1,7 @@ package net.openhft.chronicle.map.example; -import net.openhft.chronicle.hash.ChannelProvider; -import net.openhft.chronicle.hash.ChannelProviderBuilder; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.ReplicationHub; +import net.openhft.chronicle.hash.replication.TcpConfig; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.map.ChronicleMapBuilder; import org.junit.Assert; @@ -11,6 +10,7 @@ import org.junit.Test; import java.io.IOException; import java.net.InetSocketAddress; +import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertEquals; /** @@ -54,8 +54,8 @@ public class ChannelExample { private ChronicleMap favoriteColourServer1; - private ChannelProvider channelProviderServer1; - private ChannelProvider channelProviderServer2; + private ReplicationHub hubOnServer1; + private ReplicationHub hubOnServer2; @Test @@ -63,32 +63,34 @@ public class ChannelExample { // server 1 with identifier = 1 { + ChronicleMapBuilder smallStringToStringMapBuilder = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + .entries(1000); + byte identifier = (byte) 1; - TcpReplicationConfig tcpConfig = TcpReplicationConfig + TcpConfig tcpConfig = TcpConfig .of(8086, new InetSocketAddress("localhost", 8087)) - .heartBeatInterval(1, java.util.concurrent.TimeUnit.SECONDS); + .heartBeatInterval(1, SECONDS); - channelProviderServer1 = new ChannelProviderBuilder() - .replicators(identifier, tcpConfig) - .create(); + hubOnServer1 = ReplicationHub.builder() + .tcpTransportAndNetwork(tcpConfig) + .create(identifier); // this demotes favoriteColour short channel1 = (short) 1; - favoriteColourServer1 = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(1000) - .channel(channelProviderServer1.createChannel(channel1)).create(); + favoriteColourServer1 = smallStringToStringMapBuilder.instance() + .replicatedViaChannel(hubOnServer1.createChannel(channel1)).create(); favoriteColourServer1.put("peter", "green"); // this demotes favoriteComputer short channel2 = (short) 2; - favoriteComputerServer1 = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(1000) - .channel(channelProviderServer1.createChannel(channel2)).create(); + favoriteComputerServer1 = smallStringToStringMapBuilder.instance() + .replicatedViaChannel(hubOnServer1.createChannel(channel2)).create(); favoriteComputerServer1.put("peter", "dell"); @@ -96,23 +98,25 @@ public class ChannelExample { // server 2 with identifier = 2 { + ChronicleMapBuilder smallStringToStringMapBuilder = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + .entries(1000); byte identifier = (byte) 2; - TcpReplicationConfig tcpConfig = - TcpReplicationConfig.of(8087).heartBeatInterval(1, java.util.concurrent.TimeUnit.SECONDS); + TcpConfig tcpConfig = + TcpConfig.of(8087).heartBeatInterval(1, SECONDS); - channelProviderServer2 = new ChannelProviderBuilder() - .replicators(identifier, tcpConfig) - .create(); + hubOnServer2 = ReplicationHub.builder() + .tcpTransportAndNetwork(tcpConfig) + .create(identifier); // this demotes favoriteColour short channel1 = (short) 1; - favoriteColourServer2 = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(1000) - .channel(channelProviderServer2.createChannel(channel1)).create(); + favoriteColourServer2 = smallStringToStringMapBuilder.instance() + .replicatedViaChannel(hubOnServer2.createChannel(channel1)).create(); favoriteColourServer2.put("rob", "blue"); @@ -120,9 +124,8 @@ public class ChannelExample { // this demotes favoriteComputer short channel2 = (short) 2; - favoriteComputerServer2 = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(1000) - .channel(channelProviderServer2.createChannel(channel2)).create(); + favoriteComputerServer2 = smallStringToStringMapBuilder.instance() + .replicatedViaChannel(hubOnServer2.createChannel(channel2)).create(); favoriteComputerServer2.put("rob", "mac"); favoriteComputerServer2.put("daniel", "mac"); @@ -149,7 +152,7 @@ public class ChannelExample { favoriteColourServer1.close(); favoriteComputerServer2.close(); favoriteColourServer2.close(); - favoriteColourServer1.close(); + favoriteComputerServer1.close(); } diff --git a/src/test/java/net/openhft/chronicle/map/example/DistributedSequenceMain.java b/src/test/java/net/openhft/chronicle/map/example/DistributedSequenceMain.java index 562ad1a71..1a8d3b140 100644 --- a/src/test/java/net/openhft/chronicle/map/example/DistributedSequenceMain.java +++ b/src/test/java/net/openhft/chronicle/map/example/DistributedSequenceMain.java @@ -30,9 +30,6 @@ public class DistributedSequenceMain { public static void main(String... ignored) throws IOException { Class longValueClass = DataValueClasses.directClassFor(LongValue.class); - ChronicleMapBuilder.of(String.class, longValueClass) - .entries(128) - .actualSegments(1).file(new File("/tmp/counters")); ChronicleMap map = ChronicleMapBuilder.of(String.class, longValueClass) .entries(128) diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/OpenJDKAndHashMapExamplesTest.java b/src/test/java/net/openhft/chronicle/map/fromdocs/OpenJDKAndHashMapExamplesTest.java index 35dc2aa58..e33723d2d 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/OpenJDKAndHashMapExamplesTest.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/OpenJDKAndHashMapExamplesTest.java @@ -59,8 +59,7 @@ public class OpenJDKAndHashMapExamplesTest { ChronicleMap chm = OffHeapUpdatableChronicleMapBuilder .of(String.class, BondVOInterface.class) .keySize(10) - .file(file) - .create(); + .createPersistedTo(file); BondVOInterface bondVO = DataValueClasses.newDirectReference(BondVOInterface.class); @@ -80,8 +79,7 @@ public class OpenJDKAndHashMapExamplesTest { ChronicleMap chmB = OffHeapUpdatableChronicleMapBuilder .of(String.class, BondVOInterface.class) .keySize(10) - .file(file) - .create(); + .createPersistedTo(file); // ZERO Copy but creates a new off heap reference each time diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/pingpong_latency/PingPongCASLeft.java b/src/test/java/net/openhft/chronicle/map/fromdocs/pingpong_latency/PingPongCASLeft.java index df78c1860..0d31aaade 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/pingpong_latency/PingPongCASLeft.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/pingpong_latency/PingPongCASLeft.java @@ -90,14 +90,7 @@ public class PingPongCASLeft { static ChronicleMap acquireCHM() throws IOException { // ensure thread ids are globally unique. AffinitySupport.setThreadId(); - - String TMP = System.getProperty("java.io.tmpdir"); - ChronicleMapBuilder.of(String.class, BondVOInterface.class) - - .entries(16) - .entrySize(64).file(new File(TMP + "/BondPortfolioCHM")); return ChronicleMapBuilder.of(String.class, BondVOInterface.class) - .entries(16) .entrySize(64).create(); } diff --git a/src/test/java/net/openhft/chronicle/map/ipc/StateMachineTutorial.java b/src/test/java/net/openhft/chronicle/map/ipc/StateMachineTutorial.java index 9abbd9dd4..7cc518e78 100644 --- a/src/test/java/net/openhft/chronicle/map/ipc/StateMachineTutorial.java +++ b/src/test/java/net/openhft/chronicle/map/ipc/StateMachineTutorial.java @@ -40,12 +40,7 @@ public class StateMachineTutorial { try { File dataFile = new File(System.getProperty("java.io.tmpdir"), "hft-state-machine"); - - ChronicleMapBuilder.of(Integer.class, StateMachineData.class) - - .entries(8).file(dataFile); map = ChronicleMapBuilder.of(Integer.class, StateMachineData.class) - .entries(8).create(); if (args.length > 0) { diff --git a/src/test/java/net/openhft/chronicle/map/jrs166/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jrs166/map/ChronicleMapTest.java index 7620fb4a3..c3665fa8c 100644 --- a/src/test/java/net/openhft/chronicle/map/jrs166/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jrs166/map/ChronicleMapTest.java @@ -49,57 +49,33 @@ public class ChronicleMapTest extends JSR166TestCase { } static ChronicleMap newShmIntString(int size) throws IOException { - - ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .entries(size).file(getPersistenceFile()); return ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(size).create(); } static ChronicleMap newShmListBoolean(int size) throws IOException { - - ChronicleMapBuilder.of(ArrayList.class, CharSequence.class) - - .entries(size).file(getPersistenceFile()); return ChronicleMapBuilder.of(ArrayList.class, CharSequence.class) - .entries(size).create(); - } static ChronicleMap newShmListBoolean() throws IOException { - - ChronicleMapBuilder.of(ArrayList.class, CharSequence.class).file(getPersistenceFile()); return ChronicleMapBuilder.of(ArrayList.class, CharSequence.class).create(); - } static ChronicleMap newShmStringString(int size) throws IOException { - - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - - .entries(size).file(getPersistenceFile()); return ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(size).create(); - } static ChronicleMap newShmIntString() throws IOException { - - ChronicleMapBuilder.of(Integer.class, CharSequence.class).file(getPersistenceFile()); return ChronicleMapBuilder.of(Integer.class, CharSequence.class).create(); - } static ChronicleMap newShmBiBoolean() throws IOException { - - ChronicleMapBuilder.of(BI.class, Boolean.class).file(getPersistenceFile()); return ChronicleMapBuilder.of(BI.class, Boolean.class).create(); - } /** diff --git a/src/test/java/net/openhft/chronicle/map/jrs166/map/MapCheck.java b/src/test/java/net/openhft/chronicle/map/jrs166/map/MapCheck.java index 261d4d796..b97be30c2 100644 --- a/src/test/java/net/openhft/chronicle/map/jrs166/map/MapCheck.java +++ b/src/test/java/net/openhft/chronicle/map/jrs166/map/MapCheck.java @@ -123,9 +123,6 @@ public class MapCheck { static Map newMap() { try { - -// return new ConcurrentHashMap(); - ChronicleMapBuilder.of(Object.class, Object.class).file(getPersistenceFile()); return ChronicleMapBuilder.of(Object.class, Object.class).create(); } catch (Exception e) { @@ -138,7 +135,7 @@ public class MapCheck { ChronicleMap chm = (ChronicleMap) map; try { chm.close(); - } catch (IOException e) { + } catch (Exception e) { throw new RuntimeException("Can't close CHM : " + e); } } diff --git a/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java index 460a640d3..8a9191fa5 100644 --- a/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jrs166/map/StatelessChronicleMapTest.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map.jrs166.map; -import net.openhft.chronicle.hash.TcpReplicationConfig; +import net.openhft.chronicle.hash.replication.TcpConfig; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.map.ChronicleMapBuilder; import net.openhft.chronicle.map.ReadContext; @@ -33,7 +33,6 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.util.*; -import static net.openhft.chronicle.hash.StatelessBuilder.remoteAddress; import static org.junit.Assert.*; /* @@ -71,7 +70,8 @@ public class StatelessChronicleMapTest extends JSR166TestCase { for (Closeable c : closeables) { try { c.close(); - } catch (IOException e) { + } catch (Exception e) { + e.printStackTrace(); // eat the exception } } @@ -190,12 +190,13 @@ public class StatelessChronicleMapTest extends JSR166TestCase { static ChronicleMap newShmIntString(int port) throws IOException { - final ChronicleMap serverMap = ChronicleMapBuilder.of(Integer.class, String.class) - .replicators((byte) 1, TcpReplicationConfig.of(port)).create(); + final ChronicleMap serverMap = + ChronicleMapBuilder.of(Integer.class, String.class) + .replication((byte) 1, TcpConfig.of(port)).create(); final ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer .class, String.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", port))).create(); + .statelessClient(new InetSocketAddress("localhost", port)).create(); return new SingleCloseMap(statelessMap, statelessMap, serverMap); @@ -205,12 +206,12 @@ public class StatelessChronicleMapTest extends JSR166TestCase { IOException { final ChronicleMap serverMap = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .replicators((byte) 2, TcpReplicationConfig.of(port)).create(); + .replication((byte) 2, TcpConfig.of(port)).create(); final ChronicleMap statelessMap = ChronicleMapBuilder.of(CharSequence .class, CharSequence.class) - .stateless(remoteAddress(new InetSocketAddress("localhost", port))).create(); + .statelessClient(new InetSocketAddress("localhost", port)).create(); return new SingleCloseMap(statelessMap, statelessMap, serverMap); } diff --git a/src/test/java/net/openhft/chronicle/map/utility/ProcessInstanceLimiter.java b/src/test/java/net/openhft/chronicle/map/utility/ProcessInstanceLimiter.java index 4997e3c36..cecae189c 100644 --- a/src/test/java/net/openhft/chronicle/map/utility/ProcessInstanceLimiter.java +++ b/src/test/java/net/openhft/chronicle/map/utility/ProcessInstanceLimiter.java @@ -138,8 +138,7 @@ public class ProcessInstanceLimiter implements Runnable { ChronicleMapBuilder.of(String.class, Data.class); builder.entries(1000); builder.entrySize(1024); - builder.file(new File(sharedMapPath)); - this.theSharedMap = builder.create(); + this.theSharedMap = builder.createPersistedTo(new File(sharedMapPath)); Thread t = new Thread(this, "ProcessInstanceLimiter updater"); t.setDaemon(true); t.start(); diff --git a/src/test/java/net/openhft/chronicle/set/SetForTCPSocketReplicationTest3way.java b/src/test/java/net/openhft/chronicle/set/SetForTCPSocketReplicationTest3way.java index efb1d7868..ce38b7ff4 100644 --- a/src/test/java/net/openhft/chronicle/set/SetForTCPSocketReplicationTest3way.java +++ b/src/test/java/net/openhft/chronicle/set/SetForTCPSocketReplicationTest3way.java @@ -28,7 +28,7 @@ import java.net.InetSocketAddress; import static java.util.concurrent.TimeUnit.SECONDS; import static net.openhft.chronicle.map.Builder.getPersistenceFile; -import static net.openhft.chronicle.hash.TcpReplicationConfig.of; +import static net.openhft.chronicle.hash.replication.TcpConfig.of; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -51,10 +51,9 @@ public class SetForTCPSocketReplicationTest3way { final int serverPort, final InetSocketAddress... endpoints) throws IOException { return (T) ChronicleSetBuilder.of(Integer.class) - .file(getPersistenceFile()) - .replicators(identifier, of(serverPort, endpoints) + .replication(identifier, of(serverPort, endpoints) .heartBeatInterval(1L, SECONDS)) - .create(); + .createPersistedTo(getPersistenceFile()); } @@ -72,7 +71,7 @@ public class SetForTCPSocketReplicationTest3way { for (final Closeable closeable : new Closeable[]{set1, set2, set3}) { try { closeable.close(); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } }