[
https://issues.apache.org/jira/browse/RATIS-2798?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Andrey Yarovoy reassigned RATIS-2798:
-------------------------------------
Assignee: Andrey Yarovoy
> zero-copy appendEntries on followers when the state machine data is not
> cached in the log
> -----------------------------------------------------------------------------------------
>
> Key: RATIS-2798
> URL: https://issues.apache.org/jira/browse/RATIS-2798
> Project: Ratis
> Issue Type: Sub-task
> Components: gRPC
> Reporter: Andrey Yarovoy
> Assignee: Andrey Yarovoy
> Priority: Major
>
> *Summary*
> A follower decoding an appendEntries request copies every log entry's state
> machine data into a new heap array, only
> to write it and drop it. When the state machine data is not cached in the
> log, which is how Apache Ozone runs, the
> follower doesn't need to keep it after replying. So it can be decoded without
> copying and released after the reply.
> In a microbenchmark this gave about +5% replication throughput with one
> connection per peer, and 3–7% less CPU per MB.
> *Description*
> **Current behaviour** (master):
> - `GrpcServerProtocolService` decodes appendEntries with gRPC's default
> protobuf marshaller. Each entry's state machine data is copied into a new
> heap `ByteString`.
> - With `raft.server.log.statemachine.data.caching.enabled = true`, Ratis
> strips the state machine data before caching or writing the log entry. It
> gives the full entry only to `StateMachine.DataApi.write`.
> - In Ozone, that data is the 4 MB of chunk data of each `WriteChunk`, for
> every pipeline the datanode is a follower in.
> - Measured in `GrpcLogAppenderConnectionsBenchmark` (RATIS-2787), with the
> setup below: about 80% of sampled allocated bytes come from
> `ByteString.copyFrom` in protobuf parsing, about two-thirds of that from
> followers' appendEntries.
> **History.**
> - RATIS-1934 wired `ZeroCopyMessageMarshaller` into appendEntries, and is
> resolved as fixed in 3.1.0.
> - But neither master nor the released 3.1.0, 3.2.0, 3.2.1 or 3.3.1 jars use
> the marshaller in any server class. The general reference counting it needed
> was reverted (RATIS-1983, `cdfd4f10`).
> - This issue takes a narrower route that needs no reference counting:
> zero-copy only for the state machine data, only on followers, and only where
> nothing keeps that data after the reply.
> *Proposal*
> - **New key:** `raft.grpc.server.zero-copy.append-entries.enabled`, default
> false.
> - It is accepted only with `raft.server.log.statemachine.data.caching.enabled
> = true`, so that the log cache and the
> log writer don't keep the data.
> - Otherwise the server logs a warning and leaves it off.
> - **Decoding** (`GrpcServerProtocolService`): appendEntries uses
> `ZeroCopyMessageMarshaller`. The rest of the service is
> unchanged; `bindServiceDefinition()` replaces only the appendEntries method's
> request marshaller.
> - **Copy all but the data:** before the request reaches the server,
> everything in it except the state machine data is
> copied to the heap: entry metadata, log data, configuration entries and
> commit infos. So only the state machine data refers to gRPC's buffers, and
> anything the server keeps (configuration, commit infos, the cached entry) is
> a heap copy.
> - **Release after the reply:**
> - The buffers are released when `appendEntriesAsync` completes successfully.
> The follower's reply already waits for
> the log entry to be written and for `DataApi.write`. With compose (the
> default), a duplicate request waits for the
> earlier append. So the data is no longer needed.
> - If the request fails before anything is appended, the buffers are released
> at once.
> - If the reply fails, they are not released, and a counter is logged: the log
> or the state machine could still be
> writing the data, and a leak is safer than corrupting it.
> - **State machine contract**, documented with the key:
> - The state machine must not keep the state machine data after `write`
> completes.
> - That includes the follower's `TransactionContext`.
> `StateMachine.startTransaction(LogEntryProto, RaftPeerRole)`
> keeps the whole entry by default, so a state machine using the key strips the
> data there, for example with
> `LogProtoUtils.removeStateMachineData`.
> **Out of scope:** the leader's client requests. The leader keeps the data to
> replicate it to followers, so releasing
> its buffers needs the reference counting RATIS-1983 tried.
> *Measured*
> - **Setup:**
> - 3 servers in one JVM, gRPC TLS on, macOS laptop with 14 cores;
> - 12 groups, all led by the same server, 1 MB entries, 8 writes in flight per
> group;
> - segmented log, state machine data not cached;
> - log fsync not awaited (`raft.server.log.unsafe-flush.enabled`), since macOS
> `F_FULLFSYNC` otherwise capped the run at about 200–275 MB/s;
> - each arm in a fresh JVM, arms alternated.
> - **State machine:**
> - It carries the payload as state machine data, like Ozone.
> - `write` computes a CRC32 of the data instead of writing it, and the
> follower's `startTransaction` strips the data.
> - The leader keeps its own data to serve `read`.
> - After each run, every follower's checksum is compared with the leader's,
> entry by entry, so data released too early would fail the run. All runs
> passed, for example 4,840 of 4,840 checksums.
> - **Allocation (async-profiler):** sampled allocated bytes fell by about 70%.
> The followers' parse no longer goes through a copying path.
> - **Throughput, one appendEntries connection per peer:** zero-copy was faster
> in 7 of 7 alternating rounds.
> | run | rounds | off (MB/s) | on (MB/s) | change | JVM CPU per MB |
> |---|---|---|---|---|---|
> | 1 (marshaller fix copying to one direct buffer per chunk) | 3 | 1,005 /
> 1,024 / 1,066 | 1,102 / 1,078 / 1,111 | +6.3% | −6.6% |
> | 2 (final: one direct buffer per message) | 4 | 909 / 1,046 / 1,051 / 1,106
> | 1,014 / 1,101 / 1,110 / 1,108 | +5.4% (+3.6% without the 909) | −3.2% |
> ## Considerations
> - **Apache Ozone's `ContainerStateMachine`** must be checked against the
> contract before Ozone enables the key:
> - its follower `startTransaction(LogEntryProto, RaftPeerRole)`;
> - whether `writeStateMachineData` keeps the data, for example in a cache,
> after it completes.
> - **Direct memory:** gRPC's buffers are held until the follower replies, so
> the feature needs metrics (RATIS-2092, RATIS-2486) and the leak detection
> from RATIS-2414.
> - **Failed replies leak by design.** They should be rare; a later change
> could release when the append futures themselves complete.
> - **Logging:** the stream observer keeps the last raw request for its
> "completed" log line. It reads only parsed metadata, but should log the
> copied request instead.
>
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)