ServiceInstanceInventory.java 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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.
 *
 */

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

21 22
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
23 24
import java.util.*;
import lombok.*;
25 26 27 28 29 30
import org.apache.skywalking.oap.server.core.Const;
import org.apache.skywalking.oap.server.core.register.annotation.InventoryType;
import org.apache.skywalking.oap.server.core.remote.annotation.StreamData;
import org.apache.skywalking.oap.server.core.remote.grpc.proto.RemoteData;
import org.apache.skywalking.oap.server.core.source.Scope;
import org.apache.skywalking.oap.server.core.storage.StorageBuilder;
31
import org.apache.skywalking.oap.server.core.storage.annotation.*;
32
import org.apache.skywalking.oap.server.library.util.BooleanUtils;
33
import org.apache.skywalking.oap.server.library.util.StringUtils;
34 35 36 37 38 39

/**
 * @author peng-yongsheng
 */
@InventoryType(scope = Scope.ServiceInstance)
@StreamData
40
@StorageEntity(name = ServiceInstanceInventory.MODEL_NAME, builder = ServiceInstanceInventory.Builder.class, deleteHistory = false)
41 42 43 44 45
public class ServiceInstanceInventory extends RegisterSource {

    public static final String MODEL_NAME = "service_instance_inventory";

    public static final String NAME = "name";
46
    public static final String INSTANCE_UUID = "instance_uuid";
47
    public static final String SERVICE_ID = "service_id";
48 49
    private static final String IS_ADDRESS = "is_address";
    private static final String ADDRESS_ID = "address_id";
50 51 52 53
    public static final String OS_NAME = "os_name";
    public static final String HOST_NAME = "host_name";
    public static final String PROCESS_NO = "process_no";
    public static final String IPV4S = "ipv4s";
54
    public static final String LANGUAGE = "language";
55

56 57 58
    @Setter @Getter @Column(columnName = INSTANCE_UUID, matchQuery = true)
    private String instanceUUID = Const.EMPTY_STRING;
    @Setter @Getter @Column(columnName = NAME) private String name = Const.EMPTY_STRING;
59
    @Setter @Getter @Column(columnName = SERVICE_ID) private int serviceId;
60
    @Setter @Getter @Column(columnName = LANGUAGE) private int language;
61 62 63 64 65
    @Setter @Getter @Column(columnName = IS_ADDRESS) private int isAddress;
    @Setter @Getter @Column(columnName = ADDRESS_ID) private int addressId;
    @Setter @Getter @Column(columnName = OS_NAME) private String osName;
    @Setter @Getter @Column(columnName = HOST_NAME) private String hostName;
    @Setter @Getter @Column(columnName = PROCESS_NO) private int processNo;
66
    @Setter @Getter @Column(columnName = IPV4S) private String ipv4s;
67

68 69
    public static String buildId(int serviceId, String uuid) {
        return serviceId + Const.ID_SPLIT + uuid + Const.ID_SPLIT + BooleanUtils.FALSE + Const.ID_SPLIT + Const.NONE;
70 71 72 73 74 75 76 77 78 79
    }

    public static String buildId(int serviceId, int addressId) {
        return serviceId + Const.ID_SPLIT + BooleanUtils.TRUE + Const.ID_SPLIT + addressId;
    }

    @Override public String id() {
        if (BooleanUtils.TRUE == isAddress) {
            return buildId(serviceId, addressId);
        } else {
80
            return buildId(serviceId, instanceUUID);
81 82 83 84 85 86
        }
    }

    @Override public int hashCode() {
        int result = 17;
        result = 31 * result + serviceId;
87
        result = 31 * result + instanceUUID.hashCode();
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        result = 31 * result + isAddress;
        result = 31 * result + addressId;
        return result;
    }

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

        ServiceInstanceInventory source = (ServiceInstanceInventory)obj;
        if (serviceId != source.getServiceId())
            return false;
104
        if (!instanceUUID.equals(source.getInstanceUUID()))
105 106 107 108 109 110 111 112 113 114 115
            return false;
        if (isAddress != source.getIsAddress())
            return false;
        if (addressId != source.getAddressId())
            return false;

        return true;
    }

    @Override public RemoteData.Builder serialize() {
        RemoteData.Builder remoteBuilder = RemoteData.newBuilder();
116 117 118 119 120 121 122 123 124 125
        remoteBuilder.addDataIntegers(getSequence());
        remoteBuilder.addDataIntegers(serviceId);
        remoteBuilder.addDataIntegers(language);
        remoteBuilder.addDataIntegers(isAddress);
        remoteBuilder.addDataIntegers(addressId);
        remoteBuilder.addDataIntegers(processNo);

        remoteBuilder.addDataLongs(getRegisterTime());
        remoteBuilder.addDataLongs(getHeartbeatTime());

126 127 128 129 130
        remoteBuilder.addDataStrings(StringUtils.getOrDefault(name, Const.EMPTY_STRING));
        remoteBuilder.addDataStrings(StringUtils.getOrDefault(osName, Const.EMPTY_STRING));
        remoteBuilder.addDataStrings(StringUtils.getOrDefault(hostName, Const.EMPTY_STRING));
        remoteBuilder.addDataStrings(StringUtils.getOrDefault(ipv4s, Const.EMPTY_STRING));
        remoteBuilder.addDataStrings(StringUtils.getOrDefault(instanceUUID, Const.EMPTY_STRING));
131 132 133 134 135 136
        return remoteBuilder;
    }

    @Override public void deserialize(RemoteData remoteData) {
        setSequence(remoteData.getDataIntegers(0));
        setServiceId(remoteData.getDataIntegers(1));
137 138 139 140
        setLanguage(remoteData.getDataIntegers(2));
        setIsAddress(remoteData.getDataIntegers(3));
        setAddressId(remoteData.getDataIntegers(4));
        setProcessNo(remoteData.getDataIntegers(5));
141 142 143 144 145 146 147

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

        setName(remoteData.getDataStrings(0));
        setOsName(remoteData.getDataStrings(1));
        setHostName(remoteData.getDataStrings(2));
148
        setIpv4s(remoteData.getDataStrings(3));
149
        setInstanceUUID(remoteData.getDataStrings(4));
150 151
    }

wu-sheng's avatar
wu-sheng 已提交
152 153 154 155
    @Override public int remoteHashCode() {
        return 0;
    }

156 157 158 159 160 161
    public static class Builder implements StorageBuilder<ServiceInstanceInventory> {

        @Override public ServiceInstanceInventory map2Data(Map<String, Object> dbMap) {
            ServiceInstanceInventory inventory = new ServiceInstanceInventory();
            inventory.setSequence((Integer)dbMap.get(SEQUENCE));
            inventory.setServiceId((Integer)dbMap.get(SERVICE_ID));
162
            inventory.setLanguage((Integer)dbMap.get(LANGUAGE));
163 164 165 166 167 168 169 170 171 172
            inventory.setIsAddress((Integer)dbMap.get(IS_ADDRESS));
            inventory.setAddressId((Integer)dbMap.get(ADDRESS_ID));
            inventory.setProcessNo((Integer)dbMap.get(PROCESS_NO));

            inventory.setRegisterTime((Long)dbMap.get(REGISTER_TIME));
            inventory.setHeartbeatTime((Long)dbMap.get(HEARTBEAT_TIME));

            inventory.setName((String)dbMap.get(NAME));
            inventory.setOsName((String)dbMap.get(OS_NAME));
            inventory.setHostName((String)dbMap.get(HOST_NAME));
173
            inventory.setIpv4s((String)dbMap.get(IPV4S));
174
            inventory.setInstanceUUID((String)dbMap.get(INSTANCE_UUID));
175 176 177 178 179 180 181
            return inventory;
        }

        @Override public Map<String, Object> data2Map(ServiceInstanceInventory storageData) {
            Map<String, Object> map = new HashMap<>();
            map.put(SEQUENCE, storageData.getSequence());
            map.put(SERVICE_ID, storageData.getServiceId());
182
            map.put(LANGUAGE, storageData.getLanguage());
183 184 185 186 187 188 189 190 191 192
            map.put(IS_ADDRESS, storageData.getIsAddress());
            map.put(ADDRESS_ID, storageData.getAddressId());
            map.put(PROCESS_NO, storageData.getProcessNo());

            map.put(REGISTER_TIME, storageData.getRegisterTime());
            map.put(HEARTBEAT_TIME, storageData.getHeartbeatTime());

            map.put(NAME, storageData.getName());
            map.put(OS_NAME, storageData.getOsName());
            map.put(HOST_NAME, storageData.getHostName());
193
            map.put(IPV4S, storageData.getIpv4s());
194
            map.put(INSTANCE_UUID, storageData.getInstanceUUID());
195 196 197
            return map;
        }
    }
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

    public static class AgentOsInfo {
        @Setter @Getter private String osName;
        @Setter @Getter private String hostname;
        @Setter @Getter private int processNo;
        @Getter private List<String> ipv4s;

        public AgentOsInfo() {
            this.ipv4s = new ArrayList<>();
        }

        public static String ipv4sSerialize(List<String> ipv4) {
            Gson gson = new Gson();
            return gson.toJson(ipv4);
        }

        public static List<String> ipv4sDeserialize(String ipv4s) {
            Gson gson = new Gson();
            return gson.fromJson(ipv4s, new TypeToken<List<String>>() {
            }.getType());
        }
    }
220
}