Skip to content
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

Draft to Discuss: Use native Rust support for async traits in LogExporter::export() method #2374

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

lalitb
Copy link
Member

@lalitb lalitb commented Dec 2, 2024

Changes

Continuation to #2143, more to show feasibility, performance improvements, and have discussion before raising the PR for eventual review, this PR demonstrate using async traits support in Rust exporter. The support was added in Rust v1.75, while the msrv as of now is v1.70 for otel-sdk

The change is:
Existing:

#[async_trait]
pub trait LogExporter: Send + Sync + Debug {
  async fn export(&mut self, batch: LogBatch<'_>) -> LogResult<()>;
}

PR:

pub trait LogExporter: Send + Sync + Debug {
    fn export<'a>(
        &'a mut self,
        batch: &'a LogBatch<'a>,
    ) -> impl std::future::Future<Output = LogResult<()>> + Send + 'a;
}

Have modified the stress test to use LogExporter to demonstrate the improvement:
main with modified stress test:
main...lalitb:opentelemetry-rust:main-stress-with-exporter
6,348,766 iterations/sec
5,725,142 iterations/sec
6,020,324 iterations/sec
5,814,542 iterations/sec

this PR with modified stress test:
9,550,832 iterations/sec
11,045,635 iterations/sec
10,632,124 iterations/sec
10,212,681 iterations/sec
9,810,356 iterations/sec

There are improvements of ~71%, but it need to bump msrv to 1.75. Also, most of the improvement won't be the periodic

Thanks,
Lalit

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@lalitb lalitb requested a review from a team as a code owner December 2, 2024 22:34
@@ -82,7 +80,11 @@ pub trait LogExporter: Send + Sync + Debug {
/// A `LogResult<()>`, which is a result type indicating either a successful export (with
/// `Ok(())`) or an error (`Err(LogError)`) if the export operation failed.
///
async fn export(&mut self, batch: LogBatch<'_>) -> LogResult<()>;
fn export<'a>(
&'a mut self,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lalitb I think it maybe better to first check how to avoid requiring mut reference for exporting, before evaluating the results of this perf test, as that requirement has forced the need of mutex and introduced contention and hence stress tests shows way low throughput than before.

Copy link
Member Author

@lalitb lalitb Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cijothomas If we remove the mut self-reference from the LogExporter::export() method, it will force the exporter implementation to rely on interior mutability to modify its internal state. This would involve using synchronization mechanisms like locks or atomic operations, which add complexity and overhead. But in the case of SimpleLogProcessor and BatchLogProcessor, these methods are always called sequentially, so there’s no real benefit to introducing this extra layer of synchronization. It would just make the code more complicated and less performant without actually solving any problem.

The use-case for having a non-mutating export method would be in reentrant log processors, where LogProcessor::emit() -> LogProcessor::export() could be invoked concurrently from multiple user threads. In such scenarios, interior mutability would be necessary if the exporter implementation needs to manage internal state. However, this is not relevant for the current simple and batch processors, which operate in a strictly sequential manner.

Actually, there are 4 different exporter scenarios to think about -

  • sync/concurrent:
    fn export<'a>(&'a self, batch: &'a LogBatch<'a>) -> LogResult<()>;
  • sync/sequential,
   fn export<'a>(&'a mut self, batch: &'a LogBatch<'a>) -> LogResult<()>;
  • async/concurrent
  fn export<'a>(&'a self, batch: &'a LogBatch<'a>) -> impl std::future::Future<Output = LogResult<()>> + Send + 'a;
  • async/sequential
 fn export<'a>(&'a mut self, batch: &'a LogBatch<'a>) -> impl std::future::Future<Output = LogResult<()>> + Send + 'a;

Currently:

  • The SimpleLogProcessor and BatchLogProcessor invoke the exporter in async/sequential mode.
  • The ReentrantProcessor, used by ETW and user_events in the contrib repository, operates the exporter in sync/concurrent mode.

To ensure clarity and extensibility, we need to properly model these scenarios within the design.

Copy link
Member Author

@lalitb lalitb Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to add, we don't currently have specific use-case for sync/sequential, and async/concurrent, they are mentioned above for completeness of all the scenarios.

@@ -247,11 +247,16 @@ mod tests {

#[async_trait]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need async-trait anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this can be safely removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants