Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;

import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
import org.apache.skywalking.oap.server.core.query.enumeration.Order;
import org.apache.skywalking.oap.server.core.query.enumeration.Step;
import org.apache.skywalking.oap.server.core.query.input.Duration;
import org.apache.skywalking.oap.server.core.query.input.TopNCondition;
import org.apache.skywalking.oap.server.core.query.type.KeyValue;
import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class JDBCAggregationQueryDAOTest {

private static final String TABLE = "metrics_table";

@Mock
private JDBCClient jdbcClient;
@Mock
private TableHelper tableHelper;

private JDBCAggregationQueryDAO dao;

@BeforeEach
void setUp() {
dao = new JDBCAggregationQueryDAO(jdbcClient, tableHelper);
}

private TopNCondition buildCondition(String name, int topN, Order order) {
final TopNCondition condition = new TopNCondition();
condition.setName(name);
condition.setTopN(topN);
condition.setOrder(order);
return condition;
}

private Duration buildDuration() {
final Duration duration = new Duration();
duration.setStart("2023-01-01 0000");
duration.setEnd("2023-01-02 0000");
duration.setStep(Step.MINUTE);
return duration;
}

@Test
void buildSQL_shouldContainTableColumnAndTimeBucketRange() {
final TopNCondition condition = buildCondition("service_resp_time", 10, Order.DES);
final Duration duration = buildDuration();

final SQLAndParameters result = dao.buildSQL(condition, "value", duration, null, TABLE);

assertThat(result.sql()).contains(JDBCTableInstaller.TABLE_COLUMN + " = ?");
assertThat(result.sql()).contains(Metrics.TIME_BUCKET + " >= ?");
assertThat(result.sql()).contains(Metrics.TIME_BUCKET + " <= ?");
}

@Test
void buildSQL_shouldContainSubqueryWithGroupBy() {
final TopNCondition condition = buildCondition("service_resp_time", 10, Order.DES);
final Duration duration = buildDuration();

final SQLAndParameters result = dao.buildSQL(condition, "value", duration, null, TABLE);

assertThat(result.sql()).contains("avg(value) as result");
assertThat(result.sql()).contains("group by " + Metrics.ENTITY_ID);
assertThat(result.sql()).contains("as T order by result");
}

@Test
void buildSQL_withOrderDES_shouldContainDesc() {
final TopNCondition condition = buildCondition("service_resp_time", 5, Order.DES);
final Duration duration = buildDuration();

final SQLAndParameters result = dao.buildSQL(condition, "value", duration, null, TABLE);

assertThat(result.sql()).contains("order by result desc");
assertThat(result.sql()).contains("limit 5");
}

@Test
void buildSQL_withOrderASC_shouldContainAsc() {
final TopNCondition condition = buildCondition("service_resp_time", 5, Order.ASC);
final Duration duration = buildDuration();

final SQLAndParameters result = dao.buildSQL(condition, "value", duration, null, TABLE);

assertThat(result.sql()).contains("order by result asc");
}

@Test
void buildSQL_withAdditionalConditions_shouldAppendConditions() {
final TopNCondition condition = buildCondition("service_resp_time", 10, Order.DES);
final Duration duration = buildDuration();
final KeyValue kv = new KeyValue("service_id", "svc-1");

final SQLAndParameters result = dao.buildSQL(
condition, "value", duration, Collections.singletonList(kv), TABLE);

assertThat(result.sql()).contains("service_id = ?");
assertThat(result.parameters()).contains("svc-1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;

import org.apache.skywalking.oap.server.core.profiling.ebpf.storage.EBPFProfilingDataRecord;
import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class JDBCEBPFProfilingDataDAOTest {

private static final String TABLE = "ebpf_profiling_data";

@Mock
private JDBCClient jdbcClient;
@Mock
private TableHelper tableHelper;

private JDBCEBPFProfilingDataDAO dao;

@BeforeEach
void setUp() {
dao = new JDBCEBPFProfilingDataDAO(jdbcClient, tableHelper);
}

@Test
void buildSQL_shouldContainTableColumnCondition() {
final SQLAndParameters result = dao.buildSQL(
Arrays.asList("schedule-1"), 1000L, 2000L, TABLE);

assertThat(result.sql()).contains(JDBCTableInstaller.TABLE_COLUMN + " = ?");
assertThat(result.parameters()).contains(EBPFProfilingDataRecord.INDEX_NAME);
}

@Test
void buildSQL_shouldContainScheduleIdInClause() {
final SQLAndParameters result = dao.buildSQL(
Arrays.asList("s1", "s2", "s3"), 1000L, 2000L, TABLE);

assertThat(result.sql()).contains(EBPFProfilingDataRecord.SCHEDULE_ID + " in (?, ?, ?)");
}

@Test
void buildSQL_shouldContainUploadTimeRange() {
final SQLAndParameters result = dao.buildSQL(
Arrays.asList("schedule-1"), 1000L, 2000L, TABLE);

assertThat(result.sql()).contains(EBPFProfilingDataRecord.UPLOAD_TIME + ">=?");
assertThat(result.sql()).contains(EBPFProfilingDataRecord.UPLOAD_TIME + "<?");
assertThat(result.parameters()).contains(1000L);
assertThat(result.parameters()).contains(2000L);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;

import org.apache.skywalking.oap.server.core.profiling.ebpf.storage.EBPFProfilingScheduleRecord;
import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class JDBCEBPFProfilingScheduleDAOTest {

private static final String TABLE = "ebpf_profiling_schedule";

@Mock
private JDBCClient jdbcClient;
@Mock
private TableHelper tableHelper;

private JDBCEBPFProfilingScheduleDAO dao;

@BeforeEach
void setUp() {
dao = new JDBCEBPFProfilingScheduleDAO(jdbcClient, tableHelper);
}

@Test
void buildSQL_shouldContainTableColumnCondition() {
final SQLAndParameters result = dao.buildSQL("task-1", TABLE);

assertThat(result.sql()).contains(JDBCTableInstaller.TABLE_COLUMN + " = ?");
assertThat(result.parameters()).contains(EBPFProfilingScheduleRecord.INDEX_NAME);
}

@Test
void buildSQL_shouldContainTaskIdCondition() {
final SQLAndParameters result = dao.buildSQL("task-abc", TABLE);

assertThat(result.sql()).contains(EBPFProfilingScheduleRecord.TASK_ID + "=?");
assertThat(result.parameters()).contains("task-abc");
}

@Test
void buildSQL_shouldContainWhereClause() {
final SQLAndParameters result = dao.buildSQL("task-1", TABLE);

assertThat(result.sql()).contains(" where ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.skywalking.oap.server.storage.plugin.jdbc.common.dao;

import org.apache.skywalking.oap.server.core.hierarchy.instance.InstanceHierarchyRelationTraffic;
import org.apache.skywalking.oap.server.core.hierarchy.service.ServiceHierarchyRelationTraffic;
import org.apache.skywalking.oap.server.library.client.jdbc.hikaricp.JDBCClient;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.JDBCTableInstaller;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.SQLAndParameters;
import org.apache.skywalking.oap.server.storage.plugin.jdbc.common.TableHelper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class JDBCHierarchyQueryDAOTest {

private static final String TABLE = "hierarchy_table";

@Mock
private JDBCClient jdbcClient;
@Mock
private TableHelper tableHelper;

private JDBCHierarchyQueryDAO dao;

@BeforeEach
void setUp() {
dao = new JDBCHierarchyQueryDAO(jdbcClient, 100, tableHelper);
}

@Test
void buildSQLForReadAllServiceRelations_shouldContainTableColumnAndLimit() {
final SQLAndParameters result = dao.buildSQLForReadAllServiceRelations(TABLE);

assertThat(result.sql()).contains(JDBCTableInstaller.TABLE_COLUMN + " = ?");
assertThat(result.parameters()).contains(ServiceHierarchyRelationTraffic.INDEX_NAME);
assertThat(result.sql()).contains("limit 200");
}

@Test
void buildSQLForReadInstanceRelations_shouldContainBidirectionalCondition() {
final SQLAndParameters result = dao.buildSQLForReadInstanceRelations(TABLE, "instance-1", 1);

assertThat(result.sql()).contains(JDBCTableInstaller.TABLE_COLUMN + " = ?");
assertThat(result.sql()).contains(InstanceHierarchyRelationTraffic.INSTANCE_ID + "=?");
assertThat(result.sql()).contains(InstanceHierarchyRelationTraffic.SERVICE_LAYER + "=?");
assertThat(result.sql()).contains(InstanceHierarchyRelationTraffic.RELATED_INSTANCE_ID + "=?");
assertThat(result.sql()).contains(InstanceHierarchyRelationTraffic.RELATED_SERVICE_LAYER + "=?");
}

@Test
void buildSQLForReadInstanceRelations_shouldHaveOrBetweenDirections() {
final SQLAndParameters result = dao.buildSQLForReadInstanceRelations(TABLE, "instance-1", 1);

assertThat(result.sql()).contains(") or (");
}

@Test
void buildSQLForReadInstanceRelations_shouldBindParametersCorrectly() {
final SQLAndParameters result = dao.buildSQLForReadInstanceRelations(TABLE, "inst-abc", 3);

assertThat(result.parameters())
.containsExactly(
InstanceHierarchyRelationTraffic.INDEX_NAME,
"inst-abc", 3,
"inst-abc", 3
);
}
}
Loading
Loading