-
Notifications
You must be signed in to change notification settings - Fork 27
feat: add EnableImplicitSession support #512
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
Closed
LiamHamsters
wants to merge
55
commits into
ydb-platform:main
from
LiamHamsters:add-implicit-session-flag
Closed
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
a0927a4
feat: add EnableImplicitSession flag support with parsing tests
LiamHamsters f18fbf7
feat: add EnableImplicitSession flag support with parsing tests
LiamHamsters 5c7cfc9
resolve conflict
LiamHamsters 5c9ec84
Merge remote-tracking branch 'origin/add-implicit-session-flag' into …
LiamHamsters eab1450
fix ci
LiamHamsters 72c53f3
feat: add integration tests and rework implicit session handling via …
LiamHamsters 7c35439
`Warning` -> `Debug` on DeleteSession has been `RpcException` & fixes…
KirillKurdyukov 4dfa0bd
Feat: Implement `YdbRetryPolicy` with AWS-inspired Exponential Backof…
KirillKurdyukov c44750b
feat: added support new datetime types (#517)
KirillKurdyukov a19f8bb
feat: enforce DECIMAL(p,s) overflow in parameters (#519)
KirillKurdyukov 4ac1c01
Feat ADO.NET: cache gRPC transport by `gRPCConnectionString` to reuse…
KirillKurdyukov f7da918
feat: add EnableImplicitSession flag support with parsing tests
LiamHamsters 812a077
feat: add EnableImplicitSession flag support with parsing tests
LiamHamsters 0df1155
Merge branch 'main' into add-implicit-session-flag
LiamHamsters 74eac7d
feat: update ImplicitSession for singleton driver
LiamHamsters c57c3d6
try fix DisableParallelization in PMTests and autoformat
LiamHamsters a5154b5
fix lint
LiamHamsters 3addeae
fix: keep single command session; make ImplicitSessionSource dispose-…
LiamHamsters 0c53efb
Move implicit session creation to PoolManager by flag
LiamHamsters 46b9ca3
test: validate implicit session disallows transactions but supports n…
LiamHamsters c6d6a59
fix lint
LiamHamsters 741e28b
feat(ado): add ImplicitSession with PoolManager integration and stres…
LiamHamsters 4253895
Merge branch 'main' into add-implicit-session-flag
LiamHamsters 09dd895
fix lint
LiamHamsters b8db711
fix
LiamHamsters fad0f85
hot fix
LiamHamsters 1c2d3ba
hot fix
LiamHamsters 04050d6
feat: add owner registration and dispose logic for implicit sessions
LiamHamsters 274a0e3
refactor(ado): remove onEmpty callback from ImplicitSessionSource
LiamHamsters 80ac532
hot fix
LiamHamsters 275d874
refactor
LiamHamsters 04a31a6
delete onEmpty in stressTest
LiamHamsters f0c9bff
Refactored implicit session handling and stress tests
LiamHamsters b8feb70
Merge branch 'main' into add-implicit-session-flag
LiamHamsters 4075173
feat: add EnableImplicitSession flag support with parsing tests
LiamHamsters bdfdd11
fix ci
LiamHamsters eb6fcb8
feat: add integration tests and rework implicit session handling via …
LiamHamsters 991a412
feat: update ImplicitSession for singleton driver
LiamHamsters 04f41b0
test
LiamHamsters ed0de42
test
LiamHamsters f6c2ff3
Merge branch 'main' into add-implicit-session-flag
LiamHamsters ef47d68
test
LiamHamsters 0d16841
Revert "test"
LiamHamsters d058f92
test
LiamHamsters c4158c2
test
LiamHamsters 622b48e
test
LiamHamsters e748a85
fix lint
LiamHamsters 08e6bc2
tests: add double OpenSession check in stress tests;
LiamHamsters 448244f
try fix
LiamHamsters e001a79
test
LiamHamsters ad4f8c2
Merge branch 'main' into add-implicit-session-flag
LiamHamsters ff61b60
made dispose two-phase
LiamHamsters dd0ba06
Merge remote-tracking branch 'origin/add-implicit-session-flag' into …
LiamHamsters 6852546
tried to make a “two-phase”
LiamHamsters 401e7d2
try
LiamHamsters File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
namespace Ydb.Sdk.Ado.Session; | ||
|
||
internal sealed class ImplicitSessionSource : ISessionSource | ||
{ | ||
private readonly IDriver _driver; | ||
private readonly ManualResetEventSlim _allReleased = new(false); | ||
|
||
private int _state; | ||
private int _activeLeaseCount; | ||
|
||
internal ImplicitSessionSource(IDriver driver) | ||
{ | ||
_driver = driver; | ||
} | ||
|
||
public ValueTask<ISession> OpenSession(CancellationToken cancellationToken) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
if (!TryAcquireLease()) | ||
throw new ObjectDisposedException(nameof(ImplicitSessionSource)); | ||
|
||
return new ValueTask<ISession>(new ImplicitSession(_driver, this)); | ||
} | ||
|
||
private bool TryAcquireLease() | ||
{ | ||
if (Volatile.Read(ref _state) == 2) | ||
return false; | ||
|
||
var newCount = Interlocked.Increment(ref _activeLeaseCount); | ||
|
||
var state = Volatile.Read(ref _state); | ||
|
||
if (state == 2 || (state == 1 && newCount == 1)) | ||
{ | ||
Interlocked.Decrement(ref _activeLeaseCount); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
internal void ReleaseLease() | ||
{ | ||
if (Interlocked.Decrement(ref _activeLeaseCount) == 0 && | ||
Volatile.Read(ref _state) != 0) | ||
{ | ||
_allReleased.Set(); | ||
} | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (Interlocked.CompareExchange(ref _state, 1, 0) != 0) | ||
return; | ||
|
||
if (Volatile.Read(ref _activeLeaseCount) != 0) | ||
_allReleased.Wait(); | ||
|
||
try | ||
{ | ||
Volatile.Write(ref _state, 2); | ||
await _driver.DisposeAsync(); | ||
} | ||
finally | ||
{ | ||
_allReleased.Dispose(); | ||
} | ||
} | ||
LiamHamsters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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
154 changes: 154 additions & 0 deletions
154
src/Ydb.Sdk/test/Ydb.Sdk.Ado.Tests/Session/YdbImplicitStressTests.cs
This file contains hidden or 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,154 @@ | ||
using Moq; | ||
using Xunit; | ||
using Ydb.Sdk.Ado.Session; | ||
|
||
namespace Ydb.Sdk.Ado.Tests.Session; | ||
|
||
public class YdbImplicitStressTests : TestBase | ||
{ | ||
private static IDriver DummyDriver() | ||
{ | ||
var m = new Mock<IDriver>(MockBehavior.Loose); | ||
m.Setup(d => d.DisposeAsync()).Returns(ValueTask.CompletedTask); | ||
return m.Object; | ||
} | ||
LiamHamsters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private sealed class Counter | ||
{ | ||
public int Value; | ||
public void Inc() => Interlocked.Increment(ref Value); | ||
} | ||
|
||
[Fact(Timeout = 30_000)] | ||
public async Task Dispose_WaitsForAllLeases_AndSignalsOnEmptyExactlyOnce() | ||
LiamHamsters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var driver = DummyDriver(); | ||
|
||
var opened = new Counter(); | ||
var closed = new Counter(); | ||
|
||
var source = new ImplicitSessionSource(driver); | ||
|
||
var workers = Enumerable.Range(0, 200).Select(async _ => | ||
{ | ||
var rnd = Random.Shared; | ||
for (var j = 0; j < 10; j++) | ||
{ | ||
ISession s; | ||
try | ||
{ | ||
s = await source.OpenSession(CancellationToken.None); | ||
opened.Inc(); | ||
|
||
await Task.Delay(rnd.Next(0, 5)); | ||
} | ||
catch (ObjectDisposedException) | ||
{ | ||
return; | ||
} | ||
|
||
var s2 = await source.OpenSession(CancellationToken.None); | ||
s2.Dispose(); | ||
|
||
s.Dispose(); | ||
closed.Inc(); | ||
} | ||
}).ToArray(); | ||
|
||
var disposer = Task.Run(async () => | ||
{ | ||
await Task.Delay(10); | ||
await source.DisposeAsync(); | ||
}); | ||
|
||
await Task.WhenAll(workers.Append(disposer)); | ||
|
||
Assert.True(opened.Value > 0); | ||
Assert.Equal(opened.Value, closed.Value); | ||
|
||
await Assert.ThrowsAsync<ObjectDisposedException>(() => source.OpenSession(CancellationToken.None).AsTask()); | ||
} | ||
|
||
[Fact(Timeout = 30_000)] | ||
public async Task Stress_Counts_AreBalanced() | ||
{ | ||
var driver = DummyDriver(); | ||
|
||
var opened = new Counter(); | ||
var closed = new Counter(); | ||
|
||
var source = new ImplicitSessionSource(driver); | ||
|
||
var workers = Enumerable.Range(0, 200).Select(async _ => | ||
{ | ||
var rnd = Random.Shared; | ||
for (var j = 0; j < 10; j++) | ||
{ | ||
ISession s; | ||
try | ||
{ | ||
s = await source.OpenSession(CancellationToken.None); | ||
opened.Inc(); | ||
|
||
await Task.Delay(rnd.Next(0, 3)); | ||
} | ||
catch (ObjectDisposedException) | ||
{ | ||
return; | ||
} | ||
|
||
var s2 = await source.OpenSession(CancellationToken.None); | ||
s2.Dispose(); | ||
|
||
s.Dispose(); | ||
closed.Inc(); | ||
} | ||
}).ToArray(); | ||
|
||
var disposer = Task.Run(async () => await source.DisposeAsync()); | ||
|
||
await Task.WhenAll(workers.Append(disposer)); | ||
|
||
Assert.Equal(opened.Value, closed.Value); | ||
Assert.True(opened.Value > 0); | ||
|
||
await Assert.ThrowsAsync<ObjectDisposedException>(() => source.OpenSession(CancellationToken.None).AsTask()); | ||
} | ||
|
||
[Fact(Timeout = 30_000)] | ||
public async Task Open_RacingWithDispose_StateRemainsConsistent() | ||
{ | ||
var driver = DummyDriver(); | ||
|
||
var source = new ImplicitSessionSource(driver); | ||
|
||
var opens = Enumerable.Range(0, 1000).Select(async _ => | ||
{ | ||
ISession s; | ||
try | ||
{ | ||
s = await source.OpenSession(CancellationToken.None); | ||
} | ||
catch (ObjectDisposedException) | ||
{ | ||
return 0; | ||
} | ||
|
||
var s2 = await source.OpenSession(CancellationToken.None); | ||
s2.Dispose(); | ||
|
||
s.Dispose(); | ||
return 1; | ||
}).ToArray(); | ||
|
||
var disposeTask = Task.Run(async () => | ||
{ | ||
await Task.Yield(); | ||
await source.DisposeAsync(); | ||
}); | ||
|
||
await Task.WhenAll(opens.Append(disposeTask)); | ||
|
||
await Assert.ThrowsAsync<ObjectDisposedException>(() => source.OpenSession(CancellationToken.None).AsTask()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.