From f92d4ba47e32b053db17885f00cadc82f1cc79da Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 1 Jul 2024 17:15:18 +0900 Subject: [PATCH 01/53] Remove previous parallel routines. Add single thread sort routines except index creation. --- src/query/list_file.c | 2 +- src/query/query_executor.c | 6 +- src/storage/btree_load.c | 2 +- src/storage/external_sort.c | 984 ++++++++++-------------------------- src/storage/external_sort.h | 2 +- 5 files changed, 262 insertions(+), 734 deletions(-) diff --git a/src/query/list_file.c b/src/query/list_file.c index 358890be6f2..9aa0ee19389 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -4030,7 +4030,7 @@ qfile_sort_list_with_func (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p, S sort_result = sort_listfile (thread_p, NULL_VOLID, estimated_pages, get_func, &info, put_func, &info, cmp_func, &info.key_info, - dup_option, limit, srlist_id->tfile_vfid->tde_encrypted); + dup_option, limit, srlist_id->tfile_vfid->tde_encrypted, true); if (sort_result < 0) { diff --git a/src/query/query_executor.c b/src/query/query_executor.c index 4613a849415..7f5ff57924a 100644 --- a/src/query/query_executor.c +++ b/src/query/query_executor.c @@ -4648,7 +4648,7 @@ qexec_groupby (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_stat /* sort and aggregate partial results */ if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_hash_gby_get_next, &gbstate, &qexec_hash_gby_put_next, &gbstate, cmp_fn, &gbstate.agg_hash_context->sort_key, SORT_DUP, - NO_SORT_LIMIT, gbstate.output_file->tfile_vfid->tde_encrypted) != NO_ERROR) + NO_SORT_LIMIT, gbstate.output_file->tfile_vfid->tde_encrypted, true) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } @@ -4729,7 +4729,7 @@ qexec_groupby (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_stat if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_gby_get_next, &gbstate, &qexec_gby_put_next, &gbstate, gbstate.cmp_fn, &gbstate.key_info, SORT_DUP, NO_SORT_LIMIT, - gbstate.output_file->tfile_vfid->tde_encrypted) != NO_ERROR) + gbstate.output_file->tfile_vfid->tde_encrypted, true) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } @@ -19645,7 +19645,7 @@ qexec_execute_analytic (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_analytic_get_next, &analytic_state, &qexec_analytic_put_next, &analytic_state, analytic_state.cmp_fn, &analytic_state.key_info, - SORT_DUP, NO_SORT_LIMIT, analytic_state.output_file->tfile_vfid->tde_encrypted) != NO_ERROR) + SORT_DUP, NO_SORT_LIMIT, analytic_state.output_file->tfile_vfid->tde_encrypted, true) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } diff --git a/src/storage/btree_load.c b/src/storage/btree_load.c index 69ae5ae8643..a017652ce05 100644 --- a/src/storage/btree_load.c +++ b/src/storage/btree_load.c @@ -3219,7 +3219,7 @@ btree_index_sort (THREAD_ENTRY * thread_p, SORT_ARGS * sort_args, SORT_PUT_FUNC return sort_listfile (thread_p, sort_args->hfids[0].vfid.volid, 0 /* TODO - support parallelism */ , &btree_sort_get_next, sort_args, out_func, out_args, compare_driver, sort_args, SORT_DUP, - NO_SORT_LIMIT, includes_tde_class); + NO_SORT_LIMIT, includes_tde_class, false); } /* diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index cd772652068..ee14b61dd5c 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -91,6 +91,8 @@ } \ } while (0) +#define IS_PARALLEL_EXECUTION(t) ((t)->px_max_index > 1) + typedef struct file_contents FILE_CONTENTS; struct file_contents { /* node of the file_contents linked list */ @@ -117,30 +119,6 @@ struct vol_list VOL_INFO *vol_info; /* array of volume information */ }; -/* Parallel eXecution and communition node */ -typedef struct px_tree_node PX_TREE_NODE; -struct px_tree_node -{ - int px_id; /* node ID */ -#if defined(SERVER_MODE) - int px_status; /* node status; access through px_mtx */ -#endif /* SERVER_MODE */ - - int px_height; /* tournament tree: node level */ - int px_myself; /* tournament tree: node ID */ - - int px_tran_index; - - void *px_arg; /* operation info */ - - char **px_buff; - char **px_vector; - long px_vector_size; - - char **px_result; /* output */ - long px_result_size; /* output */ -}; - typedef struct sort_param SORT_PARAM; struct sort_param { @@ -166,6 +144,10 @@ struct sort_param void *cmp_arg; SORT_DUP_OPTION option; + /* input function to apply on temporary records */ + SORT_GET_FUNC *get_fn; + void *get_arg; + /* output function to apply on temporary records */ SORT_PUT_FUNC *put_fn; void *put_arg; @@ -176,13 +158,18 @@ struct sort_param /* Details about the "limit" clause */ int limit; + /* total number of recordes */ + unsigned int total_numrecs; + /* support parallelism */ #if defined(SERVER_MODE) pthread_mutex_t px_mtx; /* px_node status mutex */ #endif - int px_height_max; /* px_node tournament tree max level */ - int px_array_size; /* px_node array size */ - PX_TREE_NODE *px_array; /* px_node array */ + int px_max_index; + int px_index; + bool px_status[32]; + int px_result_file_idx; + int px_tran_index; }; typedef struct sort_rec_list SORT_REC_LIST; @@ -244,17 +231,16 @@ typedef void MERGE_RUN_FN (char **, char **, SORT_STACK *, SORT_CMP_FUNC *, void #if !defined(NDEBUG) static int sort_validate (char **vector, long size, SORT_CMP_FUNC * compare, void *comp_arg); #endif -static PX_TREE_NODE *px_sort_assign (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, int px_id, char **px_buff, - char **px_vector, long px_vector_size, int px_height, int px_myself); -static int px_sort_myself (THREAD_ENTRY * thread_p, PX_TREE_NODE * px_node); + #if defined(SERVER_MODE) -static int px_sort_communicate (PX_TREE_NODE * px_node); +static void sort_listfile_execute (cubthread::entry & thread_ref, SORT_PARAM * sort_param); #endif - +static int sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FUNC * get_next, void *arguments, unsigned int *total_numrecs); static int sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); +static int sort_put_result_from_tmpfile (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_get_avg_numpages_of_nonempty_tmpfile (SORT_PARAM * sort_param); static void sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bool force_alloc, @@ -1345,19 +1331,13 @@ sort_run_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, char **base, lo int sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GET_FUNC * get_fn, void *get_arg, SORT_PUT_FUNC * put_fn, void *put_arg, SORT_CMP_FUNC * cmp_fn, void *cmp_arg, SORT_DUP_OPTION option, - int limit, bool includes_tde_class) + int limit, bool includes_tde_class, bool is_parallel) { + int error = NO_ERROR; SORT_PARAM *sort_param = NULL; - bool prm_enable_sort_parallel = false; /* TODO - PRM_SORT_PARALLEL_SORT */ INT32 input_pages; int i; - int file_pg_cnt_est; - unsigned int total_numrecs = 0; -#if defined(SERVER_MODE) - int num_cpus; - int rv; -#endif /* SERVER_MODE */ thread_set_sort_stats_active (thread_p, true); @@ -1373,23 +1353,13 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE return error; } -#if defined(SERVER_MODE) - rv = pthread_mutex_init (&(sort_param->px_mtx), NULL); - if (rv != 0) - { - error = ER_CSS_PTHREAD_MUTEX_INIT; - er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 0); - - free_and_init (sort_param); - - return error; - } -#endif /* SERVER_MODE */ - sort_param->cmp_fn = cmp_fn; sort_param->cmp_arg = cmp_arg; sort_param->option = option; + sort_param->get_fn = get_fn; + sort_param->get_arg = get_arg; + sort_param->put_fn = put_fn; sort_param->put_arg = put_arg; @@ -1403,8 +1373,6 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_param->file_contents[i].num_pages = NULL; } sort_param->internal_memory = NULL; - sort_param->px_height_max = sort_param->px_array_size = 0; - sort_param->px_array = NULL; /* initialize temp. overflow file. Real value will be assigned in sort_inphase_sort function, if long size sorting * records are encountered. */ @@ -1476,36 +1444,114 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_param->tde_encrypted = includes_tde_class; - sort_param->px_height_max = 0; /* init */ - sort_param->px_array_size = 1; /* init */ - - /* TODO - currently, disable parallelism */ - prm_enable_sort_parallel = false; +cleanup: + if (error != NO_ERROR) + { +#if defined(ENABLE_SYSTEMTAP) + CUBRID_SORT_END (sort_param->total_numrecs, error); +#endif /* ENABLE_SYSTEMTAP */ - tde_er_log ("sort_listfile(): tde_encrypted = %d\n", sort_param->tde_encrypted); + sort_return_used_resources (thread_p, sort_param); + sort_param = NULL; + thread_set_sort_stats_active (thread_p, false); + return error; + } #if defined(SERVER_MODE) - if (prm_enable_sort_parallel == true) - { - /* TODO - calc n, 2^^n get the number of CPUs TODO - fileio_os_sysconf really get #cores instead of #CPUs - - * NEED MORE CONSIDERATION */ - num_cpus = fileio_os_sysconf (); + SORT_INFO *sort_info_p = (SORT_INFO *) put_arg; + QFILE_LIST_ID *outlist_id, *orglist_id; - sort_param->px_height_max = (int) sqrt ((double) num_cpus); /* n */ - sort_param->px_array_size = num_cpus; /* 2^^n */ + /* TODO : add to routines to copy sort_param */ - assert_release (sort_param->px_array_size == pow ((double) 2, (double) sort_param->px_height_max)); + /* temporarily, no parallelism when creating an index. */ + if (!is_parallel) + { + sort_param->px_max_index = 1; + error = sort_listfile_internal (thread_p, sort_param); } -#endif /* SERVER_MODE */ - - sort_param->px_array = (PX_TREE_NODE *) malloc (sort_param->px_array_size * sizeof (PX_TREE_NODE)); - if (sort_param->px_array == NULL) + else { - error = ER_OUT_OF_VIRTUAL_MEMORY; - er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 1, (sort_param->px_array_size * sizeof (PX_TREE_NODE))); - goto cleanup; + /* init px variable */ + sort_param->px_status[0] = 0; + sort_param->px_max_index = 2; + sort_param->px_index = 1; + sort_param->px_result_file_idx = 0; + sort_param->px_tran_index = LOG_FIND_THREAD_TRAN_INDEX (thread_p); + + cubthread::entry_callable_task * task = + new cubthread::entry_callable_task (std::bind (sort_listfile_execute, std::placeholders::_1, sort_param)); + css_push_external_task (css_get_current_conn_entry (), task); + + /* wait for threads */ + /* TO_DO : no busy wait. need to block and wake up */ + do + { + thread_sleep (10); /* 10 msec */ + if (sort_param->px_status[0] == 1) + { + break; + } + } + while (1); + + /* TO_DO : need routines for merging parallel processed temp files into a local sort_param */ + + /* Merge the parallel processed results. */ + sort_param->px_max_index = 1; + if (sort_param->option == SORT_ELIM_DUP) + { + error = sort_exphase_merge_elim_dup (thread_p, sort_param); + } + else + { + /* SORT_DUP */ + error = sort_exphase_merge (thread_p, sort_param); + } } +#else + /* no parallel */ + sort_param->px_max_index = 1; + error = sort_listfile_internal (thread_p, sort_param); +#endif + +#if defined(ENABLE_SYSTEMTAP) + CUBRID_SORT_END (sort_param->total_numrecs, error); +#endif /* ENABLE_SYSTEMTAP */ + + sort_return_used_resources (thread_p, sort_param); + sort_param = NULL; + thread_set_sort_stats_active (thread_p, false); + + return error; +} + +#if defined(SERVER_MODE) +// *INDENT-OFF* +static void +sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) +{ + thread_ref.tran_index = sort_param->px_tran_index; + pthread_mutex_unlock (&thread_ref.tran_index_lock); + + sort_listfile_internal (&thread_ref, sort_param); + + sort_param->px_status[0] = 1; +} +// *INDENT-ON* +#endif + +/* + * sort_listfile_internal () - Perform sorting + * return: + */ +int +sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) +{ + int error = NO_ERROR; + int file_pg_cnt_est; + int i; + /* * Don't allocate any temp files yet, since we may not need them. * We'll allocate them on the fly as the need arises. @@ -1514,14 +1560,15 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE * space that is going to be needed. */ - error = sort_inphase_sort (thread_p, sort_param, get_fn, get_arg, &total_numrecs); + error = sort_inphase_sort (thread_p, sort_param, sort_param->get_fn, sort_param->get_arg, &sort_param->total_numrecs); if (error != NO_ERROR) { - goto cleanup; + return error; } - if (sort_param->tot_runs > 1) + if (sort_param->tot_runs > 1 || IS_PARALLEL_EXECUTION (sort_param)) { + assert (sort_param->tot_runs > 0); /* Create output temporary files make file and temporary volume page count estimates */ file_pg_cnt_est = sort_get_avg_numpages_of_nonempty_tmpfile (sort_param); file_pg_cnt_est = MAX (1, file_pg_cnt_est); @@ -1532,7 +1579,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted); if (error != NO_ERROR) { - goto cleanup; + return error; } } @@ -1547,16 +1594,6 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE } } /* if (sort_param->tot_runs > 1) */ -cleanup: - -#if defined(ENABLE_SYSTEMTAP) - CUBRID_SORT_END (total_numrecs, error); -#endif /* ENABLE_SYSTEMTAP */ - - sort_return_used_resources (thread_p, sort_param); - sort_param = NULL; - thread_set_sort_stats_active (thread_p, false); - return error; } @@ -1599,549 +1636,6 @@ sort_validate (char **vector, long size, SORT_CMP_FUNC * compare, void *comp_arg } #endif -/* - * px_sort_assign() - - * return: - * thread_p(in): - * sort_param(in): sort parameters - * px_id(in): - * px_buff(in): - * px_vector(in): - * px_vector_size(in): - * px_height(in): - * px_myself(in): - * - * NOTE: support parallelism - */ -static PX_TREE_NODE * -px_sort_assign (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, int px_id, char **px_buff, char **px_vector, - long px_vector_size, int px_height, int px_myself) -{ - PX_TREE_NODE *px_node; -#if defined(SERVER_MODE) - int rv = NO_ERROR; -#endif - - assert (sort_param != NULL); - - if (px_height < 0 || px_height > sort_param->px_height_max) - { - assert_release (false); - return NULL; - } - - if (px_id < 0 || px_id >= sort_param->px_array_size) - { - assert_release (false); - return NULL; - } - - if (px_myself < 0 || px_myself > px_id) - { - assert_release (false); - return NULL; - } - - px_node = &(sort_param->px_array[px_id]); - -#if defined(SERVER_MODE) - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - -#if !defined(NDEBUG) - if (px_node->px_status != 0) - { - assert (false); - pthread_mutex_unlock (&(sort_param->px_mtx)); - return NULL; - } -#endif - - px_node->px_status = 0; - - pthread_mutex_unlock (&(sort_param->px_mtx)); -#else /* SERVER_MODE */ - assert (sort_param->px_height_max == 0); - assert (sort_param->px_array_size == 1); - - assert (px_id == 0); - assert (px_height == 0); - assert (px_myself == 0); -#endif /* SERVER_MODE */ - - /* set node info */ - - px_node->px_id = px_id; - - px_node->px_height = px_height; - px_node->px_myself = px_myself; - - px_node->px_tran_index = LOG_FIND_THREAD_TRAN_INDEX (thread_p); - - /* set operation info */ - - px_node->px_arg = (void *) sort_param; - - px_node->px_buff = px_buff; - px_node->px_vector = px_vector; - px_node->px_vector_size = px_vector_size; - - px_node->px_result = px_node->px_vector; /* init */ - px_node->px_result_size = px_node->px_vector_size; /* init */ - - return px_node; -} - -#if defined(SERVER_MODE) -// *INDENT-OFF* -static void -px_sort_myself_execute (cubthread::entry &thread_ref, PX_TREE_NODE * px_node) -{ - (void) px_sort_myself (&thread_ref, px_node); -} - -/* - * px_sort_communicate() - - * return: - * thread_p(in): - * px_node(in): - * - * NOTE: support parallelism - */ -static int -px_sort_communicate (PX_TREE_NODE * px_node) -{ - SORT_PARAM *sort_param; - - assert_release (px_node != NULL); - assert_release (px_node->px_arg != NULL); - - sort_param = (SORT_PARAM *) (px_node->px_arg); - assert_release (px_node->px_height <= sort_param->px_height_max); - assert_release (px_node->px_id < sort_param->px_array_size); - assert_release (px_node->px_vector_size > 1); - - cubthread::entry_callable_task *task = - new cubthread::entry_callable_task (std::bind (px_sort_myself_execute, std::placeholders::_1, px_node)); - css_push_external_task (css_get_current_conn_entry (), task); - - return NO_ERROR; -} -// *INDENT-ON* -#endif /* SERVER_MODE */ - -/* - * px_sort_myself() - - * return: - * thread_p(in): - * px_node(in): - * - * NOTE: support parallelism - * - * Partitioned merge logic - * - * The working core: each internal node recurses on this function - * both for its left side and its right side, as nodes one closer to - * the leaf level. It then merges the results into the vector - * - * Leaf level nodes just sort the vector. - */ -static int -px_sort_myself (THREAD_ENTRY * thread_p, PX_TREE_NODE * px_node) -{ -#define SORT_PARTITION_RUN_SIZE_MIN (ONE_M) - - int ret = NO_ERROR; - bool old_check_interrupt; - -#if defined(SERVER_MODE) - int parent; - int child_right = 0; - int child_height; - - int cmp; - - int rv = NO_ERROR; -#endif /* SERVER_MODE */ - - SORT_PARAM *sort_param; - - char **buff; - char **vector; - long vector_size; - - char **result = NULL; - long result_size = -1; - - assert_release (px_node != NULL); - assert_release (px_node->px_id >= 0); - assert_release (px_node->px_arg != NULL); - - if (thread_p == NULL) - { - thread_p = thread_get_thread_entry_info (); - } - - sort_param = (SORT_PARAM *) (px_node->px_arg); - -#if defined(SERVER_MODE) - if (px_node->px_id > 0) - { - /* is new childs */ - thread_p->tran_index = px_node->px_tran_index; - pthread_mutex_unlock (&thread_p->tran_index_lock); - } - -#if !defined(NDEBUG) - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - assert (px_node->px_status == 0); - - pthread_mutex_unlock (&(sort_param->px_mtx)); -#endif -#endif /* SERVER_MODE */ - - old_check_interrupt = logtb_set_check_interrupt (thread_p, false); - - buff = px_node->px_buff; - vector = px_node->px_vector; - vector_size = px_node->px_vector_size; - - assert_release (px_node->px_result == vector); - assert_release (px_node->px_result_size == vector_size); - - result = px_node->px_result; - result_size = px_node->px_result_size; - - assert_release (vector_size > 0); - -#if defined(SERVER_MODE) - parent = px_node->px_id & ~(1 << px_node->px_height); - child_height = px_node->px_height - 1; - if (child_height >= 0) - { - child_right = px_node->px_myself | (1 << child_height); - assert_release (child_right > 0); - } - - if (vector_size <= 1) - { - assert_release (px_node->px_result == vector); - assert_release (px_node->px_result_size == vector_size); - - result = px_node->px_result = vector; - result_size = px_node->px_result_size = vector_size; - - /* mark as finished */ - - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - assert_release (px_node->px_status == 0); - px_node->px_status = 1; /* done */ - - pthread_mutex_unlock (&(sort_param->px_mtx)); - - goto exit_on_end; - } - - if (px_node->px_height > 0 && vector_size > SORT_PARTITION_RUN_SIZE_MIN) - { - long left_vector_size, right_vector_size; - char **left_vector, **right_vector; - PX_TREE_NODE *left_px_node, *right_px_node; - int i, j, k; /* Used in the merge logic */ - - assert_release (child_right > 0); - - left_vector_size = vector_size / 2; - right_vector_size = vector_size - left_vector_size; - - assert_release (vector_size == left_vector_size + right_vector_size); - - /* do new child first */ - right_vector = vector + left_vector_size; - right_px_node = px_sort_assign (thread_p, sort_param, px_node->px_id + child_right, buff + left_vector_size, - right_vector, right_vector_size, child_height, 0 /* px_myself: set as root */ ); - if (right_px_node == NULL) - { - goto exit_on_error; - } - - if (right_vector_size > 1) - { - /* launch new worker */ - if (px_sort_communicate (right_px_node) != NO_ERROR) - { - goto exit_on_error; - } - } - else - { - /* mark as finished */ - - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - assert_release (right_px_node->px_status == 0); - right_px_node->px_status = 1; /* done */ - - pthread_mutex_unlock (&(sort_param->px_mtx)); - } - - left_vector = vector; - left_px_node = - px_sort_assign (thread_p, sort_param, px_node->px_id, buff, left_vector, left_vector_size, child_height, - px_node->px_myself); - if (left_px_node == NULL) - { - goto exit_on_error; - } - - assert_release (px_node == left_px_node); -#if !defined(NDEBUG) - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - assert (px_node->px_status == 0); - - pthread_mutex_unlock (&(sort_param->px_mtx)); -#endif - - if (left_vector_size > 1) - { - if (px_sort_myself (thread_p, left_px_node) != NO_ERROR) - { - goto exit_on_error; - } - } - - /* wait for right-child finished */ - do - { - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - if (right_px_node->px_status != 0) - { - assert (right_px_node->px_status == 1); - pthread_mutex_unlock (&(sort_param->px_mtx)); - break; - } - - pthread_mutex_unlock (&(sort_param->px_mtx)); - thread_sleep (10); /* 10 msec */ - } - while (1); - - assert_release (px_node == left_px_node); -#if !defined(NDEBUG) - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - assert (right_px_node->px_status == 1); - assert (px_node->px_status == 0); - - pthread_mutex_unlock (&(sort_param->px_mtx)); -#endif - - right_vector = right_px_node->px_result; - right_vector_size = right_px_node->px_result_size; - if (right_vector == NULL || right_vector_size < 0) - { - goto exit_on_error; - } - - left_vector = left_px_node->px_result; - left_vector_size = left_px_node->px_result_size; - if (left_vector == NULL || left_vector_size < 0) - { - goto exit_on_error; - } - - assert_release (vector_size >= left_vector_size + right_vector_size); - - result_size = px_node->px_result_size = left_vector_size + right_vector_size; - if (left_vector < vector) - { - assert_release (left_vector + left_vector_size <= vector); - result = px_node->px_result = vector; - } - else - { - result = px_node->px_result = buff; - } - - /* Merge the two sub-results back into upper result */ - - i = j = k = 0; - - /* STEP 2: check CON conditions if (left_max < right_min) do FORWARD-CON. we use '<' instead of '<=' */ - cmp = (*(sort_param->cmp_fn)) (&(left_vector[left_vector_size - 1]), &(right_vector[0]), sort_param->cmp_arg); - - if (cmp == DB_LT || cmp == DB_EQ || cmp == DB_GT) - { - ; /* ok */ - } - else - { - assert_release (cmp == DB_LT || cmp == DB_EQ || cmp == DB_GT); - goto exit_on_error; - } - - if (cmp == DB_LT) - { - while (i < left_vector_size) - { - result[k++] = left_vector[i++]; - } - while (j < right_vector_size) - { - result[k++] = right_vector[j++]; - } - } - else - { - /* STEP 3: check CON conditions if (right_max < left_min) do BACKWARD-CON. we use '<' instead of '<=' */ - cmp = - (*(sort_param->cmp_fn)) (&(right_vector[right_vector_size - 1]), &(left_vector[0]), sort_param->cmp_arg); - - if (cmp == DB_LT || cmp == DB_EQ || cmp == DB_GT) - { - ; /* ok */ - } - else - { - assert_release (cmp == DB_LT || cmp == DB_EQ || cmp == DB_GT); - goto exit_on_error; - } - - if (cmp == DB_LT) - { - while (j < right_vector_size) - { - result[k++] = right_vector[j++]; - } - while (i < left_vector_size) - { - result[k++] = left_vector[i++]; - } - } - else - { - /* STEP 4: do the actual merge */ - while (i < left_vector_size && j < right_vector_size) - { - cmp = (*(sort_param->cmp_fn)) (&(left_vector[i]), &(right_vector[j]), sort_param->cmp_arg); - - if (cmp == DB_LT || cmp == DB_EQ || cmp == DB_GT) - { - ; /* ok */ - } - else - { - assert_release (cmp == DB_LT || cmp == DB_EQ || cmp == DB_GT); - goto exit_on_error; - } - - if (cmp == DB_EQ) - { - if (sort_param->option == SORT_DUP) /* allow duplicate */ - { - sort_append (&(left_vector[i]), &(right_vector[j])); - } - - result[k++] = right_vector[j++]; /* temp */ - i++; /* skip left-side dup */ - } - else if (cmp == DB_GT) - { - result[k++] = right_vector[j++]; - } - else - { - assert_release (cmp == DB_LT); - result[k++] = left_vector[i++]; - } - } - while (i < left_vector_size) - { - result[k++] = left_vector[i++]; - } - while (j < right_vector_size) - { - result[k++] = right_vector[j++]; - } - } /* else */ - } /* else */ - - assert_release (result_size >= k); - - result_size = px_node->px_result_size = k; - -#if !defined(NDEBUG) - if (sort_validate (result, result_size, sort_param->cmp_fn, sort_param->cmp_arg) != NO_ERROR) - { - goto exit_on_error; - } -#endif - } - else - { - result = px_node->px_result = sort_run_sort (thread_p, sort_param, vector, vector_size, 0 /* dummy */ , - buff, &(px_node->px_result_size)); - result_size = px_node->px_result_size; - } - -#else /* SERVER_MODE */ - - result = px_node->px_result = sort_run_sort (thread_p, sort_param, vector, vector_size, 0 /* dummy */ , - buff, &(px_node->px_result_size)); - result_size = px_node->px_result_size; - -#endif /* SERVER_MODE */ - - if (result == NULL || result_size < 0) - { - assert_release (false); - goto exit_on_error; - } - -exit_on_end: - - assert_release (result == px_node->px_result); - assert_release (result_size == px_node->px_result_size); - -#if defined(SERVER_MODE) - if (parent != px_node->px_id) - { - /* mark as finished */ - - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - assert_release (px_node->px_status == 0); - px_node->px_status = 1; /* done */ - - pthread_mutex_unlock (&(sort_param->px_mtx)); - } -#endif /* SERVER_MODE */ - - (void) logtb_set_check_interrupt (thread_p, old_check_interrupt); - - return ret; - -exit_on_error: - - result = px_node->px_result = NULL; - result_size = px_node->px_result_size = -1; - - ret = (ret == NO_ERROR && (ret = er_errid ()) == NO_ERROR) ? ER_FAILED : ret; - - goto exit_on_end; -} - /* * sort_inphase_sort () - Internal sorting phase * return: @@ -2178,16 +1672,12 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU int i; int error = NO_ERROR; - PX_TREE_NODE *px_node; #if defined (SERVER_MODE) int rv = NO_ERROR; #endif /* SERVER_MODE */ assert (sort_param->half_files <= SORT_MAX_HALF_FILES); - assert (sort_param->px_height_max >= 0); - assert (sort_param->px_array_size >= 1); - /* Initialize the current pages of all temp files to 0 */ for (i = 0; i < sort_param->half_files; i++) { @@ -2260,46 +1750,9 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU index_area++; - if (sort_numrecs == 0) - { - assert (sort_param->px_height_max >= 0); - assert (sort_param->px_array_size >= 1); -#if defined(SERVER_MODE) - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - for (i = 0; i < sort_param->px_array_size; i++) - { - sort_param->px_array[i].px_status = 0; /* init */ - } - - pthread_mutex_unlock (&(sort_param->px_mtx)); -#endif /* SERVER_MODE */ - - px_node = px_sort_assign (thread_p, sort_param, 0, index_buff, index_area, numrecs, - sort_param->px_height_max, 0 /* px_myself: set as root */ ); - if (px_node == NULL) - { - error = ER_FAILED; - goto exit_on_error; - } - - error = px_sort_myself (thread_p, px_node); - if (error != NO_ERROR) - { - goto exit_on_error; - } - - index_area = px_node->px_result; - numrecs = px_node->px_result_size; - *total_numrecs += numrecs; - } - else - { - index_area = - sort_run_sort (thread_p, sort_param, index_area, numrecs, sort_numrecs, index_buff, &numrecs); - *total_numrecs += numrecs; - } + index_area = + sort_run_sort (thread_p, sort_param, index_area, numrecs, sort_numrecs, index_buff, &numrecs); + *total_numrecs += numrecs; if (index_area == NULL || numrecs < 0) { @@ -2515,45 +1968,8 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU index_area++; - if (sort_numrecs == 0) - { - assert (sort_param->px_height_max >= 0); - assert (sort_param->px_array_size >= 1); -#if defined(SERVER_MODE) - rv = pthread_mutex_lock (&(sort_param->px_mtx)); - assert (rv == NO_ERROR); - - for (i = 0; i < sort_param->px_array_size; i++) - { - sort_param->px_array[i].px_status = 0; /* init */ - } - - pthread_mutex_unlock (&(sort_param->px_mtx)); -#endif /* SERVER_MODE */ - - px_node = px_sort_assign (thread_p, sort_param, 0, index_buff, index_area, numrecs, sort_param->px_height_max, - 0 /* px_myself: set as root */ ); - if (px_node == NULL) - { - error = ER_FAILED; - goto exit_on_error; - } - - if (px_sort_myself (thread_p, px_node) != NO_ERROR) - { - error = ER_FAILED; - goto exit_on_error; - } - - index_area = px_node->px_result; - numrecs = px_node->px_result_size; - *total_numrecs += numrecs; - } - else - { - index_area = sort_run_sort (thread_p, sort_param, index_area, numrecs, sort_numrecs, index_buff, &numrecs); - *total_numrecs += numrecs; - } + index_area = sort_run_sort (thread_p, sort_param, index_area, numrecs, sort_numrecs, index_buff, &numrecs); + *total_numrecs += numrecs; if (index_area == NULL || numrecs < 0) { @@ -2561,7 +1977,7 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU goto exit_on_error; } - if (sort_param->tot_runs > 0) + if (sort_param->tot_runs > 0 || IS_PARALLEL_EXECUTION (sort_param)) { /* There has been other runs produced already */ @@ -2595,7 +2011,7 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU } } } - else if (sort_param->tot_runs == 1) + else if (sort_param->tot_runs == 1 && !IS_PARALLEL_EXECUTION (sort_param)) { if (once_flushed) { @@ -2942,6 +2358,12 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* OUTER LOOP */ + /* for one temporary file, put result from the temp file instead of merging it. */ + if (!IS_PARALLEL_EXECUTION (sort_param) && sort_get_numpages_of_active_infiles (sort_param) == 1) + { + sort_put_result_from_tmpfile (thread_p, sort_param); + } + /* While there are more than one input files with different runs to merge */ while ((act_infiles = sort_get_numpages_of_active_infiles (sort_param)) > 1) { @@ -3253,6 +2675,12 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* Initialize the size of next run to zero */ out_runsize = 0; + /* In parallel sort, put_fn will be performed by the parent thread. save last file index. */ + if (very_last_run && IS_PARALLEL_EXECUTION (sort_param)) + { + sort_param->px_result_file_idx = cur_outfile; + } + for (;;) { /* OUTPUT A RECORD */ @@ -3265,7 +2693,7 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) { /* we found first unique sort_key record */ - if (very_last_run) + if (very_last_run && !IS_PARALLEL_EXECUTION (sort_param)) { /* OUTPUT THE RECORD */ /* Obtain the output record for this temporary record */ @@ -3551,7 +2979,7 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) } } - if (!very_last_run) + if (!(very_last_run && !IS_PARALLEL_EXECUTION (sort_param))) { /* Flush whatever is left on the output section */ out_act_bufno++; /* Since 0 refers to the first active buffer */ @@ -3616,6 +3044,100 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) return (error == SORT_PUT_STOP) ? NO_ERROR : error; } +/* + * sort_put_result_from_tmpfile () - put result from last temp file + * return: + * sort_param(in): sort parameters + * + */ +static int +sort_put_result_from_tmpfile (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) +{ + int tot_pages; + int current_pages = 0, read_pages = 0, cur_read_pages = 0; + int slot_num = 0; + char *cur_pgptr; + int result_file_idx = sort_param->px_result_file_idx; + RECDES record; + RECDES long_record; + int error; + SORT_REC *sort_rec; + int tot_rows = 0; + + tot_pages = sort_param->file_contents[result_file_idx].num_pages[0]; + while (tot_pages > 0) + { + read_pages = (tot_pages > sort_param->tot_buffers) ? sort_param->tot_buffers : tot_pages; + + error = + sort_read_area (thread_p, &sort_param->temp[result_file_idx], current_pages, read_pages, + sort_param->internal_memory); + if (error != NO_ERROR) + { + goto bailout; + } + + cur_pgptr = sort_param->internal_memory; + cur_read_pages = read_pages; + while (cur_read_pages > 0) + { + /* read record from sort temp file */ + slot_num = sort_spage_get_numrecs (cur_pgptr); + tot_rows += slot_num; + for (int i = 0; i < slot_num; i++) + { + if (sort_spage_get_record (cur_pgptr, i, &record, PEEK) != S_SUCCESS) + { + er_set (ER_FATAL_ERROR_SEVERITY, ARG_FILE_LINE, ER_SORT_TEMP_PAGE_CORRUPTED, 0); + error = ER_SORT_TEMP_PAGE_CORRUPTED; + goto bailout; + } + + /* If this is a long record retrieve it */ + if (record.type == REC_BIGONE) + { + if (sort_retrieve_longrec (thread_p, &record, &long_record) == NULL) + { + ASSERT_ERROR (); + error = er_errid (); + goto bailout; + } + } + /* write data by put_fn */ + if (record.type == REC_BIGONE) + { + error = (*sort_param->put_fn) (thread_p, &long_record, sort_param->put_arg); + if (error != NO_ERROR) + { + goto bailout; + } + } + else + { + sort_rec = (SORT_REC *) (record.data); + /* cut-off link used in Internal Sort */ + sort_rec->next = NULL; + error = (*sort_param->put_fn) (thread_p, &record, sort_param->put_arg); + if (error != NO_ERROR) + { + goto bailout; + } + } + } + + /* Switch to the next page */ + cur_pgptr += DB_PAGESIZE; + + cur_read_pages--; + current_pages++; + } + tot_pages -= read_pages; + } + +bailout: + return error; +} + /* * sort_exphase_merge () - Merge phase * return: @@ -3722,6 +3244,12 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) out_half = 0; } + /* for one temporary file, put result from the temp file instead of merging it. */ + if (!IS_PARALLEL_EXECUTION (sort_param) && sort_get_numpages_of_active_infiles (sort_param) == 1) + { + sort_put_result_from_tmpfile (thread_p, sort_param); + } + /* OUTER LOOP */ /* While there are more than one input files with different runs to merge */ @@ -4023,6 +3551,12 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* Initialize the size of next run to zero */ out_runsize = 0; + /* In parallel sort, put_fn will be performed by the parent thread. save last file index. */ + if (very_last_run && IS_PARALLEL_EXECUTION (sort_param)) + { + sort_param->px_result_file_idx = cur_outfile; + } + for (;;) { /* OUTPUT A RECORD */ @@ -4030,7 +3564,7 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* FIND MINIMUM RECORD IN THE INPUT AREA */ min = min_p->rec_pos; - if (very_last_run) + if (very_last_run && !IS_PARALLEL_EXECUTION (sort_param)) { /* OUTPUT THE RECORD */ /* Obtain the output record for this temporary record */ @@ -4299,7 +3833,7 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) } } - if (!very_last_run) + if (!(very_last_run && !IS_PARALLEL_EXECUTION (sort_param))) { /* Flush whatever is left on the output section */ @@ -4444,12 +3978,6 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) } } - if (sort_param->px_array) - { - free_and_init (sort_param->px_array); - } - sort_param->px_height_max = sort_param->px_array_size = 0; - #if defined(SERVER_MODE) rv = pthread_mutex_destroy (&(sort_param->px_mtx)); if (rv != 0) diff --git a/src/storage/external_sort.h b/src/storage/external_sort.h index 0c61dbc2f37..6499fc9a015 100644 --- a/src/storage/external_sort.h +++ b/src/storage/external_sort.h @@ -144,6 +144,6 @@ struct SORT_INFO extern int sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GET_FUNC * get_fn, void *get_arg, SORT_PUT_FUNC * put_fn, void *put_arg, SORT_CMP_FUNC * cmp_fn, void *cmp_arg, - SORT_DUP_OPTION option, int limit, bool includes_tde_class); + SORT_DUP_OPTION option, int limit, bool includes_tde_class, bool is_parallel); #endif /* _EXTERNAL_SORT_H_ */ From 3387562e2a5a1684031470b9c18fa3dd07aa8b51 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 3 Jul 2024 16:42:55 +0900 Subject: [PATCH 02/53] remove px_mtx, add list_file_close() for avoiding latch contention, change fix_mode to PGBUF_LATCH_READ on read process --- src/query/list_file.c | 3 +++ src/query/query_manager.c | 2 +- src/storage/external_sort.c | 6 ++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/query/list_file.c b/src/query/list_file.c index 9aa0ee19389..cf5602e3a75 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -3974,6 +3974,9 @@ qfile_sort_list_with_func (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p, S int sort_result, estimated_pages; SORT_DUP_OPTION dup_option; + /* The result file must be closed for parallel processing. If not closed, Latch contention may occur. */ + qfile_close_list (thread_p, list_id_p); + srlist_id = qfile_open_list (thread_p, &list_id_p->type_list, sort_list_p, list_id_p->query_id, flag, NULL); if (srlist_id == NULL) { diff --git a/src/query/query_manager.c b/src/query/query_manager.c index aaa7e6d3323..03615cfeabe 100644 --- a/src/query/query_manager.c +++ b/src/query/query_manager.c @@ -2441,7 +2441,7 @@ qmgr_get_old_page (THREAD_ENTRY * thread_p, VPID * vpid_p, QMGR_TEMP_FILE * tfil else { /* return temp file page */ - page_p = pgbuf_fix (thread_p, vpid_p, OLD_PAGE, PGBUF_LATCH_WRITE, PGBUF_UNCONDITIONAL_LATCH); + page_p = pgbuf_fix (thread_p, vpid_p, OLD_PAGE, PGBUF_LATCH_READ, PGBUF_UNCONDITIONAL_LATCH); if (page_p != NULL) { diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index ee14b61dd5c..08b732a4a42 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -163,7 +163,7 @@ struct sort_param /* support parallelism */ #if defined(SERVER_MODE) - pthread_mutex_t px_mtx; /* px_node status mutex */ + /* pthread_mutex_t px_mtx; /* px_node status mutex */ #endif int px_max_index; int px_index; @@ -3979,11 +3979,13 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) } #if defined(SERVER_MODE) - rv = pthread_mutex_destroy (&(sort_param->px_mtx)); + /* temporary disable */ + /* rv = pthread_mutex_destroy (&(sort_param->px_mtx)); if (rv != 0) { er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_CSS_PTHREAD_MUTEX_DESTROY, 0); } + */ #endif free_and_init (sort_param); From def67342eafed136ba86d4c5d7e63f1aaec27312 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 3 Jul 2024 17:10:09 +0900 Subject: [PATCH 03/53] indent --- src/storage/external_sort.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 08b732a4a42..b771391b0cc 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -163,7 +163,7 @@ struct sort_param /* support parallelism */ #if defined(SERVER_MODE) - /* pthread_mutex_t px_mtx; /* px_node status mutex */ + /* pthread_mutex_t px_mtx; /* px_node status mutex */ #endif int px_max_index; int px_index; @@ -3981,11 +3981,11 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) #if defined(SERVER_MODE) /* temporary disable */ /* rv = pthread_mutex_destroy (&(sort_param->px_mtx)); - if (rv != 0) - { - er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_CSS_PTHREAD_MUTEX_DESTROY, 0); - } - */ + if (rv != 0) + { + er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_CSS_PTHREAD_MUTEX_DESTROY, 0); + } + */ #endif free_and_init (sort_param); From a0d70d044dc4586c6d7477b242562f24e1f7cba2 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Fri, 5 Jul 2024 17:09:20 +0900 Subject: [PATCH 04/53] init record --- src/storage/external_sort.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index b771391b0cc..453905ab7e1 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -3058,8 +3058,8 @@ sort_put_result_from_tmpfile (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) int slot_num = 0; char *cur_pgptr; int result_file_idx = sort_param->px_result_file_idx; - RECDES record; - RECDES long_record; + RECDES record = RECDES_INITIALIZER; + RECDES long_record = RECDES_INITIALIZER; int error; SORT_REC *sort_rec; int tot_rows = 0; From 743743d671db3e0d27d1ec5c935fa6039bb86d45 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 8 Jul 2024 17:30:50 +0900 Subject: [PATCH 05/53] temporarily blocked --- src/query/query_executor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/query/query_executor.c b/src/query/query_executor.c index fc96bff80bd..a62a2244709 100644 --- a/src/query/query_executor.c +++ b/src/query/query_executor.c @@ -4653,7 +4653,7 @@ qexec_groupby (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_stat /* sort and aggregate partial results */ if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_hash_gby_get_next, &gbstate, &qexec_hash_gby_put_next, &gbstate, cmp_fn, &gbstate.agg_hash_context->sort_key, SORT_DUP, - NO_SORT_LIMIT, gbstate.output_file->tfile_vfid->tde_encrypted, true) != NO_ERROR) + NO_SORT_LIMIT, gbstate.output_file->tfile_vfid->tde_encrypted, false) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } @@ -4734,7 +4734,7 @@ qexec_groupby (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_stat if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_gby_get_next, &gbstate, &qexec_gby_put_next, &gbstate, gbstate.cmp_fn, &gbstate.key_info, SORT_DUP, NO_SORT_LIMIT, - gbstate.output_file->tfile_vfid->tde_encrypted, true) != NO_ERROR) + gbstate.output_file->tfile_vfid->tde_encrypted, false) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } @@ -19605,7 +19605,7 @@ qexec_execute_analytic (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_analytic_get_next, &analytic_state, &qexec_analytic_put_next, &analytic_state, analytic_state.cmp_fn, &analytic_state.key_info, - SORT_DUP, NO_SORT_LIMIT, analytic_state.output_file->tfile_vfid->tde_encrypted, true) != NO_ERROR) + SORT_DUP, NO_SORT_LIMIT, analytic_state.output_file->tfile_vfid->tde_encrypted, false) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } From efbbbff656700c0b546facce7b6ba49607d0e353 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 8 Jul 2024 19:41:30 +0900 Subject: [PATCH 06/53] temporarily, close and reopen temp file for getting. --- src/query/list_file.c | 4 +-- src/query/list_file.h | 2 ++ src/storage/external_sort.c | 54 +++++++++++++++++++++++++++++++++++++ src/storage/external_sort.h | 1 + 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/query/list_file.c b/src/query/list_file.c index 1487161b3cb..3f9df79105e 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -241,7 +241,6 @@ static void qfile_close_and_free_list_file (THREAD_ENTRY * thread_p, QFILE_LIST_ static QFILE_LIST_ID *qfile_union_list (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id1, QFILE_LIST_ID * list_id2, int flag); -static SORT_STATUS qfile_get_next_sort_item (THREAD_ENTRY * thread_p, RECDES * recdes, void *arg); static int qfile_put_next_sort_item (THREAD_ENTRY * thread_p, const RECDES * recdes, void *arg); static SORT_INFO *qfile_initialize_sort_info (SORT_INFO * info, QFILE_LIST_ID * listid, SORT_LIST * sort_list); static void qfile_clear_sort_info (SORT_INFO * info); @@ -3368,7 +3367,7 @@ qfile_generate_sort_tuple (SORTKEY_INFO * key_info_p, SORT_REC * sort_record_p, * in the list file, or if an error occurs, the sorting module is informed with * necessary return codes. */ -static SORT_STATUS +SORT_STATUS qfile_get_next_sort_item (THREAD_ENTRY * thread_p, RECDES * recdes_p, void *arg) { SORT_INFO *sort_info_p; @@ -4005,6 +4004,7 @@ qfile_sort_list_with_func (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p, S info.s_id = &s_scan_id; info.output_file = srlist_id; + info.input_file = list_id_p; info.extra_arg = extra_arg; if (get_func == NULL) diff --git a/src/query/list_file.h b/src/query/list_file.h index e843c688276..2b68bcffb91 100644 --- a/src/query/list_file.h +++ b/src/query/list_file.h @@ -228,6 +228,8 @@ extern int qfile_overwrite_tuple (THREAD_ENTRY * thread_p, PAGE_PTR first_page, extern void qfile_update_qlist_count (THREAD_ENTRY * thread_p, const QFILE_LIST_ID * list_p, int inc); extern int qfile_get_list_cache_number_of_entries (int ht_no); extern bool qfile_has_no_cache_entries (); +extern SORT_STATUS qfile_get_next_sort_item (THREAD_ENTRY * thread_p, RECDES * recdes, void *arg); + #endif /* _LIST_FILE_H_ */ diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 453905ab7e1..f83326d151e 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -50,6 +50,7 @@ #include "server_support.h" #include "thread_entry_task.hpp" #include "thread_manager.hpp" // for thread_get_thread_entry_info and thread_sleep +#include "list_file.h" #include @@ -1471,6 +1472,24 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE } else { + + if (get_fn == qfile_get_next_sort_item) + { + SORT_INFO *sort_info_p; + QFILE_LIST_SCAN_ID *scan_id_p; + + sort_info_p = (SORT_INFO *) sort_param->get_arg; + scan_id_p = sort_info_p->s_id->s_id; + + /* temporarily, close file for read */ + qfile_close_scan (thread_p, scan_id_p); + } + else + { + assert(0); + return -1; + } + /* init px variable */ sort_param->px_status[0] = 0; sort_param->px_max_index = 2; @@ -1531,11 +1550,46 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE static void sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) { + QFILE_LIST_SCAN_ID t_scan_id; + thread_ref.tran_index = sort_param->px_tran_index; pthread_mutex_unlock (&thread_ref.tran_index_lock); + if (sort_param->get_fn == qfile_get_next_sort_item) + { + SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; + + /* temporarily, open file for read */ + if (qfile_open_list_scan (sort_info_p->input_file, &t_scan_id) != NO_ERROR) + { + /* need to put error into sort_param */ + return; + } + sort_info_p->s_id->s_id = &t_scan_id; + } + else + { + /* need to put error into sort_param */ + assert(0); + return; + } + sort_listfile_internal (&thread_ref, sort_param); + /* temporarily, close file for read */ + if (sort_param->get_fn == qfile_get_next_sort_item) + { + SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; + + /* temporarily, open file for read */ + qfile_close_scan (&thread_ref, &t_scan_id); + } + else + { + /* need to put error into sort_param */ + assert(0); + return; + } sort_param->px_status[0] = 1; } // *INDENT-ON* diff --git a/src/storage/external_sort.h b/src/storage/external_sort.h index 6499fc9a015..028a923cc3b 100644 --- a/src/storage/external_sort.h +++ b/src/storage/external_sort.h @@ -135,6 +135,7 @@ struct SORT_INFO SORTKEY_INFO key_info; /* All of the interesting key information. */ QFILE_SORT_SCAN_ID *s_id; /* A SCAN_ID for the input list file. This is stateful, and records the current * location of the scan between calls to ls_sort_get_next(). */ + QFILE_LIST_ID *input_file; QFILE_LIST_ID *output_file; /* The name of the output file. This is where ls_sort_put_next_*() deposits its stuff. */ RECDES output_recdes; /* A working buffer for output of tuples; used only when we're using From 6d4ed79917cb50d756bbeab7bd8d4f1fff15f9ce Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Tue, 9 Jul 2024 17:31:43 +0900 Subject: [PATCH 07/53] reopen temp file for getting --- src/storage/external_sort.c | 46 ++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index f83326d151e..96bacfe5ed1 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1459,8 +1459,8 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE } #if defined(SERVER_MODE) - SORT_INFO *sort_info_p = (SORT_INFO *) put_arg; - QFILE_LIST_ID *outlist_id, *orglist_id; + SORT_INFO *sort_info_p; + QFILE_LIST_SCAN_ID *scan_id_p; /* TODO : add to routines to copy sort_param */ @@ -1472,12 +1472,8 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE } else { - - if (get_fn == qfile_get_next_sort_item) + if (sort_param->get_fn == qfile_get_next_sort_item) { - SORT_INFO *sort_info_p; - QFILE_LIST_SCAN_ID *scan_id_p; - sort_info_p = (SORT_INFO *) sort_param->get_arg; scan_id_p = sort_info_p->s_id->s_id; @@ -1515,6 +1511,24 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* TO_DO : need routines for merging parallel processed temp files into a local sort_param */ + if (sort_param->get_fn == qfile_get_next_sort_item) + { + sort_info_p = (SORT_INFO *) sort_param->get_arg; + + /* temporarily, open file for read */ + if (qfile_open_list_scan (sort_info_p->input_file, scan_id_p) != NO_ERROR) + { + assert(0); + return -1; + } + sort_info_p->s_id->s_id = scan_id_p; + } + else + { + assert(0); + return -1; + } + /* Merge the parallel processed results. */ sort_param->px_max_index = 1; if (sort_param->option == SORT_ELIM_DUP) @@ -1581,8 +1595,8 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) { SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; - /* temporarily, open file for read */ - qfile_close_scan (&thread_ref, &t_scan_id); + /* temporarily, close file for read */ + qfile_close_scan (&thread_ref, sort_info_p->s_id->s_id); } else { @@ -2415,7 +2429,12 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* for one temporary file, put result from the temp file instead of merging it. */ if (!IS_PARALLEL_EXECUTION (sort_param) && sort_get_numpages_of_active_infiles (sort_param) == 1) { - sort_put_result_from_tmpfile (thread_p, sort_param); + error = sort_put_result_from_tmpfile (thread_p, sort_param); + if (error != NO_ERROR) + { + ASSERT_ERROR (); + goto bailout; + } } /* While there are more than one input files with different runs to merge */ @@ -3301,7 +3320,12 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* for one temporary file, put result from the temp file instead of merging it. */ if (!IS_PARALLEL_EXECUTION (sort_param) && sort_get_numpages_of_active_infiles (sort_param) == 1) { - sort_put_result_from_tmpfile (thread_p, sort_param); + error = sort_put_result_from_tmpfile (thread_p, sort_param); + if (error != NO_ERROR) + { + ASSERT_ERROR (); + goto bailout; + } } /* OUTER LOOP */ From 5a52af40af42eeca4bcf4565b28f5fd071edacc7 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Tue, 16 Jul 2024 14:22:38 +0900 Subject: [PATCH 08/53] temporarily split list file on half and remove some assert. it should be reverted --- src/base/resource_tracker.hpp | 2 + src/storage/external_sort.c | 104 +++++++++++++++++++++++++++++++--- src/storage/page_buffer.c | 4 +- 3 files changed, 99 insertions(+), 11 deletions(-) diff --git a/src/base/resource_tracker.hpp b/src/base/resource_tracker.hpp index c78012ffc96..cd3d1af6a6a 100644 --- a/src/base/resource_tracker.hpp +++ b/src/base/resource_tracker.hpp @@ -445,6 +445,7 @@ namespace cubbase void restrack_assert (bool cond) { +#if 0 #if !defined (NDEBUG) if (restrack_is_assert_suppressed ()) { @@ -455,6 +456,7 @@ namespace cubbase assert (cond); } #endif // NDEBUG +#endif } } // namespace cubbase diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 96bacfe5ed1..aa09de5b06b 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -51,6 +51,8 @@ #include "thread_entry_task.hpp" #include "thread_manager.hpp" // for thread_get_thread_entry_info and thread_sleep #include "list_file.h" +#include "query_manager.h" //임시 +#include "object_representation.h" //임시 #include @@ -1337,6 +1339,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE int error = NO_ERROR; SORT_PARAM *sort_param = NULL; + SORT_PARAM px_sort_param[2]; INT32 input_pages; int i; @@ -1462,8 +1465,6 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE SORT_INFO *sort_info_p; QFILE_LIST_SCAN_ID *scan_id_p; - /* TODO : add to routines to copy sort_param */ - /* temporarily, no parallelism when creating an index. */ if (!is_parallel) { @@ -1479,6 +1480,41 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* temporarily, close file for read */ qfile_close_scan (thread_p, scan_id_p); + + /* split input temp file */ + /* 두개 파일로 분리하는 로직 ==> 분리하고 바로 합치기? */ + PAGE_PTR page_p; + VPID next_vpid, prev_vpid; + QFILE_LIST_ID * in_file = sort_info_p->input_file; + int half_num_page = in_file->page_cnt / 2; + in_file->tuple_cnt = in_file->tuple_cnt / 2; + in_file->page_cnt = half_num_page; + in_file->first_vpid = in_file->first_vpid; + + prev_vpid = in_file->first_vpid; + int j = 0; + /* skip pages */ + while (true) + { + page_p = qmgr_get_old_page (thread_p, &prev_vpid, in_file->tfile_vfid); + QFILE_GET_NEXT_VPID (&next_vpid, page_p); + if (VPID_ISNULL (&next_vpid)) + { + break; + } + if (++j > half_num_page) + { + QFILE_PUT_NEXT_VPID_NULL (page_p); + qmgr_free_old_page_and_init (thread_p, page_p, in_file->tfile_vfid); + break; + } + prev_vpid = next_vpid; + qmgr_free_old_page_and_init (thread_p, page_p, in_file->tfile_vfid); + } + + /* in_file->last_vpid = prev_vpid; */ + /* sort_info_p->input_file.tfile_vfid = ??; */ + } else { @@ -1486,31 +1522,64 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE return -1; } - /* init px variable */ + /* copy sort_param for parallel sort */ +/* for (int i = 0; i < 1; i++) + { + memcpy (&px_sort_param[i], sort_param, sizeof (SORT_PARAM)); + px_sort_param[i].internal_memory = (char *) malloc ((size_t) sort_param->tot_buffers * (size_t) DB_PAGESIZE); + if (px_sort_param[i].internal_memory == NULL) + { + error = ER_OUT_OF_VIRTUAL_MEMORY; + er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 1, (size_t) (sort_param->tot_buffers * DB_PAGESIZE)); + /* goto cleanup; */ +/* } + for (int j = 0; j < px_sort_param[i].tot_tempfiles; j++) + { + /* Initilize file contents list */ +/* px_sort_param[i].file_contents[j].num_pages = + (int *) db_private_alloc (thread_p, SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); + if (px_sort_param[i].file_contents[j].num_pages == NULL) + { + sort_param->tot_tempfiles = j; + error = ER_OUT_OF_VIRTUAL_MEMORY; + /* goto cleanup; */ +/* } + } + /* init px variable */ +/* px_sort_param[i].px_status[0] = 0; + px_sort_param[i].px_max_index = 2; + px_sort_param[i].px_index = i + 1; + px_sort_param[i].px_result_file_idx = 0; + px_sort_param[i].px_tran_index = LOG_FIND_THREAD_TRAN_INDEX (thread_p); + } +*/ sort_param->px_status[0] = 0; sort_param->px_max_index = 2; sort_param->px_index = 1; sort_param->px_result_file_idx = 0; sort_param->px_tran_index = LOG_FIND_THREAD_TRAN_INDEX (thread_p); - cubthread::entry_callable_task * task = - new cubthread::entry_callable_task (std::bind (sort_listfile_execute, std::placeholders::_1, sort_param)); - css_push_external_task (css_get_current_conn_entry (), task); + /* parallel execute */ + for (int i = 0; i < 1; i++) + { + cubthread::entry_callable_task * task = + new cubthread::entry_callable_task (std::bind (sort_listfile_execute, std::placeholders::_1, sort_param)); + css_push_external_task (css_get_current_conn_entry (), task); + } + /* wait for threads */ /* TO_DO : no busy wait. need to block and wake up */ do { thread_sleep (10); /* 10 msec */ - if (sort_param->px_status[0] == 1) + if (sort_param->px_status[0] == 1 /* && px_sort_param[1].px_status[0] == 1 */) { break; } } while (1); - /* TO_DO : need routines for merging parallel processed temp files into a local sort_param */ - if (sort_param->get_fn == qfile_get_next_sort_item) { sort_info_p = (SORT_INFO *) sort_param->get_arg; @@ -1529,6 +1598,18 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE return -1; } + /* TO_DO : need routines for merging parallel processed temp files into a local sort_param */ + /* sort_param의 file content를 sort param으로 복사해야함. 단순 복사 가능? */ + /* file_contents와 sort_param->temp[out_file] */ +/* sort_param->temp[0] = px_sort_param[0].temp[px_sort_param[0].px_result_file_idx]; + sort_param->file_contents[0] = px_sort_param[0].file_contents[px_sort_param[0].px_result_file_idx]; + + sort_param->px_result_file_idx = 0; + +/* sort_param->temp[1] = px_sort_param[1].temp[px_sort_param[1].px_result_file_idx]; + sort_param->file_contents[1] = px_sort_param[1].file_contents[px_sort_param[1].px_result_file_idx]; */ + + /* Merge the parallel processed results. */ sort_param->px_max_index = 1; if (sort_param->option == SORT_ELIM_DUP) @@ -1540,6 +1621,11 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* SORT_DUP */ error = sort_exphase_merge (thread_p, sort_param); } + +/* for (int i = 0;i < 1; i++) + { + sort_return_used_resources (thread_p, &sort_param[i]); + } */ } #else diff --git a/src/storage/page_buffer.c b/src/storage/page_buffer.c index ecb163a2815..62b4e665505 100644 --- a/src/storage/page_buffer.c +++ b/src/storage/page_buffer.c @@ -2659,7 +2659,7 @@ pgbuf_unfix_all (THREAD_ENTRY * thread_p) holder = thrd_holder_info->thrd_hold_list; while (holder != NULL) { - assert (false); + /* assert (false); */ CAST_BFPTR_TO_PGPTR (pgptr, holder->bufptr); @@ -2671,7 +2671,7 @@ pgbuf_unfix_all (THREAD_ENTRY * thread_p) holder = thrd_holder_info->thrd_hold_list; #else /* NDEBUG */ CAST_PGPTR_TO_BFPTR (bufptr, pgptr); - assert (!VPID_ISNULL (&bufptr->vpid)); + /* assert (!VPID_ISNULL (&bufptr->vpid)); */ latch_mode_str = pgbuf_latch_mode_str (bufptr->latch_mode); zone_str = pgbuf_zone_str (pgbuf_bcb_get_zone (bufptr)); From 01c518febee8efe5eab8b42fd0bd6ad5f29ca13a Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 5 Sep 2024 20:42:39 +0900 Subject: [PATCH 09/53] copy sort_param and split input_temp_file --- src/storage/external_sort.c | 454 ++++++++++++++++++++++++++---------- 1 file changed, 330 insertions(+), 124 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index aa09de5b06b..5bf41d02131 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -51,8 +51,8 @@ #include "thread_entry_task.hpp" #include "thread_manager.hpp" // for thread_get_thread_entry_info and thread_sleep #include "list_file.h" -#include "query_manager.h" //임시 -#include "object_representation.h" //임시 +#include "query_manager.h" +#include "object_representation.h" #include @@ -73,6 +73,8 @@ */ #define SORT_MIN_HALF_FILES 2 +#define SORT_MAX_PARALLEL 16 + /* Initial size of the dynamic array that keeps the file contents list */ #define SORT_INITIAL_DYN_ARRAY_SIZE 30 @@ -170,7 +172,7 @@ struct sort_param #endif int px_max_index; int px_index; - bool px_status[32]; + bool px_status; int px_result_file_idx; int px_tran_index; }; @@ -236,8 +238,15 @@ static int sort_validate (char **vector, long size, SORT_CMP_FUNC * compare, voi #endif #if defined(SERVER_MODE) +/* start parallel sort */ static void sort_listfile_execute (cubthread::entry & thread_ref, SORT_PARAM * sort_param); +static int sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, + int parallel_num); +static int sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, + int parallel_num); #endif +/* end parallel sort */ + static int sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FUNC * get_next, void *arguments, unsigned int *total_numrecs); @@ -245,7 +254,7 @@ static int sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * so static int sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_put_result_from_tmpfile (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_get_avg_numpages_of_nonempty_tmpfile (SORT_PARAM * sort_param); -static void sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); +static void sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bool is_parallel); static int sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bool force_alloc, bool tde_encrypted); @@ -1339,10 +1348,14 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE int error = NO_ERROR; SORT_PARAM *sort_param = NULL; - SORT_PARAM px_sort_param[2]; INT32 input_pages; int i; + /* for parallel sort */ + SORT_PARAM px_sort_param[SORT_MAX_PARALLEL]; /* TO_DO : need dynamic alloc */ + QFILE_LIST_SCAN_ID *scan_id_p; + int parallel_num = 2; /* TO_DO : depending on the number of pages in the temp file */ + thread_set_sort_stats_active (thread_p, true); #if defined(ENABLE_SYSTEMTAP) @@ -1448,132 +1461,69 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_param->tde_encrypted = includes_tde_class; -cleanup: - if (error != NO_ERROR) - { -#if defined(ENABLE_SYSTEMTAP) - CUBRID_SORT_END (sort_param->total_numrecs, error); -#endif /* ENABLE_SYSTEMTAP */ - - sort_return_used_resources (thread_p, sort_param); - sort_param = NULL; - thread_set_sort_stats_active (thread_p, false); - return error; - } #if defined(SERVER_MODE) SORT_INFO *sort_info_p; - QFILE_LIST_SCAN_ID *scan_id_p; - /* temporarily, no parallelism when creating an index. */ + /* check parallelism */ + if (sort_param->get_fn == qfile_get_next_sort_item) + { + /* get scan id of input file */ + sort_info_p = (SORT_INFO *) sort_param->get_arg; + + /* need to check the appropriate number of parallels depending on the number of pages */ + if (sort_info_p->input_file->page_cnt < 2) + { + is_parallel = false; + } + } + if (!is_parallel) { + /* temporarily, only parallelism when ORDER_BY. */ sort_param->px_max_index = 1; error = sort_listfile_internal (thread_p, sort_param); } else { - if (sort_param->get_fn == qfile_get_next_sort_item) + /* copy sort_param for parallel sort */ + error = sort_copy_sort_param (thread_p, px_sort_param, sort_param, parallel_num); + if (error != NO_ERROR) { - sort_info_p = (SORT_INFO *) sort_param->get_arg; - scan_id_p = sort_info_p->s_id->s_id; + goto cleanup; + } - /* temporarily, close file for read */ - qfile_close_scan (thread_p, scan_id_p); - - /* split input temp file */ - /* 두개 파일로 분리하는 로직 ==> 분리하고 바로 합치기? */ - PAGE_PTR page_p; - VPID next_vpid, prev_vpid; - QFILE_LIST_ID * in_file = sort_info_p->input_file; - int half_num_page = in_file->page_cnt / 2; - in_file->tuple_cnt = in_file->tuple_cnt / 2; - in_file->page_cnt = half_num_page; - in_file->first_vpid = in_file->first_vpid; - - prev_vpid = in_file->first_vpid; - int j = 0; - /* skip pages */ - while (true) + /* case of ORDER BY */ + if (sort_param->get_fn == qfile_get_next_sort_item) + { + /* split input temp file. TO_DO : it can be inside sort_copy_sort_param() */ + error = sort_split_input_temp_file (thread_p, px_sort_param, sort_param, parallel_num); + if (error != NO_ERROR) { - page_p = qmgr_get_old_page (thread_p, &prev_vpid, in_file->tfile_vfid); - QFILE_GET_NEXT_VPID (&next_vpid, page_p); - if (VPID_ISNULL (&next_vpid)) - { - break; - } - if (++j > half_num_page) - { - QFILE_PUT_NEXT_VPID_NULL (page_p); - qmgr_free_old_page_and_init (thread_p, page_p, in_file->tfile_vfid); - break; - } - prev_vpid = next_vpid; - qmgr_free_old_page_and_init (thread_p, page_p, in_file->tfile_vfid); + goto cleanup; } - - /* in_file->last_vpid = prev_vpid; */ - /* sort_info_p->input_file.tfile_vfid = ??; */ - } else { - assert(0); + /* not implemented yet (group by, analytic fuction, create index) */ return -1; } - /* copy sort_param for parallel sort */ -/* for (int i = 0; i < 1; i++) - { - memcpy (&px_sort_param[i], sort_param, sizeof (SORT_PARAM)); - px_sort_param[i].internal_memory = (char *) malloc ((size_t) sort_param->tot_buffers * (size_t) DB_PAGESIZE); - if (px_sort_param[i].internal_memory == NULL) - { - error = ER_OUT_OF_VIRTUAL_MEMORY; - er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 1, (size_t) (sort_param->tot_buffers * DB_PAGESIZE)); - /* goto cleanup; */ -/* } - for (int j = 0; j < px_sort_param[i].tot_tempfiles; j++) - { - /* Initilize file contents list */ -/* px_sort_param[i].file_contents[j].num_pages = - (int *) db_private_alloc (thread_p, SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); - if (px_sort_param[i].file_contents[j].num_pages == NULL) - { - sort_param->tot_tempfiles = j; - error = ER_OUT_OF_VIRTUAL_MEMORY; - /* goto cleanup; */ -/* } - } - /* init px variable */ -/* px_sort_param[i].px_status[0] = 0; - px_sort_param[i].px_max_index = 2; - px_sort_param[i].px_index = i + 1; - px_sort_param[i].px_result_file_idx = 0; - px_sort_param[i].px_tran_index = LOG_FIND_THREAD_TRAN_INDEX (thread_p); - } -*/ - sort_param->px_status[0] = 0; - sort_param->px_max_index = 2; - sort_param->px_index = 1; - sort_param->px_result_file_idx = 0; - sort_param->px_tran_index = LOG_FIND_THREAD_TRAN_INDEX (thread_p); - - /* parallel execute */ - for (int i = 0; i < 1; i++) + for (int i = 0; i < parallel_num; i++) { cubthread::entry_callable_task * task = - new cubthread::entry_callable_task (std::bind (sort_listfile_execute, std::placeholders::_1, sort_param)); + new cubthread::entry_callable_task (std:: + bind (sort_listfile_execute, std::placeholders::_1, &px_sort_param[i])); css_push_external_task (css_get_current_conn_entry (), task); } - + /* wait for threads */ /* TO_DO : no busy wait. need to block and wake up */ do { - thread_sleep (10); /* 10 msec */ - if (sort_param->px_status[0] == 1 /* && px_sort_param[1].px_status[0] == 1 */) + thread_sleep (10); + if (px_sort_param[0].px_status == 1 && px_sort_param[1].px_status == 1) { break; } @@ -1583,32 +1533,49 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE if (sort_param->get_fn == qfile_get_next_sort_item) { sort_info_p = (SORT_INFO *) sort_param->get_arg; + scan_id_p = sort_info_p->s_id->s_id; - /* temporarily, open file for read */ + /* open origin temp file for read */ if (qfile_open_list_scan (sort_info_p->input_file, scan_id_p) != NO_ERROR) { - assert(0); + assert (0); return -1; } - sort_info_p->s_id->s_id = scan_id_p; } else { - assert(0); + /* not implemented yet (group by, analytic fuction, create index) */ return -1; } - /* TO_DO : need routines for merging parallel processed temp files into a local sort_param */ - /* sort_param의 file content를 sort param으로 복사해야함. 단순 복사 가능? */ - /* file_contents와 sort_param->temp[out_file] */ -/* sort_param->temp[0] = px_sort_param[0].temp[px_sort_param[0].px_result_file_idx]; + /* merging temp files from parallel processed */ + /* TO_DO : fix to be processed by parallel_num */ + sort_param->temp[0] = px_sort_param[0].temp[px_sort_param[0].px_result_file_idx]; sort_param->file_contents[0] = px_sort_param[0].file_contents[px_sort_param[0].px_result_file_idx]; sort_param->px_result_file_idx = 0; -/* sort_param->temp[1] = px_sort_param[1].temp[px_sort_param[1].px_result_file_idx]; - sort_param->file_contents[1] = px_sort_param[1].file_contents[px_sort_param[1].px_result_file_idx]; */ + sort_param->temp[1] = px_sort_param[1].temp[px_sort_param[1].px_result_file_idx]; + sort_param->file_contents[1] = px_sort_param[1].file_contents[px_sort_param[1].px_result_file_idx]; + /* only 2 files remains */ + sort_param->half_files = 2; + sort_param->tot_tempfiles = 4; + sort_param->in_half = 0; + + /* Create output temporary files make file and temporary volume page count estimates */ + int file_pg_cnt_est = sort_get_avg_numpages_of_nonempty_tmpfile (sort_param); + file_pg_cnt_est = MAX (1, file_pg_cnt_est); + + for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) + { + error = + sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted); + if (error != NO_ERROR) + { + goto cleanup; + } + } /* Merge the parallel processed results. */ sort_param->px_max_index = 1; @@ -1622,10 +1589,11 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE error = sort_exphase_merge (thread_p, sort_param); } -/* for (int i = 0;i < 1; i++) + /* free px_sort_param */ + for (int i = 0; i < parallel_num; i++) { - sort_return_used_resources (thread_p, &sort_param[i]); - } */ + sort_return_used_resources (thread_p, &px_sort_param[i], true); + } } #else @@ -1634,11 +1602,12 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE error = sort_listfile_internal (thread_p, sort_param); #endif +cleanup: #if defined(ENABLE_SYSTEMTAP) CUBRID_SORT_END (sort_param->total_numrecs, error); #endif /* ENABLE_SYSTEMTAP */ - sort_return_used_resources (thread_p, sort_param); + sort_return_used_resources (thread_p, sort_param, false); sort_param = NULL; thread_set_sort_stats_active (thread_p, false); @@ -1655,6 +1624,7 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) thread_ref.tran_index = sort_param->px_tran_index; pthread_mutex_unlock (&thread_ref.tran_index_lock); +/* printf ("sort_listfie index = %d\n",thread_ref.index); */ if (sort_param->get_fn == qfile_get_next_sort_item) { SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; @@ -1690,7 +1660,7 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) assert(0); return; } - sort_param->px_status[0] = 1; + sort_param->px_status = 1; } // *INDENT-ON* #endif @@ -4104,7 +4074,7 @@ sort_get_avg_numpages_of_nonempty_tmpfile (SORT_PARAM * sort_param) * memory areas and destroying any temporary files and volumes. */ static void -sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) +sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bool is_parallel) { int k; #if defined(SERVER_MODE) @@ -4121,11 +4091,14 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) free_and_init (sort_param->internal_memory); } - for (k = 0; k < sort_param->tot_tempfiles; k++) + if (!is_parallel) { - if (sort_param->temp[k].volid != NULL_VOLID) + for (k = 0; k < sort_param->tot_tempfiles; k++) { - (void) file_temp_retire (thread_p, &sort_param->temp[k]); + if (sort_param->temp[k].volid != NULL_VOLID) + { + (void) file_temp_retire (thread_p, &sort_param->temp[k]); + } } } @@ -4134,11 +4107,35 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) (void) file_temp_retire (thread_p, &(sort_param->multipage_file)); } - for (k = 0; k < sort_param->tot_tempfiles; k++) + if (is_parallel) /* If it is parallel processing, some may not be free. need to check again. */ + { + for (k = 0; k < sort_param->tot_tempfiles; k++) + { + if (sort_param->file_contents[k].num_pages != NULL) + { + db_private_free_and_init (thread_p, sort_param->file_contents[k].num_pages); + } + } + } + + if (is_parallel) { - if (sort_param->file_contents[k].num_pages != NULL) + /* free memory */ + for (int i = 0; i < sort_param->px_max_index; i++) { - db_private_free_and_init (thread_p, sort_param->file_contents[k].num_pages); + if (sort_param->get_arg != NULL) + { + SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; + if (sort_info_p->s_id != NULL) + { + db_private_free_and_init (thread_p, sort_info_p->s_id); + } + if (sort_info_p->input_file != NULL) + { + db_private_free_and_init (thread_p, sort_info_p->input_file); + } + db_private_free_and_init (thread_p, sort_param->get_arg); + } } } @@ -4152,7 +4149,10 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) */ #endif - free_and_init (sort_param); + if (!is_parallel) + { + free_and_init (sort_param); + } } /* @@ -4220,6 +4220,212 @@ sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bo return NO_ERROR; } +/* + * sort_copy_sort_param () - copy sort param from src_param to dest_param + * return: NO_ERROR + * dest_param(in): + * src_param(in): + * parallel_num(in): + */ +static int +sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, int parallel_num) +{ + int error = NO_ERROR; + int i, j; + + /* init */ + for (i = 0; i < parallel_num; i++) + { + px_sort_param[i].internal_memory = NULL; + for (j = 0; j < px_sort_param[i].tot_tempfiles; j++) + { + px_sort_param[i].file_contents[j].num_pages = NULL; + } + } + + /* copy from origin sort param */ + for (i = 0; i < parallel_num; i++) + { + memcpy (&px_sort_param[i], sort_param, sizeof (SORT_PARAM)); + px_sort_param[i].internal_memory = (char *) malloc ((size_t) sort_param->tot_buffers * (size_t) DB_PAGESIZE); + if (px_sort_param[i].internal_memory == NULL) + { + error = ER_OUT_OF_VIRTUAL_MEMORY; + er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 1, (size_t) (sort_param->tot_buffers * DB_PAGESIZE)); + break; + } + for (j = 0; j < px_sort_param[i].tot_tempfiles; j++) + { + /* Initilize file contents list */ + px_sort_param[i].file_contents[j].num_pages = + (int *) db_private_alloc (thread_p, SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); + if (px_sort_param[i].file_contents[j].num_pages == NULL) + { + sort_param->tot_tempfiles = j; + error = ER_OUT_OF_VIRTUAL_MEMORY; + break; + } + + px_sort_param[i].file_contents[j].num_slots = SORT_INITIAL_DYN_ARRAY_SIZE; + px_sort_param[i].file_contents[j].first_run = -1; + px_sort_param[i].file_contents[j].last_run = -1; + } + + /* init px variable */ + px_sort_param[i].px_status = 0; + px_sort_param[i].px_max_index = parallel_num; + px_sort_param[i].px_index = i + 1; + px_sort_param[i].px_result_file_idx = 0; + /* Copy the parent's tran_index. */ + px_sort_param[i].px_tran_index = LOG_FIND_THREAD_TRAN_INDEX (thread_p); + /* init get_arg. it'll be copied in sort_split_input_temp_file(). TO_DO : put_arg?? */ + px_sort_param[i].get_arg = NULL; + } + + if (error != NO_ERROR) + { + /* free memory */ + for (i = 0; i < parallel_num; i++) + { + if (px_sort_param[i].internal_memory != NULL) + { + free_and_init (px_sort_param[i].internal_memory); + } + for (j = 0; j < px_sort_param[i].tot_tempfiles; j++) + { + if (px_sort_param[i].file_contents[j].num_pages != NULL) + { + db_private_free_and_init (thread_p, px_sort_param[i].file_contents[j].num_pages); + } + } + } + } + + return error; +} + +/* + * sort_split_input_temp_file () - split input temp file + * return: NO_ERROR + * px_sort_param(in): + * sort_param(in): + * parallel_num(in): + */ +static int +sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, + int parallel_num) +{ + int error = NO_ERROR; + int i = 0, j = 0, half_num_page; + PAGE_PTR page_p; + VPID next_vpid, prev_vpid, first_vpid[SORT_MAX_PARALLEL], last_vpid[SORT_MAX_PARALLEL]; + QFILE_LIST_SCAN_ID *scan_id_p; + SORT_INFO *sort_info_p, *org_sort_info_p; + + /* get scan id of input file */ + sort_info_p = (SORT_INFO *) sort_param->get_arg; + scan_id_p = sort_info_p->s_id->s_id; + + half_num_page = sort_info_p->input_file->page_cnt / parallel_num; + + /* close input file for read. it'll be opened in parallel */ + qfile_close_scan (thread_p, scan_id_p); + + /* find first and last vpid for splitted file */ + prev_vpid = sort_info_p->input_file->first_vpid; + while (true) + { + page_p = qmgr_get_old_page (thread_p, &prev_vpid, sort_info_p->input_file->tfile_vfid); + QFILE_GET_NEXT_VPID (&next_vpid, page_p); + if (VPID_ISNULL (&next_vpid)) + { + break; + } + else if (++j >= half_num_page) + { + QFILE_PUT_NEXT_VPID_NULL (page_p); + qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); + first_vpid[i] = next_vpid; + last_vpid[i] = prev_vpid; + i++; + + if (i < parallel_num - 1) + { + j = 0; + } + else + { + break; + } + } + prev_vpid = next_vpid; + qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); + } + + /* alloc get_arg */ + for (i = 0; i < parallel_num; i++) + { + px_sort_param[i].get_arg = (void *) db_private_alloc (thread_p, sizeof (SORT_INFO)); + if (px_sort_param[i].get_arg == NULL) + { + error = ER_OUT_OF_VIRTUAL_MEMORY; + goto cleanup; + } + sort_info_p = (SORT_INFO *) px_sort_param[i].get_arg; + org_sort_info_p = (SORT_INFO *) sort_param->get_arg; + memcpy (sort_info_p, org_sort_info_p, sizeof (SORT_INFO)); + + sort_info_p->input_file = NULL; + sort_info_p->s_id = NULL; + + sort_info_p->s_id = (QFILE_SORT_SCAN_ID *) db_private_alloc (thread_p, sizeof (QFILE_SORT_SCAN_ID)); + if (sort_info_p->s_id == NULL) + { + error = ER_OUT_OF_VIRTUAL_MEMORY; + goto cleanup; + } + memcpy (sort_info_p->s_id, org_sort_info_p->s_id, sizeof (QFILE_SORT_SCAN_ID)); + + sort_info_p->input_file = (QFILE_LIST_ID *) db_private_alloc (thread_p, sizeof (QFILE_LIST_ID)); + if (sort_info_p->input_file == NULL) + { + error = ER_OUT_OF_VIRTUAL_MEMORY; + goto cleanup; + } + memcpy (sort_info_p->input_file, org_sort_info_p->input_file, sizeof (QFILE_LIST_ID)); + sort_info_p->input_file->tuple_cnt = org_sort_info_p->input_file->tuple_cnt / parallel_num; + sort_info_p->input_file->page_cnt = half_num_page; + sort_info_p->input_file->first_vpid = (i == 0) ? org_sort_info_p->input_file->first_vpid : first_vpid[i - 1]; + sort_info_p->input_file->last_vpid = + (i == parallel_num - 1) ? org_sort_info_p->input_file->last_vpid : last_vpid[i]; + /* TO_DO : check to need to handle sort_info_p->in_file->tfile_vfid */ + } + +cleanup: + if (error != NO_ERROR) + { + /* free memory */ + for (i = 0; i < parallel_num; i++) + { + if (px_sort_param[i].get_arg != NULL) + { + sort_info_p = (SORT_INFO *) px_sort_param[i].get_arg; + if (sort_info_p->s_id != NULL) + { + db_private_free_and_init (thread_p, sort_info_p->s_id); + } + if (sort_info_p->input_file != NULL) + { + db_private_free_and_init (thread_p, sort_info_p->input_file); + } + db_private_free_and_init (thread_p, px_sort_param[i].get_arg); + } + } + } + + return error; +} + /* * sort_write_area () - Write memory area to disk * return: From d0ac831b73b2c40a6fb47b7a4c2d43f77f896f3b Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 11 Sep 2024 16:13:38 +0900 Subject: [PATCH 10/53] add sort_merge_run_for_parallel () --- src/storage/external_sort.c | 121 ++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 41 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 5bf41d02131..b9f3d5f499a 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -244,6 +244,8 @@ static int sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * dest_para int parallel_num); static int sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); +static int sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, + int parallel_num); #endif /* end parallel sort */ @@ -1354,7 +1356,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* for parallel sort */ SORT_PARAM px_sort_param[SORT_MAX_PARALLEL]; /* TO_DO : need dynamic alloc */ QFILE_LIST_SCAN_ID *scan_id_p; - int parallel_num = 2; /* TO_DO : depending on the number of pages in the temp file */ + int parallel_num = 4; /* TO_DO : depending on the number of pages in the temp file */ thread_set_sort_stats_active (thread_p, true); @@ -1480,7 +1482,6 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE if (!is_parallel) { - /* temporarily, only parallelism when ORDER_BY. */ sort_param->px_max_index = 1; error = sort_listfile_internal (thread_p, sort_param); } @@ -1549,44 +1550,10 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE } /* merging temp files from parallel processed */ - /* TO_DO : fix to be processed by parallel_num */ - sort_param->temp[0] = px_sort_param[0].temp[px_sort_param[0].px_result_file_idx]; - sort_param->file_contents[0] = px_sort_param[0].file_contents[px_sort_param[0].px_result_file_idx]; - - sort_param->px_result_file_idx = 0; - - sort_param->temp[1] = px_sort_param[1].temp[px_sort_param[1].px_result_file_idx]; - sort_param->file_contents[1] = px_sort_param[1].file_contents[px_sort_param[1].px_result_file_idx]; - - /* only 2 files remains */ - sort_param->half_files = 2; - sort_param->tot_tempfiles = 4; - sort_param->in_half = 0; - - /* Create output temporary files make file and temporary volume page count estimates */ - int file_pg_cnt_est = sort_get_avg_numpages_of_nonempty_tmpfile (sort_param); - file_pg_cnt_est = MAX (1, file_pg_cnt_est); - - for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) - { - error = - sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted); - if (error != NO_ERROR) - { - goto cleanup; - } - } - - /* Merge the parallel processed results. */ - sort_param->px_max_index = 1; - if (sort_param->option == SORT_ELIM_DUP) - { - error = sort_exphase_merge_elim_dup (thread_p, sort_param); - } - else + error = sort_merge_run_for_parallel (thread_p, px_sort_param, sort_param, parallel_num); + if (error != NO_ERROR) { - /* SORT_DUP */ - error = sort_exphase_merge (thread_p, sort_param); + goto cleanup; } /* free px_sort_param */ @@ -4107,7 +4074,7 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bo (void) file_temp_retire (thread_p, &(sort_param->multipage_file)); } - if (is_parallel) /* If it is parallel processing, some may not be free. need to check again. */ + if (is_parallel) { for (k = 0; k < sort_param->tot_tempfiles; k++) { @@ -4344,7 +4311,6 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= half_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); - qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; i++; @@ -4355,6 +4321,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, } else { + qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } } @@ -4426,6 +4393,78 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, return error; } +/* + * sort_merge_run_for_parallel () - merge run for parallel + * return: NO_ERROR + * px_sort_param(in): + * sort_param(in): + * parallel_num(in): + */ +static int +sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, + int parallel_num) +{ + int error = NO_ERROR; + int i = 0, j = 0, half_num_page; + PAGE_PTR page_p; + VPID next_vpid, prev_vpid, first_vpid[SORT_MAX_PARALLEL], last_vpid[SORT_MAX_PARALLEL]; + QFILE_LIST_SCAN_ID *scan_id_p; + SORT_INFO *sort_info_p, *org_sort_info_p; + + /* TO_DO : fix to not alloc in advance. */ + for (i = 0; i < sort_param->tot_tempfiles; i++) + { + db_private_free (thread_p, sort_param->file_contents[i].num_pages); + } + + /* TO_DO : Up to 4 in parallel. Expansion required. SORT_MAX_HALF_FILES */ + if (parallel_num > 4) + { + assert(0); + } + + /* copy temp file from parallel to main */ + for (int i = 0; i < parallel_num; i++) + { + sort_param->temp[i] = px_sort_param[i].temp[px_sort_param[i].px_result_file_idx]; + sort_param->file_contents[i] = px_sort_param[i].file_contents[px_sort_param[i].px_result_file_idx]; + } + + /* init file info */ + sort_param->px_result_file_idx = 0; + sort_param->half_files = parallel_num; + sort_param->tot_tempfiles = parallel_num * 2; + sort_param->in_half = 0; + + /* Create output temporary files make file and temporary volume page count estimates */ + int file_pg_cnt_est = sort_get_avg_numpages_of_nonempty_tmpfile (sort_param); + file_pg_cnt_est = MAX (1, file_pg_cnt_est); + + for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) + { + error = + sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted); + if (error != NO_ERROR) + { + return error; + } + } + + /* Merge the parallel processed results. */ + sort_param->px_max_index = 1; + if (sort_param->option == SORT_ELIM_DUP) + { + error = sort_exphase_merge_elim_dup (thread_p, sort_param); + } + else + { + /* SORT_DUP */ + error = sort_exphase_merge (thread_p, sort_param); + } + + return error; +} + /* * sort_write_area () - Write memory area to disk * return: From 32434453249e582fe4a5d80cafb24f3d2e026d27 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 12 Sep 2024 13:16:45 +0900 Subject: [PATCH 11/53] add thread wait --- src/storage/external_sort.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index b9f3d5f499a..3a7ced4457d 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1521,10 +1521,20 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* wait for threads */ /* TO_DO : no busy wait. need to block and wake up */ + int done; do { thread_sleep (10); - if (px_sort_param[0].px_status == 1 && px_sort_param[1].px_status == 1) + done = true; + for (int i = 0; i < parallel_num; i++) + { + if (px_sort_param[i].px_status != 1) + { + done = false; + break; + } + } + if (done) { break; } From b24ae695b20d19dd3b07ab0a4c693c6410010f9e Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 12 Sep 2024 19:13:37 +0900 Subject: [PATCH 12/53] fix memory leak --- src/base/resource_tracker.hpp | 2 -- src/storage/external_sort.c | 45 +++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/base/resource_tracker.hpp b/src/base/resource_tracker.hpp index cd3d1af6a6a..c78012ffc96 100644 --- a/src/base/resource_tracker.hpp +++ b/src/base/resource_tracker.hpp @@ -445,7 +445,6 @@ namespace cubbase void restrack_assert (bool cond) { -#if 0 #if !defined (NDEBUG) if (restrack_is_assert_suppressed ()) { @@ -456,7 +455,6 @@ namespace cubbase assert (cond); } #endif // NDEBUG -#endif } } // namespace cubbase diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 3a7ced4457d..da41339751a 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -98,6 +98,14 @@ #define IS_PARALLEL_EXECUTION(t) ((t)->px_max_index > 1) +enum parallel_type +{ + PX_SINGLE = 0, + PX_MAIN_IN_PARALLEL = 1, + PX_THREAD_IN_PARALLEL +}; +typedef enum parallel_type PARALLEL_TYPE; + typedef struct file_contents FILE_CONTENTS; struct file_contents { /* node of the file_contents linked list */ @@ -256,7 +264,7 @@ static int sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * so static int sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_put_result_from_tmpfile (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_get_avg_numpages_of_nonempty_tmpfile (SORT_PARAM * sort_param); -static void sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bool is_parallel); +static void sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PARALLEL_TYPE parallel_type); static int sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bool force_alloc, bool tde_encrypted); @@ -1565,17 +1573,12 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE { goto cleanup; } - - /* free px_sort_param */ - for (int i = 0; i < parallel_num; i++) - { - sort_return_used_resources (thread_p, &px_sort_param[i], true); - } } #else /* no parallel */ sort_param->px_max_index = 1; + is_parallel = false; error = sort_listfile_internal (thread_p, sort_param); #endif @@ -1584,8 +1587,21 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE CUBRID_SORT_END (sort_param->total_numrecs, error); #endif /* ENABLE_SYSTEMTAP */ - sort_return_used_resources (thread_p, sort_param, false); - sort_param = NULL; + /* free sort_param */ + if (is_parallel) + { + for (int i = 0; i < parallel_num; i++) + { + sort_return_used_resources (thread_p, &px_sort_param[i], PX_THREAD_IN_PARALLEL); + } + + sort_return_used_resources (thread_p, sort_param, PX_MAIN_IN_PARALLEL); + } + else + { + sort_return_used_resources (thread_p, sort_param, PX_SINGLE); + } + thread_set_sort_stats_active (thread_p, false); return error; @@ -4051,7 +4067,7 @@ sort_get_avg_numpages_of_nonempty_tmpfile (SORT_PARAM * sort_param) * memory areas and destroying any temporary files and volumes. */ static void -sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bool is_parallel) +sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PARALLEL_TYPE parallel_type) { int k; #if defined(SERVER_MODE) @@ -4068,7 +4084,7 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bo free_and_init (sort_param->internal_memory); } - if (!is_parallel) + if (parallel_type == PX_SINGLE || parallel_type == PX_MAIN_IN_PARALLEL) { for (k = 0; k < sort_param->tot_tempfiles; k++) { @@ -4084,7 +4100,7 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bo (void) file_temp_retire (thread_p, &(sort_param->multipage_file)); } - if (is_parallel) + if (parallel_type == PX_SINGLE || parallel_type == PX_THREAD_IN_PARALLEL) { for (k = 0; k < sort_param->tot_tempfiles; k++) { @@ -4095,9 +4111,8 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bo } } - if (is_parallel) + if (parallel_type == PX_THREAD_IN_PARALLEL) { - /* free memory */ for (int i = 0; i < sort_param->px_max_index; i++) { if (sort_param->get_arg != NULL) @@ -4126,7 +4141,7 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, bo */ #endif - if (!is_parallel) + if (parallel_type == PX_SINGLE || parallel_type == PX_MAIN_IN_PARALLEL) { free_and_init (sort_param); } From 997279d77ad6e4fc64a721792a19221368fe98bd Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 12 Sep 2024 21:40:35 +0900 Subject: [PATCH 13/53] fix big tuple --- src/storage/external_sort.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index da41339751a..92cb00c2b66 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1364,7 +1364,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* for parallel sort */ SORT_PARAM px_sort_param[SORT_MAX_PARALLEL]; /* TO_DO : need dynamic alloc */ QFILE_LIST_SCAN_ID *scan_id_p; - int parallel_num = 4; /* TO_DO : depending on the number of pages in the temp file */ + int parallel_num = 2; /* TO_DO : depending on the number of pages in the temp file */ thread_set_sort_stats_active (thread_p, true); @@ -1482,7 +1482,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_info_p = (SORT_INFO *) sort_param->get_arg; /* need to check the appropriate number of parallels depending on the number of pages */ - if (sort_info_p->input_file->page_cnt < 2) + if (sort_info_p->input_file->page_cnt < 2 || sort_info_p->input_file->tuple_cnt < 2) { is_parallel = false; } @@ -3182,7 +3182,7 @@ sort_put_result_from_tmpfile (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) int result_file_idx = sort_param->px_result_file_idx; RECDES record = RECDES_INITIALIZER; RECDES long_record = RECDES_INITIALIZER; - int error; + int error = NO_ERROR; SORT_REC *sort_rec; int tot_rows = 0; @@ -4308,7 +4308,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, int parallel_num) { int error = NO_ERROR; - int i = 0, j = 0, half_num_page; + int i = 0, j = 0, splitted_num_page; PAGE_PTR page_p; VPID next_vpid, prev_vpid, first_vpid[SORT_MAX_PARALLEL], last_vpid[SORT_MAX_PARALLEL]; QFILE_LIST_SCAN_ID *scan_id_p; @@ -4318,12 +4318,21 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, sort_info_p = (SORT_INFO *) sort_param->get_arg; scan_id_p = sort_info_p->s_id->s_id; - half_num_page = sort_info_p->input_file->page_cnt / parallel_num; - /* close input file for read. it'll be opened in parallel */ qfile_close_scan (thread_p, scan_id_p); + /* init vpid */ + for (i = 0; i < parallel_num; i++) + { + first_vpid[i] = VPID_INITIALIZER; + last_vpid[i] = VPID_INITIALIZER; + } + + splitted_num_page = sort_info_p->input_file->page_cnt / parallel_num; + splitted_num_page = MIN (splitted_num_page, sort_info_p->input_file->tuple_cnt / parallel_num); + /* find first and last vpid for splitted file */ + i = 0; prev_vpid = sort_info_p->input_file->first_vpid; while (true) { @@ -4333,7 +4342,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, { break; } - else if (++j >= half_num_page) + else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); first_vpid[i] = next_vpid; @@ -4386,7 +4395,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, } memcpy (sort_info_p->input_file, org_sort_info_p->input_file, sizeof (QFILE_LIST_ID)); sort_info_p->input_file->tuple_cnt = org_sort_info_p->input_file->tuple_cnt / parallel_num; - sort_info_p->input_file->page_cnt = half_num_page; + sort_info_p->input_file->page_cnt = splitted_num_page; /* TO_DO : Make the page count more accurate */ sort_info_p->input_file->first_vpid = (i == 0) ? org_sort_info_p->input_file->first_vpid : first_vpid[i - 1]; sort_info_p->input_file->last_vpid = (i == parallel_num - 1) ? org_sort_info_p->input_file->last_vpid : last_vpid[i]; From e83bc14b8f034e160f2926cd16edab5ad327bc8b Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 25 Sep 2024 16:03:55 +0900 Subject: [PATCH 14/53] slip --- src/storage/external_sort.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 92cb00c2b66..4e31168c88f 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1482,7 +1482,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_info_p = (SORT_INFO *) sort_param->get_arg; /* need to check the appropriate number of parallels depending on the number of pages */ - if (sort_info_p->input_file->page_cnt < 2 || sort_info_p->input_file->tuple_cnt < 2) + if (sort_info_p->input_file->page_cnt < 2 || sort_info_p->input_file->tuple_cnt < 2 || parallel_num < 2) { is_parallel = false; } @@ -4439,11 +4439,7 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param int parallel_num) { int error = NO_ERROR; - int i = 0, j = 0, half_num_page; - PAGE_PTR page_p; - VPID next_vpid, prev_vpid, first_vpid[SORT_MAX_PARALLEL], last_vpid[SORT_MAX_PARALLEL]; - QFILE_LIST_SCAN_ID *scan_id_p; - SORT_INFO *sort_info_p, *org_sort_info_p; + int i = 0; /* TO_DO : fix to not alloc in advance. */ for (i = 0; i < sort_param->tot_tempfiles; i++) @@ -4458,7 +4454,7 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param } /* copy temp file from parallel to main */ - for (int i = 0; i < parallel_num; i++) + for (i = 0; i < parallel_num; i++) { sort_param->temp[i] = px_sort_param[i].temp[px_sort_param[i].px_result_file_idx]; sort_param->file_contents[i] = px_sort_param[i].file_contents[px_sort_param[i].px_result_file_idx]; From 840b4aab0fa02f0bdc8c2dbadda71d27158952c6 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 26 Sep 2024 20:14:59 +0900 Subject: [PATCH 15/53] init prev page_id of first page when input file is splitted --- src/storage/external_sort.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 4e31168c88f..63ad8018014 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1482,7 +1482,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_info_p = (SORT_INFO *) sort_param->get_arg; /* need to check the appropriate number of parallels depending on the number of pages */ - if (sort_info_p->input_file->page_cnt < 2 || sort_info_p->input_file->tuple_cnt < 2 || parallel_num < 2) + if (sort_info_p->input_file->page_cnt < parallel_num || sort_info_p->input_file->tuple_cnt < parallel_num || parallel_num < 2) { is_parallel = false; } @@ -1507,6 +1507,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE { /* split input temp file. TO_DO : it can be inside sort_copy_sort_param() */ error = sort_split_input_temp_file (thread_p, px_sort_param, sort_param, parallel_num); + /* may need to revert the next page and prev page of temp file. */ if (error != NO_ERROR) { goto cleanup; @@ -4309,6 +4310,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, { int error = NO_ERROR; int i = 0, j = 0, splitted_num_page; + bool is_first_vpid = false; PAGE_PTR page_p; VPID next_vpid, prev_vpid, first_vpid[SORT_MAX_PARALLEL], last_vpid[SORT_MAX_PARALLEL]; QFILE_LIST_SCAN_ID *scan_id_p; @@ -4333,10 +4335,16 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* find first and last vpid for splitted file */ i = 0; + is_first_vpid = false; prev_vpid = sort_info_p->input_file->first_vpid; while (true) { page_p = qmgr_get_old_page (thread_p, &prev_vpid, sort_info_p->input_file->tfile_vfid); + if (is_first_vpid) + { + QFILE_PUT_PREV_VPID_NULL (page_p); + is_first_vpid = false; + } QFILE_GET_NEXT_VPID (&next_vpid, page_p); if (VPID_ISNULL (&next_vpid)) { @@ -4345,6 +4353,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); + is_first_vpid = true; first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; i++; @@ -4356,6 +4365,11 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else { qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); + + /* init prev page id */ + page_p = qmgr_get_old_page (thread_p, &next_vpid, sort_info_p->input_file->tfile_vfid); + QFILE_PUT_PREV_VPID_NULL (page_p); + qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } } From 07301fe77f7861603e737ec2e038b70e60106444 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 24 Oct 2024 15:53:05 +0900 Subject: [PATCH 16/53] change private_alloc to malloc to free memory by other thread --- src/storage/external_sort.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 63ad8018014..d3a185f7c13 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1452,7 +1452,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* Initilize file contents list */ sort_param->file_contents[i].num_pages = - (int *) db_private_alloc (thread_p, SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); + (int *) malloc (SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); if (sort_param->file_contents[i].num_pages == NULL) { sort_param->tot_tempfiles = i; @@ -4107,7 +4107,7 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PA { if (sort_param->file_contents[k].num_pages != NULL) { - db_private_free_and_init (thread_p, sort_param->file_contents[k].num_pages); + free_and_init (sort_param->file_contents[k].num_pages); } } } @@ -4226,6 +4226,12 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ int error = NO_ERROR; int i, j; + /* copy from origin sort param */ + for (i = 0; i < parallel_num; i++) + { + memcpy (&px_sort_param[i], sort_param, sizeof (SORT_PARAM)); + } + /* init */ for (i = 0; i < parallel_num; i++) { @@ -4236,10 +4242,9 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ } } - /* copy from origin sort param */ + /* alloc new memory */ for (i = 0; i < parallel_num; i++) { - memcpy (&px_sort_param[i], sort_param, sizeof (SORT_PARAM)); px_sort_param[i].internal_memory = (char *) malloc ((size_t) sort_param->tot_buffers * (size_t) DB_PAGESIZE); if (px_sort_param[i].internal_memory == NULL) { @@ -4251,7 +4256,7 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ { /* Initilize file contents list */ px_sort_param[i].file_contents[j].num_pages = - (int *) db_private_alloc (thread_p, SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); + (int *) malloc (SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); if (px_sort_param[i].file_contents[j].num_pages == NULL) { sort_param->tot_tempfiles = j; @@ -4288,7 +4293,7 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ { if (px_sort_param[i].file_contents[j].num_pages != NULL) { - db_private_free_and_init (thread_p, px_sort_param[i].file_contents[j].num_pages); + free_and_init (px_sort_param[i].file_contents[j].num_pages); } } } @@ -4455,12 +4460,6 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param int error = NO_ERROR; int i = 0; - /* TO_DO : fix to not alloc in advance. */ - for (i = 0; i < sort_param->tot_tempfiles; i++) - { - db_private_free (thread_p, sort_param->file_contents[i].num_pages); - } - /* TO_DO : Up to 4 in parallel. Expansion required. SORT_MAX_HALF_FILES */ if (parallel_num > 4) { @@ -4470,6 +4469,10 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param /* copy temp file from parallel to main */ for (i = 0; i < parallel_num; i++) { + /* free num_pages */ + free_and_init (sort_param->file_contents[i].num_pages); + + /* copy temp file and file contents */ sort_param->temp[i] = px_sort_param[i].temp[px_sort_param[i].px_result_file_idx]; sort_param->file_contents[i] = px_sort_param[i].file_contents[px_sort_param[i].px_result_file_idx]; } @@ -4875,7 +4878,7 @@ sort_run_add_new (FILE_CONTENTS * file_contents, int num_pages) { new_total_elements = ((int) (((float) file_contents->num_slots * SORT_EXPAND_DYN_ARRAY_RATIO) + 0.5)); file_contents->num_pages = - (int *) db_private_realloc (NULL, file_contents->num_pages, new_total_elements * sizeof (int)); + (int *) realloc (file_contents->num_pages, new_total_elements * sizeof (int)); if (file_contents->num_pages == NULL) { return ER_FAILED; From 1be195edf69798f51318bb9d4dde55191badece6 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 24 Oct 2024 17:06:29 +0900 Subject: [PATCH 17/53] fix to not add tran info for temporary file --- src/storage/external_sort.c | 10 +++++----- src/storage/file_manager.c | 9 +++++++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 15e1d41af4f..9d10c51535b 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4093,14 +4093,14 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PA { if (sort_param->temp[k].volid != NULL_VOLID) { - (void) file_temp_retire (thread_p, &sort_param->temp[k]); + (void) file_temp_retire_preserved (thread_p, &sort_param->temp[k]); } } } if (sort_param->multipage_file.volid != NULL_VOLID) { - (void) file_temp_retire (thread_p, &(sort_param->multipage_file)); + (void) file_temp_retire_preserved (thread_p, &(sort_param->multipage_file)); } if (parallel_type == PX_SINGLE || parallel_type == PX_THREAD_IN_PARALLEL) @@ -4188,7 +4188,7 @@ sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bo if (ret != NO_ERROR) { ASSERT_ERROR (); - file_temp_retire (thread_p, vfid); + file_temp_retire_preserved (thread_p, vfid); VFID_SET_NULL (vfid); return ret; } @@ -4206,7 +4206,7 @@ sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bo if (ret != NO_ERROR) { ASSERT_ERROR (); - file_temp_retire (thread_p, vfid); + file_temp_retire_preserved (thread_p, vfid); VFID_SET_NULL (vfid); return ret; } @@ -4769,7 +4769,7 @@ sort_checkalloc_numpages_of_outfiles (THREAD_ENTRY * thread_p, SORT_PARAM * sort /* If there is a file not to be used anymore, destroy it in order to reuse spaces. */ if (!VFID_ISNULL (&sort_param->temp[i])) { - error_code = file_temp_retire (thread_p, &sort_param->temp[i]); + error_code = file_temp_retire_preserved (thread_p, &sort_param->temp[i]); if (error_code != NO_ERROR) { ASSERT_ERROR (); diff --git a/src/storage/file_manager.c b/src/storage/file_manager.c index 39fa648ded9..e43a1e9f401 100644 --- a/src/storage/file_manager.c +++ b/src/storage/file_manager.c @@ -67,6 +67,7 @@ // XXX: SHOULD BE THE LAST INCLUDE HEADER #include "memory_wrapper.hpp" + /************************************************************************/ /* Define structures, globals, and macro's */ /************************************************************************/ @@ -3188,8 +3189,12 @@ file_create_temp_internal (THREAD_ENTRY * thread_p, int npages, FILE_TYPE ftype, *vfid_out = tempcache_entry->vfid; } - /* save to transaction temporary file list */ - file_tempcache_push_tran_file (thread_p, tempcache_entry); + /* if numerable then temporary file is intermediate file for sorting. No transaction info required */ + if (!is_numerable) + { + /* save to transaction temporary file list */ + file_tempcache_push_tran_file (thread_p, tempcache_entry); + } return NO_ERROR; } From 348138f047f03aa770ff8fa272ff61b20c52d39a Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 24 Oct 2024 17:25:49 +0900 Subject: [PATCH 18/53] indent --- src/storage/external_sort.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 9d10c51535b..cd59b2ba003 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -255,7 +255,7 @@ static int sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * dest_para static int sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); static int sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, - int parallel_num); + int parallel_num); #endif /* end parallel sort */ @@ -1453,8 +1453,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_param->temp[i].volid = NULL_VOLID; /* Initilize file contents list */ - sort_param->file_contents[i].num_pages = - (int *) malloc (SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); + sort_param->file_contents[i].num_pages = (int *) malloc (SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); if (sort_param->file_contents[i].num_pages == NULL) { sort_param->tot_tempfiles = i; @@ -1484,7 +1483,8 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_info_p = (SORT_INFO *) sort_param->get_arg; /* need to check the appropriate number of parallels depending on the number of pages */ - if (sort_info_p->input_file->page_cnt < parallel_num || sort_info_p->input_file->tuple_cnt < parallel_num || parallel_num < 2) + if (sort_info_p->input_file->page_cnt < parallel_num || sort_info_p->input_file->tuple_cnt < parallel_num + || parallel_num < 2) { is_parallel = false; } @@ -1525,8 +1525,8 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE for (int i = 0; i < parallel_num; i++) { cubthread::entry_callable_task * task = - new cubthread::entry_callable_task (std:: - bind (sort_listfile_execute, std::placeholders::_1, &px_sort_param[i])); + new cubthread:: + entry_callable_task (std::bind (sort_listfile_execute, std::placeholders::_1, &px_sort_param[i])); css_push_external_task (css_get_current_conn_entry (), task); } @@ -4257,8 +4257,7 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ for (j = 0; j < px_sort_param[i].tot_tempfiles; j++) { /* Initilize file contents list */ - px_sort_param[i].file_contents[j].num_pages = - (int *) malloc (SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); + px_sort_param[i].file_contents[j].num_pages = (int *) malloc (SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); if (px_sort_param[i].file_contents[j].num_pages == NULL) { sort_param->tot_tempfiles = j; @@ -4416,7 +4415,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, } memcpy (sort_info_p->input_file, org_sort_info_p->input_file, sizeof (QFILE_LIST_ID)); sort_info_p->input_file->tuple_cnt = org_sort_info_p->input_file->tuple_cnt / parallel_num; - sort_info_p->input_file->page_cnt = splitted_num_page; /* TO_DO : Make the page count more accurate */ + sort_info_p->input_file->page_cnt = splitted_num_page; /* TO_DO : Make the page count more accurate */ sort_info_p->input_file->first_vpid = (i == 0) ? org_sort_info_p->input_file->first_vpid : first_vpid[i - 1]; sort_info_p->input_file->last_vpid = (i == parallel_num - 1) ? org_sort_info_p->input_file->last_vpid : last_vpid[i]; @@ -4457,7 +4456,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, */ static int sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, - int parallel_num) + int parallel_num) { int error = NO_ERROR; int i = 0; @@ -4465,7 +4464,7 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param /* TO_DO : Up to 4 in parallel. Expansion required. SORT_MAX_HALF_FILES */ if (parallel_num > 4) { - assert(0); + assert (0); } /* copy temp file from parallel to main */ @@ -4491,8 +4490,7 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) { - error = - sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted); + error = sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted); if (error != NO_ERROR) { return error; @@ -4879,8 +4877,7 @@ sort_run_add_new (FILE_CONTENTS * file_contents, int num_pages) if (file_contents->last_run >= file_contents->num_slots) { new_total_elements = ((int) (((float) file_contents->num_slots * SORT_EXPAND_DYN_ARRAY_RATIO) + 0.5)); - file_contents->num_pages = - (int *) realloc (file_contents->num_pages, new_total_elements * sizeof (int)); + file_contents->num_pages = (int *) realloc (file_contents->num_pages, new_total_elements * sizeof (int)); if (file_contents->num_pages == NULL) { return ER_FAILED; From 4fdd6d2e1e2b734a8ca5db44b5a7fdd7eff6f559 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 24 Oct 2024 17:30:50 +0900 Subject: [PATCH 19/53] indent --- src/storage/external_sort.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index cd59b2ba003..a4b5f64cc20 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1525,8 +1525,8 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE for (int i = 0; i < parallel_num; i++) { cubthread::entry_callable_task * task = - new cubthread:: - entry_callable_task (std::bind (sort_listfile_execute, std::placeholders::_1, &px_sort_param[i])); + new cubthread::entry_callable_task (std:: + bind (sort_listfile_execute, std::placeholders::_1, &px_sort_param[i])); css_push_external_task (css_get_current_conn_entry (), task); } From 336755186b863d5311a167089a14507c72756a38 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 24 Oct 2024 18:45:41 +0900 Subject: [PATCH 20/53] add check for splitted temp file and comment --- src/storage/external_sort.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index a4b5f64cc20..d0437f00f95 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4314,6 +4314,7 @@ static int sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, int parallel_num) { + /* TO_DO : Need a logic to revert it? */ int error = NO_ERROR; int i = 0, j = 0, splitted_num_page; bool is_first_vpid = false; @@ -4336,6 +4337,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, last_vpid[i] = VPID_INITIALIZER; } + /* page_cnt contains ovfl_page. If the length of tuple is longer than page, split by the number of tuples. */ splitted_num_page = sort_info_p->input_file->page_cnt / parallel_num; splitted_num_page = MIN (splitted_num_page, sort_info_p->input_file->tuple_cnt / parallel_num); @@ -4383,6 +4385,12 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); } + if (i != parallel_num - 1) + { + error = ER_GENERIC_ERROR; + goto cleanup; + } + /* alloc get_arg */ for (i = 0; i < parallel_num; i++) { @@ -4414,12 +4422,13 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, goto cleanup; } memcpy (sort_info_p->input_file, org_sort_info_p->input_file, sizeof (QFILE_LIST_ID)); + /* tuple_cnt and page_cnt put approximately. */ + /* It can be put precisely through the page header, but not have to be precise for later process. */ sort_info_p->input_file->tuple_cnt = org_sort_info_p->input_file->tuple_cnt / parallel_num; - sort_info_p->input_file->page_cnt = splitted_num_page; /* TO_DO : Make the page count more accurate */ + sort_info_p->input_file->page_cnt = splitted_num_page; sort_info_p->input_file->first_vpid = (i == 0) ? org_sort_info_p->input_file->first_vpid : first_vpid[i - 1]; sort_info_p->input_file->last_vpid = (i == parallel_num - 1) ? org_sort_info_p->input_file->last_vpid : last_vpid[i]; - /* TO_DO : check to need to handle sort_info_p->in_file->tfile_vfid */ } cleanup: From 0c9d0efa2125a21aa5cf06bf96ac667f207f6d8d Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 24 Oct 2024 18:52:33 +0900 Subject: [PATCH 21/53] indent --- src/storage/external_sort.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index d0437f00f95..29b05c63afb 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1521,6 +1521,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE return -1; } + // *INDENT-OFF* /* parallel execute */ for (int i = 0; i < parallel_num; i++) { @@ -1529,6 +1530,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE bind (sort_listfile_execute, std::placeholders::_1, &px_sort_param[i])); css_push_external_task (css_get_current_conn_entry (), task); } + // *INDENT-ON* /* wait for threads */ /* TO_DO : no busy wait. need to block and wake up */ From 9e44f726fa24ad21a8ac535ff51abb94cc4aa30f Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 24 Oct 2024 19:42:02 +0900 Subject: [PATCH 22/53] slip --- src/storage/external_sort.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 29b05c63afb..e47b63d7cd0 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4387,13 +4387,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); } - if (i != parallel_num - 1) - { - error = ER_GENERIC_ERROR; - goto cleanup; - } - - /* alloc get_arg */ + /* add splitted file info */ for (i = 0; i < parallel_num; i++) { px_sort_param[i].get_arg = (void *) db_private_alloc (thread_p, sizeof (SORT_INFO)); From 0ffb02c213d5bdb094dbdc84176a2a73a2c8e9f3 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 24 Oct 2024 19:46:25 +0900 Subject: [PATCH 23/53] add set_dirty for temp file --- src/storage/external_sort.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index e47b63d7cd0..cdbe8c5f0ee 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4353,6 +4353,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, if (is_first_vpid) { QFILE_PUT_PREV_VPID_NULL (page_p); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = false; } QFILE_GET_NEXT_VPID (&next_vpid, page_p); @@ -4363,6 +4364,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = true; first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; @@ -4379,6 +4381,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* init prev page id */ page_p = qmgr_get_old_page (thread_p, &next_vpid, sort_info_p->input_file->tfile_vfid); QFILE_PUT_PREV_VPID_NULL (page_p); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } From e8ee99f42e85a74db078718fc7960bea6e849959 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Fri, 25 Oct 2024 16:42:00 +0900 Subject: [PATCH 24/53] remove pgbuf_set_dirty temporarily --- src/storage/external_sort.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index cdbe8c5f0ee..bdf07bf042a 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1477,6 +1477,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE SORT_INFO *sort_info_p; /* check parallelism */ +// is_parallel = sort_check_parallelism (thread_p, px_sort_param, sort_param, parallel_num); /* <== 여기 함수부터 작성 필요. 파라메터부터 입력 필요 */ if (sort_param->get_fn == qfile_get_next_sort_item) { /* get scan id of input file */ @@ -4353,7 +4354,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, if (is_first_vpid) { QFILE_PUT_PREV_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = false; } QFILE_GET_NEXT_VPID (&next_vpid, page_p); @@ -4364,7 +4365,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = true; first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; @@ -4381,7 +4382,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* init prev page id */ page_p = qmgr_get_old_page (thread_p, &next_vpid, sort_info_p->input_file->tfile_vfid); QFILE_PUT_PREV_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } From 9a89ef1596ee035b76349187510e99738ce60a10 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 28 Oct 2024 19:16:50 +0900 Subject: [PATCH 25/53] add sort_check_parallelism() --- src/query/list_file.c | 2 +- src/query/query_executor.c | 6 ++-- src/storage/btree_load.c | 2 +- src/storage/external_sort.c | 59 +++++++++++++++++++++++++------------ src/storage/external_sort.h | 10 ++++++- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/query/list_file.c b/src/query/list_file.c index 03a693f1a86..7dac979bee1 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -4042,7 +4042,7 @@ qfile_sort_list_with_func (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p, S sort_result = sort_listfile (thread_p, NULL_VOLID, estimated_pages, get_func, &info, put_func, &info, cmp_func, &info.key_info, - dup_option, limit, srlist_id->tfile_vfid->tde_encrypted, true); + dup_option, limit, srlist_id->tfile_vfid->tde_encrypted, SORT_ORDER_BY); if (sort_result < 0) { diff --git a/src/query/query_executor.c b/src/query/query_executor.c index ca1d63d51f4..afc0e9fef6b 100644 --- a/src/query/query_executor.c +++ b/src/query/query_executor.c @@ -4695,7 +4695,7 @@ qexec_groupby (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_stat /* sort and aggregate partial results */ if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_hash_gby_get_next, &gbstate, &qexec_hash_gby_put_next, &gbstate, cmp_fn, &gbstate.agg_hash_context->sort_key, SORT_DUP, - NO_SORT_LIMIT, gbstate.output_file->tfile_vfid->tde_encrypted, false) != NO_ERROR) + NO_SORT_LIMIT, gbstate.output_file->tfile_vfid->tde_encrypted, SORT_GROUP_BY) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } @@ -4776,7 +4776,7 @@ qexec_groupby (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * xasl_stat if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_gby_get_next, &gbstate, &qexec_gby_put_next, &gbstate, gbstate.cmp_fn, &gbstate.key_info, SORT_DUP, NO_SORT_LIMIT, - gbstate.output_file->tfile_vfid->tde_encrypted, false) != NO_ERROR) + gbstate.output_file->tfile_vfid->tde_encrypted, SORT_GROUP_BY) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } @@ -22208,7 +22208,7 @@ qexec_execute_analytic (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_analytic_get_next, &analytic_state, &qexec_analytic_put_next, &analytic_state, analytic_state.cmp_fn, &analytic_state.key_info, - SORT_DUP, NO_SORT_LIMIT, analytic_state.output_file->tfile_vfid->tde_encrypted, false) != NO_ERROR) + SORT_DUP, NO_SORT_LIMIT, analytic_state.output_file->tfile_vfid->tde_encrypted, SORT_ANALYTIC) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } diff --git a/src/storage/btree_load.c b/src/storage/btree_load.c index 616e53f131c..3f14767f56f 100644 --- a/src/storage/btree_load.c +++ b/src/storage/btree_load.c @@ -3217,7 +3217,7 @@ btree_index_sort (THREAD_ENTRY * thread_p, SORT_ARGS * sort_args, SORT_PUT_FUNC return sort_listfile (thread_p, sort_args->hfids[0].vfid.volid, 0 /* TODO - support parallelism */ , &btree_sort_get_next, sort_args, out_func, out_args, compare_driver, sort_args, SORT_DUP, - NO_SORT_LIMIT, includes_tde_class, false); + NO_SORT_LIMIT, includes_tde_class, SORT_INDEX_LEAF); } /* diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index bdf07bf042a..d84205ca25a 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -256,6 +256,8 @@ static int sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * des int parallel_num); static int sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); +static int sort_check_parallelism (THREAD_ENTRY * thread_p,SORT_PARALLEL_TYPE sort_parallel_type, SORT_PARAM * sort_param); + #endif /* end parallel sort */ @@ -1355,7 +1357,7 @@ sort_run_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, char **base, lo int sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GET_FUNC * get_fn, void *get_arg, SORT_PUT_FUNC * put_fn, void *put_arg, SORT_CMP_FUNC * cmp_fn, void *cmp_arg, SORT_DUP_OPTION option, - int limit, bool includes_tde_class, bool is_parallel) + int limit, bool includes_tde_class, SORT_PARALLEL_TYPE sort_parallel_type) { int error = NO_ERROR; @@ -1366,7 +1368,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* for parallel sort */ SORT_PARAM px_sort_param[SORT_MAX_PARALLEL]; /* TO_DO : need dynamic alloc */ QFILE_LIST_SCAN_ID *scan_id_p; - int parallel_num = 2; /* TO_DO : depending on the number of pages in the temp file */ + int parallel_num = 1; /* TO_DO : depending on the number of pages in the temp file */ thread_set_sort_stats_active (thread_p, true); @@ -1476,23 +1478,12 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE #if defined(SERVER_MODE) SORT_INFO *sort_info_p; - /* check parallelism */ -// is_parallel = sort_check_parallelism (thread_p, px_sort_param, sort_param, parallel_num); /* <== 여기 함수부터 작성 필요. 파라메터부터 입력 필요 */ - if (sort_param->get_fn == qfile_get_next_sort_item) - { - /* get scan id of input file */ - sort_info_p = (SORT_INFO *) sort_param->get_arg; + /* check the number of parallel process */ + parallel_num = sort_check_parallelism (thread_p, sort_parallel_type, sort_param); - /* need to check the appropriate number of parallels depending on the number of pages */ - if (sort_info_p->input_file->page_cnt < parallel_num || sort_info_p->input_file->tuple_cnt < parallel_num - || parallel_num < 2) - { - is_parallel = false; - } - } - - if (!is_parallel) + if (parallel_num == 1) { + /* single thread */ sort_param->px_max_index = 1; error = sort_listfile_internal (thread_p, sort_param); } @@ -1584,7 +1575,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE #else /* no parallel */ sort_param->px_max_index = 1; - is_parallel = false; + parallel_num = 1; error = sort_listfile_internal (thread_p, sort_param); #endif @@ -1594,7 +1585,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE #endif /* ENABLE_SYSTEMTAP */ /* free sort_param */ - if (is_parallel) + if (parallel_num > 1) { for (int i = 0; i < parallel_num; i++) { @@ -4521,6 +4512,36 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param return error; } +/* + * sort_check_parallelism () - check the number of parallel processes + * return: parallel_num + * sort_parallel_type(in): + * sort_param(in): + */ +static int +sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARALLEL_TYPE sort_parallel_type, SORT_PARAM * sort_param) +{ + SORT_INFO *sort_info_p; + + if (sort_parallel_type == SORT_ORDER_BY) + { + /* get scan id of input file */ + sort_info_p = (SORT_INFO *) sort_param->get_arg; + + /* Find the number of parallel processes by page_cnt and tuple_cnt */ + if (sort_info_p->input_file->page_cnt > 2 && sort_info_p->input_file->tuple_cnt > 2) + { + /* TO_DO : need to check the appropriate number of parallels depending on the number of pages */ + return 2; + } + } + else + { + /* Not implemented yet */ + return 1; + } +} + /* * sort_write_area () - Write memory area to disk * return: diff --git a/src/storage/external_sort.h b/src/storage/external_sort.h index 028a923cc3b..90af7863391 100644 --- a/src/storage/external_sort.h +++ b/src/storage/external_sort.h @@ -56,6 +56,14 @@ typedef enum SORT_DUP /* allow duplicate */ } SORT_DUP_OPTION; +typedef enum +{ + SORT_ORDER_BY, + SORT_GROUP_BY, + SORT_ANALYTIC, + SORT_INDEX_LEAF +} SORT_PARALLEL_TYPE; + typedef SORT_STATUS SORT_GET_FUNC (THREAD_ENTRY * thread_p, RECDES *, void *); typedef int SORT_PUT_FUNC (THREAD_ENTRY * thread_p, const RECDES *, void *); typedef int SORT_CMP_FUNC (const void *, const void *, void *); @@ -145,6 +153,6 @@ struct SORT_INFO extern int sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GET_FUNC * get_fn, void *get_arg, SORT_PUT_FUNC * put_fn, void *put_arg, SORT_CMP_FUNC * cmp_fn, void *cmp_arg, - SORT_DUP_OPTION option, int limit, bool includes_tde_class, bool is_parallel); + SORT_DUP_OPTION option, int limit, bool includes_tde_class, SORT_PARALLEL_TYPE sort_parallel_type); #endif /* _EXTERNAL_SORT_H_ */ From 6b98ae5ad56432dfdd2ea13ee4ed91bd0de60bad Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Tue, 29 Oct 2024 13:21:22 +0900 Subject: [PATCH 26/53] add start and end parallel --- src/query/list_file.c | 3 +- src/query/list_file.h | 1 - src/storage/external_sort.c | 187 +++++++++++++++++++++++------------- 3 files changed, 122 insertions(+), 69 deletions(-) diff --git a/src/query/list_file.c b/src/query/list_file.c index 7dac979bee1..b53095288b6 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -244,6 +244,7 @@ static QFILE_LIST_ID *qfile_union_list (THREAD_ENTRY * thread_p, QFILE_LIST_ID * int flag); static int qfile_put_next_sort_item (THREAD_ENTRY * thread_p, const RECDES * recdes, void *arg); +static SORT_STATUS qfile_get_next_sort_item (THREAD_ENTRY * thread_p, RECDES * recdes, void *arg); static SORT_INFO *qfile_initialize_sort_info (SORT_INFO * info, QFILE_LIST_ID * listid, SORT_LIST * sort_list); static void qfile_clear_sort_info (SORT_INFO * info); static int qfile_copy_list_pages (THREAD_ENTRY * thread_p, VPID * old_first_vpidp, QMGR_TEMP_FILE * old_tfile_vfidp, @@ -3370,7 +3371,7 @@ qfile_generate_sort_tuple (SORTKEY_INFO * key_info_p, SORT_REC * sort_record_p, * in the list file, or if an error occurs, the sorting module is informed with * necessary return codes. */ -SORT_STATUS +static SORT_STATUS qfile_get_next_sort_item (THREAD_ENTRY * thread_p, RECDES * recdes_p, void *arg) { SORT_INFO *sort_info_p; diff --git a/src/query/list_file.h b/src/query/list_file.h index 2b68bcffb91..c64af3cd39f 100644 --- a/src/query/list_file.h +++ b/src/query/list_file.h @@ -228,7 +228,6 @@ extern int qfile_overwrite_tuple (THREAD_ENTRY * thread_p, PAGE_PTR first_page, extern void qfile_update_qlist_count (THREAD_ENTRY * thread_p, const QFILE_LIST_ID * list_p, int inc); extern int qfile_get_list_cache_number_of_entries (int ht_no); extern bool qfile_has_no_cache_entries (); -extern SORT_STATUS qfile_get_next_sort_item (THREAD_ENTRY * thread_p, RECDES * recdes, void *arg); diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index d84205ca25a..2a7f362de5d 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -185,6 +185,7 @@ struct sort_param bool px_status; int px_result_file_idx; int px_tran_index; + SORT_PARALLEL_TYPE px_type; }; typedef struct sort_rec_list SORT_REC_LIST; @@ -256,8 +257,9 @@ static int sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * des int parallel_num); static int sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); -static int sort_check_parallelism (THREAD_ENTRY * thread_p,SORT_PARALLEL_TYPE sort_parallel_type, SORT_PARAM * sort_param); - +static int sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); +static int sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); +static int sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); #endif /* end parallel sort */ @@ -1357,7 +1359,7 @@ sort_run_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, char **base, lo int sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GET_FUNC * get_fn, void *get_arg, SORT_PUT_FUNC * put_fn, void *put_arg, SORT_CMP_FUNC * cmp_fn, void *cmp_arg, SORT_DUP_OPTION option, - int limit, bool includes_tde_class, SORT_PARALLEL_TYPE sort_parallel_type) + int limit, bool includes_tde_class, SORT_PARALLEL_TYPE parallel_type) { int error = NO_ERROR; @@ -1367,7 +1369,6 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* for parallel sort */ SORT_PARAM px_sort_param[SORT_MAX_PARALLEL]; /* TO_DO : need dynamic alloc */ - QFILE_LIST_SCAN_ID *scan_id_p; int parallel_num = 1; /* TO_DO : depending on the number of pages in the temp file */ thread_set_sort_stats_active (thread_p, true); @@ -1473,48 +1474,29 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_param->tmp_file_pgs = MAX (1, sort_param->tmp_file_pgs); sort_param->tde_encrypted = includes_tde_class; - + sort_param->px_type = parallel_type; #if defined(SERVER_MODE) - SORT_INFO *sort_info_p; - /* check the number of parallel process */ - parallel_num = sort_check_parallelism (thread_p, sort_parallel_type, sort_param); + parallel_num = sort_check_parallelism (thread_p, sort_param); if (parallel_num == 1) { - /* single thread */ + /* single process */ sort_param->px_max_index = 1; error = sort_listfile_internal (thread_p, sort_param); } else { - /* copy sort_param for parallel sort */ - error = sort_copy_sort_param (thread_p, px_sort_param, sort_param, parallel_num); + /* parallel process */ + error = sort_start_parallelism (thread_p, px_sort_param, sort_param, parallel_num); if (error != NO_ERROR) { goto cleanup; } - /* case of ORDER BY */ - if (sort_param->get_fn == qfile_get_next_sort_item) - { - /* split input temp file. TO_DO : it can be inside sort_copy_sort_param() */ - error = sort_split_input_temp_file (thread_p, px_sort_param, sort_param, parallel_num); - /* may need to revert the next page and prev page of temp file. */ - if (error != NO_ERROR) - { - goto cleanup; - } - } - else - { - /* not implemented yet (group by, analytic fuction, create index) */ - return -1; - } - + /* execute parallel sort */ // *INDENT-OFF* - /* parallel execute */ for (int i = 0; i < parallel_num; i++) { cubthread::entry_callable_task * task = @@ -1546,26 +1528,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE } while (1); - if (sort_param->get_fn == qfile_get_next_sort_item) - { - sort_info_p = (SORT_INFO *) sort_param->get_arg; - scan_id_p = sort_info_p->s_id->s_id; - - /* open origin temp file for read */ - if (qfile_open_list_scan (sort_info_p->input_file, scan_id_p) != NO_ERROR) - { - assert (0); - return -1; - } - } - else - { - /* not implemented yet (group by, analytic fuction, create index) */ - return -1; - } - - /* merging temp files from parallel processed */ - error = sort_merge_run_for_parallel (thread_p, px_sort_param, sort_param, parallel_num); + error = sort_end_parallelism (thread_p, px_sort_param, sort_param, parallel_num); if (error != NO_ERROR) { goto cleanup; @@ -1573,7 +1536,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE } #else - /* no parallel */ + /* single process for stand alone mode */ sort_param->px_max_index = 1; parallel_num = 1; error = sort_listfile_internal (thread_p, sort_param); @@ -1614,42 +1577,42 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) thread_ref.tran_index = sort_param->px_tran_index; pthread_mutex_unlock (&thread_ref.tran_index_lock); -/* printf ("sort_listfie index = %d\n",thread_ref.index); */ - if (sort_param->get_fn == qfile_get_next_sort_item) + if (sort_param->px_type == SORT_ORDER_BY) { SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; - /* temporarily, open file for read */ + /* open splitted temp file for read */ if (qfile_open_list_scan (sort_info_p->input_file, &t_scan_id) != NO_ERROR) { - /* need to put error into sort_param */ + sort_param->px_status = -1; return; } sort_info_p->s_id->s_id = &t_scan_id; } else { - /* need to put error into sort_param */ - assert(0); + /* Not implemented yet */ + sort_param->px_status = -1; return; } sort_listfile_internal (&thread_ref, sort_param); - /* temporarily, close file for read */ - if (sort_param->get_fn == qfile_get_next_sort_item) + if (sort_param->px_type == SORT_ORDER_BY) { SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; - /* temporarily, close file for read */ + /* close splitted temp file for read */ qfile_close_scan (&thread_ref, sort_info_p->s_id->s_id); } else { - /* need to put error into sort_param */ - assert(0); + /* Not implemented yet */ + sort_param->px_status = -1; return; } + + /* TO_DO : status enum */ sort_param->px_status = 1; } // *INDENT-ON* @@ -4319,10 +4282,6 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* get scan id of input file */ sort_info_p = (SORT_INFO *) sort_param->get_arg; - scan_id_p = sort_info_p->s_id->s_id; - - /* close input file for read. it'll be opened in parallel */ - qfile_close_scan (thread_p, scan_id_p); /* init vpid */ for (i = 0; i < parallel_num; i++) @@ -4519,11 +4478,11 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param * sort_param(in): */ static int -sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARALLEL_TYPE sort_parallel_type, SORT_PARAM * sort_param) +sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) { SORT_INFO *sort_info_p; - if (sort_parallel_type == SORT_ORDER_BY) + if (sort_param->px_type == SORT_ORDER_BY) { /* get scan id of input file */ sort_info_p = (SORT_INFO *) sort_param->get_arg; @@ -4542,6 +4501,100 @@ sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARALLEL_TYPE sort_paralle } } +/* + * sort_start_parallelism () - start parallelism + * return: NO_ERROR + * px_sort_param(in): + * sort_param(in): + * parallel_num(in): + * sort_parallel_type(in): + */ +static int +sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, + int parallel_num) +{ + int error = NO_ERROR; + + /* copy sort_param for parallel sort */ + error = sort_copy_sort_param (thread_p, px_sort_param, sort_param, parallel_num); + if (error != NO_ERROR) + { + return ER_FAILED; + } + + /* case of ORDER BY */ + if (sort_param->px_type == SORT_ORDER_BY) + { + QFILE_LIST_SCAN_ID *scan_id_p; + SORT_INFO *sort_info_p; + + /* get scan id of input file */ + sort_info_p = (SORT_INFO *) sort_param->get_arg; + scan_id_p = sort_info_p->s_id->s_id; + + /* close input file for read. it'll be opened in parallel */ + qfile_close_scan (thread_p, scan_id_p); + + /* split input temp file. TO_DO : If writing pages is not possible, need to find another way. */ + /* if possible, may need to revert the splitted pages */ + error = sort_split_input_temp_file (thread_p, px_sort_param, sort_param, parallel_num); + if (error != NO_ERROR) + { + return ER_FAILED; + } + } + else + { + /* not implemented yet (group by, analytic fuction, create index) */ + return ER_FAILED; + } + + return error; +} + +/* + * sort_end_parallelism () - end parallelism + * return: NO_ERROR + * px_sort_param(in): + * sort_param(in): + * parallel_num(in): + * sort_parallel_type(in): + */ +static int +sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, + int parallel_num) +{ + int error = NO_ERROR; + SORT_INFO *sort_info_p; + QFILE_LIST_SCAN_ID *scan_id_p; + + if (sort_param->px_type == SORT_ORDER_BY) + { + sort_info_p = (SORT_INFO *) sort_param->get_arg; + scan_id_p = sort_info_p->s_id->s_id; + + /* open origin temp file for read */ + if (qfile_open_list_scan (sort_info_p->input_file, scan_id_p) != NO_ERROR) + { + return ER_FAILED; + } + } + else + { + /* not implemented yet (group by, analytic fuction, create index) */ + return ER_FAILED; + } + + /* merging temp files from parallel processed */ + error = sort_merge_run_for_parallel (thread_p, px_sort_param, sort_param, parallel_num); + if (error != NO_ERROR) + { + return ER_FAILED; + } + + return error; +} + /* * sort_write_area () - Write memory area to disk * return: From 763bb4088f4e9f23f505c17c4c0313dea11ce7e5 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Tue, 29 Oct 2024 13:47:10 +0900 Subject: [PATCH 27/53] change private_alloc to malloc for freeing in main thread --- src/storage/external_sort.c | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 2a7f362de5d..965cdf0c8d1 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -177,9 +177,6 @@ struct sort_param unsigned int total_numrecs; /* support parallelism */ -#if defined(SERVER_MODE) - /* pthread_mutex_t px_mtx; /* px_node status mutex */ -#endif int px_max_index; int px_index; bool px_status; @@ -4080,27 +4077,17 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PA SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; if (sort_info_p->s_id != NULL) { - db_private_free_and_init (thread_p, sort_info_p->s_id); + free_and_init (sort_info_p->s_id); } if (sort_info_p->input_file != NULL) { - db_private_free_and_init (thread_p, sort_info_p->input_file); + free_and_init (sort_info_p->input_file); } - db_private_free_and_init (thread_p, sort_param->get_arg); + free_and_init (sort_param->get_arg); } } } -#if defined(SERVER_MODE) - /* temporary disable */ - /* rv = pthread_mutex_destroy (&(sort_param->px_mtx)); - if (rv != 0) - { - er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_CSS_PTHREAD_MUTEX_DESTROY, 0); - } - */ -#endif - if (parallel_type == PX_SINGLE || parallel_type == PX_MAIN_IN_PARALLEL) { free_and_init (sort_param); @@ -4344,7 +4331,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* add splitted file info */ for (i = 0; i < parallel_num; i++) { - px_sort_param[i].get_arg = (void *) db_private_alloc (thread_p, sizeof (SORT_INFO)); + px_sort_param[i].get_arg = (void *) malloc (sizeof (SORT_INFO)); if (px_sort_param[i].get_arg == NULL) { error = ER_OUT_OF_VIRTUAL_MEMORY; @@ -4357,7 +4344,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, sort_info_p->input_file = NULL; sort_info_p->s_id = NULL; - sort_info_p->s_id = (QFILE_SORT_SCAN_ID *) db_private_alloc (thread_p, sizeof (QFILE_SORT_SCAN_ID)); + sort_info_p->s_id = (QFILE_SORT_SCAN_ID *) malloc (sizeof (QFILE_SORT_SCAN_ID)); if (sort_info_p->s_id == NULL) { error = ER_OUT_OF_VIRTUAL_MEMORY; @@ -4365,7 +4352,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, } memcpy (sort_info_p->s_id, org_sort_info_p->s_id, sizeof (QFILE_SORT_SCAN_ID)); - sort_info_p->input_file = (QFILE_LIST_ID *) db_private_alloc (thread_p, sizeof (QFILE_LIST_ID)); + sort_info_p->input_file = (QFILE_LIST_ID *) malloc (sizeof (QFILE_LIST_ID)); if (sort_info_p->input_file == NULL) { error = ER_OUT_OF_VIRTUAL_MEMORY; @@ -4392,13 +4379,13 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, sort_info_p = (SORT_INFO *) px_sort_param[i].get_arg; if (sort_info_p->s_id != NULL) { - db_private_free_and_init (thread_p, sort_info_p->s_id); + free_and_init (sort_info_p->s_id); } if (sort_info_p->input_file != NULL) { - db_private_free_and_init (thread_p, sort_info_p->input_file); + free_and_init (sort_info_p->input_file); } - db_private_free_and_init (thread_p, px_sort_param[i].get_arg); + free_and_init (px_sort_param[i].get_arg); } } } From dfba094935e010305c630e4fe3c20edca94a0207 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Tue, 29 Oct 2024 14:46:49 +0900 Subject: [PATCH 28/53] slip --- src/storage/external_sort.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 965cdf0c8d1..c5bec054ac6 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1477,7 +1477,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* check the number of parallel process */ parallel_num = sort_check_parallelism (thread_p, sort_param); - if (parallel_num == 1) + if (parallel_num <= 1) { /* single process */ sort_param->px_max_index = 1; @@ -4417,7 +4417,7 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param for (i = 0; i < parallel_num; i++) { /* free num_pages */ - free_and_init (sort_param->file_contents[i].num_pages); + free_and_init (sort_param->file_contents[i].num_pages); /* 미리 동적할당 안되게 변경해야함. 지금은 px_sort만 해제하고 있다. */ /* copy temp file and file contents */ sort_param->temp[i] = px_sort_param[i].temp[px_sort_param[i].px_result_file_idx]; @@ -4486,6 +4486,9 @@ sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* Not implemented yet */ return 1; } + + /* single process */ + return 1; } /* From 74d67dc9f9eb6a3ea7b3134d6068b2fdf1e00e13 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 30 Oct 2024 12:50:56 +0900 Subject: [PATCH 29/53] revert fix mode to PGBUF_LATCH_WRITE --- src/query/query_manager.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/query_manager.c b/src/query/query_manager.c index ea17222c138..4ef4f637dbf 100644 --- a/src/query/query_manager.c +++ b/src/query/query_manager.c @@ -2469,7 +2469,7 @@ qmgr_get_old_page (THREAD_ENTRY * thread_p, VPID * vpid_p, QMGR_TEMP_FILE * tfil else { /* return temp file page */ - page_p = pgbuf_fix (thread_p, vpid_p, OLD_PAGE, PGBUF_LATCH_READ, PGBUF_UNCONDITIONAL_LATCH); + page_p = pgbuf_fix (thread_p, vpid_p, OLD_PAGE, PGBUF_LATCH_WRITE, PGBUF_UNCONDITIONAL_LATCH); if (page_p != NULL) { From 6a56eb02206ee32d1be924e40ecf9c320b1626a4 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 30 Oct 2024 17:11:35 +0900 Subject: [PATCH 30/53] fix memory leak --- src/storage/external_sort.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index c5bec054ac6..eaab47a341f 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4413,15 +4413,14 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param assert (0); } - /* copy temp file from parallel to main */ - for (i = 0; i < parallel_num; i++) + /* free num_pages */ + for (i = 0; i < sort_param->tot_tempfiles; i++) { - /* free num_pages */ - free_and_init (sort_param->file_contents[i].num_pages); /* 미리 동적할당 안되게 변경해야함. 지금은 px_sort만 해제하고 있다. */ + free_and_init (sort_param->file_contents[i].num_pages); - /* copy temp file and file contents */ - sort_param->temp[i] = px_sort_param[i].temp[px_sort_param[i].px_result_file_idx]; - sort_param->file_contents[i] = px_sort_param[i].file_contents[px_sort_param[i].px_result_file_idx]; + sort_param->file_contents[i].num_slots = SORT_INITIAL_DYN_ARRAY_SIZE; + sort_param->file_contents[i].first_run = -1; + sort_param->file_contents[i].last_run = -1; } /* init file info */ @@ -4430,6 +4429,24 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param sort_param->tot_tempfiles = parallel_num * 2; sort_param->in_half = 0; + /* copy temp file and file contents */ + for (i = 0; i < sort_param->half_files; i++) + { + sort_param->temp[i] = px_sort_param[i].temp[px_sort_param[i].px_result_file_idx]; + sort_param->file_contents[i] = px_sort_param[i].file_contents[px_sort_param[i].px_result_file_idx]; + } + for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) + { + /* init temp file and contents */ + px_sort_param[0].temp[i].volid = NULL_VOLID; + px_sort_param[0].file_contents[i].first_run = -1; + px_sort_param[0].file_contents[i].last_run = -1; + + /* copy from thread 0 */ + sort_param->temp[i] = px_sort_param[0].temp[i]; + sort_param->file_contents[i] = px_sort_param[0].file_contents[i]; + } + /* Create output temporary files make file and temporary volume page count estimates */ int file_pg_cnt_est = sort_get_avg_numpages_of_nonempty_tmpfile (sort_param); file_pg_cnt_est = MAX (1, file_pg_cnt_est); From c02ca4a45bd1b132c9aad97eeded8262fa495e56 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 30 Oct 2024 17:26:08 +0900 Subject: [PATCH 31/53] revert pgbuf_set_dirty --- src/storage/external_sort.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index eaab47a341f..d53f1c5957a 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4054,7 +4054,7 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PA if (sort_param->multipage_file.volid != NULL_VOLID) { - (void) file_temp_retire_preserved (thread_p, &(sort_param->multipage_file)); + (void) file_temp_retire (thread_p, &(sort_param->multipage_file)); } if (parallel_type == PX_SINGLE || parallel_type == PX_THREAD_IN_PARALLEL) @@ -4291,7 +4291,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, if (is_first_vpid) { QFILE_PUT_PREV_VPID_NULL (page_p); - //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = false; } QFILE_GET_NEXT_VPID (&next_vpid, page_p); @@ -4302,7 +4302,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); - //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = true; first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; @@ -4319,7 +4319,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* init prev page id */ page_p = qmgr_get_old_page (thread_p, &next_vpid, sort_info_p->input_file->tfile_vfid); QFILE_PUT_PREV_VPID_NULL (page_p); - //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } From 1c95c40a607d2a093196ebe954e0b19a7efd308a Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 30 Oct 2024 17:48:44 +0900 Subject: [PATCH 32/53] revert set_dirty --- src/storage/external_sort.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index d53f1c5957a..0e16e4f9d57 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4291,7 +4291,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, if (is_first_vpid) { QFILE_PUT_PREV_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = false; } QFILE_GET_NEXT_VPID (&next_vpid, page_p); @@ -4302,7 +4302,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = true; first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; @@ -4319,7 +4319,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* init prev page id */ page_p = qmgr_get_old_page (thread_p, &next_vpid, sort_info_p->input_file->tfile_vfid); QFILE_PUT_PREV_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } From 34292dc8d3e9a01f5d33e87ed1dac7b5426855ac Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Fri, 1 Nov 2024 16:32:59 +0900 Subject: [PATCH 33/53] add resource_tracker in parallel thread --- src/storage/external_sort.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 0e16e4f9d57..fe8941d4603 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1570,10 +1570,13 @@ static void sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) { QFILE_LIST_SCAN_ID t_scan_id; + THREAD_ENTRY * thread_p = &thread_ref; thread_ref.tran_index = sort_param->px_tran_index; pthread_mutex_unlock (&thread_ref.tran_index_lock); + thread_p->push_resource_tracks (); + if (sort_param->px_type == SORT_ORDER_BY) { SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; @@ -1611,6 +1614,8 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) /* TO_DO : status enum */ sort_param->px_status = 1; + + thread_p->pop_resource_tracks (); } // *INDENT-ON* #endif From 849eecd64cc0bbb9e378cf4f191fe03f624e298b Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Fri, 1 Nov 2024 17:01:20 +0900 Subject: [PATCH 34/53] revert private_alloc --- src/storage/external_sort.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index fe8941d4603..d262b8067ad 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4082,13 +4082,13 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PA SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; if (sort_info_p->s_id != NULL) { - free_and_init (sort_info_p->s_id); + db_private_free_and_init (thread_p, sort_info_p->s_id); } if (sort_info_p->input_file != NULL) { - free_and_init (sort_info_p->input_file); + db_private_free_and_init (thread_p, sort_info_p->input_file); } - free_and_init (sort_param->get_arg); + db_private_free_and_init (thread_p, sort_param->get_arg); } } } @@ -4336,7 +4336,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* add splitted file info */ for (i = 0; i < parallel_num; i++) { - px_sort_param[i].get_arg = (void *) malloc (sizeof (SORT_INFO)); + px_sort_param[i].get_arg = (void *) db_private_alloc (thread_p, sizeof (SORT_INFO)); if (px_sort_param[i].get_arg == NULL) { error = ER_OUT_OF_VIRTUAL_MEMORY; @@ -4349,7 +4349,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, sort_info_p->input_file = NULL; sort_info_p->s_id = NULL; - sort_info_p->s_id = (QFILE_SORT_SCAN_ID *) malloc (sizeof (QFILE_SORT_SCAN_ID)); + sort_info_p->s_id = (QFILE_SORT_SCAN_ID *) db_private_alloc (thread_p, sizeof (QFILE_SORT_SCAN_ID)); if (sort_info_p->s_id == NULL) { error = ER_OUT_OF_VIRTUAL_MEMORY; @@ -4357,7 +4357,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, } memcpy (sort_info_p->s_id, org_sort_info_p->s_id, sizeof (QFILE_SORT_SCAN_ID)); - sort_info_p->input_file = (QFILE_LIST_ID *) malloc (sizeof (QFILE_LIST_ID)); + sort_info_p->input_file = (QFILE_LIST_ID *) db_private_alloc (thread_p, sizeof (QFILE_LIST_ID)); if (sort_info_p->input_file == NULL) { error = ER_OUT_OF_VIRTUAL_MEMORY; @@ -4384,13 +4384,13 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, sort_info_p = (SORT_INFO *) px_sort_param[i].get_arg; if (sort_info_p->s_id != NULL) { - free_and_init (sort_info_p->s_id); + db_private_free_and_init (thread_p, sort_info_p->s_id); } if (sort_info_p->input_file != NULL) { - free_and_init (sort_info_p->input_file); + db_private_free_and_init (thread_p, sort_info_p->input_file); } - free_and_init (px_sort_param[i].get_arg); + db_private_free_and_init (thread_p, px_sort_param[i].get_arg); } } } From fb728f2f8fbbafb4e871fb07a44fb1c71f82abac Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Fri, 1 Nov 2024 18:34:49 +0900 Subject: [PATCH 35/53] slip --- src/storage/external_sort.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index d262b8067ad..94c19110935 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4302,6 +4302,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, QFILE_GET_NEXT_VPID (&next_vpid, page_p); if (VPID_ISNULL (&next_vpid)) { + qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } else if (++j >= splitted_num_page) From 63b33b724f71d40c5052d74ee38f01fe8a85c071 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Fri, 1 Nov 2024 18:47:41 +0900 Subject: [PATCH 36/53] indent --- src/query/query_executor.c | 3 ++- src/storage/external_sort.c | 12 ++++++------ src/storage/external_sort.h | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/query/query_executor.c b/src/query/query_executor.c index afc0e9fef6b..7f454f3a1e3 100644 --- a/src/query/query_executor.c +++ b/src/query/query_executor.c @@ -22208,7 +22208,8 @@ qexec_execute_analytic (THREAD_ENTRY * thread_p, XASL_NODE * xasl, XASL_STATE * if (sort_listfile (thread_p, NULL_VOLID, estimated_pages, &qexec_analytic_get_next, &analytic_state, &qexec_analytic_put_next, &analytic_state, analytic_state.cmp_fn, &analytic_state.key_info, - SORT_DUP, NO_SORT_LIMIT, analytic_state.output_file->tfile_vfid->tde_encrypted, SORT_ANALYTIC) != NO_ERROR) + SORT_DUP, NO_SORT_LIMIT, analytic_state.output_file->tfile_vfid->tde_encrypted, + SORT_ANALYTIC) != NO_ERROR) { GOTO_EXIT_ON_ERROR; } diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 94c19110935..8a073930636 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -255,8 +255,10 @@ static int sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * des static int sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); static int sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); -static int sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); -static int sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); +static int sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, + int parallel_num); +static int sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, + int parallel_num); #endif /* end parallel sort */ @@ -4523,8 +4525,7 @@ sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) * sort_parallel_type(in): */ static int -sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, - int parallel_num) +sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, int parallel_num) { int error = NO_ERROR; @@ -4574,8 +4575,7 @@ sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SOR * sort_parallel_type(in): */ static int -sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, - int parallel_num) +sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_PARAM * sort_param, int parallel_num) { int error = NO_ERROR; SORT_INFO *sort_info_p; diff --git a/src/storage/external_sort.h b/src/storage/external_sort.h index 90af7863391..f51915b753d 100644 --- a/src/storage/external_sort.h +++ b/src/storage/external_sort.h @@ -153,6 +153,7 @@ struct SORT_INFO extern int sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GET_FUNC * get_fn, void *get_arg, SORT_PUT_FUNC * put_fn, void *put_arg, SORT_CMP_FUNC * cmp_fn, void *cmp_arg, - SORT_DUP_OPTION option, int limit, bool includes_tde_class, SORT_PARALLEL_TYPE sort_parallel_type); + SORT_DUP_OPTION option, int limit, bool includes_tde_class, + SORT_PARALLEL_TYPE sort_parallel_type); #endif /* _EXTERNAL_SORT_H_ */ From 52f3ff325510aebb2ab61ce70c1d1548dcd5c6e2 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Fri, 1 Nov 2024 19:24:22 +0900 Subject: [PATCH 37/53] enable pgbuf_set_dirty() --- src/storage/external_sort.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 8a073930636..9e24407324b 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4298,7 +4298,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, if (is_first_vpid) { QFILE_PUT_PREV_VPID_NULL (page_p); - //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = false; } QFILE_GET_NEXT_VPID (&next_vpid, page_p); @@ -4310,7 +4310,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); - //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); is_first_vpid = true; first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; @@ -4327,7 +4327,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* init prev page id */ page_p = qmgr_get_old_page (thread_p, &next_vpid, sort_info_p->input_file->tfile_vfid); QFILE_PUT_PREV_VPID_NULL (page_p); - //pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + pgbuf_set_dirty (thread_p, page_p, DONT_FREE); qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } From 42552ce15d9c20c0012212c7e885a534d1ab7cba Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 4 Nov 2024 10:34:57 +0900 Subject: [PATCH 38/53] change pgbuf_set_dirty to qfile_set_dirty_page for memory buffer --- src/storage/external_sort.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 9e24407324b..406fbd88c98 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4298,7 +4298,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, if (is_first_vpid) { QFILE_PUT_PREV_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + qfile_set_dirty_page (thread_p, page_p, DONT_FREE, sort_info_p->input_file->tfile_vfid); is_first_vpid = false; } QFILE_GET_NEXT_VPID (&next_vpid, page_p); @@ -4310,7 +4310,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + qfile_set_dirty_page (thread_p, page_p, DONT_FREE, sort_info_p->input_file->tfile_vfid); is_first_vpid = true; first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; @@ -4327,7 +4327,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* init prev page id */ page_p = qmgr_get_old_page (thread_p, &next_vpid, sort_info_p->input_file->tfile_vfid); QFILE_PUT_PREV_VPID_NULL (page_p); - pgbuf_set_dirty (thread_p, page_p, DONT_FREE); + qfile_set_dirty_page (thread_p, page_p, DONT_FREE, sort_info_p->input_file->tfile_vfid); qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } From 2f57e120863270fa7a38c54ed2d45d5eb2a7b728 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 4 Nov 2024 18:43:52 +0900 Subject: [PATCH 39/53] change qfile_set_dirty_page to qmgr_set_dirty_page --- src/query/query_manager.c | 10 ++++++++++ src/storage/external_sort.c | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/query/query_manager.c b/src/query/query_manager.c index 4ef4f637dbf..560d3766050 100644 --- a/src/query/query_manager.c +++ b/src/query/query_manager.c @@ -2537,6 +2537,16 @@ qmgr_set_dirty_page (THREAD_ENTRY * thread_p, PAGE_PTR page_p, int free_page, LO QMGR_TEMP_FILE * tfile_vfid_p) { QMGR_PAGE_TYPE page_type; + LOG_DATA_ADDR addr; + + if (addr_p == NULL) + { + addr.vfid = NULL; + addr.pgptr = page_p; + addr.offset = -1; + + addr_p = &addr; + } page_type = qmgr_get_page_type (page_p, tfile_vfid_p); if (page_type == QMGR_UNKNOWN_PAGE) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 406fbd88c98..27a25623ceb 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4298,7 +4298,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, if (is_first_vpid) { QFILE_PUT_PREV_VPID_NULL (page_p); - qfile_set_dirty_page (thread_p, page_p, DONT_FREE, sort_info_p->input_file->tfile_vfid); + qmgr_set_dirty_page (thread_p, page_p, DONT_FREE, NULL, sort_info_p->input_file->tfile_vfid); is_first_vpid = false; } QFILE_GET_NEXT_VPID (&next_vpid, page_p); @@ -4310,7 +4310,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, else if (++j >= splitted_num_page) { QFILE_PUT_NEXT_VPID_NULL (page_p); - qfile_set_dirty_page (thread_p, page_p, DONT_FREE, sort_info_p->input_file->tfile_vfid); + qmgr_set_dirty_page (thread_p, page_p, DONT_FREE, NULL, sort_info_p->input_file->tfile_vfid); is_first_vpid = true; first_vpid[i] = next_vpid; last_vpid[i] = prev_vpid; @@ -4327,7 +4327,7 @@ sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, /* init prev page id */ page_p = qmgr_get_old_page (thread_p, &next_vpid, sort_info_p->input_file->tfile_vfid); QFILE_PUT_PREV_VPID_NULL (page_p); - qfile_set_dirty_page (thread_p, page_p, DONT_FREE, sort_info_p->input_file->tfile_vfid); + qmgr_set_dirty_page (thread_p, page_p, DONT_FREE, NULL, sort_info_p->input_file->tfile_vfid); qmgr_free_old_page_and_init (thread_p, page_p, sort_info_p->input_file->tfile_vfid); break; } From cb4195e3911eb66f9e5a97edd6d25a6885800109 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 6 Nov 2024 12:20:43 +0900 Subject: [PATCH 40/53] revert pgbuf_unfix_all() --- src/storage/page_buffer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/storage/page_buffer.c b/src/storage/page_buffer.c index 269e3015323..fea7e8f9bfb 100644 --- a/src/storage/page_buffer.c +++ b/src/storage/page_buffer.c @@ -2661,7 +2661,7 @@ pgbuf_unfix_all (THREAD_ENTRY * thread_p) holder = thrd_holder_info->thrd_hold_list; while (holder != NULL) { - /* assert (false); */ + assert (false); CAST_BFPTR_TO_PGPTR (pgptr, holder->bufptr); @@ -2673,7 +2673,7 @@ pgbuf_unfix_all (THREAD_ENTRY * thread_p) holder = thrd_holder_info->thrd_hold_list; #else /* NDEBUG */ CAST_PGPTR_TO_BFPTR (bufptr, pgptr); - /* assert (!VPID_ISNULL (&bufptr->vpid)); */ + assert (!VPID_ISNULL (&bufptr->vpid)); latch_mode_str = pgbuf_latch_mode_str (bufptr->latch_mode); zone_str = pgbuf_zone_str (pgbuf_bcb_get_zone (bufptr)); From c588ff651718c40323693559f5642bf57dbc8e01 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Tue, 19 Nov 2024 14:27:14 +0900 Subject: [PATCH 41/53] add local mutex for temp file to avoid broken Linked list in parallel process --- src/query/list_file.c | 2 +- src/query/query_manager.c | 12 ++-- src/storage/external_sort.c | 31 +++++---- src/storage/file_manager.c | 131 ++++++++++++++++++++++++++++++------ src/storage/file_manager.h | 6 +- 5 files changed, 140 insertions(+), 42 deletions(-) diff --git a/src/query/list_file.c b/src/query/list_file.c index b53095288b6..d3357fe4c92 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -2230,7 +2230,7 @@ qfile_destroy_list (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p) /* because qmgr_free_list_temp_file() destroy only FILE_TEMP file */ if (!VFID_ISNULL (&list_id_p->temp_vfid)) { - file_temp_retire (thread_p, &list_id_p->temp_vfid); + file_temp_retire (thread_p, &list_id_p->temp_vfid, false); } } diff --git a/src/query/query_manager.c b/src/query/query_manager.c index 560d3766050..6433727a265 100644 --- a/src/query/query_manager.c +++ b/src/query/query_manager.c @@ -2602,7 +2602,7 @@ qmgr_get_new_page (THREAD_ENTRY * thread_p, VPID * vpid_p, QMGR_TEMP_FILE * tfil if (VFID_ISNULL (&tfile_vfid_p->temp_vfid)) { TDE_ALGORITHM tde_algo = TDE_ALGORITHM_NONE; - if (file_create_temp (thread_p, 1, &tfile_vfid_p->temp_vfid) != NO_ERROR) + if (file_create_temp (thread_p, 1, &tfile_vfid_p->temp_vfid, false) != NO_ERROR) { ASSERT_ERROR (); return NULL; @@ -2617,7 +2617,7 @@ qmgr_get_new_page (THREAD_ENTRY * thread_p, VPID * vpid_p, QMGR_TEMP_FILE * tfil if (file_apply_tde_algorithm (thread_p, &tfile_vfid_p->temp_vfid, tde_algo) != NO_ERROR) { ASSERT_ERROR (); - file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid); + file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid, false); VFID_SET_NULL (&tfile_vfid_p->temp_vfid); return NULL; } @@ -2881,7 +2881,7 @@ qmgr_create_result_file (THREAD_ENTRY * thread_p, QUERY_ID query_id) { /* query entry is not found */ er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, ER_QPROC_UNKNOWN_QUERYID, 1, query_id); - file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid); + file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid, false); free_and_init (tfile_vfid_p); return NULL; } @@ -2894,7 +2894,7 @@ qmgr_create_result_file (THREAD_ENTRY * thread_p, QUERY_ID query_id) if (file_apply_tde_algorithm (thread_p, &tfile_vfid_p->temp_vfid, tde_algo) != NO_ERROR) { - file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid); + file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid, false); free_and_init (tfile_vfid_p); return NULL; } @@ -2964,7 +2964,7 @@ qmgr_free_temp_file_list (THREAD_ENTRY * thread_p, QMGR_TEMP_FILE * tfile_vfid_p } else { - fd_ret = file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid); + fd_ret = file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid, false); if (fd_ret != NO_ERROR) { /* set error but continue with the destroy process */ @@ -3105,7 +3105,7 @@ qmgr_free_list_temp_file (THREAD_ENTRY * thread_p, QUERY_ID query_id, QMGR_TEMP_ rc = ER_FAILED; } } - else if (file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid) != NO_ERROR) + else if (file_temp_retire (thread_p, &tfile_vfid_p->temp_vfid, false) != NO_ERROR) { /* stop; return error */ rc = ER_FAILED; diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 27a25623ceb..486bbe1d025 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -271,7 +271,7 @@ static int sort_put_result_from_tmpfile (THREAD_ENTRY * thread_p, SORT_PARAM * s static int sort_get_avg_numpages_of_nonempty_tmpfile (SORT_PARAM * sort_param); static void sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PARALLEL_TYPE parallel_type); static int sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bool force_alloc, - bool tde_encrypted); + bool tde_encrypted, bool is_parallel); static int sort_write_area (THREAD_ENTRY * thread_p, VFID * vfid, int first_page, INT32 num_pages, char *area_start, bool tde_encrypted); @@ -1657,7 +1657,8 @@ sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) { error = - sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted); + sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted, + IS_PARALLEL_EXECUTION (sort_param)); if (error != NO_ERROR) { return error; @@ -1964,7 +1965,8 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU /* Create the multipage file */ sort_param->multipage_file.volid = sort_param->temp[0].volid; - error = file_create_temp (thread_p, 1, &sort_param->multipage_file); + error = + file_create_temp (thread_p, 1, &sort_param->multipage_file, IS_PARALLEL_EXECUTION (sort_param)); if (error != NO_ERROR) { ASSERT_ERROR (); @@ -1976,7 +1978,7 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU } if (file_apply_tde_algorithm (thread_p, &sort_param->multipage_file, tde_algo) != NO_ERROR) { - file_temp_retire (thread_p, &sort_param->multipage_file); + file_temp_retire (thread_p, &sort_param->multipage_file, IS_PARALLEL_EXECUTION (sort_param)); ASSERT_ERROR (); goto exit_on_error; } @@ -2189,7 +2191,7 @@ sort_run_flush (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, int out_file, { error = sort_add_new_file (thread_p, &sort_param->temp[out_file], sort_param->tmp_file_pgs, false, - sort_param->tde_encrypted); + sort_param->tde_encrypted, IS_PARALLEL_EXECUTION (sort_param)); if (error != NO_ERROR) { return error; @@ -4054,14 +4056,14 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PA { if (sort_param->temp[k].volid != NULL_VOLID) { - (void) file_temp_retire_preserved (thread_p, &sort_param->temp[k]); + (void) file_temp_retire (thread_p, &sort_param->temp[k], false); } } } if (sort_param->multipage_file.volid != NULL_VOLID) { - (void) file_temp_retire (thread_p, &(sort_param->multipage_file)); + (void) file_temp_retire (thread_p, &(sort_param->multipage_file), false); } if (parallel_type == PX_SINGLE || parallel_type == PX_THREAD_IN_PARALLEL) @@ -4110,7 +4112,8 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PA * tde_encrypted(in): whether the file has to be encrypted or not for TDE */ static int -sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bool force_alloc, bool tde_encrypted) +sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bool force_alloc, bool tde_encrypted, + bool is_parallel) { VPID new_vpid; TDE_ALGORITHM tde_algo = TDE_ALGORITHM_NONE; @@ -4119,7 +4122,7 @@ sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bo /* todo: sort file is a case I missed that seems to use file_find_nthpages. I don't know if it can be optimized to * work without numerable files, that remains to be seen. */ - ret = file_create_temp_numerable (thread_p, file_pg_cnt_est, vfid); + ret = file_create_temp_numerable (thread_p, file_pg_cnt_est, vfid, is_parallel); if (ret != NO_ERROR) { ASSERT_ERROR (); @@ -4139,7 +4142,7 @@ sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bo if (ret != NO_ERROR) { ASSERT_ERROR (); - file_temp_retire_preserved (thread_p, vfid); + file_temp_retire (thread_p, vfid, is_parallel); VFID_SET_NULL (vfid); return ret; } @@ -4157,7 +4160,7 @@ sort_add_new_file (THREAD_ENTRY * thread_p, VFID * vfid, int file_pg_cnt_est, bo if (ret != NO_ERROR) { ASSERT_ERROR (); - file_temp_retire_preserved (thread_p, vfid); + file_temp_retire (thread_p, vfid, is_parallel); VFID_SET_NULL (vfid); return ret; } @@ -4461,7 +4464,9 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) { - error = sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted); + error = + sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted, + IS_PARALLEL_EXECUTION (sort_param)); if (error != NO_ERROR) { return error; @@ -4863,7 +4868,7 @@ sort_checkalloc_numpages_of_outfiles (THREAD_ENTRY * thread_p, SORT_PARAM * sort /* If there is a file not to be used anymore, destroy it in order to reuse spaces. */ if (!VFID_ISNULL (&sort_param->temp[i])) { - error_code = file_temp_retire_preserved (thread_p, &sort_param->temp[i]); + error_code = file_temp_retire (thread_p, &sort_param->temp[i], IS_PARALLEL_EXECUTION (sort_param)); if (error_code != NO_ERROR) { ASSERT_ERROR (); diff --git a/src/storage/file_manager.c b/src/storage/file_manager.c index e43a1e9f401..fa933ac028a 100644 --- a/src/storage/file_manager.c +++ b/src/storage/file_manager.c @@ -491,6 +491,7 @@ struct file_tempcache int ncached_numerable; pthread_mutex_t mutex; + pthread_mutex_t local_mutex; #if !defined (NDEBUG) int owner_mutex; #endif /* !NDEBUG */ @@ -728,7 +729,7 @@ static int file_perm_dealloc (THREAD_ENTRY * thread_p, PAGE_PTR page_fhead, cons static int file_rv_dealloc_internal (THREAD_ENTRY * thread_p, LOG_RCV * rcv, bool compensate_or_run_postpone); STATIC_INLINE int file_create_temp_internal (THREAD_ENTRY * thread_p, int npages, FILE_TYPE ftype, bool is_numerable, - VFID * vfid_out) __attribute__ ((ALWAYS_INLINE)); + VFID * vfid_out, bool with_lock) __attribute__ ((ALWAYS_INLINE)); static int file_sector_map_pages (THREAD_ENTRY * thread_p, const void *data, int index, bool * stop, void *args); static DISK_ISVALID file_table_check (THREAD_ENTRY * thread_p, const VFID * vfid, DISK_VOLMAP_CLONE * disk_map_clone); @@ -770,8 +771,8 @@ static int file_temp_alloc (THREAD_ENTRY * thread_p, PAGE_PTR page_fhead, FILE_A STATIC_INLINE int file_temp_set_type (THREAD_ENTRY * thread_p, VFID * vfid, FILE_TYPE ftype) __attribute__ ((ALWAYS_INLINE)); static int file_temp_reset_user_pages (THREAD_ENTRY * thread_p, const VFID * vfid); -STATIC_INLINE int file_temp_retire_internal (THREAD_ENTRY * thread_p, const VFID * vfid, bool was_preserved) - __attribute__ ((ALWAYS_INLINE)); +STATIC_INLINE int file_temp_retire_internal (THREAD_ENTRY * thread_p, const VFID * vfid, bool was_preserved, + bool with_lock) __attribute__ ((ALWAYS_INLINE)); /************************************************************************/ /* Temporary cache section */ @@ -798,6 +799,10 @@ STATIC_INLINE FILE_TEMPCACHE_ENTRY *file_tempcache_pop_tran_file (THREAD_ENTRY * __attribute__ ((ALWAYS_INLINE)); STATIC_INLINE void file_tempcache_push_tran_file (THREAD_ENTRY * thread_p, FILE_TEMPCACHE_ENTRY * entry) __attribute__ ((ALWAYS_INLINE)); +STATIC_INLINE FILE_TEMPCACHE_ENTRY *file_tempcache_pop_tran_file_with_lock (THREAD_ENTRY * thread_p, const VFID * vfid) + __attribute__ ((ALWAYS_INLINE)); +STATIC_INLINE void file_tempcache_push_tran_file_with_lock (THREAD_ENTRY * thread_p, FILE_TEMPCACHE_ENTRY * entry) + __attribute__ ((ALWAYS_INLINE)); STATIC_INLINE void file_tempcache_dump (FILE * fp) __attribute__ ((ALWAYS_INLINE)); /************************************************************************/ @@ -3153,7 +3158,8 @@ file_create_heap (THREAD_ENTRY * thread_p, bool reuse_oid, const OID * class_oid * vfid_out (out) : VFID of file (obtained from cache or created). */ STATIC_INLINE int -file_create_temp_internal (THREAD_ENTRY * thread_p, int npages, FILE_TYPE ftype, bool is_numerable, VFID * vfid_out) +file_create_temp_internal (THREAD_ENTRY * thread_p, int npages, FILE_TYPE ftype, bool is_numerable, VFID * vfid_out, + bool with_lock) { FILE_TABLESPACE tablespace; FILE_TEMPCACHE_ENTRY *tempcache_entry = NULL; @@ -3189,12 +3195,16 @@ file_create_temp_internal (THREAD_ENTRY * thread_p, int npages, FILE_TYPE ftype, *vfid_out = tempcache_entry->vfid; } - /* if numerable then temporary file is intermediate file for sorting. No transaction info required */ - if (!is_numerable) + /* save to transaction temporary file list entry */ + if (with_lock) + { + file_tempcache_push_tran_file_with_lock (thread_p, tempcache_entry); + } + else { - /* save to transaction temporary file list */ file_tempcache_push_tran_file (thread_p, tempcache_entry); } + return NO_ERROR; } @@ -3207,9 +3217,9 @@ file_create_temp_internal (THREAD_ENTRY * thread_p, int npages, FILE_TYPE ftype, * vfid (out) : File identifier */ int -file_create_temp (THREAD_ENTRY * thread_p, int npages, VFID * vfid) +file_create_temp (THREAD_ENTRY * thread_p, int npages, VFID * vfid, bool with_lock) { - return file_create_temp_internal (thread_p, npages, FILE_TEMP, false, vfid); + return file_create_temp_internal (thread_p, npages, FILE_TEMP, false, vfid, with_lock); } /* @@ -3221,9 +3231,9 @@ file_create_temp (THREAD_ENTRY * thread_p, int npages, VFID * vfid) * vfid (out) : File identifier */ int -file_create_temp_numerable (THREAD_ENTRY * thread_p, int npages, VFID * vfid) +file_create_temp_numerable (THREAD_ENTRY * thread_p, int npages, VFID * vfid, bool with_lock) { - return file_create_temp_internal (thread_p, npages, FILE_TEMP, true, vfid); + return file_create_temp_internal (thread_p, npages, FILE_TEMP, true, vfid, with_lock); } /* @@ -3236,7 +3246,7 @@ file_create_temp_numerable (THREAD_ENTRY * thread_p, int npages, VFID * vfid) int file_create_query_area (THREAD_ENTRY * thread_p, VFID * vfid) { - return file_create_temp_internal (thread_p, 1, FILE_QUERY_AREA, false, vfid); + return file_create_temp_internal (thread_p, 1, FILE_QUERY_AREA, false, vfid, false); } /* @@ -4305,9 +4315,9 @@ file_postpone_destroy (THREAD_ENTRY * thread_p, const VFID * vfid) * vfid (in) : file identifier */ int -file_temp_retire (THREAD_ENTRY * thread_p, const VFID * vfid) +file_temp_retire (THREAD_ENTRY * thread_p, const VFID * vfid, bool with_lock) { - return file_temp_retire_internal (thread_p, vfid, false); + return file_temp_retire_internal (thread_p, vfid, false, with_lock); } /* @@ -4320,7 +4330,7 @@ file_temp_retire (THREAD_ENTRY * thread_p, const VFID * vfid) int file_temp_retire_preserved (THREAD_ENTRY * thread_p, const VFID * vfid) { - return file_temp_retire_internal (thread_p, vfid, true); + return file_temp_retire_internal (thread_p, vfid, true, false); } /* @@ -4333,7 +4343,7 @@ file_temp_retire_preserved (THREAD_ENTRY * thread_p, const VFID * vfid) * transaction list. */ STATIC_INLINE int -file_temp_retire_internal (THREAD_ENTRY * thread_p, const VFID * vfid, bool was_preserved) +file_temp_retire_internal (THREAD_ENTRY * thread_p, const VFID * vfid, bool was_preserved, bool with_lock) { FILE_TEMPCACHE_ENTRY *entry = NULL; int error_code = NO_ERROR; @@ -4359,7 +4369,15 @@ file_temp_retire_internal (THREAD_ENTRY * thread_p, const VFID * vfid, bool was_ } else { - entry = file_tempcache_pop_tran_file (thread_p, vfid); + if (with_lock) + { + entry = file_tempcache_pop_tran_file_with_lock (thread_p, vfid); + } + else + { + entry = file_tempcache_pop_tran_file (thread_p, vfid); + } + assert (entry != NULL); } @@ -9496,9 +9514,56 @@ file_tempcache_cache_or_drop_entries (THREAD_ENTRY * thread_p, FILE_TEMPCACHE_EN STATIC_INLINE FILE_TEMPCACHE_ENTRY * file_tempcache_pop_tran_file (THREAD_ENTRY * thread_p, const VFID * vfid) { - FILE_TEMPCACHE_ENTRY **tran_files_p = &file_Tempcache.tran_files[file_get_tempcache_entry_index (thread_p)]; + FILE_TEMPCACHE_ENTRY **tran_files_p; + FILE_TEMPCACHE_ENTRY *entry = NULL, *prev_entry = NULL; + + tran_files_p = &file_Tempcache.tran_files[file_get_tempcache_entry_index (thread_p)]; + + for (entry = *tran_files_p; entry != NULL; entry = entry->next) + { + if (VFID_EQ (&entry->vfid, vfid)) + { + /* remove entry from transaction list */ + if (prev_entry != NULL) + { + prev_entry->next = entry->next; + } + else + { + *tran_files_p = entry->next; + } + entry->next = NULL; + + file_log ("file_tempcache_pop_tran_file", "removed entry " FILE_TEMPCACHE_ENTRY_MSG, + FILE_TEMPCACHE_ENTRY_AS_ARGS (entry)); + + return entry; + } + prev_entry = entry; + } + + /* should have found it */ + assert_release (false); + return NULL; +} + +/* + * file_tempcache_pop_tran_file_with_lock () - pop entry with the given VFID from transaction list + * + * return : popped entry + * thread_p (in) : thread entry + * vfid (in) : file identifier + */ +STATIC_INLINE FILE_TEMPCACHE_ENTRY * +file_tempcache_pop_tran_file_with_lock (THREAD_ENTRY * thread_p, const VFID * vfid) +{ + FILE_TEMPCACHE_ENTRY **tran_files_p; FILE_TEMPCACHE_ENTRY *entry = NULL, *prev_entry = NULL; + pthread_mutex_lock (&file_Tempcache.local_mutex); + + tran_files_p = &file_Tempcache.tran_files[file_get_tempcache_entry_index (thread_p)]; + for (entry = *tran_files_p; entry != NULL; entry = entry->next) { if (VFID_EQ (&entry->vfid, vfid)) @@ -9517,11 +9582,14 @@ file_tempcache_pop_tran_file (THREAD_ENTRY * thread_p, const VFID * vfid) file_log ("file_tempcache_pop_tran_file", "removed entry " FILE_TEMPCACHE_ENTRY_MSG, FILE_TEMPCACHE_ENTRY_AS_ARGS (entry)); + pthread_mutex_unlock (&file_Tempcache.local_mutex); return entry; } prev_entry = entry; } + pthread_mutex_unlock (&file_Tempcache.local_mutex); + /* should have found it */ assert_release (false); return NULL; @@ -9537,8 +9605,9 @@ file_tempcache_pop_tran_file (THREAD_ENTRY * thread_p, const VFID * vfid) STATIC_INLINE void file_tempcache_push_tran_file (THREAD_ENTRY * thread_p, FILE_TEMPCACHE_ENTRY * entry) { - FILE_TEMPCACHE_ENTRY **tran_files_p = &file_Tempcache.tran_files[file_get_tempcache_entry_index (thread_p)]; + FILE_TEMPCACHE_ENTRY **tran_files_p; + tran_files_p = &file_Tempcache.tran_files[file_get_tempcache_entry_index (thread_p)]; entry->next = *tran_files_p; *tran_files_p = entry; @@ -9546,6 +9615,30 @@ file_tempcache_push_tran_file (THREAD_ENTRY * thread_p, FILE_TEMPCACHE_ENTRY * e FILE_TEMPCACHE_ENTRY_AS_ARGS (entry)); } +/* + * file_tempcache_push_tran_file_with_lock () - push temporary file entry to transaction list + * + * return : void + * thread_p (in) : thread entry + * entry (in) : temporary cache entry + */ +STATIC_INLINE void +file_tempcache_push_tran_file_with_lock (THREAD_ENTRY * thread_p, FILE_TEMPCACHE_ENTRY * entry) +{ + FILE_TEMPCACHE_ENTRY **tran_files_p; + + pthread_mutex_lock (&file_Tempcache.local_mutex); + + tran_files_p = &file_Tempcache.tran_files[file_get_tempcache_entry_index (thread_p)]; + entry->next = *tran_files_p; + *tran_files_p = entry; + + pthread_mutex_unlock (&file_Tempcache.local_mutex); + + file_log ("file_tempcache_push_tran_file", "pushed entry " FILE_TEMPCACHE_ENTRY_MSG, + FILE_TEMPCACHE_ENTRY_AS_ARGS (entry)); +} + /* * file_get_tran_num_temp_files () - returns the number of temp file entries of the given transaction * diff --git a/src/storage/file_manager.h b/src/storage/file_manager.h index 4314fe8bd0d..3f2d71a765d 100644 --- a/src/storage/file_manager.h +++ b/src/storage/file_manager.h @@ -159,8 +159,8 @@ extern int file_create (THREAD_ENTRY * thread_p, FILE_TYPE file_type, FILE_TABLE extern int file_create_with_npages (THREAD_ENTRY * thread_p, FILE_TYPE file_type, int npages, FILE_DESCRIPTORS * des, VFID * vfid); extern int file_create_heap (THREAD_ENTRY * thread_p, bool reuse_oid, const OID * class_oid, VFID * vfid); -extern int file_create_temp (THREAD_ENTRY * thread_p, int npages, VFID * vfid); -extern int file_create_temp_numerable (THREAD_ENTRY * thread_p, int npages, VFID * vfid); +extern int file_create_temp (THREAD_ENTRY * thread_p, int npages, VFID * vfid, bool with_lock); +extern int file_create_temp_numerable (THREAD_ENTRY * thread_p, int npages, VFID * vfid, bool with_lock); extern int file_create_query_area (THREAD_ENTRY * thread_p, VFID * vfid); extern int file_create_ehash (THREAD_ENTRY * thread_p, int npages, bool is_tmp, FILE_EHASH_DES * des_ehash, VFID * vfid); @@ -169,7 +169,7 @@ extern int file_create_ehash_dir (THREAD_ENTRY * thread_p, int npages, bool is_t extern void file_postpone_destroy (THREAD_ENTRY * thread_p, const VFID * vfid); extern int file_destroy (THREAD_ENTRY * thread_p, const VFID * vfid, bool is_temp); -extern int file_temp_retire (THREAD_ENTRY * thread_p, const VFID * vfid); +extern int file_temp_retire (THREAD_ENTRY * thread_p, const VFID * vfid, bool with_lock); extern int file_temp_retire_preserved (THREAD_ENTRY * thread_p, const VFID * vfid); extern int file_init_page_type (THREAD_ENTRY * thread_p, PAGE_PTR page, void *args); From 4cc95ede0144843c9345c31fef01a4cef58f8b2c Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 20 Nov 2024 18:00:13 +0900 Subject: [PATCH 42/53] no busy wait. need to block and wake up. --- src/storage/external_sort.c | 83 ++++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 16 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 486bbe1d025..697084b4760 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -108,6 +108,14 @@ enum parallel_type }; typedef enum parallel_type PARALLEL_TYPE; +enum px_status +{ + PX_ERR_FAILED = -1, + PX_DONE = 0, + PX_PROGRESS +}; +typedef enum px_status PX_STATUS; + typedef struct file_contents FILE_CONTENTS; struct file_contents { /* node of the file_contents linked list */ @@ -179,10 +187,14 @@ struct sort_param /* support parallelism */ int px_max_index; int px_index; - bool px_status; + PX_STATUS px_status; int px_result_file_idx; int px_tran_index; SORT_PARALLEL_TYPE px_type; +#if defined(SERVER_MODE) + pthread_mutex_t *px_mtx; /* px_status mutex */ + pthread_cond_t *complete_cond; /* complete condition */ +#endif }; typedef struct sort_rec_list SORT_REC_LIST; @@ -1369,6 +1381,10 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE /* for parallel sort */ SORT_PARAM px_sort_param[SORT_MAX_PARALLEL]; /* TO_DO : need dynamic alloc */ int parallel_num = 1; /* TO_DO : depending on the number of pages in the temp file */ +#if defined(SERVER_MODE) + pthread_mutex_t px_mtx; /* px_status mutex */ + pthread_cond_t complete_cond; /* complete condition */ +#endif thread_set_sort_stats_active (thread_p, true); @@ -1384,6 +1400,25 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE return error; } +#if defined(SERVER_MODE) + if (pthread_mutex_init (&px_mtx, NULL) != 0) + { + error = ER_CSS_PTHREAD_MUTEX_INIT; + er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 0); + free_and_init (sort_param); + return error; + } + if (pthread_cond_init (&complete_cond, NULL) != 0) + { + error = ER_CSS_PTHREAD_COND_INIT; + er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 0); + free_and_init (sort_param); + return error; + } + sort_param->px_mtx = &px_mtx; + sort_param->complete_cond = &complete_cond; +#endif /* SERVER_MODE */ + sort_param->cmp_fn = cmp_fn; sort_param->cmp_arg = cmp_arg; sort_param->option = option; @@ -1506,26 +1541,33 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE // *INDENT-ON* /* wait for threads */ - /* TO_DO : no busy wait. need to block and wake up */ - int done; - do + pthread_mutex_lock (sort_param->px_mtx); + while (1) { - thread_sleep (10); - done = true; + int done = true; for (int i = 0; i < parallel_num; i++) { - if (px_sort_param[i].px_status != 1) + if (px_sort_param[i].px_status == PX_PROGRESS) { done = false; break; } + else if (px_sort_param[i].px_status == PX_ERR_FAILED) + { + error = ER_FAILED; + } } if (done) { break; } + pthread_cond_wait (sort_param->complete_cond, sort_param->px_mtx); + } + pthread_mutex_unlock (sort_param->px_mtx); + if (error != NO_ERROR) + { + goto cleanup; } - while (1); error = sort_end_parallelism (thread_p, px_sort_param, sort_param, parallel_num); if (error != NO_ERROR) @@ -1546,6 +1588,11 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE CUBRID_SORT_END (sort_param->total_numrecs, error); #endif /* ENABLE_SYSTEMTAP */ +#if defined(SERVER_MODE) + pthread_mutex_destroy (&px_mtx); + pthread_cond_destroy (&complete_cond); +#endif + /* free sort_param */ if (parallel_num > 1) { @@ -1586,16 +1633,16 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) /* open splitted temp file for read */ if (qfile_open_list_scan (sort_info_p->input_file, &t_scan_id) != NO_ERROR) { - sort_param->px_status = -1; - return; + sort_param->px_status = PX_ERR_FAILED; + goto cleanup; } sort_info_p->s_id->s_id = &t_scan_id; } else { /* Not implemented yet */ - sort_param->px_status = -1; - return; + sort_param->px_status = PX_ERR_FAILED; + goto cleanup; } sort_listfile_internal (&thread_ref, sort_param); @@ -1610,13 +1657,17 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) else { /* Not implemented yet */ - sort_param->px_status = -1; - return; + sort_param->px_status = PX_ERR_FAILED; + goto cleanup; } /* TO_DO : status enum */ - sort_param->px_status = 1; + pthread_mutex_lock(sort_param->px_mtx); + sort_param->px_status = PX_DONE; + pthread_cond_signal(sort_param->complete_cond); + pthread_mutex_unlock(sort_param->px_mtx); +cleanup: thread_p->pop_resource_tracks (); } // *INDENT-ON* @@ -4225,7 +4276,7 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ } /* init px variable */ - px_sort_param[i].px_status = 0; + px_sort_param[i].px_status = PX_PROGRESS; px_sort_param[i].px_max_index = parallel_num; px_sort_param[i].px_index = i + 1; px_sort_param[i].px_result_file_idx = 0; From 272ce1e51174cc8f96e70963a009cd083a80836a Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Wed, 20 Nov 2024 18:01:51 +0900 Subject: [PATCH 43/53] indent --- src/storage/external_sort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 697084b4760..8fa7d2f7c69 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -192,7 +192,7 @@ struct sort_param int px_tran_index; SORT_PARALLEL_TYPE px_type; #if defined(SERVER_MODE) - pthread_mutex_t *px_mtx; /* px_status mutex */ + pthread_mutex_t *px_mtx; /* px_status mutex */ pthread_cond_t *complete_cond; /* complete condition */ #endif }; From d1fcff94c23c30defa710dff1c76a7f56090ac60 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Tue, 3 Dec 2024 13:11:03 +0900 Subject: [PATCH 44/53] add merge routines upto 32 thread --- src/storage/external_sort.c | 147 +++++++++++++++++++++++++----------- 1 file changed, 104 insertions(+), 43 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 8fa7d2f7c69..b7a44a58d60 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -67,6 +67,7 @@ * or total output files at each stage of the merging process. */ #define SORT_MAX_HALF_FILES 4 +#define SORT_MAX_TOT_FILES SORT_MAX_HALF_FILES * 2 /* Lower limit on the half of the total number of the temporary files. * The exact lower limit on total number of temp files is twice this number. @@ -75,7 +76,7 @@ */ #define SORT_MIN_HALF_FILES 2 -#define SORT_MAX_PARALLEL 16 +#define SORT_MAX_PARALLEL 32 /* Initial size of the dynamic array that keeps the file contents list */ #define SORT_INITIAL_DYN_ARRAY_SIZE 30 @@ -145,9 +146,9 @@ struct vol_list typedef struct sort_param SORT_PARAM; struct sort_param { - VFID temp[2 * SORT_MAX_HALF_FILES]; /* Temporary file identifiers */ + VFID temp[SORT_MAX_TOT_FILES]; /* Temporary file identifiers */ VFID multipage_file; /* Temporary file for multi page sorting records */ - FILE_CONTENTS file_contents[2 * SORT_MAX_HALF_FILES]; /* Contents of each temporary file */ + FILE_CONTENTS file_contents[SORT_MAX_TOT_FILES]; /* Contents of each temporary file */ bool tde_encrypted; /* whether related temp files are encrypted (TDE) or not */ @@ -250,6 +251,13 @@ struct sort_stack SRUN *srun; }; +typedef struct result_run RESULT_RUN; +struct result_run +{ + VFID temp_file; + int num_pages; +}; + typedef void FIND_RUN_FN (char **, long *, SORT_STACK *, long, SORT_CMP_FUNC *, void *); typedef void MERGE_RUN_FN (char **, char **, SORT_STACK *, SORT_CMP_FUNC *, void *); @@ -257,7 +265,6 @@ typedef void MERGE_RUN_FN (char **, char **, SORT_STACK *, SORT_CMP_FUNC *, void static int sort_validate (char **vector, long size, SORT_CMP_FUNC * compare, void *comp_arg); #endif -#if defined(SERVER_MODE) /* start parallel sort */ static void sort_listfile_execute (cubthread::entry & thread_ref, SORT_PARAM * sort_param); static int sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, @@ -266,12 +273,13 @@ static int sort_split_input_temp_file (THREAD_ENTRY * thread_p, SORT_PARAM * des int parallel_num); static int sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); +static int sort_merge_nruns (THREAD_ENTRY * thread_p, RESULT_RUN * result_run, SORT_PARAM * sort_param, int first_idx, + int remaining_run, int level); static int sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); static int sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); static int sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * dest_param, SORT_PARAM * src_param, int parallel_num); -#endif /* end parallel sort */ static int sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param); @@ -1433,7 +1441,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_param->tot_tempfiles = 0; /* initialize memory allocable fields */ - for (i = 0; i < 2 * SORT_MAX_HALF_FILES; i++) + for (i = 0; i < SORT_MAX_TOT_FILES; i++) { sort_param->temp[i].volid = NULL_VOLID; sort_param->file_contents[i].num_pages = NULL; @@ -1484,7 +1492,7 @@ sort_listfile (THREAD_ENTRY * thread_p, INT16 volid, int est_inp_pg_cnt, SORT_GE sort_param->tot_tempfiles = sort_param->half_files << 1; sort_param->in_half = 0; - for (i = 0; i < sort_param->tot_tempfiles; i++) + for (i = 0; i < SORT_MAX_TOT_FILES; i++) { /* Initilize temporary file identifier; real value will be set in "sort_add_new_file () */ sort_param->temp[i].volid = NULL_VOLID; @@ -4117,14 +4125,11 @@ sort_return_used_resources (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, PA (void) file_temp_retire (thread_p, &(sort_param->multipage_file), false); } - if (parallel_type == PX_SINGLE || parallel_type == PX_THREAD_IN_PARALLEL) + for (k = 0; k < SORT_MAX_TOT_FILES; k++) { - for (k = 0; k < sort_param->tot_tempfiles; k++) + if (sort_param->file_contents[k].num_pages != NULL) { - if (sort_param->file_contents[k].num_pages != NULL) - { - free_and_init (sort_param->file_contents[k].num_pages); - } + free_and_init (sort_param->file_contents[k].num_pages); } } @@ -4243,7 +4248,7 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ for (i = 0; i < parallel_num; i++) { px_sort_param[i].internal_memory = NULL; - for (j = 0; j < px_sort_param[i].tot_tempfiles; j++) + for (j = 0; j < SORT_MAX_TOT_FILES; j++) { px_sort_param[i].file_contents[j].num_pages = NULL; } @@ -4259,7 +4264,7 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ er_set (ER_ERROR_SEVERITY, ARG_FILE_LINE, error, 1, (size_t) (sort_param->tot_buffers * DB_PAGESIZE)); break; } - for (j = 0; j < px_sort_param[i].tot_tempfiles; j++) + for (j = 0; j < SORT_MAX_TOT_FILES; j++) { /* Initilize file contents list */ px_sort_param[i].file_contents[j].num_pages = (int *) malloc (SORT_INITIAL_DYN_ARRAY_SIZE * sizeof (int)); @@ -4295,7 +4300,7 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ { free_and_init (px_sort_param[i].internal_memory); } - for (j = 0; j < px_sort_param[i].tot_tempfiles; j++) + for (j = 0; j < SORT_MAX_TOT_FILES; j++) { if (px_sort_param[i].file_contents[j].num_pages != NULL) { @@ -4467,57 +4472,98 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param int parallel_num) { int error = NO_ERROR; - int i = 0; + int i = 0, idx = 0, file_pg_cnt_est; + int remaining_run, level, merge_num; + RESULT_RUN result_run[SORT_MAX_PARALLEL]; - /* TO_DO : Up to 4 in parallel. Expansion required. SORT_MAX_HALF_FILES */ - if (parallel_num > 4) + if (parallel_num > SORT_MAX_PARALLEL) { - assert (0); + return ER_FAILED; } - /* free num_pages */ - for (i = 0; i < sort_param->tot_tempfiles; i++) + /* init result_run */ + for (i = 0; i < parallel_num; i++) { - free_and_init (sort_param->file_contents[i].num_pages); + result_run[i].temp_file = px_sort_param[i].temp[px_sort_param[i].px_result_file_idx]; + result_run[i].num_pages = px_sort_param[i].file_contents[px_sort_param[i].px_result_file_idx].num_pages[0]; + } - sort_param->file_contents[i].num_slots = SORT_INITIAL_DYN_ARRAY_SIZE; - sort_param->file_contents[i].first_run = -1; - sort_param->file_contents[i].last_run = -1; + remaining_run = parallel_num; + level = 0; + + while (remaining_run > 1) + { + merge_num = (remaining_run + (SORT_MAX_HALF_FILES - 1)) / SORT_MAX_HALF_FILES; + + for (i = 0; i < merge_num; i++) + { + idx = i * pow (SORT_MAX_HALF_FILES, level + 1); + error = sort_merge_nruns (thread_p, result_run, sort_param, idx, remaining_run, level); + if (error != NO_ERROR) + { + return error; + } + } + + remaining_run = merge_num; + level++; + } + + return error; +} + +/* + * sort_merge_nruns () - merge n run + * return: NO_ERROR + * px_sort_param(in): + * sort_param(in): + * parallel_num(in): + */ +static int +sort_merge_nruns (THREAD_ENTRY * thread_p, RESULT_RUN * result_run, SORT_PARAM * sort_param, int first_idx, + int remaining_run, int level) +{ + int error = NO_ERROR; + int i = 0, idx = 0, file_pg_cnt_est; + int half_files = MIN (remaining_run - (first_idx / pow (SORT_MAX_HALF_FILES, level)), SORT_MAX_HALF_FILES); + + if (half_files > remaining_run) + { + return ER_FAILED; } /* init file info */ sort_param->px_result_file_idx = 0; - sort_param->half_files = parallel_num; - sort_param->tot_tempfiles = parallel_num * 2; + sort_param->half_files = half_files; + sort_param->tot_tempfiles = half_files * 2; sort_param->in_half = 0; /* copy temp file and file contents */ for (i = 0; i < sort_param->half_files; i++) { - sort_param->temp[i] = px_sort_param[i].temp[px_sort_param[i].px_result_file_idx]; - sort_param->file_contents[i] = px_sort_param[i].file_contents[px_sort_param[i].px_result_file_idx]; + idx = (level == 0) ? (i + first_idx) : ((i * pow (SORT_MAX_HALF_FILES, level)) + first_idx); + sort_param->temp[i] = result_run[idx].temp_file; + /* copy the number of pages for one run */ + sort_param->file_contents[i].num_pages[0] = result_run[idx].num_pages; + sort_param->file_contents[i].first_run = 0; + sort_param->file_contents[i].last_run = 0; } for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) { /* init temp file and contents */ - px_sort_param[0].temp[i].volid = NULL_VOLID; - px_sort_param[0].file_contents[i].first_run = -1; - px_sort_param[0].file_contents[i].last_run = -1; - - /* copy from thread 0 */ - sort_param->temp[i] = px_sort_param[0].temp[i]; - sort_param->file_contents[i] = px_sort_param[0].file_contents[i]; + sort_param->temp[i].volid = NULL_VOLID; + sort_param->file_contents[i].first_run = -1; + sort_param->file_contents[i].last_run = -1; } /* Create output temporary files make file and temporary volume page count estimates */ - int file_pg_cnt_est = sort_get_avg_numpages_of_nonempty_tmpfile (sort_param); + file_pg_cnt_est = sort_get_avg_numpages_of_nonempty_tmpfile (sort_param); file_pg_cnt_est = MAX (1, file_pg_cnt_est); for (i = sort_param->half_files; i < sort_param->tot_tempfiles; i++) { error = - sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted, - IS_PARALLEL_EXECUTION (sort_param)); + sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted, false); if (error != NO_ERROR) { return error; @@ -4525,7 +4571,7 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param } /* Merge the parallel processed results. */ - sort_param->px_max_index = 1; + sort_param->px_max_index = (remaining_run <= SORT_MAX_HALF_FILES) ? 1 : 2; if (sort_param->option == SORT_ELIM_DUP) { error = sort_exphase_merge_elim_dup (thread_p, sort_param); @@ -4536,6 +4582,20 @@ sort_merge_run_for_parallel (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param error = sort_exphase_merge (thread_p, sort_param); } + /* save result run */ + result_run[first_idx].temp_file = sort_param->temp[sort_param->px_result_file_idx]; + result_run[first_idx].num_pages = sort_param->file_contents[sort_param->px_result_file_idx].num_pages[0]; + + /* retire temp file */ + for (i = 0; i < sort_param->tot_tempfiles; i++) + { + if (sort_param->temp[i].volid != NULL_VOLID && i != sort_param->px_result_file_idx) + { + (void) file_temp_retire (thread_p, &sort_param->temp[i], false); + VFID_SET_NULL (&sort_param->temp[i]); + } + } + return error; } @@ -4549,6 +4609,7 @@ static int sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) { SORT_INFO *sort_info_p; + int parallel_num = 18; if (sort_param->px_type == SORT_ORDER_BY) { @@ -4556,10 +4617,10 @@ sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) sort_info_p = (SORT_INFO *) sort_param->get_arg; /* Find the number of parallel processes by page_cnt and tuple_cnt */ - if (sort_info_p->input_file->page_cnt > 2 && sort_info_p->input_file->tuple_cnt > 2) + if (sort_info_p->input_file->page_cnt > parallel_num && sort_info_p->input_file->tuple_cnt > parallel_num) { /* TO_DO : need to check the appropriate number of parallels depending on the number of pages */ - return 2; + return parallel_num; } } else From 4f6a94c17b2b0185faa055a74591eafafdeec4bf Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 5 Dec 2024 19:24:50 +0900 Subject: [PATCH 45/53] add MAX_PARALLEL_THREAD system parameter and print execution time temporarily --- src/base/system_parameter.c | 20 +++++++++++ src/base/system_parameter.h | 3 +- src/query/list_file.c | 11 ++++++ src/storage/external_sort.c | 71 +++++++++++++++++++++++++++++++++++-- 4 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/base/system_parameter.c b/src/base/system_parameter.c index 898ec19c865..6aaee88ee59 100644 --- a/src/base/system_parameter.c +++ b/src/base/system_parameter.c @@ -664,6 +664,8 @@ static const char sysprm_ha_conf_file_name[] = "cubrid_ha.conf"; #define PRM_NAME_CTE_MAX_RECURSIONS "cte_max_recursions" +#define PRM_NAME_MAX_PARALLEL_THREAD "max_parallel_thread" + #define PRM_NAME_DWB_SIZE "double_write_buffer_size" #define PRM_NAME_DWB_BLOCKS "double_write_buffer_blocks" #define PRM_NAME_ENABLE_DWB_FLUSH_THREAD "double_write_buffer_enable_flush_thread" @@ -2159,6 +2161,12 @@ static int prm_cte_max_recursions_upper = 1000000; static int prm_cte_max_recursions_lower = 2; static unsigned int prm_cte_max_recursions_flag = 0; +int PRM_MAX_PARALLEL_THREAD = 1; +static int prm_max_parallel_thread_default = 1; +static int prm_max_parallel_thread_upper = 32; +static int prm_max_parallel_thread_lower = 1; +static unsigned int prm_max_parallel_thread_flag = 0; + bool PRM_JSON_LOG_ALLOCATIONS = false; static bool prm_json_log_allocations_default = false; static unsigned int prm_json_log_allocations_flag = 0; @@ -6448,6 +6456,18 @@ SYSPRM_PARAM prm_Def[] = { (void *) &prm_max_subquery_cache_size_lower, (char *) NULL, (DUP_PRM_FUNC) NULL, + (DUP_PRM_FUNC) NULL}, + {PRM_ID_MAX_PARALLEL_THREAD, + PRM_NAME_MAX_PARALLEL_THREAD, + (PRM_FOR_SERVER | PRM_USER_CHANGE | PRM_FOR_SESSION | PRM_HIDDEN), + PRM_INTEGER, + &prm_max_parallel_thread_flag, + (void *) &prm_max_parallel_thread_default, + (void *) &PRM_MAX_PARALLEL_THREAD, + (void *) &prm_max_parallel_thread_upper, + (void *) &prm_max_parallel_thread_lower, + (char *) NULL, + (DUP_PRM_FUNC) NULL, (DUP_PRM_FUNC) NULL} }; diff --git a/src/base/system_parameter.h b/src/base/system_parameter.h index 2ea92fcbe0c..5b9aed2f140 100644 --- a/src/base/system_parameter.h +++ b/src/base/system_parameter.h @@ -486,8 +486,9 @@ enum param_id PRM_ID_ENABLE_MEMORY_MONITORING, PRM_ID_MAX_SUBQUERY_CACHE_SIZE, + PRM_ID_MAX_PARALLEL_THREAD, /* change PRM_LAST_ID when adding new system parameters */ - PRM_LAST_ID = PRM_ID_MAX_SUBQUERY_CACHE_SIZE + PRM_LAST_ID = PRM_ID_MAX_PARALLEL_THREAD }; typedef enum param_id PARAM_ID; diff --git a/src/query/list_file.c b/src/query/list_file.c index d3357fe4c92..94db8145a8f 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -4041,10 +4041,21 @@ qfile_sort_list_with_func (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p, S dup_option = ((option == Q_DISTINCT) ? SORT_ELIM_DUP : SORT_DUP); +TSC_TICKS start_tick, end_tick; +TSCTIMEVAL tv_diff; +struct timeval orderby_time; + +tsc_getticks (&start_tick); + sort_result = sort_listfile (thread_p, NULL_VOLID, estimated_pages, get_func, &info, put_func, &info, cmp_func, &info.key_info, dup_option, limit, srlist_id->tfile_vfid->tde_encrypted, SORT_ORDER_BY); +tsc_getticks (&end_tick); +tsc_elapsed_time_usec (&tv_diff, end_tick, start_tick); +TSC_ADD_TIMEVAL (orderby_time, tv_diff); +printf ("sort_listfile time: %d\n", TO_MSEC (orderby_time)); + if (sort_result < 0) { #if 0 /* SortCache */ diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index b7a44a58d60..911a1d8687c 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -1634,6 +1634,13 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) thread_p->push_resource_tracks (); +#if !defined(NDEBUG) + TSC_TICKS start_tick, end_tick; + TSCTIMEVAL tv_diff; + struct timeval orderby_time; + tsc_getticks (&start_tick); +#endif + if (sort_param->px_type == SORT_ORDER_BY) { SORT_INFO *sort_info_p = (SORT_INFO *) sort_param->get_arg; @@ -1669,7 +1676,14 @@ sort_listfile_execute (cubthread::entry &thread_ref, SORT_PARAM * sort_param) goto cleanup; } - /* TO_DO : status enum */ +#if !defined(NDEBUG) + tsc_getticks (&end_tick); + tsc_elapsed_time_usec (&tv_diff, end_tick, start_tick); + TSC_ADD_TIMEVAL (orderby_time, tv_diff); + printf ("thread %d done, time: %d\n",thread_p->index, TO_MSEC (orderby_time)); +#endif + + /* done */ pthread_mutex_lock(sort_param->px_mtx); sort_param->px_status = PX_DONE; pthread_cond_signal(sort_param->complete_cond); @@ -1700,12 +1714,29 @@ sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) * space that is going to be needed. */ +#if !defined(NDEBUG) + TSC_TICKS start_tick, end_tick; + TSC_TICKS start_tick2, end_tick2; + TSCTIMEVAL tv_diff, tv_diff2; + struct timeval orderby_time, orderby_time2; + tsc_getticks (&start_tick); +#endif + error = sort_inphase_sort (thread_p, sort_param, sort_param->get_fn, sort_param->get_arg, &sort_param->total_numrecs); if (error != NO_ERROR) { return error; } +#if !defined(NDEBUG) + tsc_getticks (&end_tick); + tsc_elapsed_time_usec (&tv_diff, end_tick, start_tick); + TSC_ADD_TIMEVAL (orderby_time, tv_diff); + printf ("sort_inphase_sort time: %d\n", TO_MSEC (orderby_time)); + + tsc_getticks (&start_tick2); +#endif + if (sort_param->tot_runs > 1 || IS_PARALLEL_EXECUTION (sort_param)) { assert (sort_param->tot_runs > 0); @@ -1735,6 +1766,13 @@ sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) } } /* if (sort_param->tot_runs > 1) */ +#if !defined(NDEBUG) + tsc_getticks (&end_tick2); + tsc_elapsed_time_usec (&tv_diff2, end_tick2, start_tick2); + TSC_ADD_TIMEVAL (orderby_time2, tv_diff2); + printf ("sort_exphase_merge time: %d\n", TO_MSEC (orderby_time2)); +#endif + return error; } @@ -4570,8 +4608,16 @@ sort_merge_nruns (THREAD_ENTRY * thread_p, RESULT_RUN * result_run, SORT_PARAM * } } +#if !defined(NDEBUG) + TSC_TICKS start_tick, end_tick; + TSCTIMEVAL tv_diff; + struct timeval orderby_time; + tsc_getticks (&start_tick); +#endif + /* Merge the parallel processed results. */ sort_param->px_max_index = (remaining_run <= SORT_MAX_HALF_FILES) ? 1 : 2; + sort_param->px_max_index = 2; if (sort_param->option == SORT_ELIM_DUP) { error = sort_exphase_merge_elim_dup (thread_p, sort_param); @@ -4582,6 +4628,13 @@ sort_merge_nruns (THREAD_ENTRY * thread_p, RESULT_RUN * result_run, SORT_PARAM * error = sort_exphase_merge (thread_p, sort_param); } +#if !defined(NDEBUG) + tsc_getticks (&end_tick); + tsc_elapsed_time_usec (&tv_diff, end_tick, start_tick); + TSC_ADD_TIMEVAL (orderby_time, tv_diff); + printf ("n-merge time: %d\n", TO_MSEC (orderby_time)); +#endif + /* save result run */ result_run[first_idx].temp_file = sort_param->temp[sort_param->px_result_file_idx]; result_run[first_idx].num_pages = sort_param->file_contents[sort_param->px_result_file_idx].num_pages[0]; @@ -4609,7 +4662,7 @@ static int sort_check_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) { SORT_INFO *sort_info_p; - int parallel_num = 18; + int parallel_num = prm_get_integer_value (PRM_ID_MAX_PARALLEL_THREAD); if (sort_param->px_type == SORT_ORDER_BY) { @@ -4715,6 +4768,13 @@ sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ return ER_FAILED; } +#if !defined(NDEBUG) + TSC_TICKS start_tick, end_tick; + TSCTIMEVAL tv_diff; + struct timeval orderby_time; + tsc_getticks (&start_tick); +#endif + /* merging temp files from parallel processed */ error = sort_merge_run_for_parallel (thread_p, px_sort_param, sort_param, parallel_num); if (error != NO_ERROR) @@ -4722,6 +4782,13 @@ sort_end_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ return ER_FAILED; } +#if !defined(NDEBUG) + tsc_getticks (&end_tick); + tsc_elapsed_time_usec (&tv_diff, end_tick, start_tick); + TSC_ADD_TIMEVAL (orderby_time, tv_diff); + printf ("merge time: %d\n", TO_MSEC (orderby_time)); +#endif + return error; } From afc5a44c75996556a1c5dae5deebd56cd6edece3 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 5 Dec 2024 19:27:53 +0900 Subject: [PATCH 46/53] indent --- src/query/list_file.c | 16 ++++++++-------- src/storage/external_sort.c | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/query/list_file.c b/src/query/list_file.c index 94db8145a8f..842777caeaa 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -4041,20 +4041,20 @@ qfile_sort_list_with_func (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p, S dup_option = ((option == Q_DISTINCT) ? SORT_ELIM_DUP : SORT_DUP); -TSC_TICKS start_tick, end_tick; -TSCTIMEVAL tv_diff; -struct timeval orderby_time; + TSC_TICKS start_tick, end_tick; + TSCTIMEVAL tv_diff; + struct timeval orderby_time; -tsc_getticks (&start_tick); + tsc_getticks (&start_tick); sort_result = sort_listfile (thread_p, NULL_VOLID, estimated_pages, get_func, &info, put_func, &info, cmp_func, &info.key_info, dup_option, limit, srlist_id->tfile_vfid->tde_encrypted, SORT_ORDER_BY); -tsc_getticks (&end_tick); -tsc_elapsed_time_usec (&tv_diff, end_tick, start_tick); -TSC_ADD_TIMEVAL (orderby_time, tv_diff); -printf ("sort_listfile time: %d\n", TO_MSEC (orderby_time)); + tsc_getticks (&end_tick); + tsc_elapsed_time_usec (&tv_diff, end_tick, start_tick); + TSC_ADD_TIMEVAL (orderby_time, tv_diff); + printf ("sort_listfile time: %d\n", TO_MSEC (orderby_time)); if (sort_result < 0) { diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 911a1d8687c..8549836fcd2 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4617,7 +4617,7 @@ sort_merge_nruns (THREAD_ENTRY * thread_p, RESULT_RUN * result_run, SORT_PARAM * /* Merge the parallel processed results. */ sort_param->px_max_index = (remaining_run <= SORT_MAX_HALF_FILES) ? 1 : 2; - sort_param->px_max_index = 2; + sort_param->px_max_index = 2; if (sort_param->option == SORT_ELIM_DUP) { error = sort_exphase_merge_elim_dup (thread_p, sort_param); From 4af0217ddd4fad6385ed39c14df53b107296a395 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 5 Dec 2024 19:49:08 +0900 Subject: [PATCH 47/53] slip --- src/storage/external_sort.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 8549836fcd2..600c31a06ab 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4617,7 +4617,6 @@ sort_merge_nruns (THREAD_ENTRY * thread_p, RESULT_RUN * result_run, SORT_PARAM * /* Merge the parallel processed results. */ sort_param->px_max_index = (remaining_run <= SORT_MAX_HALF_FILES) ? 1 : 2; - sort_param->px_max_index = 2; if (sort_param->option == SORT_ELIM_DUP) { error = sort_exphase_merge_elim_dup (thread_p, sort_param); From 31610a5f81c565f963c4843ce4b980c13831d33d Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Thu, 5 Dec 2024 19:52:18 +0900 Subject: [PATCH 48/53] slip --- src/query/list_file.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/query/list_file.c b/src/query/list_file.c index 842777caeaa..b05dae2649f 100644 --- a/src/query/list_file.c +++ b/src/query/list_file.c @@ -4041,20 +4041,23 @@ qfile_sort_list_with_func (THREAD_ENTRY * thread_p, QFILE_LIST_ID * list_id_p, S dup_option = ((option == Q_DISTINCT) ? SORT_ELIM_DUP : SORT_DUP); +#if !defined(NDEBUG) TSC_TICKS start_tick, end_tick; TSCTIMEVAL tv_diff; struct timeval orderby_time; - tsc_getticks (&start_tick); +#endif sort_result = sort_listfile (thread_p, NULL_VOLID, estimated_pages, get_func, &info, put_func, &info, cmp_func, &info.key_info, dup_option, limit, srlist_id->tfile_vfid->tde_encrypted, SORT_ORDER_BY); +#if !defined(NDEBUG) tsc_getticks (&end_tick); tsc_elapsed_time_usec (&tv_diff, end_tick, start_tick); TSC_ADD_TIMEVAL (orderby_time, tv_diff); printf ("sort_listfile time: %d\n", TO_MSEC (orderby_time)); +#endif if (sort_result < 0) { From 3d70364e2654b03c13acdc3787fd025a347d127a Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 9 Dec 2024 13:06:23 +0900 Subject: [PATCH 49/53] slip --- src/query/query_manager.c | 10 ---------- src/storage/external_sort.c | 3 +-- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/src/query/query_manager.c b/src/query/query_manager.c index 6433727a265..25f31b33ea1 100644 --- a/src/query/query_manager.c +++ b/src/query/query_manager.c @@ -2537,16 +2537,6 @@ qmgr_set_dirty_page (THREAD_ENTRY * thread_p, PAGE_PTR page_p, int free_page, LO QMGR_TEMP_FILE * tfile_vfid_p) { QMGR_PAGE_TYPE page_type; - LOG_DATA_ADDR addr; - - if (addr_p == NULL) - { - addr.vfid = NULL; - addr.pgptr = page_p; - addr.offset = -1; - - addr_p = &addr; - } page_type = qmgr_get_page_type (page_p, tfile_vfid_p); if (page_type == QMGR_UNKNOWN_PAGE) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 600c31a06ab..c94e91aad03 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -4718,8 +4718,7 @@ sort_start_parallelism (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SOR /* close input file for read. it'll be opened in parallel */ qfile_close_scan (thread_p, scan_id_p); - /* split input temp file. TO_DO : If writing pages is not possible, need to find another way. */ - /* if possible, may need to revert the splitted pages */ + /* split input temp file. TO_DO : may need to revert the splitted pages */ error = sort_split_input_temp_file (thread_p, px_sort_param, sort_param, parallel_num); if (error != NO_ERROR) { From c6af08242d991308e956f6ed6e03bd5d11863b32 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 9 Dec 2024 13:33:46 +0900 Subject: [PATCH 50/53] remove unnecessary var --- src/storage/external_sort.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index c94e91aad03..17d07fc31fb 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -99,7 +99,7 @@ } \ } while (0) -#define IS_PARALLEL_EXECUTION(t) ((t)->px_max_index > 1) +#define IS_PARALLEL_SORT(t) ((t)->px_max_index > 1) enum parallel_type { @@ -187,7 +187,6 @@ struct sort_param /* support parallelism */ int px_max_index; - int px_index; PX_STATUS px_status; int px_result_file_idx; int px_tran_index; @@ -1737,7 +1736,7 @@ sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) tsc_getticks (&start_tick2); #endif - if (sort_param->tot_runs > 1 || IS_PARALLEL_EXECUTION (sort_param)) + if (sort_param->tot_runs > 1 || IS_PARALLEL_SORT (sort_param)) { assert (sort_param->tot_runs > 0); /* Create output temporary files make file and temporary volume page count estimates */ @@ -1748,7 +1747,7 @@ sort_listfile_internal (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) { error = sort_add_new_file (thread_p, &(sort_param->temp[i]), file_pg_cnt_est, true, sort_param->tde_encrypted, - IS_PARALLEL_EXECUTION (sort_param)); + IS_PARALLEL_SORT (sort_param)); if (error != NO_ERROR) { return error; @@ -2063,7 +2062,7 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU sort_param->multipage_file.volid = sort_param->temp[0].volid; error = - file_create_temp (thread_p, 1, &sort_param->multipage_file, IS_PARALLEL_EXECUTION (sort_param)); + file_create_temp (thread_p, 1, &sort_param->multipage_file, IS_PARALLEL_SORT (sort_param)); if (error != NO_ERROR) { ASSERT_ERROR (); @@ -2075,7 +2074,7 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU } if (file_apply_tde_algorithm (thread_p, &sort_param->multipage_file, tde_algo) != NO_ERROR) { - file_temp_retire (thread_p, &sort_param->multipage_file, IS_PARALLEL_EXECUTION (sort_param)); + file_temp_retire (thread_p, &sort_param->multipage_file, IS_PARALLEL_SORT (sort_param)); ASSERT_ERROR (); goto exit_on_error; } @@ -2157,7 +2156,7 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU goto exit_on_error; } - if (sort_param->tot_runs > 0 || IS_PARALLEL_EXECUTION (sort_param)) + if (sort_param->tot_runs > 0 || IS_PARALLEL_SORT (sort_param)) { /* There has been other runs produced already */ @@ -2191,7 +2190,7 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU } } } - else if (sort_param->tot_runs == 1 && !IS_PARALLEL_EXECUTION (sort_param)) + else if (sort_param->tot_runs == 1 && !IS_PARALLEL_SORT (sort_param)) { if (once_flushed) { @@ -2288,7 +2287,7 @@ sort_run_flush (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, int out_file, { error = sort_add_new_file (thread_p, &sort_param->temp[out_file], sort_param->tmp_file_pgs, false, - sort_param->tde_encrypted, IS_PARALLEL_EXECUTION (sort_param)); + sort_param->tde_encrypted, IS_PARALLEL_SORT (sort_param)); if (error != NO_ERROR) { return error; @@ -2539,7 +2538,7 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* OUTER LOOP */ /* for one temporary file, put result from the temp file instead of merging it. */ - if (!IS_PARALLEL_EXECUTION (sort_param) && sort_get_numpages_of_active_infiles (sort_param) == 1) + if (!IS_PARALLEL_SORT (sort_param) && sort_get_numpages_of_active_infiles (sort_param) == 1) { error = sort_put_result_from_tmpfile (thread_p, sort_param); if (error != NO_ERROR) @@ -2861,7 +2860,7 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) out_runsize = 0; /* In parallel sort, put_fn will be performed by the parent thread. save last file index. */ - if (very_last_run && IS_PARALLEL_EXECUTION (sort_param)) + if (very_last_run && IS_PARALLEL_SORT (sort_param)) { sort_param->px_result_file_idx = cur_outfile; } @@ -2878,7 +2877,7 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) { /* we found first unique sort_key record */ - if (very_last_run && !IS_PARALLEL_EXECUTION (sort_param)) + if (very_last_run && !IS_PARALLEL_SORT (sort_param)) { /* OUTPUT THE RECORD */ /* Obtain the output record for this temporary record */ @@ -3164,7 +3163,7 @@ sort_exphase_merge_elim_dup (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) } } - if (!(very_last_run && !IS_PARALLEL_EXECUTION (sort_param))) + if (!(very_last_run && !IS_PARALLEL_SORT (sort_param))) { /* Flush whatever is left on the output section */ out_act_bufno++; /* Since 0 refers to the first active buffer */ @@ -3430,7 +3429,7 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) } /* for one temporary file, put result from the temp file instead of merging it. */ - if (!IS_PARALLEL_EXECUTION (sort_param) && sort_get_numpages_of_active_infiles (sort_param) == 1) + if (!IS_PARALLEL_SORT (sort_param) && sort_get_numpages_of_active_infiles (sort_param) == 1) { error = sort_put_result_from_tmpfile (thread_p, sort_param); if (error != NO_ERROR) @@ -3742,7 +3741,7 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) out_runsize = 0; /* In parallel sort, put_fn will be performed by the parent thread. save last file index. */ - if (very_last_run && IS_PARALLEL_EXECUTION (sort_param)) + if (very_last_run && IS_PARALLEL_SORT (sort_param)) { sort_param->px_result_file_idx = cur_outfile; } @@ -3754,7 +3753,7 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) /* FIND MINIMUM RECORD IN THE INPUT AREA */ min = min_p->rec_pos; - if (very_last_run && !IS_PARALLEL_EXECUTION (sort_param)) + if (very_last_run && !IS_PARALLEL_SORT (sort_param)) { /* OUTPUT THE RECORD */ /* Obtain the output record for this temporary record */ @@ -4023,7 +4022,7 @@ sort_exphase_merge (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param) } } - if (!(very_last_run && !IS_PARALLEL_EXECUTION (sort_param))) + if (!(very_last_run && !IS_PARALLEL_SORT (sort_param))) { /* Flush whatever is left on the output section */ @@ -4321,7 +4320,6 @@ sort_copy_sort_param (THREAD_ENTRY * thread_p, SORT_PARAM * px_sort_param, SORT_ /* init px variable */ px_sort_param[i].px_status = PX_PROGRESS; px_sort_param[i].px_max_index = parallel_num; - px_sort_param[i].px_index = i + 1; px_sort_param[i].px_result_file_idx = 0; /* Copy the parent's tran_index. */ px_sort_param[i].px_tran_index = LOG_FIND_THREAD_TRAN_INDEX (thread_p); @@ -5045,7 +5043,7 @@ sort_checkalloc_numpages_of_outfiles (THREAD_ENTRY * thread_p, SORT_PARAM * sort /* If there is a file not to be used anymore, destroy it in order to reuse spaces. */ if (!VFID_ISNULL (&sort_param->temp[i])) { - error_code = file_temp_retire (thread_p, &sort_param->temp[i], IS_PARALLEL_EXECUTION (sort_param)); + error_code = file_temp_retire (thread_p, &sort_param->temp[i], IS_PARALLEL_SORT (sort_param)); if (error_code != NO_ERROR) { ASSERT_ERROR (); From a2cccb203b6becd1eec454618a7995c2f3d63084 Mon Sep 17 00:00:00 2001 From: shparkcubrid Date: Mon, 9 Dec 2024 14:14:45 +0900 Subject: [PATCH 51/53] indent --- src/storage/external_sort.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/storage/external_sort.c b/src/storage/external_sort.c index 17d07fc31fb..0634cf03ac0 100644 --- a/src/storage/external_sort.c +++ b/src/storage/external_sort.c @@ -2061,8 +2061,7 @@ sort_inphase_sort (THREAD_ENTRY * thread_p, SORT_PARAM * sort_param, SORT_GET_FU /* Create the multipage file */ sort_param->multipage_file.volid = sort_param->temp[0].volid; - error = - file_create_temp (thread_p, 1, &sort_param->multipage_file, IS_PARALLEL_SORT (sort_param)); + error = file_create_temp (thread_p, 1, &sort_param->multipage_file, IS_PARALLEL_SORT (sort_param)); if (error != NO_ERROR) { ASSERT_ERROR (); From 3ba3d8a8faa30f37a8bd71785a926db62a5f48a1 Mon Sep 17 00:00:00 2001 From: SE park Date: Sat, 14 Dec 2024 19:47:34 +0900 Subject: [PATCH 52/53] Update src/base/system_parameter.c --- src/base/system_parameter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base/system_parameter.c b/src/base/system_parameter.c index 6aaee88ee59..428ce6a0682 100644 --- a/src/base/system_parameter.c +++ b/src/base/system_parameter.c @@ -6459,7 +6459,7 @@ SYSPRM_PARAM prm_Def[] = { (DUP_PRM_FUNC) NULL}, {PRM_ID_MAX_PARALLEL_THREAD, PRM_NAME_MAX_PARALLEL_THREAD, - (PRM_FOR_SERVER | PRM_USER_CHANGE | PRM_FOR_SESSION | PRM_HIDDEN), + (PRM_FOR_SERVER | PRM_FOR_CLIENT | PRM_USER_CHANGE | PRM_FOR_SESSION | PRM_HIDDEN), PRM_INTEGER, &prm_max_parallel_thread_flag, (void *) &prm_max_parallel_thread_default, From a56a11b59e7602dc9843859a5032a9a549dd0fa3 Mon Sep 17 00:00:00 2001 From: SE park Date: Mon, 23 Dec 2024 19:12:02 +0900 Subject: [PATCH 53/53] Update src/base/system_parameter.c --- src/base/system_parameter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base/system_parameter.c b/src/base/system_parameter.c index 428ce6a0682..ecf530d163f 100644 --- a/src/base/system_parameter.c +++ b/src/base/system_parameter.c @@ -6459,7 +6459,7 @@ SYSPRM_PARAM prm_Def[] = { (DUP_PRM_FUNC) NULL}, {PRM_ID_MAX_PARALLEL_THREAD, PRM_NAME_MAX_PARALLEL_THREAD, - (PRM_FOR_SERVER | PRM_FOR_CLIENT | PRM_USER_CHANGE | PRM_FOR_SESSION | PRM_HIDDEN), + (PRM_FOR_SERVER | PRM_FOR_CLIENT | PRM_USER_CHANGE | PRM_FOR_SESSION), PRM_INTEGER, &prm_max_parallel_thread_flag, (void *) &prm_max_parallel_thread_default,