In OceanBase Database, the DUPLICATE_SCOPE parameter is used to specify the attributes of a replicated table. After a table is created, you can change the value of the DUPLICATE_SCOPE parameter to convert it between a regular table and a replicated table.
Limitations and considerations
If a load balancing task is triggered due to scaling, changing the primary zone, or modifying the locality of the current tenant, the task of converting a table to a replicated table must wait until the balancing task is completed.
Notice
The conversion process for replicated tables is asynchronous. After you change the
DUPLICATE_SCOPE, you must wait for the background partition balancing task to complete before using the table as the target attribute. For more information about partition balancing, see Intra-tenant balancing.Since replicated tables and table groups are mutually exclusive, a table cannot be converted into a replicated table if it belongs to a table group.
The conversion of a table into a replicated table depends on the load balancing and transfer features of the tenant. If any of the tenant-level parameters
enable_rebalance,enable_transfer, andenable_rereplicationis set tofalse, you cannot convert a table into a replicated table. If you set theenable_rebalanceorenable_transferparameter tofalseand there are ongoing tasks of converting tables into replicated tables, the tasks will be canceled.
Prerequisites
Before you convert a table into a replicated table, make sure that the following parameters are set to True:
enable_rebalanceThe tenant-level
enable_rebalanceparameter controls whether to perform load balancing among tenants. In a user tenant, it controls whether to perform load balancing within the tenant. The default value isTrue. For more information about theenable_rebalanceparameter, see enable_rebalance.enable_transferThe tenant-level
enable_transferparameter controls whether to perform transfer in the tenant. The default value isTrue. For more information about theenable_transferparameter, see enable_transfer.enable_rereplicationThe cluster-level
enable_rereplicationparameter controls whether to enable automatic replica supplementation. The default value isTrue. For more information about theenable_rereplicationparameter, see enable_rereplication.
SQL syntax for changing the replicated table attribute
The SQL syntax for changing the replicated table attribute is as follows:
ALTER TABLE table_name DUPLICATE_SCOPE= 'none | cluster';
In this syntax:
none: specifies to convert the current table into a regular table.cluster: specifies to convert the current table into a replicated table.
Example
The following commands show how to convert a replicated table into a regular table.
Create a replicated table named
tbl1.obclient [test]> CREATE TABLE tbl1 (c1 int,c2 int) DUPLICATE_SCOPE= 'cluster';Convert the replicated table into a regular table.
ALTER TABLE tbl1 DUPLICATE_SCOPE= 'none';Notice
The preceding SQL statement will immediately return a success message after modifying the table attribute. The actual conversion of the replicated table is asynchronously performed in the background by a thread.
View the attributes of the replicated table.
You can query the
oceanbase.DBA_OB_TABLE_LOCATIONSview for the attributes of the replicated table and the log stream ID where the replicated table is located. If theDUPLICATE_SCOPEcolumn displaysCLUSTER, the table is a replicated table. If it displaysNONE, the table is a regular table.SELECT database_name, table_name, table_id, tablet_id, ls_id, role, duplicate_scope FROM oceanbase.DBA_OB_TABLE_LOCATIONS WHERE table_name = 'tbl1';The return result is as follows:
+---------------+------------+----------+-----------+-------+--------+-----------------+ | database_name | table_name | table_id | tablet_id | ls_id | role | duplicate_scope | +---------------+------------+----------+-----------+-------+--------+-----------------+ | test | tbl1 | 500011 | 200002 | 1002 | LEADER | NONE | +---------------+------------+----------+-----------+-------+--------+-----------------+ 1 row in setNote
You can follow the steps below to check whether the partition replicas of the converted table meet the desired attribute.
View the conversion progress.
View the attribute of the log stream.
All replicated tables in a tenant must be placed on a special log stream (broadcast log stream) for replicated tables. You can query the
oceanbase.DBA_OB_LSview for the attribute of the log stream. If theFLAGcolumn containsDUPLICATE, the log stream is a broadcast log stream. Otherwise, it is a normal log stream.SELECT * FROM oceanbase.DBA_OB_LS WHERE LS_ID = 1002;The return result is as follows:
+-------+--------+--------------+---------------+-------------+---------------------+----------+---------------------+---------------------+-----------+ | LS_ID | STATUS | PRIMARY_ZONE | UNIT_GROUP_ID | LS_GROUP_ID | CREATE_SCN | DROP_SCN | SYNC_SCN | READABLE_SCN | FLAG | +-------+--------+--------------+---------------+-------------+---------------------+----------+---------------------+---------------------+-----------+ | 1002 | NORMAL | zone1 | 0 | 0 | 1713164377091593001 | NULL | 1713171198496433000 | 1713171198496433000 | DUPLICATE | +-------+--------+--------------+---------------+-------------+---------------------+----------+---------------------+---------------------+-----------+ 1 row in setDetermine whether a table conversion task needs to be generated.
Table conversion tasks are generated in the background by the load balancer and the transfer module. You can execute the following statement to quickly determine whether a table conversion task needs to be generated. Here,
DUPLICATE_SCOPEis the attribute of the table, andFLAGis the attribute of the log stream to which thetabletbelongs. If theFLAGcolumn containsDUPLICATE, the log stream is a broadcast log stream.SELECT A.duplicate_scope, B.flag FROM oceanbase.DBA_OB_TABLE_LOCATIONS AS A JOIN oceanbase.DBA_OB_LS AS B ON A.ls_id = B.ls_id WHERE A.table_name = 'tbl1' AND A.role = 'LEADER';The return result is as follows:
+-----------------+-----------+ | duplicate_scope | flag | +-----------------+-----------+ | NONE | DUPLICATE | +-----------------+-----------+ 1 row in setThe return information is described as follows:
DUPLICATE_SCOPEisNONE, andFLAGcontainsDUPLICATE: The non-replicated table is placed on a broadcast log stream. A table conversion task needs to be generated to transfer the non-replicated table from the broadcast log stream to a normal log stream.DUPLICATE_SCOPEisNONE, andFLAGdoes not containDUPLICATE: The replicated table is placed on a normal log stream. A table conversion task needs to be generated to transfer the replicated table from a normal log stream to a broadcast log stream.
Generate a table conversion task immediately.
You can temporarily set the
partition_balance_schedule_intervalparameter to a smaller value to generate a table conversion task immediately.ALTER SYSTEM SET partition_balance_schedule_interval = '30s';View the table conversion task.
Table conversion tasks can be queried from the
DBA/CDB_OB_BALANCE_JOB_HISTORYandDBA/CDB_OB_BALANCE_JOBSviews.View the execution status of historical conversion tasks.
If no historical records are returned, the task is either being executed or has not been generated yet.
SELECT * FROM oceanbase.DBA_OB_BALANCE_JOB_HISTORY;The return result is as follows:
+--------+----------------------------+----------------------------+-------------------+-------------------+-----------------+-------------------------+-----------+---------+ | JOB_ID | CREATE_TIME | FINISH_TIME | BALANCE_STRATEGY | JOB_TYPE | TARGET_UNIT_NUM | TARGET_PRIMARY_ZONE_NUM | STATUS | COMMENT | +--------+----------------------------+----------------------------+-------------------+-------------------+-----------------+-------------------------+-----------+---------+ | 777854 | 2024-04-15 16:54:29.220400 | 2024-04-15 16:54:39.231516 | partition balance | PARTITION_BALANCE | 1 | 1 | COMPLETED | NULL | +--------+----------------------------+----------------------------+-------------------+-------------------+-----------------+-------------------------+-----------+---------+ 1 row in setView the execution status of the current balancing task.
SELECT * FROM oceanbase.DBA_OB_BALANCE_JOBS;View the subtasks (including the partition lists) of the conversion task.
Replace
xxxxin the following SQL statement with thejob_idof the task:SELECT * FROM oceanbase.DBA_OB_BALANCE_TASKS WHERE JOB_ID = xxxx;SELECT * FROM oceanbase.DBA_OB_BALANCE_TASK_HISTORY WHERE JOB_ID = xxxx;
Note
- If there is another
BALANCE_JOBrunning in theDBA_OB_BALANCE_JOBSview, the system schedules the partition balancing task only after the current task is completed. - If no corresponding
BALANCE_JOBis found in the precedingBALANCErelated views, the correspondingBALANCE_JOBhas not been generated yet. In this case, you can wait or temporarily set thepartition_balance_schedule_intervalparameter to a smaller value. - If a table conversion task is not generated for a long time, check the
enable_rebalance,enable_transfer,balancer_idle_time, andenable_rereplicationparameters to see whether the transfer feature is disabled.