Skip to content

Commit

Permalink
[INLONG-11574][Agent] Add COS source unit test (#11575)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwwhuang authored Dec 6, 2024
1 parent dae7074 commit 4d78384
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
package org.apache.inlong.agent.core;

import org.apache.inlong.agent.conf.AgentConfiguration;
import org.apache.inlong.agent.conf.TaskProfile;
import org.apache.inlong.agent.constant.AgentConstants;
import org.apache.inlong.agent.constant.FetcherConstants;
import org.apache.inlong.agent.pojo.FileTask.FileTaskConfig;
import org.apache.inlong.common.enums.TaskStateEnum;
import org.apache.inlong.common.pojo.agent.DataConfig;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -74,33 +70,4 @@ public void teardownAgentHome() {
}
}
}

public TaskProfile getTaskProfile(int taskId, String pattern, boolean retry, String startTime, String endTime,
TaskStateEnum state, String timeZone) {
DataConfig dataConfig = getDataConfig(taskId, pattern, retry, startTime, endTime, state, timeZone);
TaskProfile profile = TaskProfile.convertToTaskProfile(dataConfig);
return profile;
}

private DataConfig getDataConfig(int taskId, String pattern, boolean retry, String startTime, String endTime,
TaskStateEnum state, String timeZone) {
DataConfig dataConfig = new DataConfig();
dataConfig.setInlongGroupId("testGroupId");
dataConfig.setInlongStreamId("testStreamId");
dataConfig.setDataReportType(1);
dataConfig.setTaskType(3);
dataConfig.setTaskId(taskId);
dataConfig.setTimeZone(timeZone);
dataConfig.setState(state.ordinal());
FileTaskConfig fileTaskConfig = new FileTaskConfig();
fileTaskConfig.setPattern(pattern);
fileTaskConfig.setTimeOffset("0h");
fileTaskConfig.setMaxFileCount(100);
fileTaskConfig.setCycleUnit("h");
fileTaskConfig.setRetry(retry);
fileTaskConfig.setDataTimeFrom(startTime);
fileTaskConfig.setDataTimeTo(endTime);
dataConfig.setExtParams(GSON.toJson(fileTaskConfig));
return dataConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ private void runForNormal() {
}

private void scanExistingFile() {
LOGGER.info("test123 qqqq");
List<BasicFileInfo> fileInfos = FileScanner.scanTaskBetweenTimes(cosClient, bucketName, originPattern,
taskProfile.getCycleUnit(), timeOffset, startTime, endTime, retry);
LOGGER.info("taskId {} scan {} get file count {}", getTaskId(), originPattern, fileInfos.size());
Expand Down Expand Up @@ -314,7 +313,6 @@ private void addToEvenMap(String fileName, String dataTime) {
taskProfile.getTaskId(), dataTime, fileName);
return;
}
LOGGER.info("test123 {}", cosClient);
ObjectMetadata meta = cosClient.getObjectMetadata(bucketName, fileName);
Long fileUpdateTime = meta.getLastModified().getTime();
if (!shouldAddAgain(fileName, fileUpdateTime)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,20 @@ public static List<BasicFileInfo> scanTaskInOneCycle(COSClient cosClient, String
}
List<String> commonPrefixes = objectListing.getCommonPrefixes();
int depth;
Pattern patternByDepth = null;
Pattern patternByDepth;
if (!commonPrefixes.isEmpty()) {
depth = countCharacterOccurrences(commonPrefixes.get(0), PATH_SEP);
String temp = findNthOccurrenceSubstring(pattern.pattern(), PATH_SEP, depth);
patternByDepth = Pattern.compile(temp, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
}
for (String commonPrefix : commonPrefixes) {
Matcher matcher = patternByDepth.matcher(commonPrefix);
if (matcher.matches()) {
infos.addAll(scanTaskInOneCycle(cosClient, bucketName, pattern, commonPrefix, dataTime, cycleUnit));
String nthOccurrenceSubstring = findNthOccurrenceSubstring(pattern.pattern(), PATH_SEP, depth);
if (nthOccurrenceSubstring != null) {
patternByDepth = Pattern.compile(nthOccurrenceSubstring,
Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
for (String commonPrefix : commonPrefixes) {
Matcher matcher = patternByDepth.matcher(commonPrefix);
if (matcher.matches()) {
infos.addAll(scanTaskInOneCycle(cosClient, bucketName, pattern, commonPrefix, dataTime,
cycleUnit));
}
}
}
}
List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import org.apache.inlong.agent.conf.TaskProfile;
import org.apache.inlong.agent.constant.AgentConstants;
import org.apache.inlong.agent.constant.FetcherConstants;
import org.apache.inlong.agent.pojo.COSTask.COSTaskConfig;
import org.apache.inlong.agent.pojo.FileTask.FileTaskConfig;
import org.apache.inlong.common.enums.TaskStateEnum;
import org.apache.inlong.common.enums.TaskTypeEnum;
import org.apache.inlong.common.pojo.agent.DataConfig;

import com.google.gson.Gson;
Expand Down Expand Up @@ -82,24 +84,24 @@ public void teardownAgentHome() {
}
}

public TaskProfile getTaskProfile(int taskId, String pattern, String dataContentStyle, boolean retry,
public TaskProfile getFileTaskProfile(int taskId, String pattern, String dataContentStyle, boolean retry,
String startTime, String endTime,
TaskStateEnum state, String cycleUnit, String timeZone, List<String> filterStreams) {
DataConfig dataConfig = getDataConfig(taskId, pattern, dataContentStyle, retry, startTime, endTime,
DataConfig dataConfig = getFileDataConfig(taskId, pattern, dataContentStyle, retry, startTime, endTime,
state, cycleUnit, timeZone,
filterStreams);
TaskProfile profile = TaskProfile.convertToTaskProfile(dataConfig);
return profile;
}

private DataConfig getDataConfig(int taskId, String pattern, String dataContentStyle, boolean retry,
private DataConfig getFileDataConfig(int taskId, String pattern, String dataContentStyle, boolean retry,
String startTime, String endTime, TaskStateEnum state, String cycleUnit, String timeZone,
List<String> filterStreams) {
DataConfig dataConfig = new DataConfig();
dataConfig.setInlongGroupId("testGroupId");
dataConfig.setInlongStreamId("testStreamId");
dataConfig.setDataReportType(1);
dataConfig.setTaskType(3);
dataConfig.setTaskType(TaskTypeEnum.FILE.getType());
dataConfig.setTaskId(taskId);
dataConfig.setTimeZone(timeZone);
dataConfig.setState(state.ordinal());
Expand All @@ -119,4 +121,47 @@ private DataConfig getDataConfig(int taskId, String pattern, String dataContentS
dataConfig.setExtParams(GSON.toJson(fileTaskConfig));
return dataConfig;
}

public TaskProfile getCOSTaskProfile(int taskId, String pattern, String contentStyle, boolean retry,
String startTime, String endTime,
TaskStateEnum state, String cycleUnit, String timeZone, List<String> filterStreams) {
DataConfig dataConfig = getCOSDataConfig(taskId, pattern, contentStyle, retry, startTime, endTime,
state, cycleUnit, timeZone,
filterStreams);
TaskProfile profile = TaskProfile.convertToTaskProfile(dataConfig);
return profile;
}

private DataConfig getCOSDataConfig(int taskId, String pattern, String contentStyle, boolean retry,
String startTime, String endTime, TaskStateEnum state, String cycleUnit, String timeZone,
List<String> filterStreams) {
DataConfig dataConfig = new DataConfig();
dataConfig.setInlongGroupId("testGroupId");
dataConfig.setInlongStreamId("testStreamId");
dataConfig.setDataReportType(1);
dataConfig.setTaskType(TaskTypeEnum.COS.getType());
dataConfig.setTaskId(taskId);
dataConfig.setTimeZone(timeZone);
dataConfig.setState(state.ordinal());
COSTaskConfig cosTaskConfig = new COSTaskConfig();
cosTaskConfig.setBucketName("testBucket");
cosTaskConfig.setCredentialsId("testSecretId");
cosTaskConfig.setCredentialsKey("testSecretKey");
cosTaskConfig.setRegion("testRegion");
cosTaskConfig.setPattern(pattern);
cosTaskConfig.setTimeOffset("0d");
// GMT-8:00 same with Asia/Shanghai
cosTaskConfig.setMaxFileCount(100);
cosTaskConfig.setCycleUnit(cycleUnit);
cosTaskConfig.setRetry(retry);
cosTaskConfig.setDataTimeFrom(startTime);
cosTaskConfig.setDataTimeTo(endTime);
// mix: login|87601|968|67826|23579 or login|a=b&c=d&x=y&asdf
cosTaskConfig.setContentStyle(contentStyle);
cosTaskConfig.setDataSeparator("|");
cosTaskConfig.setFilterStreams(filterStreams);
dataConfig.setExtParams(GSON.toJson(cosTaskConfig));
return dataConfig;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public static void setup() {
helper = new AgentBaseTestsHelper(TestInstanceManager.class.getName()).setupAgentHome();
String pattern = helper.getTestRootDir() + "/YYYYMMDDhh_[0-9]+.txt";
Store basicInstanceStore = TaskManager.initStore(AgentConstants.AGENT_STORE_PATH_INSTANCE);
taskProfile = helper.getTaskProfile(1, pattern, "csv", false, "", "", TaskStateEnum.RUNNING, CycleUnitType.HOUR,
"GMT+6:00", null);
taskProfile =
helper.getFileTaskProfile(1, pattern, "csv", false, "", "", TaskStateEnum.RUNNING, CycleUnitType.HOUR,
"GMT+6:00", null);
Store taskBasicStore = TaskManager.initStore(AgentConstants.AGENT_STORE_PATH_TASK);
TaskStore taskStore = new TaskStore(taskBasicStore);
taskStore.storeTask(taskProfile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public static void setUp() throws Exception {
String fileName = LOADER.getResource("test/20230928_1.txt").getPath();
helper = new AgentBaseTestsHelper(TestSenderManager.class.getName()).setupAgentHome();
String pattern = helper.getTestRootDir() + "/YYYYMMDD.log_[0-9]+";
TaskProfile taskProfile = helper.getTaskProfile(1, pattern, "csv", false, "", "", TaskStateEnum.RUNNING, "D",
"GMT+8:00", null);
TaskProfile taskProfile =
helper.getFileTaskProfile(1, pattern, "csv", false, "", "", TaskStateEnum.RUNNING, "D",
"GMT+8:00", null);
profile = taskProfile.createInstanceProfile("", fileName,
taskProfile.getCycleUnit(), "20230927", AgentUtils.getCurrentTime());
kafkaSink = new MockSink();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public static void setUp() throws Exception {
String fileName = LOADER.getResource("test/20230928_1.txt").getPath();
helper = new AgentBaseTestsHelper(TestSenderManager.class.getName()).setupAgentHome();
String pattern = helper.getTestRootDir() + "/YYYYMMDD.log_[0-9]+";
TaskProfile taskProfile = helper.getTaskProfile(1, pattern, "csv", false, "", "", TaskStateEnum.RUNNING, "D",
"GMT+8:00", null);
TaskProfile taskProfile =
helper.getFileTaskProfile(1, pattern, "csv", false, "", "", TaskStateEnum.RUNNING, "D",
"GMT+8:00", null);
profile = taskProfile.createInstanceProfile("", fileName,
taskProfile.getCycleUnit(), "20230927", AgentUtils.getCurrentTime());
pulsarSink = new MockSink();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public static void setup() {
String fileName = LOADER.getResource("test/20230928_1.txt").getPath();
helper = new AgentBaseTestsHelper(TestSenderManager.class.getName()).setupAgentHome();
String pattern = helper.getTestRootDir() + "/YYYYMMDD.log_[0-9]+";
TaskProfile taskProfile = helper.getTaskProfile(1, pattern, "csv", false, "", "", TaskStateEnum.RUNNING, "D",
"GMT+8:00", null);
TaskProfile taskProfile =
helper.getFileTaskProfile(1, pattern, "csv", false, "", "", TaskStateEnum.RUNNING, "D",
"GMT+8:00", null);
profile = taskProfile.createInstanceProfile("", fileName,
taskProfile.getCycleUnit(), "20230927", AgentUtils.getCurrentTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private LogFileSource getSource(int taskId, long lineOffset, long byteOffset, St
fileName = LOADER.getResource("test/20230928_1.txt").getPath();
pattern = helper.getTestRootDir() + "/YYYYMMDD.log_[0-9]+";
retry = false;
TaskProfile taskProfile = helper.getTaskProfile(taskId, pattern, dataContentStyle, retry, "", "",
TaskProfile taskProfile = helper.getFileTaskProfile(taskId, pattern, dataContentStyle, retry, "", "",
TaskStateEnum.RUNNING, "D",
"GMT+8:00", Arrays.asList("ok"));
InstanceProfile instanceProfile = taskProfile.createInstanceProfile("",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void initProfile() {
final String command = "zscore";
final String subOperation = "set,del";

TaskProfile taskProfile = helper.getTaskProfile(1, "", "csv", false, "", "", TaskStateEnum.RUNNING, "D",
TaskProfile taskProfile = helper.getFileTaskProfile(1, "", "csv", false, "", "", TaskStateEnum.RUNNING, "D",
"GMT+8:00", null);
profile = taskProfile.createInstanceProfile("",
"", taskProfile.getCycleUnit(), "20240725", AgentUtils.getCurrentTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private SQLServerSource getSource() {
final String tableName = "test_source";
final String serverName = "server-01";

TaskProfile taskProfile = helper.getTaskProfile(1, "", "csv", false, "", "", TaskStateEnum.RUNNING, "D",
TaskProfile taskProfile = helper.getFileTaskProfile(1, "", "csv", false, "", "", TaskStateEnum.RUNNING, "D",
"GMT+8:00", null);
instanceProfile = taskProfile.createInstanceProfile("",
"", taskProfile.getCycleUnit(), "20240725", AgentUtils.getCurrentTime());
Expand Down
Loading

0 comments on commit 4d78384

Please sign in to comment.