public long FindFreeId(ClusterOperationContext context, long subscriptionId) { if (SubscriptionId.HasValue) { return(SubscriptionId.Value); } bool idTaken; do { idTaken = false; foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context, SubscriptionState.SubscriptionPrefix(DatabaseName))) { if (keyValue.Value.TryGet(nameof(SubscriptionState.SubscriptionId), out long id) == false) { continue; } if (id == subscriptionId) { subscriptionId--; // we don't care if this end up as a negative value, we need only to be unique idTaken = true; break; } } } while (idTaken); return(subscriptionId); }
public void RavenDB_13724() { var folder = NewDataPath(forceCreateDir: true, prefix: Guid.NewGuid().ToString()); DoNotReuseServer(); var zipPath = new PathSetting("SchemaUpgrade/Issues/SystemVersion/RavenDB_13724.zip"); Assert.True(File.Exists(zipPath.FullPath)); ZipFile.ExtractToDirectory(zipPath.FullPath, folder); using (var server = GetNewServer(new ServerCreationOptions { DeletePrevious = false, RunInMemory = false, DataDirectory = folder, RegisterForDisposal = false })) using (server.ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context)) using (context.OpenReadTransaction()) { var db = server.ServerStore.Cluster.GetDatabaseNames(context).Single(); var ids = new HashSet <long>(); foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context, SubscriptionState.SubscriptionPrefix(db))) { var state = JsonDeserializationClient.SubscriptionState(keyValue.Value); Assert.True(ids.Add(state.SubscriptionId)); } } }
public long GetAllSubscriptionsCount() { using (_serverStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context)) using (context.OpenReadTransaction()) { return(ClusterStateMachine.ReadValuesStartingWith(context, SubscriptionState.SubscriptionPrefix(_db.Name)) .Count()); } }
public IEnumerable <SubscriptionGeneralDataAndStats> GetAllSubscriptions(TransactionOperationContext serverStoreContext, bool history, int start, int take) { foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(serverStoreContext, SubscriptionState.SubscriptionPrefix(_db.Name))) { var subscriptionState = JsonDeserializationClient.SubscriptionState(keyValue.Value); var subscriptionGeneralData = new SubscriptionGeneralDataAndStats(subscriptionState); GetSubscriptionInternal(subscriptionGeneralData, history); yield return(subscriptionGeneralData); } }
public void FullBackupTo(string backupPath) { using (var file = new FileStream(backupPath, FileMode.Create)) using (var package = new ZipArchive(file, ZipArchiveMode.Create, leaveOpen: true)) using (_serverStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context)) using (context.OpenReadTransaction()) { var databaseRecord = _serverStore.Cluster.ReadDatabase(context, Name); Debug.Assert(databaseRecord != null); var zipArchiveEntry = package.CreateEntry(RestoreSettings.FileName, CompressionLevel.Optimal); using (var zipStream = zipArchiveEntry.Open()) using (var writer = new BlittableJsonTextWriter(context, zipStream)) { //TODO: encrypt this file using the MasterKey //http://issues.hibernatingrhinos.com/issue/RavenDB-7546 writer.WriteStartObject(); // save the database record writer.WritePropertyName(nameof(RestoreSettings.DatabaseRecord)); var databaseRecordBlittable = EntityToBlittable.ConvertEntityToBlittable(databaseRecord, DocumentConventions.Default, context); context.Write(writer, databaseRecordBlittable); // save the database values (subscriptions, periodic backups statuses, etl states...) writer.WriteComma(); writer.WritePropertyName(nameof(RestoreSettings.DatabaseValues)); writer.WriteStartObject(); var first = true; foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context, Helpers.ClusterStateMachineValuesPrefix(Name))) { if (first == false) { writer.WriteComma(); } first = false; writer.WritePropertyName(keyValue.Key.ToString()); context.Write(writer, keyValue.Value); } writer.WriteEndObject(); // end of dictionary writer.WriteEndObject(); } BackupMethods.Full.ToFile(GetAllStoragesEnvironmentInformation(), package); file.Flush(true); // make sure that we fully flushed to disk } }
private static long GetSubscriptionCountOnNode(DocumentDatabase database, DatabaseRecord dbRecord, ServerStore serverStore, TransactionOperationContext context) { long taskCountOnNode = 0; foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context, SubscriptionState.SubscriptionPrefix(database.Name))) { var subscriptionState = JsonDeserializationClient.SubscriptionState(keyValue.Value); var taskTag = database.WhoseTaskIsIt(dbRecord.Topology, subscriptionState, subscriptionState); if (serverStore.NodeTag == taskTag) { taskCountOnNode++; } } return(taskCountOnNode); }
public string GetSubscriptionNameById(TransactionOperationContext serverStoreContext, long id) { foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(serverStoreContext, SubscriptionState.SubscriptionPrefix(_db.Name))) { if (keyValue.Value.TryGet(nameof(SubscriptionState.SubscriptionId), out long _id) == false) { continue; } if (_id == id) { if (keyValue.Value.TryGet(nameof(SubscriptionState.SubscriptionName), out string name)) { return(name); } } } return(null); }
private IEnumerable <OngoingTask> CollectSubscriptionTasks(TransactionOperationContext context, DatabaseRecord databaseRecord, ClusterTopology clusterTopology) { foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context, SubscriptionState.SubscriptionPrefix(databaseRecord.DatabaseName))) { var subscriptionState = JsonDeserializationClient.SubscriptionState(keyValue.Value); var tag = databaseRecord.Topology.WhoseTaskIsIt(subscriptionState, ServerStore.Engine.CurrentState); yield return(new OngoingTaskSubscription { // Supply only needed fields for List View ResponsibleNode = new NodeId { NodeTag = tag, NodeUrl = clusterTopology.GetUrlFromTag(tag) }, TaskName = subscriptionState.SubscriptionName, TaskState = subscriptionState.Disabled ? OngoingTaskState.Disabled : OngoingTaskState.Enabled, TaskId = subscriptionState.SubscriptionId, Query = subscriptionState.Query }); } }
public void FullBackupTo(string backupPath) { using (var file = SafeFileStream.Create(backupPath, FileMode.Create)) using (var package = new ZipArchive(file, ZipArchiveMode.Create, leaveOpen: true)) using (_serverStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context)) using (context.OpenReadTransaction()) { var databaseRecord = _serverStore.Cluster.ReadDatabase(context, Name); Debug.Assert(databaseRecord != null); var zipArchiveEntry = package.CreateEntry(RestoreSettings.SmugglerValuesFileName, CompressionLevel.Optimal); using (var zipStream = zipArchiveEntry.Open()) { var smugglerSource = new DatabaseSource(this, 0); using (DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx)) using (ctx.OpenReadTransaction()) { var smugglerDestination = new StreamDestination(zipStream, ctx, smugglerSource); var databaseSmugglerOptionsServerSide = new DatabaseSmugglerOptionsServerSide { AuthorizationStatus = AuthorizationStatus.DatabaseAdmin, OperateOnTypes = DatabaseItemType.CompareExchange | DatabaseItemType.Identities }; var smuggler = new DatabaseSmuggler(this, smugglerSource, smugglerDestination, this.Time, options: databaseSmugglerOptionsServerSide); smuggler.Execute(); } } zipArchiveEntry = package.CreateEntry(RestoreSettings.SettingsFileName, CompressionLevel.Optimal); using (var zipStream = zipArchiveEntry.Open()) using (var writer = new BlittableJsonTextWriter(context, zipStream)) { //TODO: encrypt this file using the MasterKey //http://issues.hibernatingrhinos.com/issue/RavenDB-7546 writer.WriteStartObject(); // save the database record writer.WritePropertyName(nameof(RestoreSettings.DatabaseRecord)); var databaseRecordBlittable = EntityToBlittable.ConvertEntityToBlittable(databaseRecord, DocumentConventions.Default, context); context.Write(writer, databaseRecordBlittable); // save the database values (subscriptions, periodic backups statuses, etl states...) writer.WriteComma(); writer.WritePropertyName(nameof(RestoreSettings.DatabaseValues)); writer.WriteStartObject(); var first = true; var prefix = Helpers.ClusterStateMachineValuesPrefix(Name); foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context, prefix)) { if (first == false) { writer.WriteComma(); } first = false; var key = keyValue.Key.ToString().Substring(prefix.Length); writer.WritePropertyName(key); context.Write(writer, keyValue.Value); } writer.WriteEndObject(); // end of values writer.WriteEndObject(); } BackupMethods.Full.ToFile(GetAllStoragesForBackup(), package); file.Flush(true); // make sure that we fully flushed to disk } }