Skip to content

Commit

Permalink
txHandler: applications rate limiter (#5734)
Browse files Browse the repository at this point in the history
* Rate limit incoming apps based on app id + sender IP addr when the backlog is more than 1/2 full.
* Implemented as a sharded map with sliding window limiter.
   The sliding window data is used to for least recently used eviction (list based).
* Two hash functions are used:
   1) memhash64 for app to bucket mapping
   2) salted blake2b for app id + sender caching (shrank to 8 bytes to save on memory)
  • Loading branch information
algorandskiy committed Nov 9, 2023
1 parent 3605084 commit 8e30dd4
Show file tree
Hide file tree
Showing 13 changed files with 1,353 additions and 30 deletions.
5 changes: 5 additions & 0 deletions config/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@ var StateProofTopVoters int
// in a block must not exceed MaxTxnBytesPerBlock.
var MaxTxnBytesPerBlock int

// MaxAppTxnForeignApps is the max number of foreign apps per txn across all consensus versions
var MaxAppTxnForeignApps int

func checkSetMax(value int, curMax *int) {
if value > *curMax {
*curMax = value
Expand Down Expand Up @@ -681,6 +684,8 @@ func checkSetAllocBounds(p ConsensusParams) {
checkSetMax(p.MaxAppKeyLen, &MaxAppBytesKeyLen)
checkSetMax(int(p.StateProofTopVoters), &StateProofTopVoters)
checkSetMax(p.MaxTxnBytesPerBlock, &MaxTxnBytesPerBlock)

checkSetMax(p.MaxAppTxnForeignApps, &MaxAppTxnForeignApps)
}

// SaveConfigurableConsensus saves the configurable protocols file to the provided data directory.
Expand Down
18 changes: 16 additions & 2 deletions config/localTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Local struct {
// Version tracks the current version of the defaults so we can migrate old -> new
// This is specifically important whenever we decide to change the default value
// for an existing parameter. This field tag must be updated any time we add a new version.
Version uint32 `version[0]:"0" version[1]:"1" version[2]:"2" version[3]:"3" version[4]:"4" version[5]:"5" version[6]:"6" version[7]:"7" version[8]:"8" version[9]:"9" version[10]:"10" version[11]:"11" version[12]:"12" version[13]:"13" version[14]:"14" version[15]:"15" version[16]:"16" version[17]:"17" version[18]:"18" version[19]:"19" version[20]:"20" version[21]:"21" version[22]:"22" version[23]:"23" version[24]:"24" version[25]:"25" version[26]:"26" version[27]:"27" version[28]:"28" version[29]:"29" version[30]:"30" version[31]:"31"`
Version uint32 `version[0]:"0" version[1]:"1" version[2]:"2" version[3]:"3" version[4]:"4" version[5]:"5" version[6]:"6" version[7]:"7" version[8]:"8" version[9]:"9" version[10]:"10" version[11]:"11" version[12]:"12" version[13]:"13" version[14]:"14" version[15]:"15" version[16]:"16" version[17]:"17" version[18]:"18" version[19]:"19" version[20]:"20" version[21]:"21" version[22]:"22" version[23]:"23" version[24]:"24" version[25]:"25" version[26]:"26" version[27]:"27" version[28]:"28" version[29]:"29" version[30]:"30" version[31]:"31" version[32]:"32"`

// Archival nodes retain a full copy of the block history. Non-Archival nodes will delete old blocks and only retain what's need to properly validate blockchain messages (the precise number of recent blocks depends on the consensus parameters. Currently the last 1321 blocks are required). This means that non-Archival nodes require significantly less storage than Archival nodes. Relays (nodes with a valid NetAddress) are always Archival, regardless of this setting. This may change in the future. If setting this to true for the first time, the existing ledger may need to be deleted to get the historical values stored as the setting only effects current blocks forward. To do this, shutdown the node and delete all .sqlite files within the data/testnet-version directory, except the crash.sqlite file. Restart the node and wait for the node to sync.
Archival bool `version[0]:"false"`
Expand Down Expand Up @@ -231,7 +231,21 @@ type Local struct {
// TxBacklogReservedCapacityPerPeer determines how much dedicated serving capacity the TxBacklog gives each peer
TxBacklogReservedCapacityPerPeer int `version[27]:"20"`

// EnableTxBacklogRateLimiting controls if a rate limiter and congestion manager shouild be attached to the tx backlog enqueue process
// TxBacklogAppTxRateLimiterMaxSize denotes a max size for the tx rate limiter
// calculated as "a thousand apps on a network of thousand of peers"
TxBacklogAppTxRateLimiterMaxSize int `version[32]:"1048576"`

// TxBacklogAppTxPerSecondRate determines a target app per second rate for the app tx rate limiter
TxBacklogAppTxPerSecondRate int `version[32]:"100"`

// TxBacklogRateLimitingCongestionRatio determines the backlog filling threshold percentage at which the app limiter kicks in
// or the tx backlog rate limiter kicks off.
TxBacklogRateLimitingCongestionPct int `version[32]:"50"`

// EnableTxBacklogAppRateLimiting controls if an app rate limiter should be attached to the tx backlog enqueue process
EnableTxBacklogAppRateLimiting bool `version[32]:"true"`

// EnableTxBacklogRateLimiting controls if a rate limiter and congestion manager should be attached to the tx backlog enqueue process
// if enabled, the over-all TXBacklog Size will be larger by MAX_PEERS*TxBacklogReservedCapacityPerPeer
EnableTxBacklogRateLimiting bool `version[27]:"false" version[30]:"true"`

Expand Down
6 changes: 5 additions & 1 deletion config/local_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package config

var defaultLocal = Local{
Version: 31,
Version: 32,
AccountUpdatesStatsInterval: 5000000000,
AccountsRebuildSynchronousMode: 1,
AgreementIncomingBundlesQueueLength: 15,
Expand Down Expand Up @@ -81,6 +81,7 @@ var defaultLocal = Local{
EnableRequestLogger: false,
EnableRuntimeMetrics: false,
EnableTopAccountsReporting: false,
EnableTxBacklogAppRateLimiting: true,
EnableTxBacklogRateLimiting: true,
EnableTxnEvalTracer: false,
EnableUsageLog: false,
Expand Down Expand Up @@ -141,6 +142,9 @@ var defaultLocal = Local{
TrackerDBDir: "",
TransactionSyncDataExchangeRate: 0,
TransactionSyncSignificantMessageThreshold: 0,
TxBacklogAppTxPerSecondRate: 100,
TxBacklogAppTxRateLimiterMaxSize: 1048576,
TxBacklogRateLimitingCongestionPct: 50,
TxBacklogReservedCapacityPerPeer: 20,
TxBacklogServiceRateWindowSeconds: 10,
TxBacklogSize: 26000,
Expand Down

0 comments on commit 8e30dd4

Please sign in to comment.