Esempio n. 1
0
        public Task Performance()
        {
            using (ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();

                    writer.WriteArray(context, nameof(ReplicationPerformance.Incoming), Database.ReplicationLoader.IncomingHandlers, (w, c, handler) =>
                    {
                        w.WriteStartObject();

                        w.WritePropertyName(nameof(ReplicationPerformance.IncomingStats.Source));
                        w.WriteString(handler.SourceFormatted);
                        w.WriteComma();

                        w.WriteArray(c, nameof(ReplicationPerformance.IncomingStats.Performance), handler.GetReplicationPerformance(), (innerWriter, innerContext, performance) =>
                        {
                            var djv = (DynamicJsonValue)TypeConverter.ToBlittableSupportedType(performance);
                            innerWriter.WriteObject(context.ReadObject(djv, "replication/performance"));
                        });

                        w.WriteEndObject();
                    });
                    writer.WriteComma();

                    var reporters = Database.ReplicationLoader.OutgoingHandlers.Concat <IReportOutgoingReplicationPerformance>(Database.ReplicationLoader
                                                                                                                               .OutgoingConnectionsLastFailureToConnect.Values);

                    writer.WriteArray(context, nameof(ReplicationPerformance.Outgoing), reporters, (w, c, handler) =>
                    {
                        w.WriteStartObject();

                        w.WritePropertyName(nameof(ReplicationPerformance.OutgoingStats.Destination));
                        w.WriteString(handler.DestinationFormatted);
                        w.WriteComma();

                        w.WriteArray(c, nameof(ReplicationPerformance.OutgoingStats.Performance), handler.GetReplicationPerformance(), (innerWriter, innerContext, performance) =>
                        {
                            var djv = (DynamicJsonValue)TypeConverter.ToBlittableSupportedType(performance);
                            innerWriter.WriteObject(context.ReadObject(djv, "replication/performance"));
                        });

                        w.WriteEndObject();
                    });

                    writer.WriteEndObject();
                }

            return(Task.CompletedTask);
        }
Esempio n. 2
0
        public Task ListCmpXchgValues()
        {
            var prefix = Database.Name + "/";
            var key    = prefix + GetStringQueryString("startsWith", false);
            var page   = GetStart();
            var size   = GetPageSize();

            ServerStore.EnsureNotPassive();
            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
            {
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    using (context.OpenReadTransaction())
                        writer.WriteArray(context, "Results", ServerStore.Cluster.GetCmpXchgByPrefix(context, Database.Name, key, page, size),
                                          (textWriter, operationContext, item) =>
                        {
                            operationContext.Write(textWriter, new DynamicJsonValue
                            {
                                ["Key"]   = item.Key,
                                ["Value"] = item.Value,
                                ["Index"] = item.Index
                            });
                        });
                    writer.WriteEndObject();
                    writer.Flush();
                }
            }
            return(Task.CompletedTask);
        }
Esempio n. 3
0
        public Task GetErrors()
        {
            var names = GetStringValuesQueryString("name", required: false);

            List <Index> indexes;

            if (names.Count == 0)
            {
                indexes = Database.IndexStore.GetIndexes().ToList();
            }
            else
            {
                indexes = new List <Index>();
                foreach (var name in names)
                {
                    var index = Database.IndexStore.GetIndex(name);
                    if (index == null)
                    {
                        IndexDoesNotExistException.ThrowFor(name);
                    }

                    indexes.Add(index);
                }
            }

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WriteArray(context, "Results", indexes, (w, c, index) =>
                    {
                        w.WriteStartObject();
                        w.WritePropertyName("Name");
                        w.WriteString(index.Name);
                        w.WriteComma();
                        w.WriteArray(c, "Errors", index.GetErrors(), (ew, ec, error) =>
                        {
                            ew.WriteStartObject();
                            ew.WritePropertyName(nameof(error.Timestamp));
                            ew.WriteDateTime(error.Timestamp, isUtc: true);
                            ew.WriteComma();

                            ew.WritePropertyName(nameof(error.Document));
                            ew.WriteString(error.Document);
                            ew.WriteComma();

                            ew.WritePropertyName(nameof(error.Action));
                            ew.WriteString(error.Action);
                            ew.WriteComma();

                            ew.WritePropertyName(nameof(error.Error));
                            ew.WriteString(error.Error);
                            ew.WriteEndObject();
                        });
                        w.WriteEndObject();
                    });
                    writer.WriteEndObject();
                }
            return(Task.CompletedTask);
        }
Esempio n. 4
0
        private void WriteTablesArray(string name, SqlSchemaResultTable[] tables, BlittableJsonTextWriter writer)
        {
            writer.WritePropertyName(name);
            writer.WriteStartArray();

            var first = true;

            foreach (var table in tables)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    writer.WriteComma();
                }

                writer.WriteStartObject();

                writer.WritePropertyName(nameof(table.Name));
                writer.WriteString(table.Name);
                writer.WriteComma();

                WriteColumnsArray(nameof(table.Columns), table.Columns, writer);
                writer.WriteComma();

                writer.WriteArray(nameof(table.EmbeddedTables), table.EmbeddedTables);

                writer.WriteEndObject();
            }

            writer.WriteEndArray();
        }
Esempio n. 5
0
        public Task Performance()
        {
            var stats = GetProcessesToReportOn()
                        .Select(x => new EtlProcessPerformanceStats
            {
                ProcessName = x.Name,
                Performance = x.GetPerformanceStats()
            })
                        .ToArray();

            using (Database.DocumentsStorage.ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WriteArray(context, "Results", stats, (w, c, stat) =>
                    {
                        w.WriteStartObject();

                        w.WritePropertyName(nameof(stat.ProcessName));
                        w.WriteString(stat.ProcessName);
                        w.WriteComma();

                        w.WriteArray(c, nameof(stat.Performance), stat.Performance, (wp, cp, performance) =>
                        {
                            var statsDjv = (DynamicJsonValue)TypeConverter.ToBlittableSupportedType(performance);
                            wp.WriteObject(context.ReadObject(statsDjv, "etl/performance"));
                        });

                        w.WriteEndObject();
                    });
                    writer.WriteEndObject();
                }

            return(Task.CompletedTask);
        }
Esempio n. 6
0
        public Task GetDebugStats()
        {
            var debugStats = GetProcessesToReportOn().Select(x => new DynamicJsonValue()
            {
                ["TaskName"] = x.Key,
                ["Stats"]    = x.Value.Select(y =>
                {
                    var stats = new EtlProcessTransformationStats
                    {
                        TransformationName = y.TransformationName,
                        Statistics         = y.Statistics
                    }.ToJson();

                    stats[nameof(y.Metrics)] = y.Metrics.ToJson();

                    return(stats);
                }).ToArray()
            }).ToArray();

            using (ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WriteArray(context, "Results", debugStats, (w, c, stats) =>
                    {
                        w.WriteObject(context.ReadObject(stats, "etl/debug/stats"));
                    });
                    writer.WriteEndObject();
                }
            }
            return(Task.CompletedTask);
        }
Esempio n. 7
0
        public Task Stale()
        {
            var name = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name");

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    using (context.OpenReadTransaction())
                    {
                        var index = Database.IndexStore.GetIndex(name);
                        if (index == null)
                        {
                            IndexDoesNotExistException.ThrowFor(name);
                        }

                        var stalenessReasons = new List <string>();
                        var isStale          = index.IsStale(context, stalenessReasons: stalenessReasons);

                        writer.WriteStartObject();

                        writer.WritePropertyName("IsStale");
                        writer.WriteBool(isStale);
                        writer.WriteComma();

                        writer.WriteArray("StalenessReasons", stalenessReasons);

                        writer.WriteEndObject();
                    }

            return(Task.CompletedTask);
        }
        public static void WriteSuggestionQueryResult(this BlittableJsonTextWriter writer, JsonOperationContext context, SuggestionQueryResultServerSide result)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(result.DurationInMs));
            writer.WriteInteger(result.DurationInMs);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.IndexName));
            writer.WriteString(result.IndexName);
            writer.WriteComma();

            writer.WriteArray(nameof(result.Suggestions), result.Suggestions);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.IndexTimestamp));
            writer.WriteString(result.IndexTimestamp.ToString(Default.DateTimeFormatsToWrite));
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.LastQueryTime));
            writer.WriteString(result.LastQueryTime.ToString(Default.DateTimeFormatsToWrite));
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.IsStale));
            writer.WriteBool(result.IsStale);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.ResultEtag));
            writer.WriteInteger(result.ResultEtag);

            writer.WriteEndObject();
        }
Esempio n. 9
0
        public static void PrepareRequestWithMultipleIds(StringBuilder pathBuilder, HttpRequestMessage request, string[] ids, JsonOperationContext context)
        {
            var uniqueIds = new HashSet <string>(ids);
            // if it is too big, we drop to POST (note that means that we can't use the HTTP cache any longer)
            // we are fine with that, requests to load > 1024 items are going to be rare
            var isGet = uniqueIds.Sum(x => x.Length) < 1024;

            if (isGet)
            {
                uniqueIds.ApplyIfNotNull(id => pathBuilder.Append($"&id={Uri.EscapeDataString(id)}"));
            }
            else
            {
                request.Method = HttpMethod.Post;

                request.Content = new BlittableJsonContent(stream =>
                {
                    using (var writer = new BlittableJsonTextWriter(context, stream))
                    {
                        writer.WriteStartObject();
                        writer.WriteArray("Ids", uniqueIds);
                        writer.WriteEndObject();
                    }
                });
            }
        }
Esempio n. 10
0
            private void WriteRavenConnectionStrings(Dictionary <string, RavenConnectionString> connections)
            {
                _writer.WriteStartObject();

                var first = true;

                foreach (var ravenConnectionString in connections)
                {
                    if (first == false)
                    {
                        _writer.WriteComma();
                    }
                    first = false;

                    _writer.WritePropertyName(nameof(ravenConnectionString.Key));

                    _writer.WriteStartObject();

                    var value = ravenConnectionString.Value;
                    _writer.WritePropertyName(nameof(value.Name));
                    _writer.WriteString(value.Name);
                    _writer.WriteComma();

                    _writer.WritePropertyName(nameof(value.Database));
                    _writer.WriteString(value.Database);
                    _writer.WriteComma();

                    _writer.WriteArray(nameof(value.TopologyDiscoveryUrls), value.TopologyDiscoveryUrls);

                    _writer.WriteEndObject();
                }

                _writer.WriteEndObject();
            }
Esempio n. 11
0
        private void GetCompareExchangeValues(TransactionOperationContext context)
        {
            var sw = Stopwatch.StartNew();

            var start    = GetStart();
            var pageSize = GetPageSize();

            var startsWithKey = GetStringQueryString("startsWith", false);
            var items         = ServerStore.Cluster.GetCompareExchangeValuesStartsWith(context, Database.Name, CompareExchangeKey.GetStorageKey(Database.Name, startsWithKey), start, pageSize);

            var numberOfResults = 0;

            using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
            {
                writer.WriteStartObject();

                writer.WriteArray(context, "Results", items,
                                  (textWriter, operationContext, item) =>
                {
                    numberOfResults++;
                    operationContext.Write(textWriter, new DynamicJsonValue
                    {
                        [nameof(CompareExchangeListItem.Key)]   = item.Key.Key,
                        [nameof(CompareExchangeListItem.Value)] = item.Value,
                        [nameof(CompareExchangeListItem.Index)] = item.Index
                    });
                });

                writer.WriteEndObject();
            }

            AddPagingPerformanceHint(PagingOperationType.CompareExchange, nameof(ClusterStateMachine.GetCompareExchangeValuesStartsWith),
                                     HttpContext.Request.QueryString.Value, numberOfResults, pageSize, sw.ElapsedMilliseconds);
        }
        public static void WriteDocumentQueryResult(this BlittableJsonTextWriter writer, JsonOperationContext context, DocumentQueryResult result, bool metadataOnly, out int numberOfResults)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(result.TotalResults));
            writer.WriteInteger(result.TotalResults);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.SkippedResults));
            writer.WriteInteger(result.SkippedResults);
            writer.WriteComma();

            writer.WritePropertyName(nameof(result.DurationInMs));
            writer.WriteInteger(result.DurationInMs);
            writer.WriteComma();


            writer.WriteArray(nameof(result.IncludedPaths),
                              result.IncludedPaths);
            writer.WriteComma();

            writer.WriteQueryResult(context, result, metadataOnly, out numberOfResults, partial: true);

            writer.WriteEndObject();
        }
Esempio n. 13
0
        public Task GetStats()
        {
            var etlStats = GetProcessesToReportOn().Select(x => new EtlTaskStats
            {
                TaskName = x.Key,
                Stats    = x.Value.Select(y => new EtlProcessTransformationStats
                {
                    TransformationName = y.TransformationName,
                    Statistics         = y.Statistics
                }).ToArray()
            }).ToArray();

            using (ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WriteArray(context, "Results", etlStats, (w, c, stats) =>
                    {
                        w.WriteObject(context.ReadObject(stats.ToJson(), "etl/stats"));
                    });
                    writer.WriteEndObject();
                }
            }

            return(Task.CompletedTask);
        }
        public Task State()
        {
            var state = Database.TombstoneCleaner.GetState();

            using (Database.DocumentsStorage.ContextPool.AllocateOperationContext(out JsonOperationContext context))
            {
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();

                    writer.WriteArray(context, "Results", state, (w, c, v) =>
                    {
                        w.WriteStartObject();

                        w.WritePropertyName("Collection");
                        w.WriteString(v.Key);
                        w.WriteComma();

                        w.WritePropertyName(nameof(v.Value.Component));
                        w.WriteString(v.Value.Component);
                        w.WriteComma();

                        w.WritePropertyName(nameof(v.Value.Etag));
                        w.WriteInteger(v.Value.Etag);

                        w.WriteEndObject();
                    });

                    writer.WriteEndObject();
                }
            }

            return(Task.CompletedTask);
        }
Esempio n. 15
0
        public async Task Put()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                var createdIndexes = new List <string>();
                var input          = await context.ReadForMemoryAsync(RequestBodyStream(), "Indexes");

                if (input.TryGet("Indexes", out BlittableJsonReaderArray indexes) == false)
                {
                    ThrowRequiredPropertyNameInRequest("Indexes");
                }

                foreach (var indexToAdd in indexes)
                {
                    var indexDefinition = JsonDeserializationServer.IndexDefinition((BlittableJsonReaderObject)indexToAdd);
                    indexDefinition.Name = indexDefinition.Name?.Trim();

                    if (LoggingSource.AuditLog.IsInfoEnabled)
                    {
                        var clientCert = GetCurrentCertificate();

                        var auditLog = LoggingSource.AuditLog.GetLogger(Database.Name, "Audit");
                        auditLog.Info($"Index {indexDefinition.Name} PUT by {clientCert?.Subject} {clientCert?.Thumbprint} with definition: {indexToAdd}");
                    }

                    if (indexDefinition.Maps == null || indexDefinition.Maps.Count == 0)
                    {
                        throw new ArgumentException("Index must have a 'Maps' fields");
                    }

                    if (indexDefinition.Name.StartsWith(Constants.Documents.Indexing.SideBySideIndexNamePrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException(
                                  $"Index name must not start with '{Constants.Documents.Indexing.SideBySideIndexNamePrefix}'. Provided index name: '{indexDefinition.Name}'");
                    }

                    var index = await Database.IndexStore.CreateIndex(indexDefinition);

                    createdIndexes.Add(index.Name);
                }

                HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();

                    writer.WriteArray(context, "Results", createdIndexes, (w, c, index) =>
                    {
                        w.WriteStartObject();
                        w.WritePropertyName(nameof(PutIndexResult.Index));
                        w.WriteString(index);
                        w.WriteEndObject();
                    });

                    writer.WriteEndObject();
                }
            }
        }
Esempio n. 16
0
        public Task GetAll()
        {
            var thumbprint = GetStringQueryString("thumbprint", required: false);

            var start    = GetStart();
            var pageSize = GetPageSize();

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (context.OpenReadTransaction())
                {
                    Tuple <string, BlittableJsonReaderObject>[] certificates = null;
                    try
                    {
                        if (string.IsNullOrEmpty(thumbprint))
                        {
                            certificates = ServerStore.Cluster.ItemsStartingWith(context, Constants.Certificates.Prefix, start, pageSize)
                                           .ToArray();
                        }
                        else
                        {
                            var key         = Constants.Certificates.Prefix + thumbprint;
                            var certificate = ServerStore.Cluster.Read(context, key);
                            if (certificate == null)
                            {
                                HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                                return(Task.CompletedTask);
                            }

                            certificates = new[]
                            {
                                Tuple.Create(key, certificate)
                            };
                        }

                        using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                        {
                            writer.WriteStartObject();
                            writer.WriteArray(context, "Results", certificates, (w, c, cert) =>
                            {
                                c.Write(w, cert.Item2);
                            });
                            writer.WriteEndObject();
                        }
                    }
                    finally
                    {
                        if (certificates != null)
                        {
                            foreach (var cert in certificates)
                            {
                                cert.Item2?.Dispose();
                            }
                        }
                    }
                }

            return(Task.CompletedTask);
        }
Esempio n. 17
0
        public Task GetIndexHistory()
        {
            var name = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name");

            List <IndexHistoryEntry> history;

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext ctx))
                using (ctx.OpenReadTransaction())
                    using (var rawRecord = ServerStore.Cluster.ReadRawDatabaseRecord(ctx, Database.Name))
                    {
                        var indexesHistory = rawRecord.IndexesHistory;
                        if (indexesHistory.TryGetValue(name, out history) == false)
                        {
                            HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                            return(Task.CompletedTask);
                        }
                    }

            using (ContextPool.AllocateOperationContext(out JsonOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName("Index");
                    writer.WriteString(name);
                    writer.WriteComma();

                    if (history == null || history.Count == 0)
                    {
                        writer.WriteStartArray();
                        writer.WriteEndArray();
                        writer.WriteEndObject();
                        return(Task.CompletedTask);
                    }

                    writer.WriteArray(context, "History", history, (w, c, entry) =>
                    {
                        w.WriteStartObject();

                        w.WritePropertyName(nameof(IndexHistoryEntry.Definition));
                        w.WriteIndexDefinition(c, entry.Definition);
                        w.WriteComma();

                        w.WritePropertyName(nameof(IndexHistoryEntry.CreatedAt));
                        w.WriteDateTime(entry.CreatedAt, isUtc: true);
                        w.WriteComma();

                        w.WritePropertyName(nameof(IndexHistoryEntry.Source));
                        w.WriteString(entry.Source);
                        w.WriteEndObject();
                    });

                    writer.WriteEndObject();
                }
            return(Task.CompletedTask);
        }
Esempio n. 18
0
 public Task GetStateChangeHistory()
 {
     using (ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context))
         using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
         {
             writer.WriteStartObject();
             writer.WriteArray("States", ServerStore.Engine.PrevStates.Select(s => s.ToString()));
             writer.WriteEndObject();
         }
     return(Task.CompletedTask);
 }
Esempio n. 19
0
 public Task GetHistoryLogs()
 {
     using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
         using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
         {
             context.OpenReadTransaction();
             writer.WriteArray("RachisLogHistory", ServerStore.Engine.LogHistory.GetHistoryLogs(context), context);
             writer.Flush();
         }
     return(Task.CompletedTask);
 }
Esempio n. 20
0
        public static void WriteSuggestionResult(this BlittableJsonTextWriter writer, JsonOperationContext context, SuggestionResult result)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(result.Name));
            writer.WriteString(result.Name);
            writer.WriteComma();

            writer.WriteArray(nameof(result.Suggestions), result.Suggestions);

            writer.WriteEndObject();
        }
Esempio n. 21
0
        public Task GetAll()
        {
            var name = GetStringQueryString("name", required: false);

            var start     = GetStart();
            var pageSize  = GetPageSize();
            var namesOnly = GetBoolValueQueryString("namesOnly", required: false) ?? false;

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    IndexDefinition[] indexDefinitions;
                    if (string.IsNullOrEmpty(name))
                    {
                        indexDefinitions = Database.IndexStore
                                           .GetIndexes()
                                           .OrderBy(x => x.Name)
                                           .Skip(start)
                                           .Take(pageSize)
                                           .Select(x => x.GetIndexDefinition())
                                           .ToArray();
                    }
                    else
                    {
                        var index = Database.IndexStore.GetIndex(name);
                        if (index == null)
                        {
                            HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                            return(Task.CompletedTask);
                        }

                        indexDefinitions = new[] { index.GetIndexDefinition() };
                    }

                    writer.WriteStartObject();

                    writer.WriteArray(context, "Results", indexDefinitions, (w, c, indexDefinition) =>
                    {
                        if (namesOnly)
                        {
                            w.WriteString(indexDefinition.Name);
                            return;
                        }

                        w.WriteIndexDefinition(c, indexDefinition);
                    });

                    writer.WriteEndObject();
                }

            return(Task.CompletedTask);
        }
Esempio n. 22
0
        public Task GetIps()
        {
            AssertOnlyInSetupMode();

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName("MachineName");
                    writer.WriteString(Environment.MachineName);
                    writer.WriteComma();
                    writer.WritePropertyName("NetworkInterfaces");
                    writer.WriteStartArray();
                    var first = true;
                    foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
                    {
                        var ips = netInterface.GetIPProperties().UnicastAddresses
                                  .Where(x =>
                        {
                            // filter 169.254.xxx.xxx out, they are not meaningful for binding
                            if (x.Address.AddressFamily != AddressFamily.InterNetwork)
                            {
                                return(false);
                            }
                            var addressBytes = x.Address.GetAddressBytes();
                            return(addressBytes[0] != 169 || addressBytes[1] != 254);
                        })
                                  .Select(addr => addr.Address.ToString())
                                  .ToList();


                        if (first == false)
                        {
                            writer.WriteComma();
                        }
                        first = false;

                        writer.WriteStartObject();
                        writer.WritePropertyName("Name");
                        writer.WriteString(netInterface.Name);
                        writer.WriteComma();
                        writer.WritePropertyName("Description");
                        writer.WriteString(netInterface.Description);
                        writer.WriteComma();
                        writer.WriteArray("Addresses", ips);
                        writer.WriteEndObject();
                    }
                    writer.WriteEndArray();
                    writer.WriteEndObject();
                }
            return(Task.CompletedTask);
        }
Esempio n. 23
0
        public async Task PostIndexFields()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                using (var json = await context.ReadForMemoryAsync(RequestBodyStream(), "map"))
                {
                    if (json.TryGet("Map", out string map) == false)
                    {
                        throw new ArgumentException("'Map' field is mandatory, but wasn't specified");
                    }

                    json.TryGet(nameof(IndexDefinition.AdditionalSources), out BlittableJsonReaderObject additionalSourcesJson);
                    json.TryGet(nameof(IndexDefinition.AdditionalAssemblies), out BlittableJsonReaderArray additionalAssembliesArray);

                    var indexDefinition = new IndexDefinition
                    {
                        Name = "index-fields",
                        Maps =
                        {
                            map
                        },
                        AdditionalSources    = ConvertToAdditionalSources(additionalSourcesJson),
                        AdditionalAssemblies = ConvertToAdditionalAssemblies(additionalAssembliesArray)
                    };

                    try
                    {
                        var compiledIndex = IndexCompilationCache.GetIndexInstance(indexDefinition, Database.Configuration);

                        var outputFields = compiledIndex.OutputFields;

                        using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                        {
                            writer.WriteStartObject();
                            writer.WriteArray(context, "Results", outputFields, (w, c, field) => { w.WriteString(field); });
                            writer.WriteEndObject();
                        }
                    }
                    catch (IndexCompilationException)
                    {
                        // swallow compilation exception and return empty array as response
                        using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                        {
                            writer.WriteStartArray();
                            writer.WriteEndArray();
                        }
                    }
                }
            }
        }
Esempio n. 24
0
        private async Task Send(string command, string value, string[] values)
        {
            var taskCompletionSource = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
            int currentCommandId;
            await _semaphore.WaitAsync(_cts.Token).ConfigureAwait(false);

            try
            {
                currentCommandId = ++_commandId;
                using (_requestExecutor.ContextPool.AllocateOperationContext(out var context))
                    using (var writer = new BlittableJsonTextWriter(context, _ms))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName("CommandId");
                        writer.WriteInteger(currentCommandId);

                        writer.WriteComma();
                        writer.WritePropertyName("Command");
                        writer.WriteString(command);
                        writer.WriteComma();

                        writer.WritePropertyName("Param");
                        writer.WriteString(value);

                        if (values != null && values.Length > 0)
                        {
                            writer.WriteComma();
                            writer.WriteArray("Params", values);
                        }

                        writer.WriteEndObject();
                    }

                _ms.TryGetBuffer(out var buffer);

                _confirmations.TryAdd(currentCommandId, taskCompletionSource);

                await _client.SendAsync(buffer, WebSocketMessageType.Text, endOfMessage : true, cancellationToken : _cts.Token).ConfigureAwait(false);
            }
            finally
            {
                _ms.SetLength(0);
                _semaphore.Release();
            }

            if (await taskCompletionSource.Task.WaitWithTimeout(TimeSpan.FromSeconds(15)).ConfigureAwait(false) == false)
            {
                throw new TimeoutException("Did not get a confirmation for command #" + currentCommandId);
            }
        }
Esempio n. 25
0
        public Task Databases()
        {
            // if Studio requested information about single resource - handle it
            var dbName = GetStringQueryString("name", false);

            if (dbName != null)
            {
                return(DbInfo(dbName));
            }

            var namesOnly = GetBoolValueQueryString("namesOnly", required: false) ?? false;


            //TODO: fill all required information (see: RavenDB-5438) - return Raven.Client.Data.DatabasesInfo
            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
            {
                context.OpenReadTransaction();
                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();

                    var items = ServerStore.Cluster.ItemsStartingWith(context, Constants.Documents.Prefix, GetStart(), GetPageSize());

                    if (TryGetAllowedDbs(null, out var allowedDbs, requireAdmin: false) == false)
                    {
                        return(Task.CompletedTask);
                    }

                    if (allowedDbs != null)
                    {
                        items = items.Where(item => allowedDbs.ContainsKey(item.Item1.Substring(Constants.Documents.Prefix.Length)));
                    }

                    writer.WriteArray(context, nameof(DatabasesInfo.Databases), items, (w, c, dbDoc) =>
                    {
                        var databaseName = dbDoc.Item1.Substring(Constants.Documents.Prefix.Length);
                        if (namesOnly)
                        {
                            w.WriteString(databaseName);
                            return;
                        }

                        WriteDatabaseInfo(databaseName, dbDoc.Item2, context, w);
                    });
                    writer.WriteEndObject();
                }
            }

            return(Task.CompletedTask);
        }
Esempio n. 26
0
        public async Task Put()
        {
            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
            {
                var createdIndexes = new List <KeyValuePair <string, long> >();
                var input          = await context.ReadForMemoryAsync(RequestBodyStream(), "Indexes");

                if (input.TryGet("Indexes", out BlittableJsonReaderArray indexes) == false)
                {
                    ThrowRequiredPropertyNameInRequest("Indexes");
                }

                foreach (var indexToAdd in indexes)
                {
                    var indexDefinition = JsonDeserializationServer.IndexDefinition((BlittableJsonReaderObject)indexToAdd);

                    if (indexDefinition.Maps == null || indexDefinition.Maps.Count == 0)
                    {
                        throw new ArgumentException("Index must have a 'Maps' fields");
                    }
                    var etag = await Database.IndexStore.CreateIndex(indexDefinition);

                    createdIndexes.Add(new KeyValuePair <string, long>(indexDefinition.Name, etag));
                }

                HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;

                using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteStartObject();

                    writer.WriteArray(context, "Results", createdIndexes, (w, c, index) =>
                    {
                        w.WriteStartObject();
                        w.WritePropertyName(nameof(PutIndexResult.IndexId));
                        w.WriteInteger(index.Value);

                        w.WriteComma();

                        w.WritePropertyName(nameof(PutIndexResult.Index));
                        w.WriteString(index.Key);
                        w.WriteEndObject();
                    });

                    writer.WriteEndObject();
                }
            }
        }
        public static void WriteTermsQueryResult(this BlittableJsonTextWriter writer, JsonOperationContext context, TermsQueryResultServerSide queryResult)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(queryResult.IndexName));
            writer.WriteString(queryResult.IndexName);
            writer.WriteComma();

            writer.WritePropertyName(nameof(queryResult.ResultEtag));
            writer.WriteInteger(queryResult.ResultEtag);
            writer.WriteComma();

            writer.WriteArray(nameof(queryResult.Terms), queryResult.Terms);

            writer.WriteEndObject();
        }
Esempio n. 28
0
        private async Task Explain(DocumentsOperationContext context, HttpMethod method)
        {
            var indexQuery = await GetIndexQuery(context, method);

            var explanations = Database.QueryRunner.ExplainDynamicIndexSelection(indexQuery);

            using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
            {
                writer.WriteStartObject();
                writer.WriteArray(context, "Results", explanations, (w, c, explanation) =>
                {
                    w.WriteExplanation(context, explanation);
                });
                writer.WriteEndObject();
            }
        }
Esempio n. 29
0
        public static void WritePerformanceStats(this BlittableJsonTextWriter writer, JsonOperationContext context, IEnumerable <IndexPerformanceStats> stats)
        {
            writer.WriteStartObject();
            writer.WriteArray(context, "Results", stats, (w, c, stat) =>
            {
                w.WriteStartObject();

                w.WritePropertyName(nameof(stat.Name));
                w.WriteString(stat.Name);
                w.WriteComma();

                w.WriteArray(c, nameof(stat.Performance), stat.Performance, (wp, cp, performance) => { wp.WriteIndexingPerformanceStats(context, performance); });

                w.WriteEndObject();
            });
            writer.WriteEndObject();
        }
Esempio n. 30
0
        public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url)
        {
            var request = new HttpRequestMessage
            {
                Method  = HttpMethod.Post,
                Content = new BlittableJsonContent(stream =>
                {
                    using (var writer = new BlittableJsonTextWriter(ctx, stream))
                    {
                        writer.WriteStartObject();
                        writer.WriteArray("Commands", _commands);
                        if (_mode == TransactionMode.ClusterWide)
                        {
                            writer.WriteComma();
                            writer.WritePropertyName(nameof(TransactionMode));
                            writer.WriteString(nameof(TransactionMode.ClusterWide));
                        }
                        writer.WriteEndObject();
                    }
                })
            };

            if (_attachmentStreams != null && _attachmentStreams.Count > 0)
            {
                var multipartContent = new MultipartContent {
                    request.Content
                };
                foreach (var stream in _attachmentStreams)
                {
                    PutAttachmentCommandHelper.PrepareStream(stream);
                    var streamContent = new AttachmentStreamContent(stream, CancellationToken);
                    streamContent.Headers.TryAddWithoutValidation("Command-Type", "AttachmentStream");
                    multipartContent.Add(streamContent);
                }
                request.Content = multipartContent;
            }

            var sb = new StringBuilder($"{node.Url}/databases/{node.Database}/bulk_docs");

            AppendOptions(sb);

            url = sb.ToString();

            return(request);
        }