int registerFileDescForAsyncConn(int fd, uint32_t actionmask_afterconn, AsyncCommBufferHandlers methods_afterconn, void *userdata_afterconn, AsyncCommBuffer *newcommbuffer_afterconn) { AsyncCommBufferHandlerUser userdata = NULL; userdata = rm_palloc0(AsyncCommContext, sizeof(AsyncCommBufferHandlerUserData)); userdata->ActionMaskForAfterConn = actionmask_afterconn; userdata->UserDataForAfterConn = userdata_afterconn; userdata->MethodsForAfterConn = methods_afterconn; elog(DEBUG3, "Created AsyncComm Conn context."); int res = registerFileDesc(fd, ASYNCCOMM_WRITE, &AsyncCommBufferHandlersConn, userdata, newcommbuffer_afterconn); if ( res != FUNC_RETURN_OK ) { elog(WARNING, "Fail to register communication in asynchronous connection " "progress."); rm_pfree(AsyncCommContext, userdata); } elog(DEBUG3, "Registered AsyncComm FD %d", fd); return res; }
void freeBBST(BBST tree) { clearBBST(tree); freeBBSTFreeNodes(tree); freeHASHTABLE(tree->NodeIndex); rm_pfree(tree->Context, tree); }
void CleanUpHandler_Message(AsyncCommBuffer buffer) { AsyncCommMessageHandlerContext context = (AsyncCommMessageHandlerContext) (buffer->UserData); Assert( context->MessageCleanUpHandler != NULL ); context->MessageCleanUpHandler(context); rm_pfree(AsyncCommContext, context); elog(DEBUG3, "Freed AsyncComm Message context."); }
/* free simple array with allocated content. */ void freeSimpleArrayContent(SimpArrayPtr array) { Assert( array != NULL ); if ( array->Array != NULL ) { rm_pfree(array->Context, array->Array); } array->Array = NULL; array->Len = -1; }
void freeBBSTFreeNodes(BBST tree) { Assert( tree != NULL ); while( tree->Free != NULL ) { BBSTNode tofree = tree->Free; tree->Free = tofree->Right; rm_pfree(tree->Context, tofree); } }
void freeSimpleStringContent(SimpStringPtr str) { Assert( str != NULL ); if ( str->Str != NULL ) { rm_pfree(str->Context, str->Str); } str->Str = NULL; str->Len = -1; }
void resetResourceDeadLockDetector(ResqueueDeadLockDetector detector) { List *allss = NULL; ListCell *cell = NULL; getAllPAIRRefIntoList(&(detector->Sessions), &allss); foreach(cell, allss) { SessionTrack strack = (SessionTrack)(((PAIR)lfirst(cell))->Value); rm_pfree(PCONTEXT, strack); }
void ReadReadyHandler_MsgServer(AsyncCommBuffer buffer) { int res = FUNC_RETURN_OK; ConnectionTrack newtrack = NULL; ConnectionTrackData tmptrackdata; AsyncCommBuffer newcommbuffer = NULL; struct sockaddr_in clientaddr; socklen_t clientaddrlen = sizeof(clientaddr); int fd = -1; /* Always accept the connection. */ fd = accept(buffer->FD, (struct sockaddr *)&clientaddr, &clientaddrlen); if ( fd == -1 ) { elog(WARNING, "Resource manager socket accept error is detected. errno %d", errno); return; } /* Create AsyncComm Message handler instance. */ AsyncCommMessageHandlerContext context = createConnTrackHandlerContext(); /* Add new client fd into AsyncComm manager. */ res = registerFileDesc(fd, ASYNCCOMM_READBYTES | ASYNCCOMM_WRITEBYTES, &AsyncCommBufferHandlersMessage, context, &newcommbuffer); if ( res != FUNC_RETURN_OK ) { Assert(newcommbuffer == NULL); /* close the connection and cleanup. */ closeConnectionRemote(&fd); rm_pfree(AsyncCommContext, context); return; } assignFileDescClientAddressInfo(newcommbuffer, NULL, 0, &clientaddr, clientaddrlen); /* Let context able to track comm buffer. */ context->AsyncBuffer = newcommbuffer; /* Call callback function to initialize context for message handlers. */ InitHandler_Message(newcommbuffer); elog(DEBUG3, "Resource manager accepted one client connected from " "%s:%d as FD %d.\n", newcommbuffer->ClientAddrDotStr, newcommbuffer->ClientAddrPort, newcommbuffer->FD); }
void freeSimpleStringTokens(SimpStringPtr owner, SimpStringPtr *tokens, int tokensize) { for ( int i = 0 ; i < tokensize ; ++i ) { freeSimpleStringContent(&((*tokens)[i])); } rm_pfree(owner->Context, *tokens); *tokens = NULL; }
void WriteReadyHandler_Connect(AsyncCommBuffer buffer) { /* Check the error from socket FD. */ bool shouldclose = false; int error = 0; socklen_t errlen = sizeof(error); elog(DEBUG3, "AsyncConn FD %d is write ready for checking connection.", buffer->FD); int res = getsockopt(buffer->FD, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen); if (res < 0) { elog(WARNING, "getsocketopt() on FD %d have errors raised. errno %d", buffer->FD, errno); shouldclose = true; } else if ( error > 0 ) { elog(WARNING, "FD %d having errors raised. errno %d", buffer->FD, error); shouldclose = true; } if ( setConnectionLongTermNoDelay(buffer->FD) != FUNC_RETURN_OK ) { shouldclose = true; } if ( shouldclose ) { ErrorHandler_Connect(buffer); forceCloseFileDesc(buffer); return; } elog(DEBUG3, "AsyncConn FD %d is a good connection.", buffer->FD); /* Change to normal registered FD by changing callbacks and arguments. */ AsyncCommBufferHandlerUser userdata = (AsyncCommBufferHandlerUser)(buffer->UserData); buffer->ActionMask = userdata->ActionMaskForAfterConn; buffer->UserData = userdata->UserDataForAfterConn; buffer->Methods = userdata->MethodsForAfterConn; Assert(getSMBContentSize(&(buffer->ReadBuffer)) == 0); elog(DEBUG3, "Freed AsyncComm Conn context ( connected )."); rm_pfree(AsyncCommContext, userdata); }
void freeHostIPV4AddressesAsString(MCTYPE context, List **addresses) { Assert( addresses != NULL ); MEMORY_CONTEXT_SWITCH_TO(context) while( *addresses != NULL ) { rm_pfree(context, lfirst(list_head(*addresses))); *addresses = list_delete_first(*addresses); } MEMORY_CONTEXT_SWITCH_BACK }
void ErrorHandler_Connect(AsyncCommBuffer buffer) { elog(WARNING, "Resource manager socket connect has error raised."); AsyncCommBufferHandlerUser userdata = (AsyncCommBufferHandlerUser)(buffer->UserData); buffer->ActionMask = userdata->ActionMaskForAfterConn; buffer->UserData = userdata->UserDataForAfterConn; buffer->Methods = userdata->MethodsForAfterConn; rm_pfree(AsyncCommContext, userdata); elog(DEBUG3, "Freed AsyncComm Conn context ( error )."); buffer->Methods->ErrorReadyHandle(buffer); }
void CleanUpHandler_Connect(AsyncCommBuffer buffer) { elog(LOG, "Clean up handler in socket connect is called."); AsyncCommBufferHandlerUser userdata = (AsyncCommBufferHandlerUser)(buffer->UserData); buffer->ActionMask = userdata->ActionMaskForAfterConn; buffer->UserData = userdata->UserDataForAfterConn; buffer->Methods = userdata->MethodsForAfterConn; rm_pfree(AsyncCommContext, userdata); elog(DEBUG3, "Freed AsyncComm Conn context ( clean up )."); buffer->Methods->CleanUpHandle(buffer); }
/* set array value. */ void setSimpleArrayWithContent( SimpArrayPtr array, char *content, int length) { Assert( array != NULL ); if ( array->Array == NULL ) { array->Array = (char *)rm_palloc0(array->Context, length+1); } else if ( array->Len < length ) { rm_pfree(array->Context, array->Array); array->Array = (char *)rm_palloc0(array->Context, length+1); } memcpy(array->Array, content, length); array->Len = length; }
void RB_freeClusterReport(List **segments) { if (CurrentRBImp.freeClusterReport != NULL) { CurrentRBImp.freeClusterReport(segments); return; } MEMORY_CONTEXT_SWITCH_TO(PCONTEXT) /* default implementation for freeing the cluster report objects. */ while( list_length(*segments) > 0 ) { SegInfo seginfo = (SegInfo)lfirst(list_head(*segments)); *segments = list_delete_first(*segments); rm_pfree(PCONTEXT, seginfo); } MEMORY_CONTEXT_SWITCH_BACK }
void setSimpleStringWithContent( SimpStringPtr str, char *content, int length) { Assert( str != NULL ); if ( str->Str == NULL ) { //elog(RMLOG, "SET NEW SIMPSTRING: %d::%s", length, content); str->Str = (char *)rm_palloc0(str->Context, length+1); } else if ( str->Len < length ) { //elog(RMLOG, "SET UPD SIMPSTRING: %d::%s", length, content); rm_pfree(str->Context, str->Str); str->Str = (char *)rm_palloc0(str->Context, length+1); } memcpy(str->Str, content, length); str->Str[length] = '\0'; str->Len = length; }
int minusSessionInUseResource(ResqueueDeadLockDetector detector, int64_t sessionid, uint32_t memorymb, double core) { /* Build key */ SimpArray key; setSimpleArrayRef(&key, (char *)&sessionid, sizeof(int64_t)); /* Check if the session id exists. */ PAIR pair = getHASHTABLENode(&(detector->Sessions), &key); if ( pair == NULL ) { return RESQUEMGR_NO_SESSIONID; } minusResourceBundleData(&(detector->InUseTotal), memorymb, core); SessionTrack sessiontrack = (SessionTrack)(pair->Value); Assert( sessiontrack != NULL ); minusResourceBundleData(&(sessiontrack->InUseTotal), memorymb, core); Assert(detector->InUseTotal.Core >= 0.0 && detector->InUseTotal.MemoryMB >= 0); Assert(sessiontrack->InUseTotal.Core >= 0.0 && sessiontrack->InUseTotal.MemoryMB >= 0); /* If the session has no resource used, remove the session tracker. */ if ( sessiontrack->InUseTotal.MemoryMB == 0 && sessiontrack->InUseTotal.Core == 0.0 ) { rm_pfree(PCONTEXT, sessiontrack); removeHASHTABLENode(&(detector->Sessions), &key); } return FUNC_RETURN_OK; }
void sentIncreaseMemoryQuotaCleanup(AsyncCommMessageHandlerContext context) { /* Free user data in message */ GRMContainerSet ctns = (GRMContainerSet)(context->UserData); if ( ctns->Containers == NULL ) { elog(DEBUG3, "Resource manager succeeded cleaning up data for increasing " "memory quota message"); } else { elog(WARNING, "Resource manager failed to cleanup data for increasing memory " "quota message"); processContainersAfterIncreaseMemoryQuota(ctns, false); } Assert( ctns->Containers == NULL ); rm_pfree(PCONTEXT, (GRMContainerSet)(context->UserData)); /* Mark that there is one RPC done for increasing resource quota. */ DRMGlobalInstance->IncreaseMemoryRPCCounter--; Assert(DRMGlobalInstance->IncreaseMemoryRPCCounter >= 0); }
int decreaseMemoryQuota(char *seghostname, GRMContainerSet rescontainerset) { int res = FUNC_RETURN_OK; AsyncCommBuffer commbuffer = NULL; GRMContainerSet ctns = NULL; Assert( seghostname != NULL ); Assert( rescontainerset->Containers != NULL ); /* Move resource containers that should be returned to communication context.*/ ctns = createGRMContainerSet(); moveGRMContainerSetContainerList(ctns, rescontainerset); /* Build request */ SelfMaintainBufferData tosend; initializeSelfMaintainBuffer(&tosend, PCONTEXT); RPCRequestUpdateMemoryQuotaData request; request.MemoryQuotaDelta = ctns->Allocated.MemoryMB; GRMContainer firstcont = getGRMContainerSetContainerFirst(ctns); Assert( firstcont != NULL ); request.MemoryQuotaTotalPending = firstcont->Resource->Allocated.MemoryMB + firstcont->Resource->OldInuse.MemoryMB; Assert( request.MemoryQuotaTotalPending >= 0 ); elog(LOG, "Resource manager decrease host %s memory quota "UINT64_FORMAT" MB, " "Expected total memory quota after decreasing is " UINT64_FORMAT" MB. " "Include %d MB old in-use memory quota.", seghostname, request.MemoryQuotaDelta, request.MemoryQuotaTotalPending, firstcont->Resource->OldInuse.MemoryMB); appendSMBVar(&tosend, request); /* Set content to send and add to AsyncComm framework */ AsyncCommMessageHandlerContext context = (AsyncCommMessageHandlerContext)rm_palloc0(AsyncCommContext, sizeof(AsyncCommMessageHandlerContextData)); context->inMessage = false; context->UserData = ctns; context->MessageRecvReadyHandler = NULL; context->MessageRecvedHandler = recvDecreaseMemoryQuotaResponse; context->MessageSendReadyHandler = NULL; context->MessageSentHandler = sentDecreaseMemoryQuota; context->MessageErrorHandler = sentDecreaseMemoryQuotaError; context->MessageCleanUpHandler = sentDecreaseMemoryQuotaCleanup; res = registerAsyncConnectionFileDesc(NULL, seghostname, rm_segment_port, ASYNCCOMM_READBYTES | ASYNCCOMM_WRITEBYTES, &AsyncCommBufferHandlersMessage, context, &commbuffer); if ( res != FUNC_RETURN_OK ) { elog(LOG, "Resource manager failed to set connection to segment host %s " "on port %d to decrease memory quota.", seghostname, rm_segment_port); processContainersAfterDecreaseMemoryQuota(ctns, false); freeGRMContainerSet(ctns); rm_pfree(AsyncCommContext, context); return res; } else { elog(DEBUG3, "Resource manager succeeded set connection to segment host %s " "on port %d to decrease memory quota.", seghostname, rm_segment_port); } buildMessageToCommBuffer(commbuffer, tosend.Buffer, tosend.Cursor+1, REQUEST_RM_DECREASE_MEMORY_QUOTA, 0, 0); destroySelfMaintainBuffer(&tosend); context->AsyncBuffer = commbuffer; InitHandler_Message(commbuffer); return FUNC_RETURN_OK; }
/****************************************************************************** * I aM Alive. * * Request: * |<----------- 64 bits (8 bytes) ----------->| * +----------+--------------------------------+ * | TDC | BDC | Reserved | * +----------+----------+---------------------+ * | | * | Machine ID info | * | | * +-------------------------------------------+ _____ 64bit aligned * * Response: * |<----------- 64 bits (8 bytes) ----------->| * +---------------------+---------------------+ * | heartbeat result | Reserved | * +---------------------+---------------------+ _____ 64bit aligned * ******************************************************************************/ int sendIMAlive(int *errorcode, char *errorbuf, int errorbufsize) { int res = FUNC_RETURN_OK; AsyncCommBuffer newcommbuffer = NULL; Assert( DRMGlobalInstance->LocalHostStat != NULL ); /* Build request. */ SelfMaintainBufferData tosend; initializeSelfMaintainBuffer(&tosend, PCONTEXT); RPCRequestHeadIMAliveData requesthead; requesthead.TmpDirCount = getDQueueLength(&DRMGlobalInstance->LocalHostTempDirectories); requesthead.TmpDirBrokenCount = DRMGlobalInstance->LocalHostStat->FailedTmpDirNum; requesthead.Reserved = 0; appendSMBVar(&tosend, requesthead); appendSelfMaintainBuffer(&tosend, (char *)(DRMGlobalInstance->LocalHostStat), offsetof(SegStatData, Info) + DRMGlobalInstance->LocalHostStat->Info.Size); /* Set content to send and add to AsyncComm framework. */ AsyncCommMessageHandlerContext context = rm_palloc0(AsyncCommContext, sizeof(AsyncCommMessageHandlerContextData)); context->inMessage = false; context->UserData = NULL; context->MessageRecvReadyHandler = NULL; context->MessageRecvedHandler = receivedIMAliveResponse; context->MessageSendReadyHandler = NULL; context->MessageSentHandler = sentIMAlive; context->MessageErrorHandler = sentIMAliveError; context->MessageCleanUpHandler = sentIMAliveCleanUp; /* Connect to HAWQ RM server */ res = registerAsyncConnectionFileDesc(NULL, DRMGlobalInstance->SendToStandby? standby_addr_host: master_addr_host, rm_master_port, ASYNCCOMM_READBYTES | ASYNCCOMM_WRITEBYTES, &AsyncCommBufferHandlersMessage, context, &newcommbuffer); if ( res != FUNC_RETURN_OK ) { rm_pfree(AsyncCommContext, context); elog(WARNING, "Fail to register asynchronous connection for sending " "IMAlive message. %d", res); return res; } buildMessageToCommBuffer(newcommbuffer, tosend.Buffer, tosend.Cursor + 1, REQUEST_RM_IMALIVE, 0, 0); destroySelfMaintainBuffer(&tosend); context->AsyncBuffer = newcommbuffer; InitHandler_Message(newcommbuffer); return FUNC_RETURN_OK; }
int sendRUAlive(char *seghostname) { int res = FUNC_RETURN_OK; AsyncCommBuffer newcommbuffer = NULL; SegResource segres = NULL; int32_t segid = SEGSTAT_ID_INVALID; Assert(seghostname != NULL); res = getSegIDByHostName(seghostname, strlen(seghostname), &segid); if ( res != FUNC_RETURN_OK ) { elog(WARNING, "Resource manager cannot get registered host %s when send RUAlive message, " "error code: %d", seghostname, res); return res; } segres = getSegResource(segid); Assert(segres != NULL); bool oldstat = setSegResRUAlivePending(segres, true); if( oldstat != false ) { Assert(false); } /* Build request. */ SelfMaintainBufferData tosend; initializeSelfMaintainBuffer(&tosend, PCONTEXT); RPCRequestRUAliveData request; request.Reserved = 0; appendSMBVar(&tosend, request); AsyncCommMessageHandlerContext context = (AsyncCommMessageHandlerContext) rm_palloc0(AsyncCommContext, sizeof(AsyncCommMessageHandlerContextData)); context->inMessage = false; context->MessageRecvReadyHandler = NULL; context->MessageRecvedHandler = receivedRUAliveResponse; context->MessageSendReadyHandler = NULL; context->MessageSentHandler = sentRUAlive; context->MessageErrorHandler = sentRUAliveError; context->MessageCleanUpHandler = sentRUAliveCleanUp; context->UserData = (void *)segres; /* Connect to HAWQ RM server */ res = registerAsyncConnectionFileDesc(NULL, seghostname, rm_segment_port, ASYNCCOMM_READBYTES | ASYNCCOMM_WRITEBYTES, &AsyncCommBufferHandlersMessage, context, &newcommbuffer); if ( res != FUNC_RETURN_OK ) { rm_pfree(AsyncCommContext, context); elog(WARNING, "Fail to register asynchronous connection for sending " "RUAlive message. %d", res); return res; } buildMessageToCommBuffer(newcommbuffer, tosend.Buffer, tosend.Cursor+1, REQUEST_RM_RUALIVE, 0, 0); destroySelfMaintainBuffer(&tosend); context->AsyncBuffer = newcommbuffer; InitHandler_Message(newcommbuffer); return FUNC_RETURN_OK; }
int callSyncRPCRemote(const char *hostname, uint16_t port, const char *sendbuff, int sendbuffsize, uint16_t sendmsgid, uint16_t exprecvmsgid, SelfMaintainBuffer recvsmb, char *errorbuf, int errorbufsize) { int fd = -1; int res = FUNC_RETURN_OK; AsyncCommBuffer newcommbuffer = NULL; AsyncCommMessageHandlerContext context = NULL; SyncRPCContextData userdata; /* Connect to the server side. */ res = connectToServerRemote(hostname, port, &fd); if ( res != FUNC_RETURN_OK ) { snprintf(errorbuf, errorbufsize, "failed to connect to remote socket server %s:%d", hostname, port); elog(WARNING, "%s", errorbuf); goto exit; } initializeSyncRPContent(&userdata, recvsmb, exprecvmsgid); context = createMessageHandlerContext(&userdata); res = registerFileDesc(fd, NULL, ASYNCCOMM_READBYTES | ASYNCCOMM_WRITEBYTES, &AsyncCommBufferHandlersMessage, context, &newcommbuffer); if ( res != FUNC_RETURN_OK ) { rm_pfree(AsyncCommContext, context); snprintf(errorbuf, errorbufsize, "failed to register socket connection fd %d connected to %s:%d " "for resource rpc communication", fd, hostname, port); elog(WARNING, "%s", errorbuf); goto exit; } buildMessageToCommBuffer(newcommbuffer, sendbuff, sendbuffsize, sendmsgid, 0, 0); context->AsyncBuffer = newcommbuffer; InitHandler_Message(newcommbuffer); /* Wait for the complete of the communication. */ while( true ) { processAllCommFileDescs(); if ( userdata.CommStatus == SYNC_RPC_COMM_IDLE ) { break; } else if ( QueryCancelPending ) { /* * We find that this QD wants to cancel the query, we don't need * to continue the communication. */ res = TRANSCANCEL_INPROGRESS; break; } } res = res == TRANSCANCEL_INPROGRESS ? res : userdata.Result; /* Close and cleanup */ closeAndRemoveAllRegisteredFileDesc(); if (res != FUNC_RETURN_OK) { elog(WARNING, "Sync RPC framework (inet) finds exception raised."); switch(res) { case COMM2RM_CLIENT_FAIL_SEND: snprintf(errorbuf, errorbufsize, "failed to send content"); break; case COMM2RM_CLIENT_FAIL_RECV: snprintf(errorbuf, errorbufsize, "failed to receive content"); break; case REQUESTHANDLER_WRONGMESSAGE: snprintf(errorbuf, errorbufsize, "wrong message was received"); break; default: Assert(false); } } return res; exit: elog(LOG, "Close fd %d at once", fd); closeConnectionRemote(&fd); return res; }
void freeHostAddress(MCTYPE context, HostAddress addr) { rm_pfree(context, addr->Address); rm_pfree(context, addr); }
int callSyncRPCDomain(const char *sockfile, const char *sendbuff, int sendbuffsize, uint16_t sendmsgid, uint16_t exprecvmsgid, SelfMaintainBuffer recvsmb) { static char dfilename[1024]; int fd = -1; int res = FUNC_RETURN_OK; AsyncCommBuffer newcommbuffer = NULL; AsyncCommMessageHandlerContext context = NULL; SyncRPCContextData userdata; /* Connect to the server side. */ res = connectToServerDomain(sockfile, 0, &fd, 0, dfilename); if ( res != FUNC_RETURN_OK ) { elog(WARNING, "Fail to connect to domain socket server %s, result %d", sockfile, res); goto exit; } initializeSyncRPContent(&userdata, recvsmb, exprecvmsgid); context = createMessageHandlerContext(&userdata); res = registerFileDesc(fd, dfilename, ASYNCCOMM_READBYTES | ASYNCCOMM_WRITEBYTES, &AsyncCommBufferHandlersMessage, context, &newcommbuffer); if ( res != FUNC_RETURN_OK ) { rm_pfree(AsyncCommContext, context); elog(WARNING, "Fail to register FD for synchronous communication. %d", res); goto exit; } buildMessageToCommBuffer(newcommbuffer, sendbuff, sendbuffsize, sendmsgid, 0, 0); context->AsyncBuffer = newcommbuffer; InitHandler_Message(newcommbuffer); /* Wait for the complete of the communication. */ while( true ) { processAllCommFileDescs(); if ( userdata.CommStatus == SYNC_RPC_COMM_IDLE ) { break; } else if ( QueryCancelPending ) { /* * We find that this QD wants to cancel the query, we don't need * to continue the communication. */ res = TRANSCANCEL_INPROGRESS; break; } } res = res == TRANSCANCEL_INPROGRESS ? res : userdata.Result; /* Close and cleanup */ closeAndRemoveAllRegisteredFileDesc(); if (res != FUNC_RETURN_OK) { elog(WARNING, "Sync RPC framework (domain) finds exception raised."); } return res; exit: closeConnectionDomain(&fd, dfilename); return res; }