@@ -85,7 +85,7 @@ public class LoadAction extends RestBaseController {
85
85
86
86
@ RequestMapping (path = "/api/{" + DB_KEY + "}/{" + TABLE_KEY + "}/_load" , method = RequestMethod .PUT )
87
87
public Object load (HttpServletRequest request , HttpServletResponse response ,
88
- @ PathVariable (value = DB_KEY ) String db , @ PathVariable (value = TABLE_KEY ) String table ) {
88
+ @ PathVariable (value = DB_KEY ) String db , @ PathVariable (value = TABLE_KEY ) String table ) {
89
89
if (needRedirect (request .getScheme ())) {
90
90
return redirectToHttps (request );
91
91
}
@@ -102,21 +102,29 @@ public Object load(HttpServletRequest request, HttpServletResponse response,
102
102
103
103
@ RequestMapping (path = "/api/{" + DB_KEY + "}/{" + TABLE_KEY + "}/_stream_load" , method = RequestMethod .PUT )
104
104
public Object streamLoad (HttpServletRequest request ,
105
- HttpServletResponse response ,
106
- @ PathVariable (value = DB_KEY ) String db , @ PathVariable (value = TABLE_KEY ) String table ) {
107
- LOG .info ("streamload action, db: {}, tbl: {}, headers: {}" , db , table , getAllHeaders (request ));
105
+ HttpServletResponse response ,
106
+ @ PathVariable (value = DB_KEY ) String db , @ PathVariable (value = TABLE_KEY ) String table ) {
107
+ LOG .info ("streamload action, db: {}, tbl: {}, headers: {}" , db , table , getAllHeaders (request ));
108
108
boolean groupCommit = false ;
109
109
String groupCommitStr = request .getHeader ("group_commit" );
110
- if (groupCommitStr != null && groupCommitStr .equalsIgnoreCase ("async_mode" )) {
111
- groupCommit = true ;
112
- try {
113
- if (isGroupCommitBlock (db , table )) {
114
- String msg = "insert table " + table + GroupCommitPlanner .SCHEMA_CHANGE ;
115
- return new RestBaseResult (msg );
110
+ if (groupCommitStr != null ) {
111
+ if (!groupCommitStr .equalsIgnoreCase ("async_mode" ) && !groupCommitStr .equalsIgnoreCase ("sync_mode" )
112
+ && !groupCommitStr .equalsIgnoreCase ("off_mode" )) {
113
+ return new RestBaseResult ("Header `group_commit` can only be `sync_mode`, `async_mode` or `off_mode`." );
114
+ }
115
+ if (!groupCommitStr .equalsIgnoreCase ("off_mode" )) {
116
+ groupCommit = true ;
117
+ if (groupCommitStr .equalsIgnoreCase ("async_mode" )) {
118
+ try {
119
+ if (isGroupCommitBlock (db , table )) {
120
+ String msg = "insert table " + table + GroupCommitPlanner .SCHEMA_CHANGE ;
121
+ return new RestBaseResult (msg );
122
+ }
123
+ } catch (Exception e ) {
124
+ LOG .info ("exception:" + e );
125
+ return new RestBaseResult (e .getMessage ());
126
+ }
116
127
}
117
- } catch (Exception e ) {
118
- LOG .info ("exception:" + e );
119
- return new RestBaseResult (e .getMessage ());
120
128
}
121
129
}
122
130
if (needRedirect (request .getScheme ())) {
@@ -147,21 +155,32 @@ public Object streamLoadWithSql(HttpServletRequest request, HttpServletResponse
147
155
boolean groupCommit = false ;
148
156
long tableId = -1 ;
149
157
String groupCommitStr = request .getHeader ("group_commit" );
150
- if (groupCommitStr != null && groupCommitStr .equalsIgnoreCase ("async_mode" )) {
151
- groupCommit = true ;
152
- try {
153
- String [] pair = parseDbAndTb (sql );
154
- Database db = Env .getCurrentInternalCatalog ()
155
- .getDbOrException (pair [0 ], s -> new TException ("database is invalid for dbName: " + s ));
156
- Table tbl = db .getTableOrException (pair [1 ], s -> new TException ("table is invalid: " + s ));
157
- tableId = tbl .getId ();
158
- if (isGroupCommitBlock (pair [0 ], pair [1 ])) {
159
- String msg = "insert table " + pair [1 ] + GroupCommitPlanner .SCHEMA_CHANGE ;
160
- return new RestBaseResult (msg );
158
+ if (groupCommitStr != null ) {
159
+ if (!groupCommitStr .equalsIgnoreCase ("async_mode" ) && !groupCommitStr .equalsIgnoreCase ("sync_mode" )
160
+ && !groupCommitStr .equalsIgnoreCase ("off_mode" )) {
161
+ return new RestBaseResult ("Header `group_commit` can only be `sync_mode`, `async_mode` or `off_mode`." );
162
+ }
163
+ if (!groupCommitStr .equalsIgnoreCase ("off_mode" )) {
164
+ try {
165
+ groupCommit = true ;
166
+ String [] pair = parseDbAndTb (sql );
167
+ Database db = Env .getCurrentInternalCatalog ()
168
+ .getDbOrException (pair [0 ], s -> new TException ("database is invalid for dbName: " + s ));
169
+ Table tbl = db .getTableOrException (pair [1 ], s -> new TException ("table is invalid: " + s ));
170
+ tableId = tbl .getId ();
171
+
172
+ // async mode needs to write WAL, we need to block load during waiting WAL.
173
+ if (groupCommitStr .equalsIgnoreCase ("async_mode" )) {
174
+ if (isGroupCommitBlock (pair [0 ], pair [1 ])) {
175
+ String msg = "insert table " + pair [1 ] + GroupCommitPlanner .SCHEMA_CHANGE ;
176
+ return new RestBaseResult (msg );
177
+ }
178
+
179
+ }
180
+ } catch (Exception e ) {
181
+ LOG .info ("exception:" + e );
182
+ return new RestBaseResult (e .getMessage ());
161
183
}
162
- } catch (Exception e ) {
163
- LOG .info ("exception:" + e );
164
- return new RestBaseResult (e .getMessage ());
165
184
}
166
185
}
167
186
executeCheckPassword (request , response );
@@ -223,8 +242,8 @@ private String[] parseDbAndTb(String sql) throws Exception {
223
242
224
243
@ RequestMapping (path = "/api/{" + DB_KEY + "}/_stream_load_2pc" , method = RequestMethod .PUT )
225
244
public Object streamLoad2PC (HttpServletRequest request ,
226
- HttpServletResponse response ,
227
- @ PathVariable (value = DB_KEY ) String db ) {
245
+ HttpServletResponse response ,
246
+ @ PathVariable (value = DB_KEY ) String db ) {
228
247
LOG .info ("streamload action 2PC, db: {}, headers: {}" , db , getAllHeaders (request ));
229
248
if (needRedirect (request .getScheme ())) {
230
249
return redirectToHttps (request );
@@ -236,9 +255,9 @@ public Object streamLoad2PC(HttpServletRequest request,
236
255
237
256
@ RequestMapping (path = "/api/{" + DB_KEY + "}/{" + TABLE_KEY + "}/_stream_load_2pc" , method = RequestMethod .PUT )
238
257
public Object streamLoad2PC_table (HttpServletRequest request ,
239
- HttpServletResponse response ,
240
- @ PathVariable (value = DB_KEY ) String db ,
241
- @ PathVariable (value = TABLE_KEY ) String table ) {
258
+ HttpServletResponse response ,
259
+ @ PathVariable (value = DB_KEY ) String db ,
260
+ @ PathVariable (value = TABLE_KEY ) String table ) {
242
261
LOG .info ("streamload action 2PC, db: {}, tbl: {}, headers: {}" , db , table , getAllHeaders (request ));
243
262
if (needRedirect (request .getScheme ())) {
244
263
return redirectToHttps (request );
@@ -382,7 +401,7 @@ private TNetworkAddress selectRedirectBackend(HttpServletRequest request, boolea
382
401
if (Strings .isNullOrEmpty (cloudClusterName )) {
383
402
throw new LoadException ("No cloud cluster name selected." );
384
403
}
385
- return selectCloudRedirectBackend (cloudClusterName , request , groupCommit );
404
+ return selectCloudRedirectBackend (cloudClusterName , request , groupCommit , tableId );
386
405
} else {
387
406
return selectLocalRedirectBackend (groupCommit , request , tableId );
388
407
}
@@ -409,21 +428,7 @@ private TNetworkAddress selectLocalRedirectBackend(boolean groupCommit, HttpServ
409
428
throw new LoadException (SystemInfoService .NO_BACKEND_LOAD_AVAILABLE_MSG + ", policy: " + policy );
410
429
}
411
430
if (groupCommit ) {
412
- ConnectContext ctx = new ConnectContext ();
413
- ctx .setEnv (Env .getCurrentEnv ());
414
- ctx .setThreadLocalInfo ();
415
- ctx .setRemoteIP (request .getRemoteAddr ());
416
- // We set this variable to fulfill required field 'user' in
417
- // TMasterOpRequest(FrontendService.thrift)
418
- ctx .setQualifiedUser (Auth .ADMIN_USER );
419
- ctx .setThreadLocalInfo ();
420
-
421
- try {
422
- backend = Env .getCurrentEnv ().getGroupCommitManager ()
423
- .selectBackendForGroupCommit (tableId , ctx , false );
424
- } catch (DdlException e ) {
425
- throw new RuntimeException (e );
426
- }
431
+ backend = selectBackendForGroupCommit ("" , request , tableId , false );
427
432
} else {
428
433
backend = Env .getCurrentSystemInfo ().getBackend (backendIds .get (0 ));
429
434
}
@@ -433,17 +438,23 @@ private TNetworkAddress selectLocalRedirectBackend(boolean groupCommit, HttpServ
433
438
return new TNetworkAddress (backend .getHost (), backend .getHttpPort ());
434
439
}
435
440
436
- private TNetworkAddress selectCloudRedirectBackend (String clusterName , HttpServletRequest req , boolean groupCommit )
441
+ private TNetworkAddress selectCloudRedirectBackend (String clusterName , HttpServletRequest req , boolean groupCommit ,
442
+ long tableId )
437
443
throws LoadException {
438
- Backend backend = StreamLoadHandler .selectBackend (clusterName , groupCommit );
444
+ Backend backend = null ;
445
+ if (groupCommit ) {
446
+ backend = selectBackendForGroupCommit (clusterName , req , tableId , true );
447
+ } else {
448
+ backend = StreamLoadHandler .selectBackend (clusterName );
449
+ }
439
450
440
451
String redirectPolicy = req .getHeader (LoadAction .HEADER_REDIRECT_POLICY );
441
452
// User specified redirect policy
442
453
if (redirectPolicy != null && redirectPolicy .equalsIgnoreCase (REDIRECT_POLICY_RANDOM_BE )) {
443
454
return new TNetworkAddress (backend .getHost (), backend .getHttpPort ());
444
455
}
445
456
redirectPolicy = redirectPolicy == null || redirectPolicy .isEmpty ()
446
- ? Config .streamload_redirect_policy : redirectPolicy ;
457
+ ? Config .streamload_redirect_policy : redirectPolicy ;
447
458
448
459
Pair <String , Integer > publicHostPort = null ;
449
460
Pair <String , Integer > privateHostPort = null ;
@@ -563,7 +574,7 @@ private boolean checkClusterToken(String token) {
563
574
// temporarily addressing the users' needs for audit logs.
564
575
// So this function is not widely tested under general scenario
565
576
private Object executeWithClusterToken (HttpServletRequest request , String db ,
566
- String table , boolean isStreamLoad ) {
577
+ String table , boolean isStreamLoad ) {
567
578
try {
568
579
ConnectContext ctx = new ConnectContext ();
569
580
ctx .setEnv (Env .getCurrentEnv ());
@@ -647,4 +658,29 @@ private String getAllHeaders(HttpServletRequest request) {
647
658
}
648
659
return headers .toString ();
649
660
}
661
+
662
+ private Backend selectBackendForGroupCommit (String clusterName , HttpServletRequest req , long tableId ,
663
+ boolean isCloud )
664
+ throws LoadException {
665
+ ConnectContext ctx = new ConnectContext ();
666
+ ctx .setEnv (Env .getCurrentEnv ());
667
+ ctx .setThreadLocalInfo ();
668
+ ctx .setRemoteIP (req .getRemoteAddr ());
669
+ // We set this variable to fulfill required field 'user' in
670
+ // TMasterOpRequest(FrontendService.thrift)
671
+ ctx .setQualifiedUser (Auth .ADMIN_USER );
672
+ ctx .setThreadLocalInfo ();
673
+ if (isCloud ) {
674
+ ctx .setCloudCluster (clusterName );
675
+ }
676
+
677
+ Backend backend = null ;
678
+ try {
679
+ backend = Env .getCurrentEnv ().getGroupCommitManager ()
680
+ .selectBackendForGroupCommit (tableId , ctx , isCloud );
681
+ } catch (DdlException e ) {
682
+ throw new LoadException (e .getMessage (), e );
683
+ }
684
+ return backend ;
685
+ }
650
686
}
0 commit comments