Browse Source

error optimizing frozen table

That is related to #1818
Aleksey N. Vinogradov 1 year ago
parent
commit
bb4de29a0f
5 changed files with 48 additions and 4 deletions
  1. 8 1
      src/searchd.cpp
  2. 6 2
      src/sphinxrt.cpp
  3. 1 1
      src/sphinxrt.h
  4. 1 0
      test/test_287/model.bin
  5. 32 0
      test/test_287/test.xml

+ 8 - 1
src/searchd.cpp

@@ -15637,7 +15637,14 @@ void HandleMysqlOptimize ( RowBuffer_i & tOut, const SqlStmt_t & tStmt )
 	tTask.m_eVerb = OptimizeTask_t::eManualOptimize;
 	tTask.m_iCutoff = tStmt.m_tQuery.m_iCutoff<=0 ? 0 : tStmt.m_tQuery.m_iCutoff;
 
-	RIdx_T<RtIndex_i *> ( pIndex )->StartOptimize ( std::move ( tTask ) );
+	auto bOptimizeStarted = RIdx_T<RtIndex_i *> ( pIndex )->StartOptimize ( std::move ( tTask ) );
+
+	if ( tStmt.m_tQuery.m_bSync && !bOptimizeStarted )
+	{
+		tOut.Error ( "Can't optimize frozen table" );
+		return;
+	}
+
 	if ( tStmt.m_tQuery.m_bSync && !PollOptimizeRunning ( sIndex ) )
 		tOut.Error ( "RT table went away during waiting" );
 	else

+ 6 - 2
src/sphinxrt.cpp

@@ -1250,7 +1250,7 @@ public:
 	bool				Truncate ( CSphString & sError, Truncate_e eAction ) final;
 	bool				CheckValidateOptimizeParams ( OptimizeTask_t& tTask ) const;
 	bool				CheckValidateChunk ( int& iChunk, int iChunks, bool bByOrder ) const;
-	void				StartOptimize ( OptimizeTask_t tTask ) final;
+	bool				StartOptimize ( OptimizeTask_t tTask ) final;
 	int					OptimizesRunning() const noexcept final;
 	void				Optimize ( OptimizeTask_t tTask ) final;
 	void				CheckStartAutoOptimize ();
@@ -9876,13 +9876,17 @@ bool RtIndex_c::CheckValidateOptimizeParams ( OptimizeTask_t& tTask ) const
 }
 
 
-void RtIndex_c::StartOptimize ( OptimizeTask_t tTask )
+bool RtIndex_c::StartOptimize ( OptimizeTask_t tTask )
 {
+	if ( m_tSaving.GetNumOfLocks ()>0 )
+		return false;
+
 	Threads::StartJob ( [tTask = std::move ( tTask ), this] () {
 		// want to track optimize only at work
 		auto pDesc = PublishSystemInfo ( "OPTIMIZE" );
 		Optimize ( std::move ( tTask ) );
 	} );
+	return true;
 }
 
 

+ 1 - 1
src/sphinxrt.h

@@ -149,7 +149,7 @@ public:
 	virtual bool Truncate ( CSphString & sError, Truncate_e eAction ) = 0;
 
 	virtual void Optimize ( OptimizeTask_t tTask ) {}
-	virtual void StartOptimize ( OptimizeTask_t tTask ) {}
+	virtual bool StartOptimize ( OptimizeTask_t tTask ) { return true; }
 	virtual int OptimizesRunning () const noexcept { return 0; }
 
 	/// check settings vs current and return back tokenizer and dictionary in case of difference

+ 1 - 0
test/test_287/model.bin

@@ -0,0 +1 @@
+a:1:{i:0;a:6:{i:0;a:2:{s:8:"sphinxql";s:25:"drop table if exists test";s:14:"total_affected";i:0;}i:1;a:2:{s:8:"sphinxql";s:17:"create table test";s:14:"total_affected";i:0;}i:2;a:2:{s:8:"sphinxql";s:23:"freeze test like 'fake'";s:10:"total_rows";i:0;}i:3;a:3:{s:8:"sphinxql";s:33:"optimize table test option sync=1";s:5:"errno";i:1064;s:5:"error";s:27:"Can't optimize frozen table";}i:4;a:2:{s:8:"sphinxql";s:13:"unfreeze test";s:14:"total_affected";i:1;}i:5;a:2:{s:8:"sphinxql";s:15:"drop table test";s:14:"total_affected";i:0;}}}

+ 32 - 0
test/test_287/test.xml

@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<test>
+
+<name>avoid optimize frozen</name>
+
+<requires>
+	<force-rt/>
+</requires>
+
+<skip_indexer/>
+
+<config>
+searchd
+{
+	<searchd_settings/>
+	data_dir = <data_path/>
+	binlog_path =
+}
+</config>
+
+<queries>
+<sphinxql>
+	drop table if exists test;
+	create table test;
+	freeze test like 'fake';
+	optimize table test option sync=1;
+	unfreeze test;
+	drop table test;
+</sphinxql>
+</queries>
+
+</test>