Skip to content

MultiProcessConsumer: an option to use current offsets #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions kafka/consumer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class Consumer(object):
"""
def __init__(self, client, group, topic, partitions=None, auto_commit=True,
auto_commit_every_n=AUTO_COMMIT_MSG_COUNT,
auto_commit_every_t=AUTO_COMMIT_INTERVAL):
auto_commit_every_t=AUTO_COMMIT_INTERVAL,
load_initial_offsets=False):

self.client = client
self.topic = topic
Expand All @@ -67,7 +68,7 @@ def __init__(self, client, group, topic, partitions=None, auto_commit=True,
self.commit)
self.commit_timer.start()

if auto_commit:
if auto_commit or load_initial_offsets:
self.fetch_last_known_offsets(partitions)
else:
for partition in partitions:
Expand Down
14 changes: 9 additions & 5 deletions kafka/consumer/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
log = logging.getLogger("kafka")


def _mp_consume(client, group, topic, chunk, queue, start, exit, pause, size):
def _mp_consume(client, group, topic, chunk, queue, start, exit, pause, size, load_initial_offsets):
"""
A child process worker which consumes messages based on the
notifications given by the controller process
Expand All @@ -37,7 +37,8 @@ def _mp_consume(client, group, topic, chunk, queue, start, exit, pause, size):
partitions=chunk,
auto_commit=False,
auto_commit_every_n=None,
auto_commit_every_t=None)
auto_commit_every_t=None,
load_initial_offsets=load_initial_offsets)

# Ensure that the consumer provides the partition information
consumer.provide_partition_info()
Expand Down Expand Up @@ -105,15 +106,17 @@ class MultiProcessConsumer(Consumer):
def __init__(self, client, group, topic, auto_commit=True,
auto_commit_every_n=AUTO_COMMIT_MSG_COUNT,
auto_commit_every_t=AUTO_COMMIT_INTERVAL,
num_procs=1, partitions_per_proc=0):
num_procs=1, partitions_per_proc=0,
child_loads_initial_offsets=False):

# Initiate the base consumer class
super(MultiProcessConsumer, self).__init__(
client, group, topic,
partitions=None,
auto_commit=auto_commit,
auto_commit_every_n=auto_commit_every_n,
auto_commit_every_t=auto_commit_every_t)
auto_commit_every_t=auto_commit_every_t,
load_initial_offsets=child_loads_initial_offsets)

# Variables for managing and controlling the data flow from
# consumer child process to master
Expand Down Expand Up @@ -146,7 +149,8 @@ def __init__(self, client, group, topic, auto_commit=True,
args = (client.copy(),
group, topic, chunk,
self.queue, self.start, self.exit,
self.pause, self.size)
self.pause, self.size,
child_loads_initial_offsets)

proc = Process(target=_mp_consume, args=args)
proc.daemon = True
Expand Down
7 changes: 5 additions & 2 deletions kafka/consumer/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,16 @@ def __init__(self, client, group, topic, auto_commit=True, partitions=None,
buffer_size=FETCH_BUFFER_SIZE_BYTES,
max_buffer_size=MAX_FETCH_BUFFER_SIZE_BYTES,
iter_timeout=None,
auto_offset_reset='largest'):
auto_offset_reset='largest',
load_initial_offsets=False):

super(SimpleConsumer, self).__init__(
client, group, topic,
partitions=partitions,
auto_commit=auto_commit,
auto_commit_every_n=auto_commit_every_n,
auto_commit_every_t=auto_commit_every_t)
auto_commit_every_t=auto_commit_every_t,
load_initial_offsets=load_initial_offsets)

if max_buffer_size is not None and buffer_size > max_buffer_size:
raise ValueError("buffer_size (%d) is greater than "
Expand Down
36 changes: 36 additions & 0 deletions test/test_consumer_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ def test_simple_consumer_no_reset(self):
with self.assertRaises(OffsetOutOfRangeError):
consumer.get_message()

@kafka_versions("0.8.1", "0.8.1.1", "0.8.2.0")
def test_simple_consumer_load_initial_offsets(self):
self.send_messages(0, range(0, 100))
self.send_messages(1, range(100, 200))

# Create 1st consumer and change offsets
consumer = self.consumer()
self.assertEqual(consumer.offsets, {0: 0, 1: 0})
consumer.offsets.update({0:51, 1:101})
# Update counter after manual offsets update
consumer.count_since_commit += 1
consumer.commit()

# Create 2nd consumer and check initial offsets
consumer = self.consumer(load_initial_offsets=True, auto_commit=False)
self.assertEqual(consumer.offsets, {0: 51, 1: 101})

@kafka_versions("all")
def test_simple_consumer__seek(self):
self.send_messages(0, range(0, 100))
Expand Down Expand Up @@ -251,6 +268,25 @@ def test_multi_proc_pending(self):

consumer.stop()

@kafka_versions("0.8.1", "0.8.1.1", "0.8.2.0")
def test_multi_process_consumer_load_initial_offsets(self):
self.send_messages(0, range(0, 10))
self.send_messages(1, range(10, 20))

# Create 1st consumer and change offsets
consumer = self.consumer()
self.assertEqual(consumer.offsets, {0: 0, 1: 0})
consumer.offsets.update({0:5, 1:15})
# Update counter after manual offsets update
consumer.count_since_commit += 1
consumer.commit()

# Create 2nd consumer and check initial offsets
consumer = self.consumer(consumer = MultiProcessConsumer,
auto_commit=False,
child_loads_initial_offsets=True)
self.assertEqual(consumer.offsets, {0: 5, 1: 15})

@kafka_versions("all")
def test_large_messages(self):
# Produce 10 "normal" size messages
Expand Down