* mq: carry offsets, epoch and heartbeats on the follow stream The follow stream sent the publisher's raw message, so the follower could not keep the leader's offsets, and nothing named the generation or told a quiet leader from a dead one. The stream now carries log entries with their offsets, the epoch and leader in its init, heartbeats, and the flushed offset; acks report the follower's offset. EndPartitionGeneration lets a broker of a broken pair report it to the balancer. * mq broker: hand a partition's fence between generations A fence could only be taken by the next leader, at once. A pair that breaks needs more: the survivor must keep persisting what it holds before a new leader writes, and a follower must be able to take the fence from a leader it lost. The holder can now renew the fence, which tells a new leader it is still persisting, and seal it when it is done; a new leader takes an earlier generation's fence once it is sealed or left unrenewed for 30 seconds. Within one generation the fence moves only from the leader to its follower, and a sealed one stays sealed. Sealing writes a new token, so a follower that read the fence before the seal cannot take it over after; a plain renewal keeps the token, so flushes under way still match. A stale leader that reloads seals its old fence once its last flush lands, so the reload does not wait. * mq broker: ack a publish once the follower holds it The leader acked a publish as soon as it was in its own memory, sent the follower the raw message without its offset, learned its follower only from the publisher's init message (which the Kafka gateway never sets), and wrote to one follower stream from every publisher at once. The leader now takes its follower from the partition's assignment when it loads the partition and streams each entry, with its offset, from one sender goroutine in offset order; a publish is acked once the follower acks that offset. Heartbeats and a 5 second silence limit tell a lost follower from a quiet one, and a broken pair answers the publisher GENERATION_ENDED. The follower holds the entries the leader has not stored yet, drops them as flush notices arrive, and the leader closes the stream gracefully once it has stored everything, when the partition unloads. A partition with no follower acks from memory as before. * mq: give a partition whose pair broke a new pair Nothing reassigned a partition whose leader or follower was lost while the other half lived on: the balancer only replaced brokers that had left its broker set, and only on its own schedule. A broker of a broken pair now reports the epoch with EndPartitionGeneration, and the balancer assigns a new pair and the next epoch, preferring brokers outside the old pair. A report for an epoch that already moved on is ignored, so both halves can report, and reassignments are serialized. * mq broker: a leader that loses its follower stores what it holds and ends the generation When the follower stream broke, the leader kept serving alone, acking from memory again. A broken pair now ends the generation on the leader: it stops taking publishes, renews its fence so a new leader waits, stores everything it holds under that fence, seals it, and reports the epoch so the balancer assigns a new pair. A follower that cannot be reached when the partition loads ends the generation the same way. * mq broker: a follower takes over from a leader it lost When the follow stream broke, the follower dropped what it held, so entries the leader had acked but not stored were gone with it. The follower now moves the fence from the lost leader to itself, unless the leader sealed it first, reads what is already stored, stores the entries above that, seals the fence and reports the generation ended. The leader acks only what the follower holds, so every acked entry is stored once, and a leader still writing is fenced out. * mq broker: reload topic.conf when the balancer names this broker leader A broker made leader by a reassignment could still hold the previous topic.conf in its 30 second cache, so it either found no assignment for itself or tried to take the fence with the old epoch, and refused publishes until the cache expired. A publish that finds either now drops the cached config and loads the partition again. * mq broker: stop routing subscriptions through the follower A subscription dialed the follower named in its init and advanced its committed position only while that follower stream existed, so a partition without a follower never saved a consumer position, and a dead follower failed every subscription. The leader now tracks the position itself and saves it when the subscriber leaves. * mq broker: tell a partition's leader its follower when assigning it The balancer created a topic's partitions on their leaders with only the partition and epoch, so a partition made that way never knew its follower and acked from memory. It now sends the whole assignment. Only the named leader creates the partition; the follower learns of it from the leader's follow stream, so the old forward of the leader's request to the follower, which would have made the follower take the fence too, is gone. * mq broker: report an ended generation until a settled balancer takes it A broker reported a broken pair once. When the lost broker was also the balancer, the report failed and the partition stayed at a sealed epoch nobody would serve. The report is now retried with backoff until a balancer takes it, and a balancer takes reports only after its settle window, so the new pair is picked from every live broker rather than the few that reconnected first. * mq: describe the replicated pair in the failover notes * mq broker: break a pair whose follower stopped reading The follower's silence was checked in the loop that sends to it, so once a send blocked on a follower that stopped reading but kept its connection, nothing checked again and publishers waited on acks that never came. The check now runs on its own and cancels the stream. * mq broker: a follower that cannot ack its leader takes over An ack that failed to send ended the follow stream with an error and dropped the follower's entries, though they may include messages the leader had already acked to publishers. A failed ack now takes over the partition, as a broken receive does. * mq broker: retry a takeover through filer errors A follower taking over gave up on the first failed fence or offset read, dropping the entries its lost leader had not stored, which may be the only copy left. It now retries until the fence has moved on or the broker stops. * mq broker: keep the fence renewed while a survivor persists A leader that lost its follower renewed its fence once before storing what it held, and a follower taking over not at all after moving the fence, so a store that ran past 30 seconds let a new leader take the unsealed fence, refusing the survivor's write and dropping what it held. Both now renew every 10 seconds until they seal. * mq broker: leave the follower its copy when a retiring leader dropped a flush A retiring leader closed its follow stream gracefully once its buffer shut down, which tells the follower everything is stored, but a flush that failed until shutdown is dropped. With the filer unavailable both copies of acked messages went. The stream is now closed only when the flushed offset reached the last one assigned; otherwise it is broken, so the follower takes over and stores its copy. * mq broker: order every topic.conf read-modify-write on the balancer Only the generation handoff held a lock around its read and save of topic.conf, so a concurrent ConfigureTopic or assignment refresh that read the old assignments could save them back over the new pair, after the reporter had been told the generation was taken. All of them now take the same lock. * mq broker: stamp a fence write when it lands, not when it was first tried A fence write took its mtime before retrying, so a renewal that landed after a long run of store errors was already older than the seal wait, and a new leader could take the fence from a survivor still persisting. Each attempt now stamps the time it is sent. * mq broker: watch a leader's follower only once its partition is listed The lost-follower watch started before the partition was registered, so a pair that broke in that gap ended its generation while the partition could not be removed, and the partition was then registered and served after its fence was sealed. Both load paths now start the watch after registering it. * mq/kafka: answer a produce on an ended generation with NOT_LEADER_OR_FOLLOWER A partition whose pair broke answers publishes with GenerationEnded while the balancer hands it to a new pair, which the gateway mapped to UNKNOWN_SERVER_ERROR: it kept the session and Kafka clients did not refresh their metadata. It now maps to NOT_LEADER_OR_FOLLOWER, so the gateway drops the session and clients look the leader up again. * mq broker: take a stale fence only if its holder has not renewed it since A new leader judged an earlier generation's unsealed fence stale by its mtime, then wrote under the token alone. A renewal keeps the token and moves only the mtime, so a holder that renewed in between, still persisting what it held, lost the fence and the write that followed. Taking a fence judged stale now requires the entry as it was read. * mq broker: bound the seal when a leader cannot reach its follower A leader whose replica failed to start sealed its fence and reported the generation under the broker's access lock with no deadline, and a fence write retries until it lands, so a filer that kept failing held every partition operation on the broker. The seal now gives up after the fence take timeout and the generation is reported regardless. * mq broker: bound each report of an ended generation A generation report called the balancer with no deadline, so a balancer that accepted the call and stopped answering held the retry loop and the partition stayed unassigned. Each attempt now gives up after 30 seconds and is retried; a repeat after a save that landed finds the epoch moved on and returns without writing.
3.6 KiB
SeaweedMQ Message Queue on SeaweedFS (WIP, not ready)
What are the use cases it is designed for?
Message queues are like water pipes. Messages flow in the pipes to their destinations.
However, what if a flood comes? Of course, you can increase the number of partitions, add more brokers, restart, and watch the traffic level closely.
Sometimes the flood is expected. For example, backfill some old data in batch, and switch to online messages. You may want to ensure enough brokers to handle the data and reduce them later to cut cost.
SeaweedMQ is designed for use cases that need to:
- Receive and save large number of messages.
- Handle spike traffic automatically.
What is special about SeaweedMQ?
- Separate computation and storage nodes to scale independently.
- Unlimited storage space by adding volume servers.
- Unlimited message brokers to handle incoming messages.
- Offline messages can be operated as normal files.
- Scale up and down with auto split and merge message topics.
- Topics can automatically split into segments when traffic increases, and vice verse.
- Pass messages by reference instead of copying.
- Clients can optionally upload the messages first and just submit the references.
- Drastically reduce the broker load.
- Stateless brokers
- All brokers are equal. One broker is dynamically picked as the leader.
- Add brokers at any time.
- Allow rolling restart brokers or remove brokers at a pace.
Design
How it works?
Brokers are just computation nodes without storage. When a broker starts, it reports itself to masters. Among all the brokers, one of them will be selected as the leader by the masters.
A topic needs to define its partition key on its messages.
Messages for a topic are divided into segments. One segment can cover a range of partitions. A segment can be split into 2 segments, or 2 neighboring segments can be merged back to one segment.
During write time, the client will ask the broker leader for a few brokers to process the segment.
The broker leader will check whether the segment already has assigned the brokers. If not, select a few brokers based on their loads, save the selection into filer, and tell the client.
The client will write the messages for this segment to the selected brokers.
Failover
The broker leader does not contain any state. If it fails, the masters will select a different broker.
A partition's files are written only by the broker holding the partition's fence, so a broker that lost its leadership cannot add to them. When that broker fails, the partition is assigned to another broker, which takes the fence at a new epoch and continues from the files.
Each partition has a leader and a follower on two brokers. The leader acknowledges a message once the follower holds it, and the follower keeps what the leader has not flushed yet in memory. If either is lost, the survivor writes what it holds, seals the fence and reports the generation ended; the balancer then assigns a new pair at the next epoch, and publishers resend what was not acknowledged. A partition placed while only one broker is live has no follower, and messages it acknowledged but had not flushed yet are lost when that broker fails.
Auto Split or Merge
(The idea is learned from Pravega.)
The brokers should report its traffic load to the broker leader periodically.
If any segment has too much load, the broker leader will ask the brokers to tell the client to close current one and create two new segments.
If 2 neighboring segments have the combined load below average load per segment, the broker leader will ask the brokers to tell the client to close this 2 segments and create a new segment.