All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.streamnative.pulsar.handlers.kop.storage.TxnMetadata Maven / Gradle / Ivy

There is a newer version: 4.0.0.4
Show newest version
/**
 * 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 com.google.common.annotations.VisibleForTesting;
import java.io.DataInputStream;
import java.io.IOException;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import org.apache.bookkeeper.mledger.Position;
import org.apache.bookkeeper.mledger.impl.PositionImpl;

@AllArgsConstructor
@EqualsAndHashCode
public final class TxnMetadata {
    public static final int SIZE = 8 + 8 + 8 + 8 * 2;

    final long producerId;
    final long firstOffset;
    // This field is Option[Long] in Kafka and only used for replication. However, it's never used in KSN. It's not
    // removed just for compatibility of the producer state snapshot. Additional, Kafka does not serialize TxnMetadata
    // when taking snapshot.
    final long lastOffset;
    volatile Position firstPosition;

    @VisibleForTesting
    public long firstOffset() {
        return firstOffset;
    }

    @Override
    public String toString() {
        return String.format("TxnMetadata(producerId=%d, firstOffset=%d, lastOffset=%d, firstPosition=%s",
            producerId, firstOffset, lastOffset, (firstPosition == null) ? "null" : firstPosition.toString());
    }

    void writeTo(ByteArrayStream stream) throws IOException {
        stream.writeLong(producerId);
        stream.writeLong(firstOffset);
        stream.writeLong(lastOffset);
        if (firstPosition != null) {
            stream.writeLong(firstPosition.getLedgerId());
            stream.writeLong(firstPosition.getEntryId());
        } else {
            stream.writeLong(-1L);
            stream.writeLong(-1L);
        }
    }

    static TxnMetadata readFrom(DataInputStream stream) throws IOException {
        final var producerId = stream.readLong();
        final var firstOffset = stream.readLong();
        final var lastOffset = stream.readLong();
        final var ledgerId = stream.readLong();
        final var entryId = stream.readLong();
        final PositionImpl firstPosition;
        if (ledgerId != -1L && entryId != -1L) {
            firstPosition = new PositionImpl(ledgerId, entryId);
        } else {
            firstPosition = null;
        }
        return new TxnMetadata(producerId, firstOffset, lastOffset, firstPosition);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy