Commit Graph

669 Commits

Author SHA1 Message Date
Eric Anderson 6907d81109 core: Make getTransport's fast path lock-free
This change exposed a pre-existing bug where shutdownNow wasn't called
for decommissionedTransports. The bug is fixed and a test added in this
commit.

Fixes #2120
2016-09-12 12:55:03 -07:00
Kun Zhang 3d4ae36074 Fix a deadlock in TransportSet.
Honor the lock order that transport lock > channel lock.

Resolves #2246
2016-09-12 11:11:32 -07:00
Eric Anderson 8b745d9114 core: Make PickFirst.pick's fast path lock-free
Fixes #2121
2016-09-12 10:19:09 -07:00
Jakob Buchgraber 4aadf550ee netty: Fix receipt of ClosedChannelException instead of actual error. Fixes #1330.
Our API allows pings to be send even after the transport has been shutdown. We currently
don't handle the case, where the Netty channel has been closed but the NettyClientHandler
has not yet been removed from the pipeline, correctly. That is, we need to query the shutdown
status whenever we receive a ClosedChannelException.

Also, some cleanup.
2016-09-12 19:17:34 +02:00
Jakob Buchgraber 8c18a0d355 netty: use custom http2 headers for decoding.
The DefaultHttp2Headers class is a general-purpose Http2Headers implementation
and provides much more functionality than we need in gRPC. In gRPC, when reading
headers off the wire, we only inspect a handful of them, before converting to
Metadata.

This commit introduces a Http2Headers implementation that aims for insertion
efficiency, a low memory footprint and fast conversion to Metadata.

  - Header names and values are stored in plain byte[].
  - Insertion is O(1), while lookup is now O(n).
  - Binary header values are base64 decoded as they are inserted.
  - The byte[][] returned by namesAndValues() can directly be used to construct
    a new Metadata object.
  - For HTTP/2 request headers, the pseudo headers are no longer carried over to
    Metadata.

A microbenchmark aiming to replicate the usage of Http2Headers in NettyClientHandler
and NettyServerHandler shows decent throughput gains when compared to DefaultHttp2Headers.

Benchmark                                             Mode  Cnt     Score    Error  Units
InboundHeadersBenchmark.defaultHeaders_clientHandler  avgt   10   283.830 ±  4.063  ns/op
InboundHeadersBenchmark.defaultHeaders_serverHandler  avgt   10  1179.975 ± 21.810  ns/op
InboundHeadersBenchmark.grpcHeaders_clientHandler     avgt   10   190.108 ±  3.510  ns/op
InboundHeadersBenchmark.grpcHeaders_serverHandler     avgt   10   561.426 ±  9.079  ns/op

Additionally, the memory footprint is reduced by more than 50%!

gRPC Request Headers: 864 bytes
Netty Request Headers: 1728 bytes
gRPC Response Headers: 216 bytes
Netty Response Headers: 528 bytes

Furthermore, this change does most of the gRPC groundwork necessary to be able
to cache higher ordered objects in HPACK's dynamic table, as discussed in [1].

[1] https://github.com/grpc/grpc-java/issues/2217
2016-09-09 23:15:18 +02:00
Carl Mastrangelo e79a27dec3 core: fix a javadoc bug in ClientCall 2016-09-09 13:33:00 -07:00
Carl Mastrangelo 35c3f8171b core/internal: replace make Stream id a primitive 2016-09-08 17:39:51 -07:00
Xiao Hang c69a4b8f19 core: add a new option to MethodDescriptor for caching support 2016-09-08 16:53:51 -07:00
Eric Anderson bdf8b01129 core,protobuf: Add simple argument introspection for methods
The cast required in protobuf makes me question how much I like
ReflectableMarshaller, but it seems to be pretty sound and the cast is
more an artifact of generics than the API.

Nano and Thrift were purposefully not updated, since getting just the
class requires making a new message instance. That seems a bit lame. It
probably is no burden to create an instance to get the class, and it may
not be too hard to improve the factory to provide class information, but
didn't want to bother at this point. Especially since nano users are
unlikely to need the introspection functionality.
2016-09-08 11:05:16 -07:00
Carl Mastrangelo f78644d762 core: add Metadata.discardAll()
Metadata.removeAll creates an iterator for looking through removed
values even if the call doens't use it.  This change adds a similar
method which doesn't create garbage.

This change makes it easier in the future to alter the internals
of Metadata where it may be expensive to return removed values.
2016-09-08 10:29:22 -07:00
Carl Mastrangelo 5379de726d core: fix indentation in MethodDescriptor 2016-09-07 17:40:21 -07:00
Carl Mastrangelo ca5a402fe6 netty: cache method path conversion
Benchmark                                      Mode      Cnt   Score   Error  Units
MethodDescriptorBenchmark.direct             sample  1179094  43.483 ± 0.610  ns/op
MethodDescriptorBenchmark.old                sample  1079285  66.210 ± 5.402  ns/op
MethodDescriptorBenchmark.transportSpecific  sample  1408070  36.800 ± 0.423  ns/op
2016-09-07 14:41:02 -07:00
Carl Mastrangelo 893ef44b4c all/tests: add a ClientCall and ServerCall suitable for use in tests 2016-09-02 17:09:49 -07:00
Carl Mastrangelo ea3f506249 core: avoid allocating Iterators in EquivalentAddressGroup, which is called for each new RPC
Benchmarked with 3 runs of 4 forks
Before:
Benchmark                         (direct)  (transport)    Mode     Cnt       Score     Error  Units
TransportBenchmark.unaryCall1024      true        NETTY  sample  255593  156248.670 ± 563.514  ns/op
Benchmark                         (direct)  (transport)    Mode     Cnt       Score     Error  Units
TransportBenchmark.unaryCall1024      true        NETTY  sample  261443  152753.415 ± 500.957  ns/op
Benchmark                         (direct)  (transport)    Mode     Cnt       Score     Error  Units
TransportBenchmark.unaryCall1024      true        NETTY  sample  258978  154205.374 ± 453.808  ns/op

After:
Benchmark                         (direct)  (transport)    Mode     Cnt       Score     Error  Units
TransportBenchmark.unaryCall1024      true        NETTY  sample  262194  152313.101 ± 502.563  ns/op
Benchmark                         (direct)  (transport)    Mode     Cnt       Score     Error  Units
TransportBenchmark.unaryCall1024      true        NETTY  sample  264811  150809.474 ± 477.459  ns/op
Benchmark                         (direct)  (transport)    Mode     Cnt       Score     Error  Units
TransportBenchmark.unaryCall1024      true        NETTY  sample  263606  151501.729 ± 593.149  ns/op
2016-09-02 15:48:23 -07:00
Kun Zhang c4f7f5c4fd core: split Context into a separate grpc-context artifact.
The Context API is not particularly gRPC-specific, and will be used by
Census as its context propagation mechanism.

Removed all dependencies to make it easy for other libraries to depend
on.
2016-09-02 13:18:35 -07:00
Kun Zhang 58d78dd0aa core: add ServerTransportFilter
Called whenever a ServerTransport is ready and terminated.  Has the
ability to modify transport attributes, which ServerCall.attributes()
are based on.

Related changes:

- Attribute keys for remote address and SSL session are now moved from
ServerCall to a neutral place io.grpc.Grpc, because they can also be
used from ServerTransportFilter, and probably will be used on the
client-side too.  The old keys on ServerCall is marked deprecated and
are equivalent to the new keys.
- Added transportReady() to ServerTransportListener.

Resolves #2132
2016-08-31 14:45:15 -07:00
Carl Mastrangelo 48c6b3d398 all/tests: unmock ClientCall and ServerCall 2016-08-30 09:58:02 -07:00
Carl Mastrangelo 3bf8d94f02 core/tests: add missing RunWith annotation 2016-08-29 12:53:52 -07:00
Carl Mastrangelo 6d28a93e21 core: simplify timeout header processing
Changes slightly improve performance

Benchmark                    (serialized)    Mode     Cnt    Score    Error  Units
GrpcUtilBenchmark.encodeNew         1000n  sample  336623   51.718 ±  1.417  ns/op
GrpcUtilBenchmark.encodeNew         1000u  sample  236574   77.555 ± 20.875  ns/op
GrpcUtilBenchmark.encodeNew         1000m  sample  224392   71.155 ±  1.600  ns/op
GrpcUtilBenchmark.encodeNew         1000S  sample  229616   67.269 ±  2.037  ns/op
GrpcUtilBenchmark.encodeNew         1000M  sample  215301   70.282 ±  1.933  ns/op
GrpcUtilBenchmark.encodeNew         1000H  sample  225063   73.679 ± 20.430  ns/op
GrpcUtilBenchmark.encodeOld         1000n  sample  311832   85.519 ±  1.729  ns/op
GrpcUtilBenchmark.encodeOld         1000u  sample  291613   92.320 ±  1.732  ns/op
GrpcUtilBenchmark.encodeOld         1000m  sample  271871   93.447 ±  1.872  ns/op
GrpcUtilBenchmark.encodeOld         1000S  sample  234932  117.956 ± 16.810  ns/op
GrpcUtilBenchmark.encodeOld         1000M  sample  224636  124.310 ± 20.249  ns/op
GrpcUtilBenchmark.encodeOld         1000H  sample  226764  130.803 ± 19.211  ns/op
GrpcUtilBenchmark.parseNew          1000n  sample  320709   60.480 ±  1.303  ns/op
GrpcUtilBenchmark.parseNew          1000u  sample  316349   64.447 ± 13.673  ns/op
GrpcUtilBenchmark.parseNew          1000m  sample  318209   61.705 ±  2.580  ns/op
GrpcUtilBenchmark.parseNew          1000S  sample  319629   59.342 ±  1.758  ns/op
GrpcUtilBenchmark.parseNew          1000M  sample  305715   59.362 ±  1.489  ns/op
GrpcUtilBenchmark.parseNew          1000H  sample  314919   60.224 ±  1.563  ns/op
GrpcUtilBenchmark.parseOld          1000n  sample  279243   64.040 ±  1.510  ns/op
GrpcUtilBenchmark.parseOld          1000u  sample  278008   71.313 ± 13.620  ns/op
GrpcUtilBenchmark.parseOld          1000m  sample  272633   67.872 ±  2.967  ns/op
GrpcUtilBenchmark.parseOld          1000S  sample  280955   63.966 ±  2.490  ns/op
GrpcUtilBenchmark.parseOld          1000M  sample  257645   71.329 ±  2.117  ns/op
GrpcUtilBenchmark.parseOld          1000H  sample  282510   68.425 ± 17.650  ns/op
2016-08-26 17:32:41 -07:00
Eric Anderson 656e8ce37f core: Remove com.google.common.collect usages for Android
This reduces the number of methods gRPC brings in by ~450, which is
substantial. Each application will see different numbers though,
depending on their usage and their other dependencies.

A very rough (under) counting for number of methods included because of
gRPC in android-interop-test is 2746, and that is reduced to 2313 (-433)
by this change. That count includes grpc, guava, okhttp, okio, and nano.
The actual reduction of methods is 447, with the discrepency due to
reduction of methods in java.util and java.lang. Of the 433 removed
methods, 377 are from com.google.common.collect and 61 from
com.google.common.base. The removal costed an increase of 5 methods
(total 1671) within io.grpc itself.
2016-08-26 15:13:45 -07:00
Carl Mastrangelo 7c847acf57 core: clarify EOS dataframe error came from server 2016-08-25 17:59:29 -07:00
Lukasz Strzalkowski 864158bdfb core: add tests for ResolvedServerInfo 2016-08-25 16:11:40 -07:00
Carl Mastrangelo 26a08cd75e core: add DoNotMock annotations 2016-08-25 11:14:43 -07:00
Kun Zhang 7909abf065 core: remove unused code path.
writeKnownLength() is never called with compressed == true, because in
most cases it's impossible to know the size of the compressed message.
2016-08-24 15:38:49 -07:00
Lukasz Strzalkowski 23652c5b03 core: change API of NameResolver#onUpdate
Instead of `List<List<ResolvedServerInfo>>`, `onUpdate` now takes
`List<ResolvedServerInfoGroup>` as an argument and every `ResolvedServerInfoGroup`
object can have `Attributes` attached to it which means that we can provide
attributes on each level:

  * root level via `onUpdate` argument (applies to all servers)
  * group level via property of `ResolvedServerInfoGroup` (applies to all servers
    in the group)
  * host level via property of `ResolvedServerInfo` (applies to a single server)
2016-08-24 15:14:57 -07:00
Lukasz Strzalkowski 577164c42f Rename Dummy load balancer to PickFirst 2016-08-23 09:15:56 -07:00
Lukasz Strzalkowski 5345be2ba0 core: Add equals/hashCode
Defines equality conditions for `Attributes` and `ResolvedServerInfo`.
2016-08-23 09:07:41 -07:00
Carl Mastrangelo c47d948a47 protobuf: copy input data before decoding
CodedInputStream is risk averse in ways that hurt performance when
parsing large messages.  gRPC knows how large the input size is as it
is being read from the wire, and only tries to parse it once the entire
message has been read in.  The message is represented as chunks of
memory strung together in a CompositeReadableBuffer, and then wrapped
in a custom BufferInputStream.

When passed to Protobuf, CodedInputStream attempts to read data out
of this InputStream into CIS's internal 4K buffer.  For messages that
are much larger, CIS copies from the input in chunks of 4K and saved in
an ArrayList.  Once the entire message size is read in, it is re-copied
into one large byte array and passed back up.  This only happens for
ByteStrings and ByteBuffers that are read out of CIS.  (See
CIS.readRawBytesSlowPath for implementation).

gRPC doesn't need this overhead, since we already have the entire
message in memory, albeit in chunks.  This change copies the composite
buffer into a single heap byte buffer, and passes this (via
UnsafeByteOperations) into CodedInputStream.  This pays one copy to
build the heap buffer, but avoids the two copes in CIS.  This also
ensures that the buffer is considered "immutable" from CIS's point of
view.

Because CIS does not have ByteString aliasing turned on, this large
buffer will not accidentally be kept in memory even if only tiny fields
from the proto are still referenced.  Instead, reading ByteStrings out
of CIS will always copy.  (This copy, and the problems it avoids, can
be turned off by calling CIS.enableAliasing.)

Benchmark results will come shortly, but initial testing shows
significant speedup in throughput tests.  Profiling has shown that
copying memory was a large time consumer for messages of size 1MB.
2016-08-17 15:45:21 -07:00
Carl Mastrangelo 1285477133 all: add parameter name to checkNotNull
After debugging #2153, it would have been nice to know what the exact
parameter was that was null. This change adds a name for each
checkNotNull (and tries to normalized on static imports in order to
shorten lines)
2016-08-12 14:55:00 -07:00
Kun Zhang 7a33fae8e8 core: fix a deadlock in TransportSet.
Resolves #2152
2016-08-10 09:47:25 -07:00
Eric Anderson c8648dc54e core: Remove usage of LogExceptionRunnable from Deadline
io.grpc should not be depending on anything from internal. Also, the
convenience method of Deadline is part of our public API and shouldn't
use LogExceptionRunnable because it would surprise our users.

Swapped to lower-case 'log' since the logger is not immutable.
2016-08-09 14:02:45 -07:00
Carl Mastrangelo 5384f9706a core: propagate name resolution errors 2016-08-08 17:53:58 -07:00
Eric Anderson 7c44a4e111 docs: Clarify the relationship between Status*Exception 2016-08-04 15:33:31 -07:00
Eric Anderson d394cef775 core: Avoid wrapping Errors in RuntimeException 2016-08-04 14:25:40 -07:00
Eric Anderson 7d464fcb02 inprocess: Avoid creating unnecessary threads
Implementations of ManagedClientTransport.start() are restricted from
calling the passed listener until start() returns, in order to avoid
reentrency problems with locks. For most transports this isn't a
problem, because they need additional threads anyway. InProcess uses no
additional threads naturally so ends up needing a thread just to
notifyReady. Now transports can just return a Runnable that can be run
after locks are dropped.

This was originally intended to be a performance optimization, but the
thread also causes nondeterminism because RPCs are delayed until
notifyReady is called. So avoiding the thread reduces needless fakes
during tests.
2016-08-02 13:16:36 -07:00
Kun Zhang 1775ab3847 core: call newStream() and applyRequestMetadata() under context.
`ClientTransport.newStream()` and
`CallCredentials.applyRequestMetadata()` is now called under the context
of the call.  This can be used to pass any call-specific information to
`CallCredentials`.
2016-07-29 17:24:38 -07:00
ZHANG Dapeng 41f166fd1e doc: fix io.grpc package level javadoc not showing up
- Resolves {@link ...} with full class name
- Javadoc can show up in package-summary.html
2016-07-28 12:39:48 -07:00
Naveen Reddy Chedeti 2860959d5f Support Thrift Messages 2016-07-28 08:48:27 -07:00
Kun Zhang 093ab06530 core: Log a warning in ClientCall.cancel if no detail is provided.
Resolves #2054
2016-07-25 10:30:54 -07:00
Kun Zhang d0c173b5d2 core: Remove default implementation for ClientCall.cancel.
Resolves #2050
2016-07-25 09:50:06 -07:00
Louis Ryan e276359f0e Force the immediate release of reference counted resources on AppEngine
to avoid executors backed by request-scoped threads from becoming
zombies
See
https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/ThreadManager.html#currentRequestThreadFactory--
2016-07-22 13:56:44 -07:00
Eric Anderson 2cd884ea80 core: Reduce DeadlineTest flake
To my knowledge, there has been just a single DeadlineTest flake since
the code was fixed to avoid issues with I/O due to class loading:

io.grpc.DeadlineTest > defaultTickerIsSystemTicker[0] FAILED
    java.lang.AssertionError: <-21431071 ns from now> and <0 ns from now> should have been within <20000000ns> of each other

But we don't really need fine-grained verification during the test
though; if the code is not using nanoTime, then it is almost certainly
not going to have even a day of accuracy (except on a fresh VM). So
checking for a second of accuracy vs 20ms shouldn't really be an issue.
2016-07-20 09:53:00 -07:00
Eric Anderson 4289aaf79c core: Hard-code a list of providers for Android
Class.getResource() is expensive on Android, which is used by
ServiceLoader. So we hard-code a list for Android instead.

Fixes #2037
2016-07-20 09:49:59 -07:00
Eric Anderson 083ea8e6aa core: Add missing assert in CallOptionsTest 2016-07-19 11:12:28 -07:00
Eric Anderson 2e4138aa5c core: Fix doc to refer to ImplBase 2016-07-14 16:06:58 -07:00
Louis Ryan f52b4e52cd Make the OkHTTP transport AppEngine friendly. AppEngine may support
conscrypt at some point which would allow ALPN to function
Clarify the SSLContext.getDefault is not used when constructing the
default SSLSocketFactory.
2016-07-13 18:27:43 -07:00
Eric Anderson d2cc576320 core: Fix ErrorProne errors 2016-07-13 14:14:15 -07:00
Eric Anderson bfcb04c196 core: Allow avoiding Provider loading, for Android 2016-07-11 10:02:23 -07:00
Eric Anderson 7fb43d2a1e core: Add generics to raw types in KeepAliveManagerTest
Swapping to doReturn is necessary since passing a <?> to thenReturn()
does not match the <?> of OngoingStubbing (without a cast).
2016-07-10 11:57:07 -07:00
Eric Anderson 0e47be1287 core: Save accept encoding since Metadata is not thread-safe
Metadata has been passed to the application. The application may be
modifying Metadata concurrently, so we must not access Metadata after
that point.

Fixes #1947
2016-07-10 11:28:04 -07:00