Andrey Yarovoy created RATIS-2850:
-------------------------------------

             Summary: Make the write-buffer watermark of the gRPC client 
channel configurable
                 Key: RATIS-2850
                 URL: https://issues.apache.org/jira/browse/RATIS-2850
             Project: Ratis
          Issue Type: Bug
          Components: gRPC
            Reporter: Andrey Yarovoy


h3. Problem

{\{GrpcClientProtocolClient.buildChannel}} builds the client's channel to a 
RaftServer without a \{{ChannelOption.WRITE_BUFFER_WATER_MARK}}, so Netty's 
default applies: 32 KiB low, 64 KiB high. The only watermark Ratis sets today 
is the fixed 64 KiB / 128 KiB in \{{GrpcStubPool}} (RATIS-2325), which is on 
the server-to-server side.

With a 64 KiB high watermark, a client that sends large requests runs this 
cycle for every 64 KiB it sends:
# The HTTP/2 flow controller hands 64 KiB to the channel, and the channel turns 
unwritable.
# The 64 KiB goes to the socket, and the channel turns writable again.
# The writability event wakes the flow controller, which hands over the next 64 
KiB.

Each turn costs a writability event through the pipeline, promise callbacks, an 
event-loop wake-up and a \{{writev}} call. Apache Ozone is such a client: every 
write to a RATIS/THREE pipeline is a \{{WriteChunk}} request carrying up to 4 
MB of data (the default \{{ozone.scm.chunk.size}}), sent through 
\{{RaftClient}} to the leader. 
[HDDS-17871|https://issues.apache.org/jira/browse/HDDS-17871] found and fixed 
the same cycle on the Ozone datanode's own gRPC server, by raising its 
child-channel watermark to 512 KiB / 1 MiB. That fix doesn't reach this channel.

h3. Measurements

The setup:
* *Cluster:* 12 Ozone datanodes, gRPC TLS 1.3 on, a RATIS/THREE bucket.
* *Client:* one Ozone client on an 80-thread host with JDK 17, writing a 20 GB 
local file with Hadoop's \{{FsShell -put}}. That is a single stream with no 
other load.
* *Ratis:* the Ozone client uses Ratis 3.3.1 with ratis-thirdparty 1.1.0.
* *The patch:* for the test, \{{GrpcClientProtocolClient}} was patched to read 
a watermark from a JVM system property (patch below), and swapped into the 
client jar. Rebuilt without the patch, the class is byte-for-byte identical to 
the one in the jar, so nothing else changed.

*Socket writes to the leader during a 2 GB write* (counted with \{{strace -f 
-yy}}):
||Client channel watermark||writes||average size||most common size||
|Netty default, 32 KiB / 64 KiB|33,583|62.6 KB|65,692 bytes, 96% of the writes|
|512 KiB / 1 MiB|2,877|731 KB|72% of the writes are 512 KiB or more|

65,692 bytes is 64 KiB of data plus TLS record overhead, so with the default 
every write is cut at the high watermark.

*CPU of the 20 GB write* (mean of two rounds; each arm's rounds agreed within 1 
s):
||Client channel watermark||CPU, user + system||system||wall||
|Netty default|47.2 s|9.9 s|34.5 s|
|512 KiB / 1 MiB|42.8 s (-9%)|8.8 s|34.5 s|

The 2 GB write, traced with \{{strace}}, showed the same: 25.4 s against 22.5 s 
of CPU (-12%). Wall time doesn't change, because a single client isn't 
CPU-bound. The saving is CPU, which matters when many clients share a host, for 
example MapReduce or Spark tasks writing to Ozone.

*Before and after the ratis-thirdparty upgrade:* an older Ozone client with 
Ratis 3.2 and ratis-thirdparty 1.0.11 never reached the watermark. Its writes 
to the leader alternated between about 32 KiB and 48 KiB, and it used 5-6% less 
CPU per byte than the newer client with the default watermark. In the two 
ratis-thirdparty versions, Netty's \{{WriteBufferWaterMark}}, 
\{{DefaultHttp2RemoteFlowController}} and \{{AbstractEpollStreamChannel}} 
classes are byte-identical. gRPC's \{{NettyClientHandler}} is not, so the 
change in how much each flush carries most likely comes from gRPC. We haven't 
identified the exact gRPC change. Either way, the newer client now hits the 
default watermark on almost every write.

h3. Proposed change

Add two keys under \{{GrpcConfigKeys.Client}}, both \{{SizeInBytes}}:
* \{{raft.grpc.client.write-buffer-water-mark.low}} for the low watermark;
* \{{raft.grpc.client.write-buffer-water-mark.high}} for the high watermark.

When both are set, \{{buildChannel}} adds 
\{{withOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new 
WriteBufferWaterMark(low, high))}}. When they're not set, the channel stays as 
it is today, so the default behaviour doesn't change. Applications such as 
Ozone can then choose a value, the way HDDS-17871 did for the datanode server.

Whether Ratis should also change the default is open. 512 KiB / 1 MiB removed 
the cycle in our test. The cost is up to the high watermark of queued outbound 
data per busy channel, while the HTTP/2 flow-control window 
(\{{raft.grpc.flow.control.window}}, default 1 MB) still bounds what is in 
flight to the server.

The patch used for the measurements reads a JVM system property instead of a 
\{{RaftProperties}} key, so it is for testing only:
{code:java}
    // EXPERIMENT (not for commit): 
-Dratis.grpc.client.write-buffer-water-mark=<low>,<high> (bytes) sets the
    // channel's write-buffer watermark; unset keeps Netty's default (32 KiB / 
64 KiB).
    final String waterMark = 
System.getProperty("ratis.grpc.client.write-buffer-water-mark");
    if (waterMark != null) {
      final String[] lowHigh = waterMark.split(",");
      channelBuilder.withOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new 
WriteBufferWaterMark(
          Integer.parseInt(lowHigh[0].trim()), 
Integer.parseInt(lowHigh[1].trim())));
      LOG.info("Ratis client channel to {}: write-buffer water mark {}", 
address, waterMark);
    }
{code}

h3. Out of scope

* \{{GrpcStubPool}}'s fixed 64 KiB / 128 KiB watermark and the 
\{{GrpcServerProtocolClient}} channel, which carries \{{appendEntries}} from 
leader to follower. Large \{{appendEntries}} may hit the same cycle, but we 
haven't measured it.
* The admin channel. It is built by the same \{{buildChannel}} and would get 
the same option, but it carries small requests.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to