Skip to content

Commit 0e3d3d9

Browse files
authored
Finish implementing byte-based flow control (#5362)
A followup to #4353 to flow-control total bytes not just flood bytes.
2 parents 6936b09 + cce38ae commit 0e3d3d9

12 files changed

Lines changed: 244 additions & 59 deletions

‎docs/stellar-core_example.cfg‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,13 @@ PEER_FLOOD_READING_CAPACITY_BYTES=0
610610
# If set to 0, calculate automatically based on soroban configs (tx size)
611611
FLOW_CONTROL_SEND_MORE_BATCH_SIZE_BYTES=0
612612

613+
# PEER_TOTAL_READING_CAPACITY_BYTES (Integer) default 1048576
614+
# The number of total input bytes on a TCP connection that a receiver will
615+
# read and start processing, before it temporarily pauses reading to finish
616+
# processing them. As input-processing completes, the balance of this
617+
# capacity is replenished.
618+
PEER_TOTAL_READING_CAPACITY_BYTES=1048576
619+
613620
# OUTBOUND_TX_QUEUE_BYTE_LIMIT (Integer) default 3145728
614621
# Byte limit for outbound transaction queue.
615622
OUTBOUND_TX_QUEUE_BYTE_LIMIT=3145728

‎src/main/Config.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ Config::Config() : NODE_SEED(SecretKey::random())
285285
PEER_FLOOD_READING_CAPACITY_BYTES = 0;
286286
FLOW_CONTROL_SEND_MORE_BATCH_SIZE_BYTES = 0;
287287
OUTBOUND_TX_QUEUE_BYTE_LIMIT = 1024 * 1024 * 3;
288+
PEER_TOTAL_READING_CAPACITY_BYTES = 1024 * 1024;
288289

289290
// WORKER_THREADS: setting this too low risks a form of priority inversion
290291
// where a long-running background task occupies all worker threads and
@@ -1141,6 +1142,11 @@ Config::processConfig(std::shared_ptr<cpptoml::table> t)
11411142
PEER_FLOOD_READING_CAPACITY_BYTES =
11421143
readInt<uint32_t>(item, 0);
11431144
}},
1145+
{"PEER_TOTAL_READING_CAPACITY_BYTES",
1146+
[&]() {
1147+
PEER_TOTAL_READING_CAPACITY_BYTES =
1148+
readInt<uint32_t>(item, 1);
1149+
}},
11441150
{"FLOW_CONTROL_SEND_MORE_BATCH_SIZE_BYTES",
11451151
[&]() {
11461152
FLOW_CONTROL_SEND_MORE_BATCH_SIZE_BYTES =

‎src/main/Config.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ class Config : public std::enable_shared_from_this<Config>
486486
uint32_t PEER_FLOOD_READING_CAPACITY_BYTES;
487487
uint32_t FLOW_CONTROL_SEND_MORE_BATCH_SIZE_BYTES;
488488

489+
// The number of total input bytes on a TCP connection that a receiver will
490+
// read and start processing, before it temporarily pauses reading to finish
491+
// processing them. As input-processing completes, the balance of this
492+
// capacity is replenished.
493+
uint32_t PEER_TOTAL_READING_CAPACITY_BYTES;
494+
489495
// Byte limit for outbound transaction queue.
490496
uint32_t OUTBOUND_TX_QUEUE_BYTE_LIMIT;
491497

‎src/overlay/FlowControl.cpp‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ FlowControl::FlowControl(AppConnector& connector, bool useBackgroundThread)
3232
: mFlowControlCapacity(connector.getConfig(), mNodeID)
3333
, mFlowControlBytesCapacity(
3434
connector.getConfig(), mNodeID,
35-
connector.getOverlayManager().getFlowControlBytesTotal())
35+
connector.getOverlayManager().getFlowControlFloodByteCapacity())
3636
, mOverlayMetrics(connector.getOverlayManager().getOverlayMetrics())
3737
, mAppConnector(connector)
3838
, mUseBackgroundThread(useBackgroundThread)
@@ -577,20 +577,14 @@ FlowControl::getFlowControlJsonInfo(bool compact) const
577577
MutexLocker guard(mFlowControlMutex);
578578

579579
Json::Value res;
580-
if (mFlowControlCapacity.getCapacity().mTotalCapacity)
581-
{
582-
res["local_capacity"]["reading"] = static_cast<Json::UInt64>(
583-
*(mFlowControlCapacity.getCapacity().mTotalCapacity));
584-
}
580+
res["local_capacity"]["reading"] = static_cast<Json::UInt64>(
581+
mFlowControlCapacity.getCapacity().mTotalCapacity);
585582
res["local_capacity"]["flood"] = static_cast<Json::UInt64>(
586583
mFlowControlCapacity.getCapacity().mFloodCapacity);
587584
res["peer_capacity"] =
588585
static_cast<Json::UInt64>(mFlowControlCapacity.getOutboundCapacity());
589-
if (mFlowControlBytesCapacity.getCapacity().mTotalCapacity)
590-
{
591-
res["local_capacity_bytes"]["reading"] = static_cast<Json::UInt64>(
592-
*(mFlowControlBytesCapacity.getCapacity().mTotalCapacity));
593-
}
586+
res["local_capacity_bytes"]["reading"] = static_cast<Json::UInt64>(
587+
mFlowControlBytesCapacity.getCapacity().mTotalCapacity);
594588
res["local_capacity_bytes"]["flood"] = static_cast<Json::UInt64>(
595589
mFlowControlBytesCapacity.getCapacity().mFloodCapacity);
596590
res["peer_capacity_bytes"] = static_cast<Json::UInt64>(

‎src/overlay/FlowControlCapacity.cpp‎

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "overlay/FlowControlCapacity.h"
66
#include "overlay/FlowControl.h"
77
#include "overlay/OverlayManager.h"
8+
#include "overlay/Peer.h"
89
#include "util/Logging.h"
910
#include <Tracy.hpp>
1011

@@ -28,8 +29,7 @@ FlowControlMessageCapacity::getMsgResourceCount(StellarMessage const& msg) const
2829
FlowControlCapacity::ReadingCapacity
2930
FlowControlMessageCapacity::getCapacityLimits() const
3031
{
31-
return {mConfig.PEER_FLOOD_READING_CAPACITY,
32-
std::make_optional<uint64_t>(mConfig.PEER_READING_CAPACITY)};
32+
return {mConfig.PEER_FLOOD_READING_CAPACITY, mConfig.PEER_READING_CAPACITY};
3333
}
3434

3535
void
@@ -50,14 +50,15 @@ bool
5050
FlowControlMessageCapacity::canRead() const
5151
{
5252
ZoneScoped;
53-
releaseAssert(mCapacity.mTotalCapacity);
54-
return *mCapacity.mTotalCapacity > 0;
53+
return mCapacity.mTotalCapacity > 0;
5554
}
5655

5756
FlowControlByteCapacity::FlowControlByteCapacity(Config const& cfg,
5857
NodeID const& nodeID,
59-
uint32_t capacity)
60-
: FlowControlCapacity(cfg, nodeID), mCapacityLimits{capacity, std::nullopt}
58+
uint32_t floodCapacity)
59+
: FlowControlCapacity(cfg, nodeID)
60+
, mCapacityLimits{floodCapacity, BYTE_CAPACITY_READ_FLOOR +
61+
cfg.PEER_TOTAL_READING_CAPACITY_BYTES}
6162
{
6263
mCapacity = mCapacityLimits;
6364
}
@@ -91,8 +92,7 @@ FlowControlByteCapacity::releaseOutboundCapacity(StellarMessage const& msg)
9192
bool
9293
FlowControlByteCapacity::canRead() const
9394
{
94-
releaseAssert(!mCapacity.mTotalCapacity);
95-
return true;
95+
return mCapacity.mTotalCapacity > BYTE_CAPACITY_READ_FLOOR;
9696
}
9797

9898
void
@@ -115,16 +115,8 @@ FlowControlCapacity::checkCapacityInvariants() const
115115
ZoneScoped;
116116
releaseAssert(getCapacityLimits().mFloodCapacity >=
117117
mCapacity.mFloodCapacity);
118-
if (getCapacityLimits().mTotalCapacity)
119-
{
120-
releaseAssert(mCapacity.mTotalCapacity);
121-
releaseAssert(*getCapacityLimits().mTotalCapacity >=
122-
*mCapacity.mTotalCapacity);
123-
}
124-
else
125-
{
126-
releaseAssert(!mCapacity.mTotalCapacity);
127-
}
118+
releaseAssert(getCapacityLimits().mTotalCapacity >=
119+
mCapacity.mTotalCapacity);
128120
}
129121

130122
void
@@ -150,12 +142,9 @@ FlowControlCapacity::canLockLocalCapacity(StellarMessage const& msg) const
150142
return false;
151143
}
152144
}
153-
if (mCapacity.mTotalCapacity)
145+
if (mCapacity.mTotalCapacity < msgResources)
154146
{
155-
if (*mCapacity.mTotalCapacity < msgResources)
156-
{
157-
return false;
158-
}
147+
return false;
159148
}
160149
return true;
161150
}
@@ -168,14 +157,12 @@ FlowControlCapacity::lockLocalCapacity(StellarMessage const& msg)
168157
releaseAssert(canLockLocalCapacity(msg));
169158

170159
auto msgResources = getMsgResourceCount(msg);
171-
if (mCapacity.mTotalCapacity)
172-
{
173-
releaseAssert(*mCapacity.mTotalCapacity >= msgResources);
174-
*mCapacity.mTotalCapacity -= msgResources;
175-
}
160+
releaseAssert(mCapacity.mTotalCapacity >= msgResources);
161+
mCapacity.mTotalCapacity -= msgResources;
176162

177163
if (OverlayManager::isFloodMessage(msg))
178164
{
165+
releaseAssert(mCapacity.mFloodCapacity >= msgResources);
179166
mCapacity.mFloodCapacity -= msgResources;
180167
if (mCapacity.mFloodCapacity == 0)
181168
{
@@ -192,10 +179,7 @@ FlowControlCapacity::releaseLocalCapacity(StellarMessage const& msg)
192179

193180
uint64_t releasedFloodCapacity = 0;
194181
size_t resourcesFreed = getMsgResourceCount(msg);
195-
if (mCapacity.mTotalCapacity)
196-
{
197-
*mCapacity.mTotalCapacity += resourcesFreed;
198-
}
182+
mCapacity.mTotalCapacity += resourcesFreed;
199183

200184
if (OverlayManager::isFloodMessage(msg))
201185
{

‎src/overlay/FlowControlCapacity.h‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#pragma once
66

77
#include "main/Config.h"
8+
#include "overlay/Peer.h"
9+
#include "overlay/TCPPeer.h"
810
#include <optional>
911

1012
namespace stellar
@@ -22,7 +24,7 @@ class FlowControlCapacity
2224
struct ReadingCapacity
2325
{
2426
uint64_t mFloodCapacity;
25-
std::optional<uint64_t> mTotalCapacity;
27+
uint64_t mTotalCapacity;
2628
};
2729

2830
// Capacity of local node configured by the operator
@@ -80,8 +82,20 @@ class FlowControlByteCapacity : public FlowControlCapacity
8082
ReadingCapacity mCapacityLimits;
8183

8284
public:
85+
// Subtle: when we issue a read, want to deduct the size of the read from
86+
// the capacity and stop when we "hit zero"; but we won't know how big the
87+
// read is before we perform it. It could be anywhere up to MAX_MESSAGE_SIZE
88+
// (plus 4 for the XDR header bytes) and so we treat that as an artificial
89+
// floor for the capacity value -- a sort of "virtual zero point" -- and
90+
// only allow reads when we have capacity _greater_ than that floor, such
91+
// that we know any read won't underflow true 0 and wrap around. The other
92+
// option would be to use a signed integer for capacity and tolerate
93+
// negative capacities sometimes, which would be confusing/alarming in a
94+
// different way. We decided this was nicer.
95+
uint64_t static constexpr BYTE_CAPACITY_READ_FLOOR = MAX_MESSAGE_SIZE + 4;
96+
8397
FlowControlByteCapacity(Config const& cfg, NodeID const& nodeID,
84-
uint32_t capacity);
98+
uint32_t floodCapacity);
8599
virtual ~FlowControlByteCapacity() = default;
86100
virtual uint64_t
87101
getMsgResourceCount(StellarMessage const& msg) const override;

‎src/overlay/OverlayManager.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class OverlayManager
201201

202202
virtual void recordMessageMetric(StellarMessage const& stellarMsg,
203203
Peer::pointer peer) = 0;
204-
virtual uint32_t getFlowControlBytesTotal() const = 0;
204+
virtual uint32_t getFlowControlFloodByteCapacity() const = 0;
205205

206206
virtual ~OverlayManager()
207207
{

‎src/overlay/OverlayManagerImpl.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ OverlayManagerImpl::start()
360360
}
361361

362362
uint32_t
363-
OverlayManagerImpl::getFlowControlBytesTotal() const
363+
OverlayManagerImpl::getFlowControlFloodByteCapacity() const
364364
{
365365
releaseAssert(threadIsMain());
366366
auto const maxTxSize = mApp.getHerder().getMaxTxSize();

‎src/overlay/OverlayManagerImpl.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class OverlayManagerImpl : public OverlayManager
215215
void extractPeersFromMap(std::map<NodeID, Peer::pointer> const& peerMap,
216216
std::vector<Peer::pointer>& result);
217217
void shufflePeerList(std::vector<Peer::pointer>& peerList);
218-
uint32_t getFlowControlBytesTotal() const override;
218+
uint32_t getFlowControlFloodByteCapacity() const override;
219219

220220
// Returns `true` iff the overlay can accept the outbound peer at `address`.
221221
// Logs whenever a peer cannot be accepted.

‎src/overlay/Peer.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ Peer::endMessageProcessing(StellarMessage const& msg)
311311
}
312312

313313
// If throttled, schedule read as soon as a full batch is processed
314-
if (mFlowControl->isThrottled() && res.numTotalMessages > 0)
314+
if (mFlowControl->isThrottled() && mFlowControl->canRead())
315315
{
316316
mFlowControl->stopThrottling();
317317
#ifdef BUILD_TESTS
@@ -1965,7 +1965,7 @@ Peer::recvAuth(StellarMessage const& msg)
19651965
}
19661966

19671967
uint32_t fcBytes =
1968-
mAppConnector.getOverlayManager().getFlowControlBytesTotal();
1968+
mAppConnector.getOverlayManager().getFlowControlFloodByteCapacity();
19691969

19701970
// Subtle: after successful auth, must send sendMore message first to
19711971
// tell the other peer about the local node's reading capacity.

0 commit comments

Comments
 (0)