生活随笔
收集整理的這篇文章主要介紹了
OpenGauss线程管理-主线程-Postmaster(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
OpenGauss線程管理-主線程-Postmaster(1)
主線程postmaster負責內存、全局信息、信號、線程池等的初始化,用來創建其他子線程,OpenGauss是單進程多線程,在程序啟動時,一個進程被操作系統創建,同時主線程立刻運行。
主線程運行時可以用來接收前端的命令,對命令做出處理,還會對子線程進行監視,在子線程出現問題時會對子線程進行管理,保證數據庫正常的運行。
主線程是和后端共享的內存,但它不會碰共享內存,避免崩潰、卡住。
PostmasterMain函數
openGauss-server\src\gausskernel\process\postmaster\postmaster.cpp
初始化postmaster配置參數初始化審計模塊建立輸入socket監聽建立共享內存和信號量池初始化postmaster信號管理初始化宕機監聽初始化統計數據收集子系統初始化工作負載管理器初始化unique sql資源
int PostmasterMain(int argc
, char* argv
[])
{int opt
;int status
= STATUS_OK
;char* output_config_variable
= NULL;char* userDoption
= NULL;bool listen_addr_saved
= false;int use_pooler_port
= -1;int i
;OptParseContext optCtxt
;errno_t rc
= 0;Port port
;t_thrd
.proc_cxt
.MyProcPid
= PostmasterPid
= gs_thread_self();t_thrd
.proc_cxt
.MyStartTime
= time(NULL);IsPostmasterEnvironment
= true;t_thrd
.proc_cxt
.MyProgName
= "gaussmaster";umask(S_IRWXG
| S_IRWXO
);t_thrd
.mem_cxt
.postmaster_mem_cxt
= AllocSetContextCreate(t_thrd
.top_mem_cxt
,"Postmaster",ALLOCSET_DEFAULT_MINSIZE
,ALLOCSET_DEFAULT_INITSIZE
,ALLOCSET_DEFAULT_MAXSIZE
);MemoryContextSwitchTo(t_thrd
.mem_cxt
.postmaster_mem_cxt
);initialize_feature_flags();#ifndef ENABLE_LITE_MODEinitOBSCacheObject();S3_init();
#endifpm_set_unzip_memfuncs();cJSON_Hooks hooks
= {cJSON_internal_malloc
, cJSON_internal_free
};cJSON_InitHooks(&hooks
);#ifdef ENABLE_LLVM_COMPILECodeGenProcessInitialize();
#endifgetInstallationPaths(argv
[0]);InitializeGUCOptions();SetcbForGetLCName(GetLogicClusterForAlarm
);optCtxt
.opterr
= 1;initOptParseContext(&optCtxt
);while ((opt
= getopt_r(argc
, argv
, "A:B:bc:C:D:d:EeFf:h:ijk:lM:N:nOo:Pp:Rr:S:sTt:u:W:g:X:-:", &optCtxt
)) != -1) {switch (opt
) {case 'A':SetConfigOption("debug_assertions", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'B':SetConfigOption("shared_buffers", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'b':u_sess
->proc_cxt
.IsBinaryUpgrade
= true;break;case 'C':output_config_variable
= optCtxt
.optarg
;break;case 'D':userDoption
= optCtxt
.optarg
;break;case 'd':set_debug_options(atoi(optCtxt
.optarg
), PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'E':SetConfigOption("log_statement", "all", PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'e':SetConfigOption("datestyle", "euro", PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'F':SetConfigOption("fsync", "false", PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'f':if (!set_plan_disabling_options(optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
)) {write_stderr("%s: invalid argument for option -f: \"%s\"\n", progname
, optCtxt
.optarg
);ExitPostmaster(1);}break;case 'g':SetConfigOption("xlog_file_path", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'h':SetConfigOption("listen_addresses", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'i':SetConfigOption("listen_addresses", "*", PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'j':break;case 'k':SetConfigOption("unix_socket_directory", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'l':SetConfigOption("ssl", "true", PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'M':if (0 == strncmp(optCtxt
.optarg
, "primary", strlen("primary")) &&'\0' == optCtxt
.optarg
[strlen("primary")]) {t_thrd
.xlog_cxt
.server_mode
= PRIMARY_MODE
;} else if (0 == strncmp(optCtxt
.optarg
, "standby", strlen("standby")) &&'\0' == optCtxt
.optarg
[strlen("standby")]) {t_thrd
.xlog_cxt
.server_mode
= STANDBY_MODE
;} else if (0 == strncmp(optCtxt
.optarg
, "pending", strlen("pending")) &&'\0' == optCtxt
.optarg
[strlen("pending")]) {t_thrd
.xlog_cxt
.server_mode
= PENDING_MODE
;} else if (0 == strncmp(optCtxt
.optarg
, "normal", strlen("normal")) &&'\0' == optCtxt
.optarg
[strlen("normal")]) {t_thrd
.xlog_cxt
.server_mode
= NORMAL_MODE
;} else if (0 == strncmp(optCtxt
.optarg
, "cascade_standby", strlen("cascade_standby")) &&'\0' == optCtxt
.optarg
[strlen("cascade_standby")]) {t_thrd
.xlog_cxt
.server_mode
= STANDBY_MODE
;t_thrd
.xlog_cxt
.is_cascade_standby
= true;} else if (0 == strncmp(optCtxt
.optarg
, "hadr_main_standby", strlen("hadr_main_standby")) &&'\0' == optCtxt
.optarg
[strlen("hadr_main_standby")]) {t_thrd
.xlog_cxt
.server_mode
= STANDBY_MODE
;t_thrd
.xlog_cxt
.is_hadr_main_standby
= true;} else {ereport(FATAL
, (errmsg("the options of -M is not recognized")));}break;case 'N':SetConfigOption("max_connections", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'n':ereport(FATAL
, (errmsg("the options of -n is deprecated")));break;case 'O':SetConfigOption("allow_system_table_mods", "true", PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'o':rc
= snprintf_s(g_instance
.ExtraOptions
+ strlen(g_instance
.ExtraOptions
),sizeof(g_instance
.ExtraOptions
) - strlen(g_instance
.ExtraOptions
),sizeof(g_instance
.ExtraOptions
) - strlen(g_instance
.ExtraOptions
) - 1," %s",optCtxt
.optarg
);securec_check_ss(rc
, "", "");break;case 'P':SetConfigOption("ignore_system_indexes", "true", PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'p':SetConfigOption("port", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;
#ifdef ENABLE_MULTIPLE_NODEScase 'R':dummyStandbyMode
= true;break;
#endifcase 'r':break;case 'S':SetConfigOption("work_mem", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;case 's':SetConfigOption("log_statement_stats", "true", PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'T':ereport(FATAL
, (errmsg("the options of -T is deprecated")));break;case 't': {const char* tmp
= get_stats_option_name(optCtxt
.optarg
);if (tmp
!= NULL) {SetConfigOption(tmp
, "true", PGC_POSTMASTER
, PGC_S_ARGV
);} else {write_stderr("%s: invalid argument for option -t: \"%s\"\n", progname
, optCtxt
.optarg
);ExitPostmaster(1);}break;}case 'u':errno
= 0;pg_atomic_write_u32(&WorkingGrandVersionNum
, (uint32
)strtoul(optCtxt
.optarg
, NULL, 10));if (errno
!= 0 || pg_atomic_read_u32(&WorkingGrandVersionNum
) > GRAND_VERSION_NUM
) {write_stderr("%s: invalid argument for option -u: \"%s\", GRAND_VERSION_NUM is %u\n",progname
,optCtxt
.optarg
,(uint32
)GRAND_VERSION_NUM
);ExitPostmaster(1);} else if (pg_atomic_read_u32(&WorkingGrandVersionNum
) == INPLACE_UPGRADE_PRECOMMIT_VERSION
) {pg_atomic_write_u32(&WorkingGrandVersionNum
, GRAND_VERSION_NUM
);g_instance
.comm_cxt
.force_cal_space_info
= true;}break;case 'W':SetConfigOption("post_auth_delay", optCtxt
.optarg
, PGC_POSTMASTER
, PGC_S_ARGV
);break;case 'X':if ((optCtxt
.optarg
!= NULL) && (strlen(optCtxt
.optarg
) > 0)) {if (strlen(optCtxt
.optarg
) > MAX_BARRIER_ID_LENGTH
) {ereport(FATAL
, (errmsg("the options of -X is too long")));}rc
= strncpy_s(g_instance
.csn_barrier_cxt
.stopBarrierId
, MAX_BARRIER_ID_LENGTH
,(char *)optCtxt
.optarg
, strlen(optCtxt
.optarg
));securec_check(rc
, "\0", "\0");ereport(LOG
, (errmsg("Set stop barrierID %s", g_instance
.csn_barrier_cxt
.stopBarrierId
)));}break;case 'c':case '-': {char* name
= NULL;char* value
= NULL;ParseLongOption(optCtxt
.optarg
, &name
, &value
);#ifndef ENABLE_MULTIPLE_NODESif (opt
== '-' && (strcmp(name
, "coordinator") == 0 || strcmp(name
, "datanode") == 0)) {ereport(FATAL
,(errcode(ERRCODE_SYNTAX_ERROR
),errmsg("Single node mode: must start as single node (--single_node)\n")));}
#endifif (name
!= NULL && strcmp(name
, "coordinator") == 0 && value
== NULL)g_instance
.role
= VCOORDINATOR
;else if (name
!= NULL && strcmp(name
, "datanode") == 0 && value
== NULL)g_instance
.role
= VDATANODE
;else if (name
!= NULL && strcmp(name
, "single_node") == 0 && value
== NULL) {g_instance
.role
= VSINGLENODE
;useLocalXid
= true;} else if (name
!= NULL && strcmp(name
, "restoremode") == 0 && value
== NULL) {isRestoreMode
= true;g_instance
.role
= VDATANODE
;} else if (name
!= NULL && 0 == strcmp(name
, "fenced") && value
== NULL)FencedUDFMasterMode
= true;else if (name
!= NULL && strlen(name
) == SECURITY_MODE_NAME_LEN
&&strncmp(name
, SECURITY_MODE_NAME
, SECURITY_MODE_NAME_LEN
) == 0 && value
== NULL) {isSecurityMode
= true;} else {if (value
== NULL) {if (opt
== '-')ereport(ERROR
,(errcode(ERRCODE_SYNTAX_ERROR
), errmsg("--%s requires a value", optCtxt
.optarg
)));elseereport(ERROR
,(errcode(ERRCODE_SYNTAX_ERROR
), errmsg("-c %s requires a value", optCtxt
.optarg
)));}SetConfigOption(name
, value
, PGC_POSTMASTER
, PGC_S_ARGV
);}pfree(name
);if (value
!= NULL) {pfree(value
);}break;}default:write_stderr("Try \"%s --help\" for more information.\n", progname
);ExitPostmaster(1);}}
#if ((defined ENABLE_PYTHON2) || (defined ENABLE_PYTHON3))if (!SelectConfigFiles(userDoption
, progname
))ExitPostmaster(1);if (strlen(GetConfigOption(const_cast<char*>("unix_socket_directory"), true, false)) != 0) {PythonFencedMasterModel
= true;SetConfigOption("enable_bbox_dump", "false", PGC_POSTMASTER
, PGC_S_ARGV
);}
#elseif (FencedUDFMasterMode
) {SetConfigOption("enable_bbox_dump", "false", PGC_POSTMASTER
, PGC_S_ARGV
);} else if (!SelectConfigFiles(userDoption
, progname
)) {ExitPostmaster(1);}
#endifif ((g_instance
.attr
.attr_security
.transparent_encrypted_string
!= NULL &&g_instance
.attr
.attr_security
.transparent_encrypted_string
[0] != '\0') &&(g_instance
.attr
.attr_common
.transparent_encrypt_kms_url
!= NULL &&g_instance
.attr
.attr_common
.transparent_encrypt_kms_url
[0] != '\0') &&(g_instance
.attr
.attr_security
.transparent_encrypt_kms_region
!= NULL &&g_instance
.attr
.attr_security
.transparent_encrypt_kms_region
[0] != '\0')) {isSecurityMode
= true;}InitializePostmasterGUC();t_thrd
.myLogicTid
= noProcLogicTid
+ POSTMASTER_LID
;if (output_config_variable
!= NULL) {puts(GetConfigOption(output_config_variable
, false, false));ExitPostmaster(0);}InitializeNumLwLockPartitions();noProcLogicTid
= GLOBAL_ALL_PROCS
;if (FencedUDFMasterMode
) {if (userDoption
!= NULL && userDoption
[0] == '/') {if (chdir(userDoption
) == -1)ExitPostmaster(1);SetDataDir(userDoption
);} else {ExitPostmaster(1);}gs_thread_args_pool_init(GLOBAL_ALL_PROCS
+ EXTERN_SLOTS_NUM
, sizeof(BackendParameters
));gs_signal_slots_init(GLOBAL_ALL_PROCS
+ EXTERN_SLOTS_NUM
);gs_signal_startup_siginfo("PostmasterMain");gs_signal_monitor_startup();g_instance
.attr
.attr_common
.Logging_collector
= true;g_instance
.global_sysdbcache
.Init(INSTANCE_GET_MEM_CXT_GROUP(MEMORY_CONTEXT_DEFAULT
));CreateLocalSysDBCache();g_instance
.pid_cxt
.SysLoggerPID
= SysLogger_Start();FencedUDFMasterMain(0, NULL);return 0;}checkDataDir();ChangeToDataDir();CheckGUCConflicts();ConfigRecoveryParallelism();ProcessRedoCpuBindInfo();if (!CheckDateTokenTables()) {write_stderr("%s: invalid datetoken tables, please fix\n", progname
);ExitPostmaster(1);}initKnlRTOContext();optCtxt
.optind
= 1;
#ifdef HAVE_INT_OPTRESEToptreset
= 1;
#endifint rc1
= snprintf_s(gaussdb_state_file
, sizeof(gaussdb_state_file
), MAXPGPATH
- 1, "%s/gaussdb.state", t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);gaussdb_state_file
[MAXPGPATH
- 1] = '\0';if (!SetDBStateFileState(UNKNOWN_STATE
, true)) {write_stderr("Failed to set gaussdb.state with UNKNOWN_STATE");ExitPostmaster(1);}if (g_instance
.attr
.attr_storage
.dcf_attr
.enable_dcf
) {rc1
= snprintf_s(AddMemberFile
, sizeof(AddMemberFile
), MAXPGPATH
- 1, "%s/addmember", t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);AddMemberFile
[MAXPGPATH
- 1] = '\0';rc1
= snprintf_s(RemoveMemberFile
, sizeof(RemoveMemberFile
), MAXPGPATH
- 1, "%s/removemember",t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);RemoveMemberFile
[MAXPGPATH
- 1] = '\0';rc1
= snprintf_s(TimeoutFile
, sizeof(TimeoutFile
), MAXPGPATH
- 1, "%s/timeout", t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);TimeoutFile
[MAXPGPATH
- 1] = '\0';rc1
= snprintf_s(SwitchoverStatusFile
, sizeof(SwitchoverStatusFile
), MAXPGPATH
- 1, "%s/switchoverstatus",t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);SwitchoverStatusFile
[MAXPGPATH
- 1] = '\0';rc1
= snprintf_s(SetRunmodeStatusFile
, sizeof(SetRunmodeStatusFile
), MAXPGPATH
- 1, "%s/setrunmodestatus",t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);SetRunmodeStatusFile
[MAXPGPATH
- 1] = '\0';rc1
= snprintf_s(g_changeRoleStatusFile
, sizeof(g_changeRoleStatusFile
), MAXPGPATH
- 1, "%s/changerolestatus",t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);g_changeRoleStatusFile
[MAXPGPATH
- 1] = '\0';rc1
=snprintf_s(ChangeRoleFile
, sizeof(ChangeRoleFile
), MAXPGPATH
- 1, "%s/changerole", t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);ChangeRoleFile
[MAXPGPATH
- 1] = '\0';rc1
= snprintf_s(StartMinorityFile
, sizeof(StartMinorityFile
), MAXPGPATH
- 1, "%s/startminority",t_thrd
.proc_cxt
.DataDir
);securec_check_intval(rc1
, , -1);StartMinorityFile
[MAXPGPATH
- 1] = '\0';}{extern char** environ
;char** p
;ereport(DEBUG3
, (errmsg_internal("%s: PostmasterMain: initial environment dump:", progname
)));ereport(DEBUG3
, (errmsg_internal("-----------------------------------------")));for (p
= environ
; *p
; ++p
)ereport(DEBUG3
, (errmsg_internal("\t%s", *p
)));ereport(DEBUG3
, (errmsg_internal("-----------------------------------------")));}rc
= memcpy_s(g_alarmComponentPath
, MAXPGPATH
- 1, Alarm_component
, strlen(Alarm_component
));securec_check_c(rc
, "\0", "\0");g_alarmReportInterval
= AlarmReportInterval
;AlarmEnvInitialize();CreateDataDirLockFile(true);pgaudit_agent_init();auto_explain_init();ledger_hook_init();process_shared_preload_libraries();for (i
= 0; i
< MAXLISTEN
; i
++)t_thrd
.postmaster_cxt
.ListenSocket
[i
] = PGINVALID_SOCKET
;if (g_instance
.attr
.attr_network
.ListenAddresses
&& !dummyStandbyMode
) {char* rawstring
= NULL;List
* elemlist
= NULL;ListCell
* l
= NULL;int success
= 0;if (CommProxyNeedSetup()) {CommProxyStartUp();}rawstring
= pstrdup(g_instance
.attr
.attr_network
.ListenAddresses
);if (!SplitIdentifierString(rawstring
, ',', &elemlist
)) {ereport(FATAL
,(errcode(ERRCODE_INVALID_PARAMETER_VALUE
), errmsg("invalid list syntax for \"listen_addresses\"")));}bool haswildcard
= false;foreach (l
, elemlist
) {char* curhost
= (char*)lfirst(l
);if (strcmp(curhost
, "*") == 0) {haswildcard
= true;break;}}if (haswildcard
== true) {char *wildcard
= "*";elemlist
= list_cell_clear(elemlist
, (void *)wildcard
, isNotWildcard
);}foreach (l
, elemlist
) {char* curhost
= (char*)lfirst(l
);if (strcmp(curhost
, "*") == 0)status
= StreamServerPort(AF_UNSPEC
,NULL,(unsigned short)g_instance
.attr
.attr_network
.PostPortNumber
,g_instance
.attr
.attr_network
.UnixSocketDir
,t_thrd
.postmaster_cxt
.ListenSocket
,MAXLISTEN
,true,true,false);elsestatus
= StreamServerPort(AF_UNSPEC
,curhost
,(unsigned short)g_instance
.attr
.attr_network
.PostPortNumber
,g_instance
.attr
.attr_network
.UnixSocketDir
,t_thrd
.postmaster_cxt
.ListenSocket
,MAXLISTEN
,true,true,false);if (status
== STATUS_OK
)success
++;else {print_port_info();ereport(FATAL
,(errmsg("could not create listen socket for \"%s:%d\"",curhost
,g_instance
.attr
.attr_network
.PostPortNumber
)));}use_pooler_port
= NeedPoolerPort(curhost
);if (t_thrd
.xlog_cxt
.server_mode
== NORMAL_MODE
|| use_pooler_port
== -1) {if (g_instance
.attr
.attr_network
.PoolerPort
!= (g_instance
.attr
.attr_network
.PostPortNumber
+ 1)) {ereport(FATAL
, (errmsg("pooler_port must equal to gsql listen port plus one!")));}if (strcmp(curhost
, "*") == 0) {status
= StreamServerPort(AF_UNSPEC
,NULL,(unsigned short)g_instance
.attr
.attr_network
.PoolerPort
,g_instance
.attr
.attr_network
.UnixSocketDir
,t_thrd
.postmaster_cxt
.ListenSocket
,MAXLISTEN
,false,false,false);} else {status
= StreamServerPort(AF_UNSPEC
,curhost
,(unsigned short)g_instance
.attr
.attr_network
.PoolerPort
,g_instance
.attr
.attr_network
.UnixSocketDir
,t_thrd
.postmaster_cxt
.ListenSocket
,MAXLISTEN
,false,false,false);}if (status
!= STATUS_OK
)ereport(FATAL
,(errmsg("could not create ha listen socket for \"%s:%d\"",curhost
,g_instance
.attr
.attr_network
.PoolerPort
)));if (!listen_addr_saved
&& !IsInplicitIp(curhost
)) {AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR
, curhost
);listen_addr_saved
= true;}}}if (!success
&& list_length(elemlist
))ereport(FATAL
, (errmsg("could not create any TCP/IP sockets")));list_free_ext(elemlist
);pfree(rawstring
);}if (t_thrd
.xlog_cxt
.server_mode
!= NORMAL_MODE
) {SetListenSocket(t_thrd
.postmaster_cxt
.ReplConnArray
, &listen_addr_saved
);ReportResumeAbnormalDataHAInstListeningSocket();}SetListenSocket(t_thrd
.postmaster_cxt
.CrossClusterReplConnArray
, &listen_addr_saved
);ReportResumeAbnormalDataHAInstListeningSocket();
#ifdef USE_BONJOURif (g_instance
.attr
.attr_common
.enable_bonjour
&& t_thrd
.postmaster_cxt
.ListenSocket
[0] != PGINVALID_SOCKET
) {DNSServiceErrorType err
;err
= DNSServiceRegister(&bonjour_sdref
,0,0,g_instance
.attr
.attr_common
.bonjour_name
,"_postgresql._tcp.",NULL,NULL,htons(g_instance
.attr
.attr_network
.PostPortNumber
),0,NULL,NULL,NULL);if (err
!= kDNSServiceErr_NoError
)ereport(LOG
, (errmsg("DNSServiceRegister() failed: error code %ld", (long)err
)));}#endif#ifdef HAVE_UNIX_SOCKETSif (!dummyStandbyMode
) {status
= StreamServerPort(AF_UNIX
,NULL,(unsigned short)g_instance
.attr
.attr_network
.PostPortNumber
,g_instance
.attr
.attr_network
.UnixSocketDir
,t_thrd
.postmaster_cxt
.ListenSocket
,MAXLISTEN
,false,true,false);if (status
!= STATUS_OK
)ereport(FATAL
,(errmsg("could not create Unix-domain socket for \"%s:%d\"",g_instance
.attr
.attr_network
.UnixSocketDir
,g_instance
.attr
.attr_network
.PostPortNumber
)));status
= StreamServerPort(AF_UNIX
,NULL,(unsigned short)g_instance
.attr
.attr_network
.PoolerPort
,g_instance
.attr
.attr_network
.UnixSocketDir
,t_thrd
.postmaster_cxt
.ListenSocket
,MAXLISTEN
,false,false,false);if (status
!= STATUS_OK
)ereport(FATAL
,(errmsg("could not create Unix-domain socket for \"%s:%d\"",g_instance
.attr
.attr_network
.UnixSocketDir
,g_instance
.attr
.attr_network
.PoolerPort
)));if (g_instance
.attr
.attr_storage
.comm_cn_dn_logic_conn
&& !isRestoreMode
&& !IS_SINGLE_NODE
) {status
= StreamServerPort(AF_UNIX
,NULL,(unsigned short)g_instance
.attr
.attr_network
.comm_sctp_port
,g_instance
.attr
.attr_network
.UnixSocketDir
,t_thrd
.postmaster_cxt
.ListenSocket
,MAXLISTEN
,false,true,true);if (status
!= STATUS_OK
)ereport(WARNING
, (errmsg("could not create Unix-domain for comm socket")));}}
#endifif (t_thrd
.postmaster_cxt
.ListenSocket
[0] == PGINVALID_SOCKET
) {ereport(FATAL
, (errmsg("no socket created for listening")));}on_proc_exit(CloseServerPorts
, 0);if (!listen_addr_saved
) {AddToDataDirLockFile(LOCK_FILE_LINE_LISTEN_ADDR
, "");ereport(WARNING
, (errmsg("No explicit IP is configured for listen_addresses GUC.")));}if (g_instance
.attr
.attr_common
.enable_thread_pool
) {if (!dummyStandbyMode
) {g_threadPoolControler
= (ThreadPoolControler
*)New(INSTANCE_GET_MEM_CXT_GROUP(MEMORY_CONTEXT_EXECUTOR
)) ThreadPoolControler();g_threadPoolControler
->SetThreadPoolInfo();} else {g_instance
.attr
.attr_common
.enable_thread_pool
= false;g_threadPoolControler
= NULL;AdjustThreadAffinity();}}int errcode
= gstrace_init(g_instance
.attr
.attr_network
.PostPortNumber
);if (errcode
!= 0) {ereport(LOG
, (errmsg("gstrace initializes with failure. errno = %d.", errcode
)));}InitGlobalBcm();reset_shared(g_instance
.attr
.attr_network
.PostPortNumber
);BackendArrayAllocation();gs_thread_args_pool_init(GLOBAL_ALL_PROCS
+ EXTERN_SLOTS_NUM
, sizeof(BackendParameters
));gs_signal_slots_init(GLOBAL_ALL_PROCS
+ EXTERN_SLOTS_NUM
);gs_signal_startup_siginfo("PostmasterMain");gs_signal_monitor_startup();SetHaShmemData();PMInitDBStateFile();set_max_safe_fds();set_stack_base();g_instance
.backend_list
= DLNewList();InitPostmasterDeathWatchHandle();#ifdef WIN32win32ChildQueue
= CreateIoCompletionPort(INVALID_HANDLE_VALUE
, NULL, 0, 1);if (win32ChildQueue
== NULL)ereport(FATAL
, (errmsg("could not create I/O completion port for child queue")));#endifif (!CreateOptsFile(argc
, (const char**)argv
, (const char*)my_exec_path
))ExitPostmaster(1);#ifdef EXEC_BACKENDwrite_nondefault_variables(PGC_POSTMASTER
);
#endif#ifndef ENABLE_LITE_MODE
#if defined (ENABLE_MULTIPLE_NODES) || defined (ENABLE_PRIVATEGAUSS)if (hotpatch_remove_signal_file(t_thrd
.proc_cxt
.DataDir
) == HP_OK
) {int ret
;ret
= hotpatch_init(t_thrd
.proc_cxt
.DataDir
, (HOTPATCH_LOG_FUNC
)gs_hotpatch_log_callback
);if (ret
!= HP_OK
) {write_stderr("hotpatch init failed ret is %d!\n", ret
);}}
#endif
#endifif (g_instance
.attr
.attr_common
.external_pid_file
) {FILE
* fpidfile
= fopen(g_instance
.attr
.attr_common
.external_pid_file
, "w");if (fpidfile
!= NULL) {fprintf(fpidfile
, "%lu\n", t_thrd
.proc_cxt
.MyProcPid
);fclose(fpidfile
);if (chmod(g_instance
.attr
.attr_common
.external_pid_file
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
) != 0)write_stderr("%s: could not change permissions of external PID file \"%s\": %s\n",progname
,g_instance
.attr
.attr_common
.external_pid_file
,gs_strerror(errno
));} elsewrite_stderr("%s: could not write external PID file \"%s\": %s\n",progname
,g_instance
.attr
.attr_common
.external_pid_file
,gs_strerror(errno
));}gs_signal_setmask(&t_thrd
.libpq_cxt
.BlockSig
, NULL);gs_signal_block_sigusr2();(void)gspqsignal(SIGHUP
, SIGHUP_handler
); (void)gspqsignal(SIGINT
, pmdie
); (void)gspqsignal(SIGQUIT
, pmdie
); (void)gspqsignal(SIGTERM
, pmdie
); pqsignal(SIGALRM
, SIG_IGN
); pqsignal(SIGPIPE
, SIG_IGN
); pqsignal(SIGFPE
, FloatExceptionHandler
);(void)gspqsignal(SIGUSR1
, sigusr1_handler
); (void)gspqsignal(SIGUSR2
, dummy_handler
); (void)gspqsignal(SIGCHLD
, reaper
); (void)gspqsignal(SIGTTIN
, SIG_IGN
); (void)gspqsignal(SIGTTOU
, SIG_IGN
);
#ifdef SIGXFSZ(void)gspqsignal(SIGXFSZ
, SIG_IGN
);
#endifbbox_initialize();pgstat_init();GlobalStatsTrackerInit();InitializeWorkloadManager();g_instance
.global_sysdbcache
.Init(INSTANCE_GET_MEM_CXT_GROUP(MEMORY_CONTEXT_DEFAULT
));CreateLocalSysDBCache();ProcSubXidCacheContext
= AllocSetContextCreate(g_instance
.instance_context
,"ProcSubXidCacheContext",ALLOCSET_DEFAULT_MINSIZE
,ALLOCSET_DEFAULT_INITSIZE
,ALLOCSET_DEFAULT_MAXSIZE
,SHARED_CONTEXT
);StreamInfoContext
= AllocSetContextCreate(g_instance
.instance_context
,"StreamInfoContext",ALLOCSET_DEFAULT_MINSIZE
,ALLOCSET_DEFAULT_INITSIZE
,ALLOCSET_DEFAULT_MAXSIZE
,SHARED_CONTEXT
);knl_g_cachemem_create();ngroup_info_hash_create();InitRoleIdHashTable();RealInitialMMapLockArray();InitUniqueSQL();InitHypopg();InitAsp();InitInstrUser();InitOpfusionFunctionId();init_capture_view();InitPercentile();InitTrackStmtControl();InitGlobalSeq();
#ifdef ENABLE_MULTIPLE_NODESCompactionProcess::init_instance();if (g_instance
.attr
.attr_common
.enable_tsdb
) {TagsCacheMgr::GetInstance().init();PartIdMgr::GetInstance().init();Tsdb
::PartCacheMgr::GetInstance().init();InitExtensiblePlanMethodsHashTable();}
#endifg_instance
.pid_cxt
.SysLoggerPID
= SysLogger_Start();if (IS_PGXC_DATANODE
&& !dummyStandbyMode
&& !isRestoreMode
) {StreamObj::startUp();StreamNodeGroup::StartUp();pthread_mutex_init(&nodeDefCopyLock
, NULL);}InitDnHashTable();int loadhbaCount
= 0;while (!load_hba()) {check_old_hba(true);loadhbaCount
++;if (loadhbaCount
>= 3) {ereport(FATAL
, (errmsg("could not load pg_hba.conf")));}pg_usleep(200000L); }if (ENABLE_THREAD_POOL_DN_LOGICCONN
) {InitCommLogicResource();}if ((!IS_SINGLE_NODE
) &&((IS_PGXC_DATANODE
&& !dummyStandbyMode
&& !isRestoreMode
) ||(IS_PGXC_COORDINATOR
&& g_instance
.attr
.attr_storage
.comm_cn_dn_logic_conn
&& !isRestoreMode
))) {status
= init_stream_comm();if (status
!= STATUS_OK
)ereport(FATAL
, (errmsg("Init libcomm for stream failed, maybe listen port already in use")));}if (g_instance
.attr
.attr_security
.enable_tde
) {TDE
::CKMSMessage::get_instance().init();TDE
::CKMSMessage::get_instance().load_user_info();if (IS_PGXC_DATANODE
) {TDE
::TDEKeyStorage::get_instance().init();TDE
::TDEBufferCache::get_instance().init();}}#ifndef ENABLE_LITE_MODEif (g_instance
.attr
.attr_storage
.enable_adio_function
)AioResourceInitialize();
#endifif (!dummyStandbyMode
)g_instance
.pid_cxt
.AlarmCheckerPID
= startAlarmChecker();g_instance
.pid_cxt
.ReaperBackendPID
= initialize_util_thread(REAPER
);t_thrd
.postgres_cxt
.whereToSendOutput
= DestNone
;autovac_init();load_ident();RemovePgTempFiles();RemoveErrorCacheFiles();t_thrd
.time_cxt
.pg_start_time
= GetCurrentTimestamp();gs_lock_test_and_set_64(&g_instance
.stat_cxt
.NodeStatResetTime
, GetCurrentTimestamp());gettimeofday(&t_thrd
.postmaster_cxt
.random_start_time
, NULL);ShareStorageInit();g_instance
.pid_cxt
.StartupPID
= initialize_util_thread(STARTUP
);Assert(g_instance
.pid_cxt
.StartupPID
!= 0);pmState
= PM_STARTUP
;#ifdef ENABLE_MULTIPLE_NODESif (IS_PGXC_COORDINATOR
) {MemoryContext oldcontext
= MemoryContextSwitchTo(THREAD_GET_MEM_CXT_GROUP(MEMORY_CONTEXT_DEFAULT
));StartPoolManager();MemoryContextSwitchTo(oldcontext
);}load_searchserver_library();
#endifif (g_instance
.attr
.attr_storage
.dcf_attr
.enable_dcf
) {int ss_rc
= memset_s(&port
, sizeof(port
), 0, sizeof(port
));securec_check(ss_rc
, "\0", "\0");port
.sock
= PGINVALID_SOCKET
;BackendVariablesGlobal
= static_cast<BackendParameters *>(palloc(sizeof(BackendParameters
)));save_backend_variables(BackendVariablesGlobal
, &port
);}if (PythonFencedMasterModel
) {g_instance
.attr
.attr_common
.Logging_collector
= true;g_instance
.pid_cxt
.SysLoggerPID
= SysLogger_Start();StartUDFMaster();}if (status
== STATUS_OK
)status
= ServerLoop();ExitPostmaster(status
!= STATUS_OK
);return 0;
}
總結
以上是生活随笔為你收集整理的OpenGauss线程管理-主线程-Postmaster(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。