Write to Kafka from Dask Stream

Open
#315 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
kafka, python

Research direction

Start by reviewing the existing DaskStream and core.to_kafka implementations, including to_kafka.update and the get_producer workaround shown in the issue. Determine how the existing back-pressure handling and worker-local Kafka connection should fit together without pickling the producer. Done means Dask Stream can write to Kafka with the existing back-pressure behavior and a reliable worker-side producer.

Written by the indexing model from the issue text.

Description

It seems like the exiting PR for this needs improvements. I'm creating this issue to invite discussion on now to properly solve this problem.

I would like to use the existing back pressure handling implementation in the existing to_kafka function. I'm thinking that we can just call the existing to_kafka.update from Dask. It looks something like this:

@DaskStream.register_api()
class to_kafka(DaskStream, core.to_kafka):
    def update(self, x, who=None, metadata=None):
        client = default_client()
        result = client.submit(to_kafka.update, self, x, who, metadata)
        yield self._emit(result)

However, there's still the issue of the Kafka connection. It seems Dask attempts to pickle the Kafka producer and push it the worker. This fails due to the Confluent producer using some sort of lazy instantiation. In my previous attempt to make this work, I was able to create the producer using the getattr workaround. It also attaches the producer to the worker in order to maintain the connection.

def get_producer(config):
    w = get_worker()
    if hasattr(w, 'producer'):
        return w.producer

    import confluent_kafka as ck
    w.producer = getattr(ck, 'Producer')(config)
    return w.producer

This seems very much like a hack to me, and I would prefer a better solution. If anyone has more expertise on Dask, any help would be appreciated.

Dominant language
Python
Stars
1.3k
Forks
149
Avg merge
17h 39m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-streamz/streamz

All issues in python-streamz/streamz

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.