EndpointInventory.java 5.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 */

19
package org.apache.skywalking.oap.server.core.register;
20

21 22 23 24
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
25
import org.apache.skywalking.oap.server.core.Const;
彭勇升 pengys 已提交
26
import org.apache.skywalking.oap.server.core.register.annotation.InventoryType;
27 28
import org.apache.skywalking.oap.server.core.remote.annotation.StreamData;
import org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData;
彭勇升 pengys 已提交
29
import org.apache.skywalking.oap.server.core.source.Scope;
30
import org.apache.skywalking.oap.server.core.storage.StorageBuilder;
31 32 33
import org.apache.skywalking.oap.server.core.storage.annotation.Column;
import org.apache.skywalking.oap.server.core.storage.annotation.StorageEntity;
import org.apache.skywalking.oap.server.library.util.StringUtils;
34 35 36 37

/**
 * @author peng-yongsheng
 */
彭勇升 pengys 已提交
38
@InventoryType(scope = Scope.Endpoint)
39
@StreamData
40
@StorageEntity(name = EndpointInventory.MODEL_NAME, builder = EndpointInventory.Builder.class, deleteHistory = false)
41
public class EndpointInventory extends RegisterSource {
42

43 44
    public static final String MODEL_NAME = "endpoint_inventory";

45 46
    public static final String SERVICE_ID = "service_id";
    public static final String NAME = "name";
47
    public static final String DETECT_POINT = "detect_point";
48 49

    @Setter @Getter @Column(columnName = SERVICE_ID) private int serviceId;
50
    @Setter @Getter @Column(columnName = NAME, matchQuery = true) private String name = Const.EMPTY_STRING;
51
    @Setter @Getter @Column(columnName = DETECT_POINT) private int detectPoint;
52

53 54 55 56
    public static String buildId(int serviceId, String endpointName) {
        return serviceId + Const.ID_SPLIT + endpointName;
    }

57
    @Override public String id() {
58
        return buildId(serviceId, name);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    }

    @Override public int hashCode() {
        int result = 17;
        result = 31 * result + serviceId;
        result = 31 * result + name.hashCode();
        return result;
    }

    @Override public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;

76
        EndpointInventory source = (EndpointInventory)obj;
77 78
        if (serviceId != source.getServiceId())
            return false;
79
        if (!name.equals(source.getName()))
80 81 82 83 84 85 86
            return false;

        return true;
    }

    @Override public RemoteData.Builder serialize() {
        RemoteData.Builder remoteBuilder = RemoteData.newBuilder();
87 88 89
        remoteBuilder.addDataIntegers(getSequence());
        remoteBuilder.addDataIntegers(serviceId);
        remoteBuilder.addDataIntegers(detectPoint);
90

91 92
        remoteBuilder.addDataLongs(getRegisterTime());
        remoteBuilder.addDataLongs(getHeartbeatTime());
93

94
        remoteBuilder.addDataStrings(StringUtils.getOrDefault(name, Const.EMPTY_STRING));
95 96 97 98 99 100
        return remoteBuilder;
    }

    @Override public void deserialize(RemoteData remoteData) {
        setSequence(remoteData.getDataIntegers(0));
        setServiceId(remoteData.getDataIntegers(1));
101
        setDetectPoint(remoteData.getDataIntegers(2));
102 103 104 105

        setRegisterTime(remoteData.getDataLongs(0));
        setHeartbeatTime(remoteData.getDataLongs(1));

106
        setName(remoteData.getDataStrings(0));
107 108
    }

wu-sheng's avatar
wu-sheng 已提交
109 110 111 112
    @Override public int remoteHashCode() {
        return 0;
    }

113 114 115
    public static class Builder implements StorageBuilder<EndpointInventory> {

        @Override public EndpointInventory map2Data(Map<String, Object> dbMap) {
116 117 118 119
            EndpointInventory inventory = new EndpointInventory();
            inventory.setSequence((Integer)dbMap.get(SEQUENCE));
            inventory.setServiceId((Integer)dbMap.get(SERVICE_ID));
            inventory.setName((String)dbMap.get(NAME));
120
            inventory.setDetectPoint((Integer)dbMap.get(DETECT_POINT));
121 122 123
            inventory.setRegisterTime((Long)dbMap.get(REGISTER_TIME));
            inventory.setHeartbeatTime((Long)dbMap.get(HEARTBEAT_TIME));
            return inventory;
124 125
        }

126
        @Override public Map<String, Object> data2Map(EndpointInventory storageData) {
127 128 129 130
            Map<String, Object> map = new HashMap<>();
            map.put(SEQUENCE, storageData.getSequence());
            map.put(SERVICE_ID, storageData.getServiceId());
            map.put(NAME, storageData.getName());
131
            map.put(DETECT_POINT, storageData.getDetectPoint());
132 133 134 135 136 137
            map.put(REGISTER_TIME, storageData.getRegisterTime());
            map.put(HEARTBEAT_TIME, storageData.getHeartbeatTime());
            return map;
        }
    }
}