Beispiel #1
0
        public async Task Should_be_able_to_delete_time_series()
        {
            var expectedClientNames = new[] { TimeSeriesName + "A", TimeSeriesName + "C" };

            using (var store = NewRemoteTimeSeriesStore(createDefaultTimeSeries: false))
            {
                await store.Admin.CreateTimeSeriesAsync(MultiDatabase.CreateTimeSeriesDocument(expectedClientNames[0]));

                await store.Admin.CreateTimeSeriesAsync(MultiDatabase.CreateTimeSeriesDocument("TimeSeriesThatWillBeDeleted"));

                await store.Admin.CreateTimeSeriesAsync(MultiDatabase.CreateTimeSeriesDocument(expectedClientNames[1]));

                await store.Admin.DeleteTimeSeriesAsync("TimeSeriesThatWillBeDeleted", true);

                var timeSeriesNames = await store.Admin.GetTimeSeriesNamesAsync();

                Assert.Equal(expectedClientNames.Length, timeSeriesNames.Count());
                foreach (var name in expectedClientNames)
                {
                    Assert.Contains(name, timeSeriesNames);
                }
            }
        }
Beispiel #2
0
        protected virtual IDatabaseChanges CreateDatabaseChanges(string database)
        {
            if (string.IsNullOrEmpty(Url))
            {
                throw new InvalidOperationException("Changes API requires usage of server/client");
            }

            database = database ?? DefaultDatabase;

            var dbUrl = MultiDatabase.GetRootDatabaseUrl(Url);

            if (string.IsNullOrEmpty(database) == false)
            {
                dbUrl = dbUrl + "/databases/" + database;
            }

            return(new RemoteDatabaseChanges(dbUrl,
                                             Credentials,
                                             jsonRequestFactory,
                                             Conventions,
                                             GetReplicationInformerForDatabase(database),
                                             () => databaseChanges.Remove(database)));
        }
        private void UpdateReplicationInformationFromDocument(JsonDocument document)
        {
            var replicationDocument = document.DataAsJson.JsonDeserialization <ReplicationDocument>();

            replicationDestinations = replicationDocument.Destinations.Select(x =>
            {
                var url = string.IsNullOrEmpty(x.ClientVisibleUrl) ? x.Url : x.ClientVisibleUrl;
                if (string.IsNullOrEmpty(url) || x.Disabled || x.IgnoredClient)
                {
                    return(null);
                }
                if (string.IsNullOrEmpty(x.Database))
                {
                    return(new OperationMetadata(url, x.Username, x.Password, x.Domain, x.ApiKey));
                }

                return(new OperationMetadata(
                           MultiDatabase.GetRootDatabaseUrl(url) + "/databases/" + x.Database + "/",
                           x.Username,
                           x.Password,
                           x.Domain,
                           x.ApiKey));
            })
                                      // filter out replication destination that don't have the url setup, we don't know how to reach them
                                      // so we might as well ignore them. Probably private replication destination (using connection string names only)
                                      .Where(x => x != null)
                                      .ToList();
            foreach (var replicationDestination in replicationDestinations)
            {
                FailureCounter value;
                if (failureCounts.TryGetValue(replicationDestination.Url, out value))
                {
                    continue;
                }
                failureCounts[replicationDestination.Url] = new FailureCounter();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Initialize the document store access method to RavenDB
        /// </summary>
        protected virtual void InitializeInternal()
        {
            var rootDatabaseUrl = MultiDatabase.GetRootDatabaseUrl(Url);

#if !DNXCORE50
            // TODO [ppekrol] how to set this?
            rootServicePoint = ServicePointManager.FindServicePoint(new Uri(rootDatabaseUrl));
            rootServicePoint.UseNagleAlgorithm = false;
            rootServicePoint.Expect100Continue = false;
            rootServicePoint.ConnectionLimit   = 256;
            rootServicePoint.MaxIdleTime       = Timeout.Infinite;
#endif

            databaseCommandsGenerator = () =>
            {
                string databaseUrl = Url;
                if (string.IsNullOrEmpty(DefaultDatabase) == false)
                {
                    databaseUrl = rootDatabaseUrl;
                    databaseUrl = databaseUrl + "/databases/" + DefaultDatabase;
                }
                return(new ServerClient(new AsyncServerClient(databaseUrl, Conventions, new OperationCredentials(ApiKey, Credentials), jsonRequestFactory,
                                                              currentSessionId, GetReplicationInformerForDatabase, null,
                                                              Listeners.ConflictListeners, true)));
            };

            asyncDatabaseCommandsGenerator = () =>
            {
                var asyncServerClient = new AsyncServerClient(Url, Conventions, new OperationCredentials(ApiKey, Credentials), jsonRequestFactory, currentSessionId, GetReplicationInformerForDatabase, null, Listeners.ConflictListeners, true);

                if (string.IsNullOrEmpty(DefaultDatabase))
                {
                    return(asyncServerClient);
                }
                return(asyncServerClient.ForDatabase(DefaultDatabase));
            };
        }
Beispiel #5
0
 public void CreateDb(string dbname = null)
 {
     try
     {
         if (dbname == null)
         {
             dbname = "test";
         }
         var doc = MultiDatabase.CreateDatabaseDocument(dbname);
         store.AsyncDatabaseCommands.GlobalAdmin.CreateDatabaseAsync(doc).Wait();
     }
     catch (Exception ex)
     {
         if (ex.Message.Contains("already exists"))
         {
             Log($"Database '{dbname}' already exists!");
         }
         else
         {
             Log("Cannot create DB " + dbname + ". Exception : " + ex.Message, true);
             throw;
         }
     }
 }
Beispiel #6
0
        public ReplicationInformer GetReplicationInformerForDatabase(string dbName = null)
        {
            var key = Url;

            dbName = dbName ?? DefaultDatabase;
            if (string.IsNullOrEmpty(dbName) == false)
            {
                key = MultiDatabase.GetRootDatabaseUrl(Url) + "/databases/" + dbName;
            }
#if SILVERLIGHT || NETFX_CORE
            lock (replicationInformersLocker)
            {
                ReplicationInformer result;
                if (!replicationInformers.TryGetValue(key, out result))
                {
                    result = Conventions.ReplicationInformerFactory(key);
                    replicationInformers.Add(key, result);
                }
                return(result);
            }
#else
            return(replicationInformers.GetOrAdd(key, Conventions.ReplicationInformerFactory));
#endif
        }
Beispiel #7
0
        static SlappCommand()
        {
            // Initialise Slapp
            try
            {
                // Construct the database
                jsonDatabase        = new SplatTagJsonDatabase(saveFolder);
                multiSourceImporter = new GenericFilesImporter(saveFolder);
                splatTagDatabase    = new MultiDatabase(saveFolder, jsonDatabase, multiSourceImporter);

                // Construct the controller.
                splatTagController = new SplatTagController(splatTagDatabase);

                initialiseTask = Task.Run(() =>
                {
                    // Load the database
                    splatTagController.Initialise();
                });
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Exception: " + ex.ToString());
            }
        }
Beispiel #8
0
        public async Task AdminClientWorkWithOAuthEnabled()
        {
            var client      = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, apiKey: apiKey);
            var adminClient = client.Admin;

            await adminClient.CreateFileSystemAsync(MultiDatabase.CreateFileSystemDocument("testName"));

            var names = await adminClient.GetNamesAsync();

            Assert.Equal(2, names.Length);
            Assert.Contains("AdminClientWorkWithOAuthEnabled", names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.Equal(0, stats.Length); // 0 because our fs aren't active

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName"))
            {
                var buffer = Enumerable.Range(1, 1).Select(x => (byte)(x % byte.MaxValue)).ToArray();
                await createdFsClient.UploadAsync("fooFoo", new MemoryStream(buffer));
            }

            await adminClient.DeleteFileSystemAsync("testName", true);
        }
Beispiel #9
0
        public async Task EnsureDatabaseExistsAsync(string name, bool ignoreFailures = false, CancellationToken token = default(CancellationToken))
        {
            var serverClient = (AsyncServerClient)(innerAsyncServerClient.ForSystemDatabase());

            var doc = MultiDatabase.CreateDatabaseDocument(name);

            serverClient.ForceReadFromMaster();

            try
            {
                var get = await serverClient.GetAsync(doc.Id, token).ConfigureAwait(false);

                if (get != null)
                {
                    return;
                }

                await serverClient.GlobalAdmin.CreateDatabaseAsync(doc, token).ConfigureAwait(false);

                try
                {
                    await new RavenDocumentsByEntityName().ExecuteAsync(serverClient.ForDatabase(name), new DocumentConvention(), token).ConfigureAwait(false);
                }
                catch (Exception)
                {
                    // this is a courtesy, not required, and can happen if we don't have permissions to the new db
                }
            }
            catch (Exception)
            {
                if (ignoreFailures == false)
                {
                    throw;
                }
            }
        }
Beispiel #10
0
        public void Store(object entity)
        {
            var shardId = shardResolutionStrategy.GenerateShardIdFor(entity, this);
            var shard   = shards[shardId];
            BulkInsertOperation bulkInsertOperation;

            if (Bulks.TryGetValue(shardId, out bulkInsertOperation) == false)
            {
                var actualDatabaseName = database ?? ((dynamic)shard).DefaultDatabase ?? MultiDatabase.GetDatabaseName(shard.Url);
                bulkInsertOperation = new BulkInsertOperation(actualDatabaseName, shard, shard.Listeners, options, shard.Changes());
                Bulks.Add(shardId, bulkInsertOperation);
            }

            DatabaseCommands = string.IsNullOrWhiteSpace(database)
                ? shard.AsyncDatabaseCommands
                : shard.AsyncDatabaseCommands.ForDatabase(database);

            string id;

            if (generateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) == false)
            {
                id = generateEntityIdOnTheClient.GetOrGenerateDocumentKey(entity);
            }
            var modifyDocumentId = shardStrategy.ModifyDocumentId(shardedDocumentStore.Conventions, shardId, id);

            bulkInsertOperation.Store(entity, modifyDocumentId);
        }
Beispiel #11
0
        public async Task CanUseBetween()
        {
            using (var server1 = new RavenDbServer(new RavenConfiguration()
            {
                Port = Port1,
                ServerName = ServerName1
            })
            {
                RunInMemory = true,
                UseEmbeddedHttpServer = true
            }.Initialize())
            {
                var doc = MultiDatabase.CreateDatabaseDocument("db1");
                ((ServerClient)server1.DocumentStore.DatabaseCommands.ForSystemDatabase()).GlobalAdmin.CreateDatabase(doc);

                using (var store1 = new DocumentStore
                {
                    Url = "http://localhost:" + Port1,
                    DefaultDatabase = "db1"
                }.Initialize())
                {
                    new Users_ByName().Execute(store1);
                    new UsersTransformer().Execute(store1);

                    using (var session = store1.OpenSession("db1"))
                    {
                        session.Store(new User {
                            Name = "Name1", LastName = "LastName1"
                        });
                        session.Store(new User {
                            Name = "Name2", LastName = "LastName2"
                        });
                        session.SaveChanges();
                    }

                    store1.DatabaseCommands.PutAttachment("attachement1", null, new MemoryStream(new byte[] { 3 }), new RavenJObject());

                    using (var server2 = new RavenDbServer(new RavenConfiguration()
                    {
                        Port = Port2,
                        ServerName = ServerName2
                    })
                    {
                        RunInMemory = true,
                        UseEmbeddedHttpServer = true
                    }.Initialize())
                    {
                        var doc2 = MultiDatabase.CreateDatabaseDocument("db2");
                        ((ServerClient)server2.DocumentStore.DatabaseCommands.ForSystemDatabase()).GlobalAdmin.CreateDatabase(doc2);

                        using (var store2 = new DocumentStore
                        {
                            Url = "http://localhost:" + Port2,
                            DefaultDatabase = "db2"
                        }.Initialize())
                        {
                            var smugglerApi = new SmugglerDatabaseApi();
                            await smugglerApi.Between(new SmugglerBetweenOptions <RavenConnectionStringOptions>
                            {
                                From = new RavenConnectionStringOptions {
                                    Url = "http://localhost:" + Port1, DefaultDatabase = "db1"
                                },
                                To = new RavenConnectionStringOptions {
                                    Url = "http://localhost:" + Port2, DefaultDatabase = "db2"
                                }
                            });

                            var docs = store2.DatabaseCommands.GetDocuments(0, 10);
                            Assert.Equal(3, docs.Length);
                            var indexes = store2.DatabaseCommands.GetIndexes(0, 10);
                            Assert.Equal(1, indexes.Length);
                            var transformers = store2.DatabaseCommands.GetTransformers(0, 10);
                            Assert.Equal(1, transformers.Length);
                            var attachments = store2.DatabaseCommands.GetAttachments(0, new Etag(), 10);
                            Assert.Equal(1, attachments.Length);
                        }
                    }
                }
            }
        }
Beispiel #12
0
        public static string Databases(this string url, int pageSize, int start)
        {
            var databases = MultiDatabase.GetRootDatabaseUrl(url) + "/databases?pageSize=" + pageSize;

            return(start > 0 ? databases + "&start=" + start : databases);
        }
Beispiel #13
0
        public async Task TestMassiveParallelDatabaseLoads(string storageEngine)
        {
            using (var server = GetNewServer())
            {
                var defaultStore = server.DocumentStore;
                var userRJO      = RavenJObject.FromObject(new User
                {
                    Name = "Abe"
                });
                for (var i = 0; i < 120; i++)
                {
                    var dbName           = "Database" + i;
                    var databaseDocument = MultiDatabase.CreateDatabaseDocument(dbName);
                    databaseDocument.Settings["Raven/StorageTypeName"] = storageEngine;

                    databaseDocument.Settings.Add("Raven/Tenants/MaxIdleTimeForTenantDatabase", "20");
                    await defaultStore.AsyncDatabaseCommands.GlobalAdmin.CreateDatabaseAsync(
                        databaseDocument).ConfigureAwait(false);

                    using (var curStore = new DocumentStore()
                    {
                        Url = GetServerUrl(false, server.SystemDatabase.ServerUrl),
                        Conventions = new DocumentConvention()
                        {
                            FailoverBehavior = FailoverBehavior.FailImmediately
                        },
                        DefaultDatabase = dbName
                    })
                    {
                        curStore.Initialize();

                        using (var bi = curStore.BulkInsert())
                        {
                            for (var j = 0; j < 5; j++)
                            {
                                bi.Store(new User()
                                {
                                    Name = "UserNumber" + j
                                });
                            }
                        }
                    }

                    await defaultStore.JsonRequestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(null,
                                                                                                                defaultStore.Url + "/admin/databases-toggle-disable?id=" + dbName + "&isSettingDisabled=true",
                                                                                                                HttpMethod.Post,
                                                                                                                null,
                                                                                                                defaultStore.Conventions,
                                                                                                                null)).ExecuteRequestAsync().ConfigureAwait(false);
                }

                Parallel.For(0, 120, new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 20
                }, i =>
                {
                    var dbName = "Database" + i;
                    defaultStore.JsonRequestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(null,
                                                                                                          defaultStore.Url + "/admin/databases-toggle-disable?id=" + dbName + "&isSettingDisabled=false",
                                                                                                          HttpMethod.Post,
                                                                                                          null,
                                                                                                          defaultStore.Conventions,
                                                                                                          null)).ExecuteRequest();

                    using (var store = NewDocumentStore(databaseName: dbName, seedData: new[] { new string[] {} }, indexes: new AbstractIndexCreationTask[]
                    {
                    }))
                    {
                        store.DatabaseCommands.Put("users/1", null, userRJO, null);
                    }
                });
            }
        }
Beispiel #14
0
        private TcpConnectionInfo GetTcpInfo()
        {
            var convention = new DocumentConvention();

            //since we use it only once when the connection is initialized, no reason to keep requestFactory around for long
            using (var requestFactory = new HttpJsonRequestFactory(1))
                using (var request = requestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(null, string.Format("{0}/info/tcp",
                                                                                                                              MultiDatabase.GetRootDatabaseUrl(_destination.Url)),
                                                                                                          HttpMethod.Get,
                                                                                                          new OperationCredentials(_destination.ApiKey, CredentialCache.DefaultCredentials), convention)
                {
                    Timeout = TimeSpan.FromSeconds(15)
                }))
                {
                    var result            = request.ReadResponseJson();
                    var tcpConnectionInfo = convention.CreateSerializer().Deserialize <TcpConnectionInfo>(new RavenJTokenReader(result));
                    if (_log.IsInfoEnabled)
                    {
                        _log.Info($"Will replicate to {_destination.Database} @ {_destination.Url} via {tcpConnectionInfo.Url}");
                    }
                    return(tcpConnectionInfo);
                }
        }
Beispiel #15
0
        protected virtual DocumentStore GetDocumentStore(
            [CallerMemberName] string caller = null,
            string dbSuffixIdentifier        = null,
            string path = null,
            Action <DatabaseDocument> modifyDatabaseDocument = null,
            Func <string, string> modifyName = null,
            string apiKey = null)
        {
            var name = caller != null ? $"{caller}_{Interlocked.Increment(ref _counter)}" : Guid.NewGuid().ToString("N");

            if (dbSuffixIdentifier != null)
            {
                name = $"{name}_{dbSuffixIdentifier}";
            }

            if (modifyName != null)
            {
                name = modifyName(name);
            }

            var hardDelete  = true;
            var runInMemory = true;

            if (path == null)
            {
                path = NewDataPath(name);
            }
            else
            {
                hardDelete  = false;
                runInMemory = false;
            }

            var doc = MultiDatabase.CreateDatabaseDocument(name);

            doc.Settings[RavenConfiguration.GetKey(x => x.Core.RunInMemory)]   = runInMemory.ToString();
            doc.Settings[RavenConfiguration.GetKey(x => x.Core.DataDirectory)] = path;
            doc.Settings[RavenConfiguration.GetKey(x => x.Core.ThrowIfAnyIndexOrTransformerCouldNotBeOpened)] = "true";
            doc.Settings[RavenConfiguration.GetKey(x => x.Indexing.MinNumberOfMapAttemptsAfterWhichBatchWillBeCanceledIfRunningLowOnMemory)] = int.MaxValue.ToString();
            modifyDatabaseDocument?.Invoke(doc);

            TransactionOperationContext context;

            using (Server.ServerStore.ContextPool.AllocateOperationContext(out context))
            {
                context.OpenReadTransaction();
                if (Server.ServerStore.Read(context, Constants.Database.Prefix + name) != null)
                {
                    throw new InvalidOperationException($"Database '{name}' already exists");
                }
            }

            var store = new DocumentStore
            {
                Url             = UseFiddler(Server.Configuration.Core.ServerUrl),
                DefaultDatabase = name,
                ApiKey          = apiKey
            };

            ModifyStore(store);
            store.Initialize();

            store.DatabaseCommands.GlobalAdmin.CreateDatabase(doc);
            store.AfterDispose += (sender, args) =>
            {
                if (CreatedStores.TryRemove(store) == false)
                {
                    return; // can happen if we are wrapping the store inside sharded one
                }
                if (Server.Disposed == false)
                {
                    var databaseTask = Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(name);
                    if (databaseTask != null && databaseTask.IsCompleted == false)
                    {
                        databaseTask.Wait(); // if we are disposing store before database had chance to load then we need to wait
                    }
                    store.DatabaseCommands.GlobalAdmin.DeleteDatabase(name, hardDelete: hardDelete);
                }
            };
            CreatedStores.Add(store);
            return(store);
        }
Beispiel #16
0
 public static string AdminStats(this string url)
 {
     return(MultiDatabase.GetRootDatabaseUrl(url) + "/admin/stats");
 }
Beispiel #17
0
        private void WriteToServer(string url, Stream serverStream)
        {
            const string debugTag        = "bulk/insert/document";
            var          jsonParserState = new JsonParserState();
            //var streamNetworkBuffer = new BufferedStream(serverStream, 32 * 1024);
            var streamNetworkBuffer = serverStream;
            var writeToStreamBuffer = new byte[32 * 1024];
            var header = Encoding.UTF8.GetBytes(RavenJObject.FromObject(new TcpConnectionHeaderMessage
            {
                DatabaseName = MultiDatabase.GetDatabaseName(url),
                Operation    = TcpConnectionHeaderMessage.OperationTypes.BulkInsert
            }).ToString());

            streamNetworkBuffer.Write(header, 0, header.Length);
            JsonOperationContext.ManagedPinnedBuffer bytes;
            using (_jsonOperationContext.GetManagedBuffer(out bytes))
            {
                while (_documents.IsCompleted == false)
                {
                    _cts.Token.ThrowIfCancellationRequested();

                    MemoryStream jsonBuffer;

                    try
                    {
                        jsonBuffer = _documents.Take();
                    }
                    catch (InvalidOperationException)
                    {
                        break;
                    }

                    var needToThrottle = _throttlingEvent.Wait(0) == false;

                    _jsonOperationContext.ResetAndRenew();
                    using (var jsonParser = new UnmanagedJsonParser(_jsonOperationContext, jsonParserState, debugTag))
                        using (var builder = new BlittableJsonDocumentBuilder(_jsonOperationContext,
                                                                              BlittableJsonDocumentBuilder.UsageMode.ToDisk, debugTag,
                                                                              jsonParser, jsonParserState))
                        {
                            _jsonOperationContext.CachedProperties.NewDocument();
                            builder.ReadObjectDocument();
                            while (true)
                            {
                                var read = jsonBuffer.Read(bytes.Buffer.Array, bytes.Buffer.Offset, bytes.Length);
                                if (read == 0)
                                {
                                    throw new EndOfStreamException("Stream ended without reaching end of json content");
                                }
                                jsonParser.SetBuffer(bytes, read);
                                if (builder.Read())
                                {
                                    break;
                                }
                            }
                            _buffers.Add(jsonBuffer);
                            builder.FinalizeDocument();
                            WriteVariableSizeInt(streamNetworkBuffer, builder.SizeInBytes);
                            WriteToStream(streamNetworkBuffer, builder, writeToStreamBuffer);
                        }

                    if (needToThrottle)
                    {
                        streamNetworkBuffer.Flush();
                        _throttlingEvent.Wait(500);
                    }
                }
                streamNetworkBuffer.WriteByte(0); //done
                streamNetworkBuffer.Flush();
            }
        }
Beispiel #18
0
        public async Task StoreAsync(object entity)
        {
            var shardId = shardResolutionStrategy.GenerateShardIdFor(entity, this);
            var shard   = shards[shardId];
            BulkInsertOperation bulkInsertOperation;

            if (Bulks.TryGetValue(shardId, out bulkInsertOperation) == false)
            {
                var actualDatabaseName = database ?? ((DocumentStore)shard).DefaultDatabase ?? MultiDatabase.GetDatabaseName(shard.Url);
                bulkInsertOperation = new BulkInsertOperation(actualDatabaseName, shard, shard.Listeners);
                Bulks.Add(shardId, bulkInsertOperation);
            }

            DatabaseCommands = shard.AsyncDatabaseCommands;
            string id;

            if (generateEntityIdOnTheClient.TryGetIdFromInstance(entity, out id) == false)
            {
                id = generateEntityIdOnTheClient.GetOrGenerateDocumentKey(entity);
            }
            var modifyDocumentId = shardStrategy.ModifyDocumentId(shardedDocumentStore.Conventions, shardId, id);
            await bulkInsertOperation.StoreAsync(entity, modifyDocumentId).ConfigureAwait(false);
        }
Beispiel #19
0
        public static string Databases(this string url, int pageSize, int start)
        {
            var databases = $"{MultiDatabase.GetRootDatabaseUrl(url)}/databases?pageSize={pageSize}";

            return(start > 0 ? $"{databases}&start={start}" : databases);
        }
Beispiel #20
0
        public async Task Foo()
        {
            IFilesStore store = null;

            #region create_fs_2

            await store.AsyncFilesCommands.Admin
            .CreateFileSystemAsync(new FileSystemDocument
            {
                Id       = "Raven/FileSystem/NorthwindFS",
                Settings =
                {
                    { Constants.FileSystem.DataDirectory, "~/FileSystems/NorthwindFS" },
                    { Constants.ActiveBundles,            "Versioning"                }
                }
            });

            #endregion

            #region create_fs_3

            await store.AsyncFilesCommands.Admin
            .CreateFileSystemAsync(MultiDatabase.CreateFileSystemDocument("NorthwindFS"));

            #endregion

            #region ensure_fs_exists_2

            await store.AsyncFilesCommands.Admin
            .EnsureFileSystemExistsAsync("NorthwindFS");

            #endregion

            #region delete_fs_2

            await store.AsyncFilesCommands.Admin
            .DeleteFileSystemAsync(hardDelete : true);

            #endregion

            #region start_backup_2
            await store.AsyncFilesCommands.Admin
            .StartBackup(@"C:\backups\NorthwindFS", null, false, "NorthwindFS");

            #endregion

            #region start_backup_3
            BackupStatus status = await store.AsyncFilesCommands.Configuration
                                  .GetKeyAsync <BackupStatus>(BackupStatus.RavenBackupStatusDocumentKey);

            if (status.IsRunning)
            {
                // ... //
            }
            #endregion

            #region start_restore_2
            long restoreOperationId = await store.AsyncFilesCommands.Admin
                                      .StartRestore(new FilesystemRestoreRequest()
            {
                BackupLocation     = @"C:\backups\NorthwindFS",
                FilesystemLocation = @"~\FileSystems\NewNorthwindFS",
                FilesystemName     = "NewNorthwindFS"
            });

            #endregion

            #region start_restore_3
            using (var sysDbStore = new DocumentStore
            {
                Url = "http://localhost:8080/"
            }.Initialize())
            {
                await new Operation((AsyncServerClient)sysDbStore.AsyncDatabaseCommands, restoreOperationId)
                .WaitForCompletionAsync();
            }
            #endregion

            #region start_compact_2
            long compactOperationId = await store.AsyncFilesCommands.Admin
                                      .StartCompact("NorthwindFS");

            #endregion

            #region start_compact_3
            using (var sysDbStore = new DocumentStore
            {
                Url = "http://localhost:8080/"
            }.Initialize())
            {
                await new Operation((AsyncServerClient)sysDbStore.AsyncDatabaseCommands, compactOperationId)
                .WaitForCompletionAsync();
            }
            #endregion

            #region reset_indexes_2

            await store.AsyncFilesCommands.Admin
            .ResetIndexes("NorthwindFS");

            #endregion

            #region get_names_2
            string[] fsNames = await store.AsyncFilesCommands.Admin
                               .GetNamesAsync();

            #endregion

            #region get_stats_2
            FileSystemStats[] fsStats = await store.AsyncFilesCommands.Admin
                                        .GetStatisticsAsync();

            #endregion
        }
Beispiel #21
0
        protected virtual DocumentStore GetDocumentStore([CallerMemberName] string caller = null,
                                                         string dbSuffixIdentifier        = null, string path = null,
                                                         Action <DatabaseDocument> modifyDatabaseDocument = null,
                                                         string apiKey             = null,
                                                         bool deleteDbAfterDispose = true)
        {
            var name = caller != null ? $"{caller}_{Interlocked.Increment(ref _counter)}" : Guid.NewGuid().ToString("N");

            if (dbSuffixIdentifier != null)
            {
                name = $"{name}_{dbSuffixIdentifier}";
            }

            var runInMemory = true;

            if (path == null)
            {
                path = NewDataPath(name);
            }
            else
            {
                runInMemory = false;
            }

            var doc = MultiDatabase.CreateDatabaseDocument(name);

            doc.Settings[RavenConfiguration.GetKey(x => x.Core.RunInMemory)]   = runInMemory.ToString();
            doc.Settings[RavenConfiguration.GetKey(x => x.Core.DataDirectory)] = path;
            doc.Settings[RavenConfiguration.GetKey(x => x.Core.ThrowIfAnyIndexOrTransformerCouldNotBeOpened)] = "true";
            doc.Settings[RavenConfiguration.GetKey(x => x.Indexing.MinNumberOfMapAttemptsAfterWhichBatchWillBeCanceledIfRunningLowOnMemory)] = int.MaxValue.ToString();
            modifyDatabaseDocument?.Invoke(doc);

            TransactionOperationContext context;

            using (Server.ServerStore.ContextPool.AllocateOperationContext(out context))
            {
                context.OpenReadTransaction();
                if (Server.ServerStore.Read(context, Constants.Database.Prefix + name) != null)
                {
                    throw new InvalidOperationException($"Database '{name}' already exists");
                }
            }

            var store = new DocumentStore
            {
                Url             = UseFiddler(Server.Configuration.Core.ServerUrl),
                DefaultDatabase = name,
                ApiKey          = apiKey
            };

            ModifyStore(store);
            store.Initialize();

            var createDatabaseOperation = new CreateDatabaseOperation(context);
            var command = createDatabaseOperation.CreateRequest(store, doc);

            if (command != null)
            {
                store.GetRequestExecuter(name).Execute(command, context);
            }
            store.AfterDispose += (sender, args) =>
            {
                var databaseTask = Server.ServerStore.DatabasesLandlord.TryGetOrCreateResourceStore(name);
                if (databaseTask != null && databaseTask.IsCompleted == false)
                {
                    databaseTask.Wait(); // if we are disposing store before database had chance to load then we need to wait
                }
                Server.ServerStore.DatabasesLandlord.UnloadAndLock(name, () =>
                {
                    if (deleteDbAfterDispose)
                    {
                        var dbId = Constants.Database.Prefix + name;
                        using (Server.ServerStore.ContextPool.AllocateOperationContext(out context))
                            using (var tx = context.OpenWriteTransaction())
                            {
                                Server.ServerStore.Delete(context, dbId);
                                tx.Commit();
                            }

                        if (databaseTask != null)
                        {
                            DatabaseHelper.DeleteDatabaseFiles(databaseTask.Result.Configuration);
                        }
                    }
                });

                CreatedStores.TryRemove(store);
            };
            CreatedStores.Add(store);
            return(store);
        }
Beispiel #22
0
        private IRequestExecuter GetRequestExecuterForDatabase(AsyncServerClient serverClient, string databaseName,
                                                               bool incrementStrippingBase)
        {
            var key = Url;

            if (string.IsNullOrEmpty(databaseName) == false)
            {
                key = MultiDatabase.GetRootDatabaseUrl(Url) + "/databases/" + databaseName;
            }

            var originalKey = MultiDatabase.GetRootDatabaseUrl(Url);

            IRequestExecuter requestExecuter;
            IRequestExecuter originalRequestExecuter = null;

            if (Conventions.FailoverBehavior == FailoverBehavior.ReadFromLeaderWriteToLeader ||
                Conventions.FailoverBehavior == FailoverBehavior.ReadFromLeaderWriteToLeaderWithFailovers ||
                Conventions.FailoverBehavior == FailoverBehavior.ReadFromAllWriteToLeader ||
                Conventions.FailoverBehavior == FailoverBehavior.ReadFromAllWriteToLeaderWithFailovers)
            {
                requestExecuter = clusterAwareRequestExecuters.GetOrAdd(key, url => new ClusterAwareRequestExecuter());
                if (originalKey != key)
                {
                    originalRequestExecuter = clusterAwareRequestExecuters.GetOrAdd(originalKey, url => new ClusterAwareRequestExecuter());
                }
            }
            else
            {
                requestExecuter = new ReplicationAwareRequestExecuter(replicationInformers.GetOrAdd(key, url => Conventions.ReplicationInformerFactory(url, jsonRequestFactory, GetRequestTimeMetricForUrl)));
                if (originalKey != key)
                {
                    originalRequestExecuter = new ReplicationAwareRequestExecuter(replicationInformers.GetOrAdd(originalKey, url => Conventions.ReplicationInformerFactory(url, jsonRequestFactory, GetRequestTimeMetricForUrl)));
                }
            }

            if (incrementStrippingBase || originalRequestExecuter == null)
            {
                requestExecuter.GetReadStripingBase(incrementStrippingBase);
            }
            else
            {
                requestExecuter.SetReadStripingBase(originalRequestExecuter.GetReadStripingBase(false));
            }

            if (FailoverServers == null)
            {
                return(requestExecuter);
            }

            if (databaseName == DefaultDatabase)
            {
                if (FailoverServers.IsSetForDefaultDatabase && requestExecuter.FailoverServers == null)
                {
                    requestExecuter.FailoverServers = FailoverServers.ForDefaultDatabase;
                }
            }
            else
            {
                if (FailoverServers.IsSetForDatabase(databaseName) && requestExecuter.FailoverServers == null)
                {
                    requestExecuter.FailoverServers = FailoverServers.GetForDatabase(databaseName);
                }
            }

            return(requestExecuter);
        }
Beispiel #23
0
        public async Task Should_not_be_able_to_create_time_series_with_the_same_name_twice()
        {
            using (var store = NewRemoteTimeSeriesStore())
            {
                await store.Admin.CreateTimeSeriesAsync(MultiDatabase.CreateTimeSeriesDocument(TimeSeriesName));

                var exception = await AssertAsync.Throws <InvalidOperationException>(() => store.Admin.CreateTimeSeriesAsync(MultiDatabase.CreateTimeSeriesDocument(TimeSeriesName)));
            }
        }