Repository navigation
fix(channel): make retries skip tried channels and restore the top tier after affinity #8069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "math/rand" | ||
| "sort" | ||
|
|
@@ -119,10 +118,11 @@ func GetRandomSatisfiedChannel( | |
| model string, | ||
| retry int, | ||
| filters []dto.ChannelFilter, | ||
| excludeChannelIds []int, | ||
| ) (*Channel, error) { | ||
| // if memory cache is disabled, get channel directly from database | ||
| if !common.MemoryCacheEnabled { | ||
| return GetChannel(group, model, retry, filters) | ||
| return GetChannel(group, model, retry, filters, excludeChannelIds) | ||
| } | ||
|
|
||
| channelSyncLock.RLock() | ||
|
|
@@ -137,19 +137,39 @@ func GetRandomSatisfiedChannel( | |
| channels, _ = filterCandidateIDs(group2model2channels[group][normalizedModel], model, filters) | ||
| } | ||
|
|
||
| if len(channels) == 0 { | ||
| // Prefer channels this request has not attempted yet, so a retry walks | ||
| // fresh channels first. When every eligible channel was already tried | ||
| // (for example a single channel with a transient failure), fall back to | ||
| // the full pool instead of failing the request. | ||
| candidates := channels | ||
| if len(candidates) == 0 { | ||
| return nil, nil | ||
| } | ||
| if len(excludeChannelIds) > 0 { | ||
| excluded := make(map[int]bool, len(excludeChannelIds)) | ||
| for _, channelId := range excludeChannelIds { | ||
| excluded[channelId] = true | ||
| } | ||
| kept := make([]int, 0, len(channels)) | ||
| for _, channelId := range channels { | ||
| if !excluded[channelId] { | ||
| kept = append(kept, channelId) | ||
| } | ||
| } | ||
| if len(kept) > 0 { | ||
| candidates = kept | ||
| } | ||
| } | ||
|
Comment on lines
+148
to
+162
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same tier-index shift as
Example: tiers 10, 5, and 1 each hold one channel. After the tier-10 channel fails, retry 1 selects tier 1 and skips tier 5. The Derive the tier ladder from the full Also applies to: 192-192
|
||
|
|
||
| if len(channels) == 1 { | ||
| if channel, ok := channelsIDM[channels[0]]; ok { | ||
| if len(candidates) == 1 { | ||
| if channel, ok := channelsIDM[candidates[0]]; ok { | ||
| return channel, nil | ||
| } | ||
| return nil, fmt.Errorf("database consistency error: channel #%d does not exist, please contact the administrator", channels[0]) | ||
| return nil, fmt.Errorf("database consistency error: channel #%d does not exist, please contact the administrator", candidates[0]) | ||
| } | ||
|
|
||
| uniquePriorities := make(map[int]bool) | ||
| for _, channelId := range channels { | ||
| for _, channelId := range candidates { | ||
| if channel, ok := channelsIDM[channelId]; ok { | ||
| uniquePriorities[int(channel.GetPriority())] = true | ||
| } else { | ||
|
|
@@ -165,55 +185,59 @@ func GetRandomSatisfiedChannel( | |
| if retry >= len(uniquePriorities) { | ||
| retry = len(uniquePriorities) - 1 | ||
| } | ||
| targetPriority := int64(sortedUniquePriorities[retry]) | ||
|
|
||
| // get the priority for the given retry number | ||
| var sumWeight = 0 | ||
| var targetChannels []*Channel | ||
| for _, channelId := range channels { | ||
| if channel, ok := channelsIDM[channelId]; ok { | ||
| if channel.GetPriority() == targetPriority { | ||
| sumWeight += channel.GetWeight() | ||
| targetChannels = append(targetChannels, channel) | ||
| // Walk from the retry tier downwards and pick the first tier that still | ||
| // has candidates, so tiers left empty by the exclusion above are skipped | ||
| // instead of failing or re-rolling an already tried channel. | ||
| for idx := retry; idx < len(sortedUniquePriorities); idx++ { | ||
| targetPriority := int64(sortedUniquePriorities[idx]) | ||
|
|
||
| var sumWeight = 0 | ||
| var targetChannels []*Channel | ||
| for _, channelId := range candidates { | ||
| if channel, ok := channelsIDM[channelId]; ok { | ||
| if channel.GetPriority() == targetPriority { | ||
| sumWeight += channel.GetWeight() | ||
| targetChannels = append(targetChannels, channel) | ||
| } | ||
| } else { | ||
| return nil, fmt.Errorf("database consistency error: channel #%d does not exist, please contact the administrator", channelId) | ||
| } | ||
| } else { | ||
| return nil, fmt.Errorf("database consistency error: channel #%d does not exist, please contact the administrator", channelId) | ||
| } | ||
| } | ||
|
|
||
| if len(targetChannels) == 0 { | ||
| return nil, errors.New(fmt.Sprintf("no channel found, group: %s, model: %s, priority: %d", group, model, targetPriority)) | ||
| } | ||
| if len(targetChannels) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| // smoothing factor and adjustment | ||
| smoothingFactor := 1 | ||
| smoothingAdjustment := 0 | ||
| // smoothing factor and adjustment | ||
| smoothingFactor := 1 | ||
| smoothingAdjustment := 0 | ||
|
|
||
| if sumWeight == 0 { | ||
| // when all channels have weight 0, set sumWeight to the number of channels and set smoothing adjustment to 100 | ||
| // each channel's effective weight = 100 | ||
| sumWeight = len(targetChannels) * 100 | ||
| smoothingAdjustment = 100 | ||
| } else if sumWeight/len(targetChannels) < 10 { | ||
| // when the average weight is less than 10, set smoothing factor to 100 | ||
| smoothingFactor = 100 | ||
| } | ||
| if sumWeight == 0 { | ||
| // when all channels have weight 0, set sumWeight to the number of channels and set smoothing adjustment to 100 | ||
| // each channel's effective weight = 100 | ||
| sumWeight = len(targetChannels) * 100 | ||
| smoothingAdjustment = 100 | ||
| } else if sumWeight/len(targetChannels) < 10 { | ||
| // when the average weight is less than 10, set smoothing factor to 100 | ||
| smoothingFactor = 100 | ||
| } | ||
|
|
||
| // Calculate the total weight of all channels up to endIdx | ||
| totalWeight := sumWeight * smoothingFactor | ||
| // Calculate the total weight of all channels up to endIdx | ||
| totalWeight := sumWeight * smoothingFactor | ||
|
|
||
| // Generate a random value in the range [0, totalWeight) | ||
| randomWeight := rand.Intn(totalWeight) | ||
| // Generate a random value in the range [0, totalWeight) | ||
| randomWeight := rand.Intn(totalWeight) | ||
|
|
||
| // Find a channel based on its weight | ||
| for _, channel := range targetChannels { | ||
| randomWeight -= channel.GetWeight()*smoothingFactor + smoothingAdjustment | ||
| if randomWeight < 0 { | ||
| return channel, nil | ||
| // Find a channel based on its weight | ||
| for _, channel := range targetChannels { | ||
| randomWeight -= channel.GetWeight()*smoothingFactor + smoothingAdjustment | ||
| if randomWeight < 0 { | ||
| return channel, nil | ||
| } | ||
| } | ||
| } | ||
| // return null if no channel is not found | ||
| return nil, errors.New("channel not found") | ||
| // no tier holds a pickable channel; unreachable for a non-empty pool | ||
| return nil, nil | ||
| } | ||
|
|
||
| func CacheGetChannel(id int) (*Channel, error) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exclusion runs before tier computation. Retries can skip a higher-priority tier.
The code removes excluded abilities at Lines 134-145. It builds
prioritiesat Lines 147-159 from the filtered set. Tiers that the request already exhausted therefore disappear from the ladder, and every later tier index shifts up by one. Theretryindex no longer refers to the original tier.Example: tier 10 has channel A, tier 5 has channel B, and tier 1 has channel C.
prioritiesbecomes[5, 1].With
RetryTimes=1, B is never tried. This contradicts the stated goal of preserving prior behavior when tiers are single-channel.The downward walk at Lines 166-176 also cannot do its job. After the filter, every tier in
prioritieshas at least one ability. The first iteration always succeeds, so "skip tiers emptied by exclusions" is never exercised.Compute
prioritiesfrom the unfiltered abilities. Pick the tier byretryon that ladder. Walk down to the first tier that still has non-excluded abilities. If none exists, wrap from the top. If every ability is excluded, use the tier atretrywith the full pool. The memory-cache path inmodel/channel_cache.goneeds the same change.Sketch of the fix
rangeFrom(a, b)is a local helper that returns the indices[a, b).Also applies to: 166-176