public async Task CreateTopicAsync(string topic, int?numPartitions, short?replicationFactor,
                                       Dictionary <string, string> config, TimeSpan timeout)
    {
        var topicSpecification = new TopicSpecification
        {
            Name              = topic,
            NumPartitions     = numPartitions ?? -1,
            ReplicationFactor = replicationFactor ?? -1,
            Configs           = config
        };

        var options = new CreateTopicsOptions
        {
            RequestTimeout = timeout
        };

        try
        {
            await _adminClient.CreateTopicsAsync(new[] { topicSpecification }, options);
        }
        catch (KafkaException e)
        {
            throw new AdminClientException("Unable to create topic.", e);
        }
    }
        async Task CreateTopic()
        {
            var client = new AdminClientBuilder(_config).Build();

            try
            {
                var options = new CreateTopicsOptions {
                    RequestTimeout = _options.RequestTimeout
                };
                LogContext.Debug?.Log("Creating topic: {Topic}", _specification.Name);
                await client.CreateTopicsAsync(new[] { _specification }, options).ConfigureAwait(false);
            }
            catch (CreateTopicsException e)
            {
                if (!e.Results.All(x => x.Error.Reason.EndsWith("already exists.", StringComparison.OrdinalIgnoreCase)))
                {
                    EnabledLogger?logger = e.Error.IsFatal ? LogContext.Error : LogContext.Debug;
                    logger?.Log("An error occured creating topics. {Errors}", string.Join(", ", e.Results.Select(x => $"{x.Topic}:{x.Error.Reason}")));
                }
            }
            finally
            {
                client.Dispose();
            }
        }
        /// <summary>
        /// Creates the required topics for end to end tests
        /// </summary>
        /// <returns>The async.</returns>
        public async Task InitializeAsync()
        {
            var adminClientBuilder = new AdminClientBuilder(new AdminClientConfig()
            {
                BootstrapServers = this.Broker,
            });

            var adminClient = adminClientBuilder.Build();

            try
            {
                var createTopicOptions = new CreateTopicsOptions()
                {
                    OperationTimeout = TimeSpan.FromMinutes(2),
                    RequestTimeout   = TimeSpan.FromMinutes(2),
                };

                await adminClient.CreateTopicsAsync(GetAllTopics(), createTopicOptions);
            }
            catch (CreateTopicsException createTopicsException)
            {
                if (!createTopicsException.Results.All(x => x.Error.Code == ErrorCode.TopicAlreadyExists))
                {
                    Console.WriteLine($"Error creation topics: {createTopicsException.ToString()}");
                    throw;
                }
            }
            catch (KafkaException ex)
            {
                if (!ex.Error.Reason.Equals("No topics to create", StringComparison.InvariantCultureIgnoreCase))
                {
                    throw;
                }
            }
        }
Пример #4
0
 public Task CreateTopicsAsync(IEnumerable <TopicSpecification> topics, CreateTopicsOptions options = null)
 {
     return(Task.Run(() =>
     {
         foreach (var t in topics)
         {
             producer.CreateTopic(t.Name);
         }
     }));
 }
 public Task CreateTopicsAsync(IEnumerable <TopicSpecification> topics, CreateTopicsOptions options = null)
 {
     return(Task.Run(() =>
     {
         foreach (var specs in topics)
         {
             if (specs.NumPartitions > 0)
             {
                 cluster.CreateTopic(specs.Name, specs.NumPartitions);
             }
             else
             {
                 cluster.CreateTopic(specs.Name);
             }
         }
     }));
 }
Пример #6
0
        public async void LocalTimeout()
        {
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = "localhost:666"
            }).Build())
            {
                var testTopics = new List <TopicSpecification>
                {
                    new TopicSpecification
                    {
                        Name = "my-topic",
                        ReplicationFactor = 1,
                        NumPartitions     = 1
                    },
                    new TopicSpecification
                    {
                        Name = "my-topic-2",
                        ReplicationFactor = 1,
                        NumPartitions     = 1
                    }
                };

                var options = new CreateTopicsOptions
                {
                    RequestTimeout = TimeSpan.FromMilliseconds(200)
                };

                foreach (var topic in testTopics)
                {
                    // Correct input, fail with timeout
                    var ex = await Assert.ThrowsAsync <KafkaException>(() =>
                                                                       adminClient.CreateTopicsAsync(
                                                                           new List <TopicSpecification> {
                        topic
                    },
                                                                           options)
                                                                       );

                    Assert.Equal("Failed while waiting for controller: Local: Timed out", ex.Message);
                }
            }
        }
        async Task CreateTopic()
        {
            var client = new AdminClientBuilder(_config).Build();

            try
            {
                var options = new CreateTopicsOptions {
                    RequestTimeout = TimeSpan.FromSeconds(30)
                };
                LogContext.Debug?.Log("Creating topic: {Topic}", _specification.Name);
                await client.CreateTopicsAsync(new[] { _specification }, options).ConfigureAwait(false);
            }
            catch (CreateTopicsException e)
            {
                EnabledLogger?logger = e.Error.IsFatal ? LogContext.Critical : LogContext.Error;
                logger?.Log("An error occured creating topics. {Errors}", string.Join(", ", e.Results.Select(x => $"{x.Topic}:{x.Error.Reason}")));
            }
            finally
            {
                client.Dispose();
            }
        }
 public Task CreateTopicsAsync(IEnumerable <TopicSpecification> topics, CreateTopicsOptions options = null)
 {
     throw new NotImplementedException();
 }