/* * Create and initialize per query session context */ POOL_QUERY_CONTEXT *pool_init_query_context(void) { POOL_QUERY_CONTEXT *qc; qc = calloc(1, sizeof(*qc)); if (!qc) { pool_error("pool_init_query_context: cannot allocate memory"); return NULL; } /* Create memory context */ qc->memory_context = pool_memory_create(PARSER_BLOCK_SIZE); return qc; }
/* * read in hba config file */ void load_hba(char *hbapath) { FILE *file; POOL_MEMORY_POOL *old_context; if (hba_memory_context == NULL) { hba_memory_context = pool_memory_create(PARSER_BLOCK_SIZE); if (hba_memory_context == NULL) { pool_error("load_hba: pool_memory_create() failed"); exit(1); } } /* switch memory context */ old_context = pool_memory; pool_memory = hba_memory_context; if (hba_lines || hba_line_nums) free_lines(&hba_lines, &hba_line_nums); file = fopen(hbapath, "r"); if (!file) { pool_error("could not open \"%s\". reason: %s", hbapath, strerror(errno)); exit(1); } pool_debug("loading \"%s\" for client authentication configuration file", hbapath); tokenize_file(hbapath, file, &hba_lines, &hba_line_nums); fclose(file); hbaFileName = pstrdup(hbapath); /* switch old memory context */ pool_memory = old_context; }
/* * Initialize per session context */ void pool_init_session_context(POOL_CONNECTION *frontend, POOL_CONNECTION_POOL *backend) { session_context = &session_context_d; /* Get Process context */ session_context->process_context = pool_get_process_context(); if (!session_context->process_context) { pool_error("pool_init_session_context: cannot get process context"); return; } /* Set connection info */ session_context->frontend = frontend; session_context->backend = backend; /* Initialize query context */ session_context->query_context = NULL; /* Initialize local session id */ pool_incremnet_local_session_id(); /* Initialize sent message list */ init_sent_message_list(); /* Create memory context */ session_context->memory_context = pool_memory_create(PREPARE_BLOCK_SIZE); /* Choose load balancing node if necessary */ if (pool_config->load_balance_mode) { ProcessInfo *process_info = pool_get_my_process_info(); if (!process_info) { pool_error("pool_init_session_context: pool_get_my_process_info failed"); return; } session_context->load_balance_node_id = process_info->connection_info->load_balancing_node = select_load_balancing_node(); pool_debug("selected load balancing node: %d", backend->info->load_balancing_node); } /* Unset query is in progress */ pool_unset_query_in_progress(); /* The command in progress has not succeeded yet */ pool_unset_command_success(); /* We don't have a write query in this transaction yet */ pool_unset_writing_transaction(); /* Error doesn't occur in this transaction yet */ pool_unset_failed_transaction(); /* Forget transaction isolation mode */ pool_unset_transaction_isolation(); /* We don't skip reading from backends */ pool_unset_skip_reading_from_backends(); /* Backends have not ignored messages yet */ pool_unset_ignore_till_sync(); /* Initialize where to send map for PREPARE statements */ #ifdef NOT_USED memset(&session_context->prep_where, 0, sizeof(session_context->prep_where)); session_context->prep_where.nelem = POOL_MAX_PREPARED_STATEMENTS; #endif /* NOT_USED */ /* Reset flag to indicate difference in number of affected tuples * in UPDATE/DELETE. */ session_context->mismatch_ntuples = false; if (pool_config->memory_cache_enabled) { session_context->query_cache_array = pool_create_query_cache_array(); session_context->num_selects = 0; } }
/* * trigger_failover_command: execute specified command at failover. * command_line is null-terminated string. */ static int trigger_failover_command(int node, const char *command_line) { int r = 0; String *exec_cmd; char port_buf[6]; char buf[2]; BackendInfo *info; if (command_line == NULL || (strlen(command_line) == 0)) return 0; /* check nodeID */ if (node < 0 || node > NUM_BACKENDS) return -1; info = pool_get_node_info(node); if (!info) return -1; buf[1] = '\0'; pool_memory = pool_memory_create(PREPARE_BLOCK_SIZE); if (!pool_memory) { pool_error("trigger_failover_command: pool_memory_create() failed"); return -1; } exec_cmd = init_string(""); while (*command_line) { if (*command_line == '%') { if (*(command_line + 1)) { char val = *(command_line + 1); switch (val) { case 'p': /* port */ snprintf(port_buf, sizeof(port_buf), "%d", info->backend_port); string_append_char(exec_cmd, port_buf); break; case 'D': /* database directory */ string_append_char(exec_cmd, info->backend_data_directory); break; case 'd': /* node id */ snprintf(port_buf, sizeof(port_buf), "%d", node); string_append_char(exec_cmd, port_buf); break; case 'h': /* host name */ string_append_char(exec_cmd, info->backend_hostname); break; case 'm': /* new master node id */ snprintf(port_buf, sizeof(port_buf), "%d", get_next_master_node()); string_append_char(exec_cmd, port_buf); break; case 'M': /* old master node id */ snprintf(port_buf, sizeof(port_buf), "%d", MASTER_NODE_ID); string_append_char(exec_cmd, port_buf); break; case '%': /* escape */ string_append_char(exec_cmd, "%"); break; default: /* ignore */ break; } command_line++; } } else { buf[0] = *command_line; string_append_char(exec_cmd, buf); } command_line++; } if (strlen(exec_cmd->data) != 0) { pool_log("execute command: %s", exec_cmd->data); r = system(exec_cmd->data); } pool_memory_delete(pool_memory, 0); pool_memory = NULL; return r; }