@@ -155,6 +155,12 @@ class ObEmbeddingTaskPhaseManager {
155155 }
156156};
157157
158+ enum class ObEmbeddingTasSourceType
159+ {
160+ INDEX_PIPELINE = 0 ,
161+ ASYNC_INDEX = 1 ,
162+ };
163+
158164class ObEmbeddingTaskHandler ;
159165
160166// Constants for field lengths
@@ -169,10 +175,14 @@ class ObEmbeddingTask
169175 const ObString &provider,
170176 const ObString &user_key,
171177 const ObIArray<ObString> &input_chunks,
178+ const ObCollationType col_type,
172179 int64_t dimension,
173180 int64_t http_timeout_us,
174181 int64_t http_max_retries,
175- storage::ObEmbeddingIOCallbackHandle *cb_handle = nullptr );
182+ int64_t source_task_id,
183+ ObEmbeddingTasSourceType source_task_type,
184+ storage::ObEmbeddingIOCallbackHandle *cb_handle = nullptr ,
185+ bool always_retry = false );
176186 template <typename ThreadPoolType>
177187 int do_work (ThreadPoolType *thread_pool);
178188 int64_t get_task_id () const { return task_id_; }
@@ -181,29 +191,56 @@ class ObEmbeddingTask
181191
182192 TO_STRING_KV (K_(is_inited),
183193 K_ (task_id),
194+ K_ (phase),
184195 K_ (model_url),
185196 K_ (model_name),
186- K_ (user_key),
187197 K (input_chunks_.count()),
188198 K (output_vectors_.count()),
189199 K_ (dimension),
190200 K_ (batch_size),
201+ K_ (current_batch_idx),
202+ K_ (http_total_retry_count),
203+ K_ (http_retry_count),
204+ K_ (http_max_retry_count),
205+ K_ (http_retry_start_time_us),
206+ K_ (http_last_retry_time_us),
191207 K_ (processed_chunks),
192208 K_ (total_chunks),
193- K_ (process_callback_offset));
209+ K_ (process_callback_offset),
210+ K_ (col_type),
211+ K_ (current_input_token_limit),
212+ K_ (need_cancel),
213+ K_ (source_task_id),
214+ K_ (source_task_type),
215+ K_ (always_retry),
216+ K_ (next_handle_task_time_us),
217+ K_ (internal_error_code));
194218 bool is_completed ();
195219 void retain_if_managed ();
196220 void release_if_managed ();
197221 int get_async_result (ObArray<float *> &output_vectors);
198222 // 公共方法用于外部设置任务失败
199223 int mark_task_failed (int error_code);
200224 int maybe_callback ();
201- int wait_for_completion ();
225+ // Wait for task completion with periodic check interval
226+ // @param check_interval_us: the interval to wait before returning, must be > 0
227+ // @return:
228+ // - OB_INVALID_ARGUMENT: if check_interval_us <= 0
229+ // - OB_NOT_INIT: if task is not initialized
230+ // - OB_SUCCESS: if task completed (callback_done_ is true)
231+ // - OB_TIMEOUT: if task has exceeded wait_for_completion_timeout_us_ (real timeout)
232+ // - OB_EAGAIN: if wait timed out but task not yet exceeded total timeout,
233+ // caller should check cancel status and retry
234+ // Usage: call in a loop, check for cancel between calls, handle OB_TIMEOUT as task failure
235+ int wait_for_completion (int64_t check_interval_us);
202236 int wake_up ();
203237 void disable_callback ();
204238 void set_callback_done ();
205239 bool need_callback () { return cb_handle_ != nullptr ? true : false ; };
206-
240+ void set_need_cancel () { ATOMIC_STORE (&need_cancel_, true ); }
241+ bool need_cancel () const { return ATOMIC_LOAD (&need_cancel_); }
242+ common::ObCurTraceId::TraceId get_trace_id () const { return trace_id_; }
243+ void set_always_retry (bool always_retry) { always_retry_ = always_retry; }
207244public:
208245 static const ObString MODEL_URL_NAME;
209246 static const ObString MODEL_NAME_NAME;
@@ -215,28 +252,35 @@ class ObEmbeddingTask
215252 static const ObString USER_KEY_NAME;
216253 static const ObString INPUT_NAME;
217254 static const ObString DIMENSIONS_NAME;
255+ static const ObString REQUEST_TOO_LARGE_ERROR_MSG;
218256
219257 static const int64_t HTTP_REQUEST_TIMEOUT; // 20 seconds
220258
221259 // Reschedule related constants
222260 static const int64_t MAX_RESCHEDULE_RETRY_CNT;
223261 static const int64_t RESCHEDULE_RETRY_INTERVAL_US;
262+ static const int64_t MAX_NEXT_HANDLE_INTERVAL_US;
224263
225264 // HTTP retry related constants
226265 static const int64_t MAX_HTTP_RETRY_CNT;
227266 static const int64_t HTTP_RETRY_BASE_INTERVAL_US;
228267 static const int64_t HTTP_RETRY_MAX_INTERVAL_US;
229268 static const int64_t HTTP_RETRY_MULTIPLIER;
269+ static const int64_t ALWAYS_RETRY_MIN_INTERVAL_US;
270+ static const int64_t MIN_EMBEDDING_MODEL_RPM;
271+ static const int64_t EMBEDDING_MODEL_WAIT_RATIO;
230272
231273 // Callback related constants
232274 static const int64_t CALLBACK_BATCH_SIZE;
275+ static const int64_t MAX_INPUT_TOKEN; // Default max token count for each input: 512
233276
234277private:
278+ void init_members (); // Common initialization for all constructors
235279 void reset ();
236280 bool is_finished () const ; // Internal use only - no lock needed
237- void set_stop ();
281+ void set_stop (int ret_code );
238282 int set_phase (ObEmbeddingTaskPhase new_phase);
239- int complete_task (ObEmbeddingTaskPhase new_phase, int result_code, bool finished = true );
283+ int complete_task (int result_code, bool finished = true );
240284 int start_async_work ();
241285 int check_async_progress ();
242286
@@ -256,14 +300,24 @@ class ObEmbeddingTask
256300 int parse_embedding_response (const char *response_data, size_t response_size);
257301
258302 // Helper methods for retry logic
259- bool should_retry_http_request (int64_t http_error_code) const ;
303+ bool should_retry_http_request (int64_t http_error_code, const ObString &http_error_msg ) const ;
260304 bool is_batch_size_related_error (int64_t http_error_code) const ;
261305 int64_t calculate_retry_interval () const ;
262306 int adjust_batch_size_for_retry ();
307+ int adjust_input_token_limit_for_retry ();
263308 void reset_retry_state ();
264309 int map_http_error_to_internal_error (int64_t http_error_code) const ;
265310 void try_increase_batch_size ();
266311 int init_curl_handler (const ObString &model_url, const ObString &user_key, const int64_t http_timeout_us);
312+ bool is_request_too_large (int64_t http_error_code, const ObString &content) const ;
313+ int truncate_text_by_token_count (ObString &text, const ObCollationType cs_type, const int64_t max_token_count) const ;
314+ bool can_retry_request () const { return http_retry_count_ < http_max_retry_count_; }
315+ bool is_ready_to_handle () const ; // Check if current time exceeds next_handle_task_time_us_
316+ int finish_task ();
317+ int try_rescheule_task ();
318+ int calc_max_wait_completion_time_us (int64_t http_timeout_us, int64_t http_max_retry_count,
319+ int64_t batch_size, int64_t input_chunks_count,
320+ int64_t &max_wait_completion_time_us) const ;
267321
268322 struct HttpResponseData {
269323 HttpResponseData (ObIAllocator &allocator) : data(nullptr ), size(0 ), allocator(allocator) {}
@@ -365,20 +419,31 @@ class ObEmbeddingTask
365419 int64_t wait_for_completion_timeout_us_; // For controlling the maximum timeout of waiting for completion
366420
367421 bool need_retry_flag_;
422+ bool always_retry_; // If true, task can retry even after exceeding max retry count, but must wait at least 3 minutes
423+ int64_t last_exceeded_retry_time_us_; // Time when retry count was exceeded
368424
369425 // Batch size adjustment for retry
370426 uint32_t original_batch_size_;
371427 bool batch_size_adjusted_;
372428 uint32_t current_batch_size_;
373429 uint32_t successful_requests_count_;
374430
431+ // Input token limit adjustment for retry (when request too large)
432+ int64_t current_input_token_limit_;
433+
375434 ObThreadCond task_cond_;
376435 bool callback_done_;
377436
378437 // TODO(fanfangyao.ffy): use taskhandle to manage task reference count
379438 // ref_cnt_ is only used to track the reference count of the post create embedding task
380439 int64_t ref_cnt_;
381-
440+ ObCollationType col_type_;
441+ bool need_cancel_;
442+ common::ObCurTraceId::TraceId trace_id_;
443+ int64_t source_task_id_;
444+ ObEmbeddingTasSourceType source_task_type_;
445+ int64_t next_handle_task_time_us_;
446+ ObEmbeddingTaskHandler *thread_pool_;
382447 private:
383448 DISALLOW_COPY_AND_ASSIGN (ObEmbeddingTask);
384449};
0 commit comments