Merge remote-tracking branch 'origin/master'

Conflicts:
	src/main/java/net/openhft/chronicle/hash/ChronicleHash.java
This commit is contained in:
Peter Lawrey
2014-11-09 14:05:22 +00:00
44 changed files with 784 additions and 581 deletions
@@ -18,9 +18,10 @@
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.SingleChronicleHashReplication;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.chronicle.hash.serialization.*;
import net.openhft.chronicle.map.*;
import net.openhft.chronicle.set.ChronicleSet;
import net.openhft.chronicle.set.ChronicleSetBuilder;
@@ -212,7 +213,7 @@ public interface ChronicleHashBuilder<K, C extends ChronicleHash,
* like iteration. If timeout expires, {@link ChronicleHashErrorListener#onLockTimeout(long)} is
* called, and then thread tries to obtain the segment lock one more time, and so in a loop,
* until thread is interrupted. However, you can configure {@linkplain #errorListener(
*ChronicleHashErrorListener) error listener} to throw an exception on the first (or n-th) lock
* ChronicleHashErrorListener) error listener} to throw an exception on the first (or n-th) lock
* acquisition fail.
*
* <p>Default lock time out is 2 seconds.
@@ -236,7 +237,7 @@ public interface ChronicleHashBuilder<K, C extends ChronicleHash,
*
* @param timeProvider a new time provider for replication needs
* @return this builder back
* @see #replication(SimpleReplication)
* @see #replication(SingleChronicleHashReplication)
*/
B timeProvider(TimeProvider timeProvider);
@@ -286,10 +287,49 @@ public interface ChronicleHashBuilder<K, C extends ChronicleHash,
*
* @param keyMarshaller the marshaller used to serialize keys
* @return this builder back
* @see #keyMarshallers(BytesWriter, BytesReader)
* @see #objectSerializer(ObjectSerializer)
*/
B keyMarshaller(@NotNull BytesMarshaller<K> keyMarshaller);
/**
* Configures the marshallers, used to serialize/deserialize keys to/from off-heap
* memory in hash containers, created by this builder. See
* <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the section about
* serialization in ChronicleMap manual</a> for more information.
*
* <p>Configuring marshalling this way results to a little bit more compact in-memory layout
* of the map, comparing to a single interface configuration:
* {@link #keyMarshaller(BytesMarshaller)}.
*
* <p>Passing {@link BytesInterop} (which is a subinterface of {@link BytesWriter}) as the
* first argument is supported, and even more advantageous from performance perspective.
*
* @param keyWriter the new key object &rarr; {@link Bytes} writer (interop) strategy
* @param keyReader the new {@link Bytes} &rarr; key object reader strategy
* @return this builder back
* @see #keyMarshaller(BytesMarshaller)
*/
B keyMarshallers(@NotNull BytesWriter<K> keyWriter, @NotNull BytesReader<K> keyReader);
/**
* Configures the marshaller used to serialize actual key sizes to off-heap memory
* in hash containers, created by this builder.
*
* <p>Default key size marshaller is so-called {@linkplain SizeMarshallers#stopBit()
* stop bit encoding marshalling}. If {@linkplain #constantKeySizeBySample(Object) constant key
* size} is configured, or defaulted if the key type is always constant and {@code
* ChronicleHashBuilder} implementation knows about it, this configuration takes no effect,
* because a special {@link SizeMarshaller} implementation, which doesn't actually do any
* marshalling, and just returns the known constant size on {@link SizeMarshaller#readSize(
* Bytes)} calls, is used instead of any {@code SizeMarshaller} configured using this method.
*
* @param keySizeMarshaller the new marshaller, used to serialize actual key sizes to off-heap
* memory
* @return this builder back
*/
B keySizeMarshaller(@NotNull SizeMarshaller keySizeMarshaller);
/**
* Configures factory which is used to create a new key instance, if key class is either
* {@link Byteable}, {@link BytesMarshallable} or {@link Externalizable} subclass in maps,
@@ -336,26 +376,26 @@ public interface ChronicleHashBuilder<K, C extends ChronicleHash,
* <p>By default, hash containers, created by this builder doesn't replicate their data.
*
* <p>This method call overrides all previous replication configurations of this builder, made
* either by this method or {@link #replication(byte, TcpConfig)} shortcut method.
* either by this method or {@link #replication(byte, TcpTransportAndNetworkConfig)} shortcut method.
*
* @param replication the replication config
* @return this builder back
* @see ChronicleHashInstanceConfig#replicated(SimpleReplication)
* @see #replication(byte, TcpConfig)
* @see ChronicleHashInstanceConfig#replicated(SingleChronicleHashReplication)
* @see #replication(byte, TcpTransportAndNetworkConfig)
*/
B replication(SimpleReplication replication);
B replication(SingleChronicleHashReplication replication);
/**
* Shortcut for {@code replication(SimpleReplication.builder()
* .tcpTransportAndNetwork(tcpTransportAndNetwork).create(identifier))}.
* .tcpTransportAndNetwork(tcpTransportAndNetwork).createWithId(identifier))}.
*
* @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
* @see #replication(SimpleReplication)
* @see ChronicleHashInstanceConfig#replicated(byte, TcpConfig)
* @see #replication(SingleChronicleHashReplication)
* @see ChronicleHashInstanceConfig#replicated(byte, TcpTransportAndNetworkConfig)
*/
B replication(byte identifier, TcpConfig tcpTransportAndNetwork);
B replication(byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork);
StatelessClientConfig<C> statelessClient(InetSocketAddress remoteAddress);
@@ -17,17 +17,17 @@
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 net.openhft.chronicle.hash.replication.SingleChronicleHashReplication;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import java.io.File;
import java.io.IOException;
public interface ChronicleHashInstanceConfig<C extends ChronicleHash> {
ChronicleHashInstanceConfig<C> replicated(byte identifier, TcpConfig tcpTransportAndNetwork);
ChronicleHashInstanceConfig<C> replicated(byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork);
ChronicleHashInstanceConfig<C> replicated(SimpleReplication replication);
ChronicleHashInstanceConfig<C> replicated(SingleChronicleHashReplication replication);
/**
* Configures replication of the hash containers, created by this builder, via so called
@@ -35,19 +35,19 @@ public interface ChronicleHashInstanceConfig<C extends ChronicleHash> {
* <a href="https://github.com/OpenHFT/Chronicle-Map#channels-and-channelprovider">the
* section about Channels and ChannelProvider in ChronicleMap manual</a> for more information.
*
* <p>Another way to establish replication is {@link #replicated(SimpleReplication)} method
* or it's shortcut: {@link #replicated(byte, TcpConfig)}.
* <p>Another way to establish replication is {@link #replicated(SingleChronicleHashReplication)} method
* or it's shortcut: {@link #replicated(byte, TcpTransportAndNetworkConfig)}.
*
* <p>By default, hash containers, created by this builder doesn't replicate their data.
*
* <p>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.
* either by means of this method, {@link #replicated(SingleChronicleHashReplication)}
* or {@link #replicated(byte, TcpTransportAndNetworkConfig)} 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)
* @see #replicated(SingleChronicleHashReplication)
*/
ChronicleHashInstanceConfig<C> replicatedViaChannel(ReplicationChannel channel);
@@ -16,19 +16,20 @@
* limitations under the License.
*/
package net.openhft.chronicle.hash.exceptions;
package net.openhft.chronicle.hash;
/**
* TODO mention in docs
* @author Rob Austin.
*/
public class TimeoutRuntimeException extends RuntimeException {
public class RemoteCallTimeoutException extends RuntimeException {
public TimeoutRuntimeException(String s) {
public RemoteCallTimeoutException(String s) {
super(s);
}
public TimeoutRuntimeException() {
public RemoteCallTimeoutException() {
super();
}
}
@@ -1,31 +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.exceptions;
import java.io.IOException;
/**
* @author Rob Austin.
*/
public class IORuntimeException extends RuntimeException{
public IORuntimeException(IOException e) {
super(e);
}
}
@@ -19,14 +19,14 @@ 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;
private final byte localIdentifier;
private final TcpTransportAndNetworkConfig tcpConfig;
private final UdpTransportConfig udpConfig;
AbstractReplication(byte localIdentifier, Builder builder) {
udpConfig = builder.udpConfig;
this.localIdentifier = localIdentifier;
tcpConfig = builder.tcpConfig;
udpConfig = builder.udpConfig;
}
public byte identifier() {
@@ -34,25 +34,25 @@ class AbstractReplication {
}
@Nullable
public TcpConfig tcpTransportAndNetwork() {
public TcpTransportAndNetworkConfig tcpTransportAndNetwork() {
return tcpConfig;
}
@Nullable
public UdpConfig udpTransport() {
public UdpTransportConfig udpTransport() {
return udpConfig;
}
static abstract class Builder<B extends Builder> {
protected UdpConfig udpConfig = null;
protected TcpConfig tcpConfig = null;
private TcpTransportAndNetworkConfig tcpConfig = null;
private UdpTransportConfig udpConfig = null;
public B tcpTransportAndNetwork(TcpConfig tcpConfig) {
public B tcpTransportAndNetwork(TcpTransportAndNetworkConfig tcpConfig) {
this.tcpConfig = tcpConfig;
return (B) this;
}
public B udpTransport(UdpConfig udpConfig) {
public B udpTransport(UdpTransportConfig udpConfig) {
this.udpConfig = udpConfig;
return (B) this;
}
@@ -62,6 +62,16 @@ public final class ReplicationHub extends AbstractReplication {
private Builder() {}
@Override
public Builder tcpTransportAndNetwork(TcpTransportAndNetworkConfig tcpConfig) {
return super.tcpTransportAndNetwork(tcpConfig);
}
@Override
public Builder udpTransport(UdpTransportConfig udpConfig) {
return super.udpTransport(udpConfig);
}
public Builder maxEntrySize(int maxEntrySize) {
this.maxEntrySize = maxEntrySize;
return this;
@@ -72,7 +82,7 @@ public final class ReplicationHub extends AbstractReplication {
return this;
}
public ReplicationHub create(byte identifier) throws IOException {
public ReplicationHub createWithId(byte identifier) throws IOException {
check();
return new ReplicationHub(identifier, this);
}
@@ -16,23 +16,32 @@
package net.openhft.chronicle.hash.replication;
//TODO better name
public final class SimpleReplication extends AbstractReplication {
public final class SingleChronicleHashReplication extends AbstractReplication {
public static Builder builder() {
return new Builder();
}
SimpleReplication(byte localIdentifier, Builder builder) {
SingleChronicleHashReplication(byte localIdentifier, Builder builder) {
super(localIdentifier, builder);
}
public static final class Builder extends AbstractReplication.Builder<Builder> {
private Builder() {}
public SimpleReplication create(byte identifier) {
public SingleChronicleHashReplication createWithId(byte identifier) {
check();
return new SimpleReplication(identifier, this);
return new SingleChronicleHashReplication(identifier, this);
}
@Override
public Builder tcpTransportAndNetwork(TcpTransportAndNetworkConfig tcpConfig) {
return super.tcpTransportAndNetwork(tcpConfig);
}
@Override
public Builder udpTransport(UdpTransportConfig udpConfig) {
return super.udpTransport(udpConfig);
}
}
}
@@ -26,7 +26,7 @@ import static java.util.Collections.unmodifiableSet;
import static java.util.concurrent.TimeUnit.SECONDS;
public final class TcpConfig {
public final class TcpTransportAndNetworkConfig {
private static final int DEFAULT_PACKET_SIZE = 1024 * 8;
private static final long DEFAULT_HEART_BEAT_INTERVAL = 20;
@@ -41,10 +41,12 @@ public final class TcpConfig {
private final TimeUnit heartBeatIntervalUnit;
private final IdentifierListener identifierListener;
private TcpConfig(int serverPort, Set<InetSocketAddress> endpoints, int packetSize,
boolean autoReconnectedUponDroppedConnection,
ThrottlingConfig throttlingConfig, long heartBeatInterval,
TimeUnit heartBeatIntervalUnit, IdentifierListener identifierListener) {
private TcpTransportAndNetworkConfig(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;
@@ -56,29 +58,30 @@ public final class TcpConfig {
}
public static TcpConfig forSendingNode(int serverPort, InetSocketAddress... endpoints) {
public static TcpTransportAndNetworkConfig forSendingNode(int serverPort,
InetSocketAddress... endpoints) {
return forSendingNode(serverPort, Arrays.asList(endpoints));
}
public static TcpConfig forSendingNode(int serverPort,
public static TcpTransportAndNetworkConfig forSendingNode(int serverPort,
Collection<InetSocketAddress> endpoints) {
if (endpoints.isEmpty())
throw new IllegalArgumentException("There should be some endpoints");
return unknownTopology(serverPort, endpoints);
return forUnknownTopology(serverPort, endpoints);
}
public static TcpConfig forReceivingOnlyNode(int serverPort) {
return unknownTopology(serverPort, Collections.<InetSocketAddress>emptyList());
public static TcpTransportAndNetworkConfig forReceivingOnlyNode(int serverPort) {
return forUnknownTopology(serverPort, Collections.<InetSocketAddress>emptyList());
}
public static TcpConfig unknownTopology(int serverPort,
Collection<InetSocketAddress> endpoints) {
public static TcpTransportAndNetworkConfig forUnknownTopology(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(
return new TcpTransportAndNetworkConfig(
serverPort, unmodifiableSet(new HashSet<InetSocketAddress>(endpoints)),
DEFAULT_PACKET_SIZE,
true, // autoReconnectedUponDroppedConnection
@@ -92,8 +95,9 @@ public final class TcpConfig {
return identifierListener;
}
public TcpConfig nonUniqueIdentifierListener(final IdentifierListener identifierListener) {
return new TcpConfig(serverPort, endpoints, packetSize,
public TcpTransportAndNetworkConfig nonUniqueIdentifierListener(
final IdentifierListener identifierListener) {
return new TcpTransportAndNetworkConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
@@ -102,8 +106,9 @@ public final class TcpConfig {
return autoReconnectedUponDroppedConnection;
}
public TcpConfig autoReconnectedUponDroppedConnection(boolean autoReconnectedUponDroppedConnection) {
return new TcpConfig(serverPort, endpoints, packetSize,
public TcpTransportAndNetworkConfig autoReconnectedUponDroppedConnection(
boolean autoReconnectedUponDroppedConnection) {
return new TcpTransportAndNetworkConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
@@ -112,9 +117,9 @@ public final class TcpConfig {
return throttlingConfig;
}
public TcpConfig throttlingConfig(ThrottlingConfig throttlingConfig) {
public TcpTransportAndNetworkConfig throttlingConfig(ThrottlingConfig throttlingConfig) {
ThrottlingConfig.checkMillisecondBucketInterval(throttlingConfig, "TCP");
return new TcpConfig(serverPort, endpoints, packetSize,
return new TcpTransportAndNetworkConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
@@ -127,8 +132,8 @@ public final class TcpConfig {
return serverPort;
}
public TcpConfig serverPort(int serverPort) {
return new TcpConfig(serverPort, endpoints, packetSize,
public TcpTransportAndNetworkConfig serverPort(int serverPort) {
return new TcpTransportAndNetworkConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
@@ -137,13 +142,13 @@ public final class TcpConfig {
return endpoints;
}
public TcpConfig endpoints(Set<InetSocketAddress> endpoints) {
public TcpTransportAndNetworkConfig 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");
}
return new TcpConfig(serverPort, endpoints, packetSize,
return new TcpTransportAndNetworkConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
@@ -152,16 +157,17 @@ public final class TcpConfig {
return packetSize;
}
public TcpConfig packetSize(int packetSize) {
public TcpTransportAndNetworkConfig packetSize(int packetSize) {
if (packetSize <= 0)
throw new IllegalArgumentException();
return new TcpConfig(serverPort, endpoints, packetSize,
return new TcpTransportAndNetworkConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
public TcpConfig heartBeatInterval(long heartBeatInterval, TimeUnit heartBeatIntervalUnit) {
return new TcpConfig(serverPort, endpoints, packetSize,
public TcpTransportAndNetworkConfig heartBeatInterval(long heartBeatInterval,
TimeUnit heartBeatIntervalUnit) {
return new TcpTransportAndNetworkConfig(serverPort, endpoints, packetSize,
autoReconnectedUponDroppedConnection, throttlingConfig, heartBeatInterval,
heartBeatIntervalUnit, identifierListener);
}
@@ -172,7 +178,7 @@ public final class TcpConfig {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TcpConfig that = (TcpConfig) o;
TcpTransportAndNetworkConfig that = (TcpTransportAndNetworkConfig) o;
if (autoReconnectedUponDroppedConnection != that.autoReconnectedUponDroppedConnection) return false;
if (heartBeatInterval != that.heartBeatInterval) return false;
@@ -25,16 +25,16 @@ import java.net.InetAddress;
import java.net.NetworkInterface;
public final class UdpConfig {
public final class UdpTransportConfig {
private final InetAddress address;
private final int port;
private final NetworkInterface networkInterface;
private final @Nullable NetworkInterface networkInterface;
private final ThrottlingConfig throttlingConfig;
private UdpConfig(
private UdpTransportConfig(
InetAddress address,
int port,
NetworkInterface networkInterface,
@Nullable NetworkInterface networkInterface,
ThrottlingConfig throttlingConfig) {
if (address == null) {
throw new NullPointerException("Null address");
@@ -49,23 +49,16 @@ public final class UdpConfig {
this.throttlingConfig = throttlingConfig;
}
// TODO better name
public static UdpConfig simple(@NotNull InetAddress address, int port) {
public static UdpTransportConfig of(@NotNull InetAddress address, int port) {
if (address.isMulticastAddress())
throw new IllegalArgumentException();
return create(address, port, null, ThrottlingConfig.noThrottling());
}
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 UdpConfig create(InetAddress address, int port, NetworkInterface networkInterface,
ThrottlingConfig throttlingConfig) {
return new UdpConfig(address, port, networkInterface, throttlingConfig);
static UdpTransportConfig create(InetAddress address, int port,
@Nullable NetworkInterface networkInterface,
ThrottlingConfig throttlingConfig) {
return new UdpTransportConfig(address, port, networkInterface, throttlingConfig);
}
@NotNull
@@ -102,8 +95,8 @@ public final class UdpConfig {
if (o == this) {
return true;
}
if (o instanceof UdpConfig) {
UdpConfig that = (UdpConfig) o;
if (o instanceof UdpTransportConfig) {
UdpTransportConfig that = (UdpTransportConfig) o;
return (this.address.equals(that.address()))
&& (this.port == that.port())
&& (this.networkInterface.equals(that.networkInterface()))
@@ -126,7 +119,14 @@ public final class UdpConfig {
return h;
}
public UdpConfig throttlingConfig(@NotNull ThrottlingConfig throttlingConfig) {
public UdpTransportConfig networkInterfaceForMulticast(
@Nullable NetworkInterface networkInterface) {
if (!address().isMulticastAddress())
throw new IllegalArgumentException();
return create(address(), port(), networkInterface, throttlingConfig());
}
public UdpTransportConfig throttlingConfig(@NotNull ThrottlingConfig throttlingConfig) {
ThrottlingConfig.checkMillisecondBucketInterval(throttlingConfig, "UDP");
return create(address(), port(), networkInterface(), throttlingConfig);
}