Beispiel #1
0
int clsAsyncPipeMng::Init(clsConfigure *poConf)
{
    int iRet;
    m_tIdleIdxList.clear();

    memset(m_aiGroupCnt, 0, sizeof(m_aiGroupCnt));

    for (uint32_t i = 0; i < MAX_ASYNC_PIPE_NUM; ++i)
    {
        iRet = MakeNonBlockPipe(m_aaAsyncPipe[i][0],
                m_aaAsyncPipe[i][1]);
        AssertEqual(iRet, 0);

#if 0
        iRet = SetNonBlock(m_aaAsyncPipe[i][0], false);
        AssertEqual(iRet, 0);
#endif

        m_tIdleIdxList.push_back(i);

        m_aiEntityIDMap[i] = INVALID_ENTITY_ID;
    }

    m_poCertain = clsCertainWrapper::GetInstance();
    m_iMaxGroupLimit = m_poCertain->GetCertainUser()->GetControlGroupLimit();
    CertainLogZero("MAX_ASYNC_PIPE_NUM %u m_iMaxGroupLimit %u",
            MAX_ASYNC_PIPE_NUM, m_iMaxGroupLimit);

    return 0;
}
Beispiel #2
0
static int basic_udata(void *tdata) {
  struct basic_test_data *btd = (struct basic_test_data *)tdata;

  AssertEqual(nsock_pool_get_udata(btd->nsp), NULL);
  nsock_pool_set_udata(btd->nsp, btd);
  AssertEqual(nsock_pool_get_udata(btd->nsp), btd);
  return 0;
}
Beispiel #3
0
	void Validate() const
	{
		if(!performSanityChecks) return;

		m_segregatedRangeLists.Validate(BoundaryTagManager::s_headerId);
		m_stats.Validate();

		AssertEqual(m_stats.FreeBlocks(), m_segregatedRangeLists.FreeBlocks(), m_boundaryTagManager.FreeBlocks());
		AssertEqual(m_stats.FreeBytes(), m_segregatedRangeLists.FreeBytes(), m_boundaryTagManager.FreeBytes());
	}
Beispiel #4
0
void clsEntityWorker::Run()
{
	SetThreadTitle("entity_%u_%u", m_iLocalAcceptorID, m_iWorkerID);
	CertainLogDebug("worker_id %u run", m_iWorkerID);

	while (1)
	{
		bool bHasWork = false;

		// 1.Do with IO request.
		clsCmdBase *poCmd = NULL;
		int iRet = m_poIOReqQueue->TakeByOneThread(&poCmd);
		if (iRet == 0)
		{
			assert(poCmd != NULL);
			bHasWork = true;

			iRet = DoWithIOReq(poCmd);
			if (iRet < 0)
			{
				CertainLogError("DoWithIOReq ret %d cmd %s",
						iRet, poCmd->GetTextCmd().c_str());
			}
			// iRet = 1: reuse the ptr, use clsRefPtr in future
			if (iRet != 1)
			{
				delete poCmd, poCmd = NULL;
			}
		}

		// 2.Do with PLog response.
		clsPaxosCmd *poPaxosCmd;
		iRet = m_poPLogRspQueue->TakeByOneThread(&poPaxosCmd);
		if (iRet == 0)
		{
			bHasWork = true;

			iRet = DoWithPLogRsp(poPaxosCmd);
			if (iRet < 0)
			{
				const EntryRecord_t &tSrcRecord = poPaxosCmd->GetSrcRecord();
				CertainLogError("DoWithPLogRsp ret %d record %s",
						iRet, EntryRecordToString(tSrcRecord).c_str());
			}
			delete poPaxosCmd, poPaxosCmd = NULL;
		}

		// 3.Do with Timeout EntryState.
		iRet = DoWithTimeout();
		AssertEqual(iRet, 0);

		if (!bHasWork)
		{
			usleep(1);
		}
	}
}
Beispiel #5
0
// iPtr is for check only.
int clsAsyncPipeMng::SyncWriteByPipeIdx(uint32_t iIdx, uintptr_t iPtr)
{
    CertainLogDebug("iIdx %u iPtr %lu", iIdx, iPtr);
    AssertLess(iIdx, MAX_ASYNC_PIPE_NUM);

    int iOutFD = m_aaAsyncPipe[iIdx][1];
    int iRet = write(iOutFD, &iPtr, sizeof(iPtr));
    AssertEqual(iRet, sizeof(iPtr));
    return 0;
}
Beispiel #6
0
int clsAsyncPipeMng::SyncWaitByPipeIdx(uint32_t iIdx, uintptr_t iPtr)
{
    CertainLogDebug("iIdx %u iPtr %lu", iIdx, iPtr);
    int iInFD = m_aaAsyncPipe[iIdx][0];

    struct pollfd PollFd; 
    PollFd.events = POLLIN | POLLERR;
    PollFd.fd = iInFD;

    int iEventCnt = 0;
    int iTimeout = 1000;
    int iTimeoutCnt = 0;

    while (true)
    {
        errno = 0;
        iEventCnt = poll(&PollFd, 1, iTimeout);
        if (errno == EINTR)
        {
            continue;
        }
        else if (iEventCnt > 0)
        {
            break;
        }
        else
        {
            iTimeoutCnt++;
            OSS::ReportPollTimeout();

            // It won't be timeout forever.
            CertainLogError("iPtr %lu PipeIdx %u iTimeoutCnt %d",
                    iPtr, iIdx, iTimeoutCnt);
        }
    }

    if (iEventCnt <= 0)
    {
        return -1;
    }

    uintptr_t iRetPtr;
    int iRet = read(iInFD, &iRetPtr, sizeof(iRetPtr));
    AssertEqual(iRet, sizeof(iRetPtr));

    if (iRetPtr != iPtr)
    {
        CertainLogFatal("BUG %lu %lu", iRetPtr, iPtr);
        return eRetCodePipePtrErr;
    }

    return 0;
}
Beispiel #7
0
void clsConnWorker::HandleIOEvent(clsFDBase *poFD)
{
	int iRet;
	clsFDContext *poFDCtx = dynamic_cast<clsFDContext *>(poFD);

	if (poFDCtx->IsListening())
	{
		while (1)
		{
			iRet = HandleListening(poFDCtx);
			if (iRet == 0)
			{
				continue;
			}
			else if (iRet < 0)
			{
				// (TODO)rock: remove and close
				CertainLogError("HandleListening ret %d", iRet);
				break;
			}

			AssertEqual(iRet, 1);
			break;
		}
	}
	else
	{
		clsNegoContext *poNegoCtx = dynamic_cast<clsNegoContext *>(poFD);
		clsAutoDelete<clsNegoContext> oAuto(poNegoCtx);

		iRet = RecvNegoMsg(poNegoCtx);
		if (iRet != 0)
		{
			CertainLogError("RecvNegoMsg ret %d", iRet);
			m_poEpollIO->RemoveAndCloseFD(dynamic_cast<clsFDBase *>(poNegoCtx));
			return;
		}

		m_poEpollIO->Remove(dynamic_cast<clsFDBase *>(poNegoCtx));

		iRet = clsConnInfoMng::GetInstance()->PutByOneThread(
				poNegoCtx->GetAcceptorID(), poNegoCtx->GetConnInfo());
		if (iRet != 0)
		{
			CertainLogError("clsConnInfoMng PutByOneThread ret %d", iRet);
			m_poEpollIO->RemoveAndCloseFD(dynamic_cast<clsFDBase *>(poNegoCtx));
		}
	}
}
Beispiel #8
0
static int connect_tcp_failure(void *tdata) {
  struct connect_test_data *ctd = (struct connect_test_data *)tdata;
  struct sockaddr_in peer;

  memset(&peer, 0, sizeof(peer));
  peer.sin_family = AF_INET;
  inet_aton("127.0.0.1", &peer.sin_addr);

  /* pass in addrlen == 0 to force connect(2) to fail */
  nsock_connect_tcp(ctd->nsp, ctd->nsi, connect_handler, 4000, NULL,
                    (struct sockaddr *)&peer, 0, PORT_TCP);

  nsock_loop(ctd->nsp, 4000);
  AssertEqual(ctd->connect_result, -EINVAL);
  return 0;
}
Beispiel #9
0
int clsConnWorker::RecvNegoMsg(clsNegoContext *poNegoCtx)
{
	int iRet;

	const ConnInfo_t &tConnInfo = poNegoCtx->GetConnInfo();
	int iFD = tConnInfo.iFD;

	uint8_t acNego[1];

	while (1)
	{
		iRet = read(iFD, acNego, 1);
		if (iRet == -1)
		{
			if (errno == EAGAIN || errno == EINTR)
			{
				continue;
			}

			CertainLogError("fd %d errno %d", iFD, errno);
			return -1;
		}
		else if (iRet == 0)
		{
			Endpoint_t tPeerAddr = tConnInfo.tPeerAddr;
			CertainLogError("closed by peer %s:%hu fd %d",
					tPeerAddr.sIP, tPeerAddr.hPort, iFD);
			return -2;
		}
		else
		{
			AssertEqual(iRet, 1);
			break;
		}
	}

	// (TODO)rock: uncomment this
	//uint32_t iAcceptorID = uint32_t(acNego[0]);
	uint32_t iAcceptorID = uint32_t(acNego[0] - '0');
	poNegoCtx->SetAcceptorID(iAcceptorID);

	CertainLogDebug("iFD %d local %s:%hu peer %s:%hu iAcceptorID %u",
			iFD, tConnInfo.tLocalAddr.sIP, tConnInfo.tLocalAddr.hPort,
			tConnInfo.tPeerAddr.sIP, tConnInfo.tPeerAddr.hPort, iAcceptorID);

	return 0;
}
Beispiel #10
0
void clsEntityWorker::SyncEntryRecord(EntryInfo_t *ptInfo,
		clsPaxosCmd *poPaxosCmd)
{
	uint32_t iAcceptorID = poPaxosCmd->GetDestAcceptorID();

	uint32_t iEntityID = ptInfo->iEntityID;
	uint32_t iEntry = ptInfo->iEntry;

	clsEntryStateMachine *poMachine = ptInfo->poMachine;
	const EntryRecord_t &tSrcRecord = poMachine->GetRecord(m_iLocalAcceptorID);

	CertainLogDebug("state %d bRemoteUpdated %u bBroadcast %u record %s",
			poMachine->GetEntryState(), ptInfo->bRemoteUpdated,
			ptInfo->bBroadcast, EntryRecordToString(tSrcRecord).c_str());

	if (ptInfo->bBroadcast)
	{
		AssertEqual(iAcceptorID, INVALID_ACCEPTOR_ID);

		BroadcastToRemote(iEntityID, iEntry, poMachine);
		ptInfo->bBroadcast = false;
		ptInfo->bRemoteUpdated = false;
	}

	if (ptInfo->bRemoteUpdated)
	{
		AssertNotEqual(iAcceptorID, INVALID_ACCEPTOR_ID);

		const EntryRecord_t &tDestRecord = poMachine->GetRecord(iAcceptorID);

		// If dest has been chosen, SendDataPacket makes no matter.
		if (!tDestRecord.bChosen)
		{
			clsPaxosCmd *po = new clsPaxosCmd(m_iLocalAcceptorID, iEntityID,
					iEntry, &tSrcRecord, &tDestRecord);

			po->SetDestAcceptorID(iAcceptorID);
			m_poIOWorkerRouter->GoAndDeleteIfFailed(po);
		}
		ptInfo->bRemoteUpdated = false;
	}
}
Beispiel #11
0
int clsConnWorker::AcceptOneFD(clsListenContext *poContext,
		ConnInfo_t &tConnInfo)
{
	int iListenFD = poContext->GetFD();

	struct sockaddr_in tSockAddr;
	socklen_t tLen = sizeof(tSockAddr);

	int iFD = accept(iListenFD, (struct sockaddr *)(&tSockAddr), &tLen);
	if (iFD == -1)
	{
		if (errno == EAGAIN)
		{
			return 1;
		}

		// (TODO)rock: close it
		CertainLogError("accept ret -1 errno %d", errno);
		return -1;
	}

	int iFlags = fcntl(iFD, F_GETFL, 0);
	int iRet = fcntl(iFD, F_SETFL, iFlags | O_NONBLOCK);
	AssertEqual(iRet, 0);

	Endpoint_t tPeerAddr;
	strcpy(tPeerAddr.sIP, inet_ntoa(tSockAddr.sin_addr));
	tPeerAddr.hPort = ntohs(tSockAddr.sin_port);

	tConnInfo.tLocalAddr = poContext->GetLocalAddr();
	tConnInfo.tPeerAddr = tPeerAddr;
	tConnInfo.iFD = iFD;

	CertainLogDebug("iFD %d local %s:%hu peer %s:%hu",
			iFD, tConnInfo.tLocalAddr.sIP, tConnInfo.tLocalAddr.hPort,
			tConnInfo.tPeerAddr.sIP, tConnInfo.tPeerAddr.hPort);

	return 0;
}
Beispiel #12
0
int clsEntityWorker::DoWithPLogRsp(clsPaxosCmd *poPaxosCmd)
{
	int iRet;
	uint32_t iEntityID = poPaxosCmd->GetEntityID();
	uint32_t iEntry = poPaxosCmd->GetEntry();

	CertainLogDebug("iEntityID %u iEntry %u", iEntityID, iEntry);

	EntityInfo_t *ptEntityInfo = m_poEntityMng->GetEntityInfo(iEntityID);
	assert(ptEntityInfo != NULL);

	EntryInfo_t *ptInfo = m_poEntryMng->FindEntryInfo(iEntityID, iEntry);
	assert(ptInfo != NULL);

	assert(ptInfo->bUncertain);
	ptInfo->bUncertain = false;

	SyncEntryRecord(ptInfo, poPaxosCmd);

	clsEntryStateMachine *poMachine = ptInfo->poMachine;
	const EntryRecord_t &tSrcRecord = poMachine->GetRecord(m_iLocalAcceptorID);

	if (CERTAIN_DEBUG)
	{
		string strInfoRecord = EntryRecordToString(poPaxosCmd->GetSrcRecord());
		string strMachineRecord = EntryRecordToString(tSrcRecord);
		assert(strInfoRecord == strMachineRecord);
	}

	list<clsPaxosCmd *> tWaitingList;
	swap(*ptInfo->ptWaitingList, tWaitingList);

	if (tWaitingList.size() > 0)
	{
		CertainLogDebug("iEntityID %u iEntry %u tWaitingList.size() %lu",
				iEntityID, iEntry, tWaitingList.size());
	}

	if (poMachine->GetEntryState() == kEntryStateChosen)
	{
		clsCmdFactory *poCmdFactory = clsCmdFactory::GetInstance();
		// (TODO)rock: reduce PB once
		clsCmdBase *poCmd = poCmdFactory->CreateCmd(
				tSrcRecord.strValue.c_str(), tSrcRecord.strValue.size());

		clsClientCmd *poClientCmd = ptEntityInfo->poClientCmd;
		clsClientCmd *poChosenCmd = dynamic_cast<clsClientCmd *>(poCmd);

		if (poClientCmd != NULL && poClientCmd->GetEntry() == iEntry)
		{
			CertainLogDebug("cli_cmd: %s chosen: %s",
					poClientCmd->GetTextCmd().c_str(),
					EntryRecordToString(tSrcRecord).c_str());

			if (poClientCmd->GetUUID() == poChosenCmd->GetUUID())
			{
				delete poChosenCmd, poChosenCmd = NULL;

				poChosenCmd = ptEntityInfo->poClientCmd;
				ptEntityInfo->poClientCmd = NULL;

				poChosenCmd->SetNeedRsp(true);

				if (ptEntityInfo->iMaxEntryChosenByLocal < iEntry)
				{
					ptEntityInfo->iMaxEntryChosenByLocal = iEntry;
				}
			}
			else
			{
				CertainLogError("uuid (%lu %lu) cmd %s",
						poClientCmd->GetUUID(), poChosenCmd->GetUUID(),
						poClientCmd->GetTextCmd().c_str());

				InvalidClientCmd(ptEntityInfo);
			}
		}

		// (TODO)rock: Push Rsp here instead of in DB thread
		poChosenCmd->SetEntry(iEntry);
		iRet = m_poDBReqQueue->PushByMultiThread(poChosenCmd);
		AssertEqual(iRet, 0); // (TODO)rock: dbpending

		if (ptEntityInfo->iMaxChosenEntry < iEntry)
		{
			ptEntityInfo->iMaxChosenEntry = iEntry;
		}

		CleanUpEntry(ptEntityInfo, ptInfo);
	}

	iRet = DoWithWaitingList(&tWaitingList);
	if (iRet != 0)
	{
		CertainLogError("DoWithWaitingList ret %d", iRet);
	}

	return 0;
}
Beispiel #13
0
void csRectRegionDebug::UnitTest()
{

  csPrintf("Running tests");

  int i,j;
  csRectRegion rr;
  rr.makeEmpty();
  MakeEmpty();
  AssertEqual(rr);



  // include test
  for(i= 0; i < 500; i++)
  {
    csRect r = RandNonEmptyRect();
    rr.Include(r);
    Include(r);
    AssertEqual(rr);
  }

  rr.makeEmpty();
  MakeEmpty();
  AssertEqual(rr);

  for(i = 0; i < 500; i++)
  {
    csRect r = RandRect();
    rr.Include(r);
    Include(r);
    AssertEqual(rr);
  }

  rr.makeEmpty();
  MakeEmpty();
  AssertEqual(rr);
  


  //exclude test
  csRect r(0,0,CS_RECT_REG_SIZE,CS_RECT_REG_SIZE);
  Include(r);
  rr.Include(r);
  AssertEqual(rr);

  for(i = 0; i < 500; i++)
  {
    csRect r = RandRect();
    rr.Exclude(r);
    Exclude(r);
    AssertEqual(rr);
  }

  csRect r1(0,0,CS_RECT_REG_SIZE,CS_RECT_REG_SIZE);
  Include(r1);
  rr.Include(r1);
  AssertEqual(rr);

  for(i = 0; i < 500; i++)
  {
    csRect r = RandNonEmptyRect();
    rr.Exclude(r);
    Exclude(r);
    AssertEqual(rr);
  }



  // Clip test
  rr.makeEmpty();
  MakeEmpty();
  AssertEqual(rr);

  for(i = 0; i < 100; i++)
  {
    for(j = 0; j < 50; j++)
    {
      csRect r = RandNonEmptyRect();
      rr.Include(r);
      Include(r);
    }

    csRect r = RandNonEmptyRect();
    ClipTo(r);
    rr.ClipTo(r);
    AssertEqual(rr);
  }

  rr.makeEmpty();
  MakeEmpty();
  AssertEqual(rr);

  for(i = 0; i < 10; i++)
  {
    for(j = 0; j < 50; j++)
    {
      csRect r = RandRect();
      rr.Include(r);
      Include(r);
    }

    csRect r = RandRect();
    ClipTo(r);
    rr.ClipTo(r);
    AssertEqual(rr);
  }

  // intermixed op test
  rr.makeEmpty();
  MakeEmpty();
  AssertEqual(rr);

  for(i = 0; i < 50000; i++)
  {

    if(i % 1000 == 0)
      csPrintf(".");

    unsigned int op = rand->Get(11);
    csRect r = RandRect();
    if(op < 5)
    {
      Include(r);
      rr.Include(r);
    }
    else if(op < 10)
    {
      Exclude(r);
      rr.Exclude(r);
    }
    else
    {
      ClipTo(r);
      rr.ClipTo(r);
    }

    AssertEqual(rr);
  }

  csPrintf("Done\n");

}