@@ -43,6 +43,7 @@ public class ObDirectLoadStatementExecutor {
4343 private static final int FAIL = 6 ;
4444 private static final int ABORT = 7 ;
4545 private AtomicInteger stateFlag = new AtomicInteger (NONE );
46+ private boolean isDetached = false ;
4647
4748 private final ObDirectLoadStatement statement ;
4849 private final ObDirectLoadTraceId traceId ;
@@ -88,6 +89,10 @@ public ObAddr getSvrAddr() {
8889 return svrAddr ;
8990 }
9091
92+ public boolean isDetached () {
93+ return isDetached ;
94+ }
95+
9196 public String toString () {
9297 return String .format ("{svrAddr:%s, tableId:%d, taskId:%d}" , svrAddr , tableId , taskId );
9398 }
@@ -134,6 +139,28 @@ public synchronized ObDirectLoadStatementFuture commit() {
134139 return task ;
135140 }
136141
142+ public synchronized void detach () throws ObDirectLoadException {
143+ logger .info ("statement call detach" );
144+ checkState (LOADING , COMMITTING , "detach" );
145+ if (isDetached ) {
146+ logger .debug ("statement already is detached" );
147+ } else {
148+ ObDirectLoadStatementPromiseTask task = new ObDirectLoadStatementDetachTask (statement ,
149+ this );
150+ task .run ();
151+ if (!task .isDone ()) {
152+ logger .warn ("statement detach task unexpected not done" );
153+ throw new ObDirectLoadUnexpectedException (
154+ "statement detach task unexpected not done" );
155+ }
156+ if (!task .isSuccess ()) {
157+ throw task .cause ();
158+ }
159+ isDetached = true ;
160+ logger .debug ("statement detach successful" );
161+ }
162+ }
163+
137164 public ObDirectLoadStatementExecutionId getExecutionId () throws ObDirectLoadException {
138165 checkState (LOADING , "getExecutionId" );
139166 ObDirectLoadStatementExecutionId executionId = new ObDirectLoadStatementExecutionId (
@@ -155,59 +182,74 @@ public synchronized void resume(ObDirectLoadStatementExecutionId executionId)
155182 svrAddr = executionId .getSvrAddr ();
156183 }
157184
158- public synchronized void close () {
185+ public void close () {
159186 // 如果begin还在执行, 等待begin结束
160- if (beginFuture != null && !beginFuture .isDone ()) {
187+ ObDirectLoadStatementFuture beginFuture = null ;
188+ synchronized (this ) {
189+ if (this .beginFuture != null && !this .beginFuture .isDone ()) {
190+ beginFuture = this .beginFuture ;
191+ }
192+ }
193+ if (beginFuture != null ) {
161194 logger .info ("statement close wait begin" );
162195 try {
163196 beginFuture .await ();
164197 } catch (ObDirectLoadInterruptedException e ) {
165198 logger .warn ("statement wait begin failed" );
166199 }
167200 }
168- beginFuture = null ;
169201 // 如果commit还在执行, 等待commit结束
170- if (commitFuture != null && !commitFuture .isDone ()) {
202+ ObDirectLoadStatementFuture commitFuture = null ;
203+ synchronized (this ) {
204+ if (this .commitFuture != null && !this .commitFuture .isDone ()) {
205+ commitFuture = this .commitFuture ;
206+ }
207+ }
208+ if (commitFuture != null ) {
171209 logger .info ("statement close wait commit" );
172210 try {
173211 commitFuture .await ();
174212 } catch (ObDirectLoadInterruptedException e ) {
175213 logger .warn ("statement wait commit failed" );
176214 }
177215 }
178- commitFuture = null ;
179216 // 如果heart beat还在执行, 取消heart beat
180- if (heartBeatTask != null && !heartBeatTask .isDone ()) {
181- logger .info ("statement close wait heart beat" );
182- final boolean canceled = heartBeatTask .cancel ();
183- if (!canceled ) {
184- try {
185- heartBeatTask .await ();
186- } catch (ObDirectLoadInterruptedException e ) {
187- logger .warn ("statement wait heart beat failed" );
217+ ObDirectLoadStatementHeartBeatTask heartBeatTask = null ;
218+ synchronized (this ) {
219+ if (this .heartBeatTask != null && !this .heartBeatTask .isDone ()) {
220+ final boolean canceled = this .heartBeatTask .cancel ();
221+ if (!canceled ) {
222+ heartBeatTask = this .heartBeatTask ;
188223 }
189224 }
190225 }
191- heartBeatTask = null ;
226+ if (heartBeatTask != null ) {
227+ logger .info ("statement close wait heart beat" );
228+ try {
229+ heartBeatTask .await ();
230+ } catch (ObDirectLoadInterruptedException e ) {
231+ logger .warn ("statement wait heart beat failed" );
232+ }
233+ }
192234 // 退出任务
193235 abortIfNeed ();
236+ ObDirectLoadStatementFuture abortFuture = null ;
237+ synchronized (this ) {
238+ if (this .abortFuture != null && !this .abortFuture .isDone ()) {
239+ abortFuture = this .abortFuture ;
240+ }
241+ }
194242 if (abortFuture != null ) {
243+ logger .info ("statement close wait abort" );
195244 try {
196- if (!abortFuture .isDone ()) {
197- logger .info ("statement close wait abort" );
198- abortFuture .await ();
199- }
200- if (!abortFuture .isSuccess ()) {
201- throw abortFuture .cause ();
202- }
203- logger .info ("statement abort successful" );
245+ abortFuture .await ();
204246 } catch (ObDirectLoadException e ) {
205247 logger .warn ("statement abort failed" , e );
206248 }
207249 }
208250 }
209251
210- private void abortIfNeed () {
252+ private synchronized void abortIfNeed () {
211253 logger .debug ("statement abort if need" );
212254 if (abortFuture != null ) {
213255 logger .debug ("statement in abort" );
@@ -247,6 +289,8 @@ private void abortIfNeed() {
247289 logger .debug ("statement no need abort because " + reason );
248290 setState (ABORT );
249291 }
292+ } else if (isDetached ) {
293+ logger .debug ("statement no need abort because is detached" );
250294 } else {
251295 abort ();
252296 }
@@ -471,9 +515,7 @@ void setFailure(ObDirectLoadException cause) {
471515 }
472516 executor .cause = cause ;
473517 logger .warn ("statement begin failed" , cause );
474- synchronized (executor ) {
475- executor .abortIfNeed ();
476- }
518+ executor .abortIfNeed ();
477519 }
478520
479521 void clear () {
@@ -508,9 +550,7 @@ void setFailure(ObDirectLoadException cause) {
508550 }
509551 executor .cause = cause ;
510552 logger .warn ("statement commit failed" , cause );
511- synchronized (executor ) {
512- executor .abortIfNeed ();
513- }
553+ executor .abortIfNeed ();
514554 }
515555
516556 };
@@ -559,9 +599,7 @@ void setFailure(ObDirectLoadException cause) {
559599 }
560600 executor .cause = cause ;
561601 logger .warn ("statement heart beat failed" , cause );
562- synchronized (executor ) {
563- executor .abortIfNeed ();
564- }
602+ executor .abortIfNeed ();
565603 }
566604
567605 };
0 commit comments