EndpointInventory.java 5.0 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

import java.util.*;
import lombok.*;
import org.apache.skywalking.oap.server.core.Const;
彭勇升 pengys 已提交
24
import org.apache.skywalking.oap.server.core.register.annotation.InventoryType;
25 26
import org.apache.skywalking.oap.server.core.remote.annotation.StreamData;
import org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData;
彭勇升 pengys 已提交
27
import org.apache.skywalking.oap.server.core.source.Scope;
28 29 30 31 32 33
import org.apache.skywalking.oap.server.core.storage.StorageBuilder;
import org.apache.skywalking.oap.server.core.storage.annotation.*;

/**
 * @author peng-yongsheng
 */
彭勇升 pengys 已提交
34
@InventoryType(scope = Scope.Endpoint)
35
@StreamData
36
@StorageEntity(name = EndpointInventory.MODEL_NAME, builder = EndpointInventory.Builder.class)
37
public class EndpointInventory extends RegisterSource {
38

39 40
    public static final String MODEL_NAME = "endpoint_inventory";

41 42 43 44 45
    private static final String SERVICE_ID = "service_id";
    private static final String NAME = "name";
    private static final String SRC_SPAN_TYPE = "src_span_type";

    @Setter @Getter @Column(columnName = SERVICE_ID) private int serviceId;
46
    @Setter @Getter @Column(columnName = NAME, matchQuery = true) private String name = Const.EMPTY_STRING;
47 48
    @Setter @Getter @Column(columnName = SRC_SPAN_TYPE) private int srcSpanType;

49 50 51 52
    public static String buildId(int serviceId, String endpointName) {
        return serviceId + Const.ID_SPLIT + endpointName;
    }

53
    @Override public String id() {
54
        return buildId(serviceId, name);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    }

    @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;

72
        EndpointInventory source = (EndpointInventory)obj;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        if (serviceId != source.getServiceId())
            return false;
        if (name.equals(source.getName()))
            return false;

        return true;
    }

    @Override public RemoteData.Builder serialize() {
        RemoteData.Builder remoteBuilder = RemoteData.newBuilder();
        remoteBuilder.setDataIntegers(0, getSequence());
        remoteBuilder.setDataIntegers(1, serviceId);
        remoteBuilder.setDataIntegers(2, srcSpanType);

        remoteBuilder.setDataLongs(0, getRegisterTime());
        remoteBuilder.setDataLongs(1, getHeartbeatTime());

        remoteBuilder.setDataStrings(0, name);
        return remoteBuilder;
    }

    @Override public void deserialize(RemoteData remoteData) {
        setSequence(remoteData.getDataIntegers(0));
        setServiceId(remoteData.getDataIntegers(1));
        setSrcSpanType(remoteData.getDataIntegers(2));

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

102
        setName(remoteData.getDataStrings(0));
103 104
    }

105 106 107
    public static class Builder implements StorageBuilder<EndpointInventory> {

        @Override public EndpointInventory map2Data(Map<String, Object> dbMap) {
108 109 110 111 112 113 114 115
            EndpointInventory inventory = new EndpointInventory();
            inventory.setSequence((Integer)dbMap.get(SEQUENCE));
            inventory.setServiceId((Integer)dbMap.get(SERVICE_ID));
            inventory.setName((String)dbMap.get(NAME));
            inventory.setSrcSpanType((Integer)dbMap.get(SRC_SPAN_TYPE));
            inventory.setRegisterTime((Long)dbMap.get(REGISTER_TIME));
            inventory.setHeartbeatTime((Long)dbMap.get(HEARTBEAT_TIME));
            return inventory;
116 117
        }

118
        @Override public Map<String, Object> data2Map(EndpointInventory storageData) {
119 120 121 122 123 124 125 126 127 128 129
            Map<String, Object> map = new HashMap<>();
            map.put(SEQUENCE, storageData.getSequence());
            map.put(SERVICE_ID, storageData.getServiceId());
            map.put(NAME, storageData.getName());
            map.put(SRC_SPAN_TYPE, storageData.getSrcSpanType());
            map.put(REGISTER_TIME, storageData.getRegisterTime());
            map.put(HEARTBEAT_TIME, storageData.getHeartbeatTime());
            return map;
        }
    }
}