|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.inlong.sort.base.metric; |
| 19 | + |
| 20 | +import org.apache.inlong.audit.AuditReporterImpl; |
| 21 | + |
| 22 | +import lombok.extern.slf4j.Slf4j; |
| 23 | +import org.apache.commons.collections.CollectionUtils; |
| 24 | +import org.apache.flink.table.data.RowData; |
| 25 | +import org.apache.flink.types.RowKind; |
| 26 | + |
| 27 | +import java.io.Serializable; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static org.apache.inlong.audit.consts.ConfigConstants.DEFAULT_AUDIT_TAG; |
| 33 | +import static org.apache.inlong.common.constant.Constants.DEFAULT_AUDIT_VERSION; |
| 34 | +import static org.apache.inlong.sort.base.Constants.GROUP_ID; |
| 35 | +import static org.apache.inlong.sort.base.Constants.STREAM_ID; |
| 36 | +import static org.apache.inlong.sort.base.util.CalculateObjectSizeUtils.getDataSize; |
| 37 | + |
| 38 | +@Slf4j |
| 39 | +public class CdcExactlyMetric implements Serializable, SourceMetricsReporter { |
| 40 | + |
| 41 | + private final Map<String, String> labels; |
| 42 | + private final Map<RowKind, Integer> auditKeyMap; |
| 43 | + private final String groupId; |
| 44 | + private final String streamId; |
| 45 | + |
| 46 | + private AuditReporterImpl auditReporter; |
| 47 | + private Long currentCheckpointId = 0L; |
| 48 | + private Long lastCheckpointId = 0L; |
| 49 | + |
| 50 | + public CdcExactlyMetric(MetricOption option) { |
| 51 | + this.labels = option.getLabels(); |
| 52 | + this.groupId = labels.get(GROUP_ID); |
| 53 | + this.streamId = labels.get(STREAM_ID); |
| 54 | + this.auditKeyMap = new HashMap<>(); |
| 55 | + |
| 56 | + if (option.getIpPorts().isPresent()) { |
| 57 | + auditReporter = new AuditReporterImpl(); |
| 58 | + auditReporter.setAutoFlush(false); |
| 59 | + auditReporter.setAuditProxy(option.getIpPortSet()); |
| 60 | + List<Integer> auditKeys = option.getInlongAuditKeys(); |
| 61 | + |
| 62 | + if (CollectionUtils.isEmpty(auditKeys)) { |
| 63 | + log.warn("inlong audit keys is empty"); |
| 64 | + } else if (auditKeys.size() == 1) { |
| 65 | + auditKeyMap.put(RowKind.INSERT, auditKeys.get(0)); |
| 66 | + log.warn("only the insert audit key is set, the update and delete audit will be ignored"); |
| 67 | + } else if (auditKeys.size() == 4) { |
| 68 | + auditKeyMap.put(RowKind.INSERT, auditKeys.get(0)); |
| 69 | + auditKeyMap.put(RowKind.UPDATE_BEFORE, auditKeys.get(1)); |
| 70 | + auditKeyMap.put(RowKind.UPDATE_AFTER, auditKeys.get(2)); |
| 71 | + auditKeyMap.put(RowKind.DELETE, auditKeys.get(3)); |
| 72 | + } else { |
| 73 | + throw new IllegalArgumentException("audit key size must be 1 or 4"); |
| 74 | + } |
| 75 | + } |
| 76 | + log.info("CdcExactlyMetric init, groupId: {}, streamId: {}, audit key: {}", groupId, streamId, auditKeyMap); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void outputMetricsWithEstimate(Object data, long dataTime) { |
| 81 | + long size = getDataSize(data); |
| 82 | + if (data instanceof RowData) { |
| 83 | + RowData rowData = (RowData) data; |
| 84 | + RowKind rowKind = rowData.getRowKind(); |
| 85 | + int key = auditKeyMap.get(rowKind); |
| 86 | + outputMetrics(1, size, dataTime, key); |
| 87 | + } else { |
| 88 | + outputMetrics(1, size, dataTime, auditKeyMap.get(RowKind.INSERT)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void outputMetrics(long rowCountSize, long rowDataSize, long dataTime, int key) { |
| 93 | + if (auditReporter != null) { |
| 94 | + auditReporter.add( |
| 95 | + this.currentCheckpointId, |
| 96 | + key, |
| 97 | + DEFAULT_AUDIT_TAG, |
| 98 | + groupId, |
| 99 | + streamId, |
| 100 | + dataTime, |
| 101 | + rowCountSize, |
| 102 | + rowDataSize, |
| 103 | + DEFAULT_AUDIT_VERSION); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public void updateLastCheckpointId(Long checkpointId) { |
| 108 | + lastCheckpointId = checkpointId; |
| 109 | + } |
| 110 | + |
| 111 | + public void updateCurrentCheckpointId(Long checkpointId) { |
| 112 | + currentCheckpointId = checkpointId; |
| 113 | + } |
| 114 | + |
| 115 | + public void flushAudit() { |
| 116 | + if (auditReporter != null) { |
| 117 | + auditReporter.flush(lastCheckpointId); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments