|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.clone; |
| 19 | + |
| 20 | +import org.apache.doris.catalog.Database; |
| 21 | +import org.apache.doris.catalog.Env; |
| 22 | +import org.apache.doris.catalog.MaterializedIndex; |
| 23 | +import org.apache.doris.catalog.OlapTable; |
| 24 | +import org.apache.doris.catalog.Replica; |
| 25 | +import org.apache.doris.catalog.Tablet; |
| 26 | +import org.apache.doris.common.Config; |
| 27 | +import org.apache.doris.common.FeConstants; |
| 28 | +import org.apache.doris.common.util.DebugPointUtil; |
| 29 | +import org.apache.doris.system.Backend; |
| 30 | +import org.apache.doris.utframe.TestWithFeService; |
| 31 | + |
| 32 | +import com.google.common.collect.Maps; |
| 33 | +import org.junit.jupiter.api.Assertions; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +public class BeDownCancelCloneTest extends TestWithFeService { |
| 40 | + |
| 41 | + @Override |
| 42 | + protected int backendNum() { |
| 43 | + return 4; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void beforeCreatingConnectContext() throws Exception { |
| 48 | + FeConstants.runningUnitTest = true; |
| 49 | + FeConstants.default_scheduler_interval_millisecond = 1000; |
| 50 | + Config.enable_debug_points = true; |
| 51 | + Config.tablet_checker_interval_ms = 100; |
| 52 | + Config.tablet_schedule_interval_ms = 100; |
| 53 | + Config.tablet_repair_delay_factor_second = 1; |
| 54 | + Config.allow_replica_on_same_host = true; |
| 55 | + Config.disable_balance = true; |
| 56 | + Config.schedule_batch_size = 1000; |
| 57 | + Config.schedule_slot_num_per_hdd_path = 1000; |
| 58 | + Config.heartbeat_interval_second = 5; |
| 59 | + Config.max_backend_heartbeat_failure_tolerance_count = 1; |
| 60 | + Config.min_clone_task_timeout_sec = 20 * 60 * 1000; |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void test() throws Exception { |
| 65 | + connectContext = createDefaultCtx(); |
| 66 | + |
| 67 | + createDatabase("db1"); |
| 68 | + System.out.println(Env.getCurrentInternalCatalog().getDbNames()); |
| 69 | + |
| 70 | + // 3. create table tbl1 |
| 71 | + createTable("create table db1.tbl1(k1 int) distributed by hash(k1) buckets 1;"); |
| 72 | + RebalancerTestUtil.updateReplicaPathHash(); |
| 73 | + |
| 74 | + Database db = Env.getCurrentInternalCatalog().getDbOrMetaException("db1"); |
| 75 | + OlapTable tbl = (OlapTable) db.getTableOrMetaException("tbl1"); |
| 76 | + Assertions.assertNotNull(tbl); |
| 77 | + Tablet tablet = tbl.getPartitions().iterator().next() |
| 78 | + .getMaterializedIndices(MaterializedIndex.IndexExtState.ALL).iterator().next() |
| 79 | + .getTablets().iterator().next(); |
| 80 | + |
| 81 | + Assertions.assertEquals(3, tablet.getReplicas().size()); |
| 82 | + long destBeId = Env.getCurrentSystemInfo().getAllBackendIds(true).stream() |
| 83 | + .filter(beId -> tablet.getReplicaByBackendId(beId) == null) |
| 84 | + .findFirst() |
| 85 | + .orElse(-1L); |
| 86 | + Assertions.assertTrue(destBeId != -1L); |
| 87 | + Backend destBe = Env.getCurrentSystemInfo().getBackend(destBeId); |
| 88 | + Assertions.assertNotNull(destBe); |
| 89 | + Assertions.assertTrue(destBe.isAlive()); |
| 90 | + |
| 91 | + // add debug point, make clone wait |
| 92 | + DebugPointUtil.addDebugPoint("MockedBackendFactory.handleCloneTablet.block"); |
| 93 | + |
| 94 | + // move replica[0] to destBeId |
| 95 | + Replica srcReplica = tablet.getReplicas().get(0); |
| 96 | + String moveTabletSql = "ADMIN SET REPLICA STATUS PROPERTIES(\"tablet_id\" = \"" + tablet.getId() + "\", " |
| 97 | + + "\"backend_id\" = \"" + srcReplica.getBackendId() + "\", \"status\" = \"drop\")"; |
| 98 | + Assertions.assertNotNull(getSqlStmtExecutor(moveTabletSql)); |
| 99 | + Assertions.assertFalse(srcReplica.isScheduleAvailable()); |
| 100 | + |
| 101 | + Thread.sleep(3000); |
| 102 | + |
| 103 | + Assertions.assertEquals(0, Env.getCurrentEnv().getTabletScheduler().getHistoryTablets(100).size()); |
| 104 | + Assertions.assertEquals(4, tablet.getReplicas().size()); |
| 105 | + Replica destReplica = tablet.getReplicaByBackendId(destBeId); |
| 106 | + Assertions.assertNotNull(destReplica); |
| 107 | + Assertions.assertEquals(Replica.ReplicaState.CLONE, destReplica.getState()); |
| 108 | + |
| 109 | + // clone a replica on destBe |
| 110 | + List<TabletSchedCtx> runningTablets = Env.getCurrentEnv().getTabletScheduler().getRunningTablets(100); |
| 111 | + Assertions.assertEquals(1, runningTablets.size()); |
| 112 | + Assertions.assertEquals(destBeId, runningTablets.get(0).getDestBackendId()); |
| 113 | + |
| 114 | + Map<String, String> params2 = Maps.newHashMap(); |
| 115 | + params2.put("deadBeIds", String.valueOf(destBeId)); |
| 116 | + DebugPointUtil.addDebugPointWithParams("HeartbeatMgr.BackendHeartbeatHandler", params2); |
| 117 | + |
| 118 | + Thread.sleep((Config.heartbeat_interval_second |
| 119 | + * Config.max_backend_heartbeat_failure_tolerance_count + 4) * 1000L); |
| 120 | + |
| 121 | + destBe = Env.getCurrentSystemInfo().getBackend(destBeId); |
| 122 | + Assertions.assertNotNull(destBe); |
| 123 | + Assertions.assertFalse(destBe.isAlive()); |
| 124 | + |
| 125 | + // delete clone dest task |
| 126 | + Assertions.assertFalse(Env.getCurrentEnv().getTabletScheduler().getHistoryTablets(100).isEmpty()); |
| 127 | + |
| 128 | + // first drop dest replica (its backend is dead) and src replica (it's mark as drop) |
| 129 | + // then re clone a replica to src be, and waiting for cloning. |
| 130 | + runningTablets = Env.getCurrentEnv().getTabletScheduler().getRunningTablets(100); |
| 131 | + Assertions.assertEquals(1, runningTablets.size()); |
| 132 | + Assertions.assertEquals(srcReplica.getBackendId(), runningTablets.get(0).getDestBackendId()); |
| 133 | + |
| 134 | + DebugPointUtil.removeDebugPoint("MockedBackendFactory.handleCloneTablet.block"); |
| 135 | + Thread.sleep(2000); |
| 136 | + |
| 137 | + // destBe is dead, cancel clone task |
| 138 | + runningTablets = Env.getCurrentEnv().getTabletScheduler().getRunningTablets(100); |
| 139 | + Assertions.assertEquals(0, runningTablets.size()); |
| 140 | + |
| 141 | + Assertions.assertEquals(3, tablet.getReplicas().size()); |
| 142 | + for (Replica replica : tablet.getReplicas()) { |
| 143 | + Assertions.assertTrue(replica.getBackendId() != destBeId); |
| 144 | + Assertions.assertTrue(replica.isScheduleAvailable()); |
| 145 | + Assertions.assertEquals(Replica.ReplicaState.NORMAL, replica.getState()); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments