Made TcpConfig back immutable; Draw the borderline: Builders are mutable, Configs are immutable (hence ChronicleHashInstanceBuilder and StatelessClientBuilder renamed to Configs; improved TcpConfig static factory naming

This commit is contained in:
leventov
2014-11-07 12:24:52 +03:00
parent 4a51b129a3
commit 4c86dae04f
29 changed files with 198 additions and 215 deletions
@@ -340,7 +340,7 @@ public interface ChronicleHashBuilder<K, C extends ChronicleHash,
*
* @param replication the replication config
* @return this builder back
* @see ChronicleHashInstanceBuilder#replicated(SimpleReplication)
* @see ChronicleHashInstanceConfig#replicated(SimpleReplication)
* @see #replication(byte, TcpConfig)
*/
B replication(SimpleReplication replication);
@@ -353,13 +353,13 @@ public interface ChronicleHashBuilder<K, C extends ChronicleHash,
* @param tcpTransportAndNetwork configuration of tcp connection and network
* @return this builder back
* @see #replication(SimpleReplication)
* @see ChronicleHashInstanceBuilder#replicated(byte, TcpConfig)
* @see ChronicleHashInstanceConfig#replicated(byte, TcpConfig)
*/
B replication(byte identifier, TcpConfig tcpTransportAndNetwork);
StatelessClientBuilder<C> statelessClient(InetSocketAddress remoteAddress);
StatelessClientConfig<C> statelessClient(InetSocketAddress remoteAddress);
ChronicleHashInstanceBuilder<C> instance();
ChronicleHashInstanceConfig<C> instance();
/**
* Creates a new hash container, storing it's data in off-heap memory, not mapped to any file.
@@ -401,7 +401,7 @@ public interface ChronicleHashBuilder<K, C extends ChronicleHash,
* @see ChronicleHash#file()
* @see ChronicleHash#close()
* @see #create()
* @see ChronicleHashInstanceBuilder#persistedTo(File)
* @see ChronicleHashInstanceConfig#persistedTo(File)
*/
C createPersistedTo(File file) throws IOException;
@@ -23,11 +23,11 @@ import net.openhft.chronicle.hash.replication.TcpConfig;
import java.io.File;
import java.io.IOException;
public interface ChronicleHashInstanceBuilder<C extends ChronicleHash> {
public interface ChronicleHashInstanceConfig<C extends ChronicleHash> {
ChronicleHashInstanceBuilder<C> replicated(byte identifier, TcpConfig tcpTransportAndNetwork);
ChronicleHashInstanceConfig<C> replicated(byte identifier, TcpConfig tcpTransportAndNetwork);
ChronicleHashInstanceBuilder<C> replicated(SimpleReplication replication);
ChronicleHashInstanceConfig<C> replicated(SimpleReplication replication);
/**
* Configures replication of the hash containers, created by this builder, via so called
@@ -49,9 +49,9 @@ public interface ChronicleHashInstanceBuilder<C extends ChronicleHash> {
* @return this builder object back
* @see #replicated(SimpleReplication)
*/
ChronicleHashInstanceBuilder<C> replicatedViaChannel(ReplicationChannel channel);
ChronicleHashInstanceConfig<C> replicatedViaChannel(ReplicationChannel channel);
ChronicleHashInstanceBuilder<C> persistedTo(File file);
ChronicleHashInstanceConfig<C> persistedTo(File file);
C create() throws IOException;
}
@@ -1,27 +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;
/**
* @author Rob Austin.
*/
public interface ReplicationConfig<B> {
}
@@ -38,8 +38,8 @@ import java.util.concurrent.TimeUnit;
* @param <C> the type of {@code ChronicleHash} accessed remotely, {@code ChronicleMap} or
* {@code ChronicleSet}
*/
public interface StatelessClientBuilder<C extends ChronicleHash> {
StatelessClientBuilder<C> timeout(long timeout, TimeUnit units);
public interface StatelessClientConfig<C extends ChronicleHash> {
StatelessClientConfig<C> timeout(long timeout, TimeUnit units);
C create() throws IOException;
}
@@ -16,8 +16,6 @@
package net.openhft.chronicle.hash.replication;
import net.openhft.chronicle.hash.ReplicationConfig;
import java.io.IOException;
public final class ReplicationHub extends AbstractReplication {
@@ -16,6 +16,7 @@
package net.openhft.chronicle.hash.replication;
//TODO better name
public final class SimpleReplication extends AbstractReplication {
public static Builder builder() {
@@ -18,48 +18,84 @@
package net.openhft.chronicle.hash.replication;
import net.openhft.chronicle.hash.ReplicationConfig;
import org.jetbrains.annotations.NotNull;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.concurrent.TimeUnit;
import static java.util.Collections.unmodifiableSet;
import static java.util.concurrent.TimeUnit.SECONDS;
public class TcpConfig implements ReplicationConfig {
public final class TcpConfig {
private static final int DEFAULT_PACKET_SIZE = 1024 * 8;
private static final long DEFAULT_HEART_BEAT_INTERVAL = 20;
private static final TimeUnit DEFAULT_HEART_BEAT_INTERVAL_UNIT = SECONDS;
private int serverPort = DEFAULT_PACKET_SIZE;
private Set<InetSocketAddress> endpoints;
private int packetSize = DEFAULT_PACKET_SIZE;
private boolean autoReconnectedUponDroppedConnection = true;
private ThrottlingConfig throttlingConfig = ThrottlingConfig.noThrottling();
private long heartBeatInterval = DEFAULT_HEART_BEAT_INTERVAL;
private TimeUnit heartBeatIntervalUnit = DEFAULT_HEART_BEAT_INTERVAL_UNIT;
private IdentifierListener identifierListener;
private final int serverPort;
private final Set<InetSocketAddress> endpoints;
private final int packetSize;
private final boolean autoReconnectedUponDroppedConnection;
private final ThrottlingConfig throttlingConfig;
private final long heartBeatInterval;
private final TimeUnit heartBeatIntervalUnit;
private final IdentifierListener identifierListener;
TcpConfig() {
private TcpConfig(int serverPort, Set<InetSocketAddress> endpoints, int packetSize,
boolean autoReconnectedUponDroppedConnection,
ThrottlingConfig throttlingConfig, long heartBeatInterval,
TimeUnit heartBeatIntervalUnit, IdentifierListener identifierListener) {
this.serverPort = serverPort;
this.endpoints = endpoints;
this.packetSize = packetSize;
this.autoReconnectedUponDroppedConnection = autoReconnectedUponDroppedConnection;
this.throttlingConfig = throttlingConfig;
this.heartBeatInterval = heartBeatInterval;
this.heartBeatIntervalUnit = heartBeatIntervalUnit;
this.identifierListener = identifierListener;
}
public static TcpConfig forSendingNode(int serverPort, InetSocketAddress... endpoints) {
return forSendingNode(serverPort, Arrays.asList(endpoints));
}
public static TcpConfig forSendingNode(int serverPort,
Collection<InetSocketAddress> endpoints) {
if (endpoints.isEmpty())
throw new IllegalArgumentException("There should be some endpoints");
return unknownTopology(serverPort, endpoints);
}
public static TcpConfig forReceivingOnlyNode(int serverPort) {
return unknownTopology(serverPort, Collections.<InetSocketAddress>emptyList());
}
public static TcpConfig unknownTopology(int serverPort,
Collection<InetSocketAddress> endpoints) {
for (final InetSocketAddress endpoint : endpoints) {
if (endpoint.getPort() == serverPort && "localhost".equals(endpoint.getHostName()))
throw new IllegalArgumentException("endpoint=" + endpoint
+ " can not point to the same port as the server");
}
return new TcpConfig(
serverPort, unmodifiableSet(new HashSet<InetSocketAddress>(endpoints)),
DEFAULT_PACKET_SIZE,
true, // autoReconnectedUponDroppedConnection
ThrottlingConfig.noThrottling(),
DEFAULT_HEART_BEAT_INTERVAL,
DEFAULT_HEART_BEAT_INTERVAL_UNIT,
null); // identifierListener
}
public IdentifierListener nonUniqueIdentifierListener() {
return identifierListener;
}
public TcpConfig nonUniqueIdentifierListener(@NotNull final IdentifierListener
identifierListener) {
this.identifierListener = identifierListener;
return this;
public TcpConfig nonUniqueIdentifierListener(final IdentifierListener identifierListener) {
return new TcpConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
public boolean autoReconnectedUponDroppedConnection() {
@@ -67,17 +103,20 @@ public class TcpConfig implements ReplicationConfig {
}
public TcpConfig autoReconnectedUponDroppedConnection(boolean autoReconnectedUponDroppedConnection) {
this.autoReconnectedUponDroppedConnection = autoReconnectedUponDroppedConnection;
return this;
return new TcpConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
public ThrottlingConfig throttlingConfig() {
public ThrottlingConfig throttlingConfig() {
return throttlingConfig;
}
public TcpConfig throttlingConfig(ThrottlingConfig throttlingConfig) {
this.throttlingConfig = throttlingConfig;
return this;
ThrottlingConfig.checkMillisecondBucketInterval(throttlingConfig, "TCP");
return new TcpConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
public long heartBeatInterval(TimeUnit unit) {
@@ -89,8 +128,9 @@ public class TcpConfig implements ReplicationConfig {
}
public TcpConfig serverPort(int serverPort) {
this.serverPort = serverPort;
return this;
return new TcpConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
public Set<InetSocketAddress> endpoints() {
@@ -98,58 +138,32 @@ public class TcpConfig implements ReplicationConfig {
}
public TcpConfig endpoints(Set<InetSocketAddress> endpoints) {
for (final InetSocketAddress endpoint : endpoints) {
if (endpoint.getPort() == serverPort && "localhost".equals(endpoint.getHostName()))
throw new IllegalArgumentException("endpoint=" + endpoint
+ " can not point to the same port as the server");
}
this.endpoints = endpoints;
return this;
return new TcpConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
public int packetSize() {
return packetSize;
}
public static TcpConfig of(int serverPort, InetSocketAddress... endpoints) {
return of(serverPort, Arrays.asList(endpoints));
}
public static TcpConfig of(int serverPort, Collection<InetSocketAddress> endpoints) {
// at least in tests, we configure "receive-only" replication without endpoints.
// TODO decide what to do with this case
// if (endpoints.isEmpty())
// throw new IllegalArgumentException("There should be some endpoints");
for (final InetSocketAddress endpoint : endpoints) {
if (endpoint.getPort() == serverPort && "localhost".equals(endpoint.getHostName()))
throw new IllegalArgumentException("endpoint=" + endpoint
+ " can not point to the same port as the server");
}
TcpConfig tcpConfig = new TcpConfig();
tcpConfig.serverPort(serverPort);
tcpConfig.endpoints = unmodifiableSet(new HashSet<InetSocketAddress>(endpoints));
return tcpConfig;
}
public TcpConfig packetSize(int packetSize) {
if (packetSize <= 0)
throw new IllegalArgumentException();
this.packetSize = packetSize;
return this;
return new TcpConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
public TcpConfig throttlingConfig(long heartBeatInterval, TimeUnit heartBeatIntervalUnit) {
this.heartBeatInterval = heartBeatInterval;
this.heartBeatIntervalUnit = heartBeatIntervalUnit;
return this;
public TcpConfig heartBeatInterval(long heartBeatInterval, TimeUnit heartBeatIntervalUnit) {
return new TcpConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
@@ -196,12 +210,4 @@ public class TcpConfig implements ReplicationConfig {
", heartBeatIntervalUnit=" + heartBeatIntervalUnit +
'}';
}
public TcpConfig heartBeatInterval(long time, TimeUnit unit) {
this.heartBeatInterval = time;
this.heartBeatIntervalUnit = unit;
return this;
}
}
@@ -23,7 +23,6 @@ import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
public class ThrottlingConfig {
private static final long DEFAULT_BUCKET_INTERVAL = 100L;
@@ -79,7 +78,7 @@ public class ThrottlingConfig {
return create(maxBits, perUnit, DEFAULT_BUCKET_INTERVAL, DEFAULT_BUCKET_INTERVAL_UNIT);
}
public static void checkMillisecondBucketInterval(ThrottlingConfig config, String replicator) {
static void checkMillisecondBucketInterval(ThrottlingConfig config, String replicator) {
if (config.bucketInterval(MILLISECONDS) < 1) {
throw new IllegalArgumentException(
"Minimum throttle bucketing interval of 1 millisecond is supported by " +
@@ -18,7 +18,6 @@
package net.openhft.chronicle.hash.replication;
import net.openhft.chronicle.hash.ReplicationConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -26,7 +25,7 @@ import java.net.InetAddress;
import java.net.NetworkInterface;
public final class UdpConfig implements ReplicationConfig {
public final class UdpConfig {
private final InetAddress address;
private final int port;
private final NetworkInterface networkInterface;
@@ -50,6 +49,7 @@ public final class UdpConfig implements ReplicationConfig {
this.throttlingConfig = throttlingConfig;
}
// TODO better name
public static UdpConfig simple(@NotNull InetAddress address, int port) {
if (address.isMulticastAddress())
throw new IllegalArgumentException();