-
-
Save mmnosek/02bd09b2a682baae9b93072be9cdff08 to your computer and use it in GitHub Desktop.
Multi-thread transaction Percona K8s Operator
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
FROM mcr.microsoft.com/dotnet/aspnet:6.0 | |
COPY bin/Release/net6.0/publish/ App/ | |
WORKDIR /App | |
ENTRYPOINT ["dotnet", "NetCore.Docker.dll"] |
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
using System; | |
using System.Threading.Tasks; | |
using MongoDB.Bson; | |
using MongoDB.Driver; | |
namespace NetCore.Docker | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var transactionTest = new TransactionTest(); | |
await transactionTest.TestTransactionAsync(); | |
} | |
} | |
} | |
public class TransactionTest | |
{ | |
private const string DatabaseName = "Test"; | |
private const string CollectionName = "Test"; | |
public const string ConnectionString = "mongodb://userAdmin:k1GQz2qs8nvWvj658T@my-cluster-name-mongos.default.svc.cluster.local/admin?ssl=false"; | |
public MongoClient GetMongoClient(int timeout = 5) | |
{ | |
var clientSettings = MongoClientSettings.FromConnectionString(ConnectionString); | |
clientSettings.ConnectTimeout = TimeSpan.FromSeconds(5); | |
clientSettings.ServerSelectionTimeout = TimeSpan.FromSeconds(timeout); | |
clientSettings.AllowInsecureTls = true; | |
var mongoClient = new MongoClient(clientSettings); | |
return mongoClient; | |
} | |
public async Task TestTransactionAsync() | |
{ | |
var client = GetMongoClient(); | |
var tasks = new List<Task>(); | |
for (int i = 0; i < 5; ++i) | |
{ | |
//uncomment below to get a new MongoClient instance for each execution | |
//var client = GetMongoClient(i + 5); | |
tasks.Add(DoAsync(client)); | |
} | |
await Task.WhenAll(tasks); | |
} | |
private async Task DoAsync(IMongoClient mongoClient) | |
{ | |
Console.WriteLine("Client hashcode: " + mongoClient.GetHashCode()); | |
var collection = mongoClient.GetDatabase(DatabaseName).GetCollection<BsonDocument>(CollectionName); | |
while (true) | |
{ | |
var uuid1 = Guid.NewGuid().ToString("N").Substring(24); | |
var uuid2 = Guid.NewGuid().ToString("N").Substring(24); | |
try | |
{ | |
using (var session = await mongoClient.StartSessionAsync()) | |
{ | |
session.StartTransaction(); | |
await collection.InsertOneAsync(session, new BsonDocument("Uuid", uuid1)); | |
await collection.InsertOneAsync(session, new BsonDocument("Uuid", uuid2)); | |
await session.CommitTransactionAsync(); | |
} | |
Console.WriteLine($"[{uuid1}] [{uuid2}]"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("$$$ " + e.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment