Exemplo n.º 1
0
        /// <summary> Catch up with locally stored data, without remote fetches. </summary>
        /// <returns>
        ///     The number of events that have been passed to the projection.
        /// </returns>
        private uint CatchUpLocal()
        {
            var caughtUpWithProjection   = false;
            var eventsPassedToProjection = 0u;

            while (true)
            {
                TEvent nextEvent;

                try
                {
                    // This might throw due to serialization error
                    //  (but not for other reasons)
                    nextEvent = Stream.TryGetNext();
                }
                catch (Exception ex)
                {
                    _log?.Warning($"[ES read] unreadable event at seq {Stream.Sequence}.", ex);
                    _projection.SetPossiblyInconsistent();
                    Quarantine.Add(Stream.Sequence, ex);
                    continue;
                }

                // No more local events left
                if (nextEvent == null)
                {
                    break;
                }

                var seq = Stream.Sequence;

                if (_log != null && seq % 1000 == 0)
                {
                    _log.Info($"[ES read] processing event at seq {seq}.");
                }

                if (caughtUpWithProjection || seq > _projection.Sequence)
                {
                    caughtUpWithProjection = true;
                    try
                    {
                        ++eventsPassedToProjection;

                        // This might throw due to event processing issues
                        //  by one or more projection components
                        _projection.Apply(seq, nextEvent);
                    }
                    catch (Exception ex)
                    {
                        _log?.Warning($"[ES read] processing error on event at seq {seq}.", ex);
                        _projection.SetPossiblyInconsistent();
                        Quarantine.Add(seq, nextEvent, ex);
                    }
                }
            }

            return(eventsPassedToProjection);
        }
Exemplo n.º 2
0
        internal static async Task Catchup(
            IReifiedProjection projection,
            IEventStream stream,
            CancellationToken cancel = default,
            ILogAdapter log          = null)
        {
            try
            {
                // Load project and discard events before that.
                log?.Info("[ES init] loading projections.");

                await projection.TryLoadAsync(cancel).ConfigureAwait(false);

                var catchUp = projection.Sequence + 1;

                log?.Info($"[ES init] advancing stream to seq {catchUp}.");
                var streamSequence = await stream.DiscardUpTo(catchUp, cancel).ConfigureAwait(false);

                if (cancel.IsCancellationRequested)
                {
                    return;
                }

                if (streamSequence != projection.Sequence)
                {
                    log?.Warning(
                        $"[ES init] projection seq {projection.Sequence} not found in stream (max seq is {streamSequence}: resetting everything.");

                    // Cache is apparently beyond the available sequence. Could happen in
                    // development environments with non-persistent events but persistent
                    // caches. Treat cache as invalid and start from the beginning.
                    stream.Reset();
                    projection.Reset();
                }
            }
            catch (Exception e)
            {
                log?.Warning("[ES init] error while reading cache.", e);

                // Something went wrong when reading the cache. Stop.
                stream.Reset();
                projection.Reset();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempt to load this projection from the source, updating its
        /// <see cref="Current"/> and <see cref="Sequence"/>.
        /// </summary>
        /// <remarks>
        /// Object is unchanged if loading fails.
        ///
        /// Obviously, as this object does not support multi-threaded access,
        /// it should NOT be accessed in any way before the task has completed.
        /// </remarks>
        public async Task TryLoadAsync(CancellationToken cancel = default)
        {
            if (_cacheProvider == null)
            {
                _log?.Warning($"[{Name}] no read cache provider !");
                return;
            }

            var sw = Stopwatch.StartNew();

            IEnumerable <Task <CacheCandidate> > candidates;

            try
            {
                candidates = await _cacheProvider.OpenReadAsync(Name);
            }
            catch (Exception ex)
            {
                _log?.Warning($"[{Name}] error when opening cache.", ex);
                return;
            }

            foreach (var candidateTask in candidates)
            {
                CacheCandidate candidate;
                try
                {
                    candidate = await candidateTask;
                }
                catch (Exception ex)
                {
                    _log?.Warning($"[{Name}] error when opening cache.", ex);
                    continue;
                }

                _log?.Info($"[{Name}] reading cache {candidate.Name}");

                var stream = candidate.Contents;
                try
                {
                    // Load the sequence number from the input
                    uint seq;
                    using (var br = new BinaryReader(stream, Encoding.UTF8, true))
                        seq = br.ReadUInt32();

                    _log?.Debug($"[{Name}] cache is at seq {seq}.");

                    // Create a new stream to hide the write of the sequence numbers
                    // (at the top and the bottom of the stream).
                    var boundedStream = new BoundedStream(stream, stream.Length - 8);

                    // Load the state, which advances the stream
                    var state = await _projection.TryLoadAsync(boundedStream, cancel)
                                .ConfigureAwait(false);

                    if (state == null)
                    {
                        _log?.Warning($"[{Name}] projection could not parse cache {candidate.Name}");
                        continue;
                    }

                    // Sanity check: is the same sequence number found at the end ?
                    uint endseq;
                    using (var br = new BinaryReader(stream, Encoding.UTF8, true))
                        endseq = br.ReadUInt32();

                    if (endseq != seq)
                    {
                        _log?.Warning($"[{Name}] sanity-check seq is {endseq} in cache {candidate.Name}");
                        continue;
                    }

                    _log?.Info($"[{Name}] loaded {stream.Length} bytes in {sw.Elapsed:mm':'ss'.'fff} from cache {candidate.Name}");

                    Current  = state;
                    Sequence = seq;
                    return;

                    // Do NOT set _possiblyInconsistent to false here !
                    // Inconsistency can have external causes, e.g. event read
                    // failure, that are not automagically solved by loading from cache.
                }
                catch (EndOfStreamException)
                {
                    _log?.Warning($"[{Name}] incomplete cache {candidate.Name}");
                    // Incomplete streams are simply treated as missing
                }
                catch (Exception ex)
                {
                    _log?.Warning($"[{Name}] could not parse cache {candidate.Name}", ex);
                    // If a cache file cannot be parsed, try the next one
                }
                finally
                {
                    stream.Dispose();
                }
            }
        }
Exemplo n.º 4
0
 public void Info(string message)
 {
     _log.Info(Elapsed + " " + message);
 }
Exemplo n.º 5
0
Arquivo: Log.cs Projeto: enklu/orchid
 /// <inheritdoc/>
 public static void Info(object caller, object message, params object[] replacements)
 => _log.Info(caller, message, replacements);
Exemplo n.º 6
0
        /// <summary>
        /// Attempt to load this projection from the source, updating its
        /// <see cref="Current"/> and <see cref="Sequence"/>.
        /// </summary>
        /// <remarks>
        /// Object is unchanged if loading fails.
        ///
        /// Obviously, as this object does not support multi-threaded access,
        /// it should NOT be accessed in any way before the task has completed.
        /// </remarks>
        public async Task TryLoadAsync(CancellationToken cancel = default(CancellationToken))
        {
            if (_cacheProvider == null)
            {
                _log?.Warning($"[{Name}] no read cache provider !");
                return;
            }

            Stream source;

            var sw = Stopwatch.StartNew();

            try
            {
                source = await _cacheProvider.OpenReadAsync(Name);
            }
            catch (Exception ex)
            {
                _log?.Warning($"[{Name}] error when opening cache.", ex);
                return;
            }

            if (source == null)
            {
                _log?.Info($"[{Name}] no cached data found.");
                return;
            }

            try
            {
                // Load the sequence number from the input
                uint seq;
                using (var br = new BinaryReader(source, Encoding.UTF8, true))
                    seq = br.ReadUInt32();

                _log?.Debug($"[{Name}] cache is at seq {seq}.");

                // Load the state, which advances the stream
                var state = await _projection.TryLoadAsync(source, cancel).ConfigureAwait(false);

                if (state == null)
                {
                    _log?.Warning($"[{Name}] projection could not parse cache.");
                    return;
                }

                // Sanity check: is the same sequence number found at the end ?
                uint endseq;
                using (var br = new BinaryReader(source, Encoding.UTF8, true))
                    endseq = br.ReadUInt32();

                if (endseq != seq)
                {
                    _log?.Warning($"[{Name}] sanity-check seq is {endseq}.");
                    return;
                }

                _log?.Info($"[{Name}] loaded from cache in {sw.Elapsed:mm':'ss'.'fff}.");

                Current  = state;
                Sequence = seq;

                // Do NOT set _possiblyInconsistent to false here !
                // Inconsistency can have external causes, e.g. event read
                // failure, that are not automagically solved by loading from cache.
            }
            catch (EndOfStreamException)
            {
                _log?.Warning($"[{Name}] cache is incomplete.");
                // Incomplete streams are simply treated as missing
            }
            catch (Exception ex)
            {
                _log?.Warning($"[{Name}] could not parse cache.", ex);
                throw;
            }
        }