/// <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();
                }
            }
        }
Beispiel #2
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;
            }
        }