Package io.debezium.kafka
Class KafkaCluster
- java.lang.Object
-
- io.debezium.kafka.KafkaCluster
-
@ThreadSafe public class KafkaCluster extends Object
An embeddable cluster of Kafka servers and a single Zookeeper server. This may be useful when creating a complete environment within a single process, but doing so offers limited durability and fault tolerance compared to the normal approach of using an external cluster of Kafka servers and Zookeeper servers with proper replication and fault tolerance.- Author:
- Randall Hauch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceKafkaCluster.InteractiveConsumer<K,V>A simple interactive Kafka consumer for use with the cluster.static interfaceKafkaCluster.InteractiveProducer<K,V>A simple interactive Kafka producer for use with the cluster.classKafkaCluster.UsageA set of methods to use a running KafkaCluster.
-
Field Summary
Fields Modifier and Type Field Description private FiledataDirstatic booleanDEFAULT_DELETE_DATA_PRIOR_TO_STARTUPstatic booleanDEFAULT_DELETE_DATA_UPON_SHUTDOWNprivate booleandeleteDataPriorToStartupprivate booleandeleteDataUponShutdownprivate PropertieskafkaConfigprivate ConcurrentMap<Integer,KafkaServer>kafkaServersprivate static org.slf4j.LoggerLOGGERprivate AtomicLongnextKafkaPortprivate booleanrunningprivate intstartingKafkaPortprivate ZookeeperServerzkServer
-
Constructor Summary
Constructors Constructor Description KafkaCluster()Create a new embedded cluster.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description KafkaClusteraddBrokers(int count)Add a number of new Kafka broker to the cluster.StringbrokerList()Get the list of brokers.voidcreateTopic(String topic, int numPartitions, int replicationFactor)Create the specified topic.voidcreateTopics(int numPartitions, int replicationFactor, String... topics)Create the specified topics.voidcreateTopics(int numPartitions, int replicationFactor, Set<String> topics)Create the specified topics.voidcreateTopics(String... topics)Create the specified topics.voidcreateTopics(Set<String> topics)Create the specified topics.KafkaClusterdeleteDataPriorToStartup(boolean delete)Specify whether the data is to be deleted prior tostartup().KafkaClusterdeleteDataUponShutdown(boolean delete)Specify whether the data is to be deleted uponshutdown().booleanisRunning()Determine if the cluster is running.(package private) voidonEachDirectory(Consumer<File> consumer)Perform the supplied function on each directory used by this cluster.KafkaClustershutdown()Shutdown the embedded Zookeeper server and the Kafka serversin the cluster.private voidshutdownReliably(KafkaServer server)KafkaClusterstartup()Start the embedded Zookeeper server and the Kafka serversin the cluster.KafkaCluster.UsageuseTo()Obtain the interface for using this cluster.KafkaClusterusingDirectory(File dataDir)Set the parent directory where the brokers logs and server's logs and snapshots will be kept.KafkaClusterwithKafkaConfiguration(Properties properties)Set the configuration properties for each of the brokers.KafkaClusterwithPorts(int zkPort, int firstKafkaPort)Set the port numbers for Zookeeper and the Kafka brokers.intzkPort()Get the Zookeeper port.
-
-
-
Field Detail
-
DEFAULT_DELETE_DATA_UPON_SHUTDOWN
public static final boolean DEFAULT_DELETE_DATA_UPON_SHUTDOWN
- See Also:
- Constant Field Values
-
DEFAULT_DELETE_DATA_PRIOR_TO_STARTUP
public static final boolean DEFAULT_DELETE_DATA_PRIOR_TO_STARTUP
- See Also:
- Constant Field Values
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
kafkaServers
private final ConcurrentMap<Integer,KafkaServer> kafkaServers
-
zkServer
private final ZookeeperServer zkServer
-
dataDir
private volatile File dataDir
-
deleteDataUponShutdown
private volatile boolean deleteDataUponShutdown
-
deleteDataPriorToStartup
private volatile boolean deleteDataPriorToStartup
-
running
private volatile boolean running
-
kafkaConfig
private volatile Properties kafkaConfig
-
startingKafkaPort
private volatile int startingKafkaPort
-
nextKafkaPort
private final AtomicLong nextKafkaPort
-
-
Method Detail
-
deleteDataUponShutdown
public KafkaCluster deleteDataUponShutdown(boolean delete)
Specify whether the data is to be deleted uponshutdown().- Parameters:
delete- true if the data is to be deleted upon shutdown, or false otherwise- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
deleteDataPriorToStartup
public KafkaCluster deleteDataPriorToStartup(boolean delete)
Specify whether the data is to be deleted prior tostartup().- Parameters:
delete- true if the data is to be deleted upon shutdown, or false otherwise- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
addBrokers
public KafkaCluster addBrokers(int count)
Add a number of new Kafka broker to the cluster. The broker IDs will be generated.- Parameters:
count- the number of new brokers to add- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
usingDirectory
public KafkaCluster usingDirectory(File dataDir)
Set the parent directory where the brokers logs and server's logs and snapshots will be kept.- Parameters:
dataDir- the parent directory for the server's logs and snapshots; may be null if a temporary directory will be used- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is runningIllegalArgumentException- if the supplied file is not a directory or not writable
-
withKafkaConfiguration
public KafkaCluster withKafkaConfiguration(Properties properties)
Set the configuration properties for each of the brokers. This method does nothing if the supplied properties are null or empty.- Parameters:
properties- the Kafka configuration properties- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
withPorts
public KafkaCluster withPorts(int zkPort, int firstKafkaPort)
Set the port numbers for Zookeeper and the Kafka brokers.- Parameters:
zkPort- the port number that Zookeeper should use; may be -1 if an available port should be discoveredfirstKafkaPort- the port number for the first Kafka broker (additional brokers will use subsequent port numbers); may be -1 if available ports should be discovered- Returns:
- this instance to allow chaining methods; never null
- Throws:
IllegalStateException- if the cluster is running
-
isRunning
public boolean isRunning()
Determine if the cluster is running.- Returns:
- true if the cluster is running, or false otherwise
-
startup
public KafkaCluster startup() throws IOException
Start the embedded Zookeeper server and the Kafka serversin the cluster. This method does nothing if the cluster is already running.- Returns:
- this instance to allow chaining methods; never null
- Throws:
IOException- if there is an error during startup
-
shutdown
public KafkaCluster shutdown()
Shutdown the embedded Zookeeper server and the Kafka serversin the cluster. This method does nothing if the cluster is not running.- Returns:
- this instance to allow chaining methods; never null
-
createTopics
public void createTopics(String... topics)
Create the specified topics.- Parameters:
topics- the names of the topics to create- Throws:
IllegalStateException- if the cluster is not running
-
createTopics
public void createTopics(Set<String> topics)
Create the specified topics.- Parameters:
topics- the names of the topics to create- Throws:
IllegalStateException- if the cluster is not running
-
createTopics
public void createTopics(int numPartitions, int replicationFactor, String... topics)Create the specified topics.- Parameters:
numPartitions- the number of partitions for each topicreplicationFactor- the replication factor for each topictopics- the names of the topics to create
-
createTopics
public void createTopics(int numPartitions, int replicationFactor, Set<String> topics)Create the specified topics.- Parameters:
numPartitions- the number of partitions for each topicreplicationFactor- the replication factor for each topictopics- the names of the topics to create
-
createTopic
public void createTopic(String topic, int numPartitions, int replicationFactor)
Create the specified topic.- Parameters:
topic- the name of the topic to createnumPartitions- the number of partitions for the topicreplicationFactor- the replication factor for the topic
-
onEachDirectory
void onEachDirectory(Consumer<File> consumer)
Perform the supplied function on each directory used by this cluster.- Parameters:
consumer- the consumer function; may not be null
-
brokerList
public String brokerList()
Get the list of brokers.- Returns:
- the broker list
-
zkPort
public int zkPort()
Get the Zookeeper port.- Returns:
- the Zookeeper port
-
shutdownReliably
private void shutdownReliably(KafkaServer server)
-
useTo
public KafkaCluster.Usage useTo()
Obtain the interface for using this cluster.- Returns:
- the usage interface; never null
- Throws:
IllegalStateException- if the cluster is not running
-
-