Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
change spinlock to event-based for blackholes.
(may fix github #52)
  • Loading branch information
klirichek committed Feb 7, 2018
1 parent 7a99bbd commit 5fcf016
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
66 changes: 36 additions & 30 deletions src/searchd.cpp
Expand Up @@ -1483,6 +1483,7 @@ static bool SaveIndexes ()
}

static void ClearDistributedHash();
CSphAutoEvent g_dBlackholeEvent;

void Shutdown ()
{
Expand All @@ -1496,6 +1497,8 @@ void Shutdown ()
*g_bDaemonAtShutdown.GetWritePtr() = 1;
}

g_dBlackholeEvent.SetEvent();

#if !USE_WINDOWS
// stopwait handshake
CSphString sPipeName = GetNamedPipeName ( getpid() );
Expand Down Expand Up @@ -14752,34 +14755,40 @@ struct BlackholeRequestBuilder_t : public IRequestBuilder_t

struct BlackholeReplyParser_t : public IReplyParser_t
{
BlackholeReplyParser_t ()
{}

virtual bool ParseReply ( MemInputBuffer_c & , AgentConn_t & ) const
bool ParseReply ( MemInputBuffer_c & , AgentConn_t & ) const override
{
return true;
}
};


CSphMutex g_tBlackholeLock;
static CSphVector<AgentSendData_t *> g_dBlackholeAgents;

static CSphVector<AgentSendData_t *> g_dBlackholeAgents GUARDED_BY ( g_tBlackholeLock );

static const int g_iBlackholeTimeout = 5;
static const int g_iBlackholeRetries = 3;

static void BlackholeTick()
{
if ( !g_dBlackholeAgents.GetLength() )
g_dBlackholeEvent.WaitEvent ();

if ( g_bShutdown )
return;

ThreadSystem_t tThdSystemDesc ( "BLACKHOLE" );

VectorPtrsGuard_T<AgentSendData_t> tBlackholes;
// grab blackholes under lock
g_tBlackholeLock.Lock();
tBlackholes.m_dPtrs.SwapData ( g_dBlackholeAgents );
g_tBlackholeLock.Unlock();

{
CSphScopedLock<CSphMutex> BlackHoleGuard { g_tBlackholeLock };

if ( !g_dBlackholeAgents.GetLength () )
return;

// grab blackholes under lock
tBlackholes.m_dPtrs.SwapData ( g_dBlackholeAgents );
}

// agent vector for remote controller
AgentsVector dAgents;
Expand Down Expand Up @@ -14833,22 +14842,17 @@ static void BlackholeThreadFunc ( void * )
SphCrashLogger_c::SetTopQueryTLS ( &tQueryTLS );

while ( !g_bShutdown )
{
if ( !g_dBlackholeAgents.GetLength() )
{
sphSleepMsec ( 1 );
continue;
}

BlackholeTick();
}

// clean up
g_tBlackholeLock.Lock();
ARRAY_FOREACH ( i, g_dBlackholeAgents )
SafeDelete ( g_dBlackholeAgents[i] );
g_dBlackholeAgents.Reset();
g_tBlackholeLock.Unlock();
{
CSphScopedLock<CSphMutex> BlackHoleGuard { g_tBlackholeLock };
for ( auto * dBlackHoleAgent : g_dBlackholeAgents )
SafeDelete ( dBlackHoleAgent );
g_dBlackholeAgents.Reset ();
}

g_dBlackholeEvent.Done();
}

void BlackholeAddAgents ( AgentsVector & dAgents, const IRequestBuilder_t & tReq )
Expand Down Expand Up @@ -14879,13 +14883,13 @@ void BlackholeAddAgents ( AgentsVector & dAgents, const IRequestBuilder_t & tReq
}

// move blackholes to pool for process
g_tBlackholeLock.Lock();

AgentSendData_t ** ppNew = g_dBlackholeAgents.AddN ( dBlackholes.GetLength() );
ARRAY_FOREACH ( i, dBlackholes )
ppNew[i] = dBlackholes[i];

g_tBlackholeLock.Unlock();
{
CSphScopedLock<CSphMutex> tLock { g_tBlackholeLock };
AgentSendData_t ** ppNew = g_dBlackholeAgents.AddN ( dBlackholes.GetLength() );
ARRAY_FOREACH ( i, dBlackholes )
ppNew[i] = dBlackholes[i];
}
g_dBlackholeEvent.SetEvent ();
}

static bool HasBlackhole ( AgentsVector & dAgents )
Expand Down Expand Up @@ -24312,6 +24316,8 @@ int WINAPI ServiceMain ( int argc, char **argv )
if ( !g_tPingThread.Create ( PingThreadFunc, 0 ) )
sphDie ( "failed to create ping service thread" );

if ( !g_dBlackholeEvent.Init() )
sphDie ( "failed to create blackhole service event" );
if ( !g_tBlackholeThread.Create ( BlackholeThreadFunc, 0 ) )
sphDie ( "failed to create blackhole service thread" );

Expand Down
3 changes: 1 addition & 2 deletions src/searchdhttp.cpp
Expand Up @@ -1618,7 +1618,7 @@ bool HttpHandlerPQ_c::GotDocuments ( PercolateIndex_i * pIndex, const CSphString

CSphString sTokenFilterOpts;
SphDocID_t uDocID = 1;
ARRAY_FOREACH ( iDoc, dDocs )
for ( const cJSON * pDoc : dDocs )
{
// reset all back to defaults
dFields.Fill ( sTmp.scstr() );
Expand All @@ -1629,7 +1629,6 @@ bool HttpHandlerPQ_c::GotDocuments ( PercolateIndex_i * pIndex, const CSphString
tLoc.m_bDynamic = true;
tDoc.SetDefaultAttr ( tLoc, tCol.m_eAttrType );
}
const cJSON * pDoc = dDocs[iDoc];
const cJSON * pChild = nullptr;
cJSON_ArrayForEach ( pChild, pDoc )
{
Expand Down

0 comments on commit 5fcf016

Please sign in to comment.