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

com.aliyun.datahub.client.impl.DatahubClientPbImpl Maven / Gradle / Ivy

The newest version!
package com.aliyun.datahub.client.impl;

import com.aliyun.datahub.client.auth.Account;
import com.aliyun.datahub.client.common.DatahubConfig;
import com.aliyun.datahub.client.common.DatahubConstant;
import com.aliyun.datahub.client.exception.InvalidParameterException;
import com.aliyun.datahub.client.http.HttpConfig;
import com.aliyun.datahub.client.impl.request.protobuf.GetRecordsRequestPB;
import com.aliyun.datahub.client.impl.request.protobuf.PutRecordsRequestPB;
import com.aliyun.datahub.client.impl.schemaregistry.SchemaRegistryFactory;
import com.aliyun.datahub.client.metircs.ClientMetrics;
import com.aliyun.datahub.client.metircs.MetricType;
import com.aliyun.datahub.client.model.*;
import com.aliyun.datahub.client.model.protobuf.GetRecordsResultPB;
import com.aliyun.datahub.client.model.protobuf.PutRecordsResultPB;
import com.aliyun.datahub.client.util.FormatUtils;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Timer;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DatahubClientPbImpl extends DatahubClientJsonImpl {
    DatahubClientPbImpl(String endpoint, Account account, DatahubConfig datahubConfig, HttpConfig httpConfig,
                               String userAgent, SchemaRegistryFactory schemaRegistryFactory) {
        super(endpoint, account, datahubConfig, httpConfig, userAgent, schemaRegistryFactory);
    }

    @Override
    public PutRecordsResult putRecords(String projectName, String topicName, final List records) {
        FormatUtils.checkProjectName(projectName);
        FormatUtils.checkTopicName(topicName);
        if (records == null || records.isEmpty()) {
            throw new InvalidParameterException("Records is null or empty");
        }

        final PutRecordsRequestPB request = new PutRecordsRequestPB().setRecords(records);
        String metricKey = ClientMetrics.genMetricKey(projectName, topicName);
        Timer.Context putLatency = METRIC_PROXY.getTimerContext(MetricType.PUT_RECORD_LATENCY.getName(), metricKey);
        try {
            PutRecordsResultPB result = callWrapper(getService().putPbRecords(projectName, topicName, request));
            if (result != null) {
                if (result.getFailedRecordCount() > 0) {
                    List failedRecords = new ArrayList<>();
                    for (PutErrorEntry errorEntry : result.getPutErrorEntries()) {
                        failedRecords.add(request.getRecords().get(errorEntry.getIndex()));
                    }
                    result.setFailedRecords(failedRecords);
                }

                Meter putQps = METRIC_PROXY.getMeter(MetricType.PUT_RECORD_QPS.getName(), metricKey);
                Meter putRps = METRIC_PROXY.getMeter(MetricType.PUT_RECORD_RPS.getName(), metricKey);
                if (putQps != null) {
                    putQps.mark(1);
                }

                if (putRps != null) {
                    putRps.mark(records.size() - result.getFailedRecordCount());
                }
            }
            return result;
        } finally {
            if (putLatency != null) {
                putLatency.stop();
            }
        }
    }

    @Override
    public PutRecordsByShardResult putRecordsByShard(String projectName, String topicName, String shardId, List records) {
        FormatUtils.checkProjectName(projectName);
        FormatUtils.checkTopicName(topicName);
        FormatUtils.checkShardId(shardId);

        if (records == null || records.isEmpty()) {
            throw new InvalidParameterException("Records is null or empty");
        }

        final PutRecordsRequestPB request = new PutRecordsRequestPB().setRecords(records);

        String metricKey = ClientMetrics.genMetricKey(projectName, topicName);
        Timer.Context putLatency = METRIC_PROXY.getTimerContext(MetricType.PUT_RECORD_LATENCY.getName(), metricKey);
        try {
            PutRecordsByShardResult result = callWrapper(getService().putPbRecordsByShard(projectName,
                    topicName, shardId, request));
            if (result != null) {
                Meter putQps = METRIC_PROXY.getMeter(MetricType.PUT_RECORD_QPS.getName(), metricKey);
                Meter putRps = METRIC_PROXY.getMeter(MetricType.PUT_RECORD_RPS.getName(), metricKey);

                if (putQps != null) {
                    putQps.mark(1);
                }

                if (putRps != null) {
                    putRps.mark(records.size());
                }
            }
            return result;
        } finally {
            if (putLatency != null) {
                putLatency.stop();
            }
        }
    }

    @Override
    public GetRecordsResult getRecords(String projectName, String topicName, String shardId, String cursor, int limit) {
        return getRecords(projectName, topicName, shardId, null, cursor, limit);
    }

    @Override
    public GetRecordsResult getRecords(String projectName, String topicName, final String shardId, final RecordSchema schema, String cursor, int limit) {
        FormatUtils.checkProjectName(projectName);
        FormatUtils.checkTopicName(topicName);

        if (StringUtils.isEmpty(cursor)) {
            throw new InvalidParameterException("Cursor format is invalid");
        }

        limit = Math.max(MIN_FETCH_SIZE, limit);
        limit = Math.min(MAX_FETCH_SIZE, limit);

        final GetRecordsRequestPB request = new GetRecordsRequestPB().setCursor(cursor).setLimit(limit);

        String metricKey = ClientMetrics.genMetricKey(projectName, topicName);
        Timer.Context getLatency = METRIC_PROXY.getTimerContext(MetricType.GET_RECORD_LATENCY.getName(), metricKey);

        try {
            GetRecordsResultPB result = callWrapper(getService().getPBRecords(projectName,
                    topicName, shardId, request));

            if (result != null) {
                result.internalSetSchema(schema);
                result.internalSetShardId(shardId);

                Meter getQps = METRIC_PROXY.getMeter(MetricType.GET_RECORD_QPS.getName(), metricKey);
                Meter getRps = METRIC_PROXY.getMeter(MetricType.GET_RECORD_RPS.getName(), metricKey);
                if (getQps != null) {
                    getQps.mark(1);
                }
                if (getRps != null) {
                    getRps.mark(result.getRecordCount());
                }
            }
            return result;
        } finally {
            if (getLatency != null) {
                getLatency.stop();
            }
        }
    }

    @Override
    public PutRecordsByShardResult putRecordsByShard(String projectName, String topicName, String shardId, final List records, String token) {
        FormatUtils.checkProjectName(projectName);
        FormatUtils.checkTopicName(topicName);
        FormatUtils.checkShardId(shardId);

        if (records == null || records.isEmpty()) {
            throw new InvalidParameterException("Records is null or empty");
        }

        if (StringUtils.isEmpty(token)) {
            throw new InvalidParameterException("Token is empty");
        }

        Map headers = new HashMap<>();
        if (!StringUtils.isEmpty(token)) {
            headers.put(DatahubConstant.X_DATAHUB_ACCESS_TOKEN, token);
        }

        final PutRecordsRequestPB request = new PutRecordsRequestPB().setRecords(records);

        String metricKey = ClientMetrics.genMetricKey(projectName, topicName);
        Timer.Context putLatency = METRIC_PROXY.getTimerContext(MetricType.PUT_RECORD_LATENCY.getName(), metricKey);
        try {
            PutRecordsByShardResult result = callWrapper(getService().putPbRecordsByShard(projectName,
                    topicName, shardId, request, headers));
            if (result != null) {
                Meter putQps = METRIC_PROXY.getMeter(MetricType.PUT_RECORD_QPS.getName(), metricKey);
                Meter putRps = METRIC_PROXY.getMeter(MetricType.PUT_RECORD_RPS.getName(), metricKey);
                if (putQps != null) {
                    putQps.mark(1);
                }

                if (putRps != null) {
                    putRps.mark(records.size());
                }
            }
            return result;
        } finally {
            if (putLatency != null) {
                putLatency.stop();
            }
        }
    }

    @Override
    public GetRecordsResult getRecords(String projectName, String topicName, String shardId, String cursor, int limit, String subId, String filter, String token) {
        return getRecords(projectName, topicName, shardId, null, cursor, limit, subId, filter, token);
    }

    @Override
    public GetRecordsResult getRecords(String projectName, String topicName, final String shardId, final RecordSchema schema, String cursor, int limit, String subId, String filter, String token) {
        FormatUtils.checkProjectName(projectName);
        FormatUtils.checkTopicName(topicName);
        if (StringUtils.isEmpty(cursor)) {
            throw new InvalidParameterException("Cursor format is invalid");
        }

        if (StringUtils.isEmpty(subId)) {
            throw new InvalidParameterException("SubId is empty");
        }

        if (StringUtils.isEmpty(token)) {
            throw new InvalidParameterException("Token is empty");
        }

        limit = Math.max(MIN_FETCH_SIZE, limit);
        limit = Math.min(MAX_FETCH_SIZE, limit);

        Map headers = new HashMap<>();
        headers.put(DatahubConstant.X_DATAHUB_SUB_ID, subId);
        headers.put(DatahubConstant.X_DATAHUB_ACCESS_TOKEN, token);

        final GetRecordsRequestPB request = new GetRecordsRequestPB().setCursor(cursor).setLimit(limit);
        if (!StringUtils.isEmpty(filter)) {
            request.setFilter(filter);
        }

        String metricKey = ClientMetrics.genMetricKey(projectName, topicName, subId);

        Timer.Context getLatency = METRIC_PROXY.getTimerContext(MetricType.GET_RECORD_LATENCY.getName(), metricKey);
        try {
            GetRecordsResultPB result = callWrapper(getService().getPBRecords(projectName,
                    topicName, shardId, request, headers));

            if (result != null) {
                result.internalSetSchema(schema);
                result.internalSetShardId(shardId);

                Meter getQps = METRIC_PROXY.getMeter(MetricType.GET_RECORD_QPS.getName(), metricKey);
                Meter getRps = METRIC_PROXY.getMeter(MetricType.GET_RECORD_RPS.getName(), metricKey);
                if (getQps != null) {
                    getQps.mark(1);
                }
                if (getRps != null) {
                    getRps.mark(result.getRecordCount());
                }
            }
            return result;
        } finally {
            if (getLatency != null) {
                getLatency.stop();
            }
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy