/* 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; }
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; }
int SimpleStringTokens( SimpStringPtr str, char split, SimpStringPtr *tokens, int *tokensize) { if ( str->Str == NULL || str->Len == 0 ) { *tokens = NULL; *tokensize = 0; return FUNC_RETURN_OK; } *tokensize = 1; for( int i = 0 ; i < str->Len ; ++i ) { if ( str->Str[i] == split ) { (*tokensize)++; } } *tokens = rm_palloc0(str->Context, sizeof(SimpString) * (*tokensize)); char *prevp = str->Str; char *p = str->Str; for ( int i = 0 ; i < *tokensize ; ++i ) { while( *p != split && *p != '\0' ) p++; initSimpleString(&((*tokens)[i]), str->Context); setSimpleStringWithContent(&((*tokens)[i]), prevp, p-prevp); p++; prevp = p; } return FUNC_RETURN_OK; }
/* * Node creation helper. */ BBSTNode createBBSTNode(BBST tree, void *data) { BBSTNode res = NULL; Assert( tree != NULL ); Assert( data != NULL ); /* Check if freed nodes can be reused to avoid memory allocation cost. */ if ( tree->Free != NULL ) { res = tree->Free; tree->Free = tree->Free->Right; res->Right = NULL; } else { res = (BBSTNode) rm_palloc0( tree->Context, sizeof(struct BBSTNodeData)); /* Expect that palloc0 set the attributes: Left, Right, Parent to NULLs. */ } /* Set node properties. */ res->Data = data; res->NodeCount = 1; res->Depth = 1; return res; }
int deserializeToSimpleString(SimpStringPtr str, char *content) { Assert(str != NULL); str->Len = *((int32_t *)content); str->Str = (char *)rm_palloc0(str->Context, (str->Len+1)); memcpy(str->Str, content+4, str->Len); return __SIZE_ALIGN64(str->Len + sizeof(int32_t)); }
SimpArrayPtr createSimpleArray(MCTYPE context) { SimpArrayPtr res = (SimpArrayPtr) rm_palloc0(context, sizeof(SimpArray)); res->Array = NULL; res->Len = -1; res->Context = context; return res; }
void setSimpleStringFilled( SimpStringPtr str, uint8_t val, int length) { Assert( str != NULL ); str->Str = (char *)rm_palloc0(str->Context, length+1); memset(str->Str, val, length); str->Len = length; }
SimpStringPtr createSimpleString(MCTYPE context) { SimpStringPtr res = (SimpStringPtr) rm_palloc0(context, sizeof(SimpString)); res->Str = NULL; res->Len = -1; res->Context = context; return res; }
/* * Create BBST. */ BBST createBBST(MCTYPE context, void *comparg, CompareFunctionType compfunc) { BBST res = NULL; Assert(compfunc != NULL); res = (BBST)rm_palloc0(context, sizeof(struct BBSTData)); initializeBBST(res, context, comparg, compfunc); return res; }
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; }
HostAddress createHostAddressAsStringFromIPV4AddressStr(MCTYPE context, const char *addr) { static uint32_t addrlen = 0; HostAddress result = NULL; int resultsize = 0; result = rm_palloc0(context, sizeof(HostAddressData)); result->Attribute.Offset = 0; result->Attribute.Mark |= HOST_ADDRESS_CONTENT_STRING; addrlen = strlen(addr); resultsize = __SIZE_ALIGN64(offsetof(AddressStringData, Address) + addrlen + 1 ); AddressString straddr = rm_palloc0(context, resultsize); straddr->Length = addrlen; strcpy(straddr->Address, addr); result->Address = (char *)straddr; result->AddressSize = resultsize; return result; }
AsyncCommMessageHandlerContext createMessageHandlerContext(SyncRPCContext content) { AsyncCommMessageHandlerContext context = NULL; context = rm_palloc0(AsyncCommContext, sizeof(AsyncCommMessageHandlerContextData)); context->inMessage = false; context->UserData = NULL; context->MessageRecvReadyHandler = NULL; context->MessageRecvedHandler = receivedSyncRPCResponse; context->MessageSendReadyHandler = NULL; context->MessageSentHandler = sentSyncRPCRequest; context->MessageErrorHandler = sentSyncRPCRequestError; context->MessageCleanUpHandler = sentSyncRPCRequestCleanUp; context->UserData = (void *)content; return context; }
/* Register message handlers for clien FDs. */ AsyncCommMessageHandlerContext createConnTrackHandlerContext(void) { AsyncCommMessageHandlerContext result = rm_palloc0(AsyncCommContext, sizeof(AsyncCommMessageHandlerContextData)); result->inMessage = false; result->UserData = NULL; result->MessageRecvReadyHandler = NULL; result->MessageRecvedHandler = addMessageToConnTrack; result->MessageSendReadyHandler = NULL; result->MessageSentHandler = sentMessageFromConnTrack; result->MessageErrorHandler = hasCommErrorInConnTrack; result->MessageCleanUpHandler = cleanupConnTrack; elog(DEBUG3, "Created AsyncComm Message context."); return result; }
void createAndLockSessionResource(ResqueueDeadLockDetector detector, int64_t sessionid) { SessionTrack curstrack = NULL; /* 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 ) { curstrack = (SessionTrack)rm_palloc0(PCONTEXT, sizeof(SessionTrackData)); curstrack->SessionID = sessionid; curstrack->Locked = false; resetResourceBundleData(&(curstrack->InUseTotal), 0, 0.0, 0); /* Add to the detector. */ setHASHTABLENode(&(detector->Sessions), &key, curstrack, false); } else { curstrack = (SessionTrack)(pair->Value); } Assert( curstrack != NULL ); Assert( !curstrack->Locked ); curstrack->Locked = true; addResourceBundleDataByBundle(&(detector->LockedTotal), &(curstrack->InUseTotal)); elog(RMLOG, "Locked session "INT64_FORMAT" Locked %d MB", sessionid, detector->LockedTotal.MemoryMB); }
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 getHostIPV4AddressesByHostNameAsString(MCTYPE context, const char *hostname, SimpStringPtr ohostname, List **addresses) { Assert(hostname != NULL); Assert(ohostname != NULL); Assert(addresses != NULL); char ipstr[32]; struct hostent *hent = NULL; *addresses = NULL; /* Try to resolve this host by hostname. */ for ( int i = 0 ; i < NETWORK_RETRY_TIMES ; ++i ) { hent = gethostbyname(hostname); if( hent != NULL ) { break; } else if ( h_errno != TRY_AGAIN ) { write_log("Failed to call gethostbyname() to get host %s, %s", hostname, hstrerror(h_errno)); break; } pg_usleep(NETWORK_RETRY_SLEEP_US); } if ( hent == NULL ) { write_log("Failed to resolve host %s.", hostname); return SYSTEM_CALL_ERROR; } setSimpleStringNoLen(ohostname, hent->h_name); if ( hent->h_addrtype != AF_INET ) { return FUNC_RETURN_OK; /* No IPv4 addresses. addresses is set NULL. */ } /* This switch is to support List operation */ MEMORY_CONTEXT_SWITCH_TO(context) for ( char **paddr = hent->h_addr_list ; *paddr != NULL ; paddr++ ) { inet_ntop(hent->h_addrtype, *paddr, ipstr, sizeof(ipstr)); int newaddrlen = strlen(ipstr); AddressString newaddr = (AddressString) rm_palloc0(context, __SIZE_ALIGN64( offsetof(AddressStringData, Address) + newaddrlen + 1)); newaddr->Length = newaddrlen; memcpy(newaddr->Address, ipstr, newaddrlen+1); *addresses = lappend(*addresses, (void *)newaddr); } MEMORY_CONTEXT_SWITCH_BACK 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 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; }