Esempio n. 1
0
        private void SendDocumentsBatch(DocumentsOperationContext documentsContext, OutgoingReplicationStatsScope stats)
        {
            if (_log.IsInfoEnabled)
            {
                _log.Info($"Starting sending replication batch ({_parent._database.Name}) with {_orderedReplicaItems.Count:#,#;;0} docs, and last etag {_lastEtag:#,#;;0}");
            }

            var sw         = Stopwatch.StartNew();
            var headerJson = new DynamicJsonValue
            {
                [nameof(ReplicationMessageHeader.Type)]                   = ReplicationMessageType.Documents,
                [nameof(ReplicationMessageHeader.LastDocumentEtag)]       = _lastEtag,
                [nameof(ReplicationMessageHeader.ItemsCount)]             = _orderedReplicaItems.Count,
                [nameof(ReplicationMessageHeader.AttachmentStreamsCount)] = _replicaAttachmentStreams.Count
            };

            stats.RecordLastEtag(_lastEtag);

            _parent.WriteToServer(headerJson);

            foreach (var item in _orderedReplicaItems)
            {
                using (Slice.From(documentsContext.Allocator, item.Value.ChangeVector, out var cv))
                {
                    item.Value.Write(cv, _stream, _tempBuffer, stats);
                }
            }

            foreach (var item in _replicaAttachmentStreams)
            {
                item.Value.WriteStream(_stream, _tempBuffer);
                stats.RecordAttachmentOutput(item.Value.Stream.Length);
            }

            // close the transaction as early as possible, and before we wait for reply
            // from other side
            documentsContext.Transaction.Dispose();
            _stream.Flush();
            sw.Stop();

            if (_log.IsInfoEnabled && _orderedReplicaItems.Count > 0)
            {
                _log.Info($"Finished sending replication batch. Sent {_orderedReplicaItems.Count:#,#;;0} documents and {_replicaAttachmentStreams.Count:#,#;;0} attachment streams in {sw.ElapsedMilliseconds:#,#;;0} ms. Last sent etag = {_lastEtag:#,#;;0}");
            }

            var(type, _) = _parent.HandleServerResponse();
            if (type == ReplicationMessageReply.ReplyType.MissingAttachments)
            {
                MissingAttachmentsInLastBatch = true;
                return;
            }
            _parent._lastSentDocumentEtag = _lastEtag;

            _parent._lastDocumentSentTime = DateTime.UtcNow;
        }
Esempio n. 2
0
        private void SendDocumentsBatch()
        {
            if (_log.IsInfoEnabled)
            {
                _log.Info(
                    $"Starting sending replication batch ({_parent._database.Name}) with {_orderedReplicaItems.Count:#,#;;0} docs, and last etag {_lastEtag}");
            }

            var sw = Stopwatch.StartNew();

            try
            {
                var headerJson = new DynamicJsonValue
                {
                    [nameof(ReplicationMessageHeader.Type)]                       = ReplicationMessageType.Documents,
                    [nameof(ReplicationMessageHeader.LastDocumentEtag)]           = _lastEtag,
                    [nameof(ReplicationMessageHeader.LastIndexOrTransformerEtag)] = _parent._lastSentIndexOrTransformerEtag,
                    [nameof(ReplicationMessageHeader.ItemCount)]                  = _orderedReplicaItems.Count
                };
                _parent.WriteToServerAndFlush(headerJson);
                foreach (var item in _orderedReplicaItems)
                {
                    WriteDocumentToServer(item.Value.Key, item.Value.ChangeVector, item.Value.Data,
                                          item.Value.Collection);
                }
            }
            finally //do try-finally as precaution
            {
                // we can release the read transaction while we are waiting for
                // reply from the server and not hold it for a long time
                _parent._documentsContext.Transaction.Dispose();
            }
            _stream.Flush();
            sw.Stop();

            _parent._lastSentDocumentEtag = _lastEtag;

            if (_log.IsInfoEnabled && _orderedReplicaItems.Count > 0)
            {
                _log.Info(
                    $"Finished sending replication batch. Sent {_orderedReplicaItems.Count:#,#;;0} documents in {sw.ElapsedMilliseconds:#,#;;0} ms. Last sent etag = {_lastEtag}");
            }

            _parent._lastDocumentSentTime = DateTime.UtcNow;
            using (_parent._documentsContext.OpenReadTransaction())
                using (_parent._configurationContext.OpenReadTransaction())
                {
                    _parent.HandleServerResponse();
                }
        }
        private void SendIndexTransformerBatch()
        {
            //if we have empty documents batch we send a heartbeat;
            //for indexes/transformers we don't do this, hence this condition
            if (_orderedReplicaItems.Count == 0)
            {
                return;
            }

            if (_log.IsInfoEnabled)
            {
                _log.Info(
                    $"Starting sending replication batch ({_parent._database.Name}) with {_orderedReplicaItems.Count:#,#;;0} indexes/transformers, and last etag {LastEtag}");
            }

            var sw         = Stopwatch.StartNew();
            var headerJson = new DynamicJsonValue
            {
                [nameof(ReplicationMessageHeader.Type)]                       = ReplicationMessageType.IndexesTransformers,
                [nameof(ReplicationMessageHeader.LastDocumentEtag)]           = _parent._lastSentDocumentEtag,
                [nameof(ReplicationMessageHeader.LastIndexOrTransformerEtag)] = LastEtag,
                [nameof(ReplicationMessageHeader.ItemCount)]                  = _orderedReplicaItems.Count
            };

            _parent.WriteToServerAndFlush(headerJson);

            foreach (var item in _orderedReplicaItems)
            {
                WriteMetadataToServer(item.Value);
            }

            _stream.Flush();
            sw.Stop();

            _parent._lastSentIndexOrTransformerEtag = LastEtag;

            if (_log.IsInfoEnabled && _orderedReplicaItems.Count > 0)
            {
                _log.Info(
                    $"Finished sending replication batch. Sent {_orderedReplicaItems.Count:#,#;;0} documents in {sw.ElapsedMilliseconds:#,#;;0} ms. Last sent etag = {LastEtag}");
            }

            _parent._lastIndexOrTransformerSentTime = DateTime.UtcNow;

            _parent.HandleServerResponse();
        }