Пример #1
0
        public void When_file_already_exists_and_is_too_large_a_new_sequence_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var dateTime = new DateTime(2010, 10, 1);
                var logger1  = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => dateTime
                };
                logger1.Write("Some long text");
                var logger2 = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => dateTime
                };
                logger2.Write("Bar");
                var files = tempPath.GetFiles();

                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Some long text\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(files.First()));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_1.txt", Path.GetFileName(second));
                Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
        public void When_many_files_written_over_size_old_files_are_deleted()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, numberOfArchiveFilesToKeep: 2, maxFileSize: 5)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.WriteLine("Long text0");
                logger.WriteLine("Long text1");
                logger.WriteLine("Long text2");
                logger.WriteLine("Long text3");
                logger.WriteLine("Long text4");
                var files = tempPath.GetFiles();
                Assert.AreEqual(3, files.Count, "Should be numberOfArchiveFilesToKeep + 1 (the current file) ");

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_2.txt", Path.GetFileName(first));
                Assert.AreEqual($"Long text2{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_3.txt", Path.GetFileName(second));
                Assert.AreEqual($"Long text3{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(second));

                var third = files[2];
                Assert.AreEqual("nsb_log_2010-10-01_4.txt", Path.GetFileName(third));
                Assert.AreEqual($"Long text4{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(third));
            }
        }
        public void When_many_files_written_over_dates_old_files_are_deleted()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, numberOfArchiveFilesToKeep: 2)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.WriteLine("Foo1");
                logger.GetDate = () => new DateTime(2010, 10, 2);
                logger.WriteLine("Foo2");
                logger.GetDate = () => new DateTime(2010, 10, 3);
                logger.WriteLine("Foo3");
                logger.GetDate = () => new DateTime(2010, 10, 4);
                logger.WriteLine("Foo4");
                logger.GetDate = () => new DateTime(2010, 10, 5);
                logger.WriteLine("Foo5");
                var files = tempPath.GetFiles();
                Assert.AreEqual(3, files.Count, "Should be numberOfArchiveFilesToKeep + 1 (the current file) ");

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-03_0.txt", Path.GetFileName(first));
                Assert.AreEqual($"Foo3{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-04_0.txt", Path.GetFileName(second));
                Assert.AreEqual($"Foo4{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(second));

                var third = files[2];
                Assert.AreEqual("nsb_log_2010-10-05_0.txt", Path.GetFileName(third));
                Assert.AreEqual($"Foo5{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(third));
            }
        }
        public void When_file_already_exists_with_wrong_date_a_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var logger1 = new RollingLogger(tempPath.TempDirectory)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger1.WriteLine("Foo");
                var logger2 = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => new DateTime(2010, 10, 2)
                };
                logger2.WriteLine("Bar");
                var files = tempPath.GetFiles();

                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual($"Foo{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(files.First()));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-02_0.txt", Path.GetFileName(second));
                Assert.AreEqual($"Bar{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
 public void When_line_is_write_line_appears_in_file()
 {
     using (var tempPath = new TempPath())
     {
         var logger = new RollingLogger(tempPath.TempDirectory);
         logger.WriteLine("Foo");
         var singleFile = tempPath.GetSingle();
         Assert.AreEqual($"Foo{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(singleFile));
     }
 }
Пример #6
0
 public void When_new_write_causes_overlap_of_file_size_line_is_written_to_current_file()
 {
     using (var tempPath = new TempPath())
     {
         var logger = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10);
         logger.Write("Foo");
         logger.Write("Some long text");
         var singleFile = tempPath.GetSingle();
         Assert.AreEqual("Foo\r\nSome long text\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(singleFile));
     }
 }
Пример #7
0
 public void When_multiple_lines_are_written_lines_appears_in_file()
 {
     using (var tempPath = new TempPath())
     {
         var logger = new RollingLogger(tempPath.TempDirectory);
         logger.Write("Foo");
         logger.Write("Bar");
         var singleFile = tempPath.GetSingle();
         Assert.AreEqual("Foo\r\nBar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(singleFile));
     }
 }
Пример #8
0
        public void StartRaven(EmbeddableDocumentStore documentStore, Settings settings, MarkerFileService markerFileService, bool maintenanceMode)
        {
            Directory.CreateDirectory(settings.DbPath);

            documentStore.DataDirectory                   = settings.DbPath;
            documentStore.UseEmbeddedHttpServer           = maintenanceMode || settings.ExposeRavenDB;
            documentStore.EnlistInDistributedTransactions = false;

            var localRavenLicense = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RavenLicense.xml");

            if (File.Exists(localRavenLicense))
            {
                Logger.InfoFormat("Loading RavenDB license found from {0}", localRavenLicense);
                documentStore.Configuration.Settings["Raven/License"] = NonLockingFileReader.ReadAllTextWithoutLocking(localRavenLicense);
            }
            else
            {
                Logger.InfoFormat("Loading Embedded RavenDB license");
                documentStore.Configuration.Settings["Raven/License"] = ReadLicense();
            }

            //This is affects only remote access to the database in maintenace mode and enables access without authentication
            documentStore.Configuration.Settings["Raven/AnonymousAccess"] = "Admin";
            documentStore.Configuration.Settings["Raven/Licensing/AllowAdminAnonymousAccessForCommercialUse"] = "true";

            if (!maintenanceMode)
            {
                documentStore.Configuration.Settings.Add("Raven/ActiveBundles", "CustomDocumentExpiration");
            }

            documentStore.Configuration.DisableClusterDiscovery     = true;
            documentStore.Configuration.ResetIndexOnUncleanShutdown = true;
            documentStore.Configuration.Port     = settings.DatabaseMaintenancePort;
            documentStore.Configuration.HostName = settings.Hostname == "*" || settings.Hostname == "+"
                ? "localhost"
                : settings.Hostname;
            documentStore.Configuration.CompiledIndexCacheDirectory = settings.DbPath;
            documentStore.Conventions.SaveEnumsAsIntegers           = true;

            documentStore.Configuration.Catalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));

            using (markerFileService.CreateMarker("datamigration.marker"))
            {
                documentStore.Initialize();
            }

            Logger.Info("Index creation started");

            IndexCreation.CreateIndexes(typeof(RavenBootstrapper).Assembly, documentStore);

            PurgeKnownEndpointsWithTemporaryIdsThatAreDuplicate(documentStore);
        }
 public void When_file_is_deleted_underneath_immediately_before_write()
 {
     using (var tempPath = new TempPath())
     {
         var logger = new RollingLoggerThatDeletesBeforeWrite(tempPath.TempDirectory)
         {
             GetDate = () => new DateTime(2010, 10, 1)
         };
         logger.WriteLine("Foo");
         var singleFile = tempPath.GetSingle();
         File.Delete(singleFile);
         logger.WriteLine("Bar");
         Assert.AreEqual($"Bar{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(singleFile));
     }
 }
Пример #10
0
 public void When_file_is_deleted_underneath_continues_to_write_afterwards()
 {
     using (var tempPath = new TempPath())
     {
         var logger = new RollingLogger(tempPath.TempDirectory)
         {
             GetDate = () => new DateTime(2010, 10, 1)
         };
         logger.Write("Foo");
         var single = tempPath.GetSingle();
         File.Delete(single);
         logger.Write("Bar");
         Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(single));
     }
 }
Пример #11
0
        public void StartRaven(EmbeddableDocumentStore documentStore, Settings settings, bool maintenanceMode)
        {
            Directory.CreateDirectory(settings.DbPath);

            documentStore.DataDirectory                   = settings.DbPath;
            documentStore.UseEmbeddedHttpServer           = maintenanceMode || settings.ExposeRavenDB;
            documentStore.EnlistInDistributedTransactions = false;

            var localRavenLicense = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RavenLicense.xml");

            if (File.Exists(localRavenLicense))
            {
                Logger.InfoFormat("Loading RavenDB license found from {0}", localRavenLicense);
                documentStore.Configuration.Settings["Raven/License"] = NonLockingFileReader.ReadAllTextWithoutLocking(localRavenLicense);
            }
            else
            {
                Logger.InfoFormat("Loading Embedded RavenDB license");
                documentStore.Configuration.Settings["Raven/License"] = ReadLicense();
            }

            if (!maintenanceMode)
            {
                documentStore.Configuration.Settings.Add("Raven/ActiveBundles", "CustomDocumentExpiration");
            }

            documentStore.Configuration.DisableClusterDiscovery     = true;
            documentStore.Configuration.ResetIndexOnUncleanShutdown = true;
            documentStore.Configuration.DisablePerformanceCounters  = settings.DisableRavenDBPerformanceCounters;
            documentStore.Configuration.Port     = settings.Port;
            documentStore.Configuration.HostName = (settings.Hostname == "*" || settings.Hostname == "+")
                ? "localhost"
                : settings.Hostname;
            documentStore.Configuration.VirtualDirectory            = $"{settings.VirtualDirectory}/storage";
            documentStore.Configuration.CompiledIndexCacheDirectory = settings.DbPath;
            documentStore.Conventions.SaveEnumsAsIntegers           = true;

            documentStore.Configuration.Catalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));

            documentStore.Initialize();

            Logger.Info("Index creation started");

            IndexCreation.CreateIndexes(typeof(RavenBootstrapper).Assembly, documentStore);

            PurgeKnownEndpointsWithTemporaryIdsThatAreDuplicate(documentStore);
        }
Пример #12
0
        public static bool TryImportLicense(string licenseFile, out string errorMessage)
        {
            Exception validationFailure;
            License   license;
            var       licenseText = NonLockingFileReader.ReadAllTextWithoutLocking(licenseFile);

            if (!LicenseVerifier.TryVerify(licenseText, out validationFailure))
            {
                errorMessage = "Invalid license file";
                return(false);
            }

            if (!TryDeserializeLicense(licenseText, out license))
            {
                errorMessage = "Invalid license file";
                return(false);
            }

            if (!license.ValidForApplication("ServiceControl"))
            {
                errorMessage = "License is not for ServiceControl";
                return(false);
            }

            try
            {
                new RegistryLicenseStore(Registry.LocalMachine).StoreLicense(licenseText);
            }
            catch (Exception)
            {
                errorMessage = "Failed to import license into the registry";
                return(false);
            }

            try
            {
                new FilePathLicenseStore().StoreLicense(FilePathLicenseStore.MachineLevelLicenseLocation, licenseText);
            }
            catch (Exception)
            {
                errorMessage = "Failed to import license into the filesystem";
                return(false);
            }

            errorMessage = null;
            return(true);
        }
 public void When_file_is_locked_exception_is_swallowed()
 {
     using (var tempPath = new TempPath())
     {
         var logger = new RollingLogger(tempPath.TempDirectory)
         {
             GetDate = () => new DateTime(2010, 10, 1)
         };
         logger.WriteLine("Foo");
         var single = tempPath.GetSingle();
         using (LockFile(single))
         {
             logger.WriteLine("Bar");
         }
         Assert.AreEqual($"Foo{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(single));
     }
 }
Пример #14
0
        public static bool TryImportLicense(string licenseFile, out string errorMessage)
        {
            Exception validationFailure;
            License   license;
            var       licenseText = NonLockingFileReader.ReadAllTextWithoutLocking(licenseFile);

            if (!LicenseVerifier.TryVerify(licenseText, out validationFailure))
            {
                errorMessage = "Invalid license file";
                return(false);
            }

            if (!TryDeserializeLicense(licenseText, out license))
            {
                errorMessage = "Invalid license file";
                return(false);
            }

            if (!license.ValidForApplication("ServiceControl"))
            {
                errorMessage = "License is not for ServiceControl";
                return(false);
            }

            try
            {
                using (var localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Default))
                {
                    new RegistryLicenseStore(localMachine).StoreLicense(licenseText);
                }
            }
            catch (Exception)
            {
                errorMessage = "Failed to import license into the registry";
                return(false);
            }

            errorMessage = null;
            return(true);
        }
        public void When_max_file_size_is_exceeded_sequence_number_is_added()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory, maxFileSize: 10)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.WriteLine("Some long text");
                logger.WriteLine("Bar");
                var files = tempPath.GetFiles();
                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual($"Some long text{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-01_1.txt", Path.GetFileName(second));
                Assert.AreEqual($"Bar{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
Пример #16
0
        public void When_date_changes_new_file_is_written()
        {
            using (var tempPath = new TempPath())
            {
                var logger = new RollingLogger(tempPath.TempDirectory)
                {
                    GetDate = () => new DateTime(2010, 10, 1)
                };
                logger.Write("Foo");
                logger.GetDate = () => new DateTime(2010, 10, 2);
                logger.Write("Bar");
                var files = tempPath.GetFiles();
                Assert.AreEqual(2, files.Count);

                var first = files[0];
                Assert.AreEqual("nsb_log_2010-10-01_0.txt", Path.GetFileName(first));
                Assert.AreEqual("Foo\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(first));

                var second = files[1];
                Assert.AreEqual("nsb_log_2010-10-02_0.txt", Path.GetFileName(second));
                Assert.AreEqual("Bar\r\n", NonLockingFileReader.ReadAllTextWithoutLocking(second));
            }
        }
 public void When_file_already_exists_that_file_is_written_to()
 {
     using (var tempPath = new TempPath())
     {
         var dateTime = new DateTime(2010, 10, 1);
         var logger1  = new RollingLogger(tempPath.TempDirectory)
         {
             GetDate = () => dateTime
         };
         logger1.WriteLine("Foo");
         var files1 = tempPath.GetFiles();
         Assert.AreEqual(1, files1.Count);
         Assert.AreEqual($"Foo{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(files1.First()));
         var logger2 = new RollingLogger(tempPath.TempDirectory)
         {
             GetDate = () => dateTime
         };
         logger2.WriteLine("Bar");
         var files2 = tempPath.GetFiles();
         Assert.AreEqual(1, files2.Count);
         Assert.AreEqual($"Foo{Environment.NewLine}Bar{Environment.NewLine}", NonLockingFileReader.ReadAllTextWithoutLocking(files2.First()));
     }
 }
Пример #18
0
        public void Init()
        {
            Directory.CreateDirectory(Settings.DbPath);

            var documentStore = new EmbeddableDocumentStore
            {
                DataDirectory                   = Settings.DbPath,
                UseEmbeddedHttpServer           = Settings.MaintenanceMode || Settings.ExposeRavenDB,
                EnlistInDistributedTransactions = false,
            };

            var localRavenLicense = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RavenLicense.xml");

            if (File.Exists(localRavenLicense))
            {
                Logger.InfoFormat("Loading RavenDB license found from {0}", localRavenLicense);
                documentStore.Configuration.Settings["Raven/License"] = NonLockingFileReader.ReadAllTextWithoutLocking(localRavenLicense);
            }
            else
            {
                Logger.InfoFormat("Loading Embedded RavenDB license");
                documentStore.Configuration.Settings["Raven/License"] = ReadLicense();
            }

            documentStore.Configuration.Catalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));

            if (!Settings.MaintenanceMode)
            {
                documentStore.Configuration.Settings.Add("Raven/ActiveBundles", "CustomDocumentExpiration");
            }

            documentStore.Configuration.Port     = Settings.Port;
            documentStore.Configuration.HostName = (Settings.Hostname == "*" || Settings.Hostname == "+")
                ? "localhost"
                : Settings.Hostname;
            documentStore.Configuration.CompiledIndexCacheDirectory = Settings.DbPath;
            documentStore.Configuration.VirtualDirectory            = Settings.VirtualDirectory + "/storage";
            documentStore.Conventions.SaveEnumsAsIntegers           = true;
            documentStore.Initialize();

            Logger.Info("Index creation started");

            //Create this index synchronously as we are using it straight away
            //Should be quick as number of endpoints will always be a small number
            documentStore.ExecuteIndex(new KnownEndpointIndex());

            if (Settings.CreateIndexSync)
            {
                IndexCreation.CreateIndexes(typeof(RavenBootstrapper).Assembly, documentStore);
            }
            else
            {
                IndexCreation.CreateIndexesAsync(typeof(RavenBootstrapper).Assembly, documentStore)
                .ContinueWith(c =>
                {
                    if (c.IsFaulted)
                    {
                        Logger.Error("Index creation failed", c.Exception);
                    }
                });
            }

            PurgeKnownEndpointsWithTemporaryIdsThatAreDuplicate(documentStore);

            Configure.Instance.Configurer.RegisterSingleton <IDocumentStore>(documentStore);
            Configure.Component(builder =>
            {
#pragma warning disable 618
                var context = builder.Build <PipelineExecutor>().CurrentContext;
#pragma warning restore 618

                IDocumentSession session;

                if (context.TryGet(out session))
                {
                    return(session);
                }

                throw new InvalidOperationException("No session available");
            }, DependencyLifecycle.InstancePerCall);

            Configure.Instance.RavenDBStorageWithSelfManagedSession(documentStore, false,
                                                                    () => Configure.Instance.Builder.Build <IDocumentSession>())
            .UseRavenDBSagaStorage()
            .UseRavenDBSubscriptionStorage()
            .UseRavenDBTimeoutStorage();
        }