提交 974192ec 编写于 作者: wu-sheng's avatar wu-sheng 提交者: 彭勇升 pengys

Keep service instance register based on UUID, but change name to a meaning string. (#1836)

上级 56fbb0d2
......@@ -80,13 +80,13 @@ public class ServiceInstanceInventoryCache implements Service {
return serviceInstanceInventory;
}
public int getServiceInstanceId(int serviceId, String serviceInstanceName) {
Integer serviceInstanceId = serviceInstanceNameCache.getIfPresent(ServiceInstanceInventory.buildId(serviceId, serviceInstanceName));
public int getServiceInstanceId(int serviceId, String uuid) {
Integer serviceInstanceId = serviceInstanceNameCache.getIfPresent(ServiceInstanceInventory.buildId(serviceId, uuid));
if (Objects.isNull(serviceInstanceId) || serviceInstanceId == Const.NONE) {
serviceInstanceId = getCacheDAO().getServiceInstanceId(serviceId, serviceInstanceName);
serviceInstanceId = getCacheDAO().getServiceInstanceId(serviceId, uuid);
if (serviceId != Const.NONE) {
serviceInstanceNameCache.put(ServiceInstanceInventory.buildId(serviceId, serviceInstanceName), serviceInstanceId);
serviceInstanceNameCache.put(ServiceInstanceInventory.buildId(serviceId, uuid), serviceInstanceId);
}
}
return serviceInstanceId;
......
......@@ -20,15 +20,20 @@ package org.apache.skywalking.oap.server.core.register;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.*;
import lombok.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import lombok.Setter;
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.core.storage.annotation.Column;
import org.apache.skywalking.oap.server.core.storage.annotation.StorageEntity;
import org.apache.skywalking.oap.server.library.util.BooleanUtils;
/**
......@@ -42,6 +47,7 @@ public class ServiceInstanceInventory extends RegisterSource {
public static final String MODEL_NAME = "service_instance_inventory";
public static final String NAME = "name";
public static final String INSTANCE_UUID = "instance_uuid";
public static final String SERVICE_ID = "service_id";
private static final String IS_ADDRESS = "is_address";
private static final String ADDRESS_ID = "address_id";
......@@ -51,7 +57,9 @@ public class ServiceInstanceInventory extends RegisterSource {
public static final String IPV4S = "ipv4s";
public static final String LANGUAGE = "language";
@Setter @Getter @Column(columnName = NAME, matchQuery = true) private String name = Const.EMPTY_STRING;
@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;
@Setter @Getter @Column(columnName = SERVICE_ID) private int serviceId;
@Setter @Getter @Column(columnName = LANGUAGE) private int language;
@Setter @Getter @Column(columnName = IS_ADDRESS) private int isAddress;
......@@ -61,8 +69,8 @@ public class ServiceInstanceInventory extends RegisterSource {
@Setter @Getter @Column(columnName = PROCESS_NO) private int processNo;
@Setter @Getter @Column(columnName = IPV4S) private String ipv4s;
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, String uuid) {
return serviceId + Const.ID_SPLIT + uuid + Const.ID_SPLIT + BooleanUtils.FALSE + Const.ID_SPLIT + Const.NONE;
}
public static String buildId(int serviceId, int addressId) {
......@@ -73,14 +81,14 @@ public class ServiceInstanceInventory extends RegisterSource {
if (BooleanUtils.TRUE == isAddress) {
return buildId(serviceId, addressId);
} else {
return buildId(serviceId, name);
return buildId(serviceId, instanceUUID);
}
}
@Override public int hashCode() {
int result = 17;
result = 31 * result + serviceId;
result = 31 * result + name.hashCode();
result = 31 * result + instanceUUID.hashCode();
result = 31 * result + isAddress;
result = 31 * result + addressId;
return result;
......@@ -97,7 +105,7 @@ public class ServiceInstanceInventory extends RegisterSource {
ServiceInstanceInventory source = (ServiceInstanceInventory)obj;
if (serviceId != source.getServiceId())
return false;
if (!name.equals(source.getName()))
if (!instanceUUID.equals(source.getInstanceUUID()))
return false;
if (isAddress != source.getIsAddress())
return false;
......@@ -123,6 +131,7 @@ public class ServiceInstanceInventory extends RegisterSource {
remoteBuilder.setDataStrings(1, osName);
remoteBuilder.setDataStrings(2, hostName);
remoteBuilder.setDataStrings(3, ipv4s);
remoteBuilder.setDataStrings(4, instanceUUID);
return remoteBuilder;
}
......@@ -141,6 +150,7 @@ public class ServiceInstanceInventory extends RegisterSource {
setOsName(remoteData.getDataStrings(1));
setHostName(remoteData.getDataStrings(2));
setIpv4s(remoteData.getDataStrings(3));
setInstanceUUID(remoteData.getDataStrings(4));
}
@Override public int remoteHashCode() {
......@@ -165,6 +175,7 @@ public class ServiceInstanceInventory extends RegisterSource {
inventory.setOsName((String)dbMap.get(OS_NAME));
inventory.setHostName((String)dbMap.get(HOST_NAME));
inventory.setIpv4s((String)dbMap.get(IPV4S));
inventory.setInstanceUUID((String)dbMap.get(INSTANCE_UUID));
return inventory;
}
......@@ -184,6 +195,7 @@ public class ServiceInstanceInventory extends RegisterSource {
map.put(OS_NAME, storageData.getOsName());
map.put(HOST_NAME, storageData.getHostName());
map.put(IPV4S, storageData.getIpv4s());
map.put(INSTANCE_UUID, storageData.getInstanceUUID());
return map;
}
}
......
......@@ -26,7 +26,7 @@ import org.apache.skywalking.oap.server.library.module.Service;
*/
public interface IServiceInstanceInventoryRegister extends Service {
int getOrCreate(int serviceId, String serviceInstanceName, long registerTime,
int getOrCreate(int serviceId, String serviceInstanceName, String uuid, long registerTime,
ServiceInstanceInventory.AgentOsInfo osInfo);
int getOrCreate(int serviceId, int addressId, long registerTime);
......
......@@ -50,18 +50,19 @@ public class ServiceInstanceInventoryRegister implements IServiceInstanceInvento
return serviceInstanceInventoryCache;
}
@Override public int getOrCreate(int serviceId, String serviceInstanceName, long registerTime,
@Override public int getOrCreate(int serviceId, String serviceInstanceName, String uuid, long registerTime,
ServiceInstanceInventory.AgentOsInfo osInfo) {
if (logger.isDebugEnabled()) {
logger.debug("Get or create service instance by service instance name, service id: {}, service instance name: {}, registerTime: {}", serviceId, serviceInstanceName, registerTime);
logger.debug("Get or create service instance by service instance name, service id: {}, service instance name: {},uuid: {}, registerTime: {}", serviceId, serviceInstanceName, uuid, registerTime);
}
int serviceInstanceId = getServiceInstanceInventoryCache().getServiceInstanceId(serviceId, serviceInstanceName);
int serviceInstanceId = getServiceInstanceInventoryCache().getServiceInstanceId(serviceId, uuid);
if (serviceInstanceId == Const.NONE) {
ServiceInstanceInventory serviceInstanceInventory = new ServiceInstanceInventory();
serviceInstanceInventory.setServiceId(serviceId);
serviceInstanceInventory.setName(serviceInstanceName);
serviceInstanceInventory.setInstanceUUID(uuid);
serviceInstanceInventory.setIsAddress(BooleanUtils.FALSE);
serviceInstanceInventory.setAddressId(Const.NONE);
......
......@@ -28,7 +28,7 @@ public interface IServiceInstanceInventoryCacheDAO extends DAO {
ServiceInstanceInventory get(int serviceInstanceId);
int getServiceInstanceId(int serviceId, String serviceInstanceName);
int getServiceInstanceId(int serviceId, String uuid);
int getServiceInstanceId(int serviceId, int addressId);
}
......@@ -53,7 +53,9 @@ public class ServiceMeshMetricDataDecorator {
}
sourceServiceInstanceId = origin.getSourceServiceInstanceId();
if (sourceServiceId != Const.NONE && sourceServiceInstanceId == Const.NONE) {
sourceServiceInstanceId = CoreRegisterLinker.getServiceInstanceInventoryRegister().getOrCreate(sourceServiceId, origin.getSourceServiceInstance(), origin.getEndTime(),
sourceServiceInstanceId = CoreRegisterLinker.getServiceInstanceInventoryRegister()
.getOrCreate(sourceServiceId, origin.getSourceServiceInstance(), origin.getSourceServiceInstance(),
origin.getEndTime(),
getOSInfoForMesh(origin.getSourceServiceInstance()));
if (sourceServiceInstanceId != Const.NONE) {
getNewDataBuilder().setSourceServiceInstanceId(sourceServiceInstanceId);
......@@ -72,7 +74,9 @@ public class ServiceMeshMetricDataDecorator {
}
destServiceInstanceId = origin.getDestServiceInstanceId();
if (destServiceId != Const.NONE && destServiceInstanceId == Const.NONE) {
destServiceInstanceId = CoreRegisterLinker.getServiceInstanceInventoryRegister().getOrCreate(destServiceId, origin.getDestServiceInstance(), origin.getEndTime(),
destServiceInstanceId = CoreRegisterLinker.getServiceInstanceInventoryRegister()
.getOrCreate(destServiceId, origin.getDestServiceInstance(), origin.getDestServiceInstance(),
origin.getEndTime(),
getOSInfoForMesh(origin.getSourceServiceInstance()));
if (destServiceInstanceId != Const.NONE) {
getNewDataBuilder().setDestServiceInstanceId(destServiceInstanceId);
......
......@@ -23,10 +23,13 @@ import java.util.Objects;
import org.apache.skywalking.apm.network.language.agent.*;
import org.apache.skywalking.oap.server.core.CoreModule;
import org.apache.skywalking.oap.server.core.cache.ServiceInstanceInventoryCache;
import org.apache.skywalking.oap.server.core.cache.ServiceInventoryCache;
import org.apache.skywalking.oap.server.core.register.ServiceInstanceInventory;
import org.apache.skywalking.oap.server.core.register.ServiceInventory;
import org.apache.skywalking.oap.server.core.register.service.*;
import org.apache.skywalking.oap.server.library.module.ModuleManager;
import org.apache.skywalking.oap.server.library.server.grpc.GRPCHandler;
import org.apache.skywalking.oap.server.library.util.StringUtils;
import org.slf4j.*;
/**
......@@ -36,11 +39,13 @@ public class InstanceDiscoveryServiceHandler extends InstanceDiscoveryServiceGrp
private static final Logger logger = LoggerFactory.getLogger(InstanceDiscoveryServiceHandler.class);
private final ServiceInventoryCache serviceInventoryCache;
private final ServiceInstanceInventoryCache serviceInstanceInventoryCache;
private final IServiceInventoryRegister serviceInventoryRegister;
private final IServiceInstanceInventoryRegister serviceInstanceInventoryRegister;
public InstanceDiscoveryServiceHandler(ModuleManager moduleManager) {
this.serviceInventoryCache = moduleManager.find(CoreModule.NAME).getService(ServiceInventoryCache.class);
this.serviceInstanceInventoryCache = moduleManager.find(CoreModule.NAME).getService(ServiceInstanceInventoryCache.class);
this.serviceInventoryRegister = moduleManager.find(CoreModule.NAME).getService(IServiceInventoryRegister.class);
this.serviceInstanceInventoryRegister = moduleManager.find(CoreModule.NAME).getService(IServiceInstanceInventoryRegister.class);
......@@ -56,7 +61,17 @@ public class InstanceDiscoveryServiceHandler extends InstanceDiscoveryServiceGrp
agentOsInfo.setProcessNo(osinfo.getProcessNo());
agentOsInfo.getIpv4s().addAll(osinfo.getIpv4SList());
int serviceInstanceId = serviceInstanceInventoryRegister.getOrCreate(request.getApplicationId(), request.getAgentUUID(), request.getRegisterTime(), agentOsInfo);
ServiceInventory serviceInventory = serviceInventoryCache.get(request.getApplicationId());
String instanceName = serviceInventory.getName();
if (osinfo.getProcessNo() != 0) {
instanceName += "-pid:" + osinfo.getProcessNo();
}
if (StringUtils.isNotEmpty(osinfo.getHostname())) {
instanceName += "@" + osinfo.getHostname();
}
int serviceInstanceId = serviceInstanceInventoryRegister.getOrCreate(request.getApplicationId(), instanceName, request.getAgentUUID(), request.getRegisterTime(), agentOsInfo);
ApplicationInstanceMapping.Builder builder = ApplicationInstanceMapping.newBuilder();
builder.setApplicationId(request.getApplicationId());
builder.setApplicationInstanceId(serviceInstanceId);
......
......@@ -22,10 +22,13 @@ import com.google.gson.*;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.skywalking.oap.server.core.CoreModule;
import org.apache.skywalking.oap.server.core.cache.ServiceInventoryCache;
import org.apache.skywalking.oap.server.core.register.ServiceInstanceInventory;
import org.apache.skywalking.oap.server.core.register.ServiceInventory;
import org.apache.skywalking.oap.server.core.register.service.IServiceInstanceInventoryRegister;
import org.apache.skywalking.oap.server.library.module.ModuleManager;
import org.apache.skywalking.oap.server.library.server.jetty.*;
import org.apache.skywalking.oap.server.library.util.StringUtils;
import org.slf4j.*;
/**
......@@ -36,6 +39,7 @@ public class InstanceDiscoveryServletHandler extends JettyJsonHandler {
private static final Logger logger = LoggerFactory.getLogger(InstanceDiscoveryServletHandler.class);
private final IServiceInstanceInventoryRegister serviceInstanceInventoryRegister;
private final ServiceInventoryCache serviceInventoryCache;
private final Gson gson = new Gson();
private static final String APPLICATION_ID = "ai";
......@@ -45,6 +49,7 @@ public class InstanceDiscoveryServletHandler extends JettyJsonHandler {
private static final String OS_INFO = "oi";
public InstanceDiscoveryServletHandler(ModuleManager moduleManager) {
this.serviceInventoryCache = moduleManager.find(CoreModule.NAME).getService(ServiceInventoryCache.class);
this.serviceInstanceInventoryRegister = moduleManager.find(CoreModule.NAME).getService(IServiceInstanceInventoryRegister.class);
}
......@@ -73,7 +78,17 @@ public class InstanceDiscoveryServletHandler extends JettyJsonHandler {
JsonArray ipv4s = osInfoJson.get("ipv4s").getAsJsonArray();
ipv4s.forEach(ipv4 -> agentOsInfo.getIpv4s().add(ipv4.getAsString()));
int instanceId = serviceInstanceInventoryRegister.getOrCreate(applicationId, agentUUID, registerTime, agentOsInfo);
ServiceInventory serviceInventory = serviceInventoryCache.get(applicationId);
String instanceName = serviceInventory.getName();
if (agentOsInfo.getProcessNo() != 0) {
instanceName += "-pid:" + agentOsInfo.getProcessNo();
}
if (StringUtils.isNotEmpty(agentOsInfo.getHostname())) {
instanceName += "@" + agentOsInfo.getHostname();
}
int instanceId = serviceInstanceInventoryRegister.getOrCreate(applicationId, instanceName, agentUUID, registerTime, agentOsInfo);
responseJson.addProperty(APPLICATION_ID, applicationId);
responseJson.addProperty(INSTANCE_ID, instanceId);
} catch (IOException e) {
......
......@@ -62,8 +62,8 @@ public class ServiceInstanceInventoryCacheDAO extends EsDAO implements IServiceI
}
}
@Override public int getServiceInstanceId(int serviceId, String serviceInstanceName) {
String id = ServiceInstanceInventory.buildId(serviceId, serviceInstanceName);
@Override public int getServiceInstanceId(int serviceId, String uuid) {
String id = ServiceInstanceInventory.buildId(serviceId, uuid);
return get(id);
}
......
......@@ -45,8 +45,8 @@ public class H2ServiceInstanceInventoryCacheDAO extends H2SQLExecutor implements
}
}
@Override public int getServiceInstanceId(int serviceId, String serviceInstanceName) {
String id = ServiceInstanceInventory.buildId(serviceId, serviceInstanceName);
@Override public int getServiceInstanceId(int serviceId, String uuid) {
String id = ServiceInstanceInventory.buildId(serviceId, uuid);
return getByID(id);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册