Beispiel #1
0
        public IDocumentStore GetOrCreate(
            string name,
            AccessMode accessMode = AccessMode.ReadOnly,
            Action <DocumentStore> initializer = null)
        {
            name.Ensure("name").IsNotNullOrWhiteSpace();

            var existingWrapper = allStoreWrappers.SingleOrDefault(
                store =>
                store.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
                store.AccessMode == accessMode);

            if (existingWrapper != null)
            {
                return(existingWrapper);
            }

            initializer = initializer ?? (store => store.Initialize());
            Action <DocumentStoreWrapper> updateInnerStore =
                wrapper => this.UpdateInnerStore(wrapper, initializer);
            var newStore = new DocumentStoreWrapper(name, accessMode, updateInnerStore);

            allStoreWrappers.Add(newStore);
            return(newStore);
        }
Beispiel #2
0
        private void LogException(
            Exception exception,
            DocumentStoreWrapper wrapper,
            DocumentStore newInnerStore)
        {
            string newUrl  = (newInnerStore == null) ? null : newInnerStore.Url;
            string message = string.Format(
                "{0} {1} store Url could not be set to '{2}'. {3}",
                wrapper.AccessMode,
                wrapper.Name,
                newUrl,
                (wrapper.IsInitialized && !wrapper.InnerStore.WasDisposed)
                    ? string.Format("Active Url remains {0}.", wrapper.Url)
                    : "Store has not been initialized.");

            logger.ErrorException(message, exception);
        }
Beispiel #3
0
        private Instance GetClosestInstanceOrDefault(DocumentStoreWrapper wrapper)
        {
            var      store    = ravenConfig.Stores.SelectByName(wrapper.Name);
            Instance instance = null;

            if (store != null)
            {
                instance = store.GetClosestReplica(
                    Environment.MachineName,
                    wrapper.AccessMode == AccessMode.ReadWrite);
            }

            instance = instance ??
                       new Instance
            {
                Url         = new Uri(this.operationsStoreUrl, wrapper.Name.ToLowerInvariant()),
                AllowReads  = true,
                AllowWrites = wrapper.AccessMode == AccessMode.ReadWrite
            };

            return(instance);
        }
Beispiel #4
0
        public IDocumentStore GetOrCreate(
            string name,
            AccessMode accessMode = AccessMode.ReadOnly,
            Action<DocumentStore> initializer = null)
        {
            name.Ensure("name").IsNotNullOrWhiteSpace();

            var existingWrapper = allStoreWrappers.SingleOrDefault(
                store =>
                    store.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
                    store.AccessMode == accessMode);
            if (existingWrapper != null)
            {
                return existingWrapper;
            }

            initializer = initializer ?? (store => store.Initialize());
            Action<DocumentStoreWrapper> updateInnerStore =
                wrapper => this.UpdateInnerStore(wrapper, initializer);
            var newStore = new DocumentStoreWrapper(name, accessMode, updateInnerStore);

            allStoreWrappers.Add(newStore);
            return newStore;
        }
Beispiel #5
0
        private void UpdateInnerStore(
            DocumentStoreWrapper wrapper,
            Action<DocumentStore> innerStoreInitializer)
        {
            DocumentStore existingInnerStore = wrapper.InnerStore;
            DocumentStore newInnerStore = null;
            bool newInnerStoreApplied = false;

            try
            {
                var storeInstance = GetClosestInstanceOrDefault(wrapper);

                if (wrapper.IsInitialized &&
                    wrapper.InnerStore.Url.Equals(storeInstance.Url.ToString(), StringComparison.OrdinalIgnoreCase) &&
                    !wrapper.InnerStore.WasDisposed)
                {
                    logger.Debug("{0} {1} store Url did not change. It remains {2}.", wrapper.AccessMode, wrapper.Name, wrapper.InnerStore.Url);
                    return;
                }

                newInnerStore = this.CreateDocumentStore(storeInstance, wrapper.AccessMode);

                innerStoreInitializer(newInnerStore);

                wrapper.InnerStore = newInnerStore;
                wrapper.IsInitialized = true;
                newInnerStoreApplied = true;

                logger.Info("{0} {1} store Url set to {2}.", wrapper.AccessMode, wrapper.Name, newInnerStore.Url);
            }
            catch (Exception exception)
            {
                LogException(exception, wrapper, newInnerStore);

                if (exception.IsFatal())
                {
                    throw;
                }
            }
            finally
            {
                if (newInnerStoreApplied)
                {
                    if (existingInnerStore != null)
                    {
                        // Wait for current operations to complete.
                        // This is on a background thread.
                        Thread.Sleep(5000);
                        logger.Debug("Disposing store for {0}", existingInnerStore.Url);
                        existingInnerStore.Dispose();
                    }
                }
                else
                {
                    if (newInnerStore != null)
                    {
                        logger.Debug("Disposing store for {0}", newInnerStore.Url);
                        newInnerStore.Dispose();
                    }
                }
            }
        }
Beispiel #6
0
 private void LogException(
     Exception exception,
     DocumentStoreWrapper wrapper,
     DocumentStore newInnerStore)
 {
     string newUrl = (newInnerStore == null) ? null : newInnerStore.Url;
     string message = string.Format(
         "{0} {1} store Url could not be set to '{2}'. {3}",
         wrapper.AccessMode,
         wrapper.Name,
         newUrl,
         (wrapper.IsInitialized && !wrapper.InnerStore.WasDisposed)
             ? string.Format("Active Url remains {0}.", wrapper.Url)
             : "Store has not been initialized.");
     logger.ErrorException(message, exception);
 }
Beispiel #7
0
        private Instance GetClosestInstanceOrDefault(DocumentStoreWrapper wrapper)
        {
            var store = ravenConfig.Stores.SelectByName(wrapper.Name);
            Instance instance = null;
            if (store != null)
            {
                instance = store.GetClosestReplica(
                    Environment.MachineName,
                    wrapper.AccessMode == AccessMode.ReadWrite);
            }

            instance = instance ??
                new Instance
                {
                    Url = new Uri(this.operationsStoreUrl, wrapper.Name.ToLowerInvariant()),
                    AllowReads = true,
                    AllowWrites = wrapper.AccessMode == AccessMode.ReadWrite
                };

            return instance;
        }
Beispiel #8
0
        private void UpdateInnerStore(
            DocumentStoreWrapper wrapper,
            Action <DocumentStore> innerStoreInitializer)
        {
            DocumentStore existingInnerStore   = wrapper.InnerStore;
            DocumentStore newInnerStore        = null;
            bool          newInnerStoreApplied = false;

            try
            {
                var storeInstance = GetClosestInstanceOrDefault(wrapper);

                if (wrapper.IsInitialized &&
                    wrapper.InnerStore.Url.Equals(storeInstance.Url.ToString(), StringComparison.OrdinalIgnoreCase) &&
                    !wrapper.InnerStore.WasDisposed)
                {
                    logger.Debug("{0} {1} store Url did not change. It remains {2}.", wrapper.AccessMode, wrapper.Name, wrapper.InnerStore.Url);
                    return;
                }

                newInnerStore = this.CreateDocumentStore(storeInstance, wrapper.AccessMode);

                innerStoreInitializer(newInnerStore);

                wrapper.InnerStore    = newInnerStore;
                wrapper.IsInitialized = true;
                newInnerStoreApplied  = true;

                logger.Info("{0} {1} store Url set to {2}.", wrapper.AccessMode, wrapper.Name, newInnerStore.Url);
            }
            catch (Exception exception)
            {
                LogException(exception, wrapper, newInnerStore);

                if (exception.IsFatal())
                {
                    throw;
                }
            }
            finally
            {
                if (newInnerStoreApplied)
                {
                    if (existingInnerStore != null)
                    {
                        // Wait for current operations to complete.
                        // This is on a background thread.
                        Thread.Sleep(5000);
                        logger.Debug("Disposing store for {0}", existingInnerStore.Url);
                        existingInnerStore.Dispose();
                    }
                }
                else
                {
                    if (newInnerStore != null)
                    {
                        logger.Debug("Disposing store for {0}", newInnerStore.Url);
                        newInnerStore.Dispose();
                    }
                }
            }
        }