io.streamnative.pulsar.handlers.kop.storage.ProducerStateEntry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pulsar-protocol-handler-kafka Show documentation
Show all versions of pulsar-protocol-handler-kafka Show documentation
Kafka on Pulsar implemented using Pulsar Protocol Handler
/**
* Copyright (c) 2019 - 2024 StreamNative, Inc.. All Rights Reserved.
*/
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.streamnative.pulsar.handlers.kop.storage;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Optional;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.kafka.common.record.RecordBatch;
/**
* the batchMetadata is ordered such that the batch with the lowest sequence is at the head of the queue while the
* batch with the highest sequence is at the tail of the queue. We will retain at most ProducerStateEntry.
* NumBatchesToRetain elements in the queue. When the queue is at capacity, we remove the first element to make
* space for the incoming batch.
*/
@AllArgsConstructor
@EqualsAndHashCode
@ToString
public class ProducerStateEntry {
public static final int SIZE = 8 + 2 + 4 + 8 + 8;
final long producerId;
Short producerEpoch;
Integer coordinatorEpoch;
Long lastTimestamp;
Optional currentTxnFirstOffset;
void addBatch(short producerEpoch, long timestamp) {
maybeUpdateProducerEpoch(producerEpoch);
this.lastTimestamp = timestamp;
}
public void maybeUpdateProducerEpoch(Short producerEpoch) {
if (this.producerEpoch == null || !this.producerEpoch.equals(producerEpoch)) {
this.producerEpoch = producerEpoch;
}
}
public void update(ProducerStateEntry nextEntry) {
maybeUpdateProducerEpoch(nextEntry.producerEpoch);
this.currentTxnFirstOffset = nextEntry.currentTxnFirstOffset;
this.lastTimestamp = nextEntry.lastTimestamp;
}
public static ProducerStateEntry empty(long producerId){
return new ProducerStateEntry(producerId,
RecordBatch.NO_PRODUCER_EPOCH, -1, RecordBatch.NO_TIMESTAMP, Optional.empty());
}
void writeTo(ByteArrayStream stream) throws IOException {
stream.writeLong(producerId);
stream.writeShort(producerEpoch);
stream.writeInt(coordinatorEpoch);
stream.writeLong(lastTimestamp);
stream.writeLong(currentTxnFirstOffset.orElse(-1L));
}
static ProducerStateEntry readFrom(DataInputStream stream) throws IOException {
final var producerId = stream.readLong();
final var producerEpoch = stream.readShort();
final var coordinatorEpoch = stream.readInt();
final var lastTimestamp = stream.readLong();
final var currentTxnFirstOffset = stream.readLong();
return new ProducerStateEntry(producerId, producerEpoch, coordinatorEpoch, lastTimestamp,
(currentTxnFirstOffset != -1L) ? Optional.of(currentTxnFirstOffset) : Optional.empty());
}
}