Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions src/main/java/com/basho/riak/client/api/RiakClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.basho.riak.client.core.RiakNode;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -151,6 +151,7 @@
* </ul>
* @author Dave Rusek <drusek at basho dot com>
* @author Brian Roach <roach at basho dot com>
* @author Sergey Galkin <srggal at gmail dot com>
* @since 2.0
*/
public class RiakClient
Expand Down Expand Up @@ -240,13 +241,9 @@ public static RiakClient newClient(String... remoteAddresses) throws UnknownHost
*/
public static RiakClient newClient(int port, List<String> remoteAddresses) throws UnknownHostException
{
RiakNode.Builder builder = new RiakNode.Builder()
.withRemotePort(port)
.withMinConnections(10);
List<RiakNode> nodes = RiakNode.Builder.buildNodes(builder, remoteAddresses);
RiakCluster cluster = new RiakCluster.Builder(nodes).build();
cluster.start();
return new RiakClient(cluster);
RiakNode.Builder builder = createDefaultNodeBuilder()
.withRemotePort(port);
return newClient(builder, remoteAddresses);
}

/**
Expand All @@ -258,19 +255,57 @@ public static RiakClient newClient(int port, List<String> remoteAddresses) throw
*/
public static RiakClient newClient(InetSocketAddress... addresses) throws UnknownHostException
{
RiakNode.Builder builder = new RiakNode.Builder().withMinConnections(10);
List<RiakNode> nodes = new LinkedList<RiakNode>();
final List<String> remoteAddresses = new ArrayList<String>(addresses.length);

for (InetSocketAddress addy : addresses)
{
builder.withRemoteAddress(addy.getHostName())
.withRemotePort(addy.getPort());
nodes.add(builder.build());
remoteAddresses.add(
String.format("%s:%s", addy.getHostName(), addy.getPort())
);
}
RiakCluster cluster = new RiakCluster.Builder(nodes).build();

return newClient(createDefaultNodeBuilder(), remoteAddresses);
}

/**
* Static factory method to create a new client instance.
* This method produces a client connected to the supplied addresses and containing the {@link RiakNode}s
* that will be build by using provided builder.
* @param addresses one or more addresses to connect to.
* @return a new RiakClient instance.
* @throws java.net.UnknownHostException if a supplied hostname cannot be resolved.
* @since 2.0.3
* @see com.basho.riak.client.core.RiakCluster.Builder#Builder(RiakNode.Builder, List)
*/
public static RiakClient newClient(RiakNode.Builder nodeBuilder, List<String> addresses) throws UnknownHostException
{
final RiakCluster cluster = new RiakCluster.Builder(nodeBuilder, addresses).build();
cluster.start();

return new RiakClient(cluster);
}


/**
* Static factory method to create a new client instance.
*
* @since 2.0.3
* @see #newClient(RiakNode.Builder, List)
*/
public static RiakClient newClient(RiakNode.Builder nodeBuilder, String... addresses) throws UnknownHostException
{
return newClient(nodeBuilder, Arrays.asList(addresses));
}

/**
*
* @since 2.0.3
*/
public static RiakNode.Builder createDefaultNodeBuilder()
{
return new RiakNode.Builder()
.withMinConnections(10);
}

/**
* Execute a RiakCommand synchronously.
* <p>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/basho/riak/client/core/RiakCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* </p>
*
* @author Brian Roach <roach at basho dot com>
* @author Sergey Galkin <srggal at gmail dot com>
* @since 2.0
*/
public class RiakCluster implements OperationRetrier, NodeStateListener
Expand Down Expand Up @@ -652,6 +653,28 @@ public Builder(List<RiakNode> riakNodes)
this.riakNodes = new ArrayList<RiakNode>(riakNodes);
}

/**
* Instantiate a Builder containing the {@link RiakNode}s that will be build by using provided builder.
* The RiakNode.Builder is used for setting common properties among the nodes.
* @since 2.0.3
* @see com.basho.riak.client.core.RiakNode.Builder#buildNodes(RiakNode.Builder, List)
*/
public Builder(RiakNode.Builder nodeBuilder, List<String> remoteAddresses) throws UnknownHostException
{
riakNodes = RiakNode.Builder.buildNodes(nodeBuilder, remoteAddresses );
}

/**
* Instantiate a Builder containing the {@link RiakNode}s that will be build by using provided builder.
* The RiakNode.Builder is used for setting common properties among the nodes.
* @since 2.0.3
* @see com.basho.riak.client.core.RiakNode.Builder#buildNodes(RiakNode.Builder, String...)
*/
public Builder(RiakNode.Builder nodeBuilder, String... remoteAddresses) throws UnknownHostException
{
riakNodes = RiakNode.Builder.buildNodes(nodeBuilder, remoteAddresses );
}

/**
* Instantiate a Builder containing a single {@link RiakNode}
* @param node
Expand Down
55 changes: 47 additions & 8 deletions src/main/java/com/basho/riak/client/core/RiakNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.basho.riak.client.core.netty.*;
import com.basho.riak.client.core.util.Constants;
import com.basho.riak.client.core.util.HostAndPort;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
Expand All @@ -42,6 +43,7 @@

/**
* @author Brian Roach <roach at basho dot com>
* @author Sergey Galkin <srggal at gmail dot com>
* @since 2.0
*/
public class RiakNode implements RiakResponseListener
Expand Down Expand Up @@ -1263,13 +1265,13 @@ public Builder()
/**
* Sets the remote address for this RiakNode.
*
* @param remoteAddress Can either be a FQDN or IP address
* @param remoteAddress Can either be a FQDN or IP address. Since 2.0.3 it may contain port delimited by ':'.
* @return this
* @see #DEFAULT_REMOTE_ADDRESS
*/
public Builder withRemoteAddress(String remoteAddress)
{
this.remoteAddress = remoteAddress;
withRemoteAddress(HostAndPort.fromString(remoteAddress, this.port));
return this;
}

Expand All @@ -1286,6 +1288,23 @@ public Builder withRemotePort(int port)
return this;
}

/**
* Specifies the remote host and remote port for this RiakNode.
*
* @param hp - host and port
* @return this
* @see #DEFAULT_REMOTE_PORT
*
* @since 2.0.3
* @see HostAndPort
*/
public Builder withRemoteAddress(HostAndPort hp)
{
this.port = hp.getPortOrDefault(DEFAULT_REMOTE_PORT);
this.remoteAddress = hp.getHost();
return this;
}

/**
* Set the minimum number of active connections to maintain.
* These connections are exempt from the idle timeout.
Expand Down Expand Up @@ -1483,27 +1502,47 @@ public RiakNode build() throws UnknownHostException
return new RiakNode(this);
}


/**
* Build a set of RiakNodes.
* The provided builder will be used to construct a set of RiakNodes
* using the supplied addresses.
*
* @param builder a configured builder
* @param remoteAddresses a list of IP addresses or FQDN
* @param builder a configured builder, used for common properties among the nodes
* @param remoteAddresses a list of IP addresses or FQDN.
* Since 2.0.3 each of list item is treated as a comma separated list
* of FQDN or IP addresses with the optional remote port delimited by ':'.
* @return a list of constructed RiakNodes
* @throws UnknownHostException if a supplied FQDN can not be resolved.
*/
public static List<RiakNode> buildNodes(Builder builder, List<String> remoteAddresses)
throws UnknownHostException
{
List<RiakNode> nodes = new ArrayList<RiakNode>(remoteAddresses.size());
for (String remoteAddress : remoteAddresses)
final Set<HostAndPort> hps = new HashSet<HostAndPort>();
for (String remoteAddress: remoteAddresses)
{
builder.withRemoteAddress(remoteAddress);
hps.addAll( HostAndPort.hostsFromString(remoteAddress, builder.port) );
}

final List<RiakNode> nodes = new ArrayList<RiakNode>(hps.size());
for (HostAndPort hp : hps)
{
builder.withRemoteAddress(hp);
nodes.add(builder.build());
}
return nodes;
}

/**
* Build a set of RiakNodes.
* The provided builder will be used to construct a set of RiakNodes using the supplied addresses.
*
* @see #buildNodes(Builder, List)
* @since 2.0.3
*/
public static List<RiakNode> buildNodes(Builder builder, String... remoteAddresses)
throws UnknownHostException
{
return buildNodes(builder, Arrays.asList(remoteAddresses));
}
}
}
Loading