Skip to content

Commit b01af24

Browse files
authored
Merge pull request #3932 from manticoresoftware/fix/lock-tables-buddy-daemon
Fix/lock tables buddy daemon
2 parents 1a091f2 + 1c00de5 commit b01af24

File tree

9 files changed

+81
-29
lines changed

9 files changed

+81
-29
lines changed

src/coroutine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,11 @@ bool ReadTableLock_c::UnlockRead() noexcept
11671167
return true;
11681168
}
11691169

1170+
[[nodiscard]] DWORD ReadTableLock_c::GetReads() const noexcept
1171+
{
1172+
return m_uReads;
1173+
}
1174+
11701175
ScopedWriteTable_c::ScopedWriteTable_c ( ReadTableLock_c& tTableLock )
11711176
: m_tTableLock { tTableLock }
11721177
, m_bCanWrite { tTableLock.TryWrite() }

src/coroutine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ class ReadTableLock_c final
355355
void WaitRead() noexcept;
356356
void FinishWrite() noexcept;
357357
[[nodiscard]] bool UnlockRead() noexcept;
358+
[[nodiscard]] DWORD GetReads() const noexcept;
358359
};
359360

360361
class ScopedWriteTable_c final

src/searchd.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10842,32 +10842,32 @@ void HandleMysqlShowLocks ( RowBuffer_i & tOut )
1084210842
if ( !tOut.HeadEnd () )
1084310843
return;
1084410844

10845+
auto fnLine = [&tOut](const NamedIndexType_t& dPair, int iLocks, const char* szType ) noexcept -> bool
10846+
{
10847+
tOut.PutString ( GetIndexTypeName ( dPair.m_eType ) );
10848+
tOut.PutString ( dPair.m_sName );
10849+
tOut.PutString ( szType );
10850+
tOut.PutStringf ( "Count: %d", iLocks );
10851+
return tOut.Commit ();
10852+
};
1084510853

1084610854
// collect local, rt, percolate
1084710855
auto dIndexes = GetAllServedIndexes ();
1084810856
for ( auto & dPair: dIndexes )
1084910857
{
10850-
switch ( dPair.m_eType )
10851-
{
10852-
case IndexType_e::RT:
10853-
case IndexType_e::PERCOLATE:
10858+
auto pIndex = GetServed ( dPair.m_sName );
10859+
if ( ServedDesc_t::IsMutable ( pIndex ) )
1085410860
{
10855-
auto pIndex = GetServed ( dPair.m_sName );
10856-
assert ( ServedDesc_t::IsMutable ( pIndex ) );
1085710861
RIdx_T<RtIndex_i *> pRt { pIndex };
10858-
int iLocks = pRt->GetNumOfLocks ();
10859-
if ( iLocks>0 )
10860-
{
10861-
tOut.PutString ( GetIndexTypeName ( dPair.m_eType ) );
10862-
tOut.PutString ( dPair.m_sName );
10863-
tOut.PutString ( "freeze" );
10864-
tOut.PutStringf ( "Count: %d", iLocks );
10865-
if ( !tOut.Commit () )
10866-
return;
10867-
}
10862+
const int iLocks = pRt->GetNumOfLocks ();
10863+
if ( iLocks>0 && !fnLine ( dPair, iLocks, "freeze" ) )
10864+
return;
1086810865
}
10869-
default:
10870-
break;
10866+
if ( ServedDesc_t::IsLocal ( pIndex ) )
10867+
{
10868+
const int iRLocks = pIndex->GetReadLocks();
10869+
if ( iRLocks>0 && !fnLine ( dPair, iRLocks, "read" ) )
10870+
return;
1087110871
}
1087210872
}
1087310873

@@ -10886,20 +10886,18 @@ void UnlockTables(ClientSession_c* pSess)
1088610886
pSess->m_dLockedTables.Reset();
1088710887
}
1088810888

10889-
void HandleMysqlLockTables ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
10889+
void HandleMysqlLockTables ( RowBuffer_i & tOut, const SqlStmt_t & tStmt, CSphString& sWarningOut )
1089010890
{
1089110891
StrVec_t dIndexes;
1089210892
bool bHasWrite = false;
1089310893
tStmt.m_dInsertValues.for_each ( [&] (const SqlInsert_t& tVal) {
10894-
bHasWrite |= tVal.m_iType>10;
10895-
dIndexes.Add(tVal.m_sVal);
10894+
if ( tVal.m_iType>10 )
10895+
bHasWrite = true;
10896+
else
10897+
dIndexes.Add(tVal.m_sVal);
1089610898
});
1089710899

10898-
if (bHasWrite)
10899-
{
10900-
tOut.Error ( "Write lock is not implemented." );
10901-
return;
10902-
}
10900+
sWarningOut = bHasWrite ? "Write lock is not implemented." : "";
1090310901

1090410902
if ( session::GetProto()!=Proto_e::MYSQL41 )
1090510903
{
@@ -10932,7 +10930,7 @@ void HandleMysqlLockTables ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
1093210930
sphLogDebug ( "Locked %s", sIndex.cstr() );
1093310931
}
1093410932

10935-
tOut.Ok ( 0 );
10933+
tOut.Ok ( 0, bHasWrite ? 1 : 0 );
1093610934
}
1093710935

1093810936
void HandleMysqlUnlockTables ( RowBuffer_i & tOut )
@@ -11516,7 +11514,7 @@ bool ClientSession_c::Execute ( Str_t sQuery, RowBuffer_i & tOut )
1151611514
return true;
1151711515

1151811516
case STMT_LOCK_TABLES:
11519-
HandleMysqlLockTables ( tOut, *pStmt );
11517+
HandleMysqlLockTables ( tOut, *pStmt, m_tLastMeta.m_sWarning );
1152011518
return true;
1152111519

1152211520
case STMT_UNLOCK_TABLES:

src/searchdaemon.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,11 @@ void ServedIndex_c::LockRead() const noexcept
12851285
return m_tTableLock.UnlockRead();
12861286
}
12871287

1288+
[[nodiscard]] DWORD ServedIndex_c::GetReadLocks() const noexcept
1289+
{
1290+
return m_tTableLock.GetReads();
1291+
}
1292+
12881293
[[nodiscard]] Threads::Coro::ReadTableLock_c& ServedIndex_c::Locker() const noexcept
12891294
{
12901295
return m_tTableLock;

src/searchdaemon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ class ServedIndex_c : public ServedDesc_t
741741

742742
void LockRead() const noexcept;
743743
[[nodiscard]] bool UnlockRead() const noexcept;
744+
[[nodiscard]] DWORD GetReadLocks() const noexcept;
744745
[[nodiscard]] Threads::Coro::ReadTableLock_c& Locker() const noexcept;
745746
};
746747

test/test_316/model.bin

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

test/test_316/test.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ searchd
5959
</sphinxql>
6060
<sphinxql conn="1">lock tables a read</sphinxql>
6161
<sphinxql>
62+
show locks;
6263
insert into a values (9, 1);
6364
insert into b values (9, 2);
6465
update a set iid=9 where id=1;
@@ -75,7 +76,11 @@ searchd
7576
select * from a order by id asc;
7677
select * from b order by id asc;
7778
lock tables a read, b write;
79+
show warnings;
80+
show locks;
7881
lock tables a read, b read;
82+
show warnings;
83+
show locks;
7984
insert into a values (6, 1);
8085
insert into b values (6, 2);
8186
update a set iid=6 where id=1;
@@ -87,6 +92,7 @@ searchd
8792
update b set iid=7 where id=1;
8893
select * from a order by id asc;
8994
select * from b order by id asc;
95+
show locks;
9096
drop table a;
9197
drop table b;
9298
</sphinxql>

test/test_387/model.bin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a:1:{i:0;a:9:{i:0;a:2:{s:8:"sphinxql";s:23:"create table a(iid int)";s:14:"total_affected";i:0;}i:1;a:2:{s:8:"sphinxql";s:16:"create cluster c";s:14:"total_affected";i:0;}i:2;a:2:{s:8:"sphinxql";s:21:"alter cluster c add a";s:14:"total_affected";i:0;}i:3;a:3:{s:8:"sphinxql";s:18:"lock tables a read";s:5:"errno";i:1064;s:5:"error";s:53:"Table a is member of a cluster; not suitable for lock";}i:4;a:3:{s:8:"sphinxql";s:20:"lock tables c:a read";s:5:"errno";i:1064;s:5:"error";s:42:"Table c:a absent, or not suitable for lock";}i:5;a:2:{s:8:"sphinxql";s:22:"alter cluster c drop a";s:14:"total_affected";i:0;}i:6;a:2:{s:8:"sphinxql";s:16:"delete cluster c";s:14:"total_affected";i:0;}i:7;a:2:{s:8:"sphinxql";s:12:"drop table a";s:14:"total_affected";i:0;}i:8;a:3:{s:8:"sphinxql";s:12:"drop table b";s:5:"errno";i:1064;s:5:"error";s:36:"DROP TABLE failed: unknown table 'b'";}}}

test/test_387/test.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<test>
3+
4+
<name>cluster table lock</name>
5+
6+
<requires>
7+
<replication/>
8+
<non-windows/>
9+
<force-rt/>
10+
</requires>
11+
<skip_indexer/>
12+
13+
<config>
14+
searchd
15+
{
16+
<searchd_Settings/>
17+
data_dir = <data_path/>
18+
}
19+
</config>
20+
21+
<queries>
22+
<sphinxql>
23+
create table a(iid int);
24+
create cluster c;
25+
alter cluster c add a;
26+
lock tables a read;
27+
lock tables c:a read;
28+
alter cluster c drop a;
29+
delete cluster c;
30+
drop table a;
31+
drop table b;
32+
</sphinxql>
33+
</queries>
34+
35+
</test>

0 commit comments

Comments
 (0)