public void EnumerateTransfersCanBeStopped()
        {
            var initialJobResponse = Stubs.BuildJobResponse(
                Stubs.Chunk1(null, false, false),
                Stubs.Chunk2(null, false, false),
                Stubs.Chunk3(null, false, false)
                );
            var factory = new Mock <IDs3ClientFactory>(MockBehavior.Strict);

            factory
            .Setup(f => f.GetClientForNodeId(It.IsAny <Guid?>()))
            .Returns(new Mock <IDs3Client>(MockBehavior.Strict).Object);
            var client = new Mock <IDs3Client>(MockBehavior.Strict);

            client
            .Setup(c => c.BuildFactory(It.IsAny <IEnumerable <Node> >()))
            .Returns(factory.Object);
            client
            .Setup(c => c.GetAvailableJobChunks(AvailableChunks(Stubs.JobId)))
            .Returns(GetAvailableJobChunksResponse.Success(
                         TimeSpan.FromMinutes(5),
                         Stubs.BuildJobResponse(Stubs.Chunk1(Stubs.NodeId1, true, true))
                         ));

            var transferItemSource = new ReadTransferItemSource(_ => {}, client.Object, initialJobResponse);

            var readyToStop = new ManualResetEventSlim();
            var task        = Task.Run(() =>
            {
                readyToStop.Wait();
                Thread.Sleep(130);
                transferItemSource.Stop();
            });

            using (var transfers = transferItemSource.EnumerateAvailableTransfers().GetEnumerator())
            {
                for (int i = 0; i < 2; i++)
                {
                    Assert.True(transfers.MoveNext());
                }
                readyToStop.Set();
                var timer = Stopwatch.StartNew();
                Assert.False(transfers.MoveNext());
                timer.Stop();
                Assert.GreaterOrEqual(timer.Elapsed, TimeSpan.FromMilliseconds(100));
            }
            task.Wait();
        }
Exemplo n.º 2
0
        public void BasicReadTransfer()
        {
            var initialJobResponse = Stubs.BuildJobResponse(
                Stubs.Chunk1(null, false, false),
                Stubs.Chunk2(null, false, false),
                Stubs.Chunk3(null, false, false)
                );
            var availableJobResponse = Stubs.BuildJobResponse(
                Stubs.Chunk1(Stubs.NodeId2, true, true),
                Stubs.Chunk2(Stubs.NodeId2, true, true),
                Stubs.Chunk3(Stubs.NodeId1, true, true)
                );

            var node1Client = new Mock <IDs3Client>(MockBehavior.Strict);

            SetupGetObject(node1Client, "hello", 0L, "ABCDefGHIJ");
            SetupGetObject(node1Client, "bar", 35L, "zABCDEFGHIJ");

            var node2Client = new Mock <IDs3Client>(MockBehavior.Strict);

            SetupGetObject(node2Client, "bar", 0L, "0123456789abcde");
            SetupGetObject(node2Client, "foo", 10L, "klmnopqrst");
            SetupGetObject(node2Client, "foo", 0L, "abcdefghij");
            SetupGetObject(node2Client, "bar", 15L, "fghijklmnopqrstuvwxy");

            var clientFactory = new Mock <IDs3ClientFactory>(MockBehavior.Strict);

            clientFactory
            .Setup(cf => cf.GetClientForNodeId(Stubs.NodeId1))
            .Returns(node1Client.Object);
            clientFactory
            .Setup(cf => cf.GetClientForNodeId(Stubs.NodeId2))
            .Returns(node2Client.Object);

            var client = new Mock <IDs3Client>(MockBehavior.Strict);

            client
            .Setup(c => c.BuildFactory(Stubs.Nodes))
            .Returns(clientFactory.Object);
            client
            .Setup(c => c.BulkGet(ItIsBulkGetRequest(
                                      Stubs.BucketName,
                                      ChunkOrdering.None,
                                      Stubs.ObjectNames,
                                      Enumerable.Empty <Ds3PartialObject>()
                                      )))
            .Returns(initialJobResponse);
            client
            .Setup(c => c.GetAvailableJobChunks(ItIsGetAvailableJobChunksRequest(Stubs.JobId)))
            .Returns(GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(1), availableJobResponse));

            var job = new Ds3ClientHelpers(client.Object).StartReadJob(
                Stubs.BucketName,
                Stubs.ObjectNames.Select(name => new Ds3Object(name, null))
                );

            var dataTransfers  = new ConcurrentQueue <long>();
            var itemsCompleted = new ConcurrentQueue <string>();

            job.DataTransferred += dataTransfers.Enqueue;
            job.ItemCompleted   += itemsCompleted.Enqueue;

            var streams = new ConcurrentDictionary <string, MockStream>();

            job.Transfer(key => streams.GetOrAdd(key, k => new MockStream()));

            node1Client.VerifyAll();
            node2Client.VerifyAll();
            clientFactory.VerifyAll();
            client.VerifyAll();

            CollectionAssert.AreEqual(
                new[]
            {
                new { Key = "bar", Value = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJ" },
                new { Key = "foo", Value = "abcdefghijklmnopqrst" },
                new { Key = "hello", Value = "ABCDefGHIJ" },
            },
                from item in streams
                orderby item.Key
                select new { item.Key, Value = _encoding.GetString(item.Value.Result) }
                );
            CollectionAssert.AreEquivalent(new[] { 15L, 20L, 11L, 10L, 10L, 10L }, dataTransfers);
            CollectionAssert.AreEquivalent(Stubs.ObjectNames, itemsCompleted);
        }
Exemplo n.º 3
0
        public void ReadTransferFailsUponTransferrerException()
        {
            var initialJobResponse = Stubs.BuildJobResponse(
                Stubs.Chunk1(null, false, false),
                Stubs.Chunk2(null, false, false),
                Stubs.Chunk3(null, false, false)
                );
            var availableJobResponse = Stubs.BuildJobResponse(
                Stubs.Chunk2(Stubs.NodeId2, true, true)
                );

            var node2Client = new Mock <IDs3Client>(MockBehavior.Strict);

            SetupGetObject(node2Client, "foo", 0L, "abcdefghij");
            node2Client
            .Setup(c => c.GetObject(ItIsGetObjectRequest(
                                        Stubs.BucketName,
                                        "bar",
                                        Stubs.JobId,
                                        15L,
                                        Enumerable.Empty <Range>()
                                        )))
            .Throws <NullReferenceException>();

            var clientFactory = new Mock <IDs3ClientFactory>(MockBehavior.Strict);

            clientFactory
            .Setup(cf => cf.GetClientForNodeId(Stubs.NodeId2))
            .Returns(node2Client.Object);

            var client = new Mock <IDs3Client>(MockBehavior.Strict);

            client
            .Setup(c => c.BuildFactory(Stubs.Nodes))
            .Returns(clientFactory.Object);
            client
            .Setup(c => c.BulkGet(ItIsBulkGetRequest(
                                      Stubs.BucketName,
                                      ChunkOrdering.None,
                                      Stubs.ObjectNames,
                                      Enumerable.Empty <Ds3PartialObject>()
                                      )))
            .Returns(initialJobResponse);
            client
            .Setup(c => c.GetAvailableJobChunks(ItIsGetAvailableJobChunksRequest(Stubs.JobId)))
            .Returns(GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(1), availableJobResponse));

            var job = new Ds3ClientHelpers(client.Object).StartReadJob(
                Stubs.BucketName,
                Stubs.ObjectNames.Select(name => new Ds3Object(name, null))
                );

            try
            {
                job.Transfer(key => new MockStream());
                Assert.Fail("Should have thrown an exception.");
            }
            catch (AggregateException e)
            {
                Assert.IsInstanceOf <NullReferenceException>(e.InnerException);
            }
        }
Exemplo n.º 4
0
        public void PartialReadTransfer()
        {
            var partialObjects = new[]
            {
                new Ds3PartialObject(Range.ByLength(0L, 4L), "foo"),
                new Ds3PartialObject(Range.ByLength(6L, 10L), "foo"),
                new Ds3PartialObject(Range.ByLength(18L, 1L), "foo"),
                new Ds3PartialObject(Range.ByLength(10L, 26L), "bar"),
            };
            var fullObjects = new[] { "hello" };

            var initialJobResponse = Stubs.BuildJobResponse(
                Stubs.Chunk1(null, false, false),
                Stubs.Chunk2(null, false, false),
                Stubs.Chunk3(null, false, false)
                );
            var availableJobResponse = Stubs.BuildJobResponse(
                Stubs.Chunk1(Stubs.NodeId2, true, true),
                Stubs.Chunk2(Stubs.NodeId2, true, true),
                Stubs.Chunk3(Stubs.NodeId1, true, true)
                );

            var node1Client = new Mock <IDs3Client>(MockBehavior.Strict);

            SetupGetObject(node1Client, "hello", 0L, "ABCDefGHIJ", Range.ByLength(0L, 10L));
            SetupGetObject(node1Client, "bar", 35L, "z", Range.ByLength(35L, 1L));

            var node2Client = new Mock <IDs3Client>(MockBehavior.Strict);

            SetupGetObject(node2Client, "bar", 0L, "abcde", Range.ByLength(10L, 5L));
            SetupGetObject(node2Client, "foo", 10L, "klmnop!", Range.ByLength(10L, 6L), Range.ByLength(18L, 1L));
            SetupGetObject(node2Client, "foo", 0L, "abcdghij", Range.ByLength(0L, 4L), Range.ByLength(6L, 4L));
            SetupGetObject(node2Client, "bar", 15L, "fghijklmnopqrstuvwxy", Range.ByLength(15L, 20L));

            var clientFactory = new Mock <IDs3ClientFactory>(MockBehavior.Strict);

            clientFactory
            .Setup(cf => cf.GetClientForNodeId(Stubs.NodeId1))
            .Returns(node1Client.Object);
            clientFactory
            .Setup(cf => cf.GetClientForNodeId(Stubs.NodeId2))
            .Returns(node2Client.Object);

            var client = new Mock <IDs3Client>(MockBehavior.Strict);

            client
            .Setup(c => c.BuildFactory(Stubs.Nodes))
            .Returns(clientFactory.Object);
            client
            .Setup(c => c.BulkGet(ItIsBulkGetRequest(
                                      Stubs.BucketName,
                                      ChunkOrdering.None,
                                      fullObjects,
                                      partialObjects
                                      )))
            .Returns(initialJobResponse);
            client
            .Setup(c => c.GetAvailableJobChunks(ItIsGetAvailableJobChunksRequest(Stubs.JobId)))
            .Returns(GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(1), availableJobResponse));

            var job = new Ds3ClientHelpers(client.Object)
                      .StartPartialReadJob(Stubs.BucketName, fullObjects, partialObjects);

            CollectionAssert.AreEquivalent(
                partialObjects.Concat(new[] { new Ds3PartialObject(Range.ByLength(0L, 10L), "hello") }),
                job.AllItems
                );

            var dataTransfers  = new ConcurrentQueue <long>();
            var itemsCompleted = new ConcurrentQueue <Ds3PartialObject>();

            job.DataTransferred += dataTransfers.Enqueue;
            job.ItemCompleted   += itemsCompleted.Enqueue;

            var streams = new ConcurrentDictionary <Ds3PartialObject, MockStream>();

            job.Transfer(key => streams.GetOrAdd(key, k => new MockStream()));

            node1Client.VerifyAll();
            node2Client.VerifyAll();
            clientFactory.VerifyAll();
            client.VerifyAll();

            var fullObjectPart = new Ds3PartialObject(Range.ByLength(0L, 10L), fullObjects[0]);

            CollectionAssert.AreEqual(
                new[]
            {
                new { Key = partialObjects[0], Value = "abcd" },
                new { Key = partialObjects[1], Value = "ghijklmnop" },
                new { Key = partialObjects[2], Value = "!" },
                new { Key = partialObjects[3], Value = "abcdefghijklmnopqrstuvwxyz" },
                new { Key = fullObjectPart, Value = "ABCDefGHIJ" },
            }.OrderBy(it => it.Key).ToArray(),
                (
                    from item in streams
                    orderby item.Key
                    select new { item.Key, Value = _encoding.GetString(item.Value.Result) }
                ).ToArray()
                );
            CollectionAssert.AreEquivalent(
                new[] { 1L, 1L, 4L, 4L, 5L, 6L, 10L, 20L },
                dataTransfers.Sorted().ToArray()
                );
            CollectionAssert.AreEquivalent(partialObjects.Concat(new[] { fullObjectPart }), itemsCompleted);
        }
        public void EnumerateTransfersStreamsNewlyAvailableTransferItems()
        {
            var jobResponse1 = Stubs.BuildJobResponse(
                Stubs.Chunk1(null, false, false),
                Stubs.Chunk2(null, false, false),
                Stubs.Chunk3(null, false, false)
                );
            var jobResponse2 = Stubs.BuildJobResponse(
                Stubs.Chunk2(Stubs.NodeId2, true, true)
                );
            var jobResponse3 = Stubs.BuildJobResponse(
                Stubs.Chunk2(Stubs.NodeId2, true, true),
                Stubs.Chunk1(Stubs.NodeId1, true, true)
                );
            var jobResponse4 = Stubs.BuildJobResponse(
                Stubs.Chunk3(Stubs.NodeId2, true, true)
                );

            var node1Client = new Mock <IDs3Client>(MockBehavior.Strict).Object;
            var node2Client = new Mock <IDs3Client>(MockBehavior.Strict).Object;

            var clientFactory = new Mock <IDs3ClientFactory>(MockBehavior.Strict);

            clientFactory.Setup(cf => cf.GetClientForNodeId(Stubs.NodeId1)).Returns(node1Client);
            clientFactory.Setup(cf => cf.GetClientForNodeId(Stubs.NodeId2)).Returns(node2Client);

            var actionSequence = Queue(new { Item = (object)null, Type = "" });

            var client = new Mock <IDs3Client>(MockBehavior.Strict);

            client.Setup(c => c.BuildFactory(Stubs.Nodes)).Returns(clientFactory.Object);
            var chunkResponses = new[]
            {
                GetAvailableJobChunksResponse.RetryAfter(TimeSpan.FromMinutes(5)),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(11), jobResponse2),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(8), jobResponse2),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(11), jobResponse3),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(7), jobResponse3),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(6), jobResponse3),
                GetAvailableJobChunksResponse.RetryAfter(TimeSpan.FromMinutes(4)),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(11), jobResponse4)
            };
            var chunkResponseQueue = new Queue <GetAvailableJobChunksResponse>(chunkResponses);

            client
            .Setup(c => c.GetAvailableJobChunks(AvailableChunks(Stubs.JobId)))
            .Returns(() =>
            {
                var r = chunkResponseQueue.Dequeue();
                actionSequence.Enqueue(new { Item = (object)r, Type = "Allocated" });
                return(r);
            });

            var sleeps = new List <TimeSpan>();

            var source = new ReadTransferItemSource(sleeps.Add, client.Object, jobResponse1);

            var blobs = new[]
            {
                new Blob(Range.ByLength(0, 10), "foo"),
                new Blob(Range.ByLength(15, 20), "bar"),
                new Blob(Range.ByLength(0, 15), "bar"),
                new Blob(Range.ByLength(10, 10), "foo"),
                new Blob(Range.ByLength(0, 10), "hello"),
                new Blob(Range.ByLength(35, 11), "bar")
            };
            var producerConsumer  = new ProducerConsumer(1);
            var completeBlobsTask = Task.Run(() =>
            {
                for (int i = 0; i < blobs.Length; i++)
                {
                    producerConsumer.Read(() =>
                                          actionSequence.Enqueue(new { Item = (object)blobs[i], Type = "Completed" })
                                          );
                    source.CompleteBlob(blobs[i]);
                }
            });

            CollectionAssert.AreEqual(
                new[]
            {
                new TransferItem(node2Client, blobs[0]),
                new TransferItem(node2Client, blobs[1]),
                new TransferItem(node1Client, blobs[2]),
                new TransferItem(node1Client, blobs[3]),
                new TransferItem(node2Client, blobs[4]),
                new TransferItem(node2Client, blobs[5]),
            },
                source
                .EnumerateAvailableTransfers()
                .Select(ti =>
            {
                producerConsumer.Write(() =>
                                       actionSequence.Enqueue(new { Item = (object)ti.Blob, Type = "Returned" })
                                       );
                return(ti);
            })
                .ToArray(),
                new TransferItemSourceHelpers.TransferItemComparer()
                );
            completeBlobsTask.Wait();
            var results = actionSequence.ToArray();

            CollectionAssert.AreEqual(
                new[]
            {
                new { Item = (object)chunkResponses[0], Type = "Allocated" },
                new { Item = (object)chunkResponses[1], Type = "Allocated" },
                new { Item = (object)blobs[0], Type = "Returned" },
                new { Item = (object)blobs[0], Type = "Completed" },
                new { Item = (object)blobs[1], Type = "Returned" },
                new { Item = (object)blobs[1], Type = "Completed" },
                new { Item = (object)chunkResponses[2], Type = "Allocated" },
                new { Item = (object)chunkResponses[3], Type = "Allocated" },
                new { Item = (object)blobs[2], Type = "Returned" },
                new { Item = (object)blobs[2], Type = "Completed" },
                new { Item = (object)blobs[3], Type = "Returned" },
                new { Item = (object)blobs[3], Type = "Completed" },
                new { Item = (object)chunkResponses[4], Type = "Allocated" },
                new { Item = (object)chunkResponses[5], Type = "Allocated" },
                new { Item = (object)chunkResponses[6], Type = "Allocated" },
                new { Item = (object)chunkResponses[7], Type = "Allocated" },
                new { Item = (object)blobs[4], Type = "Returned" },
                new { Item = (object)blobs[4], Type = "Completed" },
                new { Item = (object)blobs[5], Type = "Returned" },
                new { Item = (object)blobs[5], Type = "Completed" },
            },
                results
                );
            CollectionAssert.AreEqual(
                new[]
            {
                TimeSpan.FromMinutes(5),
                TimeSpan.FromMinutes(8),
                TimeSpan.FromMinutes(7),
                TimeSpan.FromMinutes(6),
                TimeSpan.FromMinutes(4)
            },
                sleeps
                );

            clientFactory.VerifyAll();
            client.VerifyAll();
        }