47
47
import org .apache .doris .common .UserException ;
48
48
import org .apache .doris .common .io .DeepCopy ;
49
49
import org .apache .doris .common .io .Text ;
50
- import org .apache .doris .common .profile .SummaryProfile ;
51
50
import org .apache .doris .common .util .PropertyAnalyzer ;
52
51
import org .apache .doris .common .util .Util ;
52
+ import org .apache .doris .datasource .InternalCatalog ;
53
53
import org .apache .doris .mtmv .MTMVRelatedTableIf ;
54
54
import org .apache .doris .mtmv .MTMVSnapshotIf ;
55
55
import org .apache .doris .mtmv .MTMVVersionSnapshot ;
56
56
import org .apache .doris .persist .gson .GsonPostProcessable ;
57
57
import org .apache .doris .persist .gson .GsonUtils ;
58
58
import org .apache .doris .qe .ConnectContext ;
59
59
import org .apache .doris .qe .OriginStatement ;
60
- import org .apache .doris .qe .StmtExecutor ;
61
60
import org .apache .doris .resource .Tag ;
62
61
import org .apache .doris .rpc .RpcException ;
63
62
import org .apache .doris .statistics .AnalysisInfo ;
@@ -2225,7 +2224,6 @@ public int getBaseSchemaVersion() {
2225
2224
return baseIndexMeta .getSchemaVersion ();
2226
2225
}
2227
2226
2228
-
2229
2227
public void setEnableSingleReplicaCompaction (boolean enableSingleReplicaCompaction ) {
2230
2228
if (tableProperty == null ) {
2231
2229
tableProperty = new TableProperty (new HashMap <>());
@@ -2849,6 +2847,7 @@ public long getVisibleVersion() {
2849
2847
if (Config .isNotCloudMode ()) {
2850
2848
return tableAttributes .getVisibleVersion ();
2851
2849
}
2850
+
2852
2851
// get version rpc
2853
2852
Cloud .GetVersionRequest request = Cloud .GetVersionRequest .newBuilder ()
2854
2853
.setDbId (this .getDatabase ().getId ())
@@ -2858,7 +2857,7 @@ public long getVisibleVersion() {
2858
2857
.build ();
2859
2858
2860
2859
try {
2861
- Cloud .GetVersionResponse resp = getVersionFromMeta (request );
2860
+ Cloud .GetVersionResponse resp = VersionHelper . getVersionFromMeta (request );
2862
2861
long version = -1 ;
2863
2862
if (resp .getStatus ().getCode () == Cloud .MetaServiceCode .OK ) {
2864
2863
version = resp .getVersion ();
@@ -2874,7 +2873,90 @@ public long getVisibleVersion() {
2874
2873
}
2875
2874
return version ;
2876
2875
} catch (RpcException e ) {
2877
- throw new RuntimeException ("get version from meta service failed" );
2876
+ throw new RuntimeException ("get version from meta service failed" , e );
2877
+ }
2878
+ }
2879
+
2880
+ // Get the table versions in batch.
2881
+ public static List <Long > getVisibleVersionByTableIds (Collection <Long > tableIds ) {
2882
+ List <OlapTable > tables = new ArrayList <>();
2883
+
2884
+ InternalCatalog catalog = Env .getCurrentEnv ().getInternalCatalog ();
2885
+ for (long tableId : tableIds ) {
2886
+ Table table = catalog .getTableByTableId (tableId );
2887
+ if (table == null ) {
2888
+ throw new RuntimeException ("get table visible version failed, no such table " + tableId + " exists" );
2889
+ }
2890
+ if (table .getType () != TableType .OLAP ) {
2891
+ throw new RuntimeException (
2892
+ "get table visible version failed, table " + tableId + " is not a OLAP table" );
2893
+ }
2894
+ tables .add ((OlapTable ) table );
2895
+ }
2896
+
2897
+ return getVisibleVersionInBatch (tables );
2898
+ }
2899
+
2900
+ // Get the table versions in batch.
2901
+ public static List <Long > getVisibleVersionInBatch (Collection <OlapTable > tables ) {
2902
+ if (tables .isEmpty ()) {
2903
+ return new ArrayList <>();
2904
+ }
2905
+
2906
+ if (Config .isNotCloudMode ()) {
2907
+ return tables .stream ()
2908
+ .map (table -> table .tableAttributes .getVisibleVersion ())
2909
+ .collect (Collectors .toList ());
2910
+ }
2911
+
2912
+ List <Long > dbIds = new ArrayList <>();
2913
+ List <Long > tableIds = new ArrayList <>();
2914
+ for (OlapTable table : tables ) {
2915
+ dbIds .add (table .getDatabase ().getId ());
2916
+ tableIds .add (table .getId ());
2917
+ }
2918
+
2919
+ return getVisibleVersionFromMeta (dbIds , tableIds );
2920
+ }
2921
+
2922
+ private static List <Long > getVisibleVersionFromMeta (List <Long > dbIds , List <Long > tableIds ) {
2923
+ // get version rpc
2924
+ Cloud .GetVersionRequest request = Cloud .GetVersionRequest .newBuilder ()
2925
+ .setDbId (-1 )
2926
+ .setTableId (-1 )
2927
+ .setPartitionId (-1 )
2928
+ .addAllDbIds (dbIds )
2929
+ .addAllTableIds (tableIds )
2930
+ .setBatchMode (true )
2931
+ .setIsTableVersion (true )
2932
+ .build ();
2933
+
2934
+ try {
2935
+ Cloud .GetVersionResponse resp = VersionHelper .getVersionFromMeta (request );
2936
+ if (resp .getStatus ().getCode () != Cloud .MetaServiceCode .OK ) {
2937
+ throw new RpcException ("get table visible version" , "unexpected status " + resp .getStatus ());
2938
+ }
2939
+
2940
+ List <Long > versions = resp .getVersionsList ();
2941
+ if (versions .size () != tableIds .size ()) {
2942
+ throw new RpcException ("get table visible version" ,
2943
+ "wrong number of versions, required " + tableIds .size () + ", but got " + versions .size ());
2944
+ }
2945
+
2946
+ if (LOG .isDebugEnabled ()) {
2947
+ LOG .debug ("get table version from meta service, tables: {}, versions: {}" , tableIds , versions );
2948
+ }
2949
+
2950
+ for (int i = 0 ; i < versions .size (); i ++) {
2951
+ // Set visible version to 1 if no such table version exists.
2952
+ if (versions .get (i ) <= 0L ) {
2953
+ versions .set (i , 1L );
2954
+ }
2955
+ }
2956
+
2957
+ return versions ;
2958
+ } catch (RpcException e ) {
2959
+ throw new RuntimeException ("get table version from meta service failed" , e );
2878
2960
}
2879
2961
}
2880
2962
@@ -2921,19 +3003,6 @@ public MTMVSnapshotIf getTableSnapshot() {
2921
3003
return new MTMVVersionSnapshot (visibleVersion );
2922
3004
}
2923
3005
2924
- private static Cloud .GetVersionResponse getVersionFromMeta (Cloud .GetVersionRequest req )
2925
- throws RpcException {
2926
- long startAt = System .nanoTime ();
2927
- try {
2928
- return VersionHelper .getVisibleVersion (req );
2929
- } finally {
2930
- SummaryProfile profile = getSummaryProfile ();
2931
- if (profile != null ) {
2932
- profile .addGetTableVersionTime (System .nanoTime () - startAt );
2933
- }
2934
- }
2935
- }
2936
-
2937
3006
@ Override
2938
3007
public boolean needAutoRefresh () {
2939
3008
return true ;
@@ -2944,17 +3013,6 @@ public boolean isPartitionColumnAllowNull() {
2944
3013
return true ;
2945
3014
}
2946
3015
2947
- private static SummaryProfile getSummaryProfile () {
2948
- ConnectContext ctx = ConnectContext .get ();
2949
- if (ctx != null ) {
2950
- StmtExecutor executor = ctx .getExecutor ();
2951
- if (executor != null ) {
2952
- return executor .getSummaryProfile ();
2953
- }
2954
- }
2955
- return null ;
2956
- }
2957
-
2958
3016
public void setStatistics (Statistics statistics ) {
2959
3017
this .statistics = statistics ;
2960
3018
}
0 commit comments