Google-cloud-dotnet: How can I control the number of sessions per grpc channel in spanner?

Created on 15 Oct 2020  路  5Comments  路  Source: googleapis/google-cloud-dotnet

Hello!

The document says We also recommend having no more than 100 sessions per gRPC channel.
https://cloud.google.com/spanner/docs/sessions?hl=en

I couldn't figure out how to control this using Google.Cloud.Spanner.Data.

Do I just specify MaximumActiveSessions and MaximumGrpcChannels when creating the SpannerConnection?

For example, if I use a SpannerConnection generated as follows, will I be limited to 100 sessions per channel?

var sessionOptions = new SessionPoolOptions
{
    MaximumActiveSessions = 2000,
};

var sessionPoolManager = SessionPoolManager.Create(sessionOptions);

var spannerConnectionStringBuilder = new SpannerConnectionStringBuilder
{
    DataSource = $"MyDataBase",
    MaximumGrpcChannels = 20,
    SessionPoolManager = sessionPoolManager,
};

var spannerConnection = new SpannerConnection(spannerConnectionStringBuilder);

Thank you.

spanner question

All 5 comments

The exact channel allocation process is somewhat tricky. The code you've got there won't guarantee 100 sessions per channel, but it will sort of aim for it. When a CreateSession request is issued, that will be allocated to the least-loaded existing channel... but looking at the code, it looks like that's based on current active requests rather than sessions allocated to that channel.

Now of course, we shouldn't be creating more sessions if there are idle ones (at least once we've reached a reasonably steady state) but it does mean they may not be evenly allocated. Batch allocation of sessions makes this even more complex, unfortunately.

I think the configuration you've specified there is a reasonable one - assuming you really need that many active sessions at all, of course - but it's not a guarantee. You could increase the maximum number of gRPC channels (e.g. to 30 or 40) to reduce the average number per channel, potentially.

@skuruppu and @amanda-tarafa please review what I've written above, just in case you're aware of anything I'm not...

I would only add that you can also use SessionPoolOptions.CreateSessionMaximumBatchSize to control the size of the batch when batch allocating sessions, which means that less sessions will be bound to a channel _at once_ and that should help reduce the average number of sessions per channel. It defaults to 5, which compared to your MaximumActiveSessions of 2000 value is on the small side though, so there might not be so much improvement when changing it.

Your clear explanations have helped me to understand.
Thank you for your quick reply!

@y-nishine: Great! In that case, I'll close this issue for now... but if you have further questions on this topic, or experience any difficulties, please add another comment and we can reopen it.

Was this page helpful?
0 / 5 - 0 ratings