Tanya-W commented on code in PR #16371: URL: https://github.com/apache/doris/pull/16371#discussion_r1098516172
########## be/src/olap/schema_change.cpp: ########## @@ -958,6 +1232,270 @@ Status SchemaChangeHandler::_do_process_alter_tablet_v2(const TAlterTabletReqV2& return res; } +Status SchemaChangeHandler::_do_process_alter_inverted_index( + TabletSharedPtr tablet, const TAlterInvertedIndexReq& request) { + Status res = Status::OK(); + // TODO(wy): check whether the tablet's max continuous version == request.version + if (tablet->tablet_state() == TABLET_TOMBSTONED || tablet->tablet_state() == TABLET_STOPPED || + tablet->tablet_state() == TABLET_SHUTDOWN) { + LOG(WARNING) << "tablet's state=" << tablet->tablet_state() + << " cannot alter inverted index"; + return Status::Error<ErrorCode::INTERNAL_ERROR>(); + } + + std::shared_lock base_migration_rlock(tablet->get_migration_lock(), std::try_to_lock); + if (!base_migration_rlock.owns_lock()) { + return Status::Error<TRY_LOCK_FAILED>(); + } + + TabletSchemaSPtr tablet_schema = std::make_shared<TabletSchema>(); + tablet_schema->copy_from(*tablet->tablet_schema()); + if (!request.columns.empty() && request.columns[0].col_unique_id >= 0) { + tablet_schema->clear_columns(); + for (const auto& column : request.columns) { + tablet_schema->append_column(TabletColumn(column)); + } + } + + // get rowset reader + std::vector<RowsetReaderSharedPtr> rs_readers; + DeleteHandler delete_handler; + RETURN_IF_ERROR( + _get_rowset_readers(tablet, tablet_schema, request, &rs_readers, &delete_handler)); + if (request.__isset.is_drop_op && request.is_drop_op) { + // drop index + res = _drop_inverted_index(rs_readers, tablet_schema, tablet, request); + } else { + // add index + res = _add_inverted_index(rs_readers, &delete_handler, tablet_schema, tablet, request); + } + + if (!res.ok()) { + LOG(WARNING) << "failed to alter tablet. tablet=" << tablet->full_name(); + return res; + } + + return Status::OK(); +} + +Status SchemaChangeHandler::_get_rowset_readers(TabletSharedPtr tablet, + const TabletSchemaSPtr& tablet_schema, + const TAlterInvertedIndexReq& request, + std::vector<RowsetReaderSharedPtr>* rs_readers, + DeleteHandler* delete_handler) { + Status res = Status::OK(); + std::vector<Version> versions_to_be_changed; + std::vector<ColumnId> return_columns; + std::vector<TOlapTableIndex> alter_inverted_indexs; + + if (request.__isset.alter_inverted_indexes) { + alter_inverted_indexs = request.alter_inverted_indexes; + } + + for (auto& inverted_index : alter_inverted_indexs) { + DCHECK_EQ(inverted_index.columns.size(), 1); + auto column_name = inverted_index.columns[0]; + auto idx = tablet_schema->field_index(column_name); + return_columns.emplace_back(idx); + } + + // obtain base tablet's push lock and header write lock to prevent loading data + { + std::lock_guard<std::mutex> tablet_lock(tablet->get_push_lock()); + std::lock_guard<std::shared_mutex> tablet_wlock(tablet->get_header_lock()); + + do { Review Comment: The main function used here is `capture_rs_readers`, other logic is to prepare for rs_reader's init param which maybe set differently according to different use scenarios. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org