-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[extension/ackextension] Implement in-memory ack extension (#31651)
**Description:** Adding the in-memory implementation of the Ack extension proposed in #26376. **Link to tracking Issue:** #26376
- Loading branch information
1 parent
e752b99
commit 41a9700
Showing
12 changed files
with
475 additions
and
6 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: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: ackextension | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: adding the in-memory implementation of the ackextension | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [26376] | ||
|
||
# (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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,109 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ackextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/ackextension" | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
|
||
lru "github.com/hashicorp/golang-lru/v2" | ||
"go.opentelemetry.io/collector/component" | ||
) | ||
|
||
// inMemoryAckExtension is the in-memory implementation of the AckExtension | ||
// When MaxNumPartition is reached, the acks associated with the least recently used partition are evicted. | ||
// When MaxNumPendingAcksPerPartition is reached, the least recently used ack is evicted | ||
type inMemoryAckExtension struct { | ||
partitionMap *lru.Cache[string, *ackPartition] | ||
maxNumPendingAcksPerPartition uint64 | ||
} | ||
|
||
func newInMemoryAckExtension(conf *Config) *inMemoryAckExtension { | ||
cache, _ := lru.New[string, *ackPartition](int(conf.MaxNumPartition)) | ||
return &inMemoryAckExtension{ | ||
partitionMap: cache, | ||
maxNumPendingAcksPerPartition: conf.MaxNumPendingAcksPerPartition, | ||
} | ||
} | ||
|
||
type ackPartition struct { | ||
id atomic.Uint64 | ||
ackMap *lru.Cache[uint64, bool] | ||
} | ||
|
||
func newAckPartition(maxPendingAcks uint64) *ackPartition { | ||
cache, _ := lru.New[uint64, bool](int(maxPendingAcks)) | ||
return &ackPartition{ | ||
ackMap: cache, | ||
} | ||
} | ||
|
||
func (as *ackPartition) nextAck() uint64 { | ||
id := as.id.Add(1) | ||
as.ackMap.Add(id, false) | ||
return id | ||
} | ||
|
||
func (as *ackPartition) ack(key uint64) { | ||
if _, ok := as.ackMap.Get(key); ok { | ||
as.ackMap.Add(key, true) | ||
} | ||
} | ||
|
||
func (as *ackPartition) computeAcks(ackIDs []uint64) map[uint64]bool { | ||
result := make(map[uint64]bool, len(ackIDs)) | ||
for _, val := range ackIDs { | ||
if isAcked, ok := as.ackMap.Get(val); ok && isAcked { | ||
result[val] = true | ||
as.ackMap.Remove(val) | ||
} else { | ||
result[val] = false | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// Start of inMemoryAckExtension does nothing and returns nil | ||
func (i *inMemoryAckExtension) Start(_ context.Context, _ component.Host) error { | ||
return nil | ||
} | ||
|
||
// Shutdown of inMemoryAckExtension does nothing and returns nil | ||
func (i *inMemoryAckExtension) Shutdown(_ context.Context) error { | ||
return nil | ||
} | ||
|
||
// ProcessEvent marks the beginning of processing an event. It generates an ack ID for the associated partition ID. | ||
func (i *inMemoryAckExtension) ProcessEvent(partitionID string) (ackID uint64) { | ||
if val, ok := i.partitionMap.Get(partitionID); ok { | ||
return val.nextAck() | ||
} | ||
|
||
i.partitionMap.ContainsOrAdd(partitionID, newAckPartition(i.maxNumPendingAcksPerPartition)) | ||
val, _ := i.partitionMap.Get(partitionID) | ||
return val.nextAck() | ||
} | ||
|
||
// Ack acknowledges an event has been processed. | ||
func (i *inMemoryAckExtension) Ack(partitionID string, ackID uint64) { | ||
if val, ok := i.partitionMap.Get(partitionID); ok { | ||
val.ack(ackID) | ||
} | ||
} | ||
|
||
// QueryAcks checks the statuses of given ackIDs for a partition. | ||
// ackIDs that are not generated from ProcessEvent or have been removed as a result of previous calls to QueryAcks will return false. | ||
func (i *inMemoryAckExtension) QueryAcks(partitionID string, ackIDs []uint64) map[uint64]bool { | ||
if val, ok := i.partitionMap.Get(partitionID); ok { | ||
return val.computeAcks(ackIDs) | ||
} | ||
|
||
result := make(map[uint64]bool, len(ackIDs)) | ||
for _, ackID := range ackIDs { | ||
result[ackID] = false | ||
} | ||
|
||
return result | ||
} |
Oops, something went wrong.