Use bytes instead of a string for IP address.

Ported from internal.
This commit is contained in:
Kun Zhang 2016-03-10 14:17:52 -08:00
parent 9913d1d0fc
commit 016d4a5e22
6 changed files with 69 additions and 130 deletions

View File

@ -72,7 +72,7 @@ public final class LoadBalancerProto {
"sponse_type\"i\n\nServerList\022#\n\007servers\030\001 \003" + "sponse_type\"i\n\nServerList\022#\n\007servers\030\001 \003" +
"(\0132\022.grpc.lb.v1.Server\0226\n\023expiration_int" + "(\0132\022.grpc.lb.v1.Server\0226\n\023expiration_int" +
"erval\030\003 \001(\0132\031.google.protobuf.Duration\"\\", "erval\030\003 \001(\0132\031.google.protobuf.Duration\"\\",
"\n\006Server\022\022\n\nip_address\030\001 \001(\t\022\014\n\004port\030\002 \001" + "\n\006Server\022\022\n\nip_address\030\001 \001(\014\022\014\n\004port\030\002 \001" +
"(\005\022\032\n\022load_balance_token\030\003 \001(\t\022\024\n\014drop_r" + "(\005\022\032\n\022load_balance_token\030\003 \001(\t\022\024\n\014drop_r" +
"equest\030\004 \001(\0102b\n\014LoadBalancer\022R\n\013BalanceL" + "equest\030\004 \001(\0102b\n\014LoadBalancer\022R\n\013BalanceL" +
"oad\022\036.grpc.lb.v1.LoadBalanceRequest\032\037.gr" + "oad\022\036.grpc.lb.v1.LoadBalanceRequest\032\037.gr" +

View File

@ -15,7 +15,7 @@ public final class Server extends
super(builder); super(builder);
} }
private Server() { private Server() {
ipAddress_ = ""; ipAddress_ = com.google.protobuf.ByteString.EMPTY;
port_ = 0; port_ = 0;
loadBalanceToken_ = ""; loadBalanceToken_ = "";
dropRequest_ = false; dropRequest_ = false;
@ -46,9 +46,8 @@ public final class Server extends
break; break;
} }
case 10: { case 10: {
java.lang.String s = input.readStringRequireUtf8();
ipAddress_ = s; ipAddress_ = input.readBytes();
break; break;
} }
case 16: { case 16: {
@ -92,53 +91,27 @@ public final class Server extends
} }
public static final int IP_ADDRESS_FIELD_NUMBER = 1; public static final int IP_ADDRESS_FIELD_NUMBER = 1;
private volatile java.lang.Object ipAddress_; private com.google.protobuf.ByteString ipAddress_;
/** /**
* <code>optional string ip_address = 1;</code> * <code>optional bytes ip_address = 1;</code>
* *
* <pre> * <pre>
* A resolved address and port for the server. The IP address string may * A resolved address for the server, serialized in network-byte-order. It may
* either be an IPv4 or IPv6 address. * either be an IPv4 or IPv6 address.
* </pre> * </pre>
*/ */
public java.lang.String getIpAddress() { public com.google.protobuf.ByteString getIpAddress() {
java.lang.Object ref = ipAddress_; return ipAddress_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
ipAddress_ = s;
return s;
}
}
/**
* <code>optional string ip_address = 1;</code>
*
* <pre>
* A resolved address and port for the server. The IP address string may
* either be an IPv4 or IPv6 address.
* </pre>
*/
public com.google.protobuf.ByteString
getIpAddressBytes() {
java.lang.Object ref = ipAddress_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
ipAddress_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
} }
public static final int PORT_FIELD_NUMBER = 2; public static final int PORT_FIELD_NUMBER = 2;
private int port_; private int port_;
/** /**
* <code>optional int32 port = 2;</code> * <code>optional int32 port = 2;</code>
*
* <pre>
* A resolved port number for the server.
* </pre>
*/ */
public int getPort() { public int getPort() {
return port_; return port_;
@ -216,8 +189,8 @@ public final class Server extends
public void writeTo(com.google.protobuf.CodedOutputStream output) public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException { throws java.io.IOException {
if (!getIpAddressBytes().isEmpty()) { if (!ipAddress_.isEmpty()) {
com.google.protobuf.GeneratedMessage.writeString(output, 1, ipAddress_); output.writeBytes(1, ipAddress_);
} }
if (port_ != 0) { if (port_ != 0) {
output.writeInt32(2, port_); output.writeInt32(2, port_);
@ -235,8 +208,9 @@ public final class Server extends
if (size != -1) return size; if (size != -1) return size;
size = 0; size = 0;
if (!getIpAddressBytes().isEmpty()) { if (!ipAddress_.isEmpty()) {
size += com.google.protobuf.GeneratedMessage.computeStringSize(1, ipAddress_); size += com.google.protobuf.CodedOutputStream
.computeBytesSize(1, ipAddress_);
} }
if (port_ != 0) { if (port_ != 0) {
size += com.google.protobuf.CodedOutputStream size += com.google.protobuf.CodedOutputStream
@ -360,7 +334,7 @@ public final class Server extends
} }
public Builder clear() { public Builder clear() {
super.clear(); super.clear();
ipAddress_ = ""; ipAddress_ = com.google.protobuf.ByteString.EMPTY;
port_ = 0; port_ = 0;
@ -409,9 +383,8 @@ public final class Server extends
public Builder mergeFrom(io.grpc.grpclb.Server other) { public Builder mergeFrom(io.grpc.grpclb.Server other) {
if (other == io.grpc.grpclb.Server.getDefaultInstance()) return this; if (other == io.grpc.grpclb.Server.getDefaultInstance()) return this;
if (!other.getIpAddress().isEmpty()) { if (other.getIpAddress() != com.google.protobuf.ByteString.EMPTY) {
ipAddress_ = other.ipAddress_; setIpAddress(other.getIpAddress());
onChanged();
} }
if (other.getPort() != 0) { if (other.getPort() != 0) {
setPort(other.getPort()); setPort(other.getPort());
@ -449,58 +422,27 @@ public final class Server extends
return this; return this;
} }
private java.lang.Object ipAddress_ = ""; private com.google.protobuf.ByteString ipAddress_ = com.google.protobuf.ByteString.EMPTY;
/** /**
* <code>optional string ip_address = 1;</code> * <code>optional bytes ip_address = 1;</code>
* *
* <pre> * <pre>
* A resolved address and port for the server. The IP address string may * A resolved address for the server, serialized in network-byte-order. It may
* either be an IPv4 or IPv6 address. * either be an IPv4 or IPv6 address.
* </pre> * </pre>
*/ */
public java.lang.String getIpAddress() { public com.google.protobuf.ByteString getIpAddress() {
java.lang.Object ref = ipAddress_; return ipAddress_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
ipAddress_ = s;
return s;
} else {
return (java.lang.String) ref;
}
} }
/** /**
* <code>optional string ip_address = 1;</code> * <code>optional bytes ip_address = 1;</code>
* *
* <pre> * <pre>
* A resolved address and port for the server. The IP address string may * A resolved address for the server, serialized in network-byte-order. It may
* either be an IPv4 or IPv6 address. * either be an IPv4 or IPv6 address.
* </pre> * </pre>
*/ */
public com.google.protobuf.ByteString public Builder setIpAddress(com.google.protobuf.ByteString value) {
getIpAddressBytes() {
java.lang.Object ref = ipAddress_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
ipAddress_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>optional string ip_address = 1;</code>
*
* <pre>
* A resolved address and port for the server. The IP address string may
* either be an IPv4 or IPv6 address.
* </pre>
*/
public Builder setIpAddress(
java.lang.String value) {
if (value == null) { if (value == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
@ -510,10 +452,10 @@ public final class Server extends
return this; return this;
} }
/** /**
* <code>optional string ip_address = 1;</code> * <code>optional bytes ip_address = 1;</code>
* *
* <pre> * <pre>
* A resolved address and port for the server. The IP address string may * A resolved address for the server, serialized in network-byte-order. It may
* either be an IPv4 or IPv6 address. * either be an IPv4 or IPv6 address.
* </pre> * </pre>
*/ */
@ -523,35 +465,24 @@ public final class Server extends
onChanged(); onChanged();
return this; return this;
} }
/**
* <code>optional string ip_address = 1;</code>
*
* <pre>
* A resolved address and port for the server. The IP address string may
* either be an IPv4 or IPv6 address.
* </pre>
*/
public Builder setIpAddressBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
checkByteStringIsUtf8(value);
ipAddress_ = value;
onChanged();
return this;
}
private int port_ ; private int port_ ;
/** /**
* <code>optional int32 port = 2;</code> * <code>optional int32 port = 2;</code>
*
* <pre>
* A resolved port number for the server.
* </pre>
*/ */
public int getPort() { public int getPort() {
return port_; return port_;
} }
/** /**
* <code>optional int32 port = 2;</code> * <code>optional int32 port = 2;</code>
*
* <pre>
* A resolved port number for the server.
* </pre>
*/ */
public Builder setPort(int value) { public Builder setPort(int value) {
@ -561,6 +492,10 @@ public final class Server extends
} }
/** /**
* <code>optional int32 port = 2;</code> * <code>optional int32 port = 2;</code>
*
* <pre>
* A resolved port number for the server.
* </pre>
*/ */
public Builder clearPort() { public Builder clearPort() {

View File

@ -8,27 +8,21 @@ public interface ServerOrBuilder extends
com.google.protobuf.MessageOrBuilder { com.google.protobuf.MessageOrBuilder {
/** /**
* <code>optional string ip_address = 1;</code> * <code>optional bytes ip_address = 1;</code>
* *
* <pre> * <pre>
* A resolved address and port for the server. The IP address string may * A resolved address for the server, serialized in network-byte-order. It may
* either be an IPv4 or IPv6 address. * either be an IPv4 or IPv6 address.
* </pre> * </pre>
*/ */
java.lang.String getIpAddress(); com.google.protobuf.ByteString getIpAddress();
/**
* <code>optional string ip_address = 1;</code>
*
* <pre>
* A resolved address and port for the server. The IP address string may
* either be an IPv4 or IPv6 address.
* </pre>
*/
com.google.protobuf.ByteString
getIpAddressBytes();
/** /**
* <code>optional int32 port = 2;</code> * <code>optional int32 port = 2;</code>
*
* <pre>
* A resolved port number for the server.
* </pre>
*/ */
int getPort(); int getPort();

View File

@ -35,6 +35,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import io.grpc.Attributes; import io.grpc.Attributes;
import io.grpc.Channel; import io.grpc.Channel;
@ -49,8 +50,10 @@ import io.grpc.internal.GrpcUtil;
import io.grpc.internal.SharedResourceHolder; import io.grpc.internal.SharedResourceHolder;
import io.grpc.stub.StreamObserver; import io.grpc.stub.StreamObserver;
import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -285,14 +288,18 @@ class GrpclbLoadBalancer<T> extends LoadBalancer<T> {
if (server.getDropRequest()) { if (server.getDropRequest()) {
listBuilder.add(null); listBuilder.add(null);
} else { } else {
try {
InetSocketAddress address = new InetSocketAddress( InetSocketAddress address = new InetSocketAddress(
server.getIpAddress(), server.getPort()); InetAddress.getByAddress(server.getIpAddress().toByteArray()), server.getPort());
listBuilder.add(address); listBuilder.add(address);
// TODO(zhangkun83): fill the LB token to the attributes, and insert it to the // TODO(zhangkun83): fill the LB token to the attributes, and insert it to the
// application RPCs. // application RPCs.
if (!newServerMap.containsKey(address)) { if (!newServerMap.containsKey(address)) {
newServerMap.put(address, new ResolvedServerInfo(address, Attributes.EMPTY)); newServerMap.put(address, new ResolvedServerInfo(address, Attributes.EMPTY));
} }
} catch (UnknownHostException e) {
Throwables.propagate(e);
}
} }
} }
final RoundRobinServerList<T> newRoundRobinServerList = listBuilder.build(); final RoundRobinServerList<T> newRoundRobinServerList = listBuilder.build();

View File

@ -89,9 +89,11 @@ message ServerList {
} }
message Server { message Server {
// A resolved address and port for the server. The IP address string may // A resolved address for the server, serialized in network-byte-order. It may
// either be an IPv4 or IPv6 address. // either be an IPv4 or IPv6 address.
string ip_address = 1; bytes ip_address = 1;
// A resolved port number for the server.
int32 port = 2; int32 port = 2;
// An opaque token that is passed from the client to the server in metadata. // An opaque token that is passed from the client to the server in metadata.

View File

@ -46,6 +46,7 @@ import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings; import static org.mockito.Mockito.withSettings;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.protobuf.ByteString;
import io.grpc.Attributes; import io.grpc.Attributes;
import io.grpc.EquivalentAddressGroup; import io.grpc.EquivalentAddressGroup;
@ -476,7 +477,7 @@ public class GrpclbLoadBalancerTest {
for (ResolvedServerInfo server : servers) { for (ResolvedServerInfo server : servers) {
InetSocketAddress addr = (InetSocketAddress) server.getAddress(); InetSocketAddress addr = (InetSocketAddress) server.getAddress();
serverListBuilder.addServers(Server.newBuilder() serverListBuilder.addServers(Server.newBuilder()
.setIpAddress(addr.getHostString()) .setIpAddress(ByteString.copyFrom(addr.getAddress().getAddress()))
.setPort(addr.getPort()) .setPort(addr.getPort())
.build()); .build());
} }