Example #1
0
        public EmbeddableDocumentStore()
        {
            Conventions   = new DocumentConvention();
            Listeners     = new DocumentSessionListeners();
            Configuration = new AppSettingsBasedConfiguration();
            LegacyDataDirSupport(Configuration);

            EnlistInDistributedTransactions = true;
        }
Example #2
0
        public StorageExporter(string databaseBaseDirectory, string databaseOutputFile, int batchSize)
        {
            baseDirectory   = databaseBaseDirectory;
            outputDirectory = databaseOutputFile;
            var ravenConfiguration = new AppSettingsBasedConfiguration();

            ravenConfiguration.Core.DataDirectory          = databaseBaseDirectory;
            ravenConfiguration.Storage.PreventSchemaUpdate = true;
            CreateTransactionalStorage(ravenConfiguration);
            BatchSize = batchSize;
        }
 public void AppSettingsBasedConfigurationWithOverrideAndMsSqlEsProvider()
 {
     var configuration = new AppSettingsBasedConfiguration(ProjectId, "appsettings_mssql-es-ns.json", true).Init();
     Assert.Contains("Data Source=.;Integrated Security=true;MultipleActiveResultSets=true", configuration.DbConnectionString, System.StringComparison.InvariantCulture);
     Assert.True(configuration.IndexConnectionString == "http://localhost:9200");
     Assert.True(configuration.BinaryConnectionString == "\\\\localhost\\C$");
     Assert.True(configuration.MetadataProviderAssemblyQualifiedName == "POC.Storage.MsSql.MsSqlMetadataProvider, POC.Storage.MsSql");
     Assert.True(configuration.SearchProviderAssemblyQualifiedName == "POC.Storage.Elasticsearch.ElasticsearchSearchProvider, POC.Storage.Elasticsearch");
     Assert.True(configuration.AuditReportProviderAssemblyQualifiedName == "POC.Storage.MsSql.MsSqlAuditReportProvider, POC.Storage.MsSql");
     Assert.True(configuration.MetadataProvider.GetType() == typeof(MsSqlMetadataProvider));
     Assert.True(configuration.SearchProvider.GetType() == typeof(ElasticsearchSearchProvider));
     Assert.True(configuration.AuditReportProvider.GetType() == typeof(MsSqlAuditReportProvider));
     Assert.True(configuration.BinaryProvider.GetType() == typeof(NetworkShareBinaryProvider));
 }
 public void AppSettingsBasedConfigurationWithDefaultAndNullProvider()
 {
     var configuration = new AppSettingsBasedConfiguration(ProjectId, true).Init();
     Assert.True(configuration.DbConnectionString == "TEST_DB_CONNECTIONSTRING");
     Assert.True(configuration.IndexConnectionString == "TEST_INDEX_CONNECTIONSTRING");
     Assert.True(configuration.BinaryConnectionString == "TEST_BINARY_CONNECTIONSTRING");
     Assert.True(configuration.MetadataProviderAssemblyQualifiedName == "POC.Storage.Null.NullMetadataProvider, POC.Storage.Null");
     Assert.True(configuration.SearchProviderAssemblyQualifiedName == "POC.Storage.Null.NullSearchProvider, POC.Storage.Null");
     Assert.True(configuration.AuditReportProviderAssemblyQualifiedName == "POC.Storage.Null.NullAuditReportProvider, POC.Storage.Null");
     Assert.True(configuration.MetadataProvider.GetType() == typeof(NullMetadataProvider));
     Assert.True(configuration.SearchProvider.GetType() == typeof(NullSearchProvider));
     Assert.True(configuration.AuditReportProvider.GetType() == typeof(NullAuditReportProvider));
     Assert.True(configuration.BinaryProvider.GetType() == typeof(NullBinaryProvider));
 }
 public void AppSettingsBasedConfigurationWithProvidedConfigAndNullProvider()
 {
     var c = new ConfigurationBuilder().AddJsonFile("appSettings.json", optional: false, reloadOnChange: true).Build();
     // TODO: make a performance test with 1000000 iterations
     //for (int i = 0; i < 1000000; i++)
     //{
     var configuration = new AppSettingsBasedConfiguration(ProjectId, c).Init();
     Assert.True(configuration.DbConnectionString == "TEST_DB_CONNECTIONSTRING");
     Assert.True(configuration.IndexConnectionString == "TEST_INDEX_CONNECTIONSTRING");
     Assert.True(configuration.BinaryConnectionString == "TEST_BINARY_CONNECTIONSTRING");
     Assert.True(configuration.MetadataProviderAssemblyQualifiedName == "POC.Storage.Null.NullMetadataProvider, POC.Storage.Null");
     Assert.True(configuration.SearchProviderAssemblyQualifiedName == "POC.Storage.Null.NullSearchProvider, POC.Storage.Null");
     Assert.True(configuration.AuditReportProviderAssemblyQualifiedName == "POC.Storage.Null.NullAuditReportProvider, POC.Storage.Null");
     Assert.True(configuration.MetadataProvider.GetType() == typeof(NullMetadataProvider));
     Assert.True(configuration.SearchProvider.GetType() == typeof(NullSearchProvider));
     Assert.True(configuration.AuditReportProvider.GetType() == typeof(NullAuditReportProvider));
     Assert.True(configuration.BinaryProvider.GetType() == typeof(NullBinaryProvider));
     //}
 }
Example #6
0
        public static void Restore(AppSettingsBasedConfiguration configuration, DatabaseRestoreRequest restoreRequest, Action <string> output)
        {
            var databaseDocumentPath = FindDatabaseDocument(restoreRequest.BackupLocation);

            if (File.Exists(databaseDocumentPath) == false)
            {
                throw new InvalidOperationException("Cannot restore when the Database.Document file is missing in the backup folder: " + restoreRequest.BackupLocation);
            }

            if (Directory.Exists(Path.Combine(restoreRequest.BackupLocation, "new")))
            {
                throw new StorageNotSupportedException("Esent is no longer supported. Use Voron instead.");
            }

            if (!string.IsNullOrWhiteSpace(restoreRequest.DatabaseLocation))
            {
                configuration.Core.DataDirectory = restoreRequest.DatabaseLocation;
            }

            using (var transactionalStorage = new TransactionalStorage(configuration, () => { }, () => { }, () => { }, () => { }))
            {
                transactionalStorage.Restore(restoreRequest, output);
            }
        }
Example #7
0
        public async Task <HttpResponseMessage> Restore()
        {
            var restoreStatus = new RestoreStatus {
                State = RestoreStatusState.Running, Messages = new List <string>()
            };

            var restoreRequest = await ReadJsonObjectAsync <FilesystemRestoreRequest>().ConfigureAwait(false);

            var fileSystemDocumentPath = FindFilesystemDocument(restoreRequest.BackupLocation);

            if (!File.Exists(fileSystemDocumentPath))
            {
                throw new InvalidOperationException("Cannot restore when the Filesystem.Document file is missing in the backup folder: " + restoreRequest.BackupLocation);
            }

            var filesystemDocumentText = File.ReadAllText(fileSystemDocumentPath);
            var filesystemDocument     = RavenJObject.Parse(filesystemDocumentText).JsonDeserialization <FileSystemDocument>();

            var filesystemName = !string.IsNullOrWhiteSpace(restoreRequest.FilesystemName) ? restoreRequest.FilesystemName
                                   : filesystemDocument == null ? null : filesystemDocument.Id;

            if (string.IsNullOrWhiteSpace(filesystemName))
            {
                var errorMessage = (filesystemDocument == null || String.IsNullOrWhiteSpace(filesystemDocument.Id))
                                ? Constants.FilesystemDocumentFilename + " file is invalid - filesystem name was not found and not supplied in the request (Id property is missing or null). This is probably a bug - should never happen."
                                : "A filesystem name must be supplied if the restore location does not contain a valid " + Constants.FilesystemDocumentFilename + " file";

                restoreStatus.Messages.Add(errorMessage);
                SystemDatabase.Documents.Put(RestoreStatus.RavenFilesystemRestoreStatusDocumentKey(filesystemName), null, RavenJObject.FromObject(new { restoreStatus }), new RavenJObject(), null);

                return(GetMessageWithString(errorMessage, HttpStatusCode.BadRequest));
            }

            var ravenConfiguration = new AppSettingsBasedConfiguration(initialize: false)
            {
                FileSystemName = filesystemName,
            };

            if (filesystemDocument != null)
            {
                foreach (var setting in filesystemDocument.Settings)
                {
                    ravenConfiguration.SetSetting(setting.Key, setting.Value);
                }
            }

            ravenConfiguration.CustomizeValuesForFileSystemTenant(filesystemName);
            ravenConfiguration.Initialize();

            string documentDataDir;

            ravenConfiguration.FileSystem.DataDirectory = ResolveTenantDataDirectory(restoreRequest.FilesystemLocation, filesystemName, out documentDataDir);
            restoreRequest.FilesystemLocation           = ravenConfiguration.FileSystem.DataDirectory;


            string anotherRestoreResourceName;

            if (IsAnotherRestoreInProgress(out anotherRestoreResourceName))
            {
                if (restoreRequest.RestoreStartTimeout.HasValue)
                {
                    try
                    {
                        using (var cts = new CancellationTokenSource())
                        {
                            cts.CancelAfter(TimeSpan.FromSeconds(restoreRequest.RestoreStartTimeout.Value));
                            var token = cts.Token;
                            do
                            {
                                await Task.Delay(500, token).ConfigureAwait(false);
                            }while (IsAnotherRestoreInProgress(out anotherRestoreResourceName));
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        return(GetMessageWithString(string.Format("Another restore is still in progress (resource name = {0}). Waited {1} seconds for other restore to complete.", anotherRestoreResourceName, restoreRequest.RestoreStartTimeout.Value), HttpStatusCode.ServiceUnavailable));
                    }
                }
                else
                {
                    return(GetMessageWithString(string.Format("Another restore is in progress (resource name = {0})", anotherRestoreResourceName), HttpStatusCode.ServiceUnavailable));
                }
            }
            SystemDatabase.Documents.Put(RestoreInProgress.RavenRestoreInProgressDocumentKey, null, RavenJObject.FromObject(new RestoreInProgress
            {
                Resource = filesystemName
            }), new RavenJObject(), null);

            DatabasesLandlord.SystemDatabase.Documents.Delete(RestoreStatus.RavenFilesystemRestoreStatusDocumentKey(filesystemName), null, null);

            bool defrag;

            if (bool.TryParse(GetQueryStringValue("defrag"), out defrag))
            {
                restoreRequest.Defrag = defrag;
            }

            var task = Task.Factory.StartNew(() =>
            {
                try
                {
                    if (!string.IsNullOrWhiteSpace(restoreRequest.FilesystemLocation))
                    {
                        ravenConfiguration.FileSystem.DataDirectory = restoreRequest.FilesystemLocation;
                    }

                    using (var transactionalStorage = RavenFileSystem.CreateTransactionalStorage(ravenConfiguration))
                    {
                        transactionalStorage.Restore(restoreRequest, msg =>
                        {
                            restoreStatus.Messages.Add(msg);
                            DatabasesLandlord.SystemDatabase.Documents.Put(RestoreStatus.RavenFilesystemRestoreStatusDocumentKey(filesystemName), null,
                                                                           RavenJObject.FromObject(restoreStatus), new RavenJObject(), null);
                        });
                    }

                    if (filesystemDocument == null)
                    {
                        return;
                    }

                    filesystemDocument.Settings[RavenConfiguration.GetKey(x => x.FileSystem.DataDirectory)] = documentDataDir;

                    if (restoreRequest.IndexesLocation != null)
                    {
                        filesystemDocument.Settings[RavenConfiguration.GetKey(x => x.Core.IndexStoragePath)] = restoreRequest.IndexesLocation;
                    }
                    if (restoreRequest.JournalsLocation != null)
                    {
                        filesystemDocument.Settings[RavenConfiguration.GetKey(x => x.Storage.JournalsStoragePath)] = restoreRequest.JournalsLocation;
                    }
                    filesystemDocument.Id = filesystemName;

                    FileSystemsLandlord.Protect(filesystemDocument);

                    DatabasesLandlord.SystemDatabase.Documents.Put(Constants.FileSystem.Prefix + filesystemName, null, RavenJObject.FromObject(filesystemDocument), new RavenJObject(), null);

                    restoreStatus.State = RestoreStatusState.Completed;
                    restoreStatus.Messages.Add("The new filesystem was created");
                    DatabasesLandlord.SystemDatabase.Documents.Put(RestoreStatus.RavenFilesystemRestoreStatusDocumentKey(filesystemName), null, RavenJObject.FromObject(restoreStatus), new RavenJObject(), null);
                }
                catch (Exception e)
                {
                    restoreStatus.State = RestoreStatusState.Faulted;
                    restoreStatus.Messages.Add("Unable to restore filesystem " + e.Message);
                    DatabasesLandlord.SystemDatabase.Documents.Put(RestoreStatus.RavenFilesystemRestoreStatusDocumentKey(filesystemName), null, RavenJObject.FromObject(restoreStatus), new RavenJObject(), null);
                    throw;
                }
                finally
                {
                    SystemDatabase.Documents.Delete(RestoreInProgress.RavenRestoreInProgressDocumentKey, null, null);
                }
            }, TaskCreationOptions.LongRunning);

            long id;

            SystemDatabase.Tasks.AddTask(task, new TaskBasedOperationState(task), new TaskActions.PendingTaskDescription
            {
                StartTime = SystemTime.UtcNow,
                TaskType  = TaskActions.PendingTaskType.RestoreFilesystem,
                Payload   = "Restoring filesystem " + filesystemName + " from " + restoreRequest.BackupLocation
            }, out id);

            return(GetMessageWithObject(new
            {
                OperationId = id
            }, HttpStatusCode.Accepted));
        }