Ejemplo n.º 1
0
        /// <summary>
        /// Switch Modes.
        /// </summary>
        /// <remarks>
        /// Reader works in 2 modes. Cached, or direct. This routine allows us to switch between these 2 modes.
        /// Switch is expected to happen when a replica become primary (switch to cached) and when it loses primary status.
        /// </remarks>
        /// <param name="newMode"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task SwitchModeAsync(DataReaderMode newMode, CancellationToken token)
        {
            if (newMode == this.dataReaderMode)
            {
                EventStoreLogger.Logger.LogMessage("Target Mode: {0} == Current Mode. No-Op", this.dataReaderMode);
                return;
            }

            EventStoreLogger.Logger.LogMessage("Current Mode: {0}, New Mode: {1}", this.dataReaderMode, newMode);

            // We do the switch under lock.
            await this.singleAccess.WaitAsync(token).ConfigureAwait(false);

            try
            {
                if (this.azureAccessObject != null)
                {
                    EventStoreLogger.Logger.LogMessage("Releasing Azure Access Object");
                    await this.azureAccessObject.ReleaseAccessAsync(token).ConfigureAwait(false);

                    this.azureAccessObject = null;
                }

                this.dataReaderMode = newMode;
                this.initialized    = false;
            }
            finally
            {
                this.singleAccess.Release();
            }

            await this.InitIfRequiredAsync(token).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
 public EventStoreReader(DataReaderMode readingMode)
 {
     this.dataReaderMode = readingMode;
     this.initialized    = false;
     this.singleAccess   = new SemaphoreSlim(1);
 }