ServiceInstanceInventory.java 8.5 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import java.util.*;
import lombok.*;
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;
import org.apache.skywalking.oap.server.core.storage.annotation.*;
import org.apache.skywalking.oap.server.library.util.BooleanUtils;

/**
 * @author peng-yongsheng
 */
@InventoryType(scope = Scope.ServiceInstance)
@StreamData
@StorageEntity(name = ServiceInstanceInventory.MODEL_NAME, builder = ServiceInstanceInventory.Builder.class)
public class ServiceInstanceInventory extends RegisterSource {

    public static final String MODEL_NAME = "service_instance_inventory";

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

    @Setter @Getter @Column(columnName = NAME, matchQuery = true) private String name = Const.EMPTY_STRING;
    @Setter @Getter @Column(columnName = SERVICE_ID) private int serviceId;
56
    @Setter @Getter @Column(columnName = LANGUAGE) private int language;
57 58 59 60 61
    @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;
62
    @Setter @Getter @Column(columnName = IPV4S) private String ipv4s;
63 64 65 66 67 68 69 70 71 72 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 102 103 104 105 106 107 108 109 110 111 112 113

    public static String buildId(int serviceId, String serviceInstanceName) {
        return serviceId + Const.ID_SPLIT + serviceInstanceName + Const.ID_SPLIT + BooleanUtils.FALSE + Const.ID_SPLIT + Const.NONE;
    }

    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 {
            return buildId(serviceId, name);
        }
    }

    @Override public int hashCode() {
        int result = 17;
        result = 31 * result + serviceId;
        result = 31 * result + name.hashCode();
        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;
        if (name.equals(source.getName()))
            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();
        remoteBuilder.setDataIntegers(0, getSequence());
        remoteBuilder.setDataIntegers(1, serviceId);
114 115 116 117
        remoteBuilder.setDataIntegers(2, language);
        remoteBuilder.setDataIntegers(3, isAddress);
        remoteBuilder.setDataIntegers(4, addressId);
        remoteBuilder.setDataIntegers(5, processNo);
118 119 120 121 122 123 124

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

        remoteBuilder.setDataStrings(0, name);
        remoteBuilder.setDataStrings(1, osName);
        remoteBuilder.setDataStrings(2, hostName);
125
        remoteBuilder.setDataStrings(3, ipv4s);
126 127 128 129 130 131
        return remoteBuilder;
    }

    @Override public void deserialize(RemoteData remoteData) {
        setSequence(remoteData.getDataIntegers(0));
        setServiceId(remoteData.getDataIntegers(1));
132 133 134 135
        setLanguage(remoteData.getDataIntegers(2));
        setIsAddress(remoteData.getDataIntegers(3));
        setAddressId(remoteData.getDataIntegers(4));
        setProcessNo(remoteData.getDataIntegers(5));
136 137 138 139 140 141 142

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

        setName(remoteData.getDataStrings(0));
        setOsName(remoteData.getDataStrings(1));
        setHostName(remoteData.getDataStrings(2));
143
        setIpv4s(remoteData.getDataStrings(3));
144 145
    }

wu-sheng's avatar
wu-sheng 已提交
146 147 148 149
    @Override public int remoteHashCode() {
        return 0;
    }

150 151 152 153 154 155
    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));
156
            inventory.setLanguage((Integer)dbMap.get(LANGUAGE));
157 158 159 160 161 162 163 164 165 166
            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));
167
            inventory.setIpv4s((String)dbMap.get(IPV4S));
168 169 170 171 172 173 174
            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());
175
            map.put(LANGUAGE, storageData.getLanguage());
176 177 178 179 180 181 182 183 184 185
            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());
186
            map.put(IPV4S, storageData.getIpv4s());
187 188 189
            return map;
        }
    }
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

    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());
        }
    }
212
}