提交 f5d818e9 编写于 作者: wu-sheng's avatar wu-sheng 提交者: GitHub

Merge pull request #533 from OpenSkywalking/feature/423

Feature/423
......@@ -51,5 +51,10 @@
<artifactId>apm-network</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-collector-cache</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
......@@ -19,7 +19,7 @@
package org.skywalking.apm.collector.agentregister.application;
import org.skywalking.apm.collector.agentregister.worker.application.ApplicationRegisterRemoteWorker;
import org.skywalking.apm.collector.agentregister.worker.cache.ApplicationCache;
import org.skywalking.apm.collector.cache.ApplicationCache;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.storage.define.register.ApplicationDataDefine;
import org.skywalking.apm.collector.stream.StreamModuleContext;
......
......@@ -19,7 +19,7 @@
package org.skywalking.apm.collector.agentregister.servicename;
import org.skywalking.apm.collector.agentregister.worker.servicename.ServiceNameRegisterRemoteWorker;
import org.skywalking.apm.collector.agentregister.worker.servicename.dao.IServiceNameDAO;
import org.skywalking.apm.collector.cache.dao.IServiceNameCacheDAO;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.storage.define.register.ServiceNameDataDefine;
......@@ -38,7 +38,7 @@ public class ServiceNameService {
private final Logger logger = LoggerFactory.getLogger(ServiceNameService.class);
public int getOrCreate(int applicationId, String serviceName) {
IServiceNameDAO dao = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
IServiceNameCacheDAO dao = (IServiceNameCacheDAO)DAOContainer.INSTANCE.get(IServiceNameCacheDAO.class.getName());
int serviceId = dao.getServiceId(applicationId, serviceName);
if (serviceId == 0) {
......
......@@ -20,6 +20,7 @@ package org.skywalking.apm.collector.agentregister.worker.application;
import org.skywalking.apm.collector.agentregister.worker.IdAutoIncrement;
import org.skywalking.apm.collector.agentregister.worker.application.dao.IApplicationDAO;
import org.skywalking.apm.collector.cache.dao.IApplicationCacheDAO;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.storage.define.DataDefine;
......@@ -55,8 +56,10 @@ public class ApplicationRegisterSerialWorker extends AbstractLocalAsyncWorker {
ApplicationDataDefine.Application application = (ApplicationDataDefine.Application)message;
logger.debug("register application, application code: {}", application.getApplicationCode());
IApplicationCacheDAO cacheDao = (IApplicationCacheDAO)DAOContainer.INSTANCE.get(IApplicationCacheDAO.class.getName());
int applicationId = cacheDao.getApplicationId(application.getApplicationCode());
IApplicationDAO dao = (IApplicationDAO)DAOContainer.INSTANCE.get(IApplicationDAO.class.getName());
int applicationId = dao.getApplicationId(application.getApplicationCode());
if (applicationId == 0) {
int min = dao.getMinApplicationId();
if (min == 0) {
......
......@@ -21,12 +21,7 @@ package org.skywalking.apm.collector.agentregister.worker.application.dao;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient;
import org.skywalking.apm.collector.storage.define.register.ApplicationDataDefine;
import org.skywalking.apm.collector.storage.define.register.ApplicationTable;
......@@ -41,24 +36,6 @@ public class ApplicationEsDAO extends EsDAO implements IApplicationDAO {
private final Logger logger = LoggerFactory.getLogger(ApplicationEsDAO.class);
@Override public int getApplicationId(String applicationCode) {
ElasticSearchClient client = getClient();
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ApplicationTable.TABLE);
searchRequestBuilder.setTypes("type");
searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
searchRequestBuilder.setQuery(QueryBuilders.termQuery(ApplicationTable.COLUMN_APPLICATION_CODE, applicationCode));
searchRequestBuilder.setSize(1);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
if (searchResponse.getHits().totalHits > 0) {
SearchHit searchHit = searchResponse.getHits().iterator().next();
int applicationId = (int)searchHit.getSource().get(ApplicationTable.COLUMN_APPLICATION_ID);
return applicationId;
}
return 0;
}
@Override public int getMaxApplicationId() {
return getMaxId(ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
}
......
......@@ -18,7 +18,6 @@
package org.skywalking.apm.collector.agentregister.worker.application.dao;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.define.register.ApplicationDataDefine;
......@@ -34,14 +33,6 @@ import org.slf4j.LoggerFactory;
public class ApplicationH2DAO extends H2DAO implements IApplicationDAO {
private final Logger logger = LoggerFactory.getLogger(ApplicationH2DAO.class);
private static final String INSERT_APPLICATION_SQL = "insert into {0}({1}, {2}) values(?, ?)";
@Override
public int getApplicationId(String applicationCode) {
logger.info("get the application id with application code = {}", applicationCode);
String sql = "select " + ApplicationTable.COLUMN_APPLICATION_ID + " from " +
ApplicationTable.TABLE + " where " + ApplicationTable.COLUMN_APPLICATION_CODE + "='" + applicationCode + "'";
return getIntValueBySQL(sql);
}
@Override
public int getMaxApplicationId() {
......@@ -57,7 +48,7 @@ public class ApplicationH2DAO extends H2DAO implements IApplicationDAO {
public void save(ApplicationDataDefine.Application application) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(INSERT_APPLICATION_SQL, ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID,
ApplicationTable.COLUMN_APPLICATION_CODE);
ApplicationTable.COLUMN_APPLICATION_CODE);
Object[] params = new Object[] {application.getApplicationId(), application.getApplicationCode()};
try {
client.execute(sql, params);
......
......@@ -24,8 +24,6 @@ import org.skywalking.apm.collector.storage.define.register.ApplicationDataDefin
* @author pengys5
*/
public interface IApplicationDAO {
int getApplicationId(String applicationCode);
int getMaxApplicationId();
int getMinApplicationId();
......
......@@ -33,6 +33,4 @@ public interface IInstanceDAO {
void save(InstanceDataDefine.Instance instance);
void updateHeartbeatTime(int instanceId, long heartbeatTime);
int getApplicationId(int applicationInstanceId);
}
......@@ -20,7 +20,6 @@ package org.skywalking.apm.collector.agentregister.worker.instance.dao;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
......@@ -101,13 +100,4 @@ public class InstanceEsDAO extends EsDAO implements IInstanceDAO {
updateRequest.doc(source);
client.update(updateRequest);
}
@Override public int getApplicationId(int applicationInstanceId) {
GetResponse response = getClient().prepareGet(InstanceTable.TABLE, String.valueOf(applicationInstanceId)).get();
if (response.isExists()) {
return (int)response.getSource().get(InstanceTable.COLUMN_APPLICATION_ID);
} else {
return 0;
}
}
}
......@@ -22,7 +22,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.storage.define.register.InstanceDataDefine;
......@@ -37,15 +36,16 @@ import org.slf4j.LoggerFactory;
*/
public class InstanceH2DAO extends H2DAO implements IInstanceDAO {
private final Logger logger = LoggerFactory.getLogger(InstanceH2DAO.class);
private static final String GET_INSTANCE_ID_SQL = "select {0} from {1} where {2} = ? and {3} = ?";
private static final String GET_APPLICATION_ID_SQL = "select {0} from {1} where {2} = ?";
private static final String UPDATE_HEARTBEAT_TIME_SQL = "updatte {0} set {1} = ? where {2} = ?";
@Override public int getInstanceId(int applicationId, String agentUUID) {
logger.info("get the application id with application id = {}, agentUUID = {}", applicationId, agentUUID);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_INSTANCE_ID_SQL, InstanceTable.COLUMN_INSTANCE_ID, InstanceTable.TABLE, InstanceTable.COLUMN_APPLICATION_ID,
InstanceTable.COLUMN_AGENT_UUID);
Object[] params = new Object[]{applicationId, agentUUID};
InstanceTable.COLUMN_AGENT_UUID);
Object[] params = new Object[] {applicationId, agentUUID};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getInt(InstanceTable.COLUMN_INSTANCE_ID);
......@@ -85,7 +85,7 @@ public class InstanceH2DAO extends H2DAO implements IInstanceDAO {
@Override public void updateHeartbeatTime(int instanceId, long heartbeatTime) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(UPDATE_HEARTBEAT_TIME_SQL, InstanceTable.TABLE, InstanceTable.COLUMN_HEARTBEAT_TIME,
InstanceTable.COLUMN_INSTANCE_ID);
InstanceTable.COLUMN_INSTANCE_ID);
Object[] params = new Object[] {heartbeatTime, instanceId};
try {
client.execute(sql, params);
......@@ -93,19 +93,4 @@ public class InstanceH2DAO extends H2DAO implements IInstanceDAO {
logger.error(e.getMessage(), e);
}
}
@Override public int getApplicationId(int applicationInstanceId) {
logger.info("get the application id with application id = {}", applicationInstanceId);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_APPLICATION_ID_SQL, InstanceTable.COLUMN_APPLICATION_ID, InstanceTable.TABLE, InstanceTable.COLUMN_APPLICATION_ID);
Object[] params = new Object[]{applicationInstanceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getInt(InstanceTable.COLUMN_APPLICATION_ID);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
}
......@@ -20,6 +20,7 @@ package org.skywalking.apm.collector.agentregister.worker.servicename;
import org.skywalking.apm.collector.agentregister.worker.IdAutoIncrement;
import org.skywalking.apm.collector.agentregister.worker.servicename.dao.IServiceNameDAO;
import org.skywalking.apm.collector.cache.dao.IServiceNameCacheDAO;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.storage.define.DataDefine;
......@@ -55,9 +56,10 @@ public class ServiceNameRegisterSerialWorker extends AbstractLocalAsyncWorker {
ServiceNameDataDefine.ServiceName serviceName = (ServiceNameDataDefine.ServiceName)message;
logger.debug("register service name: {}, application id: {}", serviceName.getServiceName(), serviceName.getApplicationId());
IServiceNameDAO dao = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
int serviceId = dao.getServiceId(serviceName.getApplicationId(), serviceName.getServiceName());
IServiceNameCacheDAO cacheDao = (IServiceNameCacheDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
int serviceId = cacheDao.getServiceId(serviceName.getApplicationId(), serviceName.getServiceName());
IServiceNameDAO dao = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
if (serviceId == 0) {
int min = dao.getMinServiceId();
if (min == 0) {
......
......@@ -24,10 +24,6 @@ import org.skywalking.apm.collector.storage.define.register.ServiceNameDataDefin
* @author pengys5
*/
public interface IServiceNameDAO {
int getServiceId(int applicationId, String serviceName);
String getServiceName(int serviceId);
int getMaxServiceId();
int getMinServiceId();
......
......@@ -20,17 +20,9 @@ package org.skywalking.apm.collector.agentregister.worker.servicename.dao;
import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.define.register.ServiceNameDataDefine;
import org.skywalking.apm.collector.storage.define.register.ServiceNameTable;
import org.skywalking.apm.collector.storage.elasticsearch.dao.EsDAO;
......@@ -44,27 +36,6 @@ public class ServiceNameEsDAO extends EsDAO implements IServiceNameDAO {
private final Logger logger = LoggerFactory.getLogger(ServiceNameEsDAO.class);
@Override public int getServiceId(int applicationId, String serviceName) {
ElasticSearchClient client = getClient();
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ServiceNameTable.TABLE);
searchRequestBuilder.setTypes(ServiceNameTable.TABLE_TYPE);
searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
BoolQueryBuilder builder = QueryBuilders.boolQuery();
builder.must().add(QueryBuilders.termQuery(ServiceNameTable.COLUMN_APPLICATION_ID, applicationId));
builder.must().add(QueryBuilders.termQuery(ServiceNameTable.COLUMN_SERVICE_NAME, serviceName));
searchRequestBuilder.setQuery(builder);
searchRequestBuilder.setSize(1);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
if (searchResponse.getHits().totalHits > 0) {
SearchHit searchHit = searchResponse.getHits().iterator().next();
int serviceId = (int)searchHit.getSource().get(ServiceNameTable.COLUMN_SERVICE_ID);
return serviceId;
}
return 0;
}
@Override public int getMaxServiceId() {
return getMaxId(ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
}
......@@ -73,15 +44,6 @@ public class ServiceNameEsDAO extends EsDAO implements IServiceNameDAO {
return getMinId(ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
}
@Override public String getServiceName(int serviceId) {
GetResponse response = getClient().prepareGet(ServiceNameTable.TABLE, String.valueOf(serviceId)).get();
if (response.isExists()) {
return (String)response.getSource().get(ServiceNameTable.COLUMN_SERVICE_NAME);
} else {
return Const.EMPTY_STRING;
}
}
@Override public void save(ServiceNameDataDefine.ServiceName serviceName) {
logger.debug("save service name register info, application id: {}, service name: {}", serviceName.getApplicationId(), serviceName.getServiceName());
ElasticSearchClient client = getClient();
......
......@@ -18,14 +18,10 @@
package org.skywalking.apm.collector.agentregister.worker.servicename.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.define.register.ServiceNameDataDefine;
import org.skywalking.apm.collector.storage.define.register.ServiceNameTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
......@@ -38,24 +34,6 @@ import org.slf4j.LoggerFactory;
*/
public class ServiceNameH2DAO extends H2DAO implements IServiceNameDAO {
private final Logger logger = LoggerFactory.getLogger(ServiceNameH2DAO.class);
private static final String GET_SERVICE_ID_SQL = "select {0} from {1} where {2} = ? and {3} = ? limit 1";
private static final String GET_SERVICE_NAME_SQL = "select {0} from {1} where {2} = ?";
@Override
public int getServiceId(int applicationId, String serviceName) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SERVICE_ID_SQL, ServiceNameTable.COLUMN_SERVICE_ID,
ServiceNameTable.TABLE, ServiceNameTable.COLUMN_APPLICATION_ID, ServiceNameTable.COLUMN_SERVICE_NAME);
Object[] params = new Object[]{applicationId, serviceName};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getInt(ServiceNameTable.COLUMN_SERVICE_ID);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
@Override
public int getMaxServiceId() {
......@@ -67,28 +45,11 @@ public class ServiceNameH2DAO extends H2DAO implements IServiceNameDAO {
return getMinId(ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
}
@Override
public String getServiceName(int serviceId) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SERVICE_NAME_SQL, ServiceNameTable.COLUMN_SERVICE_NAME,
ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
Object[] params = new Object[]{serviceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getString(ServiceNameTable.COLUMN_SERVICE_NAME);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return Const.EMPTY_STRING;
}
@Override
public void save(ServiceNameDataDefine.ServiceName serviceName) {
logger.debug("save service name register info, application id: {}, service name: {}", serviceName.getApplicationId(), serviceName.getServiceName());
H2Client client = getClient();
Map<String, Object> source = new HashMap();
Map<String, Object> source = new HashMap<>();
source.put(ServiceNameTable.COLUMN_SERVICE_ID, serviceName.getServiceId());
source.put(ServiceNameTable.COLUMN_APPLICATION_ID, serviceName.getApplicationId());
source.put(ServiceNameTable.COLUMN_SERVICE_NAME, serviceName.getServiceName());
......
......@@ -18,6 +18,7 @@
package org.skywalking.apm.collector.agentserver;
import org.skywalking.apm.collector.core.config.GroupConfigParser;
import org.skywalking.apm.collector.core.framework.Context;
import org.skywalking.apm.collector.core.module.ModuleGroupDefine;
import org.skywalking.apm.collector.core.module.ModuleInstaller;
......@@ -28,10 +29,10 @@ import org.skywalking.apm.collector.core.module.ModuleInstaller;
public class AgentServerModuleGroupDefine implements ModuleGroupDefine {
public static final String GROUP_NAME = "agent_server";
private final AgentServerCommonModuleInstaller installer;
private final AgentServerModuleInstaller installer;
public AgentServerModuleGroupDefine() {
installer = new AgentServerCommonModuleInstaller();
installer = new AgentServerModuleInstaller();
}
@Override public String name() {
......@@ -45,4 +46,8 @@ public class AgentServerModuleGroupDefine implements ModuleGroupDefine {
@Override public ModuleInstaller moduleInstaller() {
return installer;
}
@Override public GroupConfigParser groupConfigParser() {
return null;
}
}
......@@ -20,12 +20,12 @@ package org.skywalking.apm.collector.agentserver;
import java.util.List;
import org.skywalking.apm.collector.core.framework.Context;
import org.skywalking.apm.collector.core.module.MultipleCommonModuleInstaller;
import org.skywalking.apm.collector.core.module.MultipleModuleInstaller;
/**
* @author pengys5
*/
public class AgentServerCommonModuleInstaller extends MultipleCommonModuleInstaller {
public class AgentServerModuleInstaller extends MultipleModuleInstaller {
@Override public String groupName() {
return AgentServerModuleGroupDefine.GROUP_NAME;
......
......@@ -61,5 +61,10 @@
<artifactId>apm-collector-agentregister</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-collector-cache</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream;
import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.storage.PersistenceTimer;
import org.skywalking.apm.collector.core.client.ClientException;
import org.skywalking.apm.collector.core.config.ConfigException;
import org.skywalking.apm.collector.core.framework.Context;
import org.skywalking.apm.collector.core.framework.DefineException;
import org.skywalking.apm.collector.core.module.MultipleCommonModuleInstaller;
import org.skywalking.apm.collector.core.server.ServerException;
/**
* @author pengys5
*/
public class AgentStreamCommonModuleInstaller extends MultipleCommonModuleInstaller {
@Override public String groupName() {
return AgentStreamModuleGroupDefine.GROUP_NAME;
}
@Override public Context moduleContext() {
return new AgentStreamModuleContext(groupName());
}
@Override public List<String> dependenceModules() {
return null;
}
@Override public void install() throws DefineException, ConfigException, ServerException, ClientException {
super.install();
new PersistenceTimer().start();
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream;
import java.util.Map;
import org.skywalking.apm.collector.agentstream.config.BufferFileConfig;
import org.skywalking.apm.collector.core.config.GroupConfigParser;
/**
* @author pengys5
*/
public class AgentStreamModuleGroupConfigParser implements GroupConfigParser {
private static final String BUFFER_OFFSET_MAX_FILE_SIZE = "buffer_offset_max_file_size";
private static final String BUFFER_SEGMENT_MAX_FILE_SIZE = "buffer_segment_max_file_size";
@Override public void parse(Map<String, Map> config) {
if (config.containsKey(GroupConfigParser.NODE_NAME)) {
Map<String, String> groupConfig = config.get(GroupConfigParser.NODE_NAME);
if (groupConfig.containsKey(BUFFER_OFFSET_MAX_FILE_SIZE)) {
String sizeStr = groupConfig.get(BUFFER_OFFSET_MAX_FILE_SIZE).toUpperCase();
if (sizeStr.endsWith("K")) {
int size = Integer.parseInt(sizeStr.replace("K", ""));
BufferFileConfig.BUFFER_OFFSET_MAX_FILE_SIZE = size * 1024;
} else if (sizeStr.endsWith("KB")) {
int size = Integer.parseInt(sizeStr.replace("KB", ""));
BufferFileConfig.BUFFER_OFFSET_MAX_FILE_SIZE = size * 1024;
} else if (sizeStr.endsWith("M")) {
int size = Integer.parseInt(sizeStr.replace("M", ""));
BufferFileConfig.BUFFER_OFFSET_MAX_FILE_SIZE = size * 1024 * 1024;
} else if (sizeStr.endsWith("MB")) {
int size = Integer.parseInt(sizeStr.replace("MB", ""));
BufferFileConfig.BUFFER_OFFSET_MAX_FILE_SIZE = size * 1024 * 1024;
} else {
BufferFileConfig.BUFFER_OFFSET_MAX_FILE_SIZE = 10 * 1024 * 1024;
}
} else {
BufferFileConfig.BUFFER_OFFSET_MAX_FILE_SIZE = 10 * 1024 * 1024;
}
if (groupConfig.containsKey(BUFFER_SEGMENT_MAX_FILE_SIZE)) {
String sizeStr = groupConfig.get(BUFFER_SEGMENT_MAX_FILE_SIZE).toUpperCase();
if (sizeStr.endsWith("K")) {
int size = Integer.parseInt(sizeStr.replace("K", ""));
BufferFileConfig.BUFFER_SEGMENT_MAX_FILE_SIZE = size * 1024;
} else if (sizeStr.endsWith("KB")) {
int size = Integer.parseInt(sizeStr.replace("KB", ""));
BufferFileConfig.BUFFER_SEGMENT_MAX_FILE_SIZE = size * 1024;
} else if (sizeStr.endsWith("M")) {
int size = Integer.parseInt(sizeStr.replace("M", ""));
BufferFileConfig.BUFFER_SEGMENT_MAX_FILE_SIZE = size * 1024 * 1024;
} else if (sizeStr.endsWith("MB")) {
int size = Integer.parseInt(sizeStr.replace("MB", ""));
BufferFileConfig.BUFFER_SEGMENT_MAX_FILE_SIZE = size * 1024 * 1024;
} else {
BufferFileConfig.BUFFER_SEGMENT_MAX_FILE_SIZE = 1024 * 1024;
}
} else {
BufferFileConfig.BUFFER_SEGMENT_MAX_FILE_SIZE = 1024 * 1024;
}
}
}
}
......@@ -18,6 +18,7 @@
package org.skywalking.apm.collector.agentstream;
import org.skywalking.apm.collector.core.config.GroupConfigParser;
import org.skywalking.apm.collector.core.framework.Context;
import org.skywalking.apm.collector.core.module.ModuleGroupDefine;
import org.skywalking.apm.collector.core.module.ModuleInstaller;
......@@ -45,4 +46,8 @@ public class AgentStreamModuleGroupDefine implements ModuleGroupDefine {
@Override public ModuleInstaller moduleInstaller() {
return installer;
}
@Override public GroupConfigParser groupConfigParser() {
return new AgentStreamModuleGroupConfigParser();
}
}
......@@ -24,13 +24,13 @@ import org.skywalking.apm.collector.core.client.ClientException;
import org.skywalking.apm.collector.core.config.ConfigException;
import org.skywalking.apm.collector.core.framework.Context;
import org.skywalking.apm.collector.core.framework.DefineException;
import org.skywalking.apm.collector.core.module.MultipleCommonModuleInstaller;
import org.skywalking.apm.collector.core.module.MultipleModuleInstaller;
import org.skywalking.apm.collector.core.server.ServerException;
/**
* @author pengys5
*/
public class AgentStreamModuleInstaller extends MultipleCommonModuleInstaller {
public class AgentStreamModuleInstaller extends MultipleModuleInstaller {
@Override public String groupName() {
return AgentStreamModuleGroupDefine.GROUP_NAME;
......
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.config;
/**
* @author pengys5
*/
public class BufferFileConfig {
public static int BUFFER_OFFSET_MAX_FILE_SIZE = 10 * 1024 * 1024;
public static int BUFFER_SEGMENT_MAX_FILE_SIZE = 10 * 1024 * 1024;
}
......@@ -18,15 +18,11 @@
package org.skywalking.apm.collector.agentstream.grpc.handler;
import com.google.protobuf.InvalidProtocolBufferException;
import io.grpc.stub.StreamObserver;
import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.segment.SegmentParse;
import org.skywalking.apm.collector.server.grpc.GRPCHandler;
import org.skywalking.apm.network.proto.Downstream;
import org.skywalking.apm.network.proto.TraceSegmentObject;
import org.skywalking.apm.network.proto.TraceSegmentServiceGrpc;
import org.skywalking.apm.network.proto.UniqueId;
import org.skywalking.apm.network.proto.UpstreamSegment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -43,13 +39,7 @@ public class TraceSegmentServiceHandler extends TraceSegmentServiceGrpc.TraceSeg
@Override public void onNext(UpstreamSegment segment) {
logger.debug("receive segment");
SegmentParse segmentParse = new SegmentParse();
try {
List<UniqueId> traceIds = segment.getGlobalTraceIdsList();
TraceSegmentObject segmentObject = TraceSegmentObject.parseFrom(segment.getSegment());
segmentParse.parse(traceIds, segmentObject);
} catch (InvalidProtocolBufferException e) {
logger.error(e.getMessage(), e);
}
segmentParse.parse(segment, SegmentParse.Source.Agent);
}
@Override public void onError(Throwable throwable) {
......
......@@ -66,7 +66,7 @@ public class TraceSegmentServletHandler extends JettyHandler {
while (reader.hasNext()) {
SegmentParse segmentParse = new SegmentParse();
TraceSegment traceSegment = jsonReader.read(reader);
segmentParse.parse(traceSegment.getGlobalTraceIds(), traceSegment.getTraceSegmentObject());
segmentParse.parse(traceSegment.getUpstreamSegment(), SegmentParse.Source.Agent);
}
reader.endArray();
}
......
......@@ -27,7 +27,7 @@ import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class SegmentJsonReader implements StreamJsonReader<TraceSegmentObject> {
public class SegmentJsonReader implements StreamJsonReader<TraceSegmentObject.Builder> {
private final Logger logger = LoggerFactory.getLogger(SegmentJsonReader.class);
......@@ -41,7 +41,7 @@ public class SegmentJsonReader implements StreamJsonReader<TraceSegmentObject> {
private static final String TRACE_SEGMENT_REFERENCE = "rs";
private static final String SPANS = "ss";
@Override public TraceSegmentObject read(JsonReader reader) throws IOException {
@Override public TraceSegmentObject.Builder read(JsonReader reader) throws IOException {
TraceSegmentObject.Builder builder = TraceSegmentObject.newBuilder();
reader.beginObject();
......@@ -82,6 +82,6 @@ public class SegmentJsonReader implements StreamJsonReader<TraceSegmentObject> {
}
reader.endObject();
return builder.build();
return builder;
}
}
......@@ -18,36 +18,30 @@
package org.skywalking.apm.collector.agentstream.jetty.handler.reader;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.network.proto.TraceSegmentObject;
import org.skywalking.apm.network.proto.UniqueId;
import org.skywalking.apm.network.proto.UpstreamSegment;
/**
* @author pengys5
*/
public class TraceSegment {
private List<UniqueId> uniqueIds;
private TraceSegmentObject traceSegmentObject;
private UpstreamSegment.Builder builder;
public TraceSegment() {
uniqueIds = new ArrayList<>();
builder = UpstreamSegment.newBuilder();
}
public List<UniqueId> getGlobalTraceIds() {
return uniqueIds;
public void addGlobalTraceId(UniqueId.Builder globalTraceId) {
builder.addGlobalTraceIds(globalTraceId);
}
public void addGlobalTraceId(UniqueId globalTraceId) {
uniqueIds.add(globalTraceId);
public void setTraceSegmentBuilder(TraceSegmentObject.Builder traceSegmentBuilder) {
builder.setSegment(traceSegmentBuilder.build().toByteString());
}
public TraceSegmentObject getTraceSegmentObject() {
return traceSegmentObject;
}
public void setTraceSegmentObject(TraceSegmentObject traceSegmentObject) {
this.traceSegmentObject = traceSegmentObject;
public UpstreamSegment getUpstreamSegment() {
return builder.build();
}
}
......@@ -49,16 +49,9 @@ public class TraceSegmentJsonReader implements StreamJsonReader<TraceSegment> {
}
reader.endArray();
if (logger.isDebugEnabled()) {
traceSegment.getGlobalTraceIds().forEach(uniqueId -> {
StringBuilder globalTraceId = new StringBuilder();
uniqueId.getIdPartsList().forEach(idPart -> globalTraceId.append(idPart));
logger.debug("global trace id: {}", globalTraceId.toString());
});
}
break;
case SEGMENT:
traceSegment.setTraceSegmentObject(segmentJsonReader.read(reader));
traceSegment.setTraceSegmentBuilder(segmentJsonReader.read(reader));
break;
default:
reader.skipValue();
......
......@@ -25,9 +25,9 @@ import org.skywalking.apm.network.proto.UniqueId;
/**
* @author pengys5
*/
public class UniqueIdJsonReader implements StreamJsonReader<UniqueId> {
public class UniqueIdJsonReader implements StreamJsonReader<UniqueId.Builder> {
@Override public UniqueId read(JsonReader reader) throws IOException {
@Override public UniqueId.Builder read(JsonReader reader) throws IOException {
UniqueId.Builder builder = UniqueId.newBuilder();
reader.beginArray();
......@@ -35,6 +35,6 @@ public class UniqueIdJsonReader implements StreamJsonReader<UniqueId> {
builder.addIdParts(reader.nextLong());
}
reader.endArray();
return builder.build();
return builder;
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.cache;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.skywalking.apm.collector.agentregister.worker.servicename.dao.IServiceNameDAO;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
/**
* @author pengys5
*/
public class ServiceCache {
private static Cache<Integer, String> CACHE = CacheBuilder.newBuilder().initialCapacity(1000).maximumSize(20000).build();
public static String getServiceName(int serviceId) {
try {
return CACHE.get(serviceId, () -> {
IServiceNameDAO dao = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
return dao.getServiceName(serviceId);
});
} catch (Throwable e) {
return Const.EMPTY_STRING;
}
}
}
......@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.segment.FirstSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.GlobalTraceIdsListener;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
......@@ -30,7 +31,6 @@ import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.UniqueId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -47,8 +47,9 @@ public class GlobalTraceSpanListener implements FirstSpanListener, GlobalTraceId
private long timeBucket;
@Override
public void parseFirst(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
this.timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime());
public void parseFirst(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
this.timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime());
this.segmentId = segmentId;
}
......
......@@ -20,6 +20,7 @@ package org.skywalking.apm.collector.agentstream.worker.instance.performance;
import org.skywalking.apm.collector.agentstream.worker.segment.EntrySpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.FirstSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
......@@ -28,7 +29,6 @@ import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -45,15 +45,17 @@ public class InstPerformanceSpanListener implements EntrySpanListener, FirstSpan
private long timeBucket;
@Override
public void parseEntry(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
public void parseEntry(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
}
@Override
public void parseFirst(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
public void parseFirst(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
this.applicationId = applicationId;
this.instanceId = applicationInstanceId;
this.cost = spanObject.getEndTime() - spanObject.getStartTime();
timeBucket = TimeBucketUtils.INSTANCE.getSecondTimeBucket(spanObject.getStartTime());
this.cost = spanDecorator.getEndTime() - spanDecorator.getStartTime();
timeBucket = TimeBucketUtils.INSTANCE.getSecondTimeBucket(spanDecorator.getStartTime());
}
@Override public void build() {
......
......@@ -23,6 +23,7 @@ import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.segment.EntrySpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.ExitSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.FirstSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
......@@ -31,7 +32,6 @@ import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -46,39 +46,34 @@ public class NodeComponentSpanListener implements EntrySpanListener, ExitSpanLis
private long timeBucket;
@Override
public void parseExit(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
public void parseExit(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId, String segmentId) {
NodeComponentDataDefine.NodeComponent nodeComponent = new NodeComponentDataDefine.NodeComponent();
nodeComponent.setComponentId(spanObject.getComponentId());
nodeComponent.setComponentId(spanDecorator.getComponentId());
String id;
if (spanObject.getComponentId() == 0) {
nodeComponent.setComponentName(spanObject.getComponent());
if (spanDecorator.getComponentId() == 0) {
nodeComponent.setComponentName(spanDecorator.getComponent());
id = nodeComponent.getComponentName();
} else {
nodeComponent.setComponentName(Const.EMPTY_STRING);
id = String.valueOf(nodeComponent.getComponentId());
}
nodeComponent.setPeerId(spanObject.getPeerId());
if (spanObject.getPeerId() == 0) {
nodeComponent.setPeer(spanObject.getPeer());
id = id + Const.ID_SPLIT + nodeComponent.getPeer();
} else {
nodeComponent.setPeer(Const.EMPTY_STRING);
id = id + Const.ID_SPLIT + nodeComponent.getPeerId();
}
nodeComponent.setPeerId(spanDecorator.getPeerId());
id = id + Const.ID_SPLIT + nodeComponent.getPeerId();
nodeComponent.setId(id);
nodeComponents.add(nodeComponent);
}
@Override
public void parseEntry(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
public void parseEntry(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
NodeComponentDataDefine.NodeComponent nodeComponent = new NodeComponentDataDefine.NodeComponent();
nodeComponent.setComponentId(spanObject.getComponentId());
nodeComponent.setComponentId(spanDecorator.getComponentId());
String id;
if (spanObject.getComponentId() == 0) {
nodeComponent.setComponentName(spanObject.getComponent());
if (spanDecorator.getComponentId() == 0) {
nodeComponent.setComponentName(spanDecorator.getComponent());
id = nodeComponent.getComponentName();
} else {
id = String.valueOf(nodeComponent.getComponentId());
......@@ -86,7 +81,6 @@ public class NodeComponentSpanListener implements EntrySpanListener, ExitSpanLis
}
nodeComponent.setPeerId(applicationId);
nodeComponent.setPeer(Const.EMPTY_STRING);
id = id + Const.ID_SPLIT + String.valueOf(applicationId);
nodeComponent.setId(id);
......@@ -94,8 +88,9 @@ public class NodeComponentSpanListener implements EntrySpanListener, ExitSpanLis
}
@Override
public void parseFirst(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime());
public void parseFirst(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime());
}
@Override public void build() {
......
......@@ -22,6 +22,8 @@ import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.segment.FirstSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.RefsListener;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.ReferenceDecorator;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
......@@ -30,8 +32,6 @@ import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.TraceSegmentReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -45,29 +45,21 @@ public class NodeMappingSpanListener implements RefsListener, FirstSpanListener
private List<NodeMappingDataDefine.NodeMapping> nodeMappings = new ArrayList<>();
private long timeBucket;
@Override public void parseRef(TraceSegmentReference reference, int applicationId, int applicationInstanceId,
@Override public void parseRef(ReferenceDecorator referenceDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
logger.debug("node mapping listener parse reference");
NodeMappingDataDefine.NodeMapping nodeMapping = new NodeMappingDataDefine.NodeMapping();
nodeMapping.setApplicationId(applicationId);
nodeMapping.setAddressId(reference.getNetworkAddressId());
String id = String.valueOf(applicationId);
if (reference.getNetworkAddressId() != 0) {
nodeMapping.setAddress(Const.EMPTY_STRING);
id = id + Const.ID_SPLIT + String.valueOf(nodeMapping.getAddressId());
} else {
id = id + Const.ID_SPLIT + reference.getNetworkAddress();
nodeMapping.setAddress(reference.getNetworkAddress());
}
nodeMapping.setAddressId(referenceDecorator.getNetworkAddressId());
String id = String.valueOf(applicationId) + Const.ID_SPLIT + String.valueOf(nodeMapping.getAddressId());
nodeMapping.setId(id);
nodeMappings.add(nodeMapping);
}
@Override
public void parseFirst(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime());
public void parseFirst(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime());
}
@Override public void build() {
......
......@@ -20,10 +20,12 @@ package org.skywalking.apm.collector.agentstream.worker.noderef;
import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.cache.InstanceCache;
import org.skywalking.apm.collector.agentstream.worker.segment.EntrySpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.ExitSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.RefsListener;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.ReferenceDecorator;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.cache.InstanceCache;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.CollectionUtils;
import org.skywalking.apm.collector.core.util.Const;
......@@ -33,8 +35,6 @@ import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.TraceSegmentReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -49,59 +49,53 @@ public class NodeReferenceSpanListener implements EntrySpanListener, ExitSpanLis
private List<NodeReferenceDataDefine.NodeReference> references = new LinkedList<>();
@Override
public void parseExit(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
public void parseExit(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId, String segmentId) {
NodeReferenceDataDefine.NodeReference nodeReference = new NodeReferenceDataDefine.NodeReference();
nodeReference.setFrontApplicationId(applicationId);
nodeReference.setBehindApplicationId(spanObject.getPeerId());
nodeReference.setTimeBucket(TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime()));
nodeReference.setBehindApplicationId(spanDecorator.getPeerId());
nodeReference.setTimeBucket(TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime()));
StringBuilder idBuilder = new StringBuilder();
idBuilder.append(nodeReference.getTimeBucket()).append(Const.ID_SPLIT).append(applicationId);
if (spanObject.getPeerId() != 0) {
nodeReference.setBehindPeer(Const.EMPTY_STRING);
idBuilder.append(Const.ID_SPLIT).append(spanObject.getPeerId());
} else {
nodeReference.setBehindPeer(spanObject.getPeer());
idBuilder.append(Const.ID_SPLIT).append(spanObject.getPeer());
}
idBuilder.append(nodeReference.getTimeBucket()).append(Const.ID_SPLIT).append(applicationId)
.append(Const.ID_SPLIT).append(spanDecorator.getPeerId());
nodeReference.setId(idBuilder.toString());
nodeReferences.add(buildNodeRefSum(nodeReference, spanObject.getStartTime(), spanObject.getEndTime(), spanObject.getIsError()));
nodeReferences.add(buildNodeRefSum(nodeReference, spanDecorator.getStartTime(), spanDecorator.getEndTime(), spanDecorator.getIsError()));
}
@Override
public void parseEntry(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
public void parseEntry(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
if (CollectionUtils.isNotEmpty(references)) {
references.forEach(nodeReference -> {
nodeReference.setTimeBucket(TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime()));
nodeReference.setTimeBucket(TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime()));
String idBuilder = String.valueOf(nodeReference.getTimeBucket()) + Const.ID_SPLIT + nodeReference.getFrontApplicationId() +
Const.ID_SPLIT + nodeReference.getBehindApplicationId();
nodeReference.setId(idBuilder);
nodeReferences.add(buildNodeRefSum(nodeReference, spanObject.getStartTime(), spanObject.getEndTime(), spanObject.getIsError()));
nodeReferences.add(buildNodeRefSum(nodeReference, spanDecorator.getStartTime(), spanDecorator.getEndTime(), spanDecorator.getIsError()));
});
} else {
NodeReferenceDataDefine.NodeReference nodeReference = new NodeReferenceDataDefine.NodeReference();
nodeReference.setFrontApplicationId(Const.USER_ID);
nodeReference.setBehindApplicationId(applicationId);
nodeReference.setBehindPeer(Const.EMPTY_STRING);
nodeReference.setTimeBucket(TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime()));
nodeReference.setTimeBucket(TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime()));
String idBuilder = String.valueOf(nodeReference.getTimeBucket()) + Const.ID_SPLIT + nodeReference.getFrontApplicationId() +
Const.ID_SPLIT + nodeReference.getBehindApplicationId();
nodeReference.setId(idBuilder);
nodeReferences.add(buildNodeRefSum(nodeReference, spanObject.getStartTime(), spanObject.getEndTime(), spanObject.getIsError()));
nodeReferences.add(buildNodeRefSum(nodeReference, spanDecorator.getStartTime(), spanDecorator.getEndTime(), spanDecorator.getIsError()));
}
}
@Override public void parseRef(TraceSegmentReference reference, int applicationId, int applicationInstanceId,
@Override public void parseRef(ReferenceDecorator referenceDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
int parentApplicationId = InstanceCache.get(reference.getParentApplicationInstanceId());
int parentApplicationId = InstanceCache.get(referenceDecorator.getParentApplicationInstanceId());
NodeReferenceDataDefine.NodeReference referenceSum = new NodeReferenceDataDefine.NodeReference();
referenceSum.setFrontApplicationId(parentApplicationId);
referenceSum.setBehindApplicationId(applicationId);
referenceSum.setBehindPeer(Const.EMPTY_STRING);
references.add(referenceSum);
}
......
......@@ -18,11 +18,11 @@
package org.skywalking.apm.collector.agentstream.worker.segment;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
/**
* @author pengys5
*/
public interface EntrySpanListener extends SpanListener {
void parseEntry(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId);
void parseEntry(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId, String segmentId);
}
......@@ -18,11 +18,11 @@
package org.skywalking.apm.collector.agentstream.worker.segment;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
/**
* @author pengys5
*/
public interface ExitSpanListener extends SpanListener {
void parseExit(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId);
void parseExit(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId, String segmentId);
}
......@@ -18,11 +18,11 @@
package org.skywalking.apm.collector.agentstream.worker.segment;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
/**
* @author pengys5
*/
public interface FirstSpanListener extends SpanListener {
void parseFirst(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId);
void parseFirst(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId, String segmentId);
}
......@@ -18,11 +18,11 @@
package org.skywalking.apm.collector.agentstream.worker.segment;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
/**
* @author pengys5
*/
public interface LocalSpanListener extends SpanListener {
void parseLocal(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId);
void parseLocal(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId, String segmentId);
}
......@@ -18,11 +18,11 @@
package org.skywalking.apm.collector.agentstream.worker.segment;
import org.skywalking.apm.network.proto.TraceSegmentReference;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.ReferenceDecorator;
/**
* @author pengys5
*/
public interface RefsListener extends SpanListener {
void parseRef(TraceSegmentReference reference, int applicationId, int applicationInstanceId, String segmentId);
void parseRef(ReferenceDecorator referenceDecorator, int applicationId, int applicationInstanceId, String segmentId);
}
......@@ -18,6 +18,7 @@
package org.skywalking.apm.collector.agentstream.worker.segment;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.global.GlobalTraceSpanListener;
......@@ -27,20 +28,24 @@ import org.skywalking.apm.collector.agentstream.worker.node.mapping.NodeMappingS
import org.skywalking.apm.collector.agentstream.worker.noderef.NodeReferenceSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.cost.SegmentCostSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.origin.SegmentPersistenceWorker;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.ReferenceDecorator;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.ReferenceIdExchanger;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SegmentDecorator;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SegmentStandardizationWorker;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanIdExchanger;
import org.skywalking.apm.collector.agentstream.worker.service.entry.ServiceEntrySpanListener;
import org.skywalking.apm.collector.agentstream.worker.serviceref.ServiceReferenceSpanListener;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.CollectionUtils;
import org.skywalking.apm.collector.storage.define.segment.SegmentDataDefine;
import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.SpanType;
import org.skywalking.apm.network.proto.TraceSegmentObject;
import org.skywalking.apm.network.proto.TraceSegmentReference;
import org.skywalking.apm.network.proto.UniqueId;
import org.skywalking.apm.network.proto.UpstreamSegment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -52,6 +57,7 @@ public class SegmentParse {
private final Logger logger = LoggerFactory.getLogger(SegmentParse.class);
private List<SpanListener> spanListeners;
private String segmentId;
public SegmentParse() {
spanListeners = new ArrayList<>();
......@@ -65,51 +71,83 @@ public class SegmentParse {
spanListeners.add(new InstPerformanceSpanListener());
}
public void parse(List<UniqueId> traceIds, TraceSegmentObject segmentObject) {
public boolean parse(UpstreamSegment segment, Source source) {
try {
List<UniqueId> traceIds = segment.getGlobalTraceIdsList();
TraceSegmentObject segmentObject = TraceSegmentObject.parseFrom(segment.getSegment());
SegmentDecorator segmentDecorator = new SegmentDecorator(segmentObject);
if (!preBuild(traceIds, segmentDecorator)) {
logger.debug("This segment id exchange not success, write to buffer file, id: {}", segmentId);
if (source.equals(Source.Agent)) {
writeToBufferFile(segmentId, segment);
}
return false;
} else {
logger.debug("This segment id exchange success, id: {}", segmentId);
notifyListenerToBuild();
buildSegment(segmentId, segmentDecorator.toByteArray());
return true;
}
} catch (InvalidProtocolBufferException e) {
logger.error(e.getMessage(), e);
}
return false;
}
private boolean preBuild(List<UniqueId> traceIds, SegmentDecorator segmentDecorator) {
StringBuilder segmentIdBuilder = new StringBuilder();
for (int i = 0; i < segmentObject.getTraceSegmentId().getIdPartsList().size(); i++) {
for (int i = 0; i < segmentDecorator.getTraceSegmentId().getIdPartsList().size(); i++) {
if (i == 0) {
segmentIdBuilder.append(segmentObject.getTraceSegmentId().getIdPartsList().get(i));
segmentIdBuilder.append(segmentDecorator.getTraceSegmentId().getIdPartsList().get(i));
} else {
segmentIdBuilder.append(".").append(segmentObject.getTraceSegmentId().getIdPartsList().get(i));
segmentIdBuilder.append(".").append(segmentDecorator.getTraceSegmentId().getIdPartsList().get(i));
}
}
String segmentId = segmentIdBuilder.toString();
segmentId = segmentIdBuilder.toString();
for (UniqueId uniqueId : traceIds) {
notifyGlobalsListener(uniqueId);
}
int applicationId = segmentObject.getApplicationId();
int applicationInstanceId = segmentObject.getApplicationInstanceId();
int applicationId = segmentDecorator.getApplicationId();
int applicationInstanceId = segmentDecorator.getApplicationInstanceId();
for (int i = 0; i < segmentDecorator.getRefsCount(); i++) {
ReferenceDecorator referenceDecorator = segmentDecorator.getRefs(i);
if (!ReferenceIdExchanger.getInstance().exchange(referenceDecorator, applicationId)) {
return false;
}
for (TraceSegmentReference reference : segmentObject.getRefsList()) {
notifyRefsListener(reference, applicationId, applicationInstanceId, segmentId);
notifyRefsListener(referenceDecorator, applicationId, applicationInstanceId, segmentId);
}
List<SpanObject> spans = segmentObject.getSpansList();
if (CollectionUtils.isNotEmpty(spans)) {
for (SpanObject spanObject : spans) {
if (spanObject.getSpanId() == 0) {
notifyFirstListener(spanObject, applicationId, applicationInstanceId, segmentId);
}
for (int i = 0; i < segmentDecorator.getSpansCount(); i++) {
SpanDecorator spanDecorator = segmentDecorator.getSpans(i);
if (SpanType.Exit.equals(spanObject.getSpanType())) {
notifyExitListener(spanObject, applicationId, applicationInstanceId, segmentId);
} else if (SpanType.Entry.equals(spanObject.getSpanType())) {
notifyEntryListener(spanObject, applicationId, applicationInstanceId, segmentId);
} else if (SpanType.Local.equals(spanObject.getSpanType())) {
notifyLocalListener(spanObject, applicationId, applicationInstanceId, segmentId);
} else {
logger.error("span type error, span type: {}", spanObject.getSpanType().name());
}
if (!SpanIdExchanger.getInstance().exchange(spanDecorator, applicationId)) {
return false;
}
if (spanDecorator.getSpanId() == 0) {
notifyFirstListener(spanDecorator, applicationId, applicationInstanceId, segmentId);
}
if (SpanType.Exit.equals(spanDecorator.getSpanType())) {
notifyExitListener(spanDecorator, applicationId, applicationInstanceId, segmentId);
} else if (SpanType.Entry.equals(spanDecorator.getSpanType())) {
notifyEntryListener(spanDecorator, applicationId, applicationInstanceId, segmentId);
} else if (SpanType.Local.equals(spanDecorator.getSpanType())) {
notifyLocalListener(spanDecorator, applicationId, applicationInstanceId, segmentId);
} else {
logger.error("span type value was unexpected, span type name: {}", spanDecorator.getSpanType().name());
}
}
notifyListenerToBuild();
buildSegment(segmentId, segmentObject.toByteArray());
return true;
}
private void buildSegment(String id, byte[] dataBinary) {
......@@ -126,47 +164,58 @@ public class SegmentParse {
}
}
private void writeToBufferFile(String id, UpstreamSegment upstreamSegment) {
StreamModuleContext context = (StreamModuleContext)CollectorContextHelper.INSTANCE.getContext(StreamModuleGroupDefine.GROUP_NAME);
try {
logger.debug("send to segment buffer write worker, id: {}", id);
context.getClusterWorkerContext().lookup(SegmentStandardizationWorker.WorkerRole.INSTANCE).tell(upstreamSegment);
} catch (WorkerInvokeException | WorkerNotFoundException e) {
logger.error(e.getMessage(), e);
}
}
private void notifyListenerToBuild() {
spanListeners.forEach(SpanListener::build);
}
private void notifyExitListener(SpanObject spanObject, int applicationId, int applicationInstanceId,
private void notifyExitListener(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
for (SpanListener listener : spanListeners) {
if (listener instanceof ExitSpanListener) {
((ExitSpanListener)listener).parseExit(spanObject, applicationId, applicationInstanceId, segmentId);
((ExitSpanListener)listener).parseExit(spanDecorator, applicationId, applicationInstanceId, segmentId);
}
}
}
private void notifyEntryListener(SpanObject spanObject, int applicationId, int applicationInstanceId,
private void notifyEntryListener(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
for (SpanListener listener : spanListeners) {
if (listener instanceof EntrySpanListener) {
((EntrySpanListener)listener).parseEntry(spanObject, applicationId, applicationInstanceId, segmentId);
((EntrySpanListener)listener).parseEntry(spanDecorator, applicationId, applicationInstanceId, segmentId);
}
}
}
private void notifyLocalListener(SpanObject spanObject, int applicationId, int applicationInstanceId,
private void notifyLocalListener(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
for (SpanListener listener : spanListeners) {
if (listener instanceof LocalSpanListener) {
((LocalSpanListener)listener).parseLocal(spanObject, applicationId, applicationInstanceId, segmentId);
((LocalSpanListener)listener).parseLocal(spanDecorator, applicationId, applicationInstanceId, segmentId);
}
}
}
private void notifyFirstListener(SpanObject spanObject, int applicationId, int applicationInstanceId,
private void notifyFirstListener(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
for (SpanListener listener : spanListeners) {
if (listener instanceof FirstSpanListener) {
((FirstSpanListener)listener).parseFirst(spanObject, applicationId, applicationInstanceId, segmentId);
((FirstSpanListener)listener).parseFirst(spanDecorator, applicationId, applicationInstanceId, segmentId);
}
}
}
private void notifyRefsListener(TraceSegmentReference reference, int applicationId, int applicationInstanceId,
private void notifyRefsListener(ReferenceDecorator reference, int applicationId, int applicationInstanceId,
String segmentId) {
for (SpanListener listener : spanListeners) {
if (listener instanceof RefsListener) {
......@@ -182,4 +231,8 @@ public class SegmentParse {
}
}
}
public enum Source {
Agent, Buffer
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.buffer;
/**
* @author pengys5
*/
public class Offset {
private static final String SPLIT_CHARACTER = ",";
private ReadOffset readOffset;
private WriteOffset writeOffset;
public Offset() {
readOffset = new ReadOffset();
writeOffset = new WriteOffset();
}
public String serialize() {
return readOffset.getReadFileName() + SPLIT_CHARACTER + String.valueOf(readOffset.getReadFileOffset())
+ SPLIT_CHARACTER + writeOffset.getWriteFileName() + SPLIT_CHARACTER + String.valueOf(writeOffset.getWriteFileOffset());
}
public void deserialize(String value) {
String[] values = value.split(SPLIT_CHARACTER);
if (values.length == 4) {
this.readOffset.readFileName = values[0];
this.readOffset.readFileOffset = Long.parseLong(values[1]);
this.writeOffset.writeFileName = values[2];
this.writeOffset.writeFileOffset = Long.parseLong(values[3]);
}
}
public ReadOffset getReadOffset() {
return readOffset;
}
public WriteOffset getWriteOffset() {
return writeOffset;
}
public static class ReadOffset {
private String readFileName;
private long readFileOffset = 0;
public String getReadFileName() {
return readFileName;
}
public long getReadFileOffset() {
return readFileOffset;
}
public void setReadFileName(String readFileName) {
this.readFileName = readFileName;
}
public void setReadFileOffset(long readFileOffset) {
this.readFileOffset = readFileOffset;
}
}
public static class WriteOffset {
private String writeFileName;
private long writeFileOffset = 0;
public String getWriteFileName() {
return writeFileName;
}
public long getWriteFileOffset() {
return writeFileOffset;
}
public void setWriteFileName(String writeFileName) {
this.writeFileName = writeFileName;
}
public void setWriteFileOffset(long writeFileOffset) {
this.writeFileOffset = writeFileOffset;
}
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.buffer;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.skywalking.apm.collector.agentstream.config.BufferFileConfig;
import org.skywalking.apm.collector.agentstream.worker.util.FileUtils;
import org.skywalking.apm.collector.core.util.CollectionUtils;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public enum OffsetManager {
INSTANCE;
private final Logger logger = LoggerFactory.getLogger(OffsetManager.class);
private static final String OFFSET_FILE_PREFIX = "offset";
private File offsetFile;
private Offset offset;
private boolean initialized = false;
private RandomAccessFile randomAccessFile = null;
private String lastOffsetRecord = Const.EMPTY_STRING;
public synchronized void initialize() throws IOException {
if (!initialized) {
this.offset = new Offset();
File dataPath = new File(SegmentBufferConfig.BUFFER_PATH);
if (dataPath.mkdirs()) {
createOffsetFile();
} else {
File[] offsetFiles = dataPath.listFiles(new PrefixFileNameFilter());
if (CollectionUtils.isNotEmpty(offsetFiles) && offsetFiles.length > 0) {
for (int i = 0; i < offsetFiles.length; i++) {
if (i != offsetFiles.length - 1) {
offsetFiles[i].delete();
} else {
offsetFile = offsetFiles[i];
}
}
} else {
createOffsetFile();
}
}
String offsetRecord = FileUtils.INSTANCE.readLastLine(offsetFile);
offset.deserialize(offsetRecord);
initialized = true;
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> flush(), 10, 3, TimeUnit.SECONDS);
}
}
private void createOffsetFile() throws IOException {
String timeBucket = String.valueOf(TimeBucketUtils.INSTANCE.getSecondTimeBucket(System.currentTimeMillis()));
String offsetFileName = OFFSET_FILE_PREFIX + "_" + timeBucket + "." + Const.FILE_SUFFIX;
offsetFile = new File(SegmentBufferConfig.BUFFER_PATH + offsetFileName);
this.offset.getWriteOffset().setWriteFileName(Const.EMPTY_STRING);
this.offset.getWriteOffset().setWriteFileOffset(0);
this.offset.getReadOffset().setReadFileName(Const.EMPTY_STRING);
this.offset.getReadOffset().setReadFileOffset(0);
this.flush();
}
public void flush() {
String offsetRecord = offset.serialize();
if (!lastOffsetRecord.equals(offsetRecord)) {
if (offsetFile.length() >= BufferFileConfig.BUFFER_OFFSET_MAX_FILE_SIZE) {
nextFile();
}
FileUtils.INSTANCE.writeAppendToLast(offsetFile, randomAccessFile, offsetRecord);
lastOffsetRecord = offsetRecord;
}
}
private void nextFile() {
String timeBucket = String.valueOf(TimeBucketUtils.INSTANCE.getSecondTimeBucket(System.currentTimeMillis()));
String offsetFileName = OFFSET_FILE_PREFIX + "_" + timeBucket + "." + Const.FILE_SUFFIX;
File newOffsetFile = new File(SegmentBufferConfig.BUFFER_PATH + offsetFileName);
offsetFile.delete();
offsetFile = newOffsetFile;
this.flush();
}
public String getReadFileName() {
return offset.getReadOffset().getReadFileName();
}
public long getReadFileOffset() {
return offset.getReadOffset().getReadFileOffset();
}
public void setReadOffset(long readFileOffset) {
offset.getReadOffset().setReadFileOffset(readFileOffset);
}
public void setReadOffset(String readFileName, long readFileOffset) {
offset.getReadOffset().setReadFileName(readFileName);
offset.getReadOffset().setReadFileOffset(readFileOffset);
}
public String getWriteFileName() {
return offset.getWriteOffset().getWriteFileName();
}
public long getWriteFileOffset() {
return offset.getWriteOffset().getWriteFileOffset();
}
public void setWriteOffset(String writeFileName, long writeFileOffset) {
offset.getWriteOffset().setWriteFileName(writeFileName);
offset.getWriteOffset().setWriteFileOffset(writeFileOffset);
}
public void setWriteOffset(long writeFileOffset) {
offset.getWriteOffset().setWriteFileOffset(writeFileOffset);
}
class PrefixFileNameFilter implements FilenameFilter {
@Override public boolean accept(File dir, String name) {
return name.startsWith(OFFSET_FILE_PREFIX);
}
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.buffer;
import org.skywalking.apm.collector.core.config.SystemConfig;
/**
* @author pengys5
*/
public class SegmentBufferConfig {
public static String BUFFER_PATH = SystemConfig.DATA_PATH + "/buffer/";
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.buffer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.skywalking.apm.collector.agentstream.config.BufferFileConfig;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.network.proto.UpstreamSegment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public enum SegmentBufferManager {
INSTANCE;
private final Logger logger = LoggerFactory.getLogger(SegmentBufferManager.class);
public static final String DATA_FILE_PREFIX = "data";
private FileOutputStream outputStream;
public synchronized void initialize() {
logger.info("segment buffer initialize");
try {
OffsetManager.INSTANCE.initialize();
if (new File(SegmentBufferConfig.BUFFER_PATH).mkdirs()) {
newDataFile();
} else {
String writeFileName = OffsetManager.INSTANCE.getWriteFileName();
if (StringUtils.isNotEmpty(writeFileName)) {
File dataFile = new File(SegmentBufferConfig.BUFFER_PATH + writeFileName);
if (dataFile.exists()) {
outputStream = new FileOutputStream(new File(SegmentBufferConfig.BUFFER_PATH + writeFileName), true);
} else {
newDataFile();
}
} else {
newDataFile();
}
}
SegmentBufferReader.INSTANCE.initialize();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
public synchronized void writeBuffer(UpstreamSegment segment) {
try {
segment.writeDelimitedTo(outputStream);
long position = outputStream.getChannel().position();
if (position > BufferFileConfig.BUFFER_SEGMENT_MAX_FILE_SIZE) {
newDataFile();
} else {
OffsetManager.INSTANCE.setWriteOffset(position);
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
private void newDataFile() throws IOException {
logger.debug("create new segment buffer file");
String timeBucket = String.valueOf(TimeBucketUtils.INSTANCE.getSecondTimeBucket(System.currentTimeMillis()));
String writeFileName = DATA_FILE_PREFIX + "_" + timeBucket + "." + Const.FILE_SUFFIX;
File dataFile = new File(SegmentBufferConfig.BUFFER_PATH + writeFileName);
dataFile.createNewFile();
OffsetManager.INSTANCE.setWriteOffset(writeFileName, 0);
try {
if (outputStream != null) {
outputStream.close();
}
outputStream = new FileOutputStream(dataFile);
outputStream.getChannel().position(0);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
public synchronized void flush() {
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.buffer;
import com.google.protobuf.CodedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.skywalking.apm.collector.agentstream.worker.segment.SegmentParse;
import org.skywalking.apm.collector.core.util.CollectionUtils;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.network.proto.UpstreamSegment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public enum SegmentBufferReader {
INSTANCE;
private final Logger logger = LoggerFactory.getLogger(SegmentBufferReader.class);
private InputStream inputStream;
public void initialize() {
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(this::preRead, 3, 3, TimeUnit.SECONDS);
}
private void preRead() {
String readFileName = OffsetManager.INSTANCE.getReadFileName();
if (StringUtils.isNotEmpty(readFileName)) {
File readFile = new File(SegmentBufferConfig.BUFFER_PATH + readFileName);
if (readFile.exists()) {
deleteTheDataFilesBeforeReadFile(readFileName);
long readFileOffset = OffsetManager.INSTANCE.getReadFileOffset();
read(readFile, readFileOffset);
readEarliestCreateDataFile();
} else {
deleteTheDataFilesBeforeReadFile(readFileName);
readEarliestCreateDataFile();
}
} else {
readEarliestCreateDataFile();
}
}
private void deleteTheDataFilesBeforeReadFile(String readFileName) {
File[] dataFiles = new File(SegmentBufferConfig.BUFFER_PATH).listFiles(new PrefixFileNameFilter());
long readFileCreateTime = getFileCreateTime(readFileName);
for (File dataFile : dataFiles) {
long fileCreateTime = getFileCreateTime(dataFile.getName());
if (fileCreateTime < readFileCreateTime) {
dataFile.delete();
} else if (fileCreateTime == readFileCreateTime) {
break;
}
}
}
private long getFileCreateTime(String fileName) {
fileName = fileName.replace(SegmentBufferManager.DATA_FILE_PREFIX + "_", Const.EMPTY_STRING);
fileName = fileName.replace("." + Const.FILE_SUFFIX, Const.EMPTY_STRING);
return Long.valueOf(fileName);
}
private void readEarliestCreateDataFile() {
String readFileName = OffsetManager.INSTANCE.getReadFileName();
File[] dataFiles = new File(SegmentBufferConfig.BUFFER_PATH).listFiles(new PrefixFileNameFilter());
if (CollectionUtils.isNotEmpty(dataFiles)) {
if (dataFiles[0].getName().equals(readFileName)) {
return;
}
}
for (File dataFile : dataFiles) {
logger.debug("Reading segment buffer data file, file name: {}", dataFile.getAbsolutePath());
OffsetManager.INSTANCE.setReadOffset(dataFile.getName(), 0);
if (!read(dataFile, 0)) {
break;
}
}
}
private boolean read(File readFile, long readFileOffset) {
try {
inputStream = new FileInputStream(readFile);
inputStream.skip(readFileOffset);
String writeFileName = OffsetManager.INSTANCE.getWriteFileName();
long endPoint = readFile.length();
if (writeFileName.equals(readFile.getName())) {
endPoint = OffsetManager.INSTANCE.getWriteFileOffset();
}
while (readFile.length() > readFileOffset && readFileOffset < endPoint) {
UpstreamSegment upstreamSegment = UpstreamSegment.parser().parseDelimitedFrom(inputStream);
SegmentParse parse = new SegmentParse();
if (!parse.parse(upstreamSegment, SegmentParse.Source.Buffer)) {
return false;
}
final int serialized = upstreamSegment.getSerializedSize();
readFileOffset = readFileOffset + CodedOutputStream.computeUInt32SizeNoTag(serialized) + serialized;
logger.debug("read segment buffer from file: {}, offset: {}, file length: {}", readFile.getName(), readFileOffset, readFile.length());
OffsetManager.INSTANCE.setReadOffset(readFileOffset);
}
inputStream.close();
if (!writeFileName.equals(readFile.getName())) {
readFile.delete();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
return false;
}
return true;
}
class PrefixFileNameFilter implements FilenameFilter {
@Override public boolean accept(File dir, String name) {
return name.startsWith(SegmentBufferManager.DATA_FILE_PREFIX);
}
}
}
......@@ -20,11 +20,12 @@ package org.skywalking.apm.collector.agentstream.worker.segment.cost;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.cache.ServiceCache;
import org.skywalking.apm.collector.agentstream.worker.segment.EntrySpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.ExitSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.FirstSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.LocalSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.cache.ServiceNameCache;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.collector.storage.define.segment.SegmentCostDataDefine;
......@@ -32,14 +33,13 @@ import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class SegmentCostSpanListener implements EntrySpanListener, ExitSpanListener, LocalSpanListener,FirstSpanListener {
public class SegmentCostSpanListener implements EntrySpanListener, ExitSpanListener, LocalSpanListener, FirstSpanListener {
private final Logger logger = LoggerFactory.getLogger(SegmentCostSpanListener.class);
......@@ -48,39 +48,42 @@ public class SegmentCostSpanListener implements EntrySpanListener, ExitSpanListe
private long timeBucket;
@Override
public void parseFirst(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime());
public void parseFirst(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime());
SegmentCostDataDefine.SegmentCost segmentCost = new SegmentCostDataDefine.SegmentCost();
segmentCost.setSegmentId(segmentId);
segmentCost.setApplicationId(applicationId);
segmentCost.setCost(spanObject.getEndTime() - spanObject.getStartTime());
segmentCost.setStartTime(spanObject.getStartTime());
segmentCost.setEndTime(spanObject.getEndTime());
segmentCost.setCost(spanDecorator.getEndTime() - spanDecorator.getStartTime());
segmentCost.setStartTime(spanDecorator.getStartTime());
segmentCost.setEndTime(spanDecorator.getEndTime());
segmentCost.setId(segmentId);
if (spanObject.getOperationNameId() == 0) {
segmentCost.setServiceName(spanObject.getOperationName());
if (spanDecorator.getOperationNameId() == 0) {
segmentCost.setServiceName(spanDecorator.getOperationName());
} else {
segmentCost.setServiceName(ServiceCache.getServiceName(spanObject.getOperationNameId()));
segmentCost.setServiceName(ServiceNameCache.getSplitServiceName(ServiceNameCache.get(spanDecorator.getOperationNameId())));
}
segmentCosts.add(segmentCost);
isError = isError || spanObject.getIsError();
isError = isError || spanDecorator.getIsError();
}
@Override
public void parseEntry(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
isError = isError || spanObject.getIsError();
public void parseEntry(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
isError = isError || spanDecorator.getIsError();
}
@Override
public void parseExit(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
isError = isError || spanObject.getIsError();
public void parseExit(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId, String segmentId) {
isError = isError || spanDecorator.getIsError();
}
@Override
public void parseLocal(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
isError = isError || spanObject.getIsError();
public void parseLocal(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
isError = isError || spanDecorator.getIsError();
}
@Override public void build() {
......
......@@ -18,10 +18,8 @@
package org.skywalking.apm.collector.agentstream.worker.segment.origin.dao;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.agentstream.worker.segment.cost.dao.SegmentCostH2DAO;
import org.skywalking.apm.collector.core.stream.Data;
import org.skywalking.apm.collector.storage.define.DataDefine;
......@@ -38,14 +36,16 @@ import org.slf4j.LoggerFactory;
*/
public class SegmentH2DAO extends H2DAO implements ISegmentDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(SegmentCostH2DAO.class);
@Override public Data get(String id, DataDefine dataDefine) {
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
Map<String, Object> source = new HashMap<>();
H2SqlEntity entity = new H2SqlEntity();
source.put("id", data.getDataString(0));
source.put(SegmentTable.COLUMN_DATA_BINARY, Base64.getEncoder().encode(data.getDataBytes(0)));
source.put(SegmentTable.COLUMN_DATA_BINARY, data.getDataBytes(0));
logger.debug("segment source: {}", source.toString());
String sql = SqlBuilder.buildBatchInsertSql(SegmentTable.TABLE, source.keySet());
......@@ -53,6 +53,7 @@ public class SegmentH2DAO extends H2DAO implements ISegmentDAO, IPersistenceDAO<
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
return null;
}
......
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.standardization;
/**
* @author pengys5
*/
public interface IdExchanger<T extends StandardBuilder> {
boolean exchange(T standardBuilder, int applicationId);
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.standardization;
import org.skywalking.apm.network.proto.RefType;
import org.skywalking.apm.network.proto.TraceSegmentReference;
import org.skywalking.apm.network.proto.UniqueId;
/**
* @author pengys5
*/
public class ReferenceDecorator implements StandardBuilder {
private boolean isOrigin = true;
private StandardBuilder standardBuilder;
private TraceSegmentReference referenceObject;
private TraceSegmentReference.Builder referenceBuilder;
public ReferenceDecorator(TraceSegmentReference referenceObject, StandardBuilder standardBuilder) {
this.referenceObject = referenceObject;
this.standardBuilder = standardBuilder;
}
public ReferenceDecorator(TraceSegmentReference.Builder referenceBuilder, StandardBuilder standardBuilder) {
this.referenceBuilder = referenceBuilder;
this.standardBuilder = standardBuilder;
this.isOrigin = false;
}
public RefType getRefType() {
if (isOrigin) {
return referenceObject.getRefType();
} else {
return referenceBuilder.getRefType();
}
}
public int getRefTypeValue() {
if (isOrigin) {
return referenceObject.getRefTypeValue();
} else {
return referenceBuilder.getRefTypeValue();
}
}
public int getEntryServiceId() {
if (isOrigin) {
return referenceObject.getEntryServiceId();
} else {
return referenceBuilder.getEntryServiceId();
}
}
public void setEntryServiceId(int value) {
if (isOrigin) {
toBuilder();
} else {
referenceBuilder.setEntryServiceId(value);
}
}
public String getEntryServiceName() {
if (isOrigin) {
return referenceObject.getEntryServiceName();
} else {
return referenceBuilder.getEntryServiceName();
}
}
public void setEntryServiceName(String value) {
if (isOrigin) {
toBuilder();
} else {
referenceBuilder.setEntryServiceName(value);
}
}
public int getEntryApplicationInstanceId() {
if (isOrigin) {
return referenceObject.getEntryApplicationInstanceId();
} else {
return referenceBuilder.getEntryApplicationInstanceId();
}
}
public int getParentApplicationInstanceId() {
if (isOrigin) {
return referenceObject.getParentApplicationInstanceId();
} else {
return referenceBuilder.getParentApplicationInstanceId();
}
}
public int getParentServiceId() {
if (isOrigin) {
return referenceObject.getParentServiceId();
} else {
return referenceBuilder.getParentServiceId();
}
}
public void setParentServiceId(int value) {
if (isOrigin) {
toBuilder();
} else {
referenceBuilder.setParentServiceId(value);
}
}
public int getParentSpanId() {
if (isOrigin) {
return referenceObject.getParentSpanId();
} else {
return referenceBuilder.getParentSpanId();
}
}
public String getParentServiceName() {
if (isOrigin) {
return referenceObject.getParentServiceName();
} else {
return referenceBuilder.getParentServiceName();
}
}
public void setParentServiceName(String value) {
if (isOrigin) {
toBuilder();
} else {
referenceBuilder.setParentServiceName(value);
}
}
public UniqueId getParentTraceSegmentId() {
if (isOrigin) {
return referenceObject.getParentTraceSegmentId();
} else {
return referenceBuilder.getParentTraceSegmentId();
}
}
public int getNetworkAddressId() {
if (isOrigin) {
return referenceObject.getNetworkAddressId();
} else {
return referenceBuilder.getNetworkAddressId();
}
}
public void setNetworkAddressId(int value) {
if (isOrigin) {
toBuilder();
} else {
referenceBuilder.setNetworkAddressId(value);
}
}
public String getNetworkAddress() {
if (isOrigin) {
return referenceObject.getNetworkAddress();
} else {
return referenceBuilder.getNetworkAddress();
}
}
public void setNetworkAddress(String value) {
if (isOrigin) {
toBuilder();
} else {
referenceBuilder.setNetworkAddress(value);
}
}
@Override public void toBuilder() {
this.isOrigin = false;
referenceBuilder = referenceObject.toBuilder();
standardBuilder.toBuilder();
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.standardization;
import org.skywalking.apm.collector.cache.ApplicationCache;
import org.skywalking.apm.collector.cache.InstanceCache;
import org.skywalking.apm.collector.cache.ServiceIdCache;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class ReferenceIdExchanger implements IdExchanger<ReferenceDecorator> {
private final Logger logger = LoggerFactory.getLogger(ReferenceIdExchanger.class);
private static ReferenceIdExchanger EXCHANGER;
public static ReferenceIdExchanger getInstance() {
if (EXCHANGER == null) {
EXCHANGER = new ReferenceIdExchanger();
}
return EXCHANGER;
}
@Override public boolean exchange(ReferenceDecorator standardBuilder, int applicationId) {
if (standardBuilder.getEntryServiceId() == 0 && StringUtils.isNotEmpty(standardBuilder.getEntryServiceName())) {
int entryServiceId = ServiceIdCache.get(InstanceCache.get(standardBuilder.getEntryApplicationInstanceId()), standardBuilder.getEntryServiceName());
if (entryServiceId == 0) {
return false;
} else {
standardBuilder.toBuilder();
standardBuilder.setEntryServiceId(entryServiceId);
standardBuilder.setEntryServiceName(Const.EMPTY_STRING);
}
}
if (standardBuilder.getParentServiceId() == 0 && StringUtils.isNotEmpty(standardBuilder.getParentServiceName())) {
int parentServiceId = ServiceIdCache.get(InstanceCache.get(standardBuilder.getParentApplicationInstanceId()), standardBuilder.getParentServiceName());
if (parentServiceId == 0) {
return false;
} else {
standardBuilder.toBuilder();
standardBuilder.setParentServiceId(parentServiceId);
standardBuilder.setParentServiceName(Const.EMPTY_STRING);
}
}
if (standardBuilder.getNetworkAddressId() == 0 && StringUtils.isNotEmpty(standardBuilder.getNetworkAddress())) {
int networkAddressId = ApplicationCache.get(standardBuilder.getNetworkAddress());
if (networkAddressId == 0) {
return false;
} else {
standardBuilder.toBuilder();
standardBuilder.setNetworkAddressId(networkAddressId);
standardBuilder.setNetworkAddress(Const.EMPTY_STRING);
}
}
return true;
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.standardization;
import org.skywalking.apm.network.proto.TraceSegmentObject;
import org.skywalking.apm.network.proto.UniqueId;
/**
* @author pengys5
*/
public class SegmentDecorator implements StandardBuilder {
private boolean isOrigin = true;
private final TraceSegmentObject segmentObject;
private TraceSegmentObject.Builder segmentBuilder;
public SegmentDecorator(TraceSegmentObject segmentObject) {
this.segmentObject = segmentObject;
}
public int getApplicationId() {
return segmentObject.getApplicationId();
}
public int getApplicationInstanceId() {
return segmentObject.getApplicationInstanceId();
}
public UniqueId getTraceSegmentId() {
return segmentObject.getTraceSegmentId();
}
public int getSpansCount() {
return segmentObject.getSpansCount();
}
public SpanDecorator getSpans(int index) {
if (isOrigin) {
return new SpanDecorator(segmentObject.getSpans(index), this);
} else {
return new SpanDecorator(segmentBuilder.getSpansBuilder(index), this);
}
}
public int getRefsCount() {
return segmentObject.getRefsCount();
}
public ReferenceDecorator getRefs(int index) {
if (isOrigin) {
return new ReferenceDecorator(segmentObject.getRefs(index), this);
} else {
return new ReferenceDecorator(segmentBuilder.getRefsBuilder(index), this);
}
}
public byte[] toByteArray() {
if (isOrigin) {
return segmentObject.toByteArray();
} else {
return segmentBuilder.build().toByteArray();
}
}
@Override public void toBuilder() {
if (!isOrigin) {
this.isOrigin = false;
this.segmentBuilder = segmentObject.toBuilder();
}
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.standardization;
import org.skywalking.apm.collector.agentstream.worker.segment.buffer.SegmentBufferManager;
import org.skywalking.apm.collector.core.queue.EndOfBatchCommand;
import org.skywalking.apm.collector.storage.define.DataDefine;
import org.skywalking.apm.collector.stream.worker.AbstractLocalAsyncWorker;
import org.skywalking.apm.collector.stream.worker.AbstractLocalAsyncWorkerProvider;
import org.skywalking.apm.collector.stream.worker.ClusterWorkerContext;
import org.skywalking.apm.collector.stream.worker.ProviderNotFoundException;
import org.skywalking.apm.collector.stream.worker.Role;
import org.skywalking.apm.collector.stream.worker.WorkerException;
import org.skywalking.apm.collector.stream.worker.impl.FlushAndSwitch;
import org.skywalking.apm.collector.stream.worker.selector.ForeverFirstSelector;
import org.skywalking.apm.collector.stream.worker.selector.WorkerSelector;
import org.skywalking.apm.network.proto.UpstreamSegment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class SegmentStandardizationWorker extends AbstractLocalAsyncWorker {
private final Logger logger = LoggerFactory.getLogger(SegmentStandardizationWorker.class);
public SegmentStandardizationWorker(Role role, ClusterWorkerContext clusterContext) {
super(role, clusterContext);
}
@Override public void preStart() throws ProviderNotFoundException {
super.preStart();
SegmentBufferManager.INSTANCE.initialize();
}
@Override protected void onWork(Object message) throws WorkerException {
if (message instanceof FlushAndSwitch) {
SegmentBufferManager.INSTANCE.flush();
} else if (message instanceof EndOfBatchCommand) {
} else if (message instanceof UpstreamSegment) {
UpstreamSegment upstreamSegment = (UpstreamSegment)message;
SegmentBufferManager.INSTANCE.writeBuffer(upstreamSegment);
} else {
logger.error("unhandled message, message instance must UpstreamSegment, but is %s", message.getClass().toString());
}
}
public static class Factory extends AbstractLocalAsyncWorkerProvider<SegmentStandardizationWorker> {
@Override
public Role role() {
return SegmentStandardizationWorker.WorkerRole.INSTANCE;
}
@Override
public SegmentStandardizationWorker workerInstance(ClusterWorkerContext clusterContext) {
return new SegmentStandardizationWorker(role(), clusterContext);
}
@Override public int queueSize() {
return 1024;
}
}
public enum WorkerRole implements Role {
INSTANCE;
@Override
public String roleName() {
return SegmentStandardizationWorker.class.getSimpleName();
}
@Override
public WorkerSelector workerSelector() {
return new ForeverFirstSelector();
}
@Override public DataDefine dataDefine() {
return null;
}
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.standardization;
import org.skywalking.apm.network.proto.SpanLayer;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.SpanType;
/**
* @author pengys5
*/
public class SpanDecorator implements StandardBuilder {
private boolean isOrigin = true;
private StandardBuilder standardBuilder;
private SpanObject spanObject;
private SpanObject.Builder spanBuilder;
public SpanDecorator(SpanObject spanObject, StandardBuilder standardBuilder) {
this.spanObject = spanObject;
this.standardBuilder = standardBuilder;
}
public SpanDecorator(SpanObject.Builder spanBuilder, StandardBuilder standardBuilder) {
this.spanBuilder = spanBuilder;
this.standardBuilder = standardBuilder;
this.isOrigin = false;
}
public int getSpanId() {
if (isOrigin) {
return spanObject.getSpanId();
} else {
return spanBuilder.getSpanId();
}
}
public int getParentSpanId() {
if (isOrigin) {
return spanObject.getParentSpanId();
} else {
return spanBuilder.getParentSpanId();
}
}
public SpanType getSpanType() {
if (isOrigin) {
return spanObject.getSpanType();
} else {
return spanBuilder.getSpanType();
}
}
public int getSpanTypeValue() {
if (isOrigin) {
return spanObject.getSpanTypeValue();
} else {
return spanBuilder.getSpanTypeValue();
}
}
public SpanLayer getSpanLayer() {
if (isOrigin) {
return spanObject.getSpanLayer();
} else {
return spanBuilder.getSpanLayer();
}
}
public int getSpanLayerValue() {
if (isOrigin) {
return spanObject.getSpanLayerValue();
} else {
return spanBuilder.getSpanLayerValue();
}
}
public long getStartTime() {
if (isOrigin) {
return spanObject.getStartTime();
} else {
return spanBuilder.getStartTime();
}
}
public long getEndTime() {
if (isOrigin) {
return spanObject.getEndTime();
} else {
return spanBuilder.getEndTime();
}
}
public int getComponentId() {
if (isOrigin) {
return spanObject.getComponentId();
} else {
return spanBuilder.getComponentId();
}
}
public String getComponent() {
if (isOrigin) {
return spanObject.getComponent();
} else {
return spanBuilder.getComponent();
}
}
public int getPeerId() {
if (isOrigin) {
return spanObject.getPeerId();
} else {
return spanBuilder.getPeerId();
}
}
public void setPeerId(int peerId) {
if (isOrigin) {
toBuilder();
} else {
spanBuilder.setPeerId(peerId);
}
}
public String getPeer() {
if (isOrigin) {
return spanObject.getPeer();
} else {
return spanBuilder.getPeer();
}
}
public void setPeer(String peer) {
if (isOrigin) {
toBuilder();
} else {
spanBuilder.setPeer(peer);
}
}
public int getOperationNameId() {
if (isOrigin) {
return spanObject.getOperationNameId();
} else {
return spanBuilder.getOperationNameId();
}
}
public void setOperationNameId(int value) {
if (isOrigin) {
toBuilder();
} else {
spanBuilder.setOperationNameId(value);
}
}
public String getOperationName() {
if (isOrigin) {
return spanObject.getOperationName();
} else {
return spanBuilder.getOperationName();
}
}
public void setOperationName(String value) {
if (isOrigin) {
toBuilder();
} else {
spanBuilder.setOperationName(value);
}
}
public boolean getIsError() {
if (isOrigin) {
return spanObject.getIsError();
} else {
return spanBuilder.getIsError();
}
}
@Override public void toBuilder() {
this.isOrigin = false;
spanBuilder = spanObject.toBuilder();
standardBuilder.toBuilder();
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.standardization;
import org.skywalking.apm.collector.cache.ApplicationCache;
import org.skywalking.apm.collector.cache.ServiceIdCache;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.StringUtils;
/**
* @author pengys5
*/
public class SpanIdExchanger implements IdExchanger<SpanDecorator> {
private static SpanIdExchanger EXCHANGER;
public static SpanIdExchanger getInstance() {
if (EXCHANGER == null) {
EXCHANGER = new SpanIdExchanger();
}
return EXCHANGER;
}
@Override public boolean exchange(SpanDecorator standardBuilder, int applicationId) {
if (standardBuilder.getPeerId() == 0 && StringUtils.isNotEmpty(standardBuilder.getPeer())) {
int peerId = ApplicationCache.get(standardBuilder.getPeer());
if (peerId == 0) {
return false;
} else {
standardBuilder.toBuilder();
standardBuilder.setPeerId(peerId);
standardBuilder.setPeer(Const.EMPTY_STRING);
}
}
if (standardBuilder.getOperationNameId() == 0 && StringUtils.isNotEmpty(standardBuilder.getOperationName())) {
int operationNameId = ServiceIdCache.get(applicationId, standardBuilder.getOperationName());
if (operationNameId == 0) {
return false;
} else {
standardBuilder.toBuilder();
standardBuilder.setOperationNameId(operationNameId);
standardBuilder.setOperationName(Const.EMPTY_STRING);
}
}
return true;
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.standardization;
/**
* @author pengys5
*/
public interface StandardBuilder {
void toBuilder();
}
......@@ -18,10 +18,12 @@
package org.skywalking.apm.collector.agentstream.worker.service.entry;
import org.skywalking.apm.collector.agentstream.worker.cache.ServiceCache;
import org.skywalking.apm.collector.agentstream.worker.segment.EntrySpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.FirstSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.RefsListener;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.ReferenceDecorator;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.cache.ServiceNameCache;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
......@@ -30,8 +32,6 @@ import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.TraceSegmentReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -50,25 +50,23 @@ public class ServiceEntrySpanListener implements RefsListener, FirstSpanListener
private boolean hasEntry = false;
@Override
public void parseEntry(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
public void parseEntry(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
this.applicationId = applicationId;
this.entryServiceId = spanObject.getOperationNameId();
if (spanObject.getOperationNameId() == 0) {
this.entryServiceName = spanObject.getOperationName();
} else {
this.entryServiceName = ServiceCache.getServiceName(this.entryServiceId);
}
this.entryServiceId = spanDecorator.getOperationNameId();
this.entryServiceName = ServiceNameCache.getSplitServiceName(ServiceNameCache.get(entryServiceId));
this.hasEntry = true;
}
@Override public void parseRef(TraceSegmentReference reference, int applicationId, int applicationInstanceId,
@Override public void parseRef(ReferenceDecorator referenceDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
hasReference = true;
}
@Override
public void parseFirst(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime());
public void parseFirst(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime());
}
@Override public void build() {
......@@ -76,7 +74,7 @@ public class ServiceEntrySpanListener implements RefsListener, FirstSpanListener
StreamModuleContext context = (StreamModuleContext)CollectorContextHelper.INSTANCE.getContext(StreamModuleGroupDefine.GROUP_NAME);
if (!hasReference && hasEntry) {
ServiceEntryDataDefine.ServiceEntry serviceEntry = new ServiceEntryDataDefine.ServiceEntry();
serviceEntry.setId(applicationId + Const.ID_SPLIT + entryServiceName);
serviceEntry.setId(applicationId + Const.ID_SPLIT + entryServiceId);
serviceEntry.setApplicationId(applicationId);
serviceEntry.setEntryServiceId(entryServiceId);
serviceEntry.setEntryServiceName(entryServiceName);
......
......@@ -24,7 +24,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.stream.Data;
......@@ -42,10 +41,11 @@ import org.slf4j.LoggerFactory;
*/
public class ServiceEntryH2DAO extends H2DAO implements IServiceEntryDAO, IPersistenceDAO<H2SqlEntity, H2SqlEntity> {
private final Logger logger = LoggerFactory.getLogger(ServiceEntryH2DAO.class);
private static final String GET_SERIVCE_ENTRY_SQL = "select * from {0} where {1} = ?";
private static final String GET_SERVICE_ENTRY_SQL = "select * from {0} where {1} = ?";
@Override public Data get(String id, DataDefine dataDefine) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(ServiceEntryTable.TABLE, "id");
String sql = SqlBuilder.buildSql(GET_SERVICE_ENTRY_SQL, ServiceEntryTable.TABLE, "id");
Object[] params = new Object[] {id};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
......@@ -62,6 +62,7 @@ public class ServiceEntryH2DAO extends H2DAO implements IServiceEntryDAO, IPersi
}
return null;
}
@Override public H2SqlEntity prepareBatchInsert(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
......@@ -76,6 +77,7 @@ public class ServiceEntryH2DAO extends H2DAO implements IServiceEntryDAO, IPersi
entity.setParams(source.values().toArray(new Object[0]));
return entity;
}
@Override public H2SqlEntity prepareBatchUpdate(Data data) {
H2SqlEntity entity = new H2SqlEntity();
Map<String, Object> source = new HashMap<>();
......
......@@ -20,10 +20,11 @@ package org.skywalking.apm.collector.agentstream.worker.serviceref;
import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.collector.agentstream.worker.cache.InstanceCache;
import org.skywalking.apm.collector.agentstream.worker.segment.EntrySpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.FirstSpanListener;
import org.skywalking.apm.collector.agentstream.worker.segment.RefsListener;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.ReferenceDecorator;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SpanDecorator;
import org.skywalking.apm.collector.core.framework.CollectorContextHelper;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
......@@ -32,8 +33,6 @@ import org.skywalking.apm.collector.stream.StreamModuleContext;
import org.skywalking.apm.collector.stream.StreamModuleGroupDefine;
import org.skywalking.apm.collector.stream.worker.WorkerInvokeException;
import org.skywalking.apm.collector.stream.worker.WorkerNotFoundException;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.TraceSegmentReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -44,9 +43,8 @@ public class ServiceReferenceSpanListener implements FirstSpanListener, EntrySpa
private final Logger logger = LoggerFactory.getLogger(ServiceReferenceSpanListener.class);
private List<TraceSegmentReference> referenceServices = new LinkedList<>();
private List<ReferenceDecorator> referenceServices = new LinkedList<>();
private int serviceId = 0;
private String serviceName = "";
private long startTime = 0;
private long endTime = 0;
private boolean isError = false;
......@@ -54,26 +52,23 @@ public class ServiceReferenceSpanListener implements FirstSpanListener, EntrySpa
private boolean hasEntry = false;
@Override
public void parseFirst(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanObject.getStartTime());
public void parseFirst(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime());
}
@Override public void parseRef(TraceSegmentReference reference, int applicationId, int applicationInstanceId,
@Override public void parseRef(ReferenceDecorator referenceDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
referenceServices.add(reference);
referenceServices.add(referenceDecorator);
}
@Override
public void parseEntry(SpanObject spanObject, int applicationId, int applicationInstanceId, String segmentId) {
serviceId = spanObject.getOperationNameId();
if (spanObject.getOperationNameId() == 0) {
serviceName = String.valueOf(applicationId) + Const.ID_SPLIT + spanObject.getOperationName();
} else {
serviceName = Const.EMPTY_STRING;
}
startTime = spanObject.getStartTime();
endTime = spanObject.getEndTime();
isError = spanObject.getIsError();
public void parseEntry(SpanDecorator spanDecorator, int applicationId, int applicationInstanceId,
String segmentId) {
serviceId = spanDecorator.getOperationNameId();
startTime = spanDecorator.getStartTime();
endTime = spanDecorator.getEndTime();
isError = spanDecorator.getIsError();
this.hasEntry = true;
}
......@@ -105,71 +100,39 @@ public class ServiceReferenceSpanListener implements FirstSpanListener, EntrySpa
referenceServices.forEach(reference -> {
ServiceReferenceDataDefine.ServiceReference serviceReference = new ServiceReferenceDataDefine.ServiceReference();
int entryServiceId = reference.getEntryServiceId();
String entryServiceName = buildServiceName(reference.getEntryApplicationInstanceId(), reference.getEntryServiceId(), reference.getEntryServiceName());
int frontServiceId = reference.getParentServiceId();
String frontServiceName = buildServiceName(reference.getParentApplicationInstanceId(), reference.getParentServiceId(), reference.getParentServiceName());
int behindServiceId = serviceId;
String behindServiceName = serviceName;
calculateCost(serviceReference, startTime, endTime, isError);
logger.debug("has reference, entryServiceId: {}, entryServiceName: {}", entryServiceId, entryServiceName);
sendToAggregationWorker(context, serviceReference, entryServiceId, entryServiceName, frontServiceId, frontServiceName, behindServiceId, behindServiceName);
logger.debug("has reference, entryServiceId: {}", entryServiceId);
sendToAggregationWorker(context, serviceReference, entryServiceId, frontServiceId, behindServiceId);
});
} else {
ServiceReferenceDataDefine.ServiceReference serviceReference = new ServiceReferenceDataDefine.ServiceReference();
int entryServiceId = serviceId;
String entryServiceName = serviceName;
int frontServiceId = Const.NONE_SERVICE_ID;
String frontServiceName = Const.EMPTY_STRING;
int behindServiceId = serviceId;
String behindServiceName = serviceName;
calculateCost(serviceReference, startTime, endTime, isError);
sendToAggregationWorker(context, serviceReference, entryServiceId, entryServiceName, frontServiceId, frontServiceName, behindServiceId, behindServiceName);
sendToAggregationWorker(context, serviceReference, entryServiceId, frontServiceId, behindServiceId);
}
}
}
private void sendToAggregationWorker(StreamModuleContext context,
ServiceReferenceDataDefine.ServiceReference serviceReference, int entryServiceId, String entryServiceName,
int frontServiceId, String frontServiceName, int behindServiceId, String behindServiceName) {
ServiceReferenceDataDefine.ServiceReference serviceReference, int entryServiceId, int frontServiceId,
int behindServiceId) {
StringBuilder idBuilder = new StringBuilder();
idBuilder.append(timeBucket).append(Const.ID_SPLIT);
if (entryServiceId == 0) {
idBuilder.append(entryServiceName).append(Const.ID_SPLIT);
serviceReference.setEntryServiceId(0);
serviceReference.setEntryServiceName(entryServiceName);
} else {
idBuilder.append(entryServiceId).append(Const.ID_SPLIT);
serviceReference.setEntryServiceId(entryServiceId);
serviceReference.setEntryServiceName(Const.EMPTY_STRING);
}
if (frontServiceId == 0) {
idBuilder.append(frontServiceName).append(Const.ID_SPLIT);
serviceReference.setFrontServiceId(0);
serviceReference.setFrontServiceName(frontServiceName);
} else {
idBuilder.append(frontServiceId).append(Const.ID_SPLIT);
serviceReference.setFrontServiceId(frontServiceId);
serviceReference.setFrontServiceName(Const.EMPTY_STRING);
}
idBuilder.append(entryServiceId).append(Const.ID_SPLIT);
serviceReference.setEntryServiceId(entryServiceId);
if (behindServiceId == 0) {
idBuilder.append(behindServiceName);
serviceReference.setBehindServiceId(0);
serviceReference.setBehindServiceName(behindServiceName);
} else {
idBuilder.append(behindServiceId);
serviceReference.setBehindServiceId(behindServiceId);
serviceReference.setBehindServiceName(Const.EMPTY_STRING);
}
idBuilder.append(frontServiceId).append(Const.ID_SPLIT);
serviceReference.setFrontServiceId(frontServiceId);
idBuilder.append(behindServiceId);
serviceReference.setBehindServiceId(behindServiceId);
serviceReference.setId(idBuilder.toString());
serviceReference.setTimeBucket(timeBucket);
......@@ -180,13 +143,4 @@ public class ServiceReferenceSpanListener implements FirstSpanListener, EntrySpa
logger.error(e.getMessage(), e);
}
}
private String buildServiceName(int instanceId, int serviceId, String serviceName) {
if (serviceId == 0) {
int applicationId = InstanceCache.get(instanceId);
return String.valueOf(applicationId) + Const.ID_SPLIT + serviceName;
} else {
return Const.EMPTY_STRING;
}
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.skywalking.apm.collector.core.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public enum FileUtils {
INSTANCE;
private final Logger logger = LoggerFactory.getLogger(FileUtils.class);
public String readLastLine(File file) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "r");
long length = randomAccessFile.length();
if (length == 0) {
return Const.EMPTY_STRING;
} else {
long position = length - 1;
randomAccessFile.seek(position);
while (position >= 0) {
if (randomAccessFile.read() == '\n') {
return randomAccessFile.readLine();
}
randomAccessFile.seek(position);
if (position == 0) {
return randomAccessFile.readLine();
}
position--;
}
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
return Const.EMPTY_STRING;
}
public void writeAppendToLast(File file, RandomAccessFile randomAccessFile, String value) {
if (randomAccessFile == null) {
try {
randomAccessFile = new RandomAccessFile(file, "rwd");
} catch (FileNotFoundException e) {
logger.error(e.getMessage(), e);
}
}
try {
long length = randomAccessFile.length();
randomAccessFile.seek(length);
randomAccessFile.writeBytes(System.lineSeparator());
randomAccessFile.writeBytes(value);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
......@@ -15,6 +15,8 @@ org.skywalking.apm.collector.agentstream.worker.serviceref.ServiceReferencePersi
org.skywalking.apm.collector.agentstream.worker.segment.origin.SegmentPersistenceWorker$Factory
org.skywalking.apm.collector.agentstream.worker.segment.cost.SegmentCostPersistenceWorker$Factory
org.skywalking.apm.collector.agentstream.worker.segment.standardization.SegmentStandardizationWorker$Factory
org.skywalking.apm.collector.agentstream.worker.global.GlobalTracePersistenceWorker$Factory
org.skywalking.apm.collector.agentstream.worker.instance.performance.InstPerformancePersistenceWorker$Factory
\ No newline at end of file
......@@ -21,6 +21,7 @@ package org.skywalking.apm.collector.agentstream.grpc.handler;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import org.junit.Test;
import org.skywalking.apm.network.proto.Downstream;
import org.skywalking.apm.network.proto.KeyWithStringValue;
import org.skywalking.apm.network.proto.LogMessage;
......@@ -45,6 +46,7 @@ public class TraceSegmentServiceHandlerTestCase {
private TraceSegmentServiceGrpc.TraceSegmentServiceStub stub;
@Test
public void testCollect() {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 11800).usePlaintext(true).build();
stub = TraceSegmentServiceGrpc.newStub(channel);
......@@ -70,7 +72,7 @@ public class TraceSegmentServiceHandlerTestCase {
streamObserver.onCompleted();
try {
Thread.sleep(30000);
Thread.sleep(10000);
} catch (InterruptedException e) {
}
}
......
......@@ -22,16 +22,21 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.IOException;
import org.skywalking.apm.collector.agentregister.worker.application.dao.ApplicationEsDAO;
import org.skywalking.apm.collector.agentregister.worker.instance.dao.InstanceEsDAO;
import org.skywalking.apm.collector.agentregister.worker.servicename.dao.ServiceNameEsDAO;
import org.skywalking.apm.collector.agentregister.worker.application.dao.IApplicationDAO;
import org.skywalking.apm.collector.agentregister.worker.instance.dao.IInstanceDAO;
import org.skywalking.apm.collector.agentregister.worker.servicename.dao.IServiceNameDAO;
import org.skywalking.apm.collector.agentstream.HttpClientTools;
import org.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.core.CollectorException;
import org.skywalking.apm.collector.core.config.SystemConfig;
import org.skywalking.apm.collector.core.util.TimeBucketUtils;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.storage.define.register.ApplicationDataDefine;
import org.skywalking.apm.collector.storage.define.register.InstanceDataDefine;
import org.skywalking.apm.collector.storage.define.register.ServiceNameDataDefine;
import org.skywalking.apm.collector.storage.elasticsearch.StorageElasticSearchModuleDefine;
import org.skywalking.apm.collector.storage.h2.StorageH2ModuleDefine;
/**
* @author pengys5
......@@ -39,39 +44,47 @@ import org.skywalking.apm.collector.storage.define.register.ServiceNameDataDefin
public class SegmentPost {
public static void main(String[] args) throws IOException, InterruptedException, CollectorException {
ElasticSearchClient client = new ElasticSearchClient("CollectorDBCluster", true, "127.0.0.1:9300");
client.initialize();
long now = TimeBucketUtils.INSTANCE.getSecondTimeBucket(System.currentTimeMillis());
SystemConfig.DATA_PATH = "/Users/pengys5/code/sky-walking/sky-walking/apm-collector/data";
ElasticSearchClient elasticSearchClient = new ElasticSearchClient("CollectorDBCluster", true, "127.0.0.1:9300");
elasticSearchClient.initialize();
StorageElasticSearchModuleDefine storageElasticSearchModuleDefine = new StorageElasticSearchModuleDefine();
storageElasticSearchModuleDefine.injectClientIntoDAO(elasticSearchClient);
InstanceEsDAO instanceEsDAO = new InstanceEsDAO();
instanceEsDAO.setClient(client);
H2Client h2Client = new H2Client("jdbc:h2:tcp://localhost/~/test", "sa", "");
h2Client.initialize();
StorageH2ModuleDefine storageH2ModuleDefine = new StorageH2ModuleDefine();
storageH2ModuleDefine.injectClientIntoDAO(h2Client);
long now = TimeBucketUtils.INSTANCE.getSecondTimeBucket(System.currentTimeMillis());
IInstanceDAO instanceDAO = (IInstanceDAO)DAOContainer.INSTANCE.get(IInstanceDAO.class.getName());
InstanceDataDefine.Instance consumerInstance = new InstanceDataDefine.Instance("2", 2, "dubbox-consumer", now, 2, now, osInfo("consumer").toString());
instanceEsDAO.save(consumerInstance);
instanceDAO.save(consumerInstance);
InstanceDataDefine.Instance providerInstance = new InstanceDataDefine.Instance("3", 3, "dubbox-provider", now, 3, now, osInfo("provider").toString());
instanceEsDAO.save(providerInstance);
instanceDAO.save(providerInstance);
ApplicationEsDAO applicationEsDAO = new ApplicationEsDAO();
applicationEsDAO.setClient(client);
IApplicationDAO applicationDAO = (IApplicationDAO)DAOContainer.INSTANCE.get(IApplicationDAO.class.getName());
ApplicationDataDefine.Application userApplication = new ApplicationDataDefine.Application("1", "User", 1);
applicationEsDAO.save(userApplication);
applicationDAO.save(userApplication);
ApplicationDataDefine.Application consumerApplication = new ApplicationDataDefine.Application("2", "dubbox-consumer", 2);
applicationEsDAO.save(consumerApplication);
applicationDAO.save(consumerApplication);
ApplicationDataDefine.Application providerApplication = new ApplicationDataDefine.Application("3", "dubbox-provider", 3);
applicationEsDAO.save(providerApplication);
applicationDAO.save(providerApplication);
ApplicationDataDefine.Application peer = new ApplicationDataDefine.Application("4", "172.25.0.4:20880", 4);
applicationDAO.save(peer);
ServiceNameEsDAO serviceNameEsDAO = new ServiceNameEsDAO();
serviceNameEsDAO.setClient(client);
IServiceNameDAO serviceNameDAO = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
ServiceNameDataDefine.ServiceName serviceName1 = new ServiceNameDataDefine.ServiceName("1", "", 0, 1);
serviceNameEsDAO.save(serviceName1);
serviceNameDAO.save(serviceName1);
ServiceNameDataDefine.ServiceName serviceName2 = new ServiceNameDataDefine.ServiceName("2", "org.skywaking.apm.testcase.dubbo.services.GreetService.doBusiness()", 2, 2);
serviceNameEsDAO.save(serviceName2);
serviceNameDAO.save(serviceName2);
ServiceNameDataDefine.ServiceName serviceName3 = new ServiceNameDataDefine.ServiceName("3", "/dubbox-case/case/dubbox-rest", 2, 3);
serviceNameEsDAO.save(serviceName3);
serviceNameDAO.save(serviceName3);
ServiceNameDataDefine.ServiceName serviceName4 = new ServiceNameDataDefine.ServiceName("4", "org.skywaking.apm.testcase.dubbo.services.GreetService.doBusiness()", 3, 4);
serviceNameEsDAO.save(serviceName4);
serviceNameDAO.save(serviceName4);
while (true) {
JsonElement consumer = JsonFileReader.INSTANCE.read("json/segment/normal/dubbox-consumer.json");
......@@ -84,6 +97,7 @@ public class SegmentPost {
DIFF = 0;
Thread.sleep(1000);
break;
}
}
......
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.segment.buffer;
import org.skywalking.apm.collector.agentstream.worker.segment.standardization.SegmentStandardizationWorker;
import org.skywalking.apm.collector.stream.worker.ProviderNotFoundException;
import org.skywalking.apm.collector.stream.worker.WorkerException;
import org.skywalking.apm.network.proto.SpanLayer;
import org.skywalking.apm.network.proto.SpanObject;
import org.skywalking.apm.network.proto.SpanType;
import org.skywalking.apm.network.proto.TraceSegmentObject;
import org.skywalking.apm.network.proto.UniqueId;
import org.skywalking.apm.network.proto.UpstreamSegment;
import org.skywalking.apm.network.trace.component.ComponentsDefine;
/**
* @author pengys5
*/
public class SegmentBufferWriteWorkerTestCase {
public static void main(String[] args) throws WorkerException, ProviderNotFoundException {
SegmentBufferConfig.BUFFER_PATH = "/Users/pengys5/code/sky-walking/sky-walking/apm-collector/buffer/";
SegmentStandardizationWorker worker = new SegmentStandardizationWorker(null, null);
worker.preStart();
worker.allocateJob(buildSegment());
worker.allocateJob(buildSegment());
worker.allocateJob(buildSegment());
worker.allocateJob(buildSegment());
worker.allocateJob(buildSegment());
}
private static UpstreamSegment buildSegment() {
long now = System.currentTimeMillis();
int id = 1;
UniqueId.Builder builder = UniqueId.newBuilder();
builder.addIdParts(id);
builder.addIdParts(id);
builder.addIdParts(id);
UniqueId segmentId = builder.build();
UpstreamSegment.Builder upstream = UpstreamSegment.newBuilder();
upstream.addGlobalTraceIds(segmentId);
TraceSegmentObject.Builder segmentBuilder = TraceSegmentObject.newBuilder();
segmentBuilder.setApplicationId(1);
segmentBuilder.setApplicationInstanceId(1);
segmentBuilder.setTraceSegmentId(segmentId);
SpanObject.Builder entrySpan = SpanObject.newBuilder();
entrySpan.setSpanId(0);
entrySpan.setSpanType(SpanType.Entry);
entrySpan.setSpanLayer(SpanLayer.Http);
entrySpan.setParentSpanId(-1);
entrySpan.setStartTime(now);
entrySpan.setEndTime(now + 3000);
entrySpan.setComponentId(ComponentsDefine.TOMCAT.getId());
entrySpan.setOperationNameId(1);
entrySpan.setIsError(false);
segmentBuilder.addSpans(entrySpan);
SpanObject.Builder exitSpan = SpanObject.newBuilder();
exitSpan.setSpanId(1);
exitSpan.setSpanType(SpanType.Exit);
exitSpan.setSpanLayer(SpanLayer.Database);
exitSpan.setParentSpanId(0);
exitSpan.setStartTime(now);
exitSpan.setEndTime(now + 3000);
exitSpan.setComponentId(ComponentsDefine.MONGODB.getId());
exitSpan.setOperationNameId(2);
exitSpan.setIsError(false);
exitSpan.setPeer("localhost:8888");
segmentBuilder.addSpans(exitSpan);
upstream.setSegment(segmentBuilder.build().toByteString());
return upstream.build();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2017, OpenSkywalking Organization All rights reserved.
~
~ Licensed 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.
~
~ Project repository: https://github.com/OpenSkywalking/skywalking
-->
<Configuration status="debug">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout charset="UTF-8" pattern="%d - %c -%-4r [%t] %-5p %x - %m%n"/>
</Console>
</Appenders>
<Loggers>
<logger name="org.eclipse.jetty" level="INFO"/>
<logger name="org.apache.zookeeper" level="INFO"/>
<logger name="org.skywalking.apm.collector.agentstream.worker.storage.PersistenceTimer" level="INFO"/>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
......@@ -19,6 +19,7 @@
package org.skywalking.apm.collector.boot;
import org.skywalking.apm.collector.core.CollectorException;
import org.skywalking.apm.collector.core.config.SystemConfigParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -31,6 +32,7 @@ public class CollectorBootStartUp {
public static void main(String[] args) throws CollectorException {
logger.info("collector starting...");
SystemConfigParser.INSTANCE.parse();
CollectorStarter starter = new CollectorStarter();
starter.start();
logger.info("collector start successful.");
......
......@@ -53,6 +53,9 @@ public class CollectorStarter implements Starter {
ServerHolder serverHolder = new ServerHolder();
for (ModuleGroupDefine moduleGroupDefine : moduleGroupDefineMap.values()) {
if (moduleGroupDefine.groupConfigParser() != null) {
moduleGroupDefine.groupConfigParser().parse(configuration.get(moduleGroupDefine.name()));
}
moduleGroupDefine.moduleInstaller().injectConfiguration(configuration.get(moduleGroupDefine.name()), moduleDefineMap.get(moduleGroupDefine.name()));
moduleGroupDefine.moduleInstaller().injectServerHolder(serverHolder);
moduleGroupDefine.moduleInstaller().preInstall();
......
cluster:
zookeeper:
hostPort: localhost:2181
sessionTimeout: 100000
#cluster:
# zookeeper:
# hostPort: localhost:2181
# sessionTimeout: 100000
agent_server:
jetty:
host: localhost
......@@ -15,6 +15,9 @@ agent_stream:
host: localhost
port: 12800
context_path: /
config:
buffer_offset_max_file_size: 10M
buffer_segment_max_file_size: 500M
ui:
jetty:
host: localhost
......@@ -24,16 +27,10 @@ collector_inside:
grpc:
host: localhost
port: 11800
storage:
elasticsearch:
cluster_name: CollectorDBCluster
cluster_transport_sniffer: true
cluster_nodes: localhost:9300
index_shards_number: 2
index_replicas_number: 0
# uncomment to enable h2 storage
#storage:
# h2:
# url: jdbc:h2:~/collector
# user_name: sa
# password: sa
\ No newline at end of file
# elasticsearch:
# cluster_name: CollectorDBCluster
# cluster_transport_sniffer: true
# cluster_nodes: localhost:9300
# index_shards_number: 2
# index_replicas_number: 0
\ No newline at end of file
......@@ -17,7 +17,7 @@
~ Project repository: https://github.com/OpenSkywalking/skywalking
-->
<Configuration status="error">
<Configuration status="debug">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout charset="UTF-8" pattern="%d - %c -%-4r [%t] %-5p %x - %m%n"/>
......@@ -27,7 +27,8 @@
<logger name="org.eclipse.jetty" level="INFO"/>
<logger name="org.apache.zookeeper" level="INFO"/>
<logger name="org.skywalking.apm.collector.agentstream.worker.storage.PersistenceTimer" level="INFO"/>
<Root level="info">
<logger name="io.grpc.netty.NettyServerHandler" level="INFO"/>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>apm-collector</artifactId>
<groupId>org.skywalking</groupId>
<version>3.2.3-2017</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apm-collector-cache</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-collector-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.skywalking</groupId>
<artifactId>apm-collector-storage</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -16,39 +16,62 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.cache;
package org.skywalking.apm.collector.cache;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.skywalking.apm.collector.cache.dao.IApplicationCacheDAO;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.ui.dao.IApplicationDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class ApplicationCache {
//TODO size configuration
private static Cache<Integer, String> CACHE = CacheBuilder.newBuilder().maximumSize(1000).build();
private static final Logger logger = LoggerFactory.getLogger(ApplicationCache.class);
public static String get(int applicationId) {
private static Cache<String, Integer> CODE_CACHE = CacheBuilder.newBuilder().initialCapacity(100).maximumSize(1000).build();
public static int get(String applicationCode) {
IApplicationCacheDAO dao = (IApplicationCacheDAO)DAOContainer.INSTANCE.get(IApplicationCacheDAO.class.getName());
int applicationId = 0;
try {
return CACHE.get(applicationId, () -> {
IApplicationDAO dao = (IApplicationDAO)DAOContainer.INSTANCE.get(IApplicationDAO.class.getName());
return dao.getApplicationCode(applicationId);
});
applicationId = CODE_CACHE.get(applicationCode, () -> dao.getApplicationId(applicationCode));
} catch (Throwable e) {
return Const.EXCEPTION;
logger.error(e.getMessage(), e);
}
if (applicationId == 0) {
applicationId = dao.getApplicationId(applicationCode);
if (applicationId != 0) {
CODE_CACHE.put(applicationCode, applicationId);
}
}
return applicationId;
}
public static String getForUI(int applicationId) {
String applicationCode = get(applicationId);
if (applicationCode.equals("Unknown")) {
IApplicationDAO dao = (IApplicationDAO)DAOContainer.INSTANCE.get(IApplicationDAO.class.getName());
private static Cache<Integer, String> ID_CACHE = CacheBuilder.newBuilder().maximumSize(1000).build();
public static String get(int applicationId) {
IApplicationCacheDAO dao = (IApplicationCacheDAO)DAOContainer.INSTANCE.get(IApplicationCacheDAO.class.getName());
String applicationCode = Const.EMPTY_STRING;
try {
applicationCode = ID_CACHE.get(applicationId, () -> dao.getApplicationCode(applicationId));
} catch (Throwable e) {
logger.error(e.getMessage(), e);
}
if (StringUtils.isEmpty(applicationCode)) {
applicationCode = dao.getApplicationCode(applicationId);
CACHE.put(applicationId, applicationCode);
if (StringUtils.isNotEmpty(applicationCode)) {
CODE_CACHE.put(applicationCode, applicationId);
}
}
return applicationCode;
}
......
......@@ -16,36 +16,38 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentregister.worker.cache;
package org.skywalking.apm.collector.cache;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.skywalking.apm.collector.agentregister.worker.application.dao.IApplicationDAO;
import org.skywalking.apm.collector.cache.dao.IInstanceCacheDAO;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class ApplicationCache {
public class InstanceCache {
private static Cache<String, Integer> CACHE = CacheBuilder.newBuilder().initialCapacity(100).maximumSize(1000).build();
private static final Logger logger = LoggerFactory.getLogger(InstanceCache.class);
private static Cache<Integer, Integer> INSTANCE_CACHE = CacheBuilder.newBuilder().initialCapacity(100).maximumSize(5000).build();
public static int get(int applicationInstanceId) {
IInstanceCacheDAO dao = (IInstanceCacheDAO)DAOContainer.INSTANCE.get(IInstanceCacheDAO.class.getName());
public static int get(String applicationCode) {
int applicationId = 0;
try {
applicationId = CACHE.get(applicationCode, () -> {
IApplicationDAO dao = (IApplicationDAO)DAOContainer.INSTANCE.get(IApplicationDAO.class.getName());
return dao.getApplicationId(applicationCode);
});
applicationId = INSTANCE_CACHE.get(applicationInstanceId, () -> dao.getApplicationId(applicationInstanceId));
} catch (Throwable e) {
return applicationId;
logger.error(e.getMessage(), e);
}
if (applicationId == 0) {
IApplicationDAO dao = (IApplicationDAO)DAOContainer.INSTANCE.get(IApplicationDAO.class.getName());
applicationId = dao.getApplicationId(applicationCode);
applicationId = dao.getApplicationId(applicationInstanceId);
if (applicationId != 0) {
CACHE.put(applicationCode, applicationId);
INSTANCE_CACHE.put(applicationInstanceId, applicationId);
}
}
return applicationId;
......
......@@ -16,39 +16,41 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.cache;
package org.skywalking.apm.collector.cache;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.skywalking.apm.collector.cache.dao.IServiceNameCacheDAO;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.ui.dao.IServiceNameDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class ServiceIdCache {
private static final Logger logger = LoggerFactory.getLogger(ServiceIdCache.class);
//TODO size configuration
private static Cache<String, Integer> CACHE = CacheBuilder.newBuilder().maximumSize(1000).build();
private static Cache<String, Integer> SERVICE_CACHE = CacheBuilder.newBuilder().maximumSize(1000).build();
public static int get(int applicationId, String serviceName) {
IServiceNameCacheDAO dao = (IServiceNameCacheDAO)DAOContainer.INSTANCE.get(IServiceNameCacheDAO.class.getName());
int serviceId = 0;
try {
return CACHE.get(applicationId + Const.ID_SPLIT + serviceName, () -> {
IServiceNameDAO dao = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
return dao.getServiceId(applicationId, serviceName);
});
serviceId = SERVICE_CACHE.get(applicationId + Const.ID_SPLIT + serviceName, () -> dao.getServiceId(applicationId, serviceName));
} catch (Throwable e) {
return 0;
logger.error(e.getMessage(), e);
}
}
public static int getForUI(int applicationId, String serviceName) {
int serviceId = get(applicationId, serviceName);
if (serviceId == 0) {
IServiceNameDAO dao = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
serviceId = dao.getServiceId(applicationId, serviceName);
CACHE.put(applicationId + Const.ID_SPLIT + serviceName, serviceId);
if (serviceId != 0) {
SERVICE_CACHE.put(applicationId + Const.ID_SPLIT + serviceName, serviceId);
}
}
return serviceId;
}
......
......@@ -16,40 +16,57 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.cache;
package org.skywalking.apm.collector.cache;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.skywalking.apm.collector.cache.dao.IServiceNameCacheDAO;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.core.util.StringUtils;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.skywalking.apm.collector.ui.dao.IServiceNameDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class ServiceNameCache {
private static final Logger logger = LoggerFactory.getLogger(ServiceNameCache.class);
//TODO size configuration
private static Cache<Integer, String> CACHE = CacheBuilder.newBuilder().maximumSize(10000).build();
public static String get(int serviceId) {
IServiceNameCacheDAO dao = (IServiceNameCacheDAO)DAOContainer.INSTANCE.get(IServiceNameCacheDAO.class.getName());
String serviceName = Const.EMPTY_STRING;
try {
return CACHE.get(serviceId, () -> {
IServiceNameDAO dao = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
return dao.getServiceName(serviceId);
});
serviceName = CACHE.get(serviceId, () -> dao.getServiceName(serviceId));
} catch (Throwable e) {
return Const.EXCEPTION;
logger.error(e.getMessage(), e);
}
}
public static String getForUI(int serviceId) {
String serviceName = get(serviceId);
if (serviceName.equals("Unknown")) {
IServiceNameDAO dao = (IServiceNameDAO)DAOContainer.INSTANCE.get(IServiceNameDAO.class.getName());
if (StringUtils.isEmpty(serviceName)) {
serviceName = dao.getServiceName(serviceId);
CACHE.put(serviceId, serviceName);
if (StringUtils.isNotEmpty(serviceName)) {
CACHE.put(serviceId, serviceName);
}
}
return serviceName;
}
public static String getSplitServiceName(String serviceName) {
if (StringUtils.isNotEmpty(serviceName)) {
String[] serviceNames = serviceName.split(Const.ID_SPLIT);
if (serviceNames.length == 2) {
return serviceNames[1];
} else {
return Const.EMPTY_STRING;
}
} else {
return Const.EMPTY_STRING;
}
}
}
......@@ -16,10 +16,15 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
package org.skywalking.apm.collector.cache.dao;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.define.register.ApplicationTable;
......@@ -30,9 +35,26 @@ import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class ApplicationEsDAO extends EsDAO implements IApplicationDAO {
public class ApplicationEsCacheDAO extends EsDAO implements IApplicationCacheDAO {
private final Logger logger = LoggerFactory.getLogger(ApplicationEsDAO.class);
private final Logger logger = LoggerFactory.getLogger(ApplicationEsCacheDAO.class);
@Override public int getApplicationId(String applicationCode) {
ElasticSearchClient client = getClient();
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ApplicationTable.TABLE);
searchRequestBuilder.setTypes("type");
searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);
searchRequestBuilder.setQuery(QueryBuilders.termQuery(ApplicationTable.COLUMN_APPLICATION_CODE, applicationCode));
searchRequestBuilder.setSize(1);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
if (searchResponse.getHits().totalHits > 0) {
SearchHit searchHit = searchResponse.getHits().iterator().next();
return (int)searchHit.getSource().get(ApplicationTable.COLUMN_APPLICATION_ID);
}
return 0;
}
@Override public String getApplicationCode(int applicationId) {
logger.debug("get application code, applicationId: {}", applicationId);
......@@ -43,6 +65,6 @@ public class ApplicationEsDAO extends EsDAO implements IApplicationDAO {
if (getResponse.isExists()) {
return (String)getResponse.getSource().get(ApplicationTable.COLUMN_APPLICATION_CODE);
}
return Const.UNKNOWN;
return Const.EMPTY_STRING;
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.cache.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.define.register.ApplicationTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5, clevertension
*/
public class ApplicationH2CacheDAO extends H2DAO implements IApplicationCacheDAO {
private final Logger logger = LoggerFactory.getLogger(ApplicationH2CacheDAO.class);
private static final String GET_APPLICATION_ID_OR_CODE_SQL = "select {0} from {1} where {2} = ?";
@Override
public int getApplicationId(String applicationCode) {
logger.info("get the application id with application code = {}", applicationCode);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_APPLICATION_ID_OR_CODE_SQL, ApplicationTable.COLUMN_APPLICATION_ID, ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_CODE);
Object[] params = new Object[] {applicationCode};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return 0;
}
@Override public String getApplicationCode(int applicationId) {
logger.debug("get application code, applicationId: {}", applicationId);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_APPLICATION_ID_OR_CODE_SQL, ApplicationTable.COLUMN_APPLICATION_CODE, ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
Object[] params = new Object[] {applicationId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getString(1);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return Const.EMPTY_STRING;
}
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.cache.dao;
/**
* @author pengys5
*/
public interface IApplicationCacheDAO {
int getApplicationId(String applicationCode);
String getApplicationCode(int applicationId);
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.cache.dao;
/**
* @author pengys5
*/
public interface IInstanceCacheDAO {
int getApplicationId(int applicationInstanceId);
}
......@@ -16,12 +16,12 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
package org.skywalking.apm.collector.cache.dao;
/**
* @author pengys5
*/
public interface IServiceNameDAO {
public interface IServiceNameCacheDAO {
String getServiceName(int serviceId);
int getServiceId(int applicationId, String serviceName);
......
......@@ -16,27 +16,26 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.cache;
package org.skywalking.apm.collector.cache.dao;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.skywalking.apm.collector.agentregister.worker.instance.dao.IInstanceDAO;
import org.skywalking.apm.collector.storage.dao.DAOContainer;
import org.elasticsearch.action.get.GetResponse;
import org.skywalking.apm.collector.storage.define.register.InstanceTable;
import org.skywalking.apm.collector.storage.elasticsearch.dao.EsDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public class InstanceCache {
public class InstanceEsCacheDAO extends EsDAO implements IInstanceCacheDAO {
private static Cache<Integer, Integer> CACHE = CacheBuilder.newBuilder().initialCapacity(100).maximumSize(5000).build();
private final Logger logger = LoggerFactory.getLogger(InstanceEsCacheDAO.class);
public static int get(int applicationInstanceId) {
try {
return CACHE.get(applicationInstanceId, () -> {
IInstanceDAO dao = (IInstanceDAO)DAOContainer.INSTANCE.get(IInstanceDAO.class.getName());
return dao.getApplicationId(applicationInstanceId);
});
} catch (Throwable e) {
@Override public int getApplicationId(int applicationInstanceId) {
GetResponse response = getClient().prepareGet(InstanceTable.TABLE, String.valueOf(applicationInstanceId)).get();
if (response.isExists()) {
return (int)response.getSource().get(InstanceTable.COLUMN_APPLICATION_ID);
} else {
return 0;
}
}
......
......@@ -16,39 +16,39 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
package org.skywalking.apm.collector.cache.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
import org.skywalking.apm.collector.storage.define.register.ApplicationTable;
import org.skywalking.apm.collector.storage.define.register.InstanceTable;
import org.skywalking.apm.collector.storage.h2.SqlBuilder;
import org.skywalking.apm.collector.storage.h2.dao.H2DAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author pengys5, clevertension
* @author pengys5
*/
public class ApplicationH2DAO extends H2DAO implements IApplicationDAO {
public class InstanceH2CacheDAO extends H2DAO implements IInstanceCacheDAO {
private final Logger logger = LoggerFactory.getLogger(InstanceH2CacheDAO.class);
private static final String GET_APPLICATION_ID_SQL = "select {0} from {1} where {2} = ?";
private final Logger logger = LoggerFactory.getLogger(ApplicationH2DAO.class);
private static final String GET_APPLICATION_CODE_SQL = "select {0} from {1} where {2} = ?";
@Override public String getApplicationCode(int applicationId) {
logger.debug("get application code, applicationId: {}", applicationId);
@Override public int getApplicationId(int applicationInstanceId) {
logger.info("get the application id with application id = {}", applicationInstanceId);
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_APPLICATION_CODE_SQL, ApplicationTable.COLUMN_APPLICATION_CODE, ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
Object[] params = new Object[]{applicationId};
String sql = SqlBuilder.buildSql(GET_APPLICATION_ID_SQL, InstanceTable.COLUMN_APPLICATION_ID, InstanceTable.TABLE, InstanceTable.COLUMN_APPLICATION_ID);
Object[] params = new Object[] {applicationInstanceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getString(1);
return rs.getInt(InstanceTable.COLUMN_APPLICATION_ID);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return Const.UNKNOWN;
return 0;
}
}
......@@ -16,7 +16,7 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
package org.skywalking.apm.collector.cache.dao;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
......@@ -33,7 +33,7 @@ import org.skywalking.apm.collector.storage.elasticsearch.dao.EsDAO;
/**
* @author pengys5
*/
public class ServiceNameEsDAO extends EsDAO implements IServiceNameDAO {
public class ServiceNameEsCacheDAO extends EsDAO implements IServiceNameCacheDAO {
@Override public String getServiceName(int serviceId) {
GetRequestBuilder getRequestBuilder = getClient().prepareGet(ServiceNameTable.TABLE, String.valueOf(serviceId));
......@@ -44,7 +44,7 @@ public class ServiceNameEsDAO extends EsDAO implements IServiceNameDAO {
int applicationId = ((Number)getResponse.getSource().get(ServiceNameTable.COLUMN_APPLICATION_ID)).intValue();
return applicationId + Const.ID_SPLIT + serviceName;
}
return Const.UNKNOWN;
return Const.EMPTY_STRING;
}
@Override public int getServiceId(int applicationId, String serviceName) {
......@@ -61,8 +61,7 @@ public class ServiceNameEsDAO extends EsDAO implements IServiceNameDAO {
SearchResponse searchResponse = searchRequestBuilder.get();
if (searchResponse.getHits().totalHits > 0) {
SearchHit searchHit = searchResponse.getHits().iterator().next();
int serviceId = (int)searchHit.getSource().get(ServiceNameTable.COLUMN_SERVICE_ID);
return serviceId;
return (int)searchHit.getSource().get(ServiceNameTable.COLUMN_SERVICE_ID);
}
return 0;
}
......
......@@ -16,11 +16,10 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
package org.skywalking.apm.collector.cache.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
import org.skywalking.apm.collector.core.util.Const;
......@@ -33,34 +32,38 @@ import org.slf4j.LoggerFactory;
/**
* @author pengys5, clevertension
*/
public class ServiceNameH2DAO extends H2DAO implements IServiceNameDAO {
private final Logger logger = LoggerFactory.getLogger(ServiceNameH2DAO.class);
private static final String GET_SERVICE_NAME_SQL = "select {0} from {1} where {2} = ?";
public class ServiceNameH2CacheDAO extends H2DAO implements IServiceNameCacheDAO {
private final Logger logger = LoggerFactory.getLogger(ServiceNameH2CacheDAO.class);
private static final String GET_SERVICE_NAME_SQL = "select {0},{1} from {2} where {3} = ?";
private static final String GET_SERVICE_ID_SQL = "select {0} from {1} where {2} = ? and {3} = ? limit 1";
@Override public String getServiceName(int serviceId) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SERVICE_NAME_SQL, ServiceNameTable.COLUMN_SERVICE_NAME,
ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
Object[] params = new Object[]{serviceId};
String sql = SqlBuilder.buildSql(GET_SERVICE_NAME_SQL, ServiceNameTable.COLUMN_APPLICATION_ID, ServiceNameTable.COLUMN_SERVICE_NAME,
ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_ID);
Object[] params = new Object[] {serviceId};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
return rs.getString(ServiceNameTable.COLUMN_SERVICE_NAME);
String serviceName = rs.getString(ServiceNameTable.COLUMN_SERVICE_NAME);
int applicationId = rs.getInt(ServiceNameTable.COLUMN_APPLICATION_ID);
return applicationId + Const.ID_SPLIT + serviceName;
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return Const.UNKNOWN;
return Const.EMPTY_STRING;
}
@Override public int getServiceId(int applicationId, String serviceName) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_SERVICE_ID_SQL, ServiceNameTable.COLUMN_SERVICE_ID,
ServiceNameTable.TABLE, ServiceNameTable.COLUMN_APPLICATION_ID, ServiceNameTable.COLUMN_SERVICE_NAME);
Object[] params = new Object[]{applicationId, serviceName};
ServiceNameTable.TABLE, ServiceNameTable.COLUMN_APPLICATION_ID, ServiceNameTable.COLUMN_SERVICE_NAME);
Object[] params = new Object[] {applicationId, serviceName};
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
int serviceId = rs.getInt(ServiceNameTable.COLUMN_SERVICE_ID);
return serviceId;
return rs.getInt(ServiceNameTable.COLUMN_SERVICE_ID);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
......
org.skywalking.apm.collector.cache.dao.ApplicationEsDAO
org.skywalking.apm.collector.cache.dao.InstanceEsDAO
org.skywalking.apm.collector.cache.dao.ServiceNameEsDAO
\ No newline at end of file
org.skywalking.apm.collector.cache.dao.ApplicationH2DAO
org.skywalking.apm.collector.cache.dao.InstanceH2DAO
org.skywalking.apm.collector.cache.dao.ServiceNameH2DAO
\ No newline at end of file
......@@ -18,9 +18,12 @@
package org.skywalking.apm.collector.client.h2;
import java.sql.*;
import org.h2.jdbcx.JdbcConnectionPool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.util.IOUtils;
import org.skywalking.apm.collector.core.client.Client;
import org.slf4j.Logger;
......@@ -33,7 +36,6 @@ public class H2Client implements Client {
private final Logger logger = LoggerFactory.getLogger(H2Client.class);
private JdbcConnectionPool cp;
private Connection conn;
private String url;
private String userName;
......@@ -53,18 +55,15 @@ public class H2Client implements Client {
@Override public void initialize() throws H2ClientException {
try {
cp = JdbcConnectionPool.
create(this.url, this.userName, this.password);
conn = cp.getConnection();
Class.forName("org.h2.Driver");
conn = DriverManager.
getConnection(this.url, this.userName, this.password);
} catch (Exception e) {
throw new H2ClientException(e.getMessage(), e);
}
}
@Override public void shutdown() {
if (cp != null) {
cp.dispose();
}
IOUtils.closeSilently(conn);
}
......
......@@ -19,6 +19,7 @@
package org.skywalking.apm.collector.cluster;
import org.skywalking.apm.collector.core.cluster.ClusterModuleContext;
import org.skywalking.apm.collector.core.config.GroupConfigParser;
import org.skywalking.apm.collector.core.framework.Context;
import org.skywalking.apm.collector.core.module.ModuleGroupDefine;
import org.skywalking.apm.collector.core.module.ModuleInstaller;
......@@ -46,4 +47,8 @@ public class ClusterModuleGroupDefine implements ModuleGroupDefine {
@Override public ModuleInstaller moduleInstaller() {
return installer;
}
@Override public GroupConfigParser groupConfigParser() {
return null;
}
}
......@@ -16,17 +16,15 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.agentstream.worker.util;
package org.skywalking.apm.collector.core.config;
import java.util.Map;
/**
* @author pengys5
*/
public enum ExchangeMarkUtils {
INSTANCE;
private static final String MARK_TAG = "M";
public interface GroupConfigParser {
String NODE_NAME = "config";
public String buildMarkedID(int id) {
return MARK_TAG + id;
}
void parse(Map<String, Map> config);
}
......@@ -16,11 +16,11 @@
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.ui.dao;
package org.skywalking.apm.collector.core.config;
/**
* @author pengys5
*/
public interface IApplicationDAO {
String getApplicationCode(int applicationId);
public class SystemConfig {
public static String DATA_PATH = "../data";
}
/*
* Copyright 2017, OpenSkywalking Organization All rights reserved.
*
* Licensed 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.
*
* Project repository: https://github.com/OpenSkywalking/skywalking
*/
package org.skywalking.apm.collector.core.config;
import org.skywalking.apm.collector.core.util.StringUtils;
/**
* @author pengys5
*/
public enum SystemConfigParser {
INSTANCE;
private static final String DATA_PATH = "data.path";
public void parse() {
if (!StringUtils.isEmpty(System.getProperty(DATA_PATH))) {
SystemConfig.DATA_PATH = System.getProperty(DATA_PATH);
}
}
}
......@@ -36,11 +36,11 @@ public abstract class CommonModuleInstaller implements ModuleInstaller {
this.moduleDefineMap = moduleDefineMap;
}
protected final Map<String, Map> getModuleConfig() {
final Map<String, Map> getModuleConfig() {
return moduleConfig;
}
protected final Map<String, ModuleDefine> getModuleDefineMap() {
final Map<String, ModuleDefine> getModuleDefineMap() {
return moduleDefineMap;
}
......
......@@ -18,6 +18,7 @@
package org.skywalking.apm.collector.core.module;
import org.skywalking.apm.collector.core.config.GroupConfigParser;
import org.skywalking.apm.collector.core.framework.Context;
/**
......@@ -29,4 +30,6 @@ public interface ModuleGroupDefine {
Context groupContext();
ModuleInstaller moduleInstaller();
GroupConfigParser groupConfigParser();
}
......@@ -37,11 +37,11 @@ import org.slf4j.LoggerFactory;
/**
* @author pengys5
*/
public abstract class MultipleCommonModuleInstaller extends CommonModuleInstaller {
public abstract class MultipleModuleInstaller extends CommonModuleInstaller {
private final Logger logger = LoggerFactory.getLogger(MultipleCommonModuleInstaller.class);
private final Logger logger = LoggerFactory.getLogger(MultipleModuleInstaller.class);
public MultipleCommonModuleInstaller() {
public MultipleModuleInstaller() {
moduleDefines = new LinkedList<>();
}
......
......@@ -76,7 +76,9 @@ public abstract class SingleModuleInstaller extends CommonModuleInstaller {
throw new ClusterModuleException("single module, but configure multiple default module");
}
this.moduleDefine = moduleDefineEntry.getValue();
this.moduleDefine.configParser().parse(null);
if (this.moduleDefine.configParser() != null) {
this.moduleDefine.configParser().parse(null);
}
hasDefaultModule = true;
}
}
......
......@@ -31,4 +31,5 @@ public class Const {
public static final String UNKNOWN = "Unknown";
public static final String EXCEPTION = "Exception";
public static final String EMPTY_STRING = "";
public static final String FILE_SUFFIX = "sw";
}
......@@ -18,6 +18,7 @@
package org.skywalking.apm.collector.queue;
import org.skywalking.apm.collector.core.config.GroupConfigParser;
import org.skywalking.apm.collector.core.framework.Context;
import org.skywalking.apm.collector.core.module.ModuleGroupDefine;
import org.skywalking.apm.collector.core.module.ModuleInstaller;
......@@ -45,4 +46,8 @@ public class QueueModuleGroupDefine implements ModuleGroupDefine {
@Override public ModuleInstaller moduleInstaller() {
return installer;
}
@Override public GroupConfigParser groupConfigParser() {
return null;
}
}
......@@ -68,10 +68,6 @@ public abstract class StorageModuleDefine extends ModuleDefine implements Cluste
return null;
}
@Override public final boolean defaultModule() {
return true;
}
public abstract StorageInstaller storageInstaller();
public abstract void injectClientIntoDAO(Client client) throws DefineException;
......
......@@ -18,6 +18,7 @@
package org.skywalking.apm.collector.storage;
import org.skywalking.apm.collector.core.config.GroupConfigParser;
import org.skywalking.apm.collector.core.framework.Context;
import org.skywalking.apm.collector.core.module.ModuleGroupDefine;
import org.skywalking.apm.collector.core.module.ModuleInstaller;
......@@ -45,4 +46,8 @@ public class StorageModuleGroupDefine implements ModuleGroupDefine {
@Override public ModuleInstaller moduleInstaller() {
return installer;
}
@Override public GroupConfigParser groupConfigParser() {
return null;
}
}
......@@ -38,9 +38,9 @@ public class ServiceEntryDataDefine extends DataDefine {
@Override protected void attributeDefine() {
addAttribute(0, new Attribute(ServiceEntryTable.COLUMN_ID, AttributeType.STRING, new NonOperation()));
addAttribute(1, new Attribute(ServiceEntryTable.COLUMN_APPLICATION_ID, AttributeType.INTEGER, new NonOperation()));
addAttribute(1, new Attribute(ServiceEntryTable.COLUMN_APPLICATION_ID, AttributeType.INTEGER, new CoverOperation()));
addAttribute(2, new Attribute(ServiceEntryTable.COLUMN_ENTRY_SERVICE_ID, AttributeType.INTEGER, new CoverOperation()));
addAttribute(3, new Attribute(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME, AttributeType.STRING, new NonOperation()));
addAttribute(3, new Attribute(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME, AttributeType.STRING, new CoverOperation()));
addAttribute(4, new Attribute(ServiceEntryTable.COLUMN_REGISTER_TIME, AttributeType.LONG, new NonOperation()));
addAttribute(5, new Attribute(ServiceEntryTable.COLUMN_NEWEST_TIME, AttributeType.LONG, new CoverOperation()));
}
......
......@@ -46,6 +46,10 @@ public class StorageElasticSearchModuleDefine extends StorageModuleDefine {
return MODULE_NAME;
}
@Override public final boolean defaultModule() {
return false;
}
@Override protected ModuleConfigParser configParser() {
return new StorageElasticSearchConfigParser();
}
......
......@@ -20,6 +20,7 @@ package org.skywalking.apm.collector.storage.h2;
import java.util.Map;
import org.skywalking.apm.collector.core.config.ConfigParseException;
import org.skywalking.apm.collector.core.config.SystemConfig;
import org.skywalking.apm.collector.core.module.ModuleConfigParser;
import org.skywalking.apm.collector.core.util.ObjectUtils;
import org.skywalking.apm.collector.core.util.StringUtils;
......@@ -31,15 +32,20 @@ public class StorageH2ConfigParser implements ModuleConfigParser {
private static final String URL = "url";
public static final String USER_NAME = "user_name";
public static final String PASSWORD = "password";
@Override public void parse(Map config) throws ConfigParseException {
if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(URL))) {
StorageH2Config.URL = (String)config.get(URL);
} else {
StorageH2Config.URL = "jdbc:h2:" + SystemConfig.DATA_PATH + "/h2";
}
if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(USER_NAME))) {
StorageH2Config.USER_NAME = (String)config.get(USER_NAME);
} else {
StorageH2Config.USER_NAME = "sa";
}
if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(PASSWORD))) {
StorageH2Config.PASSWORD = (String)config.get(PASSWORD);
}
}
}
}
\ No newline at end of file
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册