Example #1
0
        private async Task CreateIndexLoop()
        {
            while (true)
            {
                //TODO FIX THIS LOGIC
                if (_nodeStateService.Role == NodeState.Leader)
                {
                    string typeToCreate;
                    bool   isSuccessful;
                    do
                    {
                        isSuccessful = IndexCreationQueue.TryDequeue(out typeToCreate);
                        if (isSuccessful)
                        {
                            if (!_stateMachine.IndexExists(typeToCreate))
                            {
                                _logger.LogInformation(_nodeStateService.GetNodeLogId() + "Creating index for type " + typeToCreate);
                                var result = Allocator.CreateIndexAsync(typeToCreate, _clusterOptions.DataTransferTimeoutMs, _clusterOptions.NumberOfShards).GetAwaiter().GetResult();

                                DateTime startTime = DateTime.Now;
                                while (!_stateMachine.IndexExists(typeToCreate))
                                {
                                    if ((DateTime.Now - startTime).TotalMilliseconds < _clusterOptions.DataTransferTimeoutMs)
                                    {
                                        throw new Exception("Failed to create index " + typeToCreate + ", timed out index detection.");
                                    }
                                    _logger.LogDebug(_nodeStateService.GetNodeLogId() + "Awaiting index creation.");
                                    await Task.Delay(100);
                                }
                            }
                            else
                            {
                                _logger.LogDebug(_nodeStateService.GetNodeLogId() + "INDEX for type " + typeToCreate + " Already exists, skipping creation...");
                            }
                        }
                    }while (isSuccessful);
                    await Task.Delay(1000);
                }
                else
                {
                    await Task.Delay(1000);
                }
            }
        }