Exemplo n.º 1
0
        public virtual void Dispose()
        {
            GC.SuppressFinalize(this);

            var exceptionAggregator = new ExceptionAggregator("Could not dispose test");

            foreach (var store in CreatedStores)
            {
                exceptionAggregator.Execute(store.Dispose);
            }
            CreatedStores.Clear();

            if (_localServer != null)
            {
                if (_doNotReuseServer)
                {
                    exceptionAggregator.Execute(() =>
                    {
                        _localServer.Dispose();
                        _localServer = null;
                        RemoveUsedPort(NonReusedServerPort);
                        RemoveUsedPort(NonReusedTcpServerPort);
                    });
                }

                exceptionAggregator.ThrowIfNeeded();
            }
        }
Exemplo n.º 2
0
        public virtual void Dispose()
        {
            GC.SuppressFinalize(this);

            if (_concurrentTestsSemaphoreTaken.Lower())
            {
                ConcurrentTestsSemaphore.Release();
            }

            var exceptionAggregator = new ExceptionAggregator("Could not dispose test");

            Dispose(exceptionAggregator);

            if (_localServer != null && _localServer != _globalServer)
            {
                exceptionAggregator.Execute(() =>
                {
                    _localServer.Dispose();
                    _localServer = null;
                });
            }

            RavenTestHelper.DeletePaths(_localPathsToDelete, exceptionAggregator);

            exceptionAggregator.ThrowIfNeeded();
        }
Exemplo n.º 3
0
        private static void DisposeServerAndWaitForFinishOfDisposal(RavenServer serverToDispose)
        {
            var mre = new ManualResetEventSlim();

            serverToDispose.AfterDisposal += () => mre.Set();
            serverToDispose.Dispose();
            mre.Wait();
        }
Exemplo n.º 4
0
        protected static void DisposeServerAndWaitForFinishOfDisposal(RavenServer serverToDispose)
        {
            var mre = new ManualResetEventSlim();

            serverToDispose.AfterDisposal += () => mre.Set();
            serverToDispose.Dispose();

            Assert.True(mre.Wait(TimeSpan.FromMinutes(1)), $"Could not dispose server: {serverToDispose.WebUrl}");
        }
Exemplo n.º 5
0
        protected static async Task DisposeServerAndWaitForFinishOfDisposalAsync(RavenServer serverToDispose, CancellationToken token = default)
        {
            var mre = new AsyncManualResetEvent();

            serverToDispose.AfterDisposal += () => mre.Set();
            serverToDispose.Dispose();

            await mre.WaitAsync(token).ConfigureAwait(false);
        }
Exemplo n.º 6
0
 public void UseNewLocalServer(string customConfigPath = null)
 {
     _localServer?.Dispose();
     if (_localServer != null)
     {
         Servers.Remove(_localServer);
     }
     _localServer = GetNewServer(_customServerSettings, customConfigPath: customConfigPath);
 }
Exemplo n.º 7
0
 public void UseNewLocalServer(IDictionary <string, string> customSettings = null, bool runInMemory = true, string customConfigPath = null)
 {
     _localServer?.Dispose();
     if (_localServer != null)
     {
         Servers.Remove(_localServer);
     }
     _localServer = GetNewServer(customSettings: customSettings ?? _customServerSettings, runInMemory: runInMemory, customConfigPath: customConfigPath);
 }
Exemplo n.º 8
0
        public void Stop()
        {
            if (Logger.IsInfoEnabled)
            {
                Logger.Info($"Stopping RavenDB Windows Service: {ServiceName}.");
            }

            _ravenServer.Dispose();
        }
Exemplo n.º 9
0
        protected RachisConsensus <CountingStateMachine> SetupServer(bool bootstrap = false, int port = 0, int electionTimeout = 300, [CallerMemberName] string caller = null)
        {
            var tcpListener = new TcpListener(IPAddress.Loopback, port);

            tcpListener.Start();
            char ch;

            if (bootstrap)
            {
                ch = (char)65;
            }
            else
            {
                ch = (char)(65 + Interlocked.Increment(ref _count));
            }

            var url = $"tcp://localhost:{((IPEndPoint)tcpListener.LocalEndpoint).Port}/?{caller}#{ch}";

            var server = StorageEnvironmentOptions.CreateMemoryOnly();

            int seed          = PredictableSeeds ? _random.Next(int.MaxValue) : (int)Interlocked.Read(ref _count);
            var configuration = RavenConfiguration.CreateForServer(caller);

            configuration.Initialize();
            configuration.Core.RunInMemory        = true;
            configuration.Core.PublicServerUrl    = new UriSetting($"http://localhost:{((IPEndPoint)tcpListener.LocalEndpoint).Port}");
            configuration.Cluster.ElectionTimeout = new TimeSetting(electionTimeout, TimeUnit.Milliseconds);
            var serverStore = new RavenServer(configuration)
            {
                ThrowOnLicenseActivationFailure = true
            }.ServerStore;

            serverStore.Initialize();
            var rachis             = new RachisConsensus <CountingStateMachine>(serverStore, seed);
            var storageEnvironment = new StorageEnvironment(server);

            rachis.Initialize(storageEnvironment, configuration, configuration.Core.ServerUrls[0], out _);
            rachis.OnDispose += (sender, args) =>
            {
                serverStore.Dispose();
                storageEnvironment.Dispose();
            };
            if (bootstrap)
            {
                rachis.Bootstrap(url, "A");
            }

            rachis.Url = url;
            _listeners.Add(tcpListener);
            RachisConsensuses.Add(rachis);
            var task = AcceptConnection(tcpListener, rachis);

            return(rachis);
        }
Exemplo n.º 10
0
        public void Restart()
        {
            if (Logger.IsInfoEnabled)
            {
                Logger.Info($"Restarting RavenDB Windows Service: {ServiceName}.");
            }

            _ravenServer.Dispose();
            var configuration = RavenConfiguration.CreateForServer(null, CommandLineSwitches.CustomConfigPath);

            if (_args != null)
            {
                configuration.AddCommandLine(_args);
            }

            configuration.Initialize();
            _ravenServer = new RavenServer(configuration);
            Start(_args, _serviceStoppedCallback);

            configuration.Initialize();
        }
Exemplo n.º 11
0
        protected static async Task <(string DataDir, string Url)> DisposeServerAndWaitForFinishOfDisposalAsync(RavenServer serverToDispose, CancellationToken token = default)
        {
            var mre     = new AsyncManualResetEvent();
            var dataDir = serverToDispose.Configuration.Core.DataDirectory.FullPath.Split('/').Last();
            var url     = serverToDispose.WebUrl;

            serverToDispose.AfterDisposal += () => mre.Set();
            serverToDispose.Dispose();

            await mre.WaitAsync(token).ConfigureAwait(false);

            return(dataDir, url);
        }
Exemplo n.º 12
0
        protected static async Task <(string DataDirectory, string Url, string NodeTag)> DisposeServerAndWaitForFinishOfDisposalAsync(RavenServer serverToDispose)
        {
            var mre           = new AsyncManualResetEvent();
            var dataDirectory = serverToDispose.Configuration.Core.DataDirectory.FullPath;
            var url           = serverToDispose.WebUrl;
            var nodeTag       = serverToDispose.ServerStore.NodeTag;

            serverToDispose.AfterDisposal += () => mre.Set();
            serverToDispose.Dispose();

            Assert.True(await mre.WaitAsync(TimeSpan.FromMinutes(1)), $"Could not dispose server: {url}");

            return(dataDirectory, url, nodeTag);
        }
Exemplo n.º 13
0
        protected RachisConsensus <CountingStateMachine> SetupServer(bool bootstrap = false, int port = 0, int electionTimeout = 300, [CallerMemberName] string caller = null)
        {
            var tcpListener = new TcpListener(IPAddress.Loopback, port);

            tcpListener.Start();
            var ch = (char)(66 + _count++);

            if (bootstrap)
            {
                ch = (char)65;
                _count--;
            }

            var url = "tcp://localhost:" + ((IPEndPoint)tcpListener.LocalEndpoint).Port + "/?" + caller + "#" + ch;

            var server = StorageEnvironmentOptions.CreateMemoryOnly();

            int seed          = PredictableSeeds ? _random.Next(int.MaxValue) : _count;
            var configuration = new RavenConfiguration(caller, ResourceType.Server);

            configuration.Initialize();
            configuration.Core.RunInMemory        = true;
            configuration.Core.PublicServerUrl    = new UriSetting($"http://localhost:{((IPEndPoint)tcpListener.LocalEndpoint).Port}");
            configuration.Cluster.ElectionTimeout = new TimeSetting(electionTimeout, TimeUnit.Milliseconds);
            var serverStore = new RavenServer(configuration).ServerStore;

            serverStore.Initialize();
            var rachis             = new RachisConsensus <CountingStateMachine>(serverStore, seed);
            var storageEnvironment = new StorageEnvironment(server);

            rachis.Initialize(storageEnvironment, configuration, configuration.Core.ServerUrls[0]);
            rachis.OnDispose += (sender, args) =>
            {
                serverStore.Dispose();
                storageEnvironment.Dispose();
            };
            if (bootstrap)
            {
                rachis.Bootstrap(url, "A");
            }

            rachis.Url = url;
            _listeners.Add(tcpListener);
            RachisConsensuses.Add(rachis);
            var task = AcceptConnection(tcpListener, rachis);

            return(rachis);
        }
Exemplo n.º 14
0
        public void UseNewLocalServer(IDictionary <string, string> customSettings = null, bool?runInMemory = null, string customConfigPath = null, [CallerMemberName] string caller = null)
        {
            if (_localServer != _globalServer && _globalServer != null)
            {
                _localServer?.Dispose();
            }

            var co = new ServerCreationOptions
            {
                CustomSettings      = customSettings ?? _customServerSettings,
                RunInMemory         = runInMemory,
                CustomConfigPath    = customConfigPath,
                RegisterForDisposal = false
            };

            _localServer = GetNewServer(co, caller);
        }
Exemplo n.º 15
0
        public void UseNewLocalServer(IDictionary <string, string> customSettings = null, bool runInMemory = true, string customConfigPath = null)
        {
            _localServer?.Dispose();
            if (_localServer != null)
            {
                Servers.Remove(_localServer);
            }
            var co = new ServerCreationOptions
            {
                CustomSettings      = customSettings ?? _customServerSettings,
                RunInMemory         = runInMemory,
                CustomConfigPath    = customConfigPath,
                RegisterForDisposal = false
            };

            _localServer = GetNewServer(co);
        }
Exemplo n.º 16
0
 public void UseNewLocalServer()
 {
     _localServer?.Dispose();
     _localServer = GetNewServer(_customServerSettings);
 }
Exemplo n.º 17
0
        public async Task ShouldKeepPullingDocsAfterServerRestart()
        {
            var dataPath = NewDataPath();

            IDocumentStore store  = null;
            RavenServer    server = null;
            SubscriptionWorker <dynamic> subscriptionWorker = null;

            try
            {
                var co = new ServerCreationOptions
                {
                    RunInMemory    = false,
                    CustomSettings = new Dictionary <string, string>()
                    {
                        [RavenConfiguration.GetKey(x => x.Core.DataDirectory)] = dataPath
                    },
                    RegisterForDisposal = false
                };

                server = GetNewServer(co);

                store = new DocumentStore()
                {
                    Urls     = new[] { server.ServerStore.GetNodeHttpServerUrl() },
                    Database = "RavenDB_2627",
                }.Initialize();

                var doc    = new DatabaseRecord(store.Database);
                var result = store.Maintenance.Server.Send(new CreateDatabaseOperationWithoutNameValidation(doc));
                await WaitForRaftIndexToBeAppliedInCluster(result.RaftCommandIndex, _reasonableWaitTime);

                using (var session = store.OpenSession())
                {
                    session.Store(new User());
                    session.Store(new User());
                    session.Store(new User());
                    session.Store(new User());

                    session.SaveChanges();
                }

                var id = store.Subscriptions.Create(new SubscriptionCreationOptions <User>());

                subscriptionWorker = store.Subscriptions.GetSubscriptionWorker(new SubscriptionWorkerOptions(id)
                {
                    TimeToWaitBeforeConnectionRetry = TimeSpan.FromSeconds(1),
                    MaxDocsPerBatch = 1
                });


                var gotBatch = new ManualResetEventSlim();
                var gotArek  = new ManualResetEventSlim();
                var t        = subscriptionWorker.Run(x =>
                {
                    gotBatch.Set();

                    foreach (var item in x.Items)
                    {
                        if (item.Id == "users/arek")
                        {
                            gotArek.Set();
                        }
                    }
                });

                Assert.True(gotBatch.Wait(_reasonableWaitTime));

                Server.ServerStore.DatabasesLandlord.UnloadDirectly(store.Database);

                for (int i = 0; i < 150; i++)
                {
                    try
                    {
                        using (var session = store.OpenSession())
                        {
                            session.Store(new User(), "users/arek");
                            session.SaveChanges();
                        }
                        break;
                    }
                    catch
                    {
                        Thread.Sleep(25);
                        if (i > 100)
                        {
                            throw;
                        }
                    }
                }

                Assert.True(gotArek.Wait(_reasonableWaitTime));
            }
            finally
            {
                subscriptionWorker?.Dispose();
                store?.Dispose();
                server.Dispose();
            }
        }
Exemplo n.º 18
0
 public void UseNewLocalServer(string customConfigPath = null)
 {
     _localServer?.Dispose();
     _localServer = GetNewServer(_customServerSettings, customConfigPath: customConfigPath);
 }