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

com.taobao.drc.clusterclient.impl.DefaultPartitionImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0.1.beta
Show newest version
package com.taobao.drc.clusterclient.impl;

import com.taobao.drc.clusterclient.clustermanager.PartitionInfo;
import com.taobao.drc.clusterclient.partition.BaseCheckpoint;
import com.taobao.drc.clusterclient.partition.IPartition;
import com.taobao.drc.clusterclient.partition.CheckpointManager;

import java.util.Map;
import java.util.TreeMap;

/**
 * @author yangyang
 * @since 2017/7/6
 */
public class DefaultPartitionImpl implements IPartition {
    public static final String KEY_PARTITION_HEARTBEAT_TIME_IN_SEC = "partition.heartbeat-sec";

    private final String name;
    private final String guid;
    private final String group;
    private final String topic;
    private final Integer partitionId;
    private final Integer partitionNum;
    private final CheckpointManager checkpointManager;
    private volatile long heartbeatTimeInSec = 0;

    public DefaultPartitionImpl(PartitionInfo partitionInfo, Integer bufSize) {
        this(partitionInfo.getPartition(), partitionInfo.getGuid(), partitionInfo.getGroup(), partitionInfo.getTopic(),
                partitionInfo.getPartitionId(), partitionInfo.getPartitionNum(), bufSize);
    }

    public DefaultPartitionImpl(String name, String guid, String group, String topic, Integer partitionId,
                                Integer partitionNum, Integer bufSize) {
        this.name = name;
        this.guid = guid;
        this.group = group;
        this.topic = topic;
        this.partitionId = partitionId;
        this.partitionNum = partitionNum;
        this.checkpointManager = new CheckpointManager(bufSize);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getGuid() {
        return guid;
    }

    @Override
    public String getGroup() {
        return group;
    }

    @Override
    public String getTopic() {
        return topic;
    }

    @Override
    public Integer getPartitionId() {
        return partitionId;
    }

    @Override
    public Integer getPartitionNum() {
        return partitionNum;
    }

    public void bookCheckpoint(C checkpoint) throws InterruptedException {
        checkpointManager.book(checkpoint);
    }

    @Override
    public void ackAsConsumed(C checkpoint) {
        checkpointManager.ack(checkpoint);
    }

    @Override
    public C getCheckpoint() {
        return (C) checkpointManager.getFirstNotAckedOrLatestCheckpoint();
    }

    public void setHeartbeatTimeInSec(long heartbeatTimeInSec) {
        this.heartbeatTimeInSec = heartbeatTimeInSec;
    }

    @Override
    public long getHeartbeatTimeInSec() {
        return heartbeatTimeInSec;
    }

    @Override
    public Map getMetrics() {
        Map map = new TreeMap(checkpointManager.getMetrics());
        map.put(KEY_PARTITION_HEARTBEAT_TIME_IN_SEC, getHeartbeatTimeInSec());
        return map;
    }

    @Override
    public Iterable getCheckpoints() {
        return checkpointManager;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy