Skip to content

Commit cb34974

Browse files
committed
Add create topic operation
1 parent b194a7b commit cb34974

File tree

2 files changed

+228
-0
lines changed

2 files changed

+228
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) 2018, Matias Fontanini
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following disclaimer
13+
* in the documentation and/or other materials provided with the
14+
* distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*
28+
*/
29+
30+
#ifndef CPPKAFKA_CREATE_TOPIC_OPERATION_H
31+
#define CPPKAFKA_CREATE_TOPIC_OPERATION_H
32+
33+
#include <cstdint>
34+
#include <memory>
35+
#include <string>
36+
#include <vector>
37+
#include "operation.h"
38+
#include "../configuration_option.h"
39+
40+
#if RD_KAFKA_VERSION >= RD_KAFKA_ADMIN_API_SUPPORT_VERSION
41+
42+
namespace cppkafka {
43+
namespace admin {
44+
45+
class CreateTopicOperation : public Operation {
46+
public:
47+
/**
48+
* \brief Constructs an instance of a CreateTopicOperation
49+
*
50+
* Note that calling set_replica_assignment is invalid when using this constructor.
51+
* Use the other one which doesn't take a replication factor if you want to manually
52+
* assign the replica assignment to each partition
53+
*
54+
* \param name The name of the topic to be created
55+
* \param partitions The number of partitions to be created for this topic
56+
* \param replication_factor The topic's replication factor
57+
*/
58+
CreateTopicOperation(const std::string& name,
59+
unsigned partitions,
60+
unsigned replication_factor);
61+
62+
/**
63+
* \brief Constructs an instance of a CreateTopicOperation
64+
*
65+
* When calling this constructor, the user *must* call set_replica_assignment for each
66+
* of the partitions in ascending order. This is an API restriction imposed by librdkafka.
67+
*
68+
* \param name The name of the topic to be created
69+
* \param partitions The number of partitions to be created for this topic
70+
*/
71+
CreateTopicOperation(const std::string& name,
72+
unsigned partitions);
73+
74+
/**
75+
* \brief Sets the replica assignment for a particular partition
76+
*
77+
* This calls rd_kafka_NewTopic_set_replica_assignment under the hood
78+
*
79+
* This method can only be called if the constructor called was the one that doesn't take
80+
* a replication factor.
81+
*
82+
* This method must be called for every each partition in this topic (starting from 0)
83+
* in ascending order. This is an API restriction imposed by librdkafka.
84+
*
85+
*
86+
* \param partition The partition for which to set the replica assignment
87+
* \param broker_ids The list of broker ids that will replicate this partition
88+
*/
89+
void set_replica_assignment(int partition, const std::vector<int32_t>& broker_ids);
90+
91+
/**
92+
* \brief Sets a server side configuration option
93+
*
94+
* \param config_option The configuration option to be set
95+
*/
96+
void set_config(const ConfigurationOption& config_option);
97+
98+
private:
99+
void do_execute(KafkaHandleBase& kafka_handle,
100+
Queue& queue,
101+
const OperationOptions* options) override;
102+
103+
using HandlePtr = std::unique_ptr<rd_kafka_NewTopic_t,
104+
decltype(&rd_kafka_NewTopic_destroy)>;
105+
106+
void init(const std::string& topic, unsigned partitions, int replication_factor);
107+
108+
HandlePtr handle_;
109+
};
110+
111+
} // admin
112+
} // cppkafka
113+
114+
#endif // Admin API
115+
#endif // CPPKAFKA_CREATE_TOPIC_OPERATION_H

src/admin/create_topic_operation.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2018, Matias Fontanini
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following disclaimer
13+
* in the documentation and/or other materials provided with the
14+
* distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*
28+
*/
29+
30+
#include "admin/create_topic_operation.h"
31+
32+
using std::string;
33+
using std::vector;
34+
35+
#if RD_KAFKA_VERSION >= RD_KAFKA_ADMIN_API_SUPPORT_VERSION
36+
37+
namespace cppkafka {
38+
namespace admin {
39+
40+
CreateTopicOperation::CreateTopicOperation(const string& name,
41+
unsigned partitions,
42+
unsigned replication_factor)
43+
: handle_(nullptr, nullptr) {
44+
init(name, partitions, static_cast<int>(replication_factor));
45+
}
46+
47+
CreateTopicOperation::CreateTopicOperation(const string& name,
48+
unsigned partitions)
49+
: handle_(nullptr, nullptr) {
50+
init(name, partitions, -1);
51+
}
52+
53+
void CreateTopicOperation::set_replica_assignment(int partition,
54+
const vector<int32_t>& broker_ids) {
55+
char error_buffer[512] = { 0 };
56+
const Error result = rd_kafka_NewTopic_set_replica_assignment(
57+
handle_.get(),
58+
partition,
59+
const_cast<int32_t*>(broker_ids.data()),
60+
broker_ids.size(),
61+
error_buffer,
62+
sizeof(error_buffer)
63+
);
64+
if (result) {
65+
throw AdminOperationException(error_buffer);
66+
}
67+
}
68+
69+
void CreateTopicOperation::set_config(const ConfigurationOption& config_option) {
70+
const Error result = rd_kafka_NewTopic_set_config(
71+
handle_.get(),
72+
config_option.get_key().data(),
73+
config_option.get_value().data()
74+
);
75+
if (result) {
76+
throw AdminOperationException(result.to_string());
77+
}
78+
}
79+
80+
void CreateTopicOperation::do_execute(KafkaHandleBase& kafka_handle,
81+
Queue& queue,
82+
const OperationOptions* options) {
83+
rd_kafka_NewTopic_t* new_topic_handle = handle_.get();
84+
rd_kafka_CreateTopics(
85+
kafka_handle.get_handle(),
86+
&new_topic_handle,
87+
1 /*number of topics*/,
88+
options ? options->get_handle() : nullptr,
89+
queue.get_handle()
90+
);
91+
}
92+
93+
void CreateTopicOperation::init(const string& topic,
94+
unsigned partitions,
95+
int replication_factor) {
96+
char error_buffer[512] = { 0 };
97+
auto rdkafka_handle = rd_kafka_NewTopic_new(
98+
topic.data(),
99+
static_cast<int>(partitions),
100+
replication_factor,
101+
error_buffer,
102+
sizeof(error_buffer)
103+
);
104+
if (!rdkafka_handle) {
105+
throw AdminOperationException(error_buffer);
106+
}
107+
handle_ = HandlePtr(rdkafka_handle, rd_kafka_NewTopic_destroy);
108+
}
109+
110+
} // admin
111+
} // cppkafka
112+
113+
#endif // Admin API

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy