/// <summary> /// Put some random content into the store, up to some specified percent of maximum size. /// </summary> public static async Task <IReadOnlyList <ContentHash> > PutRandomAsync ( this IContentSession session, Context context, HashType hashType, bool provideHash, long maxSize, int percent, long fileSize, bool useExactSize ) { long bytesToPopulate = (percent * maxSize) / 100; if (useExactSize) { return(await PutRandomAsync( session, context, hashType, provideHash, (int)(bytesToPopulate / fileSize), fileSize, true).ConfigureAwait(false)); } var contentHashes = new List <ContentHash>(); long populatedSize = 0; while (populatedSize < bytesToPopulate) { var contentSizes = new List <int>(); while (populatedSize < bytesToPopulate && contentSizes.Count < MaxBulkPageSize) { var numBytes = (int)Math.Min(ThreadSafeRandom.Generator.Next((int)fileSize), bytesToPopulate - populatedSize); contentSizes.Add(numBytes); populatedSize += numBytes; } var tasks = new List <Task <PutResult> >(contentSizes.Count); tasks.AddRange( contentSizes.Select( contentSize => session.PutRandomAsync(context.CreateNested(), hashType, provideHash, contentSize, CancellationToken.None))); foreach (var task in tasks) { var result = await task.ConfigureAwait(false); if (result.Succeeded) { contentHashes.Add(result.ContentHash); } } } return(contentHashes); }
public async Task MultipleClients() { using (var testDirectory = new DisposableDirectory(FileSystem)) { var rootPath = testDirectory.Path; var config = CreateStoreConfiguration(); config.Write(FileSystem, rootPath); using (var store = CreateStore(testDirectory, config)) { try { var r = await store.StartupAsync(_context); r.ShouldBeSuccess(); IContentSession session1 = null; IContentSession session2 = null; try { var createSessionResult1 = store.CreateSession(_context, "session1", ImplicitPin.None); createSessionResult1.ShouldBeSuccess(); var createSessionResult2 = store.CreateSession(_context, "session2", ImplicitPin.None); createSessionResult2.ShouldBeSuccess(); using (createSessionResult1.Session) using (createSessionResult2.Session) { var r1 = await createSessionResult1.Session.StartupAsync(_context); r1.ShouldBeSuccess(); session1 = createSessionResult1.Session; var startupSessionResult2 = await createSessionResult2.Session.StartupAsync(_context); startupSessionResult2.ShouldBeSuccess(); session2 = createSessionResult2.Session; var putResult = await session1.PutRandomAsync( _context, ContentHashType, false, RandomContentByteCount, Token); var tasks1 = Enumerable.Range(0, (int)ConnectionsPerSession) .Select(_ => Task.Run(async() => await session1.PinAsync(_context, putResult.ContentHash, Token))) .ToList(); var tasks2 = Enumerable.Range(0, (int)ConnectionsPerSession) .Select(_ => Task.Run(async() => await session2.PinAsync(_context, putResult.ContentHash, Token))) .ToList(); foreach (var task in tasks1.Union(tasks2)) { var result = await task; result.ShouldBeSuccess(); } } } finally { if (session2 != null) { await session2.ShutdownAsync(_context).ShouldBeSuccess(); } if (session1 != null) { await session1.ShutdownAsync(_context).ShouldBeSuccess(); } } } finally { var r = await store.ShutdownAsync(_context); r.ShouldBeSuccess(); } } } }
public async Task MultipleCaches() { const string CacheName1 = "test1"; const string CacheName2 = "test2"; using (var testDirectory0 = new DisposableDirectory(FileSystem)) using (var testDirectory1 = new DisposableDirectory(FileSystem)) using (var testDirectory2 = new DisposableDirectory(FileSystem)) { var config = CreateStoreConfiguration(); var rootPath1 = testDirectory1.Path; config.Write(FileSystem, rootPath1); var rootPath2 = testDirectory2.Path; config.Write(FileSystem, rootPath2); var grpcPort = PortExtensions.GetNextAvailablePort(); var grpcPortFileName = Guid.NewGuid().ToString(); var serviceConfiguration = new ServiceConfiguration( new Dictionary <string, AbsolutePath> { { CacheName1, rootPath1 }, { CacheName2, rootPath2 } }, testDirectory0.Path, ServiceConfiguration.DefaultGracefulShutdownSeconds, grpcPort, grpcPortFileName); using (var server = CreateServer(serviceConfiguration)) { var factory = new MemoryMappedFileGrpcPortSharingFactory(Logger, grpcPortFileName); var reader = factory.GetPortReader(); var port = reader.ReadPort(); var rpcConfig = new ServiceClientRpcConfiguration(port); using (var store1 = new ServiceClientContentStore( Logger, FileSystem, new ServiceClientContentStoreConfiguration(CacheName1, rpcConfig, Scenario))) using (var store2 = new ServiceClientContentStore( Logger, FileSystem, new ServiceClientContentStoreConfiguration(CacheName2, rpcConfig, Scenario))) { try { var rs = await server.StartupAsync(_context); rs.ShouldBeSuccess(); var storeBoolResult1 = await store1.StartupAsync(_context); storeBoolResult1.ShouldBeSuccess(); var storeBoolResult2 = await store2.StartupAsync(_context); storeBoolResult2.ShouldBeSuccess(); IContentSession session1 = null; IContentSession session2 = null; try { var createSessionResult1 = store1.CreateSession(_context, "session1", ImplicitPin.None); createSessionResult1.ShouldBeSuccess(); var createSessionResult2 = store2.CreateSession(_context, "session2", ImplicitPin.None); createSessionResult2.ShouldBeSuccess(); using (createSessionResult1.Session) using (createSessionResult2.Session) { var r1 = await createSessionResult1.Session.StartupAsync(_context); r1.ShouldBeSuccess(); session1 = createSessionResult1.Session; var r2 = await createSessionResult2.Session.StartupAsync(_context); r2.ShouldBeSuccess(); session2 = createSessionResult2.Session; var r3 = await session1.PutRandomAsync( _context, ContentHashType, false, RandomContentByteCount, Token); var pinResult = await session1.PinAsync(_context, r3.ContentHash, Token); pinResult.ShouldBeSuccess(); r3 = await session2.PutRandomAsync( _context, ContentHashType, false, RandomContentByteCount, Token); pinResult = await session2.PinAsync(_context, r3.ContentHash, Token); pinResult.ShouldBeSuccess(); } } finally { if (session2 != null) { await session2.ShutdownAsync(_context).ShouldBeSuccess(); } if (session1 != null) { await session1.ShutdownAsync(_context).ShouldBeSuccess(); } } } finally { BoolResult r1 = null; BoolResult r2 = null; if (store1.StartupCompleted) { r1 = await store1.ShutdownAsync(_context); } if (store2.StartupCompleted) { r2 = await store2.ShutdownAsync(_context); } var r3 = await server.ShutdownAsync(_context); r1?.ShouldBeSuccess(); r2?.ShouldBeSuccess(); r3?.ShouldBeSuccess(); } } } } }
private static async Task <IReadOnlyList <ContentHash> > PutRandomBulkAsync( this IContentSession session, Context context, HashType hashType, bool provideHash, int fileCount, long fileSize, bool useExactSize) { Contract.Requires(fileCount > 0); var c = context.CreateNested(); var tasks = Enumerable.Range(0, fileCount).Select(_ => Task.Run(async() => await session.PutRandomAsync( c, hashType, provideHash, useExactSize ? fileSize : ThreadSafeRandom.Generator.Next(1, (int)fileSize), CancellationToken.None) .ConfigureAwait(false))).ToList(); var contentHashes = new List <ContentHash>(fileCount); foreach (var task in tasks.ToList()) { var result = await task.ConfigureAwait(false); if (result.Succeeded) { contentHashes.Add(result.ContentHash); } } return(contentHashes); }