GetReplicationDestinations() private method

private GetReplicationDestinations ( Predicate predicate = null ) : Raven.Bundles.Replication.Tasks.ReplicationStrategy[]
predicate Predicate
return Raven.Bundles.Replication.Tasks.ReplicationStrategy[]
Exemplo n.º 1
0
        public bool Execute(Func <ReplicationDestination, bool> shouldSkipDestinationPredicate = null)
        {
            if (database.Disposed)
            {
                return(false);
            }

            if (Monitor.TryEnter(indexReplicationLock) == false)
            {
                return(false);
            }

            try
            {
                using (CultureHelper.EnsureInvariantCulture())
                {
                    shouldSkipDestinationPredicate = shouldSkipDestinationPredicate ?? (x => x.SkipIndexReplication == false);
                    var replicationDestinations = replication.GetReplicationDestinations(x => shouldSkipDestinationPredicate(x));

                    foreach (var destination in replicationDestinations)
                    {
                        try
                        {
                            var now = SystemTime.UtcNow;

                            var indexTombstones = GetTombstones(Constants.RavenReplicationIndexesTombstones, 0, 64,
                                                                // we don't send out deletions immediately, we wait for a bit
                                                                // to make sure that the user didn't reset the index or delete / create
                                                                // things manually
                                                                x => (now - x.CreatedAt) >= TimeToWaitBeforeSendingDeletesOfIndexesToSiblings);

                            var replicatedIndexTombstones = new Dictionary <string, int>();

                            ReplicateIndexDeletionIfNeeded(indexTombstones, destination, replicatedIndexTombstones);

                            var candidatesForReplication = new List <Tuple <IndexDefinition, IndexingPriority> >();

                            if (database.Indexes.Definitions.Length > 0)
                            {
                                var sideBySideIndexes = database.Indexes.Definitions.Where(x => x.IsSideBySideIndex)
                                                        .ToDictionary(x => x.Name, x => x);

                                foreach (var indexDefinition in database.Indexes.Definitions.Where(x => !x.IsSideBySideIndex))
                                {
                                    IndexDefinition sideBySideIndexDefinition;
                                    if (sideBySideIndexes.TryGetValue("ReplacementOf/" + indexDefinition.Name, out sideBySideIndexDefinition))
                                    {
                                        ReplicateSingleSideBySideIndex(destination, indexDefinition, sideBySideIndexDefinition);
                                    }
                                    else
                                    {
                                        candidatesForReplication.Add(Tuple.Create(indexDefinition, database.IndexStorage.GetIndexInstance(indexDefinition.Name).Priority));
                                    }
                                }

                                ReplicateIndexesMultiPut(destination, candidatesForReplication);
                            }

                            database.TransactionalStorage.Batch(actions =>
                            {
                                foreach (var indexTombstone in replicatedIndexTombstones)
                                {
                                    if (indexTombstone.Value != replicationDestinations.Length && database.IndexStorage.HasIndex(indexTombstone.Key) == false)
                                    {
                                        continue;
                                    }

                                    actions.Lists.Remove(Constants.RavenReplicationIndexesTombstones, indexTombstone.Key);
                                }
                            });
                        }
                        catch (Exception e)
                        {
                            Log.ErrorException("Failed to replicate indexes to " + destination, e);
                        }
                    }

                    return(true);
                }
            }
            catch (Exception e)
            {
                Log.ErrorException("Failed to replicate indexes", e);

                return(false);
            }
            finally
            {
                Monitor.Exit(indexReplicationLock);
            }
        }
        public bool Execute(Func <ReplicationDestination, bool> shouldSkipDestinationPredicate = null)
        {
            if (database.Disposed)
            {
                return(false);
            }

            if (Monitor.TryEnter(replicationLock) == false)
            {
                return(false);
            }

            try
            {
                using (CultureHelper.EnsureInvariantCulture())
                {
                    shouldSkipDestinationPredicate = shouldSkipDestinationPredicate ?? (x => x.SkipIndexReplication == false);
                    var replicationDestinations = replication.GetReplicationDestinations(x => shouldSkipDestinationPredicate(x));

                    foreach (var destination in replicationDestinations)
                    {
                        try
                        {
                            var now = SystemTime.UtcNow;

                            var transformerTombstones = GetTombstones(Constants.RavenReplicationTransformerTombstones, 0, 64,
                                                                      // we don't send out deletions immediately, we wait for a bit
                                                                      // to make sure that the user didn't reset the index or delete / create
                                                                      // things manually
                                                                      x => (now - x.CreatedAt) >= TimeToWaitBeforeSendingDeletesOfTransformersToSiblings);
                            var replicatedTransformerTombstones = new Dictionary <string, int>();

                            ReplicateTransformerDeletionIfNeeded(transformerTombstones, destination, replicatedTransformerTombstones);

                            if (database.Transformers.Definitions.Length > 0)
                            {
                                foreach (var definition in database.Transformers.Definitions)
                                {
                                    ReplicateSingleTransformer(destination, definition);
                                }
                            }

                            database.TransactionalStorage.Batch(actions =>
                            {
                                foreach (var transformerTombstone in replicatedTransformerTombstones)
                                {
                                    var transfomerExists = database.Transformers.GetTransformerDefinition(transformerTombstone.Key) != null;
                                    if (transformerTombstone.Value != replicationDestinations.Length &&
                                        transfomerExists == false)
                                    {
                                        continue;
                                    }

                                    actions.Lists.Remove(Constants.RavenReplicationTransformerTombstones, transformerTombstone.Key);
                                }
                            });
                        }
                        catch (Exception e)
                        {
                            Log.ErrorException("Failed to replicate transformers to " + destination, e);
                        }
                    }
                    return(true);
                }
            }
            catch (Exception e)
            {
                Log.ErrorException("Failed to replicate transformers", e);

                return(false);
            }
            finally
            {
                Monitor.Exit(replicationLock);
            }
        }