Metadata-Version: 2.2
Name: bisos.kafka
Version: 0.11
Summary: bisos.kafka: Apache Kafka and Kafbat UI via BISOS Capability Bundles
Author: Mohsen Banan
Author-email: libre@mohsen.1.banan.byname.net
Maintainer: Mohsen Banan
Maintainer-email: libre@mohsen.1.banan.byname.net
License: AGPL
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU Affero General Public License v3
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: bisos
Requires-Dist: bisos.b
Requires-Dist: bisos.banna
Requires-Dist: bisos.basics
Requires-Dist: bisos.capability
Requires-Dist: bisos.common
Requires-Dist: bisos.csPlayer
Requires-Dist: bisos.csSeed
Requires-Dist: bisos.debian
Requires-Dist: bisos.dnsCap
Requires-Dist: bisos.sbom
Requires-Dist: bisos.webCap
Requires-Dist: setuptools==75.8.0
Requires-Dist: wheel==0.38.4
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: license
Dynamic: maintainer
Dynamic: maintainer-email
Dynamic: requires-dist
Dynamic: summary

====================================================================
bisos.kafka: Apache Kafka and Kafbat UI via BISOS Capability Bundles
====================================================================

.. contents::
   :depth: 3
..

Overview
========

*bisos.kafka* is a BISOS package for running Apache Kafka on a BISOS
host — a single-node broker in KRaft mode (no ZooKeeper), together with
`Kafbat UI <https://github.com/kafbat/kafka-ui>`__ as its web UI — as a
BISOS Capability Bundle (CBS/CBM) of ``systemd``-planted Command
Services.

A planted pointer leaf (``cbmProc.spcs``, at
``sys/cbm/collective/hereWeb/kafka/``) subprocess-invokes a Capability
Bundle Specification (``kafka-cbs.pcs``). The CBS declares package
installation (sbom), the two ``systemd`` units (``kafka`` and
``kafbat-ui``), and the assembly step that makes Kafbat UI browsable as
``http://kafka.internal``.

Debian packages neither Kafka nor Kafbat UI. The sbom therefore takes
only the JRE from apt, and fetches the Apache Kafka binary tarball and
the Kafbat UI jar from upstream at pinned versions, refusing either one
if its checksum does not match.

Package Documentation At Github
===============================

The information below is a subset of the full of documentation for this
bisos-pip package. More complete documentation is available at:
https://github.com/bisos-pip/kafka-cs

.. _table-of-contents:

Table of Contents TOC
=====================

-  `Overview <#overview>`__
-  `Package Documentation At
   Github <#package-documentation-at-github>`__
-  `The BISOS Kafka Model <#the-bisos-kafka-model>`__
-  `Summary of Kafka and
   Terminology <#summary-of-kafka-and-terminology>`__

   -  `The Mental Model: a Durable Log, Not a
      Queue <#the-mental-model-a-durable-log-not-a-queue>`__
   -  `The Terms, Precisely <#the-terms-precisely>`__
   -  `Source and Sink: Where the Words Come
      From <#source-and-sink-where-the-words-come-from>`__
   -  `Security: Authentication, Authorization,
      Encryption <#security-authentication-authorization-encryption>`__
   -  `Delivery Guarantees and
      Transactions <#delivery-guarantees-and-transactions>`__
   -  `Common Terminology Mistakes <#common-terminology-mistakes>`__
   -  `What You See on This Host <#what-you-see-on-this-host>`__

-  `What Gets Installed, and Where <#what-gets-installed-and-where>`__
-  `The 2 systemd Units <#the-2-systemd-units>`__
-  `Listeners and Ports <#listeners-and-ports>`__
-  `Installation <#installation>`__

   -  `Installation With pip <#installation-with-pip>`__
   -  `Installation With pipx <#installation-with-pipx>`__

-  `Usage <#usage>`__

   -  `Materialization <#materialization>`__
   -  `Day-to-Day Admin:
      ``kafkaAdmin.cs`` <#day-to-day-admin-kafkaadmincs>`__
   -  `Direct Kafka CLI Cheat Sheet:
      ``quickKafka.cs`` <#direct-kafka-cli-cheat-sheet-quickkafkacs>`__
   -  `Files Over Kafka: ``kafkaFileSend.cs`` and
      ``kafkaFileReceive.cs`` <#files-over-kafka-kafkafilesendcs-and-kafkafilereceivecs>`__

-  `Key Files <#key-files>`__
-  `Part of BISOS — ByStar Internet Services Operating
   System <#part-of-bisos--bystar-internet-services-operating-system>`__
-  `Documentation and Blee-Panels <#documentation-and-blee-panels>`__
-  `Support <#support>`__
-  `Planned Improvements <#planned-improvements>`__

   -  `Authentication for Kafbat UI, so it can listen beyond
      loopback <#authentication-for-kafbat-ui-so-it-can-listen-beyond-loopback>`__
   -  `Remote clients: a non-loopback broker listener, with
      SASL/TLS <#remote-clients-a-non-loopback-broker-listener-with-sasltls>`__
   -  `Live status Cmnds in kafkaAdmin.cs (broker reachable, quorum
      leader) rather than printed
      examples <#live-status-cmnds-in-kafkaadmincs-broker-reachable-quorum-leader-rather-than-printed-examples>`__
   -  `A Capability Materialization figure (kafka-graphviz.pcs), as
      bisos.airflow
      has <#a-capability-materialization-figure-kafka-graphvizpcs-as-bisosairflow-has>`__
   -  `List ``confluent-kafka`` in the package requirements
      (``setup.py`` ``requires`` is generated; the lazy import in
      ``kafkaFileTransfer.py`` is missed by detection, so a pipx install
      does not bring the
      client) <#list-confluent-kafka-in-the-package-requirements-setuppy-requires-is-generated-the-lazy-import-in-kafkafiletransferpy-is-missed-by-detection-so-a-pipx-install-does-not-bring-the-client>`__

The BISOS Kafka Model
=====================

``bisos.kafka`` (``/bisos/git/bxRepos/bisos-pip/kafka``) packages Apache
Kafka 4.3.1 (single node, KRaft combined broker and controller) plus the
Kafbat UI as a hereWeb CBS/CBM, deployed the BISOS way rather than via a
Docker Compose quick-start. **As of 2026-10-14 it is materialized on one
host (bisos3 dev host, by hand)**: broker on 24007, Kafbat UI at
http://kafka.internal; see ``AI-WorkPlan.org`` 2.3. Transactions on a
single broker need ``transaction.state.log.replication.factor=1`` and
``transaction.state.log.min.isr=1``.

Which Python client library to use is in
`KafkaUsePython.org <KafkaUsePython.org>`__.

``bisos.kafka`` is also to carry the generic ``kafkaFileSend.cs`` /
``kafkaFileReceive.cs`` (module ``bisos/kafka/kafkaFileTransfer.py``,
Cmnds in ``kafkaFileTransfer_csu.py``), installed with pipx; a
consumer's code runs them as subprocesses and never imports ``bisos.*``
(``AI-WorkPlan.org`` Stage 2).

Summary of Kafka and Terminology
================================

This section is for someone new to Kafka. It gives the mental model
first, then the exact vocabulary, then the mistakes people most often
make with it. Wording follows the Apache Kafka documentation
(https://kafka.apache.org/documentation/).

The Mental Model: a Durable Log, Not a Queue
--------------------------------------------

Kafka is a **distributed, durable, append-only log service**. Programs
append *records* to named logs, and other programs read those logs, each
at its own pace and from a position it keeps for itself.

Three properties make Kafka different from a message queue:

#. **Reading does not remove anything.** A record stays until its
   topic's retention expires (here: 7 days, ``log.retention.hours=168``)
   or the topic is compacted. Any number of independent readers can read
   the same record, and a reader can go back and read it again.
#. **The reader keeps the bookmark, not the broker's queue.** Each
   reader's position (its *offset*) is recorded per consumer group.
   Moving the bookmark back replays history; moving it forward skips
   records.
#. **Order is per partition, not per topic.** A topic is split into
   partitions for parallelism; only within one partition are records in
   a guaranteed order.

::

   Producers                  Kafka cluster (here: 1 broker)               Consumers
   ---------          +--------------------------------------+             ---------
                      | Topic "orders"                       |
    producer A -----> |  partition 0: [r0][r1][r2][r3] ->    | <--poll---  consumer group G1
    producer B -----> |  partition 1: [r0][r1][r2]     ->    |             (member 1: p0,
                      |                                      |              member 2: p1)
                      | Topic "audit"                        |
                      |  partition 0: [r0][r1]         ->    | <--poll---  consumer group G2
                      +--------------------------------------+             (reads the same topics,
                                                                            independently of G1)
    Clients talk to brokers over TCP in Kafka's own binary protocol, not HTTP.
    Each box row ends in "->": new records are appended at the end of a partition.

The Terms, Precisely
--------------------

+---------------------+-----------------------------------------------+
| Term                | Exact meaning                                 |
+=====================+===============================================+
| Record              | The unit of data. Has a *key* (optional), a   |
|                     | *value* (bytes), *headers* (optional          |
+---------------------+-----------------------------------------------+
|                     | key-value pairs) and a timestamp. Also called |
|                     | an *event*; "message" is common and           |
+---------------------+-----------------------------------------------+
|                     | acceptable, but the Kafka docs say record.    |
+---------------------+-----------------------------------------------+
| Topic               | A named log of records, e.g. ``orders``.      |
|                     | Logical: the data is physically stored as     |
+---------------------+-----------------------------------------------+
|                     | partitions. A topic is not a queue, a table   |
|                     | or a file.                                    |
+---------------------+-----------------------------------------------+
| Partition           | One ordered, append-only sequence of records  |
|                     | within a topic. The unit of                   |
+---------------------+-----------------------------------------------+
|                     | ordering, of parallelism and of replication.  |
|                     | A topic has one or more.                      |
+---------------------+-----------------------------------------------+
| Offset              | A record's sequence number *within one        |
|                     | partition*. Offsets are per partition, not    |
+---------------------+-----------------------------------------------+
|                     | per topic and not global. It is not a         |
|                     | timestamp.                                    |
+---------------------+-----------------------------------------------+
| Key                 | Optional bytes that choose the partition (the |
|                     | same key always goes to the same              |
+---------------------+-----------------------------------------------+
|                     | partition, while the partition count is       |
|                     | unchanged). Same key means same order.        |
+---------------------+-----------------------------------------------+
| Broker              | A Kafka server process. It stores partitions  |
|                     | on its disk and serves producers              |
+---------------------+-----------------------------------------------+
|                     | and consumers. Also called a *Kafka server*;  |
|                     | "node" is ambiguous (see *node*               |
+---------------------+-----------------------------------------------+
|                     | below).                                       |
+---------------------+-----------------------------------------------+
| Cluster             | A set of brokers working together. This host  |
|                     | runs a cluster of one broker.                 |
+---------------------+-----------------------------------------------+
| Controller          | The part of the cluster that manages          |
|                     | metadata: which topics exist, which broker    |
+---------------------+-----------------------------------------------+
|                     | leads each partition. In KRaft mode           |
|                     | controllers use Raft consensus to agree on    |
+---------------------+-----------------------------------------------+
|                     | it.                                           |
+---------------------+-----------------------------------------------+
| KRaft               | Kafka's built-in metadata consensus (Kafka    |
|                     | Raft). It replaced ZooKeeper, which           |
+---------------------+-----------------------------------------------+
|                     | was removed in Kafka 4.0. There is no         |
|                     | ZooKeeper on this host.                       |
+---------------------+-----------------------------------------------+
| Node                | One Kafka process, identified by ``node.id``. |
|                     | With ``process.roles=broker,controller``      |
+---------------------+-----------------------------------------------+
|                     | (combined mode, as here) one node is both a   |
|                     | broker and a controller. In                   |
+---------------------+-----------------------------------------------+
|                     | production the roles are usually separated.   |
+---------------------+-----------------------------------------------+
| Replication         | Each partition is copied to several brokers:  |
|                     | one *leader* (takes all reads and             |
+---------------------+-----------------------------------------------+
|                     | writes) and *followers*.                      |
|                     | ``replication.factor`` is the number of       |
|                     | copies.                                       |
+---------------------+-----------------------------------------------+
| ISR                 | In-Sync Replicas: the replicas that are       |
|                     | caught up with the leader.                    |
+---------------------+-----------------------------------------------+
|                     | ``min.insync.replicas`` is how many must have |
|                     | a write before it is acknowledged.            |
+---------------------+-----------------------------------------------+
| Producer            | A client application that appends records to  |
|                     | topics. "Producer" describes a                |
+---------------------+-----------------------------------------------+
|                     | role, not a product.                          |
+---------------------+-----------------------------------------------+
| Consumer            | A client application that reads records from  |
|                     | topics, in a *consumer group*.                |
+---------------------+-----------------------------------------------+
| Consumer group      | Consumers sharing one ``group.id``. Kafka     |
|                     | divides a topic's partitions among the        |
+---------------------+-----------------------------------------------+
|                     | group's members, each partition going to      |
|                     | exactly one member at a time. A               |
+---------------------+-----------------------------------------------+
|                     | different group reads the same topic          |
|                     | independently, from its own offsets.          |
+---------------------+-----------------------------------------------+
| Committed offset    | The consumer group's bookmark for a           |
|                     | partition, stored in the internal topic       |
+---------------------+-----------------------------------------------+
|                     | ``__consumer_offsets``. *Committing* moves    |
|                     | the bookmark. It does not delete or           |
+---------------------+-----------------------------------------------+
|                     | acknowledge the record.                       |
+---------------------+-----------------------------------------------+
| Client              | Anything that connects to a broker:           |
|                     | producers, consumers and admin tools          |
+---------------------+-----------------------------------------------+
|                     | (``kafka-topics.sh``, Kafbat UI) are all      |
|                     | clients.                                      |
+---------------------+-----------------------------------------------+
| Listener            | A network endpoint of a broker: an address    |
|                     | and port plus a security protocol,            |
+---------------------+-----------------------------------------------+
|                     | e.g. ``PLAINTEXT://127.0.0.1:24007``.         |
+---------------------+-----------------------------------------------+
| Bootstrap servers   | The addresses a client uses for its first     |
|                     | contact, to learn the cluster's               |
+---------------------+-----------------------------------------------+
|                     | layout. After that the client connects to     |
|                     | whichever brokers the metadata names.         |
+---------------------+-----------------------------------------------+
| Advertised listener | The address the broker *tells clients* to     |
|                     | use. It must be reachable by the              |
+---------------------+-----------------------------------------------+
|                     | client, not just by the broker itself.        |
+---------------------+-----------------------------------------------+
| Retention           | How long a topic keeps records (time or size) |
|                     | regardless of whether they were               |
+---------------------+-----------------------------------------------+
|                     | read. *Compaction* instead keeps the latest   |
|                     | record per key.                               |
+---------------------+-----------------------------------------------+

Source and Sink: Where the Words Come From
------------------------------------------

*Source* and *sink* are **not** part of Kafka's core vocabulary. The
core words are producer and consumer. Source and sink come from *Kafka
Connect*, a separate framework shipped with Kafka for moving data
between Kafka and other systems without writing code:

+------------------+--------------------------------------------------+
| Term             | Meaning                                          |
+==================+==================================================+
| Source connector | A Connect plugin that reads from an *external*   |
|                  | system (a database, a file) and                  |
+------------------+--------------------------------------------------+
|                  | writes records *into* Kafka. Internally it acts  |
|                  | as a producer.                                   |
+------------------+--------------------------------------------------+
| Sink connector   | A Connect plugin that reads records *from* Kafka |
|                  | and writes them to an external                   |
+------------------+--------------------------------------------------+
|                  | system (a database, S3, a file). Internally it   |
|                  | acts as a consumer.                              |
+------------------+--------------------------------------------------+
| Kafka Streams    | A Java library for processing records from       |
|                  | topics into topics. A different thing            |
+------------------+--------------------------------------------------+
|                  | again.                                           |
+------------------+--------------------------------------------------+

In general data-flow language a "source" is where data enters a pipeline
and a "sink" is where it leaves, so people also call any program that
feeds Kafka a source. That is a loose usage. Precisely: \*a source is
something that produces into Kafka, a producer is the client role that
does it; a sink takes data out of Kafka, a consumer is the client role
that does it.\*

**In this repo**: the planned ``kafkaFileSend.cs`` (a producer: posts a
file) and ``kafkaFileReceive.cs`` (a consumer: rebuilds it)
(``AI-WorkPlan.org`` Stage 2) are named Send and Receive, not Source and
Sink, because those two words mean Kafka Connect connectors and, in many
data pipelines, the origin of the data. They are ordinary Kafka clients,
*not* Kafka Connect connectors.

Security: Authentication, Authorization, Encryption
---------------------------------------------------

These are three separate questions, and Kafka answers each with
different settings:

+-------------------------+----------------+-------------------------+
| Question                | Name           | Kafka mechanism         |
+=========================+================+=========================+
| Who is this client?     | Authentication | SASL (mechanisms        |
|                         |                | ``PLAIN``,              |
|                         |                | ``SCRAM-SHA-256/512``,  |
|                         |                | ``GSSAPI`` for          |
+-------------------------+----------------+-------------------------+
|                         |                | Kerberos,               |
|                         |                | ``OAUTHBEARER``) or TLS |
|                         |                | client certificates     |
+-------------------------+----------------+-------------------------+
|                         |                | (mutual TLS)            |
+-------------------------+----------------+-------------------------+
| What may this client    | Authorization  | ACLs (or a pluggable    |
| do?                     |                | authorizer) on topics,  |
|                         |                | groups                  |
+-------------------------+----------------+-------------------------+
|                         |                | and the cluster. It     |
|                         |                | needs authentication    |
|                         |                | first, since            |
+-------------------------+----------------+-------------------------+
|                         |                | ACLs name an identity.  |
+-------------------------+----------------+-------------------------+
| Can others read the     | Encryption     | TLS on the listener.    |
| traffic?                |                | Kafka's configuration   |
|                         |                | calls it                |
+-------------------------+----------------+-------------------------+
|                         |                | ``SSL``, but it means   |
|                         |                | TLS.                    |
+-------------------------+----------------+-------------------------+

The authentication and encryption choices combine in the listener's
security protocol:

+--------------------+-----------+-----------------------------------+
| Security protocol  | Encrypted | Authenticated                     |
+====================+===========+===================================+
| ``PLAINTEXT``      | no        | no                                |
+--------------------+-----------+-----------------------------------+
| ``SSL``            | yes (TLS) | no, or yes with client            |
|                    |           | certificates                      |
+--------------------+-----------+-----------------------------------+
| ``SASL_PLAINTEXT`` | no        | yes (SASL): credentials cross the |
|                    |           | network in the clear for some     |
+--------------------+-----------+-----------------------------------+
|                    |           | mechanisms                        |
+--------------------+-----------+-----------------------------------+
| ``SASL_SSL``       | yes (TLS) | yes (SASL): the usual production  |
|                    |           | choice                            |
+--------------------+-----------+-----------------------------------+

**Here**: every listener is ``PLAINTEXT`` and bound to ``127.0.0.1``.
There is no authentication, no authorization and no encryption. That is
safe only because nothing outside this host can connect. Kafbat UI
likewise has no login and is loopback-only behind nginx. See "Planned
Improvements" for authentication and remote-client TODOs.

Delivery Guarantees and Transactions
------------------------------------

+---------------+-----------------------------------------------------+
| Guarantee     | Meaning                                             |
+===============+=====================================================+
| At most once  | A record is delivered zero or one times. It may be  |
|               | lost, never repeated.                               |
+---------------+-----------------------------------------------------+
| At least once | Delivered one or more times. Never lost, possibly   |
|               | repeated. This is the default for a                 |
+---------------+-----------------------------------------------------+
|               | careful producer and consumer.                      |
+---------------+-----------------------------------------------------+
| Exactly once  | The effect appears once. In Kafka it holds for      |
|               | *read-process-write inside Kafka*, using            |
+---------------+-----------------------------------------------------+
|               | an idempotent producer plus transactions.           |
+---------------+-----------------------------------------------------+

A *transaction* lets a producer write to several partitions or topics
atomically: a consumer with ``isolation.level=read_committed`` sees all
of it or none of it. A consumer in the default ``read_uncommitted`` sees
records that a transaction has not yet committed, or later aborted.
Transactions rely on an internal topic ``__transaction_state``; on this
one-broker install its replication factor is 1 (see
``py3/bisos/kafka/kafkaInfo.py``), otherwise the first transactional
send would fail.

Common Terminology Mistakes
---------------------------

#. **"Kafka is a message queue."** In a queue, reading removes the
   message. In Kafka reading removes nothing; records expire by
   retention. Two consumer groups each get every record. Use *log* for
   the model.
#. **Treating kafka.internal as Kafka.** ``http://kafka.internal`` is
   Kafbat UI, a separate open-source web application that is itself a
   Kafka *client*. Kafka proper is the broker on ``127.0.0.1:24007``,
   and it does not speak HTTP. Pointing a browser or ``curl`` at the
   broker port gets garbage.
#. **Expecting order across a topic.** Order holds only within a
   partition. If order matters for a set of records, give them the same
   key.
#. **Thinking a consumer "acknowledges and deletes".** A consumer
   *commits an offset*, which is only a bookmark. Records are removed by
   retention.
#. **Calling the cluster the broker, or the other way round.** A broker
   is one server process. A cluster is a set of them. Here they happen
   to be one.
#. **Confusing source/sink with producer/consumer.** See the previous
   section: source and sink are Kafka Connect and data-flow terms;
   producer and consumer are the Kafka client roles.
#. **Publisher / subscriber.** These are general pub-sub words. A Kafka
   consumer does not "subscribe" the way a queue subscriber does: it
   *polls* and keeps its own position. Say producer and consumer.
#. **Calling any Kafka process "the server" or "a node" without saying
   which.** Say broker, controller or node (with its role).
#. **"Exactly once" as a blanket promise.** It covers work inside Kafka.
   A sink that writes to an external system (a database, a file) is
   still at least once unless that write is itself idempotent. Make the
   write repeatable (rewrite the same file, upsert by key).
#. **Mixing up encryption and authentication.** TLS (``SSL`` in the
   config) hides traffic; it does not say who the client is. SASL says
   who, and over ``SASL_PLAINTEXT`` it does not hide it. Authorization
   (ACLs) is a third step.
#. **Saying "SSL".** The protocol is TLS. Kafka's setting names kept the
   old word.
#. **Changing the partition count casually.** A topic's partitions can
   be increased, never decreased, and increasing remaps keys to
   different partitions, breaking "same key, same partition" for
   existing data.
#. **Taking replication factor 1 for safety.** One copy on one broker
   means a lost disk loses the data. Replication factor and
   ``min.insync.replicas`` are the safety; this single node deliberately
   has neither.
#. **A wrong advertised listener.** A client connects to the bootstrap
   address, receives the advertised addresses, then fails on those. The
   usual cause of "it connects, then times out". The bootstrap server is
   not a special server; it is just the first address tried.
#. **Believing ZooKeeper is needed.** Not since Kafka 4.0. Metadata is
   in KRaft.
#. **Assuming large payloads are fine.** The default maximum record size
   is about 1 MB. Large files are sent as many line-sized records (the
   Stage 2 design), or as a reference to the file.
#. **Calling an offset a time.** An offset is a per-partition sequence
   number. Timestamps are separate, and Kafka can look up an offset by
   timestamp.

What You See on This Host
-------------------------

+----------------------+----------------------+----------------------+
| Thing                | What it is           | Where                |
+======================+======================+======================+
| Broker + controller  | One Kafka process,   | ``127.0.0.1:24007``  |
|                      | combined mode        | (clients), ``24008`` |
+----------------------+----------------------+----------------------+
|                      | (``kafka.service``)  | (controller)         |
+----------------------+----------------------+----------------------+
| Kafbat UI            | A web client of the  | ``127.0.0.1:24009``, |
|                      | broker               | via nginx as         |
+----------------------+----------------------+----------------------+
|                      | (``                  | ``htt                |
|                      | kafbat-ui.service``) | p://kafka.internal`` |
+----------------------+----------------------+----------------------+
| Topics and records   | Files under the      | ``/                  |
|                      | broker's data        | var/lib/kafka/data`` |
+----------------------+----------------------+----------------------+
|                      | directory            |                      |
+----------------------+----------------------+----------------------+
| Internal topics      | ``_                  | same                 |
|                      | _consumer_offsets``, |                      |
+----------------------+----------------------+----------------------+
|                      | ``__                 |                      |
|                      | transaction_state``, |                      |
|                      | created on first     |                      |
+----------------------+----------------------+----------------------+
|                      | use                  |                      |
+----------------------+----------------------+----------------------+
| Command-line clients | Producer, consumer,  | ``/opt/              |
|                      | topics and admin     | kafka/current/bin``; |
|                      |                      | see                  |
+----------------------+----------------------+----------------------+
|                      | scripts              | ``quickKafka.cs``    |
+----------------------+----------------------+----------------------+
| Cluster              | 1 broker,            | PLAINTEXT, loopback  |
|                      | replication factor   | only                 |
|                      | 1, no                |                      |
+----------------------+----------------------+----------------------+
|                      | authentication       |                      |
+----------------------+----------------------+----------------------+

In Kafbat UI: *Topics* lists topics and shows each one's partitions,
offsets and messages; *Consumers* lists consumer groups and how far
behind each one is; *Brokers* shows the broker. Everything there is read
from, or sent to, the broker as an ordinary client would.

A first exercise from a shell (see ``quickKafka.cs`` for the cheat
sheet):

.. code:: bash

   quickKafka.cs
   K=/opt/kafka/current/bin ; BS=127.0.0.1:24007
   $K/kafka-topics.sh --bootstrap-server $BS --create --topic try --partitions 1
   printf 'a\nb\nc\n' | $K/kafka-console-producer.sh --bootstrap-server $BS --topic try
   $K/kafka-console-consumer.sh --bootstrap-server $BS --topic try --from-beginning \
       --max-messages 3 --group g1

Then read the topic again with ``--group g2`` and see the same three
records again; that is the difference between a log and a queue.

What Gets Installed, and Where
==============================

Every fact below lives in one place, ``py3/bisos/kafka/kafkaInfo.py``,
which the sbom, the units, the dns and vhost files and the admin CS all
read.

+-------------------------+-------------------------+----------------+
| What                    | Where                   | Owner          |
+=========================+=========================+================+
| JRE                     | apt                     | root           |
| (``ope                  |                         |                |
| njdk-25-jre-headless``) |                         |                |
+-------------------------+-------------------------+----------------+
| The ``kafka`` account   | uid/gid 503, from       | —              |
| and group               | bisos.banna             |                |
|                         | ``sysAccts``            |                |
+-------------------------+-------------------------+----------------+
| Kafka 4.3.1 (Scala      | ``/opt/k                | root           |
| 2.13)                   | afka/kafka_2.13-4.3.1`` |                |
+-------------------------+-------------------------+----------------+
| Active Kafka version    | ``/opt/kafka/current``  | root           |
|                         | -> the above            |                |
+-------------------------+-------------------------+----------------+
| Kafbat UI 1.5.0         | ``/opt/kafka/           | root           |
|                         | kafbat/api-v1.5.0.jar`` |                |
+-------------------------+-------------------------+----------------+
| librdkafka 2.16.0       | ``/opt/ka               | root           |
| (Kerberos)              | fka/librdkafka-2.16.0`` |                |
+-------------------------+-------------------------+----------------+
| Active librdkafka       | ``                      | root           |
|                         | /opt/kafka/librdkafka`` |                |
|                         | -> the above            |                |
+-------------------------+-------------------------+----------------+
| Build env (Python       | ``/opt/kafka/li         | root           |
| binding)                | brdkafka/build-env.sh`` |                |
+-------------------------+-------------------------+----------------+
| Broker config           | ``/etc/ka               | root:kafka 640 |
|                         | fka/server.properties`` |                |
+-------------------------+-------------------------+----------------+
| Broker environment      | ``/etc/default/kafka``  | root:kafka 640 |
+-------------------------+-------------------------+----------------+
| Kafbat UI environment   | ``/                     | root:kafka 640 |
|                         | etc/default/kafbat-ui`` |                |
+-------------------------+-------------------------+----------------+
| KRaft metadata +        | ``/var/lib/kafka/data`` | kafka          |
| partitions              |                         |                |
+-------------------------+-------------------------+----------------+
| Application logs        | ``/var/log/kafka``      | kafka          |
+-------------------------+-------------------------+----------------+

librdkafka is the C client library under the ``confluent-kafka`` Python
package. It is built from a pinned, checksummed source tarball with
Kerberos (GSSAPI), which the PyPI wheels lack; a client that must reach
a Kerberos cluster builds its binding against it. See
`KafkaUsePython.org <KafkaUsePython.org>`__.

The account's numbers come from banna's registry, not from ``useradd``'s
per-host choice, so ``kafka`` is 503 on every platform; the sbom refuses
if 503 is already taken.

The three config files are written only when absent, so hand overrides
survive re-runs of the sbom. ``kafkaAdmin.cs -i driftCheck`` reports how
a live file differs from what its generator would write.

Storage is formatted (``kafka-storage.sh format --standalone``) only
when ``/var/lib/kafka/data/meta.properties`` is absent. Formatting a
used data directory again would mint a new cluster id and orphan every
topic.

The 2 systemd Units
===================

+----------------------+----------------------+----------------------+
| Unit                 | Runs                 | Ordering             |
+======================+======================+======================+
| ``kafka-sysd.pcs``   | ``kaf                | ``Af                 |
|                      | ka-server-start.sh`` | ter=network.target`` |
|                      | with                 |                      |
+----------------------+----------------------+----------------------+
|                      | ``/etc/kafka/        |                      |
|                      | server.properties``; |                      |
|                      | broker and           |                      |
+----------------------+----------------------+----------------------+
|                      | KRaft controller in  |                      |
|                      | one process          |                      |
+----------------------+----------------------+----------------------+
| ``                   | ``java -j            | ``A                  |
| kafbat-ui-sysd.pcs`` | ar api-v1.5.0.jar``, | fter=kafka.service`` |
|                      | configured by        |                      |
+----------------------+----------------------+----------------------+
|                      | ``/etc               | ``W                  |
|                      | /default/kafbat-ui`` | ants=kafka.service`` |
+----------------------+----------------------+----------------------+

Both run as ``User=kafka``. Kafbat UI uses ``Wants`` rather than
``Requires``, so it stays up, showing the cluster as offline, while
``kafka.service`` restarts.

Listeners and Ports
===================

Ports are assigned in ``bisos.banna`` (``tcpPorts.py``), never
hardcoded. Every listener binds loopback only: the broker serves this
host's tenants, and Kafbat UI has no authentication.

==================== ========================= ===================
Listener             Address                   banna name
==================== ========================= ===================
Broker (PLAINTEXT)   ``127.0.0.1:24007``       ``kafka``
KRaft controller     ``127.0.0.1:24008``       ``kafkaController``
Kafbat UI            ``127.0.0.1:24009``       ``kafbatUi``
Kafbat UI, via nginx ``http://kafka.internal`` —
==================== ========================= ===================

Clients use ``bootstrap.servers=127.0.0.1:24007``.

Installation
============

| The sources for the bisos.kafka pip package are maintained at:
| https://github.com/bisos-pip/kafka

| The bisos.kafka pip package is available at PYPI as
| https://pypi.org/project/bisos.kafka

You can install bisos.kafka with pip or pipx.

Installation With pip
---------------------

If you need access to bisos.kafka as a python module, install it with
pip:

.. code:: bash

   pip install bisos.kafka

Installation With pipx
----------------------

If you only need access to bisos.kafka on the command line, install it
with pipx:

.. code:: bash

   pipx install bisos.kafka

The following commands are made available:

-  ``kafkaAdmin.cs`` — day-to-day admin CS for this host's
   ``kafka.internal`` installation.
-  ``quickKafka.cs`` — direct-command cheat sheet for the Kafka CLI
   tools and the two ``systemd`` units.
-  ``kafka-assemble.cs`` — assembly step: ``kafka.internal`` dns entry
   and nginx vhost.
-  ``cbmProc-kafka.spcs`` — planted CBM pointer that materializes the
   CBS at this host.

The planted ``.pcs`` files (``kafka-cbs.pcs``, ``kafka-sbom.pcs``,
``kafkaUsage-sbom.pcs``, ``kafkaCbs-sbom.pcs``, ``kafka-sysd.pcs``,
``kafbat-ui-sysd.pcs``, ``kafka-here-dns.pcs``, ``kafka-wvd.pcs``) are
invoked directly from ``py3/bin/``.

Usage
=====

Materialization
---------------

.. code:: bash

   kafka-sbom.pcs -i sbom_fullUpdate                    # install the server only
   kafkaUsage-sbom.pcs -i sbom_fullUpdate               # install the usage side only (Kerberos-capable librdkafka)
   kafkaCbs-sbom.pcs -i sbom_fullUpdate                 # install both
   kafka-cbs.pcs -i cbs_materialize                     # both sboms + units + assemble
   cbmProc-kafka.spcs -i cbm_enable                     # at the planted CBM leaf, then:
   cbmProc-kafka.spcs -i cbm_materialize

Day-to-Day Admin: ``kafkaAdmin.cs``
-----------------------------------

Run with no arguments (or ``-i examples``) to see the full menu. Key
sub-commands:

.. code:: bash

   kafkaAdmin.cs -i hostInfo             # fqdn, listeners, install/config/data/log paths
   kafkaAdmin.cs -i servicesInfo         # links for operating the 2 systemd units
   kafkaAdmin.cs -i topicsInfo           # create, list, describe, produce, consume
   kafkaAdmin.cs -i consumerGroupsInfo   # list, lag, reset offsets
   kafkaAdmin.cs -i driftCheck           # read-only: live config and units vs kafkaInfo
   kafkaAdmin.cs -i fullUpdate           # the four info Cmnds together

Direct Kafka CLI Cheat Sheet: ``quickKafka.cs``
-----------------------------------------------

For direct ``kafka-*.sh`` / ``systemctl`` / ``journalctl`` invocations,
and hints for reading Kafbat UI, run:

.. code:: bash

   quickKafka.cs

Files Over Kafka: ``kafkaFileSend.cs`` and ``kafkaFileReceive.cs``
------------------------------------------------------------------

Two commands, generic and optional, that carry files through Kafka and,
if wished, rebuild them. They are ordinary Kafka clients (a producer and
a consumer), not Kafka Connect connectors, and they know nothing about
any particular consumer: topic, key, headers, identity headers and
output paths are all parameters. Needs the Python package
``confluent-kafka`` (for Kerberos, built against the library
``kafkaUsage-sbom.pcs`` provides; see
`KafkaUsePython.org <KafkaUsePython.org>`__).

**Try it.** Against this host's broker, with a file every host has; runs
as shown, in either order (the receiver starts at the earliest offset,
and the broker creates the topic on first send). Both commands show
these lines first in their menus.

.. code:: bash

   kafkaFileSend.cs -i fileSend --bootstrap=127.0.0.1:24007 --topic=kafkaFileDemo --headers=host=$(hostname) /etc/os-release
   kafkaFileReceive.cs -i fileReceive --bootstrap=127.0.0.1:24007 --topic=kafkaFileDemo --group=kafkaFileDemo \
       --outBase=/tmp/kafkaFileDemo --idleExit=10
   cmp /etc/os-release /tmp/kafkaFileDemo/os-release && echo identical
   rm -r /tmp/kafkaFileDemo

A second receive with the same group reads nothing new; a resend of an
unchanged file is received as a duplicate and skipped.

**Send.** Each file becomes one Kafka transaction; each line one record.
The value is the line without its terminator, and every record carries
the headers you give. A consumer that knows nothing about files reads
plain, headed rows.

.. code:: bash

   kafkaFileSend.cs -i fileSend --bootstrap=127.0.0.1:24007 --topic=results \
       --headers=run=r1,host=h1 --attrs=kind=audit --root=/data/out /data/out/a/b/data.txt

In the default ``--model files`` the first record also carries a
``kfx-bof`` header and the last a ``kfx-eof`` header (JSON): the path
(relative to ``--root``, so a hierarchy can be reproduced), size, line
count, SHA-256, the line terminator, and which headers identify the
file. The BOF is what the sender *declared*; the EOF is what it
*observed* while streaming, and if they differ the file changed during
the send and the sender aborts its own transaction. With
``--model records`` there are no markers.

**Receive.** Reads as a ``read_committed`` consumer, buffers each file
until its last record, checks lines, bytes and SHA-256 against both
markers, and writes it atomically under ``--outBase``, reproducing the
sent path (or placing it with ``--pathTemplate``). The same path with
the same content is skipped.

.. code:: bash

   kafkaFileReceive.cs -i fileReceive --bootstrap=127.0.0.1:24007 --topic=results --group=g1 \
       --outBase=/data/in --idleExit=30

A rejected file (a failed check, a path that is not relative, a
malformed marker) is reported and skipped; it does not stop the stream.
Both commands exit non-zero if anything failed. Security settings
(``sasl.*``, ``ssl.*``) come from ``--clientConf``, a file of librdkafka
``name=value`` lines.

**Known limits, by decision:** lines only (no chunking of binary files);
one uniform line terminator per file; non-empty files; a file is held in
memory by sender and receiver; senders are assumed benevolent (paths are
checked against mistakes, not attacks); a sender killed mid-transaction
holds back ``read_committed`` readers of that partition until
``transaction.timeout.ms`` (60 s by default). Design and reasons:
``AI-WorkPlan.org``, Stage 2. Tests: ``tests/test_kafkaFileTransfer.py``
(no broker) and ``tests/integration_kafkaFileTransfer.py`` (this host's
broker).

Key Files
=========

-  ``py3/bisos/kafka/kafkaFileTransfer.py`` — the pure module: records
   from a file, the receiver's assembler, path checks, atomic placement,
   and the thin ``confluent-kafka`` layer.
-  ``py3/bisos/kafka/kafkaFileTransfer_csu.py`` — the two Cmnds,
   ``fileSend`` and ``fileReceive``, and their examples.
-  ``py3/bin/kafkaFileSend.cs``, ``py3/bin/kafkaFileReceive.cs`` — the
   two thin commands over that unit.
-  ``tests/`` — unit and integration tests of the file transfer, and
   ``tests/clientCompare/`` (client comparison and the Kerberos
   handshake test).
-  ``py3/bisos/kafka/kafkaInfo.py`` — the single source of facts: pinned
   versions and checksums, paths, ports, and the generators of the three
   config files and the units' identity lines.
-  ``py3/bin/kafka-cbs.pcs`` — CBS declaring the bundle (sbom + 2 sysd
   units + assemble).
-  ``py3/bin/cbmProc-kafka.spcs`` — planted CBM pointer leaf that
   materializes the CBS.
-  ``py3/bin/kafka-sbom.pcs`` — the server's package installation via
   `bisos.sbom <https://github.com/bisos-pip/sbom>`__: the JRE from apt,
   then Kafka and Kafbat UI from upstream.
-  ``py3/bin/kafkaUsage-sbom.pcs`` — the usage side, for a host that
   uses a broker (its own or another's): the pinned librdkafka built
   with Kerberos, and its build packages. The server sbom does not pull
   it in.
-  ``py3/bin/kafkaCbs-sbom.pcs`` — the sbom of ``kafka-cbs.pcs``: runs
   the two above. It exists because a CBS names one sbom.
-  ``py3/bin/kafka-sysd.pcs``, ``py3/bin/kafbat-ui-sysd.pcs`` — the two
   units.
-  ``py3/bin/kafka-assemble.cs`` — assembly step (dns + vhost).
-  ``py3/bin/kafka-here-dns.pcs`` — ``.internal`` fake-domain DNS
   registration for ``kafka.internal``.
-  ``py3/bin/kafka-wvd.pcs`` — nginx vhost for ``kafka.internal``,
   proxying (unbuffered, for Kafbat UI's message streaming) to Kafbat UI
   on loopback.
-  ``py3/bin/kafkaAdmin.cs``, ``py3/bin/quickKafka.cs`` — admin CS and
   cheat sheet.
-  ``py3/tests/verify.sh`` — smoke test for ``kafkaAdmin.cs`` and
   ``quickKafka.cs``.
-  ``py3/setup.py``, ``py3/pypiProc.sh`` — PyPI packaging (setup.py is
   dblock-driven; do not hand-edit).

Part of BISOS — ByStar Internet Services Operating System
=========================================================

Layered on top of Debian, **BISOS** (By\* Internet Services Operating
System) is a unified and universal framework for developing both
internet services and software-service continuums that use internet
services. See `Bootstrapping ByStar, BISOS and
Blee <https://github.com/bxGenesis/start>`__ for information about
getting started with BISOS.

**BISOS** is a foundation for **The Libre-Halaal ByStar Digital
Ecosystem** which is described as a cure for losses of autonomy and
privacy in a book titled: `Nature of
Polyexistentials <https://github.com/bxplpc/120033>`__

*bisos.kafka* is part of BISOS. It is a standalone package that can be
used independently of the full BISOS environment.

Documentation and Blee-Panels
=============================

bisos.kafka is part of the ByStar Digital Ecosystem
http://www.by-star.net.

This module's primary documentation is in the form of Blee-Panels.
Blee-Panels are in the ``./panels`` directory. From within Blee and
BISOS these panels are accessible under the Blee "Panels" menu.

See
`file:./panels/bisos.kafka/_nodeBase_/fullUsagePanel-en.org <./panels/bisos.kafka/_nodeBase_/fullUsagePanel-en.org>`__
for a starting point.

*bisos.kafka* is best developed with
`Blee <https://github.com/bx-blee>`__, the *By\* BISOS Libre-Halaal
Emacs Environment* — a layer on top of Emacs and BISOS which creates a
comprehensive integrated usage and development environment.

Support
=======

| For support, criticism, comments and questions; please contact the
  author/maintainer
| `Mohsen Banan <http://mohsen.1.banan.byname.net>`__ at:
  http://mohsen.1.banan.byname.net/contact

Planned Improvements
====================

.. _authentication-for-kafbat-ui-so-it-can-listen-beyond-loopback:

TODO Authentication for Kafbat UI, so it can listen beyond loopback
-------------------------------------------------------------------

.. _remote-clients-a-non-loopback-broker-listener-with-sasltls:

TODO Remote clients: a non-loopback broker listener, with SASL/TLS
------------------------------------------------------------------

.. _live-status-cmnds-in-kafkaadmin.cs-broker-reachable-quorum-leader-rather-than-printed-examples:

TODO Live status Cmnds in kafkaAdmin.cs (broker reachable, quorum leader) rather than printed examples
------------------------------------------------------------------------------------------------------

.. _a-capability-materialization-figure-kafka-graphviz.pcs-as-bisos.airflow-has:

TODO A Capability Materialization figure (kafka-graphviz.pcs), as bisos.airflow has
-----------------------------------------------------------------------------------

.. _list-confluent-kafka-in-the-package-requirements-setup.py-requires-is-generated-the-lazy-import-in-kafkafiletransfer.py-is-missed-by-detection-so-a-pipx-install-does-not-bring-the-client:

TODO List ``confluent-kafka`` in the package requirements (``setup.py`` ``requires`` is generated; the lazy import in ``kafkaFileTransfer.py`` is missed by detection, so a pipx install does not bring the client)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
