-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Failover Connector PR2 - core failover functionality (#29557)
This is the 2nd PR for the failover connector that implements the core failover functionality. It is currently in place for Traces and once solidified will be repeated for metrics and logs Link to tracking Issue: #20766 Note: Will add traces tests today but pushing up to begin review cc: @djaglowski @fatsheep9146
- Loading branch information
Showing
7 changed files
with
595 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: new_component | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: failoverconnector | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: PR provides core logic for failover connector and implements failover for trace signals | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [20766] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
connector/failoverconnector/internal/state/pipeline_selector.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package state // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/failoverconnector/internal/state" | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// PipelineSelector is meant to serve as the source of truth for the target priority level | ||
type PipelineSelector struct { | ||
currentIndex int | ||
stableIndex int | ||
lock sync.RWMutex | ||
pipelineRetries []int | ||
maxRetry int | ||
} | ||
|
||
// UpdatePipelineIndex is the main function that updates the pipeline indexes due to an error | ||
// if the currentIndex is not the stableIndex, that means the currentIndex is a higher | ||
// priority index that was set during a retry, in which case we return to the stable index | ||
func (p *PipelineSelector) UpdatePipelineIndex(idx int) { | ||
if p.IndexIsStable(idx) { | ||
p.setToNextPriorityPipeline(idx) | ||
return | ||
} | ||
p.setToStableIndex(idx) | ||
} | ||
|
||
// NextPipeline skips through any lower priority pipelines that have exceeded their maxRetries | ||
// and sets the first that has not as the new stable | ||
func (p *PipelineSelector) setToNextPriorityPipeline(idx int) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
for ok := true; ok; ok = p.exceededMaxRetries(idx) { | ||
idx++ | ||
} | ||
p.stableIndex = idx | ||
} | ||
|
||
// retryHighPriorityPipelines responsible for single iteration through all higher priority pipelines | ||
func (p *PipelineSelector) RetryHighPriorityPipelines(ctx context.Context, stableIndex int, retryGap time.Duration) { | ||
ticker := time.NewTicker(retryGap) | ||
|
||
defer ticker.Stop() | ||
|
||
for i := 0; i < stableIndex; i++ { | ||
// if stableIndex was updated to a higher priority level during the execution of the goroutine | ||
// will return to avoid overwriting higher priority level with lower one | ||
if stableIndex > p.StableIndex() { | ||
return | ||
} | ||
// checks that max retries were not used for this index | ||
if p.MaxRetriesUsed(i) { | ||
continue | ||
} | ||
select { | ||
// return when context is cancelled by parent goroutine | ||
case <-ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
// when ticker triggers currentIndex is updated | ||
p.setToCurrentIndex(i) | ||
} | ||
} | ||
} | ||
|
||
func (p *PipelineSelector) exceededMaxRetries(idx int) bool { | ||
return idx < len(p.pipelineRetries) && (p.pipelineRetries[idx] >= p.maxRetry) | ||
} | ||
|
||
// SetToStableIndex returns the CurrentIndex to the known Stable Index | ||
func (p *PipelineSelector) setToStableIndex(idx int) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
p.pipelineRetries[idx]++ | ||
p.currentIndex = p.stableIndex | ||
} | ||
|
||
// SetToRetryIndex accepts a param and sets the CurrentIndex to this index value | ||
func (p *PipelineSelector) setToCurrentIndex(index int) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
p.currentIndex = index | ||
} | ||
|
||
// MaxRetriesUsed exported access to maxRetriesUsed | ||
func (p *PipelineSelector) MaxRetriesUsed(idx int) bool { | ||
p.lock.RLock() | ||
defer p.lock.RUnlock() | ||
return p.pipelineRetries[idx] >= p.maxRetry | ||
} | ||
|
||
// SetNewStableIndex Update stableIndex to the passed stable index | ||
func (p *PipelineSelector) setNewStableIndex(idx int) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
p.pipelineRetries[idx] = 0 | ||
p.stableIndex = idx | ||
} | ||
|
||
// IndexIsStable returns if index passed is the stable index | ||
func (p *PipelineSelector) IndexIsStable(idx int) bool { | ||
p.lock.RLock() | ||
defer p.lock.RUnlock() | ||
return p.stableIndex == idx | ||
} | ||
|
||
func (p *PipelineSelector) StableIndex() int { | ||
p.lock.RLock() | ||
defer p.lock.RUnlock() | ||
return p.stableIndex | ||
} | ||
|
||
func (p *PipelineSelector) CurrentIndex() int { | ||
p.lock.RLock() | ||
defer p.lock.RUnlock() | ||
return p.currentIndex | ||
} | ||
|
||
func (p *PipelineSelector) IndexRetryCount(idx int) int { | ||
p.lock.RLock() | ||
defer p.lock.RUnlock() | ||
return p.pipelineRetries[idx] | ||
} | ||
|
||
// reportStable reports back to the failoverRouter that the current priority level that was called by Consume.SIGNAL was | ||
// stable | ||
func (p *PipelineSelector) ReportStable(idx int) { | ||
// is stableIndex is already the known stableIndex return | ||
if p.IndexIsStable(idx) { | ||
return | ||
} | ||
// if the stableIndex is a retried index, the update the stable index to the retried index | ||
// NOTE retry will not stop due to potential higher priority index still available | ||
p.setNewStableIndex(idx) | ||
} | ||
|
||
func NewPipelineSelector(lenPriority int, maxRetries int) *PipelineSelector { | ||
return &PipelineSelector{ | ||
currentIndex: 0, | ||
stableIndex: 0, | ||
lock: sync.RWMutex{}, | ||
pipelineRetries: make([]int, lenPriority), | ||
maxRetry: maxRetries, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package state // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/failoverconnector/internal/state" | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
type TryLock struct { | ||
lock sync.Mutex | ||
} | ||
|
||
func (t *TryLock) TryExecute(fn func(int), arg int) { | ||
if t.lock.TryLock() { | ||
defer t.lock.Unlock() | ||
fn(arg) | ||
} | ||
} | ||
|
||
func NewTryLock() *TryLock { | ||
return &TryLock{} | ||
} | ||
|
||
// Manages cancel function for retry goroutine, ends up cleaner than using channels | ||
type RetryState struct { | ||
lock sync.Mutex | ||
cancelRetry context.CancelFunc | ||
} | ||
|
||
func (m *RetryState) UpdateCancelFunc(newCancelFunc context.CancelFunc) { | ||
m.lock.Lock() | ||
defer m.lock.Unlock() | ||
m.cancelRetry = newCancelFunc | ||
} | ||
|
||
func (m *RetryState) InvokeCancel() { | ||
m.lock.Lock() | ||
defer m.lock.Unlock() | ||
if m.cancelRetry != nil { | ||
m.cancelRetry() | ||
} | ||
} |
Oops, something went wrong.