Example #1
0
        private static async Task CreateTopics(string input, string output, int numberPartitions)
        {
            AdminClientConfig config = new AdminClientConfig();

            config.BootstrapServers = "localhost:9092";

            AdminClientBuilder builder = new AdminClientBuilder(config);
            var client = builder.Build();

            try
            {
                await client.CreateTopicsAsync(new List <TopicSpecification>
                {
                    new TopicSpecification()
                    {
                        Name = input, NumPartitions = numberPartitions
                    },
                    new TopicSpecification()
                    {
                        Name = output, NumPartitions = numberPartitions
                    },
                });
            }
            catch (Exception e)
            {
                // do nothing in case of topic already exist
            }
            finally
            {
                client.Dispose();
            }
        }
Example #2
0
 public static async Task CreateTopic(ClientConfig config, string name, int numPartitions, short replicationFactor)
 {
     using (var adminClient = new AdminClientBuilder(config).Build())
     {
         try
         {
             await adminClient.CreateTopicsAsync(new List <TopicSpecification> {
                 new TopicSpecification {
                     Name = name, NumPartitions = numPartitions, ReplicationFactor = replicationFactor
                 }
             }, new CreateTopicsOptions {});
         }
         catch (CreateTopicsException e)
         {
             if (e.Results[0].Error.Code != ErrorCode.TopicAlreadyExists)
             {
                 Console.WriteLine($"An error occured creating topic {name}: {e.Results[0].Error.Reason}");
             }
             else
             {
                 Console.WriteLine("Topic already exists");
             }
         }
     }
 }
Example #3
0
    protected virtual async Task CreateTopicAsync()
    {
        using (var adminClient = new AdminClientBuilder(Options.Connections.GetOrDefault(ConnectionName)).Build())
        {
            var topic = new TopicSpecification
            {
                Name              = TopicName,
                NumPartitions     = 1,
                ReplicationFactor = 1
            };

            Options.ConfigureTopic?.Invoke(topic);

            try
            {
                await adminClient.CreateTopicsAsync(new[] { topic });
            }
            catch (CreateTopicsException e)
            {
                if (e.Results.Any(x => x.Error.Code != ErrorCode.TopicAlreadyExists))
                {
                    throw;
                }
            }
        }
    }
Example #4
0
        private async Task MakeTopic()
        {
            using (var adminClient = new AdminClientBuilder(_clientConfig).Build())
            {
                try
                {
                    await adminClient.CreateTopicsAsync(new List <TopicSpecification> {
                        new TopicSpecification
                        {
                            Name              = Topic.Value,
                            NumPartitions     = NumPartitions,
                            ReplicationFactor = ReplicationFactor
                        }
                    });
                }
                catch (CreateTopicsException e)
                {
                    if (e.Results[0].Error.Code != ErrorCode.TopicAlreadyExists)
                    {
                        throw new ChannelFailureException($"An error occured creating topic {Topic.Value}: {e.Results[0].Error.Reason}");
                    }

                    s_logger.LogDebug("Topic {Topic} already exists", Topic.Value);
                }
            }
        }
Example #5
0
 static async Task CreateTopicAsync(string brokerList, List <string> topics)
 {
     using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
         BootstrapServers = brokerList
     }).Build())
     {
         try
         {
             var meta = adminClient.GetMetadata(TimeSpan.FromSeconds(20));
             foreach (var topic in topics)
             {
                 if (!meta.Topics.Exists(t => t.Topic == topic))
                 {
                     var topicSpecs = topics.Select(topicName =>
                                                    new TopicSpecification {
                         Name = topicName, ReplicationFactor = 1, NumPartitions = 1
                     });
                     await adminClient.CreateTopicsAsync(topicSpecs);
                 }
             }
         }
         catch (CreateTopicsException e)
         {
             Console.WriteLine($"An error occured creating topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
         }
     }
 }
        private IDictionary <string, QueueProperties> GetQueuesProperties()
        {
            var config = _options.ToAdminProperties();

            try
            {
                using (var admin = new AdminClientBuilder(config).Build())
                {
                    var meta  = admin.GetMetadata(_options.AdminRequestTimeout);
                    var props = from kafkaTopic in meta.Topics
                                join userTopic in _options.Topics on kafkaTopic.Topic equals userTopic.Name
                                from partition in kafkaTopic.Partitions
                                select new QueueProperties(
                        userTopic.Name,
                        (uint)partition.PartitionId,
                        userTopic.IsExternal
                        );

                    return(props.ToDictionary(prop => prop.QueueName));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to retrieve Kafka meta data. {@config}", config);
                throw;
            }
        }
Example #7
0
        public static IEnumerable <string> ListTopics(string filter = "")
        {
            var config = ConfigService.Get();

            using (var adminClient = new AdminClientBuilder(new AdminClientConfig
            {
                BootstrapServers = config.BrokerHost,
                SocketTimeoutMs = config.TimeoutInMs
            })
                                     .Build())
            {
                try
                {
                    var metaData = adminClient.GetMetadata(TimeSpan.FromMilliseconds(config.TimeoutInMs));

                    var topics = metaData.Topics.Select(tm => tm.Topic);
                    if (!string.IsNullOrWhiteSpace(filter))
                    {
                        topics = topics.Where(t => t.Contains(filter, StringComparison.OrdinalIgnoreCase));
                    }
                    return(topics.OrderBy(t => t));
                }
                catch (Exception e)
                {
                    throw new Exception($"{e.Message}");
                }
            }
        }
 public static async Task CreateTopicAsync(string bootstrapServers, string topicName, string connStr, string cacertlocation)
 {
     using (var adminClient = new AdminClientBuilder(new AdminClientConfig
     {
         BootstrapServers = bootstrapServers,
         SecurityProtocol = SecurityProtocol.SaslSsl,
         SaslMechanism = SaslMechanism.Plain,
         SaslUsername = "******",
         SaslPassword = connStr,
         SslCaLocation = cacertlocation,
     }).Build())
     {
         try
         {
             await adminClient.CreateTopicsAsync(new TopicSpecification[] {
                 new TopicSpecification {
                     Name = topicName, ReplicationFactor = 1, NumPartitions = 1
                 }
             });
         }
         catch (CreateTopicsException e)
         {
             if (e.Results[0].Error.Code == ErrorCode.TopicAlreadyExists)
             {
                 Console.WriteLine("Topic already exists.");
             }
             else
             {
                 Console.WriteLine($"An error occured creating topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
             }
         }
     }
 }
Example #9
0
        public async Task CriarTopico(string nomeTopico, int numParticoes, short fatorReplicacao, ClientConfig clientConfig)
        {
            using (var adminClient = new AdminClientBuilder(this.ProducerConfig).Build())
            {
                try
                {
                    await adminClient.CreateTopicsAsync(new List <TopicSpecification> {
                        new TopicSpecification {
                            Name = nomeTopico, NumPartitions = numParticoes, ReplicationFactor = fatorReplicacao
                        }
                    });

                    Console.WriteLine($"Tópico {nomeTopico} criado.");
                }
                catch (CreateTopicsException e)
                {
                    if (e.Results[0].Error.Code != ErrorCode.TopicAlreadyExists)
                    {
                        Console.WriteLine($"Erro na criação do tópico {nomeTopico}: {e.Results[0].Error.Reason}");
                    }
                    else
                    {
                        Console.WriteLine("O tópico já existe.");
                    }
                }

                Console.WriteLine("Pressione qualquer tecla para continuar.");
                Console.ReadKey();
            }
        }
        public Task <IEnumerable <Topic> > GetTopicsAsync()
        {
            List <Topic> result = new List <Topic>();

            try
            {
                using (var adminClient = new AdminClientBuilder(_connectionConfiguration.ToPairs()).Build())
                {
                    var metadata = adminClient.GetMetadata(TimeSpan.FromSeconds(5));
                    foreach (var metadataTopic in metadata.Topics)
                    {
                        result.Add(new Topic()
                        {
                            Name       = metadataTopic.Topic,
                            Partitions = metadataTopic.Partitions.Select(x => new Partition
                            {
                                Id = x.PartitionId,
                                PartitionLeader = x.Leader,
                                Replicas        = x.Replicas.Select(y => y).ToList()
                            }).ToList(),
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                _logger?.Error(ex);
                throw;
            }

            return(Task.FromResult((IEnumerable <Topic>)result));
        }
Example #11
0
        public void Metadata(string bootstrapServers)
        {
            LogToFile("start Metadata");

            var config = new AdminClientConfig {
                BootstrapServers = bootstrapServers
            };

            using (var adminClient = new AdminClientBuilder(config).Build())
            {
                var metadata = adminClient.GetMetadata(TimeSpan.FromSeconds(10));
                Assert.NotNull(metadata.Brokers);
                Assert.True(metadata.Brokers.Count > 0);

                var metadataAsJson = metadata.ToString();
                var deserialized   = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(metadataAsJson);

                Assert.Equal(deserialized.Value <int>("OriginatingBrokerId"), metadata.OriginatingBrokerId);
                Assert.Equal(deserialized.Value <string>("OriginatingBrokerName"), metadata.OriginatingBrokerName);
                var topics = new List <JToken>(deserialized["Topics"].Children());
                Assert.Equal(metadata.Topics.Count, topics.Count);
                for (int i = 0; i < metadata.Topics.Count; ++i)
                {
                    Assert.Equal(topics[i].Value <string>("Error"), metadata.Topics[i].Error.Code.ToString());
                    Assert.Equal(topics[i].Value <string>("Topic"), metadata.Topics[i].Topic);
                    var partitions = new List <JToken>(topics[i]["Partitions"].Children());
                    Assert.Equal(partitions.Count, metadata.Topics[i].Partitions.Count);
                    for (int j = 0; j < metadata.Topics[i].Partitions.Count; ++j)
                    {
                        Assert.Equal(partitions[j].Value <string>("Error"), metadata.Topics[i].Partitions[j].Error.Code.ToString());
                        Assert.Equal(partitions[j].Value <int>("Leader"), metadata.Topics[i].Partitions[j].Leader);
                        Assert.Equal(partitions[j].Value <int>("PartitionId"), metadata.Topics[i].Partitions[j].PartitionId);
                        var replicas = new List <JToken>(partitions[j]["Replicas"].Children());
                        Assert.Equal(replicas.Count, metadata.Topics[i].Partitions[j].Replicas.Length);
                        for (int k = 0; k < metadata.Topics[i].Partitions[j].Replicas.Length; ++k)
                        {
                            Assert.Equal(replicas[k].Value <int>(), metadata.Topics[i].Partitions[j].Replicas[k]);
                        }
                        var inSyncReplicas = new List <JToken>(partitions[j]["InSyncReplicas"].Children());
                        Assert.Equal(inSyncReplicas.Count, metadata.Topics[i].Partitions[j].InSyncReplicas.Length);
                        for (int k = 0; k < metadata.Topics[i].Partitions[j].InSyncReplicas.Length; ++k)
                        {
                            Assert.Equal(inSyncReplicas[k].Value <int>(), metadata.Topics[i].Partitions[j].InSyncReplicas[k]);
                        }
                    }
                }

                var brokers = new List <JToken>(deserialized["Brokers"].Children());
                Assert.Equal(metadata.Brokers.Count, brokers.Count);
                for (int i = 0; i < metadata.Brokers.Count; ++i)
                {
                    Assert.Equal(metadata.Brokers[i].BrokerId, brokers[i].Value <int>("BrokerId"));
                    Assert.Equal(metadata.Brokers[i].Host, brokers[i].Value <string>("Host"));
                    Assert.Equal(metadata.Brokers[i].Port, brokers[i].Value <int>("Port"));
                }
            }

            Assert.Equal(0, Library.HandleCount);
            LogToFile("end   Metadata");
        }
 public async Task AddNewTopicAsync(string topicName, int numOfPartition, short replicationFactor)
 {
     using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
         BootstrapServers = _connectionConfiguration.BootstrapServers
     }).Build())
     {
         try
         {
             await adminClient.CreateTopicsAsync(new TopicSpecification[] {
                 new TopicSpecification {
                     Name = topicName, ReplicationFactor = replicationFactor, NumPartitions = numOfPartition
                 }
             });
         }
         catch (CreateTopicsException e)
         {
             _logger.Error($"Topic creation failed with error: {e}");
             throw;
         }
         catch (Exception ex)
         {
             _logger.Error($"Topic creation failed with error: {ex}");
             throw;
         }
     }
 }
        /// <summary>
        ///     Remove all topics used by this example if they exist.
        /// </summary>
        static async Task DeleteTopics(string brokerList, string clientId)
        {
            var config = new AdminClientConfig
            {
                BootstrapServers = brokerList,
                ClientId         = clientId
            };

            using (var adminClent = new AdminClientBuilder(config).Build())
            {
                try
                {
                    await adminClent.DeleteTopicsAsync(new List <string> {
                        Topic_InputLines, Topic_Words, Topic_Counts
                    });
                }
                catch (DeleteTopicsException e)
                {
                    // propagate the exception unless the error was that one or more of the topics didn't exist.
                    if (e.Results.Select(r => r.Error.Code).Where(el => el != ErrorCode.UnknownTopicOrPart && el != ErrorCode.NoError).Count() > 0)
                    {
                        throw new Exception("Unable to delete topics", 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();
            }
        }
        public async Task <string> CreateTopicAsync(string name, int numPartitions, short replicationFactor)
        {
            using (var adminClient = new AdminClientBuilder(config).Build())
            {
                try
                {
                    await adminClient.CreateTopicsAsync(new List <TopicSpecification> {
                        new TopicSpecification {
                            Name = name, NumPartitions = numPartitions, ReplicationFactor = replicationFactor
                        }
                    });

                    return("Topic Created");
                }
                catch (CreateTopicsException e)
                {
                    if (e.Results[0].Error.Code != ErrorCode.TopicAlreadyExists)
                    {
                        logger.LogError($"An error occured creating topic {name}: {e.Results[0].Error.Reason}");
                        return("Failed to create topic");
                    }
                    else
                    {
                        logger.LogError("Topic already exists");
                        return("Topic already exists");
                    }
                }
            }
        }
Example #16
0
    public async Task TryCreateTopic(string topicName)
    {
        var adminConfig = new AdminClientConfig
        {
            BootstrapServers = _configuration["KafkaServer"]
        };

        using (var adminClient = new AdminClientBuilder(adminConfig).Build())
        {
            try
            {
                await adminClient.CreateTopicsAsync(new TopicSpecification[] {
                    new TopicSpecification
                    {
                        Name = topicName,
                        ReplicationFactor = 1,
                        NumPartitions     = 1
                    }
                });
            }
            catch (CreateTopicsException e)
            {
                Console.WriteLine($"An error occured creating topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
            }
        }
    }
        public async void InvalidAclBindingFilters()
        {
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = "localhost:90922"
            }).Build())
            {
                var suffixes = new List <string>()
                {
                    "Invalid resource type",
                    "Invalid resource pattern type",
                    "Invalid operation",
                    "Invalid permission type",
                };
                var invalidTests = suffixes.Select((suffix) => {
                    return(testAclBindingFilters[0].Clone());
                }).ToList();
                invalidTests[0].PatternFilter.Type = ResourceType.Unknown;
                invalidTests[1].PatternFilter.ResourcePatternType = ResourcePatternType.Unknown;
                invalidTests[2].EntryFilter.Operation             = AclOperation.Unknown;
                invalidTests[3].EntryFilter.PermissionType        = AclPermissionType.Unknown;

                int i = 0;
                foreach (AclBindingFilter invalidTest in invalidTests)
                {
                    var exInvalidTest = await Assert.ThrowsAsync <KafkaException>(() =>
                                                                                  adminClient.DescribeAclsAsync(invalidTest)
                                                                                  );

                    Assert.EndsWith(suffixes[i], exInvalidTest.Message);
                    ++i;
                }
            }
        }
Example #18
0
        public static async Task AssureTopic(string host, string topic)
        {
            var adminClientConfig = new AdminClientConfig();

            adminClientConfig.BootstrapServers = host;

            using (var adminClient = new AdminClientBuilder(adminClientConfig).Build())
            {
                try
                {
                    var metadata = adminClient.GetMetadata(topic, TimeSpan.FromSeconds(10));
                    if (!metadata.Topics.Any(x => x.Topic == topic))
                    {
                        var topicSpecification = new TopicSpecification()
                        {
                            Name = topic,
                            ReplicationFactor = 1,
                            NumPartitions     = 1
                        };
                        await adminClient.CreateTopicsAsync(new TopicSpecification[] { topicSpecification });
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"{nameof(KafkaCommon)} failed to create topic {topic}", ex);
                }
            }
        }
Example #19
0
        public static async Task CreateTopicAsync(string topicName, int partitions)
        {
            var config = ConfigService.Get();

            using (var adminClient = new AdminClientBuilder(new AdminClientConfig
            {
                BootstrapServers = config.BrokerHost,
                SocketTimeoutMs = config.TimeoutInMs
            })
                                     .Build())
            {
                try
                {
                    await adminClient.CreateTopicsAsync(new TopicSpecification[] {
                        new TopicSpecification {
                            Name = topicName, ReplicationFactor = 1, NumPartitions = partitions
                        }
                    });
                }
                catch (CreateTopicsException e)
                {
                    throw new Exception($"{e.Results[0].Error.Reason}");
                }
            }
        }
        public async Task EnsureTopicExistAsync(string topicName)
        {
            var bootstrapServers = _configuration["kafkaUrl"];

            using var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = bootstrapServers }).Build();
            try
            {
                var metadata   = adminClient.GetMetadata(TimeSpan.FromSeconds(10));
                var topicExist = metadata.Topics.Any(a => a.Topic == topicName);
                if (topicExist == false)
                {
                    await adminClient.CreateTopicsAsync(new[]
                    {
                        new TopicSpecification
                        {
                            Name = topicName,
                            ReplicationFactor = 1,
                            NumPartitions     = 1
                        }
                    });
                }
            }
            catch (CreateTopicsException e)
            {
                Console.WriteLine($"An error occured creating topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
            }
        }
Example #21
0
        async static Task RecreateTopicsAsync(string brokerAddress, TopicSpecification[] topicSpecs)
        {
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = brokerAddress
            }).Build())
            {
                try
                {
                    await adminClient.DeleteTopicsAsync(topicSpecs.Select(ts => ts.Name));
                }
                catch (DeleteTopicsException ex)
                {
                    foreach (var r in ex.Results)
                    {
                        if (r.Error.Code != ErrorCode.UnknownTopicOrPart)
                        {
                            throw;
                        }
                    }
                }

                await Task.Delay(TimeSpan.FromSeconds(2));

                await adminClient.CreateTopicsAsync(topicSpecs);
            }
        }
Example #22
0
        static void Assign(ConsumerBuilder <string, string> builder, string bootstrap, string groupId, string topic)
        {
            var offsets = new List <TopicPartitionOffset>
            {
                new TopicPartitionOffset(new TopicPartition(topic, 0), 2),
                new TopicPartitionOffset(new TopicPartition(topic, 1), 1),
            };

            // Check if none consumer is register for this consumer group
            var adminClientConfig = new AdminClientConfig();

            adminClientConfig.BootstrapServers = bootstrap;
            var adminClientBuilder = new AdminClientBuilder(adminClientConfig);

            using (var adminClient = adminClientBuilder.Build())
            {
                var groupInfo = adminClient.ListGroup(groupId, TimeSpan.FromSeconds(10));
                if (groupInfo.Members.Count > 0)
                {
                    Console.WriteLine($"Error consumers already exist in this consumer group {groupId}");
                    foreach (var member in groupInfo.Members)
                    {
                        Console.WriteLine(
                            $"Member {member.MemberId} (client.id:{member.ClientId}#client.host:{member.ClientHost}) =" +
                            $" Assigment {DecodeMemberAssignment(member.MemberAssignment)}");
                    }
                    return;
                }
            }

            using (var consumer = builder.Build())
            {
                consumer.Commit(offsets);
            }
        }
Example #23
0
        private bool FindTopic()
        {
            using (var adminClient = new AdminClientBuilder(_clientConfig).Build())
            {
                try
                {
                    var metadata = adminClient.GetMetadata(Topic.Value, TimeSpan.FromMilliseconds(TopicFindTimeoutMs));
                    //confirm we are in the list
                    var matchingTopics = metadata.Topics.Where(tp => tp.Topic == Topic.Value).ToArray();
                    if (matchingTopics.Length > 0)
                    {
                        var matchingTopic = matchingTopics[0];
                        if (matchingTopic.Error == null)
                        {
                            return(true);
                        }
                        if (matchingTopic.Error.Code == ErrorCode.UnknownTopicOrPart)
                        {
                            return(false);
                        }
                        else
                        {
                            s_logger.LogWarning("Topic {Topic} is in error with code: {ErrorCode} and reason: {ErrorMessage}", matchingTopic.Topic, matchingTopic.Error.Code, matchingTopic.Error.Reason);
                            return(false);
                        }
                    }

                    return(false);
                }
                catch (Exception e)
                {
                    throw new ChannelFailureException($"Error finding topic {Topic.Value}", e);
                }
            }
        }
        protected async Task CleanupKafka()
        {
            var config = new AdminClientConfig();

            config.BootstrapServers = TestSettings.KafkaBootstrapServers;
            try
            {
                using (var admin = new AdminClientBuilder(config).Build())
                {
                    await admin.DeleteTopicsAsync(new[] { AggregateType12, AggregateType34 });
                }
            }
            catch (DeleteTopicsException e)
            {
                // Don't fail if topic wasn't found (nothing to delete)
                if (e.Results.Where(r => r.Error.IsError).All(r => r.Error.Code == ErrorCode.UnknownTopicOrPart))
                {
                    TestContext.WriteLine(e.Message);
                }
                else
                {
                    throw;
                }
            }
        }
        protected override async Task <bool> PerformCheckAsync(KafkaContainer container, CancellationToken cancellationToken)
        {
            using var adminClient = new AdminClientBuilder(new AdminClientConfig
            {
                BootstrapServers = container.GetUrl(),
            })
                                    .Build();
            var topicName = $"{container.Name}_topic_health";
            var topics    = adminClient.GetMetadata(topicName, TimeSpan.FromSeconds(10)).Topics.Select(t => t.Topic).ToArray();

            if (!topics.Contains(topicName))
            {
                await adminClient.CreateTopicsAsync(new[]
                {
                    new TopicSpecification()
                    {
                        NumPartitions     = 1,
                        Name              = topicName,
                        ReplicationFactor = 1
                    },
                });
            }

            await ProduceMessage(topicName, container, cancellationToken);

            return(true);
        }
Example #26
0
        protected virtual async Task CreateTopicAsync()
        {
            using (var adminClient = new AdminClientBuilder(Options.Connections.GetOrDefault(ConnectionName)).Build())
            {
                var topic = new TopicSpecification
                {
                    Name              = TopicName,
                    NumPartitions     = 1,
                    ReplicationFactor = 1
                };

                Options.ConfigureTopic?.Invoke(topic);

                try
                {
                    await adminClient.CreateTopicsAsync(new[] { topic });
                }
                catch (CreateTopicsException e)
                {
                    if (!e.Error.Reason.Contains($"Topic '{TopicName}' already exists"))
                    {
                        throw;
                    }
                }
            }
        }
Example #27
0
        protected virtual List <TopicPartition> LoadTopicPartitions()
        {
            try
            {
                var timeout = TimeSpan.FromSeconds(5);
                using var adminClient = new AdminClientBuilder(adminClientConfig).Build();
                var metadata = adminClient.GetMetadata(this.topicName, timeout);
                if (metadata.Topics == null || metadata.Topics.Count == 0)
                {
                    logger.LogError("Could not load metadata information about topic '{topic}'", this.topicName);
                    return(new List <TopicPartition>());
                }

                var topicMetadata = metadata.Topics[0];
                var partitions    = topicMetadata.Partitions;
                if (partitions == null || partitions.Count == 0)
                {
                    logger.LogError("Could not load partition information about topic '{topic}'", this.topicName);
                    return(new List <TopicPartition>());
                }

                return(partitions.Select(x => new TopicPartition(topicMetadata.Topic, new Partition(x.PartitionId))).ToList());
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to load partition information from topic '{topic}'", this.topicName);
            }

            return(new List <TopicPartition>());
        }
Example #28
0
        public GlobalFixture()
        {
            var assemblyPath      = typeof(Tests).GetTypeInfo().Assembly.Location;
            var assemblyDirectory = Path.GetDirectoryName(assemblyPath);
            var jsonPath          = Path.Combine(assemblyDirectory, "testconf.json");
            var json = JObject.Parse(File.ReadAllText(jsonPath));

            bootstrapServers = json["bootstrapServers"].ToString();

            SinglePartitionTopic = "dotnet_test_" + Guid.NewGuid().ToString();
            PartitionedTopic     = "dotnet_test_" + Guid.NewGuid().ToString();

            // Create shared topics that are used by many of the tests.
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = bootstrapServers
            }).Build())
            {
                adminClient.CreateTopicsAsync(new List <TopicSpecification> {
                    new TopicSpecification {
                        Name = SinglePartitionTopic, NumPartitions = 1, ReplicationFactor = 1
                    },
                    new TopicSpecification {
                        Name = PartitionedTopic, NumPartitions = 2, ReplicationFactor = 1
                    }
                }).Wait();
            }
        }
        static async Task CreateTopicAsync(string bootstrapServers, string[] commandArgs)
        {
            if (commandArgs.Length != 1)
            {
                Console.WriteLine("usage: .. <bootstrapServers> create-topic <topic_name>");
                Environment.ExitCode = 1;
                return;
            }

            var topicName = commandArgs[0];

            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = bootstrapServers
            }).Build())
            {
                try
                {
                    await adminClient.CreateTopicsAsync(new TopicSpecification[] {
                        new TopicSpecification {
                            Name = topicName, ReplicationFactor = 1, NumPartitions = 1
                        }
                    });
                }
                catch (CreateTopicsException e)
                {
                    Console.WriteLine($"An error occurred creating topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
                }
            }
        }
Example #30
0
        public static List <Confluent.Kafka.GroupInfo> getGroups(string bootstrapServers)
        {
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = bootstrapServers
            }).Build())
            {
                // Warning: The API for this functionality is subject to change.
                var groups = adminClient.ListGroups(TimeSpan.FromSeconds(10));
                List <Confluent.Kafka.GroupInfo> GroupList = new List <Confluent.Kafka.GroupInfo>();
                Console.WriteLine($"Consumer Groups:");

                foreach (var g in groups)
                {
                    Console.WriteLine($"  Group: {g.Group} {g.Error} {g.State}");
                    Console.WriteLine($"  Broker: {g.Broker.BrokerId} {g.Broker.Host}:{g.Broker.Port}");
                    Console.WriteLine($"  Protocol: {g.ProtocolType} {g.Protocol}");
                    GroupList.Add(g);

                    //Collect Member-info here if needed.

                    /*
                     * Console.WriteLine ($"  Members:");
                     * foreach (var m in g.Members) {
                     *  Console.WriteLine ($"    {m.MemberId} {m.ClientId} {m.ClientHost}");
                     *  Console.WriteLine ($"    Metadata: {m.MemberMetadata.Length} bytes");
                     *  Console.WriteLine ($"    Assignment: {m.MemberAssignment.Length} bytes");
                     * } */
                }
                return(GroupList);
            }
        }