示例#1
0
        public async Task Stale()
        {
            var name = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name");

            var index = Database.IndexStore.GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
            }

            using (var context = QueryOperationContext.Allocate(Database, index))
                await using (var writer = new AsyncBlittableJsonTextWriter(context.Documents, ResponseBodyStream()))
                    using (context.OpenReadTransaction())
                    {
                        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();
                    }
        }
示例#2
0
        public Task ClearErrors()
        {
            var names = GetStringValuesQueryString("name", required: false);

            var indexes = new List <Index>();

            if (names.Count == 0)
            {
                indexes.AddRange(Database.IndexStore.GetIndexes());
            }
            else
            {
                foreach (var name in names)
                {
                    var index = Database.IndexStore.GetIndex(name);
                    if (index == null)
                    {
                        IndexDoesNotExistException.ThrowFor(name);
                    }

                    indexes.Add(index);
                }
            }

            foreach (var index in indexes)
            {
                index.DeleteErrors();
            }

            return(NoContent());
        }
        public Task KillQuery()
        {
            var name = GetQueryStringValueAndAssertIfSingleAndNotEmpty("indexName");
            var id   = GetLongQueryString("id");

            var index = Database.IndexStore.GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
            }

            var query = index.CurrentlyRunningQueries
                        .FirstOrDefault(q => q.QueryId == id);

            if (query == null)
            {
                HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(Task.CompletedTask);
            }

            query.Token.Cancel();

            return(NoContent());
        }
示例#4
0
        public override async Task <IndexEntriesQueryResult> ExecuteIndexEntriesQuery(IndexQueryServerSide query, QueryOperationContext queryContext, long?existingResultEtag, OperationCancelToken token)
        {
            var index = await MatchIndex(query, false, null, token.Token);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(query.Metadata.CollectionName);
            }

            queryContext.WithIndex(index);

            if (existingResultEtag.HasValue)
            {
                var etag = index.GetIndexEtag(queryContext, query.Metadata);
                if (etag == existingResultEtag)
                {
                    return(IndexEntriesQueryResult.NotModifiedResult);
                }
            }

            using (QueryRunner.MarkQueryAsRunning(index.Name, query, token))
            {
                return(await index.IndexEntries(query, queryContext, token));
            }
        }
示例#5
0
        public Task OpenFaultyIndex()
        {
            var name  = GetQueryStringValueAndAssertIfSingleAndNotEmpty("name");
            var index = Database.IndexStore.GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
            }

            if (index is FaultyInMemoryIndex == false)
            {
                throw new InvalidOperationException($"Cannot open non faulty index named: {name}");
            }

            lock (index)
            {
                var localIndex = Database.IndexStore.GetIndex(name);
                if (localIndex == null)
                {
                    IndexDoesNotExistException.ThrowFor(name);
                }

                if (localIndex is FaultyInMemoryIndex == false)
                {
                    throw new InvalidOperationException($"Cannot open non faulty index named: {name}");
                }

                Database.IndexStore.OpenFaultyIndex(localIndex);
            }

            return(NoContent());
        }
示例#6
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);
        }
示例#7
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);
        }
示例#8
0
        public async Task SetPriority(string name, IndexPriority priority)
        {
            await _indexLocker.WaitAsync();

            try
            {
                var index = GetIndex(name);
                if (index == null)
                {
                    IndexDoesNotExistException.ThrowFor(name);
                }

                var faultyInMemoryIndex = index as FaultyInMemoryIndex;
                if (faultyInMemoryIndex != null)
                {
                    faultyInMemoryIndex.SetPriority(priority); // this will throw proper exception
                    return;
                }

                var command = new SetIndexPriorityCommand(name, priority, _documentDatabase.Name);

                var(etag, _) = await _serverStore.SendToLeaderAsync(command);

                await _documentDatabase.RachisLogIndexNotifications.WaitForIndexNotification(etag);
            }
            finally
            {
                _indexLocker.Release();
            }
        }
示例#9
0
        public async Task Dump()
        {
            var name  = GetStringQueryString("name");
            var path  = GetStringQueryString("path");
            var index = Database.IndexStore.GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
                return; //never hit
            }

            var operationId = Database.Operations.GetNextOperationId();
            var token       = CreateTimeLimitedQueryOperationToken();

            _ = Database.Operations.AddOperation(
                Database,
                "Dump index " + name + " to " + path,
                Operations.Operations.OperationType.DumpRawIndexData,
                onProgress =>
            {
                var totalFiles = index.Dump(path, onProgress);
                return(Task.FromResult((IOperationResult) new DumpIndexResult
                {
                    Message = $"Dumped {totalFiles} files from {name}",
                }));
            }, operationId, token: token);

            using (ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                await using (var writer = new AsyncBlittableJsonTextWriter(context, ResponseBodyStream()))
                {
                    writer.WriteOperationIdAndNodeTag(context, operationId, ServerStore.NodeTag);
                }
        }
示例#10
0
        public void EnsureIsAcceptable(string indexName, StreamResult result)
        {
            if (result != null)
                return;

            if (indexName != null)
                IndexDoesNotExistException.ThrowFor(indexName);
        }
示例#11
0
        public Index GetIndex(string indexName)
        {
            var index = Database.IndexStore.GetIndex(indexName);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(indexName);
            }

            return(index);
        }
示例#12
0
        public Index ResetIndex(string name)
        {
            var index = GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
            }

            return(ResetIndexInternal(index));
        }
示例#13
0
        public void StartIndex(string name)
        {
            var index = GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
            }

            index.Start();
        }
示例#14
0
        public Task Disable()
        {
            var name  = GetStringQueryString("name");
            var index = Database.IndexStore.GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
            }

            index.Disable();

            return(NoContent());
        }
示例#15
0
        public Index GetIndex(string indexName, bool throwIfNotExists = true)
        {
            var index = Database.IndexStore.GetIndex(indexName);

            if (index == null && throwIfNotExists)
            {
                IndexDoesNotExistException.ThrowFor(indexName);
            }

            if (index?.IsPending == true)
            {
                throw new PendingRollingIndexException($"Cannot use index `{indexName}` on node {Database.ServerStore.NodeTag} because a rolling index deployment is still pending on this node.");
            }

            return(index);
        }
示例#16
0
        public void StopIndex(string name)
        {
            var index = GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
            }

            index.Stop();

            _documentDatabase.Changes.RaiseNotifications(new IndexChange
            {
                Name = name,
                Type = IndexChangeTypes.IndexPaused
            });
        }
示例#17
0
        public override async Task <IndexEntriesQueryResult> ExecuteIndexEntriesQuery(IndexQueryServerSide query, DocumentsOperationContext context, long?existingResultEtag, OperationCancelToken token)
        {
            var index = await MatchIndex(query, false, null, context, token.Token);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(query.Metadata.CollectionName);
            }

            if (existingResultEtag.HasValue)
            {
                var etag = index.GetIndexEtag();
                if (etag == existingResultEtag)
                {
                    return(IndexEntriesQueryResult.NotModifiedResult);
                }
            }

            return(index.IndexEntries(query, context, token));
        }
示例#18
0
        public Task Progress()
        {
            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 progress = index.GetProgress(context);
                        writer.WriteIndexProgress(context, progress);
                    }

            return(Task.CompletedTask);
        }
示例#19
0
        public async Task DeleteIndex(string name)
        {
            await _indexLocker.WaitAsync();

            try
            {
                var index = GetIndex(name);
                if (index == null)
                {
                    IndexDoesNotExistException.ThrowFor(name);
                }

                var(newEtag, _) = await _serverStore.SendToLeaderAsync(new DeleteIndexCommand(index.Name, _documentDatabase.Name));

                await _documentDatabase.RachisLogIndexNotifications.WaitForIndexNotification(newEtag);
            }
            finally
            {
                _indexLocker.Release();
            }
        }
示例#20
0
        protected async Task WaitForIndexAsync(DocumentStore store, string databaseName, string indexName, TimeSpan?timeout = null)
        {
            if (timeout == null)
            {
                timeout = TimeSpan.FromMinutes(10);
            }

            var admin = store.Maintenance.ForDatabase(databaseName);

            var sp = Stopwatch.StartNew();

            while (sp.Elapsed < timeout.Value)
            {
                var indexStats = await admin.SendAsync(new GetIndexStatisticsOperation(indexName));

                if (indexStats == null)
                {
                    IndexDoesNotExistException.ThrowFor(indexName);
                }

                if (indexStats.Status != IndexRunningStatus.Running)
                {
                    throw new InvalidOperationException($"Index '{indexName}' is not running!");
                }

                if (indexStats.State != IndexState.Idle && indexStats.State != IndexState.Normal)
                {
                    throw new InvalidOperationException($"Index '{indexName}' state ({indexStats.State}) is invalid!");
                }

                if (indexStats.IsStale == false)
                {
                    return;
                }

                await Task.Delay(32);
            }

            throw new TimeoutException($"The index '{indexName}' stayed stale for more than {timeout.Value}.");
        }
示例#21
0
        public async Task <IndexEntriesQueryResult> ExecuteIndexEntries(IndexQueryServerSide query, long?existingResultEtag)
        {
            var tuple = await MatchIndex(query, false);

            var index = tuple.Index;

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(query.Metadata.CollectionName);
            }

            if (existingResultEtag.HasValue)
            {
                var etag = index.GetIndexEtag();
                if (etag == existingResultEtag)
                {
                    return(IndexEntriesQueryResult.NotModifiedResult);
                }
            }

            return(index.IndexEntries(query, _context, _token));
        }
示例#22
0
        public async Task Disable()
        {
            var raftRequestId = GetRaftRequestIdFromQuery();
            var name          = GetStringQueryString("name");
            var clusterWide   = GetBoolValueQueryString("clusterWide", false) ?? false;
            var index         = Database.IndexStore.GetIndex(name);

            if (index == null)
            {
                IndexDoesNotExistException.ThrowFor(name);
            }

            if (clusterWide)
            {
                await Database.IndexStore.SetState(name, IndexState.Disabled, $"{raftRequestId}/{index}");
            }
            else
            {
                index.Disable();
            }

            NoContentStatus();
        }