private async Task EnsureIndexExists(string indexName, ElasticClient esClient)
        {
            ExistsResponse existsResult = await esClient.Indices.ExistsAsync(indexName).ConfigureAwait(false);

            if (!existsResult.IsValid)
            {
                this.ReportEsRequestError(existsResult, "Index exists check");
            }

            if (existsResult.Exists)
            {
                return;
            }

            CreateIndexRequestBuilder createIndexRequestBuilder = new CreateIndexRequestBuilder(this.connectionData.Configuration, this.healthReporter);


            CreateIndexResponse createIndexResult = await esClient.Indices.CreateAsync(indexName, createIndexRequestBuilder.BuildRequest).ConfigureAwait(false);

            if (!createIndexResult.IsValid)
            {
                try
                {
                    if (createIndexResult.ServerError?.Error?.Type != null &&
                        Regex.IsMatch(createIndexResult.ServerError.Error.Type, "index.*already.*exists.*exception", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(500)))
                    {
                        // This is fine, someone just beat us to create a new index.
                        return;
                    }
                }
                catch (RegexMatchTimeoutException) { }

                this.ReportEsRequestError(createIndexResult, "Create index");
            }
        }
Example #2
0
        public async Task testNonExistingOpCode()
        {
            ZooKeeper zk = await createClient();

            const string path = "/m1";

            RequestHeader h = new RequestHeader();

            h.set_Type(888); // This code does not exists
            ExistsRequest request = new ExistsRequest();

            request.setPath(path);
            request.setWatch(false);
            ExistsResponse response = new ExistsResponse();
            ReplyHeader    r        = await zk.cnxn.submitRequest(h, request, response, null);

            Assert.assertEquals(r.getErr(), (int)KeeperException.Code.UNIMPLEMENTED);

            try {
                await zk.existsAsync("/m1", false);

                Assert.fail("The connection should have been closed");
            }
            catch (KeeperException.ConnectionLossException) {
            }
        }
        public async Task <IActionResult> Post()
        {
            try
            {
                ExistsResponse indexExistsResponse = await _elasticClient.Indices.ExistsAsync(_appConfig.Elasticsearch.IndexName);

                if (indexExistsResponse.IsValid && indexExistsResponse.Exists)
                {
                    throw new Exception("Index of name " + _appConfig.Elasticsearch.IndexName + " already exists");
                }

                CreateIndexResponse response = await CreateIndex();

                if (!response.IsValid)
                {
                    _logger.LogError("Error creating index: " + response.DebugInformation);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError(1, ex, "Unable to create Employees index; index already exists");
                return(BadRequest());
            }
        }
        public async Task <IActionResult> Delete()
        {
            try
            {
                ExistsResponse indexExistsReponse = await _elasticClient.Indices.ExistsAsync(_appConfig.Elasticsearch.IndexName);

                if (indexExistsReponse.IsValid && indexExistsReponse.Exists)
                {
                    DeleteIndexResponse deleteIndexResult = await _elasticClient.Indices.DeleteAsync(_appConfig.Elasticsearch.IndexName);

                    if (!deleteIndexResult.IsValid)
                    {
                        _logger.LogError("Unable to delete employee index");
                        return(StatusCode(StatusCodes.Status500InternalServerError));
                    }
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError(1, ex, "Unable to delete Employees index; does not exists");
                return(BadRequest());
            }
        }
        public async Task ShouldDetectExistentIndex()
        {
            //Act
            ExistsResponse exists = null;

            await _clFx.UseTmpIndex(async indNm =>
            {
                exists = await _clFx.EsClient.Indices.ExistsAsync(indNm);
            });

            //Assert
            Assert.NotNull(exists);
            Assert.True(exists.Exists);
        }
Example #6
0
        public bool IndexExists(IndexName index, [CallerMemberName] string callerName = "")
        {
            var            timer  = Stopwatch.StartNew();
            ExistsResponse result = null;

            try
            {
                result = _client.Indices.Exists(index);
            }
            catch (Exception e)
            {
                _logger.Debug($"IndexExists: {e.Message}");
            }

            SendLog(result.ApiCall, null, timer.ElapsedMilliseconds, $"Index Exists {index.Name}");

            return(result.Exists);
        }
Example #7
0
        /// <summary>
        /// 检测索引是否已经存在
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public async Task <bool> IsIndexExsit(string index)
        {
            bool           flag   = false;
            ExistsResponse resStr = null;

            try
            {
                resStr = await ElasticLinqClient.Indices.ExistsAsync(index);

                if (resStr.Exists)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
            }

            return(flag);
        }
        public async Task <IActionResult> Get(string indexName)
        {
            try
            {
                ExistsResponse indexExistsResponse = await _elasticClient.Indices.ExistsAsync(_appConfig.Elasticsearch.IndexName);

                if (indexExistsResponse.IsValid && indexExistsResponse.Exists)
                {
                    return(Ok(indexName));
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(1, ex, "Unable to determine if index " + indexName + "exists");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
        public async Task <IActionResult> AnonymousPost()
        {
            try
            {
                _logger.LogInformation("Uri: " + _appConfig.Elasticsearch.Uri);
                _logger.LogInformation("IndexName: " + _appConfig.Elasticsearch.IndexName);
                _logger.LogInformation("Username: "******"Password: "******"Index DOES NOT exist.  Creating index...");

                    CreateIndexResponse response = await CreateIndex();

                    if (!response.IsValid)
                    {
                        _logger.LogError("Error creating index: " + response.DebugInformation);
                        return(StatusCode(StatusCodes.Status500InternalServerError));
                    }
                }
                else
                {
                    if (indexExistsResponse.Exists)
                    {
                        _logger.LogInformation("Index already exists.  Skipping index creation...");
                    }
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                _logger.LogError(1, ex, "Unable to create Employees index");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Example #10
0
        public async Task testNonExistingOpCode()
        {
            DisconnectedWatcher disconnectedWatcher = new DisconnectedWatcher();
            ZooKeeper           zk = await createClient(disconnectedWatcher);

            const string path = "/m1";

            RequestHeader h = new RequestHeader();

            h.set_Type(888); // This code does not exists
            ExistsRequest request = new ExistsRequest();

            request.setPath(path);
            request.setWatch(false);
            ExistsResponse response = new ExistsResponse();

            ReplyHeader r = await zk.cnxn.submitRequest(h, request, response, null);

            Assert.assertEquals(r.getErr(), (int)KeeperException.Code.UNIMPLEMENTED);

            // Sending a nonexisting opcode should cause the server to disconnect
            Assert.assertTrue("failed to disconnect",
                              await disconnectedWatcher.getTask().WithTimeout(5000));
        }
 protected override void ExpectResponse(ExistsResponse response)
 {
     response.Should().NotBeNull();
     response.Exists.Should().BeTrue();
 }
 protected override void ExpectResponse(ExistsResponse response)
 {
 }