Ejemplo n.º 1
0
        public static ClusterTopologyResponse TryLoad(string topologyHash, JsonOperationContext context)
        {
            try
            {
                var path = GetPath(topologyHash);
                if (File.Exists(path) == false)
                {
                    return(null);
                }

                using (var stream = SafeFileStream.Create(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    using (var blittableJsonReaderObject = context.Read(stream, "raven-cluster-topology"))
                    {
                        return(JsonDeserializationClient.ClusterTopology(blittableJsonReaderObject));
                    }
            }
            catch (Exception e)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Could not understand the persisted cluster topology", e);
                }
                return(null);
            }
        }
Ejemplo n.º 2
0
        public static Topology TryLoadTopologyFromLocalCache(string serverHash, JsonOperationContext context)
        {
            try
            {
                var path = GetTopologyPath(serverHash);
                if (File.Exists(path) == false)
                {
                    return(null);
                }

                using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    using (var blittableJsonReaderObject = context.Read(stream, "raven-topology"))
                    {
                        return(JsonDeserializationClient.Topology(blittableJsonReaderObject));
                    }
                }
            }
            catch (Exception e)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Could not understand the persisted replication information", e);
                }
                return(null);
            }
        }
Ejemplo n.º 3
0
        private BlittableJsonReaderObject InitSimpleBlittable(JsonOperationContext context, out int size)
        {
            var obj = JObject.FromObject(new Employee
            {
                Id        = "1",
                FirstName = "Hibernating",
                LastName  = "Rhinos"
            });
            var objString    = obj.ToString(Formatting.None);
            var stream       = new MemoryStream();
            var streamWriter = new StreamWriter(stream);

            streamWriter.Write(objString);
            streamWriter.Flush();
            stream.Position = 0;
            var reader = context.Read(stream, "docs/1 ");

            size = reader.Size;
            return(reader);
        }
Ejemplo n.º 4
0
        private static Topology TryLoad(string path, JsonOperationContext context)
        {
            try
            {
                if (File.Exists(path) == false)
                {
                    return(null);
                }

                using (var stream = SafeFileStream.Create(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    using (var json = context.Read(stream, "raven-database-topology"))
                    {
                        return(JsonDeserializationClient.Topology(json));
                    }
            }
            catch (Exception e)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Could not understand the persisted database topology", e);
                }
                return(null);
            }
        }
Ejemplo n.º 5
0
        protected async Task <RestoreSettings> SnapshotRestore(JsonOperationContext context, string backupPath,
                                                               Action <IOperationProgress> onProgress, RestoreResult restoreResult)
        {
            Debug.Assert(onProgress != null);

            RestoreSettings restoreSettings = null;

            var fullBackupPath = GetBackupPath(backupPath);

            using (var zip = await GetZipArchiveForSnapshot(fullBackupPath))
            {
                foreach (var zipEntries in zip.Entries.GroupBy(x => x.FullName.Substring(0, x.FullName.Length - x.Name.Length)))
                {
                    var directory = zipEntries.Key;

                    if (string.IsNullOrWhiteSpace(directory))
                    {
                        foreach (var zipEntry in zipEntries)
                        {
                            if (string.Equals(zipEntry.Name, RestoreSettings.SettingsFileName, StringComparison.OrdinalIgnoreCase))
                            {
                                using (var entryStream = zipEntry.Open())
                                {
                                    var snapshotEncryptionKey = RestoreFromConfiguration.EncryptionKey != null
                                        ? Convert.FromBase64String(RestoreFromConfiguration.EncryptionKey)
                                        : null;

                                    using (var stream = GetInputStream(entryStream, snapshotEncryptionKey))
                                    {
                                        var json = context.Read(stream, "read database settings for restore");
                                        json.BlittableValidation();

                                        restoreSettings = JsonDeserializationServer.RestoreSettings(json);

                                        restoreSettings.DatabaseRecord.DatabaseName = RestoreFromConfiguration.DatabaseName;
                                        DatabaseHelper.Validate(RestoreFromConfiguration.DatabaseName, restoreSettings.DatabaseRecord, _serverStore.Configuration);

                                        if (restoreSettings.DatabaseRecord.Encrypted && _hasEncryptionKey == false)
                                        {
                                            throw new ArgumentException("Database snapshot is encrypted but the encryption key is missing!");
                                        }

                                        if (restoreSettings.DatabaseRecord.Encrypted == false && _hasEncryptionKey)
                                        {
                                            throw new ArgumentException("Cannot encrypt a non encrypted snapshot backup during restore!");
                                        }
                                    }
                                }
                            }
                        }

                        continue;
                    }

                    var voronDataDirectory = new VoronPathSetting(RestoreFromConfiguration.DataDirectory);
                    var restoreDirectory   = directory.StartsWith(Constants.Documents.PeriodicBackup.Folders.Documents, StringComparison.OrdinalIgnoreCase)
                        ? voronDataDirectory
                        : voronDataDirectory.Combine(directory);

                    BackupMethods.Full.Restore(
                        zipEntries,
                        restoreDirectory,
                        journalDir: null,
                        onProgress: message =>
                    {
                        restoreResult.AddInfo(message);
                        restoreResult.SnapshotRestore.ReadCount++;
                        onProgress.Invoke(restoreResult.Progress);
                    },
                        cancellationToken: _operationCancelToken.Token);
                }
            }

            if (restoreSettings == null)
            {
                throw new InvalidDataException("Cannot restore the snapshot without the settings file!");
            }

            return(restoreSettings);
        }
Ejemplo n.º 6
0
        public static IndexQueryServerSide Create(HttpContext httpContext, int start, int pageSize, JsonOperationContext context, RequestTimeTracker tracker, string overrideQuery = null)
        {
            IndexQueryServerSide result = null;

            try
            {
                var isQueryOverwritten = !string.IsNullOrEmpty(overrideQuery);
                if ((httpContext.Request.Query.TryGetValue("query", out var query) == false || query.Count == 0 || string.IsNullOrWhiteSpace(query[0])) &&
                    isQueryOverwritten == false)
                {
                    throw new InvalidOperationException("Missing mandatory query string parameter 'query'");
                }

                var actualQuery = isQueryOverwritten ? overrideQuery : query[0];
                result = new IndexQueryServerSide
                {
                    Query = Uri.UnescapeDataString(actualQuery),
                    // all defaults which need to have custom value
                    Start    = start,
                    PageSize = pageSize,
                };

                foreach (var item in httpContext.Request.Query)
                {
                    try
                    {
                        switch (item.Key)
                        {
                        case "query":
                            continue;

                        case "parameters":
                            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(item.Value[0])))
                            {
                                result.QueryParameters = context.Read(stream, "query parameters");
                            }

                            continue;

                        case RequestHandler.StartParameter:
                        case RequestHandler.PageSizeParameter:
                            break;

                        case "waitForNonStaleResults":
                            result.WaitForNonStaleResults = bool.Parse(item.Value[0]);
                            break;

                        case "waitForNonStaleResultsTimeoutInMs":
                            result.WaitForNonStaleResultsTimeout = TimeSpan.FromMilliseconds(long.Parse(item.Value[0]));
                            break;

                        case "skipDuplicateChecking":
                            result.SkipDuplicateChecking = bool.Parse(item.Value[0]);
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        throw new ArgumentException($"Could not handle query string parameter '{item.Key}' (value: {item.Value}) for query: {result.Query}", e);
                    }
                }

                result.Metadata = new QueryMetadata(result.Query, result.QueryParameters, 0);
                if (result.Metadata.HasTimings)
                {
                    result.Timings = new QueryTimingsScope(start: false);
                }

                tracker.Query = result.Query;
                return(result);
            }
            catch (Exception e)
            {
                string errorMessage;
                if (e is InvalidOperationException || e is ArgumentException)
                {
                    errorMessage = e.Message;
                }
                else
                {
                    errorMessage = $"Failed to parse query: {result.Query}";
                }

                tracker.Query = errorMessage;
                if (TrafficWatchManager.HasRegisteredClients)
                {
                    AddStringToHttpContext(httpContext, errorMessage, TrafficWatchChangeType.Queries);
                }
                throw;
            }
        }