Exemplo n.º 1
0
        public async Task If_deleted_original_index_on_destination_should_simply_create_the_replacement_index()
        {
            using (var source = CreateStore())
                using (var destination = CreateStore())
                {
                    var testIndex   = new UserIndex();
                    var oldIndexDef = new IndexDefinition
                    {
                        Map = "from user in docs.Users\n select new {\n\tName = user.Name\n}"
                    };
                    source.DatabaseCommands.PutIndex(testIndex.IndexName, oldIndexDef);

                    var sourceDatabase      = await servers[0].Server.GetDatabaseInternal(source.DefaultDatabase);
                    var destinationDatabase = await servers[1].Server.GetDatabaseInternal(destination.DefaultDatabase);

                    sourceDatabase.StopBackgroundWorkers();
                    destinationDatabase.StopBackgroundWorkers();

                    var sourceReplicationTask = sourceDatabase.StartupTasks.OfType <ReplicationTask>().First();
                    sourceReplicationTask.Pause();

                    SetupReplication(source.DatabaseCommands, destination);

                    testIndex.SideBySideExecute(source);

                    //do side-by-side index replication -> but since in the destination there is no original index,
                    //simply create the side-by-side index as if it replaced the old index already
                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);

                    var definition = destination.DatabaseCommands.GetIndex(testIndex.IndexName);
                    Assert.NotNull(definition);
                    Assert.True(definition.Equals(testIndex.CreateIndexDefinition(), false));
                }
        }
Exemplo n.º 2
0
        public async Task If_deleted_original_index_on_destination_but_not_side_by_side_index()
        {
            using (var source = CreateStore())
                using (var destination = CreateStore())
                {
                    var testIndex = new UserIndex();

                    var oldIndexDef = new IndexDefinition
                    {
                        Map = "from user in docs.Users\n select new {\n\tName = user.Name\n}"
                    };

                    source.DatabaseCommands.PutIndex(testIndex.IndexName, oldIndexDef);

                    var sourceDatabase      = await servers[0].Server.GetDatabaseInternal(source.DefaultDatabase);
                    var destinationDatabase = await servers[1].Server.GetDatabaseInternal(destination.DefaultDatabase);

                    using (var session = source.OpenSession())
                    {
                        session.Store(new User
                        {
                            Name = "John Doe"
                        });
                        session.SaveChanges();
                    }

                    WaitForIndexing(source);
                    sourceDatabase.StopBackgroundWorkers();
                    destinationDatabase.StopBackgroundWorkers();

                    var sourceReplicationTask = sourceDatabase.StartupTasks.OfType <ReplicationTask>().First();
                    sourceReplicationTask.Pause();

                    SetupReplication(source.DatabaseCommands, destination);

                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);             //replicate the usual index

                    source.SideBySideExecuteIndex(testIndex);

                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);          //replicate the side-by-side index

                    destinationDatabase.Indexes.DeleteIndex(testIndex.IndexName);             //delete the original index

                    var sideBySideIndex = destinationDatabase.Indexes.GetIndexDefinition("ReplacementOf/" + testIndex.IndexName);
                    Assert.NotNull(sideBySideIndex);

                    VerifyReplacementDocumentIsThere("ReplacementOf/" + testIndex.IndexName, destinationDatabase);
                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);             //replicate the side-by-side index again

                    var oldIndex = destinationDatabase.Indexes.GetIndexDefinition(testIndex.IndexName);
                    Assert.True(oldIndex.Equals(testIndex.CreateIndexDefinition(), false));

                    sideBySideIndex = destinationDatabase.Indexes.GetIndexDefinition("ReplacementOf/" + testIndex.IndexName);
                    Assert.Null(sideBySideIndex);
                    Assert.Null(destinationDatabase.Documents.Get(Constants.IndexReplacePrefix + "ReplacementOf/" + testIndex.IndexName, null));
                }
        }
Exemplo n.º 3
0
        public async Task Side_by_side_index_should_be_replicated()
        {
            using (var source = CreateStore())
                using (var destination = CreateStore())
                {
                    var testIndex   = new UserIndex();
                    var oldIndexDef = new IndexDefinition
                    {
                        Map = "from user in docs.Users\n select new {\n\tName = user.Name\n}"
                    };
                    source.DatabaseCommands.PutIndex(testIndex.IndexName, oldIndexDef);

                    var sourceDatabase      = await servers[0].Server.GetDatabaseInternal(source.DefaultDatabase);
                    var destinationDatabase = await servers[1].Server.GetDatabaseInternal(destination.DefaultDatabase);

                    sourceDatabase.StopBackgroundWorkers();
                    destinationDatabase.StopBackgroundWorkers();

                    var sourceReplicationTask = sourceDatabase.StartupTasks.OfType <ReplicationTask>().First();
                    sourceReplicationTask.Pause();

                    SetupReplication(source.DatabaseCommands, destination);
                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);             //now the old index replicated to destination

                    var sideBySideIndexReplicated = new ManualResetEventSlim();

                    testIndex.SideBySideExecute(source);

                    var replaceIndexName = "ReplacementOf/" + testIndex.IndexName;
                    destinationDatabase.Notifications.OnIndexChange += (database, notification) =>
                    {
                        if (notification.Type == IndexChangeTypes.IndexAdded &&
                            notification.Name.Equals(replaceIndexName))
                        {
                            sideBySideIndexReplicated.Set();
                        }
                    };

                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);

                    Assert.True(sideBySideIndexReplicated.Wait(2000));

                    var definition = destination.DatabaseCommands.GetIndex(replaceIndexName);
                    Assert.NotNull(definition);
                    Assert.True(definition.Equals(testIndex.CreateIndexDefinition(), false));

                    var replacementIndexName = "ReplacementOf/" + testIndex.IndexName;
                    VerifyReplacementDocumentIsThere(replacementIndexName, destinationDatabase);
                }
        }
Exemplo n.º 4
0
        public async Task If_deleted_original_index_on_destination_should_simply_create_the_replacement_index()
        {
            using (var source = CreateStore())
                using (var destination = CreateStore())
                {
                    var sourceDatabase      = await servers[0].Server.GetDatabaseInternal(source.DefaultDatabase);
                    var destinationDatabase = await servers[1].Server.GetDatabaseInternal(destination.DefaultDatabase);

                    sourceDatabase.StopBackgroundWorkers();
                    destinationDatabase.StopBackgroundWorkers();

                    var testIndex   = new UserIndex();
                    var oldIndexDef = new IndexDefinition
                    {
                        Map = "from user in docs.Users\n select new {\n\tName = user.Name\n}"
                    };
                    source.DatabaseCommands.PutIndex(testIndex.IndexName, oldIndexDef);

                    using (var session = source.OpenSession())
                    {
                        for (var i = 0; i < 10; i++)
                        {
                            session.Store(new User
                            {
                                Name = "User - " + i
                            });
                        }

                        session.SaveChanges();
                    }

                    var sourceReplicationTask = sourceDatabase.StartupTasks.OfType <ReplicationTask>().First();
                    sourceReplicationTask.Pause();

                    SetupReplication(source.DatabaseCommands, destination);

                    testIndex.SideBySideExecute(source);

                    //the side by side will be automatically replicated and saved as a simple index
                    Assert.Null(destinationDatabase.Indexes.GetIndexDefinition(Constants.SideBySideIndexNamePrefix + testIndex.IndexName));

                    var definition = destination.DatabaseCommands.GetIndex(testIndex.IndexName);
                    Assert.NotNull(definition);
                    Assert.True(definition.Equals(testIndex.CreateIndexDefinition(), false));
                }
        }
Exemplo n.º 5
0
        public async Task If_original_index_exists_but_no_side_by_side_index_then_create_side_by_side_index()
        {
            using (var source = CreateStore())
                using (var destination = CreateStore())
                {
                    var testIndex   = new UserIndex();
                    var oldIndexDef = new IndexDefinition
                    {
                        Map = "from user in docs.Users\n select new {\n\tName = user.Name\n}"
                    };
                    source.DatabaseCommands.PutIndex(testIndex.IndexName, oldIndexDef);

                    var sourceDatabase      = await servers[0].Server.GetDatabaseInternal(source.DefaultDatabase);
                    var destinationDatabase = await servers[1].Server.GetDatabaseInternal(destination.DefaultDatabase);

                    sourceDatabase.StopBackgroundWorkers();
                    destinationDatabase.StopBackgroundWorkers();

                    var sourceReplicationTask = sourceDatabase.StartupTasks.OfType <ReplicationTask>().First();
                    sourceReplicationTask.Pause();

                    SetupReplication(source.DatabaseCommands, destination);

                    //replicate the original index
                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);

                    testIndex.SideBySideExecute(source);

                    //do side-by-side index replication -> since in the destination there is original index,
                    //simply create the side-by-side index as so it will replace the original when it catches up
                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);

                    var originalDefinition = destination.DatabaseCommands.GetIndex(testIndex.IndexName);
                    Assert.NotNull(originalDefinition);
                    Assert.True(originalDefinition.Equals(oldIndexDef, false));

                    var sideBySideDefinition = destination.DatabaseCommands.GetIndex("ReplacementOf/" + testIndex.IndexName);
                    Assert.NotNull(sideBySideDefinition);
                    Assert.True(sideBySideDefinition.Equals(testIndex.CreateIndexDefinition(), false));
                }
        }
Exemplo n.º 6
0
        public async Task Side_by_side_index_after_replication_should_have_appropriate_minimum_etag_for_the_destination_if_applicable()
        {
            using (var source = CreateStore())
                using (var destination = CreateStore())
                {
                    var testIndex   = new UserIndex();
                    var oldIndexDef = new IndexDefinition
                    {
                        Map = "from user in docs.Users\n select new {\n\tName = user.Name\n}"
                    };
                    source.DatabaseCommands.PutIndex(testIndex.IndexName, oldIndexDef);

                    var sourceDatabase      = await servers[0].Server.GetDatabaseInternal(source.DefaultDatabase);
                    var destinationDatabase = await servers[1].Server.GetDatabaseInternal(destination.DefaultDatabase);

                    using (var session = source.OpenSession())
                    {
                        for (int i = 0; i < 10; i++)
                        {
                            session.Store(new User
                            {
                                Name = "User - " + i
                            });
                        }

                        session.SaveChanges();
                    }

                    WaitForIndexing(source);
                    sourceDatabase.StopBackgroundWorkers();
                    destinationDatabase.StopBackgroundWorkers();

                    var sourceReplicationTask = sourceDatabase.StartupTasks.OfType <ReplicationTask>().First();
                    sourceReplicationTask.Pause();

                    SetupReplication(source.DatabaseCommands, destination);

                    //replicate the original index
                    await sourceReplicationTask.ExecuteReplicationOnce(true);

                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);

                    destinationDatabase.SpinBackgroundWorkers();

                    WaitForIndexing(destination);

                    destinationDatabase.StopBackgroundWorkers();

                    testIndex.SideBySideExecute(source, sourceDatabase.Statistics.LastDocEtag);

                    var replacementIndexName = "ReplacementOf/" + testIndex.IndexName;

                    //do side-by-side index replication -> since in the destination there is original index,
                    //simply create the side-by-side index as so it will replace the original when it catches up
                    sourceReplicationTask.ReplicateIndexesAndTransformersTask(null);

                    var originalDefinition = destination.DatabaseCommands.GetIndex(testIndex.IndexName);
                    Assert.NotNull(originalDefinition);
                    Assert.True(originalDefinition.Equals(oldIndexDef, false));

                    var sideBySideDefinition = destination.DatabaseCommands.GetIndex(replacementIndexName);
                    Assert.NotNull(sideBySideDefinition);
                    Assert.True(sideBySideDefinition.Equals(testIndex.CreateIndexDefinition(), false));

                    VerifyReplacementDocumentIsThere(replacementIndexName, destinationDatabase, true);
                }
        }
Exemplo n.º 7
0
        public async Task Should_replicate_index_deletion()
        {
            using (var sourceServer = GetNewServer(8077))
                using (var source = NewRemoteDocumentStore(ravenDbServer: sourceServer))
                    using (var destinationServer1 = GetNewServer(8078))
                        using (var destination1 = NewRemoteDocumentStore(ravenDbServer: destinationServer1))
                            using (var destinationServer2 = GetNewServer())
                                using (var destination2 = NewRemoteDocumentStore(ravenDbServer: destinationServer2))
                                    using (var destinationServer3 = GetNewServer(8081))
                                        using (var destination3 = NewRemoteDocumentStore(ravenDbServer: destinationServer3))
                                        {
                                            CreateDatabaseWithReplication(source, "testDB");
                                            CreateDatabaseWithReplication(destination1, "testDB");
                                            CreateDatabaseWithReplication(destination2, "testDB");
                                            CreateDatabaseWithReplication(destination3, "testDB");

                                            //turn-off automatic index replication - precaution
                                            source.Conventions.IndexAndTransformerReplicationMode = IndexAndTransformerReplicationMode.None;
                                            // ReSharper disable once AccessToDisposedClosure
                                            SetupReplication(source, "testDB", store => false, destination1, destination2, destination3);

                                            //make sure not to replicate the index automatically
                                            var userIndex = new UserIndex();
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(userIndex.IndexName, userIndex.CreateIndexDefinition());

                                            var sourceDB = await sourceServer.Server.GetDatabaseInternal("testDB");

                                            var replicationTask = sourceDB.StartupTasks.OfType <ReplicationTask>().First();
                                            replicationTask.TimeToWaitBeforeSendingDeletesOfIndexesToSiblings = TimeSpan.Zero;
                                            replicationTask.ReplicateIndexesAndTransformersTask(null);

                                            var expectedIndexNames = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase)
                                            {
                                                userIndex.IndexName
                                            };
                                            var indexStatsAfterReplication1 = destination1.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name).ToArray();
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication1));

                                            var indexStatsAfterReplication3 = destination3.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name).ToArray();
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication3));

                                            var indexStatsAfterReplication2 = destination2.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name).ToArray();
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication2));

                                            source.DatabaseCommands.ForDatabase("testDB").DeleteIndex(userIndex.IndexName);

                                            //the index is now replicated on all servers.
                                            //now delete the index and verify that deletion is replicated
                                            replicationTask.ReplicateIndexesAndTransformersTask(null);


                                            indexStatsAfterReplication1 = destination1.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name).ToArray();
                                            Assert.Empty(indexStatsAfterReplication1);

                                            indexStatsAfterReplication2 = destination2.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name).ToArray();
                                            Assert.Empty(indexStatsAfterReplication2);

                                            indexStatsAfterReplication3 = destination3.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name).ToArray();
                                            Assert.Empty(indexStatsAfterReplication3);
                                        }
        }
Exemplo n.º 8
0
        public void CanReplicateIndex()
        {
            var requestFactory = new HttpRavenRequestFactory();

            using (var sourceServer = GetNewServer(8077))
                using (var source = NewRemoteDocumentStore(ravenDbServer: sourceServer))
                    using (var destinationServer = GetNewServer(8078))
                        using (var destination = NewRemoteDocumentStore(ravenDbServer: destinationServer))
                        {
                            CreateDatabaseWithReplication(source, "testDB");
                            CreateDatabaseWithReplication(destination, "testDB");

                            //turn-off automatic index replication - precaution
                            source.Conventions.IndexAndTransformerReplicationMode = IndexAndTransformerReplicationMode.None;
                            SetupReplication(source, "testDB", destination);

                            //make sure not to replicate the index automatically
                            var userIndex = new UserIndex();
                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(userIndex.IndexName, userIndex.CreateIndexDefinition());

                            var indexStatsBeforeReplication = destination.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes;
                            Assert.False(indexStatsBeforeReplication.Any(index => index.Name.Equals(userIndex.IndexName, StringComparison.InvariantCultureIgnoreCase)));

                            var replicationRequestUrl = string.Format("{0}/databases/testDB/replication/replicate-indexes?indexName={1}", source.Url, userIndex.IndexName);
                            var replicationRequest    = requestFactory.Create(replicationRequestUrl, "POST", new RavenConnectionStringOptions
                            {
                                Url = source.Url
                            });
                            replicationRequest.ExecuteRequest();

                            var indexStatsAfterReplication = destination.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes;
                            Assert.True(indexStatsAfterReplication.Any(index => index.Name.Equals(userIndex.IndexName, StringComparison.InvariantCultureIgnoreCase)));
                        }
        }
Exemplo n.º 9
0
        public void Should_skip_index_replication_if_serverside_flag_is_true()
        {
            var requestFactory = new HttpRavenRequestFactory();

            using (var sourceServer = GetNewServer(8077))
                using (var source = NewRemoteDocumentStore(ravenDbServer: sourceServer))
                    using (var destinationServer1 = GetNewServer(8078))
                        using (var destination1 = NewRemoteDocumentStore(ravenDbServer: destinationServer1))
                            using (var destinationServer2 = GetNewServer())
                                using (var destination2 = NewRemoteDocumentStore(ravenDbServer: destinationServer2))
                                    using (var destinationServer3 = GetNewServer(8081))
                                        using (var destination3 = NewRemoteDocumentStore(ravenDbServer: destinationServer3))
                                        {
                                            CreateDatabaseWithReplication(source, "testDB");
                                            CreateDatabaseWithReplication(destination1, "testDB");
                                            CreateDatabaseWithReplication(destination2, "testDB");
                                            CreateDatabaseWithReplication(destination3, "testDB");

                                            //turn-off automatic index replication - precaution
                                            source.Conventions.IndexAndTransformerReplicationMode = IndexAndTransformerReplicationMode.None;
                                            // ReSharper disable once AccessToDisposedClosure
                                            SetupReplication(source, "testDB", store => store == destination2, destination1, destination2, destination3);

                                            //make sure not to replicate the index automatically
                                            var userIndex = new UserIndex();
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(userIndex.IndexName, userIndex.CreateIndexDefinition());

                                            var replicationRequestUrl = string.Format("{0}/databases/testDB/replication/replicate-indexes?op=replication&indexName={1}", source.Url, userIndex.IndexName);
                                            var replicationRequest    = requestFactory.Create(replicationRequestUrl, "POST", new RavenConnectionStringOptions
                                            {
                                                Url = source.Url
                                            });
                                            replicationRequest.ExecuteRequest();

                                            var indexStatsAfterReplication = destination1.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes;
                                            Assert.True(indexStatsAfterReplication.Any(index => index.Name.Equals(userIndex.IndexName, StringComparison.InvariantCultureIgnoreCase)));

                                            //this one should not have replicated index -> because of SkipIndexReplication = true in ReplicationDocument of source
                                            indexStatsAfterReplication = destination2.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes;
                                            Assert.False(indexStatsAfterReplication.Any(index => index.Name.Equals(userIndex.IndexName, StringComparison.InvariantCultureIgnoreCase)));

                                            indexStatsAfterReplication = destination3.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes;
                                            Assert.True(indexStatsAfterReplication.Any(index => index.Name.Equals(userIndex.IndexName, StringComparison.InvariantCultureIgnoreCase)));
                                        }
        }
Exemplo n.º 10
0
        public void Replicate_all_indexes_should_respect_disable_indexing_flag()
        {
            var requestFactory = new HttpRavenRequestFactory();

            using (var sourceServer = GetNewServer(9077))
                using (var source = NewRemoteDocumentStore(ravenDbServer: sourceServer, fiddler: true))
                    using (var destinationServer1 = GetNewServer(9078))
                        using (var destination1 = NewRemoteDocumentStore(ravenDbServer: destinationServer1, fiddler: true))
                            using (var destinationServer2 = GetNewServer())
                                using (var destination2 = NewRemoteDocumentStore(ravenDbServer: destinationServer2, fiddler: true))
                                    using (var destinationServer3 = GetNewServer(9081))
                                        using (var destination3 = NewRemoteDocumentStore(ravenDbServer: destinationServer3, fiddler: true))
                                        {
                                            CreateDatabaseWithReplication(source, "testDB");
                                            CreateDatabaseWithReplication(destination1, "testDB");
                                            CreateDatabaseWithReplication(destination2, "testDB");
                                            CreateDatabaseWithReplication(destination3, "testDB");

                                            //turn-off automatic index replication - precaution
                                            source.Conventions.IndexAndTransformerReplicationMode = IndexAndTransformerReplicationMode.None;
                                            // ReSharper disable once AccessToDisposedClosure
                                            //we setup replication so replication to destination2 will have "disabled" flag
                                            SetupReplication(source, "testDB", store => store == destination2, destination1, destination2, destination3);

                                            //make sure not to replicate the index automatically
                                            var userIndex           = new UserIndex();
                                            var anotherUserIndex    = new AnotherUserIndex();
                                            var yetAnotherUserIndex = new YetAnotherUserIndex();
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(userIndex.IndexName, userIndex.CreateIndexDefinition());
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(anotherUserIndex.IndexName, anotherUserIndex.CreateIndexDefinition());
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(yetAnotherUserIndex.IndexName, yetAnotherUserIndex.CreateIndexDefinition());

                                            var replicationRequestUrl = string.Format("{0}/databases/testDB/replication/replicate-indexes?op=replicate-all", source.Url);
                                            var replicationRequest    = requestFactory.Create(replicationRequestUrl, "POST", new RavenConnectionStringOptions
                                            {
                                                Url = source.Url
                                            });
                                            replicationRequest.ExecuteRequest();

                                            var expectedIndexNames = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase)
                                            {
                                                userIndex.IndexName, anotherUserIndex.IndexName, yetAnotherUserIndex.IndexName
                                            };
                                            var indexStatsAfterReplication1 = destination1.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication1));


                                            var indexStatsAfterReplication3 = destination3.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication3));

                                            //since destination2 has disabled flag - indexes should not replicate to here
                                            var indexStatsAfterReplication2 = destination2.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);
                                            Assert.Empty(indexStatsAfterReplication2);
                                        }
        }
Exemplo n.º 11
0
        public void Should_replicate_all_indexes_only_to_specific_destination_if_relevant_endpoint_hit()
        {
            var requestFactory = new HttpRavenRequestFactory();

            using (var sourceServer = GetNewServer(8077))
                using (var source = NewRemoteDocumentStore(ravenDbServer: sourceServer))
                    using (var destinationServer1 = GetNewServer(8078))
                        using (var destination1 = NewRemoteDocumentStore(ravenDbServer: destinationServer1))
                            using (var destinationServer2 = GetNewServer())
                                using (var destination2 = NewRemoteDocumentStore(ravenDbServer: destinationServer2))
                                    using (var destinationServer3 = GetNewServer(8081))
                                        using (var destination3 = NewRemoteDocumentStore(ravenDbServer: destinationServer3))
                                        {
                                            CreateDatabaseWithReplication(source, "testDB");
                                            CreateDatabaseWithReplication(destination1, "testDB");
                                            CreateDatabaseWithReplication(destination2, "testDB");
                                            CreateDatabaseWithReplication(destination3, "testDB");

                                            //turn-off automatic index replication - precaution
                                            source.Conventions.IndexAndTransformerReplicationMode = IndexAndTransformerReplicationMode.None;
                                            // ReSharper disable once AccessToDisposedClosure
                                            var destinationDocuments = SetupReplication(source, "testDB", store => false, destination1, destination2, destination3);

                                            //make sure not to replicate the index automatically
                                            var userIndex           = new UserIndex();
                                            var anotherUserIndex    = new AnotherUserIndex();
                                            var yetAnotherUserIndex = new YetAnotherUserIndex();
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(userIndex.IndexName, userIndex.CreateIndexDefinition());
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(anotherUserIndex.IndexName, anotherUserIndex.CreateIndexDefinition());
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(yetAnotherUserIndex.IndexName, yetAnotherUserIndex.CreateIndexDefinition());

                                            var replicationRequestUrl = string.Format("{0}/databases/testDB/replication/replicate-indexes?op=replicate-all-to-destination", source.Url);
                                            var replicationRequest    = requestFactory.Create(replicationRequestUrl, "POST", new RavenConnectionStringOptions
                                            {
                                                Url = source.Url
                                            });

                                            replicationRequest.Write(RavenJObject.FromObject(destinationDocuments[1]));
                                            replicationRequest.ExecuteRequest();

                                            var expectedIndexNames = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase)
                                            {
                                                userIndex.IndexName, anotherUserIndex.IndexName, yetAnotherUserIndex.IndexName
                                            };
                                            var indexStatsAfterReplication1 = destination1.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);
                                            var indexStatsAfterReplication2 = destination2.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);
                                            var indexStatsAfterReplication3 = destination3.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);

                                            Assert.Equal(0, indexStatsAfterReplication1.Count());
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication2));
                                            Assert.Equal(0, indexStatsAfterReplication3.Count());
                                        }
        }
Exemplo n.º 12
0
        public async Task Should_send_last_queried_index_time_periodically()
        {
            using (var sourceServer = GetNewServer(8077))
                using (var source = NewRemoteDocumentStore(ravenDbServer: sourceServer))
                    using (var destinationServer1 = GetNewServer(8078))
                        using (var destination1 = NewRemoteDocumentStore(ravenDbServer: destinationServer1))
                            using (var destinationServer2 = GetNewServer())
                                using (var destination2 = NewRemoteDocumentStore(ravenDbServer: destinationServer2))
                                    using (var destinationServer3 = GetNewServer(8081))
                                        using (var destination3 = NewRemoteDocumentStore(ravenDbServer: destinationServer3))
                                        {
                                            CreateDatabaseWithReplication(source, "testDB");
                                            CreateDatabaseWithReplication(destination1, "testDB");
                                            CreateDatabaseWithReplication(destination2, "testDB");
                                            CreateDatabaseWithReplication(destination3, "testDB");

                                            //turn-off automatic index replication - precaution
                                            source.Conventions.IndexAndTransformerReplicationMode = IndexAndTransformerReplicationMode.None;
                                            // ReSharper disable once AccessToDisposedClosure
                                            SetupReplication(source, "testDB", store => false, destination1, destination2, destination3);

                                            //make sure not to replicate the index automatically
                                            var userIndex           = new UserIndex();
                                            var anotherUserIndex    = new AnotherUserIndex();
                                            var yetAnotherUserIndex = new YetAnotherUserIndex();
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(userIndex.IndexName, userIndex.CreateIndexDefinition());
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(anotherUserIndex.IndexName, anotherUserIndex.CreateIndexDefinition());
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(yetAnotherUserIndex.IndexName, yetAnotherUserIndex.CreateIndexDefinition());

                                            using (var session = source.OpenSession("testDB"))
                                            {
// ReSharper disable ReturnValueOfPureMethodIsNotUsed
                                                session.Query <UserIndex>(userIndex.IndexName).ToList();                     //update last queried time
                                                session.Query <AnotherUserIndex>(anotherUserIndex.IndexName).ToList();       //update last queried time
                                                session.Query <YetAnotherUserIndex>(yetAnotherUserIndex.IndexName).ToList(); //update last queried time
// ReSharper restore ReturnValueOfPureMethodIsNotUsed

                                                session.SaveChanges();
                                            }

                                            var sourceDB = await sourceServer.Server.GetDatabaseInternal("testDB");

                                            var replicationTask = sourceDB.StartupTasks.OfType <ReplicationTask>().First();
                                            replicationTask.SendLastQueriedTask(null);

                                            var indexNames = new[] { userIndex.IndexName, anotherUserIndex.IndexName, yetAnotherUserIndex.IndexName };

                                            var sourceIndexStats       = source.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Where(x => indexNames.Contains(x.Name)).ToList();
                                            var destination1IndexStats = source.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Where(x => indexNames.Contains(x.Name)).ToList();
                                            var destination2IndexStats = source.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Where(x => indexNames.Contains(x.Name)).ToList();
                                            var destination3IndexStats = source.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Where(x => indexNames.Contains(x.Name)).ToList();

                                            Assert.NotNull(sourceIndexStats.First(x => x.Name == userIndex.IndexName).LastQueryTimestamp);           //sanity check
                                            Assert.NotNull(sourceIndexStats.First(x => x.Name == anotherUserIndex.IndexName).LastQueryTimestamp);    //sanity check
                                            Assert.NotNull(sourceIndexStats.First(x => x.Name == yetAnotherUserIndex.IndexName).LastQueryTimestamp); //sanity check

                                            Assert.Equal(sourceIndexStats.First(x => x.Name == userIndex.IndexName).LastQueryTimestamp, destination1IndexStats.First(x => x.Name == userIndex.IndexName).LastQueryTimestamp);
                                            Assert.Equal(sourceIndexStats.First(x => x.Name == anotherUserIndex.IndexName).LastQueryTimestamp, destination1IndexStats.First(x => x.Name == anotherUserIndex.IndexName).LastQueryTimestamp);
                                            Assert.Equal(sourceIndexStats.First(x => x.Name == yetAnotherUserIndex.IndexName).LastQueryTimestamp, destination1IndexStats.First(x => x.Name == yetAnotherUserIndex.IndexName).LastQueryTimestamp);

                                            Assert.Equal(sourceIndexStats.First(x => x.Name == userIndex.IndexName).LastQueryTimestamp, destination2IndexStats.First(x => x.Name == userIndex.IndexName).LastQueryTimestamp);
                                            Assert.Equal(sourceIndexStats.First(x => x.Name == anotherUserIndex.IndexName).LastQueryTimestamp, destination2IndexStats.First(x => x.Name == anotherUserIndex.IndexName).LastQueryTimestamp);
                                            Assert.Equal(sourceIndexStats.First(x => x.Name == yetAnotherUserIndex.IndexName).LastQueryTimestamp, destination2IndexStats.First(x => x.Name == yetAnotherUserIndex.IndexName).LastQueryTimestamp);

                                            Assert.Equal(sourceIndexStats.First(x => x.Name == userIndex.IndexName).LastQueryTimestamp, destination3IndexStats.First(x => x.Name == userIndex.IndexName).LastQueryTimestamp);
                                            Assert.Equal(sourceIndexStats.First(x => x.Name == anotherUserIndex.IndexName).LastQueryTimestamp, destination3IndexStats.First(x => x.Name == anotherUserIndex.IndexName).LastQueryTimestamp);
                                            Assert.Equal(sourceIndexStats.First(x => x.Name == yetAnotherUserIndex.IndexName).LastQueryTimestamp, destination3IndexStats.First(x => x.Name == yetAnotherUserIndex.IndexName).LastQueryTimestamp);
                                        }
        }
Exemplo n.º 13
0
        public async Task Should_replicate_indexes_periodically()
        {
            using (var sourceServer = GetNewServer(8077))
                using (var source = NewRemoteDocumentStore(ravenDbServer: sourceServer))
                    using (var destinationServer1 = GetNewServer(8078))
                        using (var destination1 = NewRemoteDocumentStore(ravenDbServer: destinationServer1))
                            using (var destinationServer2 = GetNewServer())
                                using (var destination2 = NewRemoteDocumentStore(ravenDbServer: destinationServer2))
                                    using (var destinationServer3 = GetNewServer(8081))
                                        using (var destination3 = NewRemoteDocumentStore(ravenDbServer: destinationServer3))
                                        {
                                            CreateDatabaseWithReplication(source, "testDB");
                                            CreateDatabaseWithReplication(destination1, "testDB");
                                            CreateDatabaseWithReplication(destination2, "testDB");
                                            CreateDatabaseWithReplication(destination3, "testDB");

                                            //turn-off automatic index replication - precaution
                                            source.Conventions.IndexAndTransformerReplicationMode = IndexAndTransformerReplicationMode.None;
                                            // ReSharper disable once AccessToDisposedClosure
                                            SetupReplication(source, "testDB", store => false, destination1, destination2, destination3);

                                            //make sure not to replicate the index automatically
                                            var userIndex           = new UserIndex();
                                            var anotherUserIndex    = new AnotherUserIndex();
                                            var yetAnotherUserIndex = new YetAnotherUserIndex();
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(userIndex.IndexName, userIndex.CreateIndexDefinition());
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(anotherUserIndex.IndexName, anotherUserIndex.CreateIndexDefinition());
                                            source.DatabaseCommands.ForDatabase("testDB").PutIndex(yetAnotherUserIndex.IndexName, yetAnotherUserIndex.CreateIndexDefinition());

                                            var sourceDB = await sourceServer.Server.GetDatabaseInternal("testDB");

                                            var replicationTask = sourceDB.StartupTasks.OfType <ReplicationTask>().First();
                                            SpinWait.SpinUntil(() => replicationTask.ReplicateIndexesAndTransformersTask(null));

                                            var expectedIndexNames = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase)
                                            {
                                                userIndex.IndexName, anotherUserIndex.IndexName, yetAnotherUserIndex.IndexName
                                            };
                                            var indexStatsAfterReplication1 = destination1.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication1));

                                            var indexStatsAfterReplication3 = destination3.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication3));

                                            var indexStatsAfterReplication2 = destination2.DatabaseCommands.ForDatabase("testDB").GetStatistics().Indexes.Select(x => x.Name);
                                            Assert.True(expectedIndexNames.SetEquals(indexStatsAfterReplication2));
                                        }
        }
Exemplo n.º 14
0
        public async Task If_deleted_original_index_on_destination_but_not_side_by_side_index()
        {
            using (var source = CreateStore())
                using (var destination = CreateStore())
                {
                    var sourceDatabase      = await servers[0].Server.GetDatabaseInternal(source.DefaultDatabase);
                    var destinationDatabase = await servers[1].Server.GetDatabaseInternal(destination.DefaultDatabase);
                    sourceDatabase.StopBackgroundWorkers();
                    destinationDatabase.StopBackgroundWorkers();

                    var testIndex = new UserIndex();

                    var oldIndexDef = new IndexDefinition
                    {
                        Map = "from user in docs.Users\n select new {\n\tName = user.Name\n}"
                    };

                    source.DatabaseCommands.PutIndex(testIndex.IndexName, oldIndexDef);

                    using (var session = source.OpenSession())
                    {
                        session.Store(new User
                        {
                            Name = "John Doe"
                        });
                        session.SaveChanges();
                    }

                    var sourceReplicationTask = sourceDatabase.StartupTasks.OfType <ReplicationTask>().First();
                    sourceReplicationTask.Pause();

                    SetupReplication(source.DatabaseCommands, destination);

                    sourceReplicationTask.IndexReplication.Execute(); //replicate the usual index

                    source.SideBySideExecuteIndex(testIndex);

                    //the side by side index will be automatically replicated
                    SpinWait.SpinUntil(() =>
                    {
                        var index = destinationDatabase.Indexes.GetIndexDefinition(Constants.SideBySideIndexNamePrefix + testIndex.IndexName);
                        return(index != null);
                    }, 5000);

                    Assert.NotNull(destinationDatabase.Indexes.GetIndexDefinition(Constants.SideBySideIndexNamePrefix + testIndex.IndexName));

                    destinationDatabase.Indexes.DeleteIndex(testIndex.IndexName); //delete the original index

                    var sideBySideIndex = destinationDatabase.Indexes.GetIndexDefinition(Constants.SideBySideIndexNamePrefix + testIndex.IndexName);
                    Assert.NotNull(sideBySideIndex);

                    VerifyReplacementDocumentIsThere(Constants.SideBySideIndexNamePrefix + testIndex.IndexName, destinationDatabase);

                    sourceReplicationTask.IndexReplication.Execute();

                    var indexDefinition = destinationDatabase.Indexes.Definitions.First(x => x.Name == Constants.SideBySideIndexNamePrefix + testIndex.IndexName);
                    destinationDatabase.IndexReplacer.ForceReplacement(indexDefinition);

                    //wait until the index will be replaced
                    SpinWait.SpinUntil(() =>
                    {
                        var index = destinationDatabase.Indexes.GetIndexDefinition(testIndex.IndexName);
                        return(index != null);
                    }, 5000);

                    var replacingIndex = destinationDatabase.Indexes.GetIndexDefinition(testIndex.IndexName);
                    Assert.True(replacingIndex != null);

                    var oldIndex = destinationDatabase.Indexes.GetIndexDefinition(testIndex.IndexName);
                    Assert.True(oldIndex.Equals(testIndex.CreateIndexDefinition(), false));

                    sideBySideIndex = destinationDatabase.Indexes.GetIndexDefinition(Constants.SideBySideIndexNamePrefix + testIndex.IndexName);
                    Assert.Null(sideBySideIndex);

                    SpinWait.SpinUntil(() =>
                    {
                        var doc = destinationDatabase.Documents.Get(Constants.IndexReplacePrefix + Constants.SideBySideIndexNamePrefix + testIndex.IndexName, null);
                        return(doc == null);
                    }, 5000);

                    Assert.Null(destinationDatabase.Documents.Get(Constants.IndexReplacePrefix + Constants.SideBySideIndexNamePrefix + testIndex.IndexName, null));
                }
        }