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

fix(event-streams): CPU Utilization #176

Merged
merged 2 commits into from
Oct 2, 2024
Merged

Conversation

cynicaljoy
Copy link
Contributor

Description

Update OpenStream to use a BlockingCollection

Motivation and context

We had a tight loop in the EventListener that was causing high CPU utilization

How was the change tested?

Integration tests and

Sandbox app
using Fauna;
using Fauna.Mapping;

var client = new Client(Environment.GetEnvironmentVariable("FAUNA_SECRET")!);
var stream = await client.EventStreamAsync<Product>(Query.FQL($"Product.all().toStream()"));
var listenerLoop = Task.Run(async () =>
{
    while (true)
    {
        Console.WriteLine("Waiting for events...");
        await Task.Delay(TimeSpan.FromSeconds(15));
    }
});

var eventsLoop = Task.Run(async () =>
{
    await foreach (var evt in stream)
    {
        Console.WriteLine($"Received Event Type: {evt.Type}");
        if (evt.Data != null)
        {
            Console.WriteLine($"Data: {evt.Data.Name}");
        }
    }
});

await Task.WhenAny(listenerLoop, eventsLoop);

// ReSharper disable once ClassNeverInstantiated.Global
public class Product
{
    [Id] public string? Id { get; set; }
    
    // ReSharper disable once UnassignedField.Global
    public required string Name;
}

Screenshots (if appropriate):

Before

image

After

image

Change types

    • Bug fix (non-breaking change that fixes an issue)
    • New feature (non-breaking change that adds functionality)
    • Breaking change (backwards-incompatible fix or feature)

Checklist:

    • My code follows the code style of this project.
    • My change requires a change to Fauna documentation.
    • My change requires a change to the README, and I have updated it accordingly.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link

@jdcryans jdcryans left a comment

Choose a reason for hiding this comment

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

Negative LOC is good :)

@@ -449,7 +449,7 @@ public Task StreamThrowsWithBadRequest()
}
});

Assert.AreEqual("BadRequest: Bad Request", ex?.Message);

Choose a reason for hiding this comment

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

How come this changed?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, if this is expected I'd like to understand why. We should also rename the test appropriately and assert the correct exception type–hopefully we're not throwing FaunaException anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't returning the response on failed requests like I was before, but I've put it back to how it was. Initially I thought this was an improvement because we didn't have to create an exception from the response body but I realized it's better to early return as I had it before.

Copy link
Contributor

@pnwpedro pnwpedro left a comment

Choose a reason for hiding this comment

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

🥳

@@ -449,7 +449,7 @@ public Task StreamThrowsWithBadRequest()
}
});

Assert.AreEqual("BadRequest: Bad Request", ex?.Message);
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, if this is expected I'd like to understand why. We should also rename the test appropriately and assert the correct exception type–hopefully we're not throwing FaunaException anywhere?

@cynicaljoy cynicaljoy requested a review from pnwpedro October 1, 2024 16:37
Copy link

github-actions bot commented Oct 1, 2024

Summary

Summary
Generated on: 10/01/2024 - 16:38:42
Parser: OpenCover
Assemblies: 1
Classes: 124
Files: 100
Line coverage: 81.5% (3234 of 3967)
Covered lines: 3234
Uncovered lines: 733
Coverable lines: 3967
Total lines: 8853
Branch coverage: 70.3% (1049 of 1492)
Covered branches: 1049
Total branches: 1492
Method coverage: Feature is only available for sponsors
Tag: 9b9db3c

Coverage

Fauna - 81.5%
Name Line Branch
Fauna 81.5% 70.3%
Fauna.BaseClient 81.4% 68.7%
Fauna.Client 74% 52.9%
Fauna.Configuration 100% 100%
Fauna.Core.Connection 94.1% 72.2%
Fauna.Core.Endpoints 100%
Fauna.Core.ErrorInfo 100%
Fauna.Core.QueryFailure 100% 66.6%
Fauna.Core.QueryOptions 82.1% 81.8%
Fauna.Core.QueryResponse 70% 77.2%
Fauna.Core.QueryStats 57.1%
Fauna.Core.QuerySuccess`1 69.2% 50%
Fauna.Core.RetryConfiguration 100% 100%
Fauna.Core.Stats 100%
Fauna.Core.StatsCollector 86.2% 100%
Fauna.Core.StreamEnumerable`1 100% 66.6%
Fauna.Exceptions.AbortException 100% 83.3%
Fauna.Exceptions.AuthenticationException 100%
Fauna.Exceptions.AuthorizationException 100%
Fauna.Exceptions.BadGatewayException 0%
Fauna.Exceptions.ConstraintFailure 100%
Fauna.Exceptions.ConstraintFailureException 100%
Fauna.Exceptions.ContendedTransactionException 100%
Fauna.Exceptions.ExceptionHandler 84.3% 89.3%
Fauna.Exceptions.FaunaException 25%
Fauna.Exceptions.InvalidRequestException 100%
Fauna.Exceptions.NetworkException 100%
Fauna.Exceptions.NullDocumentException 100% 100%
Fauna.Exceptions.ProtocolException 0%
Fauna.Exceptions.QueryCheckException 100%
Fauna.Exceptions.QueryRuntimeException 100%
Fauna.Exceptions.QueryTimeoutException 100%
Fauna.Exceptions.SerializationException 100%
Fauna.Exceptions.ServiceException 80.9%
Fauna.Exceptions.ThrottlingException 42.8%
Fauna.Exceptions.TimeoutException 100%
Fauna.IQueryFragmentExtensions 0%
Fauna.Linq.DataContext 84.1% 87.5%
Fauna.Linq.DataContextBuilder`1 92.1% 86.6%
Fauna.Linq.IntermediateQueryHelpers 80% 83.3%
Fauna.Linq.IPipelineExecutor 55.2% 28.2%
Fauna.Linq.IQuerySource 0% 0%
Fauna.Linq.LookupTable 40% 16.6%
Fauna.Linq.MappedDeserializer`2 75%
Fauna.Linq.Pipeline 100% 100%
Fauna.Linq.ProjectionAnalysisVisitor 92.3% 50%
Fauna.Linq.ProjectionDeserializer 80.9% 58.3%
Fauna.Linq.ProjectionRewriteVisitor 96.9% 90%
Fauna.Linq.QuerySource 100%
Fauna.Linq.QuerySource`1 93.1% 88.8%
Fauna.Linq.QuerySourceExtensions 25% 50%
Fauna.Linq.SubQuerySwitch 56.7% 19.4%
Fauna.Mapping.CollAttribute 100%
Fauna.Mapping.FieldAttribute 100%
Fauna.Mapping.FieldInfo 100% 91.6%
Fauna.Mapping.FieldName 100% 100%
Fauna.Mapping.IdAttribute 100%
Fauna.Mapping.IgnoreAttribute 100%
Fauna.Mapping.MappingContext 100% 100%
Fauna.Mapping.MappingInfo 97.6% 100%
Fauna.Mapping.TsAttribute 100%
Fauna.Query 100%
Fauna.QueryArr 58.6% 50%
Fauna.QueryExpr 80% 50%
Fauna.QueryLiteral 86.1% 62.5%
Fauna.QueryObj 32.1% 0%
Fauna.QueryStringHandler 100% 100%
Fauna.QueryVal 79.5% 50%
Fauna.Serialization.BaseSerializer`1 62.5% 20%
Fauna.Serialization.BooleanSerializer 92.3% 83.3%
Fauna.Serialization.ByteSerializer 92.3% 83.3%
Fauna.Serialization.ClassSerializer`1 85.9% 83.3%
Fauna.Serialization.DateOnlySerializer 92.3% 83.3%
Fauna.Serialization.DateTimeOffsetSerializer 92.3% 83.3%
Fauna.Serialization.DateTimeSerializer 92.3% 83.3%
Fauna.Serialization.DictionarySerializer`1 81% 72.2%
Fauna.Serialization.DocumentSerializer 83.7% 79.1%
Fauna.Serialization.DoubleSerializer 92.3% 83.3%
Fauna.Serialization.DynamicSerializer 82.9% 68%
Fauna.Serialization.FloatSerializer 92.3% 83.3%
Fauna.Serialization.InternalDocument 80% 56.2%
Fauna.Serialization.IntSerializer 92.3% 83.3%
Fauna.Serialization.ListSerializer`1 85% 66.6%
Fauna.Serialization.LongSerializer 92.3% 83.3%
Fauna.Serialization.ModuleSerializer 69.2% 50%
Fauna.Serialization.NamedDocumentSerializer 83.7% 79.1%
Fauna.Serialization.NamedRefSerializer 77.7% 70%
Fauna.Serialization.NullableDocumentSerializer`1 86.4% 75%
Fauna.Serialization.NullableSerializer`1 100% 100%
Fauna.Serialization.NullableStructSerializer`1 70% 100%
Fauna.Serialization.PageSerializer`1 92.1% 87.5%
Fauna.Serialization.QueryArrSerializer 0% 0%
Fauna.Serialization.QueryExprSerializer 77.7% 66.6%
Fauna.Serialization.QueryLiteralSerializer 55.5% 50%
Fauna.Serialization.QueryObjSerializer 69.2% 50%
Fauna.Serialization.QuerySerializer 60% 50%
Fauna.Serialization.QueryValSerializer 69.2% 50%
Fauna.Serialization.RefSerializer 77.7% 70%
Fauna.Serialization.SByteSerializer 92.3% 83.3%
Fauna.Serialization.Serializer 93.2% 83.6%
Fauna.Serialization.ShortSerializer 92.3% 83.3%
Fauna.Serialization.StreamSerializer 57.1% 50%
Fauna.Serialization.StringSerializer 92.8% 87.5%
Fauna.Serialization.UIntSerializer 92.3% 83.3%
Fauna.Serialization.UShortSerializer 92.3% 83.3%
Fauna.Serialization.Utf8FaunaReader 72.6% 77.8%
Fauna.Serialization.Utf8FaunaWriter 90.1%
Fauna.StreamOptions 61.5%
Fauna.Types.BaseDocument 53.3%
Fauna.Types.Document 100%
Fauna.Types.Event`1 85.4% 66.6%
Fauna.Types.Module 52.6% 20%
Fauna.Types.NamedDocument 100%
Fauna.Types.NamedRef 100%
Fauna.Types.NonNullDocument`1 100%
Fauna.Types.NullableDocument`1 100%
Fauna.Types.NullDocument`1 92.3% 75%
Fauna.Types.Page`1 100%
Fauna.Types.Ref 100%
Fauna.Types.Stream 66.6% 35.7%
Fauna.Util.DefaultExpressionSwitch`1 0%
Fauna.Util.Expressions 100% 100%
Fauna.Util.ExpressionSwitch`1 84.8% 75.5%
Fauna.Util.Extensions.PaginationExtensions 100% 100%
Fauna.Util.Extensions.TypeExtensions 93.3% 81.2%

Delta Summary

Generated on: 10/01/2024 - 16:38
Description Previous Current Delta
Coverage date: 10/01/2024 - 16:38 10/01/2024 - 16:38
Tag: main 9b9db3c
Line coverage: 81.5% 81.5% 0.0%
Covered lines: 3260 3234 -26
Coverable lines: 3996 3967 -29
Total lines: 14912 14858 -54
Branch coverage: 70.2% 70.3% 0.1%
Covered branches: 1053 1049 -4
Total branches: 1498 1492 -6
Method coverage: Feature is only available for sponsors

@cynicaljoy cynicaljoy requested a review from jdcryans October 1, 2024 19:33
@cynicaljoy cynicaljoy merged commit 4250505 into main Oct 2, 2024
5 checks passed
@cynicaljoy cynicaljoy deleted the fix-streaming-cpu-usage branch October 2, 2024 13:31
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.

4 participants