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

com.taobao.drc.clusterclient.partition.BaseCheckpoint Maven / Gradle / Ivy

The newest version!
package com.taobao.drc.clusterclient.partition;

import org.apache.commons.lang3.StringUtils;

/**
 * @author yangyang
 * @since 2017/7/6
 */
public class BaseCheckpoint {
    private static final int FIELD_NUM = 6;
    private static final int IP = 0;
    private static final int PORT = 1;
    private static final int FILE = 2;
    private static final int OFFSET = 3;
    private static final int TMSP = 4;
    private static final int MSG_ID = 5;

    private final String[] list;
    private final IPartition partition;
    // used to manage the uncommitted checkpoints
    private BaseCheckpoint prev;
    private BaseCheckpoint next;
    private boolean acked = false;

    public BaseCheckpoint(IPartition partition) {
        this.list = new String[FIELD_NUM];
        this.partition = partition;
    }

    public BaseCheckpoint(final String checkpoint, IPartition partition) {
        this.list = parseCheckpoint(checkpoint);
        this.partition = partition;
    }

    private String[] parseCheckpoint(final String checkpoint) {
        String[] segs = checkpoint.split(":");
        if (segs.length != FIELD_NUM) {
            throw new IllegalArgumentException("Invalid checkpoint [" + checkpoint + "]");
        }
        String[] ret = new String[FIELD_NUM];
        for (int i = 0; i < segs.length; i++) {
            if (segs[i] != null && !segs[i].isEmpty()) {
                ret[i] = segs[i];
            }
        }
        return ret;
    }

    public final String getIp() {
        return list[IP];
    }

    public final String getPort() {
        return list[PORT];
    }

    public final String getInstance() {
        if (list[IP] != null && list[PORT] != null) {
            return list[IP] + "-" + list[PORT];
        }
        return null;
    }

    public void setInstance(final String instance) {
        String[] ins = StringUtils.split(instance, '-');
        list[IP] = ins[0];
        list[PORT] = ins[1];
    }

    public final String getFile() {
        return list[FILE];
    }

    public final String getFileOffset() {
        return list[OFFSET];
    }

    public void setFilePosition(final String pos) {
        if(pos!=null) {
            String[] fileposition = StringUtils.split(pos, '@');
            list[FILE] = fileposition[1];
            list[OFFSET] = fileposition[0];
        }
    }

    public final String getFilePosition() {
        if (list[OFFSET] != null && list[FILE] != null) {
            return list[OFFSET] + "@" + list[FILE];
        }
        return null;
    }

    public final String getTimestamp() {
        return list[TMSP];
    }

    public void setTimestamp(final String timestamp) {
        list[TMSP] = timestamp;
    }

    public final String getId() {
        return list[MSG_ID];
    }

    public void setId(final String id) {
        list[MSG_ID] = id;
    }

    public void ackAsConsumed() {
        if (this.partition != null) {
            this.partition.ackAsConsumed(this);
        }
    }

    // the following getters and setters are used by checkpoint manager only

    BaseCheckpoint getPrev() {
        return prev;
    }

    void setPrev(BaseCheckpoint prev) {
        this.prev = prev;
    }

    BaseCheckpoint getNext() {
        return next;
    }

    void setNext(BaseCheckpoint next) {
        this.next = next;
    }

    boolean isAcked() {
        return acked;
    }

    void ack() {
        this.acked = true;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < list.length; i++) {
            builder.append(list[i]);
            if (i != list.length - 1)
                builder.append(':');
        }
        return builder.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy