public AgentDynamicConfiguration GetConfigurationData(AgentIdentity idData)
        {
            var cnf = this.config;

            return(new AgentDynamicConfiguration
            {
                Token = config,
                StreamFactory = () => new MemoryStream(Encoding.UTF8.GetBytes(cnf)),
            });
        }
예제 #2
0
        public async Task <AddAddPacketPartResult> AddIfNotExistsPacketPartAsync(AgentIdentity agentId, AddPacketPartRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            var leaseResult = this.dbClusterService.RenewLeaseReservations();

            this.ThrowIfNotValid(leaseResult);
            // выбираем бд RoundRobin-ом
            var store = this.GetPacketsStore(leaseResult.ReservedServices);

            return(await store.AddIfNotExistsPacketPartAsync(agentId, request, cancellationToken));
        }
예제 #3
0
 private AddPacketPartRequest CreateAddPacketRequest(AgentIdentity agentIdentifierData, PacketFormDataItem item, byte[] bytes)
 {
     return(new AddPacketPartRequest
     {
         PacketId = item.PacketId,
         ProviderKey = item.ProviderKey,
         StartPosition = item.StartPosition,
         EndPosition = item.EndPosition,
         IsFinal = item.IsFinal,
         Bytes = bytes,
         PreviousPartStorageToken = item.PreviousPartStorageToken,
         PreviousPartId = item.PreviousPartId,
         //AgentId = agentIdentifierData,
     });
 }
예제 #4
0
        public async Task <AddAddPacketPartResult> AddIfNotExistsPacketPartAsync(AgentIdentity agentId, AddPacketPartRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ArgumentException("request");
            }

            if (!this.initialized)
            {
                await this.InitAsync(cancellationToken);
            }

            var doc = this.Get(agentId, request);

            try
            {
                await this.chunks.InsertOneAsync(doc, null, cancellationToken);

                return(AddAddPacketPartResult.SuccessResult(request, this.storageToken, doc.Id));
            }
            catch (MongoWriteException ex)
            {
                // Instrumentation ???
                var nonDupKeyExceptions = ex.WriteError.Category != ServerErrorCategory.DuplicateKey;
                if (nonDupKeyExceptions)
                {
                    return(AddAddPacketPartResult.FailResult(request));
                }

                // пока не удастся получить id для документа который зафейлился из exception-а
                // https://jira.mongodb.org/browse/SERVER-4637
                var id = await this.chunks.Find(x => x.PacketId == doc.PacketId && x.StartPosition == doc.StartPosition).Project(x => x.Id).FirstOrDefaultAsync(cancellationToken);

                if (id == null)
                {
                    return(AddAddPacketPartResult.FailResult(request));
                }
                return(AddAddPacketPartResult.SuccessResult(request, this.storageToken, id));
            }
        }
예제 #5
0
        private PacketPart Get(AgentIdentity agentId, AddPacketPartRequest request)
        {
            var now = DateTime.UtcNow;

            return(new PacketPart
            {
                ProviderKey = request.ProviderKey,
                PacketId = request.PacketId,
                StartPosition = request.StartPosition,
                EndPosition = request.EndPosition,
                Bytes = request.Bytes,
                // TODO надо бы переделать на время сервера БД
                CreatedTime = now,
                FinalPartTransferTime = request.IsFinal ? now : (DateTime?)null,
                AgentInfo = new ServerBundle.AgentIdentity
                {
                    ComputerName = agentId.ComputerName,
                    DomainName = agentId.DomainName,
                },
                PreviousPartStorageToken = request.PreviousPartStorageToken,
                PreviousPartId = request.PreviousPartId,
            });
        }
예제 #6
0
        public async Task <AddAddPacketsPartsResult> AddIfNotExistsPacketsPartsAsync(AgentIdentity agentId, IList <AddPacketPartRequest> requests, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (requests == null)
            {
                throw new ArgumentException("requests");
            }

            if (!requests.Any())
            {
                return(AddAddPacketsPartsResult.EmptyResult());
            }

            if (!this.initialized)
            {
                await this.InitAsync(cancellationToken);
            }

            var stopWatch = Stopwatch.StartNew();

            // IsOrdered = false, неупорядоченные операции - при выполнении отдельных операций они выполняются не упорядочено и они не останавливают исполнение остальных операций
            try
            {
                var docs = requests.Select(x => this.Get(agentId, x)).ToList();
                await this.chunks.InsertManyAsync(docs, new InsertManyOptions()
                {
                    IsOrdered = false
                }, cancellationToken);

                stopWatch.Stop();
                return(AddAddPacketsPartsResult.CreateResult(this.storageToken, requests.Select((request, i) => {
                    return AddAddPacketPartResult.SuccessResult(request, this.storageToken, docs[i].Id);
                })));
            }
            catch (MongoBulkWriteOperationException ex)
            {
                // Instrumentation ???
                var results = new List <AddAddPacketPartResult>();
                for (int i = 0; i < requests.Count; i++)
                {
                    var request           = requests[i];
                    var rqNonDupKeyErrors = ex.WriteErrors.Where(x => x.Index == i && x.Category != ServerErrorCategory.DuplicateKey);
                    if (rqNonDupKeyErrors.Any())
                    {
                        results.Add(AddAddPacketPartResult.FailResult(request));
                    }
                    else
                    {
                        // TODO оптимизировать, не делать запрос на каждый fail вариант
                        var id = await this.chunks.Find(x => x.PacketId == request.PacketId && x.StartPosition == request.StartPosition).Project(x => x.Id).FirstOrDefaultAsync(cancellationToken);

                        if (id == null)
                        {
                            results.Add(AddAddPacketPartResult.FailResult(request));
                        }
                        else
                        {
                            results.Add(AddAddPacketPartResult.SuccessResult(request, this.storageToken, id));
                        }
                    }
                }

                //var failed = new List<AddAddPacketPartResult>();
                //var reqToSelect = new List<AddPacketPartRequest>();
                //for (int i = 0; i < requests.Count; i++)
                //{
                //    var request = requests[i];
                //    var rqNonDupKeyErrors = ex.WriteErrors.Where(x => x.Index == i && x.Category != ServerErrorCategory.DuplicateKey);
                //    if (rqNonDupKeyErrors.Any())
                //    {
                //        failed.Add(AddAddPacketPartResult.FailResult(request));
                //    }
                //    else
                //    {
                //        reqToSelect.Add(request);
                //    }
                //}

                //// select
                //Expression<Func<PacketPart, bool>> filter = null;

                //var f = Builders<PacketPart>.Filter
                //    .Or(reqToSelect.Select(r =>))

                //FilterDefinition<PacketPart> filterDef =
                //var ids = await this.chunks.Find(x => x.PacketId == request.PacketId && x.StartPosition == request.StartPosition).Project(x => x.Id).FirstOrDefaultAsync(cancellationToken);

                stopWatch.Stop();
                return(AddAddPacketsPartsResult.CreateResult(this.storageToken, results));
            }
        }