1919
2020package org .apache .iotdb .db .queryengine .plan .analyze ;
2121
22+ import org .apache .iotdb .common .rpc .thrift .TConsensusGroupId ;
23+ import org .apache .iotdb .common .rpc .thrift .TConsensusGroupType ;
24+ import org .apache .iotdb .common .rpc .thrift .TRegionReplicaSet ;
25+ import org .apache .iotdb .common .rpc .thrift .TSStatus ;
2226import org .apache .iotdb .commons .queryengine .plan .relational .sql .ast .ComparisonExpression ;
2327import org .apache .iotdb .commons .queryengine .plan .relational .sql .ast .Expression ;
2428import org .apache .iotdb .commons .queryengine .plan .relational .sql .ast .Identifier ;
2529import org .apache .iotdb .commons .queryengine .plan .relational .sql .ast .LongLiteral ;
30+ import org .apache .iotdb .commons .queryengine .plan .relational .sql .ast .QualifiedName ;
31+ import org .apache .iotdb .commons .queryengine .plan .relational .sql .ast .Table ;
2632import org .apache .iotdb .commons .schema .table .TsTable ;
2733import org .apache .iotdb .commons .schema .table .column .TimeColumnSchema ;
34+ import org .apache .iotdb .confignode .rpc .thrift .TGetRegionGroupsByTimeReq ;
35+ import org .apache .iotdb .confignode .rpc .thrift .TGetRegionGroupsByTimeResp ;
36+ import org .apache .iotdb .db .protocol .client .ConfigNodeClient ;
37+ import org .apache .iotdb .db .queryengine .plan .relational .sql .ast .Delete ;
38+ import org .apache .iotdb .db .storageengine .dataregion .modification .DeletionPredicate ;
2839import org .apache .iotdb .db .storageengine .dataregion .modification .TableDeletionEntry ;
40+ import org .apache .iotdb .rpc .TSStatusCode ;
2941
3042import org .apache .tsfile .enums .TSDataType ;
43+ import org .apache .tsfile .read .common .TimeRange ;
3144import org .junit .Test ;
45+ import org .mockito .ArgumentCaptor ;
46+ import org .mockito .Mockito ;
3247
48+ import java .util .Arrays ;
49+ import java .util .Collections ;
50+ import java .util .HashSet ;
3351import java .util .List ;
52+ import java .util .Set ;
3453
3554import static org .junit .Assert .assertEquals ;
55+ import static org .junit .Assert .assertTrue ;
3656
3757public class AnalyzeUtilsTest {
3858
@@ -52,4 +72,58 @@ public void testParseDeletePredicateWithRenamedTimeColumn() {
5272 assertEquals (Long .MIN_VALUE , entries .get (0 ).getStartTime ());
5373 assertEquals (100 , entries .get (0 ).getEndTime ());
5474 }
75+
76+ @ Test
77+ public void testFetchDeleteReplicaSetsOnlyQueriesTargetDatabaseRegions () throws Exception {
78+ final Delete delete = new Delete (new Table (QualifiedName .of ("table1" )));
79+ delete .setDatabaseName ("root.db1" );
80+ delete .setTableDeletionEntries (
81+ Arrays .asList (
82+ new TableDeletionEntry (new DeletionPredicate ("table1" ), new TimeRange (10 , 20 )),
83+ new TableDeletionEntry (new DeletionPredicate ("table1" ), new TimeRange (30 , 40 ))));
84+
85+ final TRegionReplicaSet regionReplicaSet1 = dataRegionReplicaSet (1 );
86+ final TRegionReplicaSet regionReplicaSet2 = dataRegionReplicaSet (2 );
87+ final TGetRegionGroupsByTimeResp resp1 =
88+ successRegionGroupsResp (Collections .singleton (regionReplicaSet1 ));
89+ final TGetRegionGroupsByTimeResp resp2 =
90+ successRegionGroupsResp (new HashSet <>(Arrays .asList (regionReplicaSet1 , regionReplicaSet2 )));
91+ final ConfigNodeClient configNodeClient = Mockito .mock (ConfigNodeClient .class );
92+ Mockito .when (
93+ configNodeClient .getRegionGroupsByTime (Mockito .any (TGetRegionGroupsByTimeReq .class )))
94+ .thenReturn (resp1 , resp2 );
95+
96+ final Set <TRegionReplicaSet > result =
97+ AnalyzeUtils .fetchDeleteReplicaSets (configNodeClient , delete );
98+
99+ assertEquals (2 , result .size ());
100+ assertTrue (result .contains (regionReplicaSet1 ));
101+ assertTrue (result .contains (regionReplicaSet2 ));
102+
103+ final ArgumentCaptor <TGetRegionGroupsByTimeReq > reqCaptor =
104+ ArgumentCaptor .forClass (TGetRegionGroupsByTimeReq .class );
105+ Mockito .verify (configNodeClient , Mockito .times (2 )).getRegionGroupsByTime (reqCaptor .capture ());
106+ Mockito .verify (configNodeClient , Mockito .never ()).getLatestRegionRouteMap ();
107+
108+ final List <TGetRegionGroupsByTimeReq > requests = reqCaptor .getAllValues ();
109+ assertEquals ("root.db1" , requests .get (0 ).getDatabase ());
110+ assertEquals (10 , requests .get (0 ).getStartTime ());
111+ assertEquals (20 , requests .get (0 ).getEndTime ());
112+ assertEquals ("root.db1" , requests .get (1 ).getDatabase ());
113+ assertEquals (30 , requests .get (1 ).getStartTime ());
114+ assertEquals (40 , requests .get (1 ).getEndTime ());
115+ }
116+
117+ private static TGetRegionGroupsByTimeResp successRegionGroupsResp (
118+ final Set <TRegionReplicaSet > replicaSets ) {
119+ final TGetRegionGroupsByTimeResp resp =
120+ new TGetRegionGroupsByTimeResp (new TSStatus (TSStatusCode .SUCCESS_STATUS .getStatusCode ()));
121+ resp .setRegionReplicaSets (replicaSets );
122+ return resp ;
123+ }
124+
125+ private static TRegionReplicaSet dataRegionReplicaSet (final int regionId ) {
126+ return new TRegionReplicaSet (
127+ new TConsensusGroupId (TConsensusGroupType .DataRegion , regionId ), Collections .emptyList ());
128+ }
55129}
0 commit comments