예제 #1
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();
        }