Ejemplo n.º 1
0
        public void RefreshReplicationInformation(ServerClient commands)
        {
            lock (this)
            {
                var serverHash = ServerHash.GetServerHash(commands.Url);

                JsonDocument document;
                try
                {
                    document = commands.DirectGet(new OperationMetadata(commands.Url, commands.Credentials), RavenReplicationDestinations);
                    failureCounts[commands.Url] = new FailureCounter();                     // we just hit the master, so we can reset its failure count
                }
                catch (Exception e)
                {
                    log.ErrorException("Could not contact master for new replication information", e);
                    document = ReplicationInformerLocalCache.TryLoadReplicationInformationFromLocalCache(serverHash);
                }
                if (document == null)
                {
                    lastReplicationUpdate = SystemTime.UtcNow;                     // checked and not found
                    return;
                }

                ReplicationInformerLocalCache.TrySavingReplicationInformationToLocalCache(serverHash, document);

                UpdateReplicationInformationFromDocument(document);

                lastReplicationUpdate = SystemTime.UtcNow;
            }
        }
Ejemplo n.º 2
0
        private void RefreshReplicationInformationInternal(string url, Func <ReplicationDocument> getReplicationDestinations)
        {
            lock (this)
            {
                var serverHash = ServerHash.GetServerHash(url);

                JsonDocument document;
                var          fromFailoverUrls = false;

                try
                {
                    var replicationDestinations = getReplicationDestinations();
                    document           = replicationDestinations == null ? null : RavenJObject.FromObject(replicationDestinations).ToJsonDocument();
                    failureCounts[url] = new FailureCounter();                     // we just hit the master, so we can reset its failure count
                }
                catch (Exception e)
                {
                    log.ErrorException("Could not contact master for new replication information", e);
                    document = ReplicationInformerLocalCache.TryLoadReplicationInformationFromLocalCache(serverHash);

                    if (document == null)
                    {
                        if (FailoverServers != null && FailoverServers.Length > 0)                         // try to use configured failover servers
                        {
                            var failoverServers = new ReplicationDocument {
                                Destinations = new List <ReplicationDestination>()
                            };

                            foreach (var failover in FailoverServers)
                            {
                                failoverServers.Destinations.Add(failover);
                            }

                            document = new JsonDocument
                            {
                                DataAsJson = RavenJObject.FromObject(failoverServers)
                            };

                            fromFailoverUrls = true;
                        }
                    }
                }


                if (document == null)
                {
                    lastReplicationUpdate = SystemTime.UtcNow;                     // checked and not found
                    return;
                }

                if (!fromFailoverUrls)
                {
                    ReplicationInformerLocalCache.TrySavingReplicationInformationToLocalCache(serverHash, document);
                }

                UpdateReplicationInformationFromDocument(document);

                lastReplicationUpdate = SystemTime.UtcNow;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Refreshes the replication information.
        /// Expert use only.
        /// </summary>
#if SILVERLIGHT || NETFX_CORE
        public Task RefreshReplicationInformation(AsyncServerClient commands)
        {
            lock (this)
            {
                var serverHash = ServerHash.GetServerHash(commands.Url);
                return(commands.DirectGetAsync(new OperationMetadata(commands.Url), RavenReplicationDestinations).ContinueWith((Task <JsonDocument> getTask) =>
                {
                    JsonDocument document;
                    if (getTask.Status == TaskStatus.RanToCompletion)
                    {
                        document = getTask.Result;
                        failureCounts[commands.Url] = new FailureCounter();                         // we just hit the master, so we can reset its failure count
                    }
                    else
                    {
                        log.ErrorException("Could not contact master for new replication information", getTask.Exception);
                        document = ReplicationInformerLocalCache.TryLoadReplicationInformationFromLocalCache(serverHash);
                    }


                    if (IsInvalidDestinationsDocument(document))
                    {
                        lastReplicationUpdate = SystemTime.UtcNow;                         // checked and not found
                        return;
                    }

                    ReplicationInformerLocalCache.TrySavingReplicationInformationToLocalCache(serverHash, document);

                    UpdateReplicationInformationFromDocument(document);

                    lastReplicationUpdate = SystemTime.UtcNow;
                }));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Refreshes the replication information.
        /// Expert use only.
        /// </summary>
#if SILVERLIGHT || NETFX_CORE
        public Task RefreshReplicationInformation(AsyncServerClient commands)
        {
            lock (this)
            {
                var serverHash = ServerHash.GetServerHash(commands.Url);
                return(commands.DirectGetAsync(new OperationMetadata(commands.Url), RavenReplicationDestinations).ContinueWith((Task <JsonDocument> getTask) =>
                {
                    JsonDocument document;

                    var fromFailoverUrls = false;

                    if (getTask.Status == TaskStatus.RanToCompletion)
                    {
                        document = getTask.Result;
                        failureCounts[commands.Url] = new FailureCounter();                         // we just hit the master, so we can reset its failure count
                    }
                    else
                    {
                        log.ErrorException("Could not contact master for new replication information", getTask.Exception);
                        document = ReplicationInformerLocalCache.TryLoadReplicationInformationFromLocalCache(serverHash);

                        if (document == null)
                        {
                            if (FailoverServers != null && FailoverServers.Length > 0)                             // try to use configured failover servers
                            {
                                var failoverServers = new ReplicationDocument {
                                    Destinations = new List <ReplicationDestination>()
                                };

                                foreach (var failover in FailoverServers)
                                {
                                    failoverServers.Destinations.Add(failover);
                                }

                                document = new JsonDocument();
                                document.DataAsJson = RavenJObject.FromObject(failoverServers);

                                fromFailoverUrls = true;
                            }
                        }
                    }

                    if (IsInvalidDestinationsDocument(document))
                    {
                        lastReplicationUpdate = SystemTime.UtcNow;                         // checked and not found
                        return;
                    }

                    if (!fromFailoverUrls)
                    {
                        ReplicationInformerLocalCache.TrySavingReplicationInformationToLocalCache(serverHash, document);
                    }

                    UpdateReplicationInformationFromDocument(document);

                    lastReplicationUpdate = SystemTime.UtcNow;
                }));
            }
        }
Ejemplo n.º 5
0
        public void RefreshReplicationInformation(ServerClient commands)
        {
            lock (this)
            {
                var serverHash = ServerHash.GetServerHash(commands.Url);

                JsonDocument document;
                var          fromFailoverUrls = false;

                try
                {
                    document = commands.DirectGet(new OperationMetadata(commands.Url, commands.PrimaryCredentials), RavenReplicationDestinations);
                    failureCounts[commands.Url] = new FailureCounter();                     // we just hit the master, so we can reset its failure count
                }
                catch (Exception e)
                {
                    log.ErrorException("Could not contact master for new replication information", e);
                    document = ReplicationInformerLocalCache.TryLoadReplicationInformationFromLocalCache(serverHash);

                    if (document == null)
                    {
                        if (FailoverServers != null && FailoverServers.Length > 0)                         // try to use configured failover servers
                        {
                            var failoverServers = new ReplicationDocument {
                                Destinations = new List <ReplicationDestination>()
                            };

                            foreach (var failover in FailoverServers)
                            {
                                failoverServers.Destinations.Add(failover);
                            }

                            document            = new JsonDocument();
                            document.DataAsJson = RavenJObject.FromObject(failoverServers);

                            fromFailoverUrls = true;
                        }
                    }
                }


                if (document == null)
                {
                    lastReplicationUpdate = SystemTime.UtcNow;                     // checked and not found
                    return;
                }

                if (!fromFailoverUrls)
                {
                    ReplicationInformerLocalCache.TrySavingReplicationInformationToLocalCache(serverHash, document);
                }

                UpdateReplicationInformationFromDocument(document);

                lastReplicationUpdate = SystemTime.UtcNow;
            }
        }
Ejemplo n.º 6
0
        private Task UpdateReplicationInformationIfNeededInternalAsync(string url, Func <ReplicationDocumentWithClusterInformation> getReplicationDestinations, bool force)
        {
            if (force == false)
            {
                if (Conventions.FailoverBehavior == FailoverBehavior.FailImmediately)
                {
                    return(new CompletedTask());
                }

                if (lastReplicationUpdate.Add(Conventions.TimeToWaitBetweenReplicationTopologyUpdates) > SystemTime.UtcNow)
                {
                    return(new CompletedTask());
                }
            }
            lock (replicationLock)
            {
                if (firstTime && force == false)
                {
                    var serverHash = ServerHash.GetServerHash(url);

                    var document = ReplicationInformerLocalCache.TryLoadReplicationInformationFromLocalCache(serverHash);
                    if (IsInvalidDestinationsDocument(document) == false)
                    {
                        UpdateReplicationInformationFromDocument(document);
                    }
                }

                firstTime = false;

                if (lastReplicationUpdate.Add(Conventions.TimeToWaitBetweenReplicationTopologyUpdates) > SystemTime.UtcNow && force == false)
                {
                    return(new CompletedTask());
                }

                var taskCopy = refreshReplicationInformationTask;
                if (taskCopy != null)
                {
                    return(taskCopy);
                }

                return(refreshReplicationInformationTask = Task.Factory.StartNew(() =>
                                                                                 RefreshReplicationInformationInternal(url, getReplicationDestinations)).
                                                           ContinueWith(task =>
                {
                    if (task.Exception != null)
                    {
                        Log.ErrorException("Failed to refresh replication information", task.Exception);
                    }
                    refreshReplicationInformationTask = null;
                }));
            }
        }
Ejemplo n.º 7
0
        public Task UpdateReplicationInformationIfNeeded(ServerClient serverClient)
#endif
        {
            if (conventions.FailoverBehavior == FailoverBehavior.FailImmediately)
            {
                return(new CompletedTask());
            }

            if (lastReplicationUpdate.AddMinutes(5) > SystemTime.UtcNow)
            {
                return(new CompletedTask());
            }

            lock (replicationLock)
            {
                if (firstTime)
                {
                    var serverHash = ServerHash.GetServerHash(serverClient.Url);

                    var document = ReplicationInformerLocalCache.TryLoadReplicationInformationFromLocalCache(serverHash);
                    if (IsInvalidDestinationsDocument(document) == false)
                    {
                        UpdateReplicationInformationFromDocument(document);
                    }
                }

                firstTime = false;

                if (lastReplicationUpdate.AddMinutes(5) > SystemTime.UtcNow)
                {
                    return(new CompletedTask());
                }

                var taskCopy = refreshReplicationInformationTask;
                if (taskCopy != null)
                {
                    return(taskCopy);
                }

                return(refreshReplicationInformationTask = Task.Factory.StartNew(() => RefreshReplicationInformation(serverClient))
                                                           .ContinueWith(task =>
                {
                    if (task.Exception != null)
                    {
                        log.ErrorException("Failed to refresh replication information", task.Exception);
                    }
                    refreshReplicationInformationTask = null;
                }));
            }
        }
Ejemplo n.º 8
0
        private Task UpdateReplicationInformationIfNeededInternal(string url, Func <ReplicationDocument> getReplicationDestinations)
        {
            if (Conventions.FailoverBehavior == FailoverBehavior.FailImmediately)
            {
                return(new CompletedTask());
            }

            if (LastReplicationUpdate.AddMinutes(5) > SystemTime.UtcNow)
            {
                return(new CompletedTask());
            }

            lock (ReplicationLock)
            {
                if (FirstTime)
                {
                    var serverHash = ServerHash.GetServerHash(url);

                    var document = ReplicationInformerLocalCache.TryLoadReplicationInformationFromLocalCache(serverHash);
                    if (IsInvalidDestinationsDocument(document) == false)
                    {
                        UpdateReplicationInformationFromDocument(document);
                    }
                }

                FirstTime = false;

                if (LastReplicationUpdate.AddMinutes(5) > SystemTime.UtcNow)
                {
                    return(new CompletedTask());
                }

                var taskCopy = RefreshReplicationInformationTask;
                if (taskCopy != null)
                {
                    return(taskCopy);
                }

                return(RefreshReplicationInformationTask = Task.Factory.StartNew(() => RefreshReplicationInformationInternal(url, getReplicationDestinations))
                                                           .ContinueWith(task =>
                {
                    if (task.Exception != null)
                    {
                        log.ErrorException("Failed to refresh replication information", task.Exception);
                    }
                    RefreshReplicationInformationTask = null;
                }));
            }
        }
Ejemplo n.º 9
0
        public override void ClearReplicationInformationLocalCache(ServerClient client)
        {
            var serverHash = ServerHash.GetServerHash(client.Url);

            ReplicationInformerLocalCache.ClearReplicationInformationFromLocalCache(serverHash);
        }