Example #1
0
        public void Index(Block block)
        {
            var hash = block.GetHash().ToString();

            using (IndexerTrace.NewCorrelation("Upload of " + hash).Open())
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                bool failedBefore = false;
                while (true)
                {
                    try
                    {
                        var container = Configuration.GetBlocksContainer();
                        var client    = container.ServiceClient;
                        client.DefaultRequestOptions.SingleBlobUploadThresholdInBytes = 32 * 1024 * 1024;
                        var          blob = container.GetPageBlobReference(hash);
                        MemoryStream ms   = new MemoryStream();
                        block.ReadWrite(ms, true);
                        var blockBytes = ms.GetBuffer();

                        long length = 512 - (ms.Length % 512);
                        if (length == 512)
                        {
                            length = 0;
                        }
                        Array.Resize(ref blockBytes, (int)(ms.Length + length));

                        try
                        {
                            blob.UploadFromByteArray(blockBytes, 0, blockBytes.Length, new AccessCondition()
                            {
                                //Will throw if already exist, save 1 call
                                IfNotModifiedSinceTime = failedBefore ? (DateTimeOffset?)null : DateTimeOffset.MinValue
                            }, new BlobRequestOptions()
                            {
                                MaximumExecutionTime = _Timeout,
                                ServerTimeout        = _Timeout
                            });
                            watch.Stop();
                            IndexerTrace.BlockUploaded(watch.Elapsed, blockBytes.Length);
                            break;
                        }
                        catch (StorageException ex)
                        {
                            var alreadyExist = ex.RequestInformation != null && ex.RequestInformation.HttpStatusCode == 412;
                            if (!alreadyExist)
                            {
                                throw;
                            }
                            watch.Stop();
                            IndexerTrace.BlockAlreadyUploaded();
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        IndexerTrace.ErrorWhileImportingBlockToAzure(new uint256(hash), ex);
                        failedBefore = true;
                        Thread.Sleep(5000);
                    }
                }
            }
        }