Better handling tables in read-only mode during insert

+ throw immediately if table has no metadata in zk
+ stop retries in case of shutdown
+ check if table is readonly at the begining of every retry
This commit is contained in:
Igor Nikonov 2022-11-01 18:47:51 +00:00
parent 9cc64a0fcc
commit e76c3c381f
2 changed files with 20 additions and 2 deletions

View File

@ -30,6 +30,7 @@ namespace ErrorCodes
extern const int DUPLICATE_DATA_PART;
extern const int PART_IS_TEMPORARILY_LOCKED;
extern const int LOGICAL_ERROR;
extern const int TABLE_IS_READ_ONLY;
}
struct ReplicatedMergeTreeSink::DelayedChunk
@ -367,6 +368,16 @@ void ReplicatedMergeTreeSink::commitPart(
retries_ctl.retryLoop([&]()
{
zookeeper->setKeeper(storage.getZooKeeper());
if (storage.is_readonly)
{
/// stop retries if in shutdown
if (storage.shutdown_called)
throw Exception(
ErrorCodes::TABLE_IS_READ_ONLY, "Table is in readonly mode due to shutdown: replica_path={}", storage.replica_path);
retries_ctl.setUserError(ErrorCodes::TABLE_IS_READ_ONLY, "Table is in readonly mode: replica_path={}", storage.replica_path);
return;
}
if (retries_ctl.isRetry())
{

View File

@ -4462,9 +4462,16 @@ void StorageReplicatedMergeTree::assertNotReadonly() const
SinkToStoragePtr StorageReplicatedMergeTree::write(const ASTPtr & /*query*/, const StorageMetadataPtr & metadata_snapshot, ContextPtr local_context)
{
const auto storage_settings_ptr = getSettings();
assertNotReadonly();
/// If table is read-only because it doesn't have metadata in zk yet, then it's not possible to insert into it
/// Without this check, we'll write data parts on disk, and afterwards will remove them since we'll fail to commit them into zk
/// In case of remote storage like s3, it'll generate unnecessary PUT requests
if (is_readonly && (!has_metadata_in_zookeeper.has_value() || false == has_metadata_in_zookeeper.value()))
throw Exception(
ErrorCodes::TABLE_IS_READ_ONLY,
"Table is in readonly mode since table metadata was not found in zookeeper: replica_path={}",
replica_path);
const auto storage_settings_ptr = getSettings();
const Settings & query_settings = local_context->getSettingsRef();
bool deduplicate = storage_settings_ptr->replicated_deduplication_window != 0 && query_settings.insert_deduplicate;