public override void AfterCommit(string key, RavenJObject document, RavenJObject metadata, Guid etag) { base.AfterCommit(key, document, metadata, etag); var entityName = metadata.Value <string>(Constants.RavenEntityName); if (entityName != UpdateCascadeOperation.EntityName) { return; } if (document.Value <string>("Status") != "Pending") { return; } log.Trace("A new operation with id {0} has been put", key); var operation = document.JsonDeserialization <UpdateCascadeOperation>(); var referencedDocId = document.Value <string>("ReferencedDocId"); var referencedDoc = this.Database.Get(referencedDocId, null); if (referencedDoc == null) { return; } if (services.RunningOperationsCoordinator != null) { services.RunningOperationsCoordinator.TryStartOperation(operation, referencedDoc); } }
private List <SynchronizationTopologySourceNode> HandleSources(IEnumerable <string> sources, SynchronizationTopologyRootNode root) { var nodes = new List <SynchronizationTopologySourceNode>(); foreach (var source in sources) { RavenJObject sourceAsJson = null; filesystem.Storage.Batch(accessor => { sourceAsJson = accessor.GetConfig(source); }); SourceSynchronizationInformation sourceInfo = null; try { sourceInfo = sourceAsJson.JsonDeserialization <SourceSynchronizationInformation>(); } catch (Exception) { root.Errors.Add("Could not deserialize source node."); } var sourceDatabaseId = Guid.Parse(source.Split('/').Last()); var node = HandleSource(sourceInfo, sourceDatabaseId); nodes.Add(node); } return(nodes); }
public void CanDeserializeGuids() { var foo1 = new Foo { Guid = Guid.NewGuid() }; RavenJObject doc = RavenJObject.FromObject(foo1); var foo2 = doc.JsonDeserialization <Foo>(); Assert.Equal(foo1.Guid, foo2.Guid); }
public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation) { if (Database.Name != null && Database.Name != Constants.SystemDatabase) return VetoResult.Allowed; if (key.StartsWith(RavenDatabasesPrefix, StringComparison.InvariantCultureIgnoreCase) == false) return VetoResult.Allowed; var tempPermission = metadata[Constants.AllowBundlesChange]; if (tempPermission != null) metadata.Remove(Constants.AllowBundlesChange); // this is a temp marker so do not persist this medatada var bundlesChangesAllowed = tempPermission != null && tempPermission.Value<string>() .Equals("true", StringComparison.InvariantCultureIgnoreCase); if (bundlesChangesAllowed) return VetoResult.Allowed; var existingDbDoc = Database.Documents.Get(key, transactionInformation); if (existingDbDoc == null) return VetoResult.Allowed; var currentDbDocument = existingDbDoc.DataAsJson.JsonDeserialization<DatabaseDocument>(); var currentBundles = new List<string>(); string value; if (currentDbDocument.Settings.TryGetValue(Constants.ActiveBundles, out value)) currentBundles = value.GetSemicolonSeparatedValues(); var newDbDocument = document.JsonDeserialization<DatabaseDocument>(); var newBundles = new List<string>(); if (newDbDocument.Settings.TryGetValue(Constants.ActiveBundles, out value)) newBundles = value.GetSemicolonSeparatedValues(); if (currentBundles.Count == newBundles.Count) return VetoResult.Allowed; if (currentBundles.Count == 0) return VetoResult.Allowed; if (currentBundles.TrueForAll(x => newBundles.Contains(x))) return VetoResult.Allowed; return VetoResult.Deny( "You should not change 'Raven/ActiveBundles' setting for a database. This setting should be set only once when a database is created. " + "If you really need to override it you have to specify {\"" + Constants.AllowBundlesChange + "\": true} in metadata of a database document every time when you send it." + Environment.NewLine + "Current: " + string.Join("; ", currentBundles) + Environment.NewLine + "New: " + string.Join("; '", newBundles)); }
private static RavenJObject DisableSynchronizationDestinations(RavenJObject config) { var destinationsConfig = config.JsonDeserialization <SynchronizationDestinationsConfig>(); foreach (var destination in destinationsConfig.Destinations) { destination.Enabled = false; } return(RavenJObject.FromObject(destinationsConfig)); }
protected override void NotifySubscribers(string type, RavenJObject value, List <CountersConnectionState> connections) { switch (type) { case "ChangeNotification": var changeNotification = value.JsonDeserialization <ChangeNotification>(); foreach (var counter in connections) { counter.Send(changeNotification); } break; case "StartingWithNotification": var counterStartingWithNotification = value.JsonDeserialization <StartingWithNotification>(); foreach (var counter in connections) { counter.Send(counterStartingWithNotification); } break; case "InGroupNotification": var countersInGroupNotification = value.JsonDeserialization <InGroupNotification>(); foreach (var counter in connections) { counter.Send(countersInGroupNotification); } break; case "BulkOperationNotification": var bulkOperationNotification = value.JsonDeserialization <BulkOperationNotification>(); foreach (var counter in connections) { counter.Send(bulkOperationNotification); } break; default: break; } }
private BackupStatus GetBackupStatus() { RavenJObject backupStatus = null; try { filesystem.Storage.Batch(accessor => backupStatus = accessor.GetConfig(BackupStatus.RavenBackupStatusDocumentKey)); } catch (FileNotFoundException) { // config doesn't exists return(null); } return(backupStatus.JsonDeserialization <BackupStatus>()); }
protected override void NotifySubscribers(string type, RavenJObject value, List <TimeSeriesConnectionState> connections) { switch (type) { case "KeyChangeNotification": var changeNotification = value.JsonDeserialization <KeyChangeNotification>(); foreach (var timeSeries in connections) { timeSeries.Send(changeNotification); } break; case "BulkOperationNotification": var bulkOperationNotification = value.JsonDeserialization <BulkOperationNotification>(); foreach (var timeSeries in connections) { timeSeries.Send(bulkOperationNotification); } break; default: throw new InvalidOperationException("Type not valid: " + type); } }
public override void AfterCommit(string key, RavenJObject document, RavenJObject metadata, Guid etag) { // When a new clocks config doc is written, reconfigure the active timers if (key != ClocksConfig.Id) return; var config = document.JsonDeserialization<ClocksConfig>(); if (config == null) return; var maintainer = Database.StartupTasks.OfType<ClockDocsMaintainer>().SingleOrDefault(); if (maintainer == null) return; maintainer.Configure(config); }
public override ReadVetoResult AllowRead(string key, RavenJObject metadata, ReadOperation readOperation, TransactionInformation transactionInformation) { if (readOperation == ReadOperation.Query) { RavenJObject rawValue = metadata[Metadata.Key] as RavenJObject; if (rawValue != null) { Metadata value = rawValue.JsonDeserialization <Metadata>(); if (value != null) { this.Id = value.Id; } } } return(ReadVetoResult.Allowed); }
public override void AfterCommit(string key, RavenJObject document, RavenJObject metadata, Guid etag) { base.AfterCommit(key, document, metadata, etag); var entityName = metadata.Value<string>(Constants.RavenEntityName); if (entityName != UpdateCascadeOperation.EntityName) return; if (document.Value<string>("Status") != "Pending") return; log.Trace("A new operation with id {0} has been put", key); var operation = document.JsonDeserialization<UpdateCascadeOperation>(); var referencedDocId = document.Value<string>("ReferencedDocId"); var referencedDoc = this.Database.Get(referencedDocId, null); if (referencedDoc == null) return; if (services.RunningOperationsCoordinator != null) { services.RunningOperationsCoordinator.TryStartOperation(operation, referencedDoc); } }
public override VetoResult AllowPut(string key, RavenJObject document, RavenJObject metadata, TransactionInformation transactionInformation) { if (Database.Name != null && Database.Name != Constants.SystemDatabase) { return(VetoResult.Allowed); } if (key.StartsWith(RavenDatabasesPrefix, StringComparison.InvariantCultureIgnoreCase) == false) { return(VetoResult.Allowed); } var tempPermission = metadata[Constants.AllowBundlesChange]; if (tempPermission != null) { metadata.Remove(Constants.AllowBundlesChange); // this is a temp marker so do not persist this medatada } var bundlesChangesAllowed = tempPermission != null && tempPermission.Value <string>() .Equals("true", StringComparison.InvariantCultureIgnoreCase); if (bundlesChangesAllowed) { return(VetoResult.Allowed); } var existingDbDoc = Database.Documents.Get(key, transactionInformation); if (existingDbDoc == null) { return(VetoResult.Allowed); } var currentDbDocument = existingDbDoc.DataAsJson.JsonDeserialization <DatabaseDocument>(); var currentBundles = new List <string>(); string value; if (currentDbDocument.Settings.TryGetValue(Constants.ActiveBundles, out value)) { currentBundles = value.GetSemicolonSeparatedValues(); } var newDbDocument = document.JsonDeserialization <DatabaseDocument>(); var newBundles = new List <string>(); if (newDbDocument.Settings.TryGetValue(Constants.ActiveBundles, out value)) { newBundles = value.GetSemicolonSeparatedValues(); } if (currentBundles.Count != newBundles.Count || currentBundles.TrueForAll(x => newBundles.Contains(x, StringComparer.InvariantCultureIgnoreCase)) == false) { return(VetoResult.Deny( "You should not change 'Raven/ActiveBundles' setting for a database. This setting should be set only once when a database is created. " + "If you really need to override it you have to specify {\"" + Constants.AllowBundlesChange + "\": true} in metadata of a database document every time when you send it." + Environment.NewLine + "Current: " + string.Join("; ", currentBundles) + Environment.NewLine + "New: " + string.Join("; ", newBundles))); } return(VetoResult.Allowed); }
public async Task <HttpResponseMessage> Backup() { var backupRequest = await ReadJsonObjectAsync <FilesystemBackupRequest>(); var incrementalString = InnerRequest.RequestUri.ParseQueryString()["incremental"]; bool incrementalBackup; if (bool.TryParse(incrementalString, out incrementalBackup) == false) { incrementalBackup = false; } if (backupRequest.FileSystemDocument == null && FileSystem.Name != null) { var jsonDocument = DatabasesLandlord.SystemDatabase.Documents.Get("Raven/FileSystems/" + FileSystem.Name, null); if (jsonDocument != null) { backupRequest.FileSystemDocument = jsonDocument.DataAsJson.JsonDeserialization <FileSystemDocument>(); FileSystemsLandlord.Unprotect(backupRequest.FileSystemDocument); backupRequest.FileSystemDocument.Id = FileSystem.Name; } } var transactionalStorage = FileSystem.Storage; var filesystemDocument = backupRequest.FileSystemDocument; var backupDestinationDirectory = backupRequest.BackupLocation; RavenJObject document = null; try { FileSystem.Storage.Batch(accessor => document = accessor.GetConfig(BackupStatus.RavenBackupStatusDocumentKey)); } catch (FileNotFoundException) { // ok, there isn't another backup in progress } if (document != null) { var backupStatus = document.JsonDeserialization <BackupStatus>(); if (backupStatus.IsRunning) { throw new InvalidOperationException("Backup is already running"); } } bool enableIncrementalBackup; if (incrementalBackup && transactionalStorage is Storage.Esent.TransactionalStorage && (bool.TryParse(Database.Configuration.Settings["Raven/Esent/CircularLog"], out enableIncrementalBackup) == false || enableIncrementalBackup)) { throw new InvalidOperationException("In order to run incremental backups using Esent you must have circular logging disabled"); } if (incrementalBackup && transactionalStorage is Storage.Voron.TransactionalStorage && Database.Configuration.Storage.Voron.AllowIncrementalBackups == false) { throw new InvalidOperationException("In order to run incremental backups using Voron you must have the appropriate setting key (Raven/Voron/AllowIncrementalBackups) set to true"); } FileSystem.Storage.Batch(accessor => accessor.SetConfig(BackupStatus.RavenBackupStatusDocumentKey, RavenJObject.FromObject(new BackupStatus { Started = SystemTime.UtcNow, IsRunning = true, }))); if (filesystemDocument.Settings.ContainsKey(Constants.FileSystem.Storage) == false) { filesystemDocument.Settings[Constants.FileSystem.Storage] = transactionalStorage.FriendlyName.ToLower() ?? transactionalStorage.GetType().AssemblyQualifiedName; } transactionalStorage.StartBackupOperation(DatabasesLandlord.SystemDatabase, FileSystem, backupDestinationDirectory, incrementalBackup, filesystemDocument); return(GetEmptyMessage(HttpStatusCode.Created)); }
public async Task <HttpResponseMessage> Backup() { var backupRequest = await ReadJsonObjectAsync <FilesystemBackupRequest>().ConfigureAwait(false); var incrementalString = InnerRequest.RequestUri.ParseQueryString()["incremental"]; bool incrementalBackup; if (bool.TryParse(incrementalString, out incrementalBackup) == false) { incrementalBackup = false; } if (backupRequest.FileSystemDocument == null && FileSystem.Name != null) { var jsonDocument = DatabasesLandlord.SystemDatabase.Documents.Get(Constants.FileSystem.Prefix + FileSystem.Name, null); if (jsonDocument != null) { backupRequest.FileSystemDocument = jsonDocument.DataAsJson.JsonDeserialization <FileSystemDocument>(); FileSystemsLandlord.Unprotect(backupRequest.FileSystemDocument); backupRequest.FileSystemDocument.Id = FileSystem.Name; } } var transactionalStorage = FileSystem.Storage; var filesystemDocument = backupRequest.FileSystemDocument; var backupDestinationDirectory = backupRequest.BackupLocation; RavenJObject document = null; try { FileSystem.Storage.Batch(accessor => document = accessor.GetConfig(BackupStatus.RavenBackupStatusDocumentKey)); } catch (FileNotFoundException) { // ok, there isn't another backup in progress } if (document != null) { var backupStatus = document.JsonDeserialization <BackupStatus>(); if (backupStatus.IsRunning) { throw new InvalidOperationException("Backup is already running"); } } HttpResponseMessage message; if (!HasPermissions(backupDestinationDirectory, out message)) { return(message); } bool enableIncrementalBackup; if (incrementalBackup && transactionalStorage is Storage.Esent.TransactionalStorage && (bool.TryParse(FileSystem.Configuration.Settings["Raven/Esent/CircularLog"], out enableIncrementalBackup) == false || enableIncrementalBackup)) { throw new InvalidOperationException("In order to run incremental backups using Esent you must have circular logging disabled"); } if (incrementalBackup && transactionalStorage is Storage.Voron.TransactionalStorage && FileSystem.Configuration.Storage.Voron.AllowIncrementalBackups == false) { throw new InvalidOperationException("In order to run incremental backups using Voron you must have the appropriate setting key (Raven/Voron/AllowIncrementalBackups) set to true"); } FileSystem.Storage.Batch(accessor => accessor.SetConfig(BackupStatus.RavenBackupStatusDocumentKey, RavenJObject.FromObject(new BackupStatus { Started = SystemTime.UtcNow, IsRunning = true, }))); if (filesystemDocument.Settings.ContainsKey(Constants.FileSystem.Storage) == false) { filesystemDocument.Settings[Constants.FileSystem.Storage] = transactionalStorage.FriendlyName.ToLower() ?? transactionalStorage.GetType().AssemblyQualifiedName; } var cts = new CancellationTokenSource(); var state = new ResourceBackupState(); var task = transactionalStorage.StartBackupOperation(DatabasesLandlord.SystemDatabase, FileSystem, backupDestinationDirectory, incrementalBackup, filesystemDocument, state, cts.Token); task.ContinueWith(_ => cts.Dispose()); long id; SystemDatabase.Tasks.AddTask(task, state, new TaskActions.PendingTaskDescription { StartTime = SystemTime.UtcNow, TaskType = TaskActions.PendingTaskType.BackupFilesystem, Description = "Backup to: " + backupRequest.BackupLocation }, out id, cts); return(GetMessageWithObject(new { OperationId = id }, HttpStatusCode.Accepted)); }
protected override void NotifySubscribers(string type, RavenJObject value, IEnumerable <KeyValuePair <string, FilesConnectionState> > connections) { switch (type) { case "ConfigurationChangeNotification": var configChangeNotification = value.JsonDeserialization <ConfigurationChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(configChangeNotification); } break; case "FileChangeNotification": var fileChangeNotification = value.JsonDeserialization <FileChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(fileChangeNotification); } break; case "SynchronizationUpdateNotification": var synchronizationUpdateNotification = value.JsonDeserialization <SynchronizationUpdateNotification>(); foreach (var counter in connections) { counter.Value.Send(synchronizationUpdateNotification); } break; case "ConflictNotification": var conflictNotification = value.JsonDeserialization <ConflictNotification>(); if (conflictNotification.Status == ConflictStatus.Detected) { // We don't care about this one (this can happen concurrently). delayedConflictNotifications.AddOrUpdate(conflictNotification.FileName, conflictNotification, (x, y) => conflictNotification); tryResolveConflictByUsingRegisteredConflictListenersAsync(conflictNotification.FileName, conflictNotification.RemoteFileHeader, conflictNotification.SourceServerUrl, () => NotifyConflictSubscribers(connections, conflictNotification)) .ContinueWith(t => { t.AssertNotFailed(); // We need the lock to avoid a race conditions where a Detected happens and also a Resolved happen before the continuation can take control.. lock ( delayedConflictNotifications ) { ConflictNotification notification; if (delayedConflictNotifications.TryRemove(conflictNotification.FileName, out notification)) { if (notification.Status == ConflictStatus.Resolved) { NotifyConflictSubscribers(connections, notification); } } } if (t.Result) { logger.Debug("Document replication conflict for {0} was resolved by one of the registered conflict listeners", conflictNotification.FileName); } }).ConfigureAwait(false); } else if (conflictNotification.Status == ConflictStatus.Resolved) { // We need the lock to avoid race conditions. lock ( delayedConflictNotifications ) { if (delayedConflictNotifications.ContainsKey(conflictNotification.FileName)) { delayedConflictNotifications.AddOrUpdate(conflictNotification.FileName, conflictNotification, (x, y) => conflictNotification); // We are delaying broadcasting. conflictNotification = null; } else { NotifyConflictSubscribers(connections, conflictNotification); } } } break; default: break; } }
public static ScriptedPatchRequest FromJson(RavenJObject patchRequestJson) { return patchRequestJson.JsonDeserialization<ScriptedPatchRequest>(); }
protected override void NotifySubscribers(string type, RavenJObject value, List <DatabaseConnectionState> connections) { switch (type) { case "DocumentChangeNotification": var documentChangeNotification = value.JsonDeserialization <DocumentChangeNotification>(); foreach (var counter in connections) { counter.Send(documentChangeNotification); } break; case "OperationStatusChangeNotification": // using deserializer from Conventions to properly handle $type mapping var operationChangeNotification = Conventions.CreateSerializer().Deserialize <OperationStatusChangeNotification>(new RavenJTokenReader(value)); foreach (var counter in connections) { counter.Send(operationChangeNotification); } break; case "BulkInsertChangeNotification": var bulkInsertChangeNotification = value.JsonDeserialization <BulkInsertChangeNotification>(); foreach (var counter in connections) { counter.Send(bulkInsertChangeNotification); } break; case "IndexChangeNotification": var indexChangeNotification = value.JsonDeserialization <IndexChangeNotification>(); foreach (var counter in connections) { counter.Send(indexChangeNotification); } break; case "TransformerChangeNotification": var transformerChangeNotification = value.JsonDeserialization <TransformerChangeNotification>(); foreach (var counter in connections) { counter.Send(transformerChangeNotification); } break; case "ReplicationConflictNotification": var replicationConflictNotification = value.JsonDeserialization <ReplicationConflictNotification>(); foreach (var counter in connections) { counter.Send(replicationConflictNotification); } if (replicationConflictNotification.ItemType == ReplicationConflictTypes.DocumentReplicationConflict) { tryResolveConflictByUsingRegisteredConflictListenersAsync(replicationConflictNotification.Id, replicationConflictNotification.Etag, replicationConflictNotification.Conflicts, null) .ContinueWith(t => { t.AssertNotFailed(); if (t.Result) { if (Logger.IsDebugEnabled) { Logger.Debug("Document replication conflict for {0} was resolved by one of the registered conflict listeners", replicationConflictNotification.Id); } } }); } break; case "DataSubscriptionChangeNotification": var dataSubscriptionChangeNotification = value.JsonDeserialization <DataSubscriptionChangeNotification>(); foreach (var counter in connections) { counter.Send(dataSubscriptionChangeNotification); } break; default: break; } }
public static ScriptedPatchRequest FromJson(RavenJObject patchRequestJson) { return(patchRequestJson.JsonDeserialization <ScriptedPatchRequest>()); }
protected override void NotifySubscribers(string type, RavenJObject value, IEnumerable <KeyValuePair <string, DatabaseConnectionState> > connections) { switch (type) { case "DocumentChangeNotification": var documentChangeNotification = value.JsonDeserialization <DocumentChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(documentChangeNotification); } break; case "BulkInsertChangeNotification": var bulkInsertChangeNotification = value.JsonDeserialization <BulkInsertChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(bulkInsertChangeNotification); } break; case "IndexChangeNotification": var indexChangeNotification = value.JsonDeserialization <IndexChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(indexChangeNotification); } break; case "TransformerChangeNotification": var transformerChangeNotification = value.JsonDeserialization <TransformerChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(transformerChangeNotification); } break; case "ReplicationConflictNotification": var replicationConflictNotification = value.JsonDeserialization <ReplicationConflictNotification>(); foreach (var counter in connections) { counter.Value.Send(replicationConflictNotification); } if (replicationConflictNotification.ItemType == ReplicationConflictTypes.DocumentReplicationConflict) { tryResolveConflictByUsingRegisteredConflictListenersAsync(replicationConflictNotification.Id, replicationConflictNotification.Etag, replicationConflictNotification.Conflicts, null) .ContinueWith(t => { t.AssertNotFailed(); if (t.Result) { log.Debug("Document replication conflict for {0} was resolved by one of the registered conflict listeners", replicationConflictNotification.Id); } }); } break; case "DataSubscriptionChangeNotification": var dataSubscriptionChangeNotification = value.JsonDeserialization <DataSubscriptionChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(dataSubscriptionChangeNotification); } break; default: break; } }
protected override void NotifySubscribers(string type, RavenJObject value, IEnumerable <KeyValuePair <string, FilesConnectionState> > connections) { switch (type) { case "ConfigurationChangeNotification": var configChangeNotification = value.JsonDeserialization <ConfigurationChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(configChangeNotification); } break; case "FileChangeNotification": var fileChangeNotification = value.JsonDeserialization <FileChangeNotification>(); foreach (var counter in connections) { counter.Value.Send(fileChangeNotification); } break; case "SynchronizationUpdateNotification": var synchronizationUpdateNotification = value.JsonDeserialization <SynchronizationUpdateNotification>(); foreach (var counter in connections) { counter.Value.Send(synchronizationUpdateNotification); } break; case "CancellationNotification": var uploadFailedNotification = value.JsonDeserialization <CancellationNotification>(); foreach (var counter in connections) { counter.Value.Send(uploadFailedNotification); } break; case "ConflictNotification": var conflictNotification = value.JsonDeserialization <ConflictNotification>(); foreach (var counter in connections) { counter.Value.Send(conflictNotification); } break; case "ConflictDetectedNotification": var conflictDetectedNotification = value.JsonDeserialization <ConflictNotification>(); foreach (var counter in connections) { counter.Value.Send(conflictDetectedNotification); } break; case "ConflictResolvedNotification": var conflictResolvedNotification = value.JsonDeserialization <ConflictNotification>(); foreach (var counter in connections) { counter.Value.Send(conflictResolvedNotification); } break; default: break; } }
public static ReplicationMessage GetReplicationMessage(RavenJObject obj) { return obj.JsonDeserialization<ReplicationMessage>(); }