Skip to content

Commit

Permalink
[#9573] Refactor TagParser to TagUtils, Apply regex precompile
Browse files Browse the repository at this point in the history
  • Loading branch information
donghun-cho committed Jan 6, 2023
1 parent 80954a1 commit 708a117
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.navercorp.pinpoint.metric.web.service.SystemMetricHostInfoService;
import com.navercorp.pinpoint.metric.web.service.YMLSystemMetricBasicGroupManager;
import com.navercorp.pinpoint.metric.web.util.Range;
import com.navercorp.pinpoint.metric.web.util.TagParser;
import com.navercorp.pinpoint.metric.web.util.TagUtils;
import com.navercorp.pinpoint.metric.web.util.TimeWindow;
import com.navercorp.pinpoint.metric.web.util.TimeWindowSampler;
import com.navercorp.pinpoint.metric.web.util.TimeWindowSlotCentricSampler;
Expand All @@ -50,9 +50,7 @@ public class SystemMetricController {
private final TenantProvider tenantProvider;

private final TimeWindowSampler DEFAULT_TIME_WINDOW_SAMPLER = new TimeWindowSlotCentricSampler(10000L, 200);

private final TagParser tagParser = new TagParser();


public SystemMetricController(SystemMetricDataService systemMetricDataService,
SystemMetricHostInfoService systemMetricHostInfoService,
YMLSystemMetricBasicGroupManager systemMetricBasicGroupManager,
Expand Down Expand Up @@ -99,7 +97,7 @@ public SystemMetricView getCollectedMetricData(@RequestParam("hostGroupName") St
String tenantId = tenantProvider.getTenantId();
TimeWindow timeWindow = new TimeWindow(Range.newRange(from, to), DEFAULT_TIME_WINDOW_SAMPLER);
MetricDataSearchKey metricDataSearchKey = new MetricDataSearchKey(tenantId, hostGroupName, hostName, systemMetricBasicGroupManager.findMetricName(metricDefinitionId), metricDefinitionId, timeWindow);
List<Tag> tagList = tagParser.parseTags(tags);
List<Tag> tagList = TagUtils.parseTags(tags);

SystemMetricData<? extends Number> systemMetricData = systemMetricDataService.getCollectedMetricData(metricDataSearchKey, timeWindow, tagList);
return new SystemMetricView(systemMetricData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.navercorp.pinpoint.metric.web.dao.model.HostInfoSearchKey;
import com.navercorp.pinpoint.metric.web.dao.model.MetricInfoSearchKey;
import com.navercorp.pinpoint.metric.web.dao.model.MetricTagsSearchKey;
import com.navercorp.pinpoint.metric.web.util.TagParser;
import com.navercorp.pinpoint.metric.web.util.TagUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -65,7 +65,7 @@ public List<String> selectCollectedMetricTags(String tenantId, String hostGroupN
List<String> jsonStrings = sqlPinotSessionTemplate.selectList(NAMESPACE + "selectCollectedMetricTags", new MetricTagsSearchKey(tenantId, hostGroupName, hostName, metricName));

return jsonStrings.stream()
.map(TagParser::toTagStrings)
.map(TagUtils::toTagString)
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.navercorp.pinpoint.metric.web.mybatis.typehandler;

import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.web.util.TagParser;
import com.navercorp.pinpoint.metric.web.util.TagUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

Expand All @@ -12,24 +12,24 @@
import java.util.List;

public class TagListTypeHandler implements TypeHandler<List<Tag>> {
private final TagParser parser = new TagParser();

@Override
public void setParameter(PreparedStatement ps, int i, List<Tag> parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, parameter.toString());
}

@Override
public List<Tag> getResult(ResultSet rs, String columnName) throws SQLException {
return parser.parseTags(rs.getString(columnName));
return TagUtils.parseTags(rs.getString(columnName));
}

@Override
public List<Tag> getResult(ResultSet rs, int columnIndex) throws SQLException {
return parser.parseTags(rs.getString(columnIndex));
return TagUtils.parseTags(rs.getString(columnIndex));
}

@Override
public List<Tag> getResult(CallableStatement cs, int columnIndex) throws SQLException {
return parser.parseTags(cs.getString(columnIndex));
return TagUtils.parseTags(cs.getString(columnIndex));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.navercorp.pinpoint.metric.web.mybatis.typehandler;

import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.web.util.TagParser;
import com.navercorp.pinpoint.metric.web.util.TagUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

Expand All @@ -11,7 +11,6 @@
import java.sql.SQLException;

public class TagTypeHandler implements TypeHandler<Tag> {
private final TagParser parser = new TagParser();

@Override
public void setParameter(PreparedStatement ps, int i, Tag parameter, JdbcType jdbcType) throws SQLException {
Expand All @@ -20,16 +19,16 @@ public void setParameter(PreparedStatement ps, int i, Tag parameter, JdbcType jd

@Override
public Tag getResult(ResultSet rs, String columnName) throws SQLException {
return parser.parseTag(rs.getString(columnName));
return TagUtils.parseTag(rs.getString(columnName));
}

@Override
public Tag getResult(ResultSet rs, int columnIndex) throws SQLException {
return parser.parseTag(rs.getString(columnIndex));
return TagUtils.parseTag(rs.getString(columnIndex));
}

@Override
public Tag getResult(CallableStatement cs, int columnIndex) throws SQLException {
return parser.parseTag(cs.getString(columnIndex));
return TagUtils.parseTag(cs.getString(columnIndex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.navercorp.pinpoint.metric.web.model.SystemMetricData;
import com.navercorp.pinpoint.metric.web.model.basic.metric.group.GroupingRule;
import com.navercorp.pinpoint.metric.web.model.chart.SystemMetricPoint;
import com.navercorp.pinpoint.metric.web.util.TagUtils;
import com.navercorp.pinpoint.metric.web.util.TimeWindow;
import com.navercorp.pinpoint.metric.web.util.metric.DoubleUncollectedDataCreator;
import com.navercorp.pinpoint.metric.web.util.metric.LongUncollectedDataCreator;
Expand Down Expand Up @@ -267,9 +268,7 @@ public String getGroupName() {
return "groupWithNoTag";
}

return tagList.stream()
.map(Tag::toString)
.collect(Collectors.joining(","));
return TagUtils.toTagString(tagList);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,21 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* @author Hyunjoon Cho
*/
public class TagParser {
public TagParser() {
public class TagUtils {

private static final Pattern MULTI_VALUE_FIELD_PATTERN = Pattern.compile("[\\[\\]\"]");
private static final Pattern JSON_TAG_STRING_PATTERN = Pattern.compile("[{}\"]");

private TagUtils() {
}

public List<Tag> parseTags(List<String> tags) {
public static List<Tag> parseTags(List<String> tags) {
if (tags == null || tags.size() == 0) {
return new ArrayList<>();
}
Expand All @@ -43,7 +49,7 @@ public List<Tag> parseTags(List<String> tags) {
return tagList;
}

public List<Tag> parseTags(String tagStrings) {
public static List<Tag> parseTags(String tagStrings) {
if (tagStrings == null || tagStrings.contains("null")) {
return new ArrayList<>();
}
Expand All @@ -59,18 +65,27 @@ public List<Tag> parseTags(String tagStrings) {
return tagList;
}

public Tag parseTag(String tagString) {
public static Tag parseTag(String tagString) {
Objects.requireNonNull(tagString, "tagString");

String[] tag = tagString.split(":");
return new Tag(tag[0], tag[1]);
}

private static String[] parseMultiValueFieldList(String string) {
return string.replaceAll("[\\[\\]\"]", "").split(",");
return MULTI_VALUE_FIELD_PATTERN.matcher(string).replaceAll("").split(",");
}

public static String toTagString(String jsonTagString) {
if (jsonTagString.equals("{}")) {
return "";
}
return JSON_TAG_STRING_PATTERN.matcher(jsonTagString).replaceAll("");
}

public static String toTagStrings(String jsonTagStrings) {
return jsonTagStrings.replaceAll("[{}\"]", "");
public static String toTagString(List<Tag> tagList) {
return tagList.stream()
.map(Tag::toString)
.collect(Collectors.joining(","));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.navercorp.pinpoint.metric.web.utill.metric;

import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.web.util.TagUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class TagUtilsTest {

@Test
public void parseTagTest() {
List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("A", "1"));
tagList.add(new Tag("B", "2"));
tagList.add(new Tag("C", "3"));

List<Tag> result = new ArrayList<>();
result.add(TagUtils.parseTag("A:1"));
result.add(TagUtils.parseTag("B:2"));
result.add(TagUtils.parseTag("C:3"));

Assertions.assertEquals(tagList, result);
}

@Test
public void parseTagsListTest() {
List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("A", "1"));
tagList.add(new Tag("B", "2"));
tagList.add(new Tag("C", "3"));

List<String> tags = new ArrayList<>();
tags.add("A:1");
tags.add("B:2");
tags.add("C:3");

List<Tag> result = TagUtils.parseTags(tags);

Assertions.assertEquals(tagList, result);
}


@Test
public void parseTagsStringParameterTest() {
List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("device", "1"));
tagList.add(new Tag("fstype", "2"));
tagList.add(new Tag("mode", "3"));
tagList.add(new Tag("path", "4"));

String parameterTagsString = "device:1,fstype:2,mode:3,path:4";
List<Tag> result = TagUtils.parseTags(parameterTagsString);

Assertions.assertEquals(tagList, result);
}

@Test
public void parseTagsStringNullParameterTest() {
String nullString = null;
List<Tag> result = TagUtils.parseTags(nullString);

Assertions.assertEquals(0, result.size());
}

@Test
public void parseTagsStringListTypeTest() {
List<Tag> tagList = new ArrayList<>();
tagList.add(new Tag("device", "1"));
tagList.add(new Tag("fstype", "2"));
tagList.add(new Tag("mode", "3"));
tagList.add(new Tag("path", "4"));

String multiValueFieldTagString = "\"device:1\",\"fstype:2\",\"mode:3\",\"path:4\"";
List<Tag> result = TagUtils.parseTags(multiValueFieldTagString);

Assertions.assertEquals(tagList, result);
}

@Test
public void parseTagsStringListTypeNullTest() {
String multiValueFieldTagString = "\"null\"";
List<Tag> result = TagUtils.parseTags(multiValueFieldTagString);

Assertions.assertEquals(0, result.size());
}


@Test
public void toTagsStringTest() {
String jsonTagString = "{\"device\":\"1\",\"fstype\":\"2\",\"mode\":\"3\",\"path\":\"4\"}";
String result = TagUtils.toTagString(jsonTagString);

Assertions.assertEquals("device:1,fstype:2,mode:3,path:4", result);
}

@Test
public void toTagsStringEmptyTest() {
String jsonTagString = "{}";
String result = TagUtils.toTagString(jsonTagString);

Assertions.assertEquals("", result);
}
}

0 comments on commit 708a117

Please sign in to comment.