oceanbase

Форк
0
/
ob_balance_group_ls_stat_operator.cpp 
1884 строки · 74.1 Кб
1
/**
2
 * Copyright (c) 2021 OceanBase
3
 * OceanBase CE is licensed under Mulan PubL v2.
4
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
5
 * You may obtain a copy of Mulan PubL v2 at:
6
 *          http://license.coscl.org.cn/MulanPubL-2.0
7
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10
 * See the Mulan PubL v2 for more details.
11
 */
12

13
#define USING_LOG_PREFIX SHARE
14

15
#include "share/schema/ob_schema_mgr.h"
16
#include "rootserver/ob_balance_group_ls_stat_operator.h"
17
#include "lib/hash/ob_hashset.h"
18
#include "lib/oblog/ob_log_module.h"
19
#include "lib/utility/ob_print_utils.h"
20
#include "common/ob_timeout_ctx.h"
21
#include "observer/ob_server_struct.h" // for GCTX
22
#include "share/schema/ob_table_schema.h"
23
#include "share/schema/ob_schema_struct.h"
24
#include "share/schema/ob_schema_getter_guard.h"
25
#include "share/ob_share_util.h"
26
#include "share/inner_table/ob_inner_table_schema_constants.h"
27
#include "share/ob_srv_rpc_proxy.h" // ObSrvRpcProxy
28
#include "share/tablet/ob_tablet_to_ls_operator.h"
29
#include "share/ls/ob_ls_operator.h" // ObLSAttrOperator
30
#include "share/balance/ob_balance_task_table_operator.h" // ObBalanceTaskTableOperator
31
#include "share/schema/ob_part_mgr_util.h"
32
#include "share/ob_debug_sync.h" // DEBUG_SYNC
33
#include "storage/tablelock/ob_lock_utils.h" // ObLSObjLockUtil
34
#include "share/ls/ob_ls_table.h" // ObLSTable
35
#include "share/ls/ob_ls_table_operator.h" // ObLSTableOperator
36
#include "share/location_cache/ob_location_service.h" // ObLocationService
37
#include "share/ob_rpc_struct.h" // ObCreateDupLSArg & ObCreateDupLSResult
38
#include "rootserver/ob_root_service.h"
39
#include "rootserver/parallel_ddl/ob_tablet_balance_allocator.h"
40

41
namespace oceanbase
42
{
43
using namespace common;
44
using namespace common::sqlclient;
45
using namespace share;
46
using namespace share::schema;
47
using namespace transaction::tablelock;
48

49
namespace rootserver
50
{
51

52

53
int64_t ObNewTableTabletAllocator::alloc_tablet_ls_offset_ = 0;
54

55
int ObBalanceGroupLSStat::build(
56
    const uint64_t tenant_id,
57
    const ObBalanceGroupID &balance_group_id,
58
    const share::ObLSID &ls_id,
59
    const int64_t tablet_group_count,
60
    const ObBalanceGroupName &balance_group_name)
61
{
62
  int ret = OB_SUCCESS;
63
  if (OB_UNLIKELY(OB_INVALID_ID == tenant_id
64
                  || !balance_group_id.is_valid()
65
                  || !ls_id.is_valid()
66
                  || tablet_group_count < 0
67
                  || balance_group_name.is_empty())) {
68
    ret = OB_INVALID_ARGUMENT;
69
    LOG_WARN("invalid argument", KR(ret),
70
             K(tenant_id),
71
             K(balance_group_id),
72
             K(ls_id),
73
             K(tablet_group_count),
74
             K(balance_group_name));
75
  } else {
76
    tenant_id_ = tenant_id;
77
    balance_group_id_= balance_group_id;
78
    ls_id_ = ls_id;
79
    tablet_group_count_ = tablet_group_count;
80
    balance_group_name_ = balance_group_name;
81
  }
82
  return ret;
83
}
84

85
ObBalanceGroupLSStatOperator::ObBalanceGroupLSStatOperator()
86
  : inited_(false),
87
    sql_proxy_(nullptr)
88
{
89
}
90

91
ObBalanceGroupLSStatOperator::~ObBalanceGroupLSStatOperator()
92
{
93
}
94

95
int ObBalanceGroupLSStatOperator::init(
96
    common::ObMySQLProxy *sql_proxy)
97
{
98
  int ret = OB_SUCCESS;
99
  if (OB_UNLIKELY(inited_)) {
100
    ret = OB_INIT_TWICE;
101
    LOG_WARN("inited twice", KR(ret), K(inited_));
102
  } else if (OB_UNLIKELY(nullptr == sql_proxy)) {
103
    ret = OB_INVALID_ARGUMENT;
104
    LOG_WARN("invalid argument", KR(ret), KP(sql_proxy));
105
  } else {
106
    sql_proxy_ = sql_proxy;
107
    inited_ = true;
108
  }
109
  return ret;
110
}
111

112
int ObBalanceGroupLSStatOperator::get_balance_group_ls_stat(
113
    const int64_t timeout,
114
    const uint64_t tenant_id,
115
    const ObBalanceGroupID &balance_group_id,
116
    common::ObIArray<ObBalanceGroupLSStat> &balance_group_ls_stat_array)
117
{
118
  int ret = OB_SUCCESS;
119
  if (OB_UNLIKELY(!inited_)) {
120
    ret = OB_NOT_INIT;
121
    LOG_WARN("not init", KR(ret));
122
  } else if (OB_UNLIKELY(timeout <= 0
123
                         || OB_INVALID_ID == tenant_id
124
                         || !balance_group_id.is_valid())) {
125
    ret = OB_INVALID_ARGUMENT;
126
    LOG_WARN("invalid argument", KR(ret),
127
             K(timeout),
128
             K(tenant_id),
129
             K(balance_group_id));
130
  } else if (OB_UNLIKELY(nullptr == sql_proxy_)) {
131
    ret = OB_ERR_UNEXPECTED;
132
    LOG_WARN("sql_proxy_ ptr is null", KR(ret), KP(sql_proxy_));
133
  } else if (OB_FAIL(get_balance_group_ls_stat(
134
          timeout,
135
          *sql_proxy_,
136
          tenant_id,
137
          balance_group_id,
138
          false, /* for update */
139
          balance_group_ls_stat_array))) {
140
    LOG_WARN("fail to get balance group ls stat", KR(ret),
141
             K(tenant_id),
142
             K(balance_group_id));
143
  }
144
  return ret;
145
}
146

147
int ObBalanceGroupLSStatOperator::get_balance_group_ls_stat(
148
    const int64_t timeout,
149
    common::ObISQLClient &sql_client,
150
    const uint64_t tenant_id,
151
    const ObBalanceGroupID &balance_group_id,
152
    const bool for_update,
153
    common::ObIArray<ObBalanceGroupLSStat> &balance_group_ls_stat_array)
154
{
155
  int ret = OB_SUCCESS;
156
  common::ObTimeoutCtx timeout_ctx;
157
  if (OB_UNLIKELY(!inited_)) {
158
    ret = OB_NOT_INIT;
159
    LOG_WARN("not init", KR(ret));
160
  } else if (OB_UNLIKELY(timeout <= 0
161
                         || OB_INVALID_ID == tenant_id
162
                         || !balance_group_id.is_valid())) {
163
    ret = OB_INVALID_ARGUMENT;
164
    LOG_WARN("invalid argument", KR(ret),
165
             K(timeout),
166
             K(tenant_id),
167
             K(balance_group_id));
168
  } else if (OB_FAIL(ObShareUtil::set_default_timeout_ctx(
169
          timeout_ctx,
170
          timeout))) {
171
    LOG_WARN("fail to set timeout", KR(ret), K(timeout));
172
  } else {
173
    common::ObSqlString sql;
174
    const uint64_t sql_tenant_id = gen_meta_tenant_id(tenant_id);
175
    balance_group_ls_stat_array.reset();
176
    SMART_VAR(ObISQLClient::ReadResult, res) {
177
      sqlclient::ObMySQLResult *result = nullptr;
178
      if (OB_FAIL(sql.append_fmt(
179
              "SELECT * FROM %s WHERE "
180
              "tenant_id = %ld AND "
181
              "balance_group_id_high = %ld AND "
182
              "balance_group_id_low = %ld%s",
183
              OB_ALL_BALANCE_GROUP_LS_STAT_TNAME,
184
              tenant_id,
185
              balance_group_id.id_high_,
186
              balance_group_id.id_low_,
187
              (for_update ? " FOR UPDATE" : "")))) {
188
        LOG_WARN("fail to assign sql", KR(ret));
189
      } else if (OB_FAIL(sql_client.read(res, sql_tenant_id, sql.ptr()))) {
190
        LOG_WARN("execute sql failed", KR(ret), K(sql_tenant_id), K(sql));
191
      } else if (OB_UNLIKELY(nullptr == (result = res.get_result()))) {
192
        ret = OB_ERR_UNEXPECTED;
193
        LOG_WARN("get mysql res failed", KR(ret), K(sql));
194
      } else {
195
        while (OB_SUCC(ret) && OB_SUCC(result->next())) {
196
          ObBalanceGroupLSStat tmp_bg_ls_stat;
197
          uint64_t tenant_id = OB_INVALID_ID;
198
          uint64_t id_high = OB_INVALID_ID;
199
          uint64_t id_low = OB_INVALID_ID;
200
          int64_t ls_id = 0;
201
          int64_t tablet_group_count = -1;
202
          ObString balance_group_name;
203
          EXTRACT_INT_FIELD_MYSQL(*result, "tenant_id", tenant_id, uint64_t);
204
          EXTRACT_INT_FIELD_MYSQL(*result, "balance_group_id_high", id_high, uint64_t);
205
          EXTRACT_INT_FIELD_MYSQL(*result, "balance_group_id_low", id_low, uint64_t);
206
          EXTRACT_INT_FIELD_MYSQL(*result, "ls_id", ls_id, int64_t);
207
          EXTRACT_INT_FIELD_MYSQL(*result, "tablet_group_count", tablet_group_count, int64_t);
208
          EXTRACT_VARCHAR_FIELD_MYSQL(*result, "balance_group_name", balance_group_name);
209
          if (OB_SUCC(ret)) {
210
            if (OB_FAIL(tmp_bg_ls_stat.build(
211
                    tenant_id,
212
                    ObBalanceGroupID(id_high, id_low),
213
                    share::ObLSID(ls_id),
214
                    tablet_group_count,
215
                    balance_group_name))) {
216
              LOG_WARN("fail to build balance group ls stat", KR(ret),
217
                       K(tenant_id),
218
                       K(id_high),
219
                       K(id_low),
220
                       K(ls_id),
221
                       K(tablet_group_count),
222
                       K(balance_group_name));
223
            } else if (OB_FAIL(balance_group_ls_stat_array.push_back(
224
                    tmp_bg_ls_stat))) {
225
              LOG_WARN("fail to push back", KR(ret));
226
            }
227
          }
228
        }
229
        if (OB_ITER_END == ret) {
230
          ret = OB_SUCCESS;
231
        }
232
      }
233
    }
234
  }
235
  return ret;
236
}
237

238
int ObBalanceGroupLSStatOperator::insert_update_balance_group_ls_stat(
239
    const int64_t timeout,
240
    const uint64_t tenant_id,
241
    const ObBalanceGroupID &balance_group_id,
242
    const common::ObIArray<ObBalanceGroupLSStat> &balance_group_ls_stat_array)
243
{
244
  int ret = OB_SUCCESS;
245
  if (OB_UNLIKELY(!inited_)) {
246
    ret = OB_NOT_INIT;
247
    LOG_WARN("not init", KR(ret));
248
  } else if (OB_UNLIKELY(timeout <= 0
249
                         || OB_INVALID_ID == tenant_id
250
                         || !balance_group_id.is_valid()
251
                         || balance_group_ls_stat_array.count() <= 0)) {
252
    ret = OB_INVALID_ARGUMENT;
253
    LOG_WARN("invalid argument", KR(ret),
254
             K(timeout),
255
             K(tenant_id),
256
             K(balance_group_id),
257
             K(balance_group_ls_stat_array));
258
  } else if (OB_UNLIKELY(nullptr == sql_proxy_)) {
259
    ret = OB_ERR_UNEXPECTED;
260
    LOG_WARN("sql_proxy_ ptr is null", KR(ret), KP(sql_proxy_));
261
  } else {
262
    ObMySQLTransaction trans;
263
    if (OB_FAIL(trans.start(
264
            sql_proxy_,
265
            gen_meta_tenant_id(tenant_id)))) {
266
      LOG_WARN("fail to start trans", KR(ret));
267
    } else {
268
      if (OB_FAIL(insert_update_balance_group_ls_stat(
269
              timeout,
270
              trans,
271
              tenant_id,
272
              balance_group_id,
273
              balance_group_ls_stat_array))) {
274
        LOG_WARN("fail to insert update balance group ls stat", KR(ret));
275
      }
276
      // commit/abort
277
      int tmp_ret = OB_SUCCESS;
278
      if (OB_SUCCESS != (tmp_ret = trans.end(OB_SUCC(ret)))) {
279
        LOG_WARN("trans end failed", K(tmp_ret), "is_commit", OB_SUCCESS == ret);
280
        ret = (OB_SUCCESS == ret ? tmp_ret : ret);
281
      }
282
    }
283
  }
284
  return ret;
285
}
286

287
int ObBalanceGroupLSStatOperator::insert_update_balance_group_ls_stat(
288
    const int64_t timeout,
289
    common::ObISQLClient &sql_client,
290
    const uint64_t tenant_id,
291
    const ObBalanceGroupID &balance_group_id,
292
    const common::ObIArray<ObBalanceGroupLSStat> &balance_group_ls_stat_array)
293
{
294
  int ret = OB_SUCCESS;
295
  common::ObTimeoutCtx timeout_ctx;
296
  if (OB_UNLIKELY(!inited_)) {
297
    ret = OB_NOT_INIT;
298
    LOG_WARN("not init", KR(ret));
299
  } else if (OB_UNLIKELY(timeout <= 0
300
                         || OB_INVALID_ID == tenant_id
301
                         || !balance_group_id.is_valid()
302
                         || balance_group_ls_stat_array.count() <= 0)) {
303
    ret = OB_INVALID_ARGUMENT;
304
    LOG_WARN("invalid argument", KR(ret),
305
             K(tenant_id),
306
             K(balance_group_id),
307
             K(balance_group_ls_stat_array));
308
  } else if (OB_FAIL(ObShareUtil::set_default_timeout_ctx(
309
          timeout_ctx,
310
          timeout))) {
311
    LOG_WARN("fail to set timeout", KR(ret), K(timeout));
312
  } else {
313
    const uint64_t sql_tenant_id = gen_meta_tenant_id(tenant_id);
314
    for (int64_t i = 0; OB_SUCC(ret) && i < balance_group_ls_stat_array.count(); ++i) {
315
      int64_t affected_rows = -1;
316
      common::ObSqlString insert_update_sql;
317
      const ObBalanceGroupLSStat &balance_group_ls_stat = balance_group_ls_stat_array.at(i);
318
      if (OB_FAIL(generate_insert_update_sql(
319
              balance_group_ls_stat,
320
              insert_update_sql))) {
321
        LOG_WARN("fail to generate insert update sql", KR(ret),
322
                 K(balance_group_ls_stat),
323
                 K(insert_update_sql));
324
      } else if (OB_FAIL(sql_client.write(
325
              sql_tenant_id,
326
              insert_update_sql.ptr(),
327
              affected_rows))) {
328
        LOG_WARN("fail to insert update", KR(ret),
329
                 K(sql_tenant_id),
330
                 K(insert_update_sql));
331
      } else if (affected_rows > 2) {
332
        ret = OB_ERR_UNEXPECTED;
333
        LOG_WARN("unexpected affected rows", KR(ret),
334
                 K(sql_tenant_id),
335
                 K(insert_update_sql),
336
                 K(affected_rows));
337
      }
338
    }
339
  }
340
  return ret;
341
}
342

343
int ObBalanceGroupLSStatOperator::inc_balance_group_ls_stat(
344
    const int64_t timeout,
345
    common::ObISQLClient &sql_client,
346
    const uint64_t tenant_id,
347
    const ObBalanceGroupLSStat &ls_stat)
348
{
349
  int ret = OB_SUCCESS;
350
  common::ObTimeoutCtx timeout_ctx;
351
  if (OB_UNLIKELY(!inited_)) {
352
    ret = OB_NOT_INIT;
353
    LOG_WARN("not init", KR(ret));
354
  } else if (OB_UNLIKELY(
355
             timeout <= 0
356
             || OB_INVALID_TENANT_ID == tenant_id
357
             || !ls_stat.get_balance_group_id().is_valid())) {
358
    ret = OB_INVALID_ARGUMENT;
359
    LOG_WARN("invalid argument", KR(ret), K(tenant_id), K(ls_stat));
360
  } else if (OB_FAIL(ObShareUtil::set_default_timeout_ctx(
361
             timeout_ctx, timeout))) {
362
    LOG_WARN("fail to set timeout", KR(ret), K(timeout));
363
  } else {
364
    const uint64_t sql_tenant_id = gen_meta_tenant_id(tenant_id);
365
    common::ObSqlString inc_sql;
366
    int64_t affected_rows = 0;
367
    if (OB_FAIL(generate_inc_sql_(ls_stat, inc_sql))) {
368
      LOG_WARN("fail to generate inc sql", KR(ret),
369
               K(tenant_id), K(ls_stat), K(inc_sql));
370
    } else if (OB_FAIL(sql_client.write(
371
               sql_tenant_id, inc_sql.ptr(), affected_rows))) {
372
      LOG_WARN("fail to insert update", KR(ret),
373
               K(tenant_id), K(inc_sql));
374
    } else if (OB_UNLIKELY(affected_rows > 2)) {
375
      ret = OB_ERR_UNEXPECTED;
376
      LOG_WARN("unexpected affected rows", KR(ret),
377
               K(tenant_id), K(inc_sql), K(affected_rows));
378
    }
379
  }
380
  return ret;
381
}
382

383
int ObBalanceGroupLSStatOperator::delete_balance_group_ls_stat(
384
    const int64_t timeout,
385
    common::ObISQLClient &sql_client,
386
    const uint64_t tenant_id)
387
{
388
  int ret = OB_SUCCESS;
389
  ObSqlString sql;
390
  int64_t affected_rows = 0;
391
  if (OB_UNLIKELY(!inited_)) {
392
    ret = OB_NOT_INIT;
393
    LOG_WARN("not init", KR(ret));
394
  } else if (OB_FAIL(sql.assign_fmt("delete from %s where tenant_id= %ld", OB_ALL_BALANCE_GROUP_LS_STAT_TNAME, tenant_id))) {
395
    LOG_WARN("fail to format sql", KR(ret));
396
  } else if (OB_FAIL(sql_client.write(gen_meta_tenant_id(tenant_id), sql.ptr(), affected_rows))) {
397
    LOG_WARN("fail to delete inner table", KR(ret), K(sql));
398
  }
399
  return ret;
400
}
401

402
int ObBalanceGroupLSStatOperator::generate_inc_sql_(
403
    const ObBalanceGroupLSStat &bg_ls_stat,
404
    common::ObSqlString &sql_string)
405
{
406
  int ret = OB_SUCCESS;
407
  if (OB_UNLIKELY(!inited_)) {
408
    ret = OB_NOT_INIT;
409
    LOG_WARN("not init", KR(ret));
410
  } else if (OB_UNLIKELY(!bg_ls_stat.is_valid())) {
411
    ret = OB_INVALID_ARGUMENT;
412
    LOG_WARN("invalid argument", KR(ret), K(bg_ls_stat));
413
  } else if (OB_FAIL(sql_string.append_fmt(
414
             "INSERT INTO %s ("
415
             "tenant_id, "
416
             "balance_group_id_high, "
417
             "balance_group_id_low, "
418
             "ls_id, "
419
             "tablet_group_count, "
420
             "balance_group_name)"
421
             " VALUES ("
422
             "%ld, %ld, %ld, %ld, %ld, '%s') "
423
             "ON DUPLICATE KEY UPDATE "
424
             "tablet_group_count = tablet_group_count + %ld, "
425
             "balance_group_name = '%s'",
426
             OB_ALL_BALANCE_GROUP_LS_STAT_TNAME,
427
             bg_ls_stat.get_tenant_id(),
428
             bg_ls_stat.get_balance_group_id().id_high_,
429
             bg_ls_stat.get_balance_group_id().id_low_,
430
             bg_ls_stat.get_ls_id().id(),
431
             bg_ls_stat.get_tablet_group_count(),
432
             to_cstring(ObHexEscapeSqlStr(bg_ls_stat.get_balance_group_name().str())),
433
             bg_ls_stat.get_tablet_group_count(),
434
             to_cstring(ObHexEscapeSqlStr(bg_ls_stat.get_balance_group_name().str()))))) {
435
    LOG_WARN("fail to append fmt", KR(ret), K(bg_ls_stat));
436
  } else {
437
    LOG_INFO("balance group ls inc sql", K(sql_string));
438
  }
439
  return ret;
440
}
441

442
int ObBalanceGroupLSStatOperator::generate_insert_update_sql(
443
    const ObBalanceGroupLSStat &bg_ls_stat,
444
    common::ObSqlString &sql_string)
445
{
446
  int ret = OB_SUCCESS;
447
  if (OB_UNLIKELY(!inited_)) {
448
    ret = OB_NOT_INIT;
449
    LOG_WARN("not init", KR(ret));
450
  } else if (OB_UNLIKELY(!bg_ls_stat.is_valid())) {
451
    ret = OB_INVALID_ARGUMENT;
452
    LOG_WARN("invalid argument", KR(ret), K(bg_ls_stat));
453
  } else {
454
    if (OB_FAIL(sql_string.append_fmt(
455
            "INSERT INTO %s ("
456
            "tenant_id, "
457
            "balance_group_id_high, "
458
            "balance_group_id_low, "
459
            "ls_id, "
460
            "tablet_group_count, "
461
            "balance_group_name)"
462
            " VALUES ("
463
            "%ld, %ld, %ld, %ld, %ld, '%s') "
464
            "ON DUPLICATE KEY UPDATE "
465
            "tablet_group_count = %ld, "
466
            "balance_group_name = '%s'",
467
            OB_ALL_BALANCE_GROUP_LS_STAT_TNAME,
468
            bg_ls_stat.get_tenant_id(),
469
            bg_ls_stat.get_balance_group_id().id_high_,
470
            bg_ls_stat.get_balance_group_id().id_low_,
471
            bg_ls_stat.get_ls_id().id(),
472
            bg_ls_stat.get_tablet_group_count(),
473
            to_cstring(ObHexEscapeSqlStr(bg_ls_stat.get_balance_group_name().str())),
474
            bg_ls_stat.get_tablet_group_count(),
475
            to_cstring(ObHexEscapeSqlStr(bg_ls_stat.get_balance_group_name().str()))))) {
476
      LOG_WARN("fail to append fmt", KR(ret), K(bg_ls_stat));
477
    } else {
478
      LOG_INFO("balance group ls update sql", K(sql_string));
479
    }
480
  }
481
  return ret;
482
}
483

484
ObNewTableTabletAllocator::ObNewTableTabletAllocator(
485
    const uint64_t tenant_id,
486
    share::schema::ObSchemaGetterGuard &schema_guard,
487
    common::ObMySQLProxy *sql_proxy,
488
    const bool use_parallel_ddl /*= false*/)
489
  : tenant_id_(tenant_id),
490
    schema_guard_(schema_guard),
491
    sql_proxy_(sql_proxy),
492
    bg_ls_stat_operator_(),
493
    status_(MyStatus::INVALID),
494
    ls_id_array_(),
495
    inited_(false),
496
    is_add_partition_(false),
497
    use_parallel_ddl_(use_parallel_ddl)
498
{
499
}
500

501
ObNewTableTabletAllocator::~ObNewTableTabletAllocator()
502
{
503
}
504

505
int ObNewTableTabletAllocator::init()
506
{
507
  int ret = OB_SUCCESS;
508
  const uint64_t meta_tenant_id = gen_meta_tenant_id(tenant_id_);
509
  if (OB_UNLIKELY(inited_)) {
510
    ret = OB_INIT_TWICE;
511
    LOG_WARN("init twice", KR(ret), K(inited_));
512
  } else if (OB_UNLIKELY(nullptr == sql_proxy_)) {
513
    ret = OB_ERR_UNEXPECTED;
514
    LOG_WARN("sql proxy ptr is null", KR(ret), KP(sql_proxy_));
515
  } else if (OB_FAIL(bg_ls_stat_operator_.init(sql_proxy_))) {
516
    LOG_WARN("fail to init bg_ls_stat_operator_", KR(ret));
517
  } else {
518
    status_ = MyStatus::WAIT_TO_PREPARE;
519
    is_add_partition_ = false;
520
    inited_ = true;
521
  }
522
  return ret;
523
}
524

525
int ObNewTableTabletAllocator::prepare(
526
    ObMySQLTransaction &trans,
527
    const share::schema::ObTableSchema &table_schema,
528
    bool is_add_partition)
529
{
530
  int ret = OB_SUCCESS;
531
  is_add_partition_ = is_add_partition;
532
  if (OB_UNLIKELY(!inited_)) {
533
    ret = OB_NOT_INIT;
534
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
535
  } else if (OB_UNLIKELY(!table_schema.has_tablet())) {
536
    ret = OB_INVALID_ARGUMENT;
537
    LOG_WARN("tablet has not tablet", KR(ret), K(table_schema));
538
  } else if (MyStatus::WAIT_TO_PREPARE != status_) {
539
    ret = OB_STATE_NOT_MATCH;
540
    LOG_WARN("NewTableTabletAllocator state not match", KR(ret), K(status_));
541
  } else if ((is_meta_tenant(table_schema.get_tenant_id()))
542
             || (is_sys_tenant(table_schema.get_tenant_id()))) {
543
    if (OB_FAIL(alloc_ls_for_meta_or_sys_tenant_tablet(table_schema))) {
544
      LOG_WARN("fail to alloc ls for meta or sys tenant tablet", KR(ret));
545
    }
546
  } else if (table_schema.is_duplicate_table()) {
547
    if (OB_FAIL(alloc_ls_for_duplicate_table_(table_schema))) {
548
      LOG_WARN("fail to alloc ls for duplicate tablet", KR(ret), K(table_schema));
549
    }
550
  } else {
551
    if (table_schema.is_index_table()) {
552
      if (table_schema.is_index_local_storage()) {
553
        // local index or global index with local storage
554
        if (OB_FAIL(alloc_ls_for_local_index_tablet(table_schema))) {
555
          LOG_WARN("fail to alloc ls for local index tablet", KR(ret));
556
        }
557
      } else {
558
        // global index
559
        if (OB_FAIL(alloc_ls_for_global_index_tablet(table_schema))) {
560
          LOG_WARN("fail to alloc ls for global index tablet", KR(ret));
561
        }
562
      }
563
    } else {
564
      if (OB_INVALID_ID != table_schema.get_tablegroup_id()) {
565
        if (OB_FAIL(alloc_ls_for_in_tablegroup_tablet(table_schema))) {
566
          LOG_WARN("fail to alloc ls for in tablegroup tablet", KR(ret));
567
        }
568
      } else {
569
        if (OB_FAIL(alloc_ls_for_normal_table_tablet(table_schema))) {
570
          LOG_WARN("fail to alloc ls for normal table tablet", KR(ret));
571
        }
572
      }
573
    }
574

575
    DEBUG_SYNC(BEFORE_LOCK_LS_WHEN_CREATE_TABLE);
576
    // If ls status is not normal or is blocking tablet in, choose new ls for tablet creating.
577
    if (OB_FAIL(ret)) {
578
    } else if (is_related_table(table_schema.get_table_type(), table_schema.get_index_type())) {
579
      // skip lock ls
580
    } else if (OB_FAIL(check_and_replace_ls_(trans, table_schema.get_tenant_id()))) {
581
      LOG_WARN("lock user ls failed", KR(ret),
582
               "tenant_id", table_schema.get_tenant_id(), K_(ls_id_array));
583
    }
584
  }
585

586
  if (OB_SUCC(ret)) {
587
    status_ = MyStatus::WAIT_TO_OUTPUT;
588
  }
589
  is_add_partition_ = false;
590
  return ret;
591
}
592

593
int ObNewTableTabletAllocator::prepare_like(
594
    const share::schema::ObTableSchema &table_schema)
595
{
596
  int ret = OB_SUCCESS;
597
  if (OB_UNLIKELY(!inited_)) {
598
    ret = OB_NOT_INIT;
599
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
600
  } else if (OB_UNLIKELY(!table_schema.has_tablet())) {
601
    ret = OB_INVALID_ARGUMENT;
602
    LOG_WARN("tablet has not tablet", KR(ret), K(table_schema));
603
  } else if (MyStatus::WAIT_TO_PREPARE != status_) {
604
    ret = OB_STATE_NOT_MATCH;
605
    LOG_WARN("NewTableTabletAllocator state not match", KR(ret), K(status_));
606
  } else if ((is_meta_tenant(table_schema.get_tenant_id()))
607
             || (is_sys_tenant(table_schema.get_tenant_id()))) {
608
    if (OB_FAIL(alloc_ls_for_meta_or_sys_tenant_tablet(table_schema))) {
609
      LOG_WARN("fail to alloc ls for meta or sys tenant tablet", KR(ret));
610
    }
611
  } else if (OB_FAIL(alloc_tablet_by_primary_schema(table_schema))) {
612
    LOG_WARN("fail to alloc tablet by primary schema", KR(ret), K(table_schema));
613
  }
614
  if (OB_SUCC(ret)) {
615
    status_ = MyStatus::WAIT_TO_OUTPUT;
616
  }
617
  return ret;
618
}
619

620
int ObNewTableTabletAllocator::get_ls_id_array(
621
    common::ObIArray<share::ObLSID> &ls_id_array)
622
{
623
  int ret = OB_SUCCESS;
624
  if (OB_UNLIKELY(!inited_)) {
625
    ret = OB_NOT_INIT;
626
    LOG_WARN("not init", KR(ret));
627
  } else if (MyStatus::WAIT_TO_OUTPUT != status_) {
628
    ret = OB_STATE_NOT_MATCH;
629
    LOG_WARN("NewTableTabletAllocator state not match", KR(ret), K(status_));
630
  } else {
631
    ls_id_array.reset();
632
    if (OB_FAIL(ls_id_array.assign(ls_id_array_))) {
633
      LOG_WARN("fail to assign ls id array", KR(ret));
634
    } else {
635
      ls_id_array_.reset();
636
      status_ = MyStatus::WAIT_TO_PREPARE;
637
    }
638
  }
639
  return ret;
640
}
641

642
int ObNewTableTabletAllocator::finish(
643
    const bool commit)
644
{
645
  UNUSED(commit);
646
  return OB_SUCCESS;
647
}
648

649
int ObNewTableTabletAllocator::get_tablet_id_array(
650
    const share::schema::ObTableSchema &table_schema,
651
    common::ObIArray<common::ObTabletID> &tablet_id_array)
652
{
653
  int ret = OB_SUCCESS;
654
  tablet_id_array.reset();
655
  if (OB_UNLIKELY(!inited_)) {
656
    ret = OB_NOT_INIT;
657
    LOG_WARN("not init", KR(ret));
658
  } else {
659
    schema::ObPartitionSchemaIter iter(table_schema, schema::CHECK_PARTITION_MODE_NORMAL);
660
    schema::ObPartitionSchemaIter::Info info;
661
    while (OB_SUCC(ret)) {
662
      if (OB_FAIL(iter.next_partition_info(info))) {
663
        if (OB_ITER_END == ret) {
664
          ret = OB_SUCCESS;
665
          break;
666
        }
667
      } else if (OB_FAIL(tablet_id_array.push_back(info.tablet_id_))) {
668
        LOG_WARN("fail to push tablet_id to array", KR(ret), K(info.tablet_id_));
669
      }
670
    }
671
  }
672
  return ret;
673
}
674

675
int ObNewTableTabletAllocator::alloc_tablet_by_primary_schema(
676
    const share::schema::ObTableSchema &table_schema)
677
{
678
  int ret = OB_SUCCESS;
679
  LOG_INFO("alloc tablet by primary schema",
680
           "tenant_id", table_schema.get_tenant_id(),
681
           "table_id", table_schema.get_table_id());
682
  if (OB_UNLIKELY(!inited_)) {
683
    ret = OB_NOT_INIT;
684
    LOG_WARN("not init", KR(ret));
685
  } else if (OB_UNLIKELY(nullptr == sql_proxy_)) {
686
    ret = OB_ERR_UNEXPECTED;
687
    LOG_WARN("sql_proxy_ ptr is null", KR(ret));
688
  } else {
689
    common::ObArray<common::ObTabletID> tablet_id_array;
690
    if (OB_FAIL(get_tablet_id_array(table_schema, tablet_id_array))) {
691
      LOG_WARN("fail to get tablet id array", KR(ret));
692
    } else if (OB_FAIL(ObTabletToLSTableOperator::batch_get_ls(
693
            *sql_proxy_,
694
            tenant_id_,
695
            tablet_id_array,
696
            ls_id_array_))) {
697
      LOG_WARN("fail to batch get ls", KR(ret));
698
    }
699
  }
700
  return ret;
701
}
702

703
int ObNewTableTabletAllocator::get_available_ls(
704
    common::ObIArray<share::ObLSID> &ls_id_array)
705
{
706
  int ret = OB_SUCCESS;
707
  if (OB_UNLIKELY(!inited_)) {
708
    ret = OB_NOT_INIT;
709
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
710
  } else if (OB_UNLIKELY(nullptr == sql_proxy_)) {
711
    ret = OB_ERR_UNEXPECTED;
712
    LOG_WARN("sql_proxy ptr is null", KR(ret));
713
  } else {
714
    share::ObLSAttrOperator ls_attr_operator(tenant_id_, sql_proxy_);
715
    ObLSAttrArray ls_attr_array;
716
    if (OB_FAIL(ls_attr_operator.get_all_ls_by_order(ls_attr_array))) {
717
      LOG_WARN("fail to load all ls", KR(ret), K_(tenant_id));
718
    } else {
719
      ARRAY_FOREACH(ls_attr_array, idx) {
720
        share::ObLSAttr &ls_attr = ls_attr_array.at(idx);
721
        if (ls_attr.ls_is_normal()
722
            && SYS_LS != ls_attr.get_ls_id()
723
            && !ls_attr.get_ls_flag().is_block_tablet_in()
724
            && !ls_attr.get_ls_flag().is_duplicate_ls()) {
725
          if (OB_FAIL(ls_id_array.push_back(ls_attr.get_ls_id()))) {
726
            LOG_WARN("fail to push back", KR(ret), K(ls_attr), K(ls_id_array));
727
          }
728
        }
729
      }
730
    }
731
  }
732
  return ret;
733
}
734

735
int ObNewTableTabletAllocator::alloc_tablet_for_create_balance_group(
736
    const ObBalanceGroupName &bg_name,
737
    const ObBalanceGroupID &bg_id,
738
    const common::ObIArray<share::ObLSID> &ls_id_array,
739
    const int64_t part_num)
740
{
741
  int ret = OB_SUCCESS;
742
  if (OB_UNLIKELY(!inited_)) {
743
    ret = OB_NOT_INIT;
744
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
745
  } else if (OB_UNLIKELY(bg_name.is_empty()
746
                         || !bg_id.is_valid()
747
                         || ls_id_array.count() <= 0
748
                         || part_num <= 0)) {
749
    ret = OB_INVALID_ARGUMENT;
750
    LOG_WARN("invalid argument", KR(ret),
751
             K(bg_name),
752
             K(bg_id),
753
             K(ls_id_array),
754
             K(part_num));
755
  } else {
756
    const int64_t bucket_num = ls_id_array.count();
757
    const int64_t min_itl = part_num / bucket_num;
758
    const int64_t max_itl = ((min_itl * bucket_num == part_num) ? (min_itl) : (min_itl + 1));
759
    const int64_t min_cnt = max_itl * bucket_num - part_num;
760
    const int64_t max_cnt = bucket_num - min_cnt;
761
    common::ObArray<ObBalanceGroupLSStat> bg_ls_stat_array;
762
    int64_t start_idx = fetch_ls_offset();
763
    for (int64_t i = 0; OB_SUCC(ret) && i < ls_id_array.count(); ++i) {
764
      const share::ObLSID &ls_id = ls_id_array.at((start_idx + i) % ls_id_array.count());
765
      const int64_t tablet_cnt = ((i < min_cnt) ? min_itl : max_itl);
766
      for (int64_t j = 0; OB_SUCC(ret) && j < tablet_cnt; ++j) {
767
        if (OB_FAIL(ls_id_array_.push_back(ls_id))) {
768
          LOG_WARN("fail to push back", KR(ret));
769
        }
770
      }
771
      if (OB_SUCC(ret)) {
772
        ObBalanceGroupLSStat bg_ls_stat;
773
        if (OB_FAIL(bg_ls_stat.build(
774
                tenant_id_,
775
                bg_id,
776
                ls_id,
777
                tablet_cnt,
778
                bg_name))) {
779
          LOG_WARN("fail to build bg ls stat", KR(ret));
780
        } else if (OB_FAIL(bg_ls_stat_array.push_back(
781
                bg_ls_stat))) {
782
          LOG_WARN("fail to push back", KR(ret));
783
        }
784
      }
785
    }
786
    if (OB_SUCC(ret)) {
787
      if (OB_FAIL(bg_ls_stat_operator_.insert_update_balance_group_ls_stat(
788
              THIS_WORKER.get_timeout_remain(),
789
              *sql_proxy_,
790
              tenant_id_,
791
              bg_id,
792
              bg_ls_stat_array))) {
793
        LOG_WARN("fail to insert update balance group ls stat", KR(ret));
794
      }
795
    }
796
  }
797
  return ret;
798
}
799

800
int ObNewTableTabletAllocator::alloc_tablet_for_add_balance_group(
801
    const common::ObIArray<ObBalanceGroupLSStat> &bg_ls_stat_array,
802
    const ObBalanceGroupName &bg_name,
803
    const ObBalanceGroupID &bg_id,
804
    const common::ObIArray<share::ObLSID> &ls_id_array,
805
    const int64_t partition_num)
806
{
807
  int ret = OB_SUCCESS;
808
  // suppose bg_ls_stat_array can be empty
809
  common::hash::ObHashSet<share::ObLSID> ls_id_set;
810
  if (OB_UNLIKELY(!inited_)) {
811
    ret = OB_NOT_INIT;
812
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
813
  } else if (OB_UNLIKELY(bg_name.is_empty()
814
                         || !bg_id.is_valid()
815
                         || ls_id_array.count() <= 0
816
                         || partition_num <= 0)) {
817
    ret = OB_INVALID_ARGUMENT;
818
    LOG_WARN("invalid argument", KR(ret),
819
             K(bg_name),
820
             K(bg_id),
821
             K(ls_id_array),
822
             K(partition_num));
823
  } else if (OB_FAIL(ls_id_set.create(MAX_TENANT_LS_CNT))) {
824
    LOG_WARN("fail to create ls id set", KR(ret));
825
  } else {
826
    common::ObArray<ObBalanceGroupLSStat> final_ls_stat_array;
827
    int64_t total_alloc_num = partition_num;
828
    int64_t valid_bg_cnt = total_alloc_num;
829
    for (int64_t i = 0; OB_SUCC(ret) && i < ls_id_array.count(); ++i) {
830
      const share::ObLSID &ls_id = ls_id_array.at(i);
831
      if (OB_FAIL(ls_id_set.set_refactored(ls_id, 0/*not overwrite*/))) {
832
        LOG_WARN("fail to set refactored", KR(ret));
833
      }
834
    }
835
    for (int64_t i = 0; OB_SUCC(ret) && i < bg_ls_stat_array.count(); ++i) {
836
      const share::ObLSID &ls_id = bg_ls_stat_array.at(i).get_ls_id();
837
      int tmp_ret = ls_id_set.exist_refactored(ls_id);
838
      LOG_INFO("balance group ls stat", "bg_ls_stat", bg_ls_stat_array.at(i));
839
      if (OB_HASH_NOT_EXIST == tmp_ret) {
840
        // ls not available
841
      } else if (OB_HASH_EXIST == tmp_ret) {
842
        if (OB_FAIL(final_ls_stat_array.push_back(bg_ls_stat_array.at(i)))) {
843
          LOG_WARN("fail to push back", KR(ret));
844
        } else if (OB_FAIL(ls_id_set.erase_refactored(ls_id))) {
845
          LOG_WARN("fail to erase refactored", KR(ret));
846
        } else {
847
          valid_bg_cnt += bg_ls_stat_array.at(i).get_tablet_group_count();
848
        }
849
      } else {
850
        ret = tmp_ret;
851
        LOG_WARN("fail to check exist", KR(ret), K(ls_id));
852
      }
853
    }
854
    for (common::hash::ObHashSet<share::ObLSID>::iterator iter = ls_id_set.begin();
855
         OB_SUCC(ret) && iter != ls_id_set.end();
856
         ++iter) {
857
      ObBalanceGroupLSStat bg_ls_stat;
858
      if (OB_FAIL(bg_ls_stat.build(
859
              tenant_id_,
860
              bg_id,
861
              iter->first, /*ls_id*/
862
              0,/*bg cnt*/
863
              bg_name))) {
864
        LOG_WARN("fail to build bg ls stat", KR(ret),
865
                 K(tenant_id_),
866
                 K(bg_id),
867
                 K(bg_name),
868
                 "ls_id", iter->first);
869
      } else if (OB_FAIL(final_ls_stat_array.push_back(bg_ls_stat))) {
870
        LOG_WARN("fail to push back", KR(ret));
871
      }
872
    }
873

874
    if (OB_FAIL(ret)) {
875
      // bypass
876
    } else if (OB_UNLIKELY(final_ls_stat_array.count() <= 0)) {
877
      ret = OB_ERR_UNEXPECTED;
878
      LOG_WARN("final ls stat array count unexpected", KR(ret), K(final_ls_stat_array));
879
    } else {
880
      std::sort(final_ls_stat_array.begin(), final_ls_stat_array.end());
881
      for (int64_t alloc_seq = 0; OB_SUCC(ret) && alloc_seq < total_alloc_num; alloc_seq++) {
882
        int64_t min_ls_tg_idx = 0;
883
        int64_t min_ls_tg_cnt = final_ls_stat_array.at(0).get_tablet_group_count();
884
        // find min
885
        for (int64_t i = 1; OB_SUCC(ret) && i < final_ls_stat_array.count(); ++i) {
886
          ObBalanceGroupLSStat &bg_ls_stat = final_ls_stat_array.at(i);
887
          if (bg_ls_stat.get_tablet_group_count() < min_ls_tg_cnt) {
888
            min_ls_tg_idx = i;
889
            min_ls_tg_cnt = bg_ls_stat.get_tablet_group_count();
890
          }
891
        }
892
        if (OB_SUCC(ret)) {
893
          final_ls_stat_array.at(min_ls_tg_idx).add_tablet_group_count(1);
894
          if (OB_FAIL(ls_id_array_.push_back(final_ls_stat_array.at(min_ls_tg_idx).get_ls_id()))) {
895
            LOG_WARN("fail to push back", KR(ret));
896
          }
897
        }
898
      }
899
      if (OB_SUCC(ret)) {
900
        if (OB_FAIL(bg_ls_stat_operator_.insert_update_balance_group_ls_stat(
901
                THIS_WORKER.get_timeout_remain(),
902
                *sql_proxy_,
903
                tenant_id_,
904
                bg_id,
905
                final_ls_stat_array))) {
906
          LOG_WARN("fail to insert update balance group ls stat", KR(ret));
907
        }
908
      }
909
    }
910
  }
911
  return ret;
912
}
913

914
int ObNewTableTabletAllocator::alloc_tablet_for_one_level_partitioned_balance_group(
915
    const share::schema::ObTableSchema &table_schema)
916
{
917
  int ret = OB_SUCCESS;
918
  LOG_INFO("alloc tablet for one level partitioned balance group",
919
           "tenant_id", table_schema.get_tenant_id(),
920
           "table_id", table_schema.get_table_id());
921
  common::ObArray<share::ObLSID> ls_id_array;
922
  ObBalanceGroup bg;
923
  if (OB_UNLIKELY(!inited_)) {
924
    ret = OB_NOT_INIT;
925
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
926
  } else if (OB_UNLIKELY(PARTITION_LEVEL_ONE != table_schema.get_part_level())) {
927
    ret = OB_INVALID_ARGUMENT;
928
    LOG_WARN("invalid argument", KR(ret), "part_level", table_schema.get_part_level());
929
  } else if (OB_FAIL(bg.init_by_table(table_schema, NULL/*partition*/))) {
930
    LOG_WARN("fail to get one level partitioned bg info", KR(ret));
931
  } else if (OB_FAIL(get_available_ls(ls_id_array))) {
932
    LOG_WARN("fail to get available ls", KR(ret));
933
  } else {
934
    if (!is_add_partition_) {
935
      if (OB_FAIL(alloc_tablet_for_create_balance_group(
936
          bg.name(),
937
          bg.id(),
938
          ls_id_array,
939
          table_schema.get_partition_num()))) {
940
        LOG_WARN("fail to alloc tablet for create balance group", KR(ret));
941
      }
942
    } else {
943
      common::ObArray<ObBalanceGroupLSStat> bg_ls_stat_array;
944
      if (OB_FAIL(bg_ls_stat_operator_.get_balance_group_ls_stat(
945
              THIS_WORKER.get_timeout_remain(),
946
              *sql_proxy_,
947
              tenant_id_,
948
              bg.id(),
949
              false, /*for update*/
950
              bg_ls_stat_array))) {
951
        LOG_WARN("fail to get balance group ls stat", KR(ret),
952
                 K(tenant_id_), K(bg));
953
      } else if (OB_FAIL(alloc_tablet_for_add_balance_group(
954
          bg_ls_stat_array,
955
          bg.name(),
956
          bg.id(),
957
          ls_id_array,
958
          table_schema.get_partition_num()))) {
959
        LOG_WARN("fail to alloc tablet for add balance group", KR(ret), K(bg));
960
      }
961
    }
962
  }
963
  return ret;
964
}
965

966
int ObNewTableTabletAllocator::alloc_tablet_for_two_level_partitioned_balance_group(
967
    const share::schema::ObTableSchema &table_schema,
968
    const int64_t part_idx)
969
{
970
  int ret = OB_SUCCESS;
971
  LOG_INFO("alloc tablet for two level partitioned balance group",
972
           "tenant_id", table_schema.get_tenant_id(),
973
           "table_id", table_schema.get_table_id());
974
  common::ObArray<share::ObLSID> ls_id_array;
975
  ObBalanceGroup bg;
976
  if (OB_UNLIKELY(!inited_)) {
977
    ret = OB_NOT_INIT;
978
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
979
  } else if (OB_UNLIKELY(PARTITION_LEVEL_TWO != table_schema.get_part_level())) {
980
    ret = OB_INVALID_ARGUMENT;
981
    LOG_WARN("invalid argument", KR(ret), "part_level", table_schema.get_part_level());
982
  } else if (OB_UNLIKELY(part_idx >= table_schema.get_partition_num())) {
983
    ret = OB_INVALID_ARGUMENT;
984
    LOG_WARN("invalid part idx", KR(ret), K(part_idx),
985
             "part_num", table_schema.get_partition_num());
986
  } else {
987
    const schema::ObPartition *partition = NULL;
988
    if (OB_FAIL(table_schema.get_partition_by_partition_index(part_idx, schema::CHECK_PARTITION_MODE_NORMAL, partition))) {
989
      LOG_WARN("get_partition_by_partition_index fail", KR(ret), K(part_idx), K(table_schema));
990
    } else if (OB_ISNULL(partition)) {
991
      ret = OB_ERR_UNEXPECTED;
992
      LOG_WARN("part ptr is null", KR(ret), K(part_idx), K(table_schema));
993
    } else if (OB_FAIL(bg.init_by_table(table_schema, partition))) {
994
      LOG_WARN("fail to init two level partitioned bg info", KR(ret), K(table_schema), K(partition));
995
    } else if (OB_FAIL(get_available_ls(ls_id_array))) {
996
      LOG_WARN("fail to get available ls", KR(ret));
997
    } else {
998
      if (!is_add_partition_) {
999
        if (OB_FAIL(alloc_tablet_for_create_balance_group(
1000
            bg.name(),
1001
            bg.id(),
1002
            ls_id_array,
1003
            partition->get_subpartition_num()))) {
1004
          LOG_WARN("fail to alloc tablet for create balance group", KR(ret));
1005
        }
1006
      } else {
1007
        common::ObArray<ObBalanceGroupLSStat> bg_ls_stat_array;
1008
        if (OB_FAIL(bg_ls_stat_operator_.get_balance_group_ls_stat(
1009
                THIS_WORKER.get_timeout_remain(),
1010
                *sql_proxy_,
1011
                tenant_id_,
1012
                bg.id(),
1013
                false, /*for update*/
1014
                bg_ls_stat_array))) {
1015
          LOG_WARN("fail to get balance group ls stat", KR(ret),
1016
                   K(tenant_id_), K(bg));
1017
        } else if (OB_FAIL(alloc_tablet_for_add_balance_group(
1018
            bg_ls_stat_array,
1019
            bg.name(),
1020
            bg.id(),
1021
            ls_id_array,
1022
            partition->get_subpartition_num()))) {
1023
          LOG_WARN("fail to alloc tablet for add balance group", KR(ret), K(bg));
1024
        }
1025
      }
1026
    }
1027
  }
1028
  return ret;
1029
}
1030

1031
int ObNewTableTabletAllocator::alloc_tablet_for_non_partitioned_balance_group(
1032
    const share::schema::ObTableSchema &table_schema)
1033
{
1034
  int ret = OB_SUCCESS;
1035
  LOG_INFO("alloc tablet for non partitioned balance group",
1036
           "tenant_id", table_schema.get_tenant_id(),
1037
           "table_id", table_schema.get_table_id());
1038
  ObBalanceGroup bg;
1039
  common::ObArray<ObBalanceGroupLSStat> bg_ls_stat_array;
1040
  common::ObArray<share::ObLSID> ls_id_array;
1041
  if (OB_UNLIKELY(!inited_)) {
1042
    ret = OB_NOT_INIT;
1043
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1044
  } else if (OB_UNLIKELY(PARTITION_LEVEL_ZERO != table_schema.get_part_level())) {
1045
    ret = OB_INVALID_ARGUMENT;
1046
    LOG_WARN("invalid argument", KR(ret),
1047
             "part_num", table_schema.get_all_part_num(),
1048
             "part_level", table_schema.get_part_level(),
1049
             K(table_schema));
1050
  } else if (OB_FAIL(bg.init_by_table(table_schema, NULL/*partition*/))) {
1051
    LOG_WARN("fail to init non partitioned bg info", KR(ret), K(bg), K(table_schema));
1052
  } else if (OB_FAIL(get_available_ls(ls_id_array))) {
1053
    LOG_WARN("fail to get available ls", KR(ret));
1054
  } else if (OB_FAIL(bg_ls_stat_operator_.get_balance_group_ls_stat(
1055
          THIS_WORKER.get_timeout_remain(),
1056
          *sql_proxy_,
1057
          tenant_id_,
1058
          bg.id(),
1059
          false, /*for update*/
1060
          bg_ls_stat_array))) {
1061
    LOG_WARN("fail to get balance group ls stat", KR(ret), K(tenant_id_), K(bg));
1062
  } else if (OB_FAIL(alloc_tablet_for_add_balance_group(
1063
          bg_ls_stat_array,
1064
          bg.name(),
1065
          bg.id(),
1066
          ls_id_array,
1067
          table_schema.get_all_part_num()))) {
1068
    LOG_WARN("fail to alloc tablet for add balance group", KR(ret), K(bg), K(bg_ls_stat_array),
1069
        K(ls_id_array), K(table_schema.get_all_part_num()));
1070
  }
1071
  return ret;
1072
}
1073

1074
int ObNewTableTabletAllocator::alloc_tablet_for_non_partitioned_balance_group_by_cache_(
1075
    const share::schema::ObTableSchema &table_schema)
1076
{
1077
  int ret = OB_SUCCESS;
1078
  LOG_INFO("alloc tablet for non partitioned balance group by cache",
1079
           "tenant_id", table_schema.get_tenant_id(),
1080
           "table_id", table_schema.get_table_id());
1081
  common::ObArray<share::ObLSID> ls_id_array;
1082
  share::ObLSID ls_id;
1083
  if (OB_UNLIKELY(!inited_)) {
1084
    ret = OB_NOT_INIT;
1085
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1086
  } else if (OB_UNLIKELY(PARTITION_LEVEL_ZERO != table_schema.get_part_level())) {
1087
    ret = OB_INVALID_ARGUMENT;
1088
    LOG_WARN("invalid argument", KR(ret),
1089
             "part_num", table_schema.get_all_part_num(),
1090
             "part_level", table_schema.get_part_level(),
1091
             K(table_schema));
1092
  } else if (OB_FAIL(get_available_ls(ls_id_array))) {
1093
    LOG_WARN("fail to get available ls", KR(ret));
1094
  } else if (OB_ISNULL(GCTX.root_service_)) {
1095
    ret = OB_ERR_UNEXPECTED;
1096
    LOG_WARN("rootservice is null", KR(ret));
1097
  } else if (OB_FAIL(GCTX.root_service_->get_ddl_service()
1098
                     .get_non_partitioned_tablet_allocator()
1099
                     .alloc_tablet(tenant_id_, ls_id_array, ls_id))) {
1100
    LOG_WARN("fail to alloc tablet by cache", KR(ret), K_(tenant_id));
1101
  } else if (OB_FAIL(ls_id_array_.push_back(ls_id))) {
1102
    LOG_WARN("fail to push back ls id", KR(ret), K_(tenant_id), K(ls_id));
1103
  }
1104
  return ret;
1105
}
1106

1107
int ObNewTableTabletAllocator::alloc_tablet_for_partitioned_balance_group(
1108
    const share::schema::ObTableSchema &table_schema)
1109
{
1110
  int ret = OB_SUCCESS;
1111
  if (OB_UNLIKELY(!inited_)) {
1112
    ret = OB_NOT_INIT;
1113
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1114
  } else {
1115
    if (PARTITION_LEVEL_ONE == table_schema.get_part_level()) {
1116
      if (OB_FAIL(alloc_tablet_for_one_level_partitioned_balance_group(
1117
              table_schema))) {
1118
        LOG_WARN("fail to alloc tablet for one level partitioned bg", KR(ret));
1119
      }
1120
    } else if (PARTITION_LEVEL_TWO == table_schema.get_part_level()) {
1121
      for (int64_t i = 0; OB_SUCC(ret) && i < table_schema.get_partition_num(); ++i) {
1122
        if (OB_FAIL(alloc_tablet_for_two_level_partitioned_balance_group(
1123
                table_schema, i))) {
1124
          LOG_WARN("fail to alloc tablet for two level partitioned bg", KR(ret));
1125
        }
1126
      }
1127
    } else {
1128
      ret = OB_ERR_UNEXPECTED;
1129
      LOG_WARN("part level unexpected", KR(ret),
1130
               "part_level", table_schema.get_part_level());
1131
    }
1132
  }
1133
  return ret;
1134
}
1135

1136
int ObNewTableTabletAllocator::alloc_tablet_by_count_balance(
1137
    const share::schema::ObTableSchema &table_schema)
1138
{
1139
  int ret = OB_SUCCESS;
1140
  if (OB_UNLIKELY(!inited_)) {
1141
    ret = OB_NOT_INIT;
1142
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1143
  } else if (table_schema.get_all_part_num() <= 0) {
1144
    ret = OB_ERR_UNEXPECTED;
1145
    LOG_WARN("table schema part num unexpected", KR(ret),
1146
             "part_num", table_schema.get_all_part_num(),
1147
             "table_schema", table_schema);
1148
  } else if (is_sys_table(table_schema.get_table_id())
1149
             || is_sys_tenant(table_schema.get_tenant_id())) {
1150
    for (int64_t i = 0; i < table_schema.get_all_part_num() && OB_SUCC(ret); i++) {
1151
      if (OB_FAIL(ls_id_array_.push_back(ObLSID(SYS_LS)))) {
1152
        LOG_WARN("failed to push_back", KR(ret), K(i));
1153
      }
1154
    }
1155
  } else if (PARTITION_LEVEL_ZERO == table_schema.get_part_level()) {
1156
    if (!use_parallel_ddl_) {
1157
      if (OB_FAIL(alloc_tablet_for_non_partitioned_balance_group(table_schema))) {
1158
        LOG_WARN("fail to alloc tablet by non partitioned balance group", KR(ret));
1159
      }
1160
    } else {
1161
      if (OB_FAIL(alloc_tablet_for_non_partitioned_balance_group_by_cache_(table_schema))) {
1162
        LOG_WARN("fail to alloc tablet by non partitioned balance group by cache", KR(ret));
1163
      }
1164
    }
1165
  } else {
1166
    if (OB_FAIL(alloc_tablet_for_partitioned_balance_group(table_schema))) {
1167
      LOG_WARN("fail to alloc tablet by partitioned balance group", KR(ret));
1168
    }
1169
  }
1170
  return ret;
1171
}
1172

1173
int ObNewTableTabletAllocator::alloc_ls_for_meta_or_sys_tenant_tablet(
1174
    const share::schema::ObTableSchema &table_schema)
1175
{
1176
  int ret = OB_SUCCESS;
1177
  LOG_INFO("alloc ls for meta or sys tenant tablet",
1178
           "tenant_id", table_schema.get_tenant_id(),
1179
           "table_id", table_schema.get_table_id());
1180
  if (OB_UNLIKELY(!inited_)) {
1181
    ret = OB_NOT_INIT;
1182
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1183
  } else if ((!is_meta_tenant(table_schema.get_tenant_id())
1184
             && (!is_sys_tenant(table_schema.get_tenant_id())))) {
1185
    ret = OB_ERR_UNEXPECTED;
1186
    LOG_WARN("unexpected table schema", KR(ret),
1187
             "tenant_id", table_schema.get_tenant_id(), K(table_schema));
1188
  } else {
1189
    for (int64_t i = 0; i < table_schema.get_all_part_num() && OB_SUCC(ret); i++) {
1190
      if (OB_FAIL(ls_id_array_.push_back(ObLSID(SYS_LS)))) {
1191
        LOG_WARN("failed to push_back", KR(ret), K(i));
1192
      }
1193
    }
1194
  }
1195
  return ret;
1196
}
1197

1198
int ObNewTableTabletAllocator::alloc_ls_for_local_index_tablet(
1199
    const share::schema::ObTableSchema &index_schema)
1200
{
1201
  int ret = OB_SUCCESS;
1202
  LOG_INFO("alloc ls for local index tablet",
1203
           "tenant_id", index_schema.get_tenant_id(),
1204
           "index_id", index_schema.get_table_id());
1205
  if (OB_UNLIKELY(!inited_)) {
1206
    ret = OB_NOT_INIT;
1207
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1208
  } else {
1209
    const uint64_t tenant_id = index_schema.get_tenant_id();
1210
    const uint64_t data_table_id = index_schema.get_data_table_id();
1211
    const share::schema::ObTableSchema *table_schema = nullptr;
1212
    if (OB_FAIL(schema_guard_.get_table_schema(
1213
        tenant_id, data_table_id, table_schema))) {
1214
      LOG_WARN("fail to get table schema", KR(ret), K(tenant_id), K(data_table_id));
1215
    } else if (OB_UNLIKELY(nullptr == table_schema)) {
1216
      ret = OB_TABLE_NOT_EXIST;
1217
      LOG_WARN("table not exist", KR(ret), K(data_table_id));
1218
    } else if (OB_FAIL(alloc_tablet_by_primary_schema(
1219
            *table_schema))) {
1220
      LOG_WARN("fail to alloc tablet by guard", KR(ret), K(data_table_id));
1221
    }
1222
  }
1223
  return ret;
1224
}
1225

1226
int ObNewTableTabletAllocator::alloc_ls_for_global_index_tablet(
1227
    const share::schema::ObTableSchema &index_schema)
1228
{
1229
  int ret = OB_SUCCESS;
1230
  LOG_INFO("alloc ls for global index tablet",
1231
           "tenant_id", index_schema.get_tenant_id(),
1232
           "index_id", index_schema.get_table_id());
1233
  if (OB_UNLIKELY(!inited_)) {
1234
    ret = OB_NOT_INIT;
1235
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1236
  } else if (OB_FAIL(alloc_tablet_by_count_balance(
1237
          index_schema))) {
1238
    LOG_WARN("fail to alloc tablet by count balance", KR(ret));
1239
  }
1240
  return ret;
1241
}
1242

1243
int ObNewTableTabletAllocator::alloc_ls_for_in_tablegroup_tablet(
1244
    const share::schema::ObTableSchema &table_schema)
1245
{
1246
  int ret = OB_SUCCESS;
1247
  LOG_INFO("alloc ls for in tablegroup tablet",
1248
           "tenant_id", table_schema.get_tenant_id(),
1249
           "table_id", table_schema.get_table_id());
1250
  if (OB_UNLIKELY(!inited_)) {
1251
    ret = OB_NOT_INIT;
1252
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1253
  } else if (OB_UNLIKELY(OB_INVALID_ID == table_schema.get_tablegroup_id())) {
1254
    ret = OB_ERR_UNEXPECTED;
1255
    LOG_WARN("shall not be here for a table without tablegroup", KR(ret), K(table_schema));
1256
  } else if (is_sys_table(table_schema.get_table_id())
1257
             || is_sys_tenant(table_schema.get_tenant_id())) {
1258
    for (int64_t i = 0; i < table_schema.get_all_part_num() && OB_SUCC(ret); i++) {
1259
      if (OB_FAIL(ls_id_array_.push_back(ObLSID(SYS_LS)))) {
1260
        LOG_WARN("failed to push_back", KR(ret), K(i));
1261
      }
1262
    }
1263
  } else {
1264
    common::ObArray<const share::schema::ObTableSchema *> table_schema_array;
1265
    const share::schema::ObSimpleTablegroupSchema *tablegroup_schema = NULL;
1266
    if (OB_FAIL(schema_guard_.get_table_schemas_in_tablegroup(
1267
            tenant_id_,
1268
            table_schema.get_tablegroup_id(),
1269
            table_schema_array))) {
1270
      LOG_WARN("fail to get table schemas in tablegroup", KR(ret),
1271
               "tenant_id", tenant_id_,
1272
               "tablegroup_id", table_schema.get_tablegroup_id());
1273
    } else if (OB_FAIL(schema_guard_.get_tablegroup_schema(tenant_id_, table_schema.get_tablegroup_id(), tablegroup_schema))) {
1274
      LOG_WARN("fail to get tablegroup_schema", KR(ret), K(table_schema.get_tablegroup_id()));
1275
    } else if (OB_ISNULL(tablegroup_schema) || !tablegroup_schema->is_valid()) {
1276
      ret = OB_ERR_UNEXPECTED;
1277
      LOG_WARN("tablegroup_schema invalid", KR(ret), K(tablegroup_schema));
1278
    } else if (table_schema_array.count() > 0) {
1279
      if (OB_UNLIKELY(nullptr == table_schema_array.at(0))) {
1280
        ret = OB_ERR_UNEXPECTED;
1281
        LOG_WARN("table schema ptr is null", KR(ret), K(table_schema_array));
1282
      } else if (!is_add_partition_ || tablegroup_schema->get_sharding() == OB_PARTITION_SHARDING_NONE) {
1283
        if (OB_FAIL(alloc_tablet_for_tablegroup(*table_schema_array.at(0), table_schema, *tablegroup_schema))) {
1284
          LOG_WARN("fail to alloc tablet for tablegroup", KR(ret), K(is_add_partition_), K(tablegroup_schema), K(*table_schema_array.at(0)), K(table_schema));
1285
        }
1286
      } else if (tablegroup_schema->get_sharding() == OB_PARTITION_SHARDING_ADAPTIVE) {
1287
        // add partition for tablegroup table may break the constraint of sharding ADAPTIVE
1288
        // so alloc tablet as new table
1289
        if (OB_FAIL(alloc_tablet_for_tablegroup(table_schema, *tablegroup_schema))) {
1290
          LOG_WARN("fail to alloc tablet for tablegroup", KR(ret), K(table_schema), K(tablegroup_schema));
1291
        }
1292
      } else if (tablegroup_schema->get_sharding() == OB_PARTITION_SHARDING_PARTITION) {
1293
        /* add partition for tablegroup sharding=PARTITION, we process only add subpart binding to existing one level partition
1294
         * otherwise alloc tablet as new table
1295
         */
1296
        const ObTableSchema *origin_table_schema = NULL;
1297
        if (OB_FAIL(schema_guard_.get_table_schema(table_schema.get_tenant_id(), table_schema.get_table_id(), origin_table_schema))) {
1298
          LOG_WARN("fail to get origin table_schema", KR(ret), K(table_schema.get_table_id()));
1299
        } else if (OB_ISNULL(origin_table_schema)) {
1300
          ret = OB_ERR_UNDEFINED;
1301
          LOG_WARN("origin_table_schema is null", KR(ret), K(table_schema.get_table_id()));
1302
        } else if (OB_FAIL(alloc_tablet_for_add_part_in_tablegroup_sharding_partition(table_schema, *origin_table_schema))) {
1303
          LOG_WARN("fail to alloc_tablet_for_tablegroup_add_part", KR(ret), K(table_schema), K(origin_table_schema));
1304
        }
1305
      }
1306
    } else {
1307
      if (OB_FAIL(alloc_tablet_for_tablegroup(table_schema, *tablegroup_schema))) {
1308
        LOG_WARN("fail to alloc tablet for tablegroup", KR(ret), K(table_schema));
1309
      }
1310
    }
1311
  }
1312
  return ret;
1313
}
1314

1315
int ObNewTableTabletAllocator::alloc_tablet_for_add_part_in_tablegroup_sharding_partition(
1316
    const schema::ObTableSchema &table_schema,
1317
    const schema::ObTableSchema &origin_table_schema)
1318
{
1319
  int ret = OB_SUCCESS;
1320
  common::ObArray<share::ObLSID> origin_ls_id_array;
1321
  common::ObArray<share::ObLSID> pre_ls_id_array;
1322
  common::ObArray<share::ObLSID> avail_ls_id_array;
1323
  if (table_schema.get_table_id() != origin_table_schema.get_table_id()) {
1324
    ret = OB_ERR_UNEXPECTED;
1325
    LOG_WARN("table schema not match", KR(ret), K(table_schema), K(origin_table_schema));
1326
  } else if (OB_FAIL(generate_ls_array_by_primary_schema(origin_table_schema, origin_ls_id_array))) {
1327
    LOG_WARN("fail to generate_ls_array_by_primary_schema", KR(ret), K(origin_table_schema));
1328
  } else if (OB_FAIL(extract_one_level_ls_array_by_primary_schema(origin_table_schema, origin_ls_id_array, pre_ls_id_array))) {
1329
    LOG_WARN("fail to extract_one_level_ls_array_by_primary_schema", KR(ret), K(origin_table_schema));
1330
  } else if (OB_FAIL(get_available_ls(avail_ls_id_array))) {
1331
    LOG_WARN("fail get_available_ls", KR(ret));
1332
  } else if (avail_ls_id_array.empty()) {
1333
    ret = OB_ERR_UNEXPECTED;
1334
    LOG_WARN("no available ls", KR(ret));
1335
  } else {
1336
    for (int i = 0; OB_SUCC(ret) && i < table_schema.get_partition_num(); i++) {
1337
      const schema::ObPartition *partition = NULL;
1338
      if (OB_FAIL(table_schema.get_partition_by_partition_index(i, schema::CHECK_PARTITION_MODE_NORMAL, partition))) {
1339
        LOG_WARN("get_partition_by_partition_index fail", KR(ret), K(i), K(table_schema));
1340
      } else if (OB_ISNULL(partition)) {
1341
        ret = OB_ERR_UNEXPECTED;
1342
        LOG_WARN("part ptr is null", KR(ret), K(i), K(table_schema));
1343
      } else {
1344
        int64_t origin_part_index = OB_INVALID_INDEX;
1345
        ObLSID dest_ls_id;
1346
        int64_t need_ls_count = 1;
1347
        if (schema::PARTITION_LEVEL_TWO == table_schema.get_part_level()) {
1348
          need_ls_count = partition->get_sub_part_num();
1349
        }
1350
        if (OB_FAIL(origin_table_schema.get_partition_index_by_id(partition->get_part_id(), schema::CHECK_PARTITION_MODE_NORMAL, origin_part_index))) {
1351
          if (OB_ENTRY_NOT_EXIST == ret) {
1352
            ret = OB_SUCCESS;
1353
            int64_t dest_idx = (fetch_ls_offset() % avail_ls_id_array.count());
1354
            // table_group can't use count balance because no partition value assign ls rule
1355
            dest_ls_id = avail_ls_id_array.at(dest_idx);
1356
          }
1357
        } else {
1358
          dest_ls_id = pre_ls_id_array.at(origin_part_index);
1359
        }
1360
        for (int c = 0; OB_SUCC(ret) && c < need_ls_count; c++) {
1361
          if (OB_FAIL(ls_id_array_.push_back(dest_ls_id))) {
1362
            LOG_WARN("fail to push ls_id to array", KR(ret), K(pre_ls_id_array), K(origin_part_index));
1363
          }
1364
        }
1365
      }
1366
    }
1367
  }
1368
  return ret;
1369
}
1370

1371
int ObNewTableTabletAllocator::alloc_tablet_for_tablegroup(
1372
    const schema::ObTableSchema &table_schema,
1373
    const schema::ObSimpleTablegroupSchema &tablegroup_schema)
1374
{
1375
  int ret = OB_SUCCESS;
1376
  common::ObArray<share::ObLSID> ls_id_array;
1377
  if (OB_FAIL(get_available_ls(ls_id_array))) {
1378
    LOG_WARN("fail to get available ls", KR(ret), K(tenant_id_));
1379
  } else if (ls_id_array.empty()) {
1380
    ret = OB_STATE_NOT_MATCH;
1381
    LOG_WARN("empty ls to alloc", KR(ret), K(tenant_id_));
1382
  } else if (tablegroup_schema.get_sharding() == OB_PARTITION_SHARDING_NONE || schema::PARTITION_LEVEL_ZERO == table_schema.get_part_level()) {
1383
    int64_t start_idx = fetch_ls_offset();
1384
    ObLSID dest_ls_id = ls_id_array.at(start_idx % ls_id_array.count());
1385
    for (int64_t i = 0; i < table_schema.get_all_part_num() && OB_SUCC(ret); i++) {
1386
      if (OB_FAIL(ls_id_array_.push_back(dest_ls_id))) {
1387
        LOG_WARN("failed to push_back", KR(ret), K(i));
1388
      }
1389
    }
1390
  } else if (tablegroup_schema.get_sharding() == OB_PARTITION_SHARDING_PARTITION || schema::PARTITION_LEVEL_ONE == table_schema.get_part_level()) {
1391
    int64_t start_idx = fetch_ls_offset();
1392
    for (int64_t i = 0; i < table_schema.get_partition_num() && OB_SUCC(ret); i++) {
1393
      ObLSID dest_ls_id = ls_id_array.at((start_idx + i) % ls_id_array.count());
1394
      if (schema::PARTITION_LEVEL_ONE == table_schema.get_part_level()) {
1395
        if (OB_FAIL(ls_id_array_.push_back(dest_ls_id))) {
1396
          LOG_WARN("failed to push_back", KR(ret), K(i));
1397
        }
1398
      } else if (schema::PARTITION_LEVEL_TWO == table_schema.get_part_level()) {
1399
        const schema::ObPartition *partition = NULL;
1400
        if (OB_FAIL(table_schema.get_partition_by_partition_index(i, schema::CHECK_PARTITION_MODE_NORMAL, partition))) {
1401
          LOG_WARN("get_partition_by_partition_index fail", KR(ret), K(i), K(table_schema));
1402
        } else if (OB_ISNULL(partition)) {
1403
          ret = OB_ERR_UNEXPECTED;
1404
          LOG_WARN("part ptr is null", KR(ret), K(i), K(table_schema));
1405
        } else {
1406
          for (int64_t sp = 0; OB_SUCC(ret) && sp < partition->get_subpartition_num(); sp++) {
1407
            if (OB_FAIL(ls_id_array_.push_back(dest_ls_id))) {
1408
              LOG_WARN("failed to push ls_id to array", KR(ret), K(dest_ls_id));
1409
            }
1410
          }
1411
        }
1412
      } else {
1413
        ret = OB_ERR_UNEXPECTED;
1414
        LOG_WARN("unexpected part_level", KR(ret), K(table_schema.get_part_level()));
1415
      }
1416
    }
1417
  } else if (tablegroup_schema.get_sharding() == OB_PARTITION_SHARDING_ADAPTIVE) {
1418
    for (int64_t i = 0; i < table_schema.get_partition_num() && OB_SUCC(ret); i++) {
1419
      int64_t start_idx = fetch_ls_offset();
1420
      if (schema::PARTITION_LEVEL_TWO == table_schema.get_part_level()) {
1421
        const schema::ObPartition *partition = NULL;
1422
        if (OB_FAIL(table_schema.get_partition_by_partition_index(i, schema::CHECK_PARTITION_MODE_NORMAL, partition))) {
1423
          LOG_WARN("get_partition_by_partition_index fail", KR(ret), K(i), K(table_schema));
1424
        } else if (OB_ISNULL(partition)) {
1425
          ret = OB_ERR_UNEXPECTED;
1426
          LOG_WARN("part ptr is null", KR(ret), K(i), K(table_schema));
1427
        } else {
1428
          for (int64_t sp = 0; OB_SUCC(ret) && sp < partition->get_subpartition_num(); sp++) {
1429
            ObLSID dest_ls_id = ls_id_array.at((start_idx + sp) % ls_id_array.count());
1430
            if (OB_FAIL(ls_id_array_.push_back(dest_ls_id))) {
1431
              LOG_WARN("failed to push ls_id to array", KR(ret), K(dest_ls_id));
1432
            }
1433
          }
1434
        }
1435
      } else {
1436
        ret = OB_ERR_UNEXPECTED;
1437
        LOG_WARN("unexpected part_level", KR(ret), K(table_schema.get_part_level()));
1438
      }
1439
    }
1440
  } else {
1441
    ret = OB_ERR_UNEXPECTED;
1442
    LOG_WARN("unknow sharding option", KR(ret), K(tablegroup_schema.get_sharding()));
1443
  }
1444
  return ret;
1445
}
1446

1447
int ObNewTableTabletAllocator::generate_ls_array_by_primary_schema(
1448
    const schema::ObTableSchema &primary_schema,
1449
    common::ObArray<share::ObLSID> &ls_id_array)
1450
{
1451
  int ret = OB_SUCCESS;
1452

1453
  ls_id_array.reuse();
1454
  common::ObArray<common::ObTabletID> tablet_id_array;
1455
  if (OB_FAIL(get_tablet_id_array(primary_schema, tablet_id_array))) {
1456
    LOG_WARN("fail to get tablet id array", KR(ret), K(primary_schema));
1457
  } else if (OB_ISNULL(sql_proxy_)) {
1458
    ret = OB_ERR_UNEXPECTED;
1459
    LOG_WARN("sql_proxy is null", KR(ret));
1460
  } else if (OB_FAIL(ObTabletToLSTableOperator::batch_get_ls(
1461
          *sql_proxy_,
1462
          tenant_id_,
1463
          tablet_id_array,
1464
          ls_id_array))) {
1465
    LOG_WARN("fail to batch get ls", KR(ret), K(tenant_id_), K(tablet_id_array), K(primary_schema));
1466
  } else if (ls_id_array.count() != primary_schema.get_all_part_num()) {
1467
    ret = OB_ERR_UNEXPECTED;
1468
    LOG_WARN("empty pre_ls_id_array", KR(ret), K(tenant_id_), K(tablet_id_array), K(primary_schema));
1469
  }
1470

1471
  return ret;
1472
}
1473

1474
int ObNewTableTabletAllocator::extract_one_level_ls_array_by_primary_schema(
1475
    const schema::ObTableSchema &primary_schema,
1476
    common::ObArray<share::ObLSID> &all_ls_id_array,
1477
    common::ObArray<share::ObLSID> &pre_ls_id_array)
1478
{
1479
  int ret = OB_SUCCESS;
1480
  pre_ls_id_array.reuse();
1481
  if (primary_schema.get_all_part_num() != all_ls_id_array.count()) {
1482
    ret = OB_ERR_UNEXPECTED;
1483
    LOG_WARN("part count not match", KR(ret), K(primary_schema.get_all_part_num()), K(all_ls_id_array.count()));
1484
  } else {
1485
    int64_t primary_partition_offset = 0;
1486
    for (int64_t part_idx = 0; OB_SUCC(ret) && part_idx < primary_schema.get_partition_num(); part_idx++) {
1487
      if (OB_FAIL(pre_ls_id_array.push_back(all_ls_id_array.at(primary_partition_offset)))) {
1488
        LOG_WARN("push ls_id to array", KR(ret), K(primary_partition_offset), K(all_ls_id_array));
1489
      } else if (schema::PARTITION_LEVEL_ONE == primary_schema.get_part_level()) {
1490
        primary_partition_offset++;
1491
      } else if (schema::PARTITION_LEVEL_TWO == primary_schema.get_part_level()) {
1492
        const schema::ObPartition *partition = NULL;
1493
        if (OB_FAIL(primary_schema.get_partition_by_partition_index(part_idx, schema::CHECK_PARTITION_MODE_NORMAL, partition))) {
1494
          LOG_WARN("get_partition_by_partition_index fail", KR(ret), K(part_idx), K(primary_schema));
1495
        } else if (OB_ISNULL(partition)) {
1496
          ret = OB_ERR_UNEXPECTED;
1497
          LOG_WARN("part ptr is null", KR(ret), K(part_idx), K(primary_schema));
1498
        } else {
1499
          primary_partition_offset += partition->get_subpartition_num();
1500
        }
1501
      } else {
1502
        ret = OB_ERR_UNEXPECTED;
1503
        LOG_WARN("unknow table part_level", KR(ret), K(primary_schema));
1504
      }
1505
    }
1506
  }
1507

1508
  return ret;
1509
}
1510

1511
int ObNewTableTabletAllocator::alloc_tablet_for_tablegroup(
1512
    const schema::ObTableSchema &primary_schema,
1513
    const schema::ObTableSchema &table_schema,
1514
    const schema::ObSimpleTablegroupSchema &tablegroup_schema)
1515
{
1516
  int ret = OB_SUCCESS;
1517
  if (tablegroup_schema.get_sharding() == OB_PARTITION_SHARDING_NONE || table_schema.get_part_level() == schema::PARTITION_LEVEL_ZERO) {
1518
    common::ObArray<share::ObLSID> pre_ls_id_array;
1519
    if (OB_FAIL(generate_ls_array_by_primary_schema(primary_schema, pre_ls_id_array))) {
1520
      LOG_WARN("fail to generate_ls_array_by_primary_schema", KR(ret), K(primary_schema));
1521
    } else {
1522
      // first tablet location ls
1523
      ObLSID dest_ls_id = pre_ls_id_array.at(0);
1524
      for (int64_t i = 0; i < table_schema.get_all_part_num() && OB_SUCC(ret); i++) {
1525
        if (OB_FAIL(ls_id_array_.push_back(dest_ls_id))) {
1526
          LOG_WARN("failed to push_back", KR(ret), K(i), K(tenant_id_), K(table_schema));
1527
        }
1528
      }
1529
    }
1530
  } else if (tablegroup_schema.get_sharding() == OB_PARTITION_SHARDING_PARTITION) {
1531
    common::ObArray<share::ObLSID> all_ls_id_array;
1532
    common::ObArray<share::ObLSID> pre_ls_id_array;
1533
    if (primary_schema.get_partition_num() != table_schema.get_partition_num()) {
1534
      ret = OB_ERR_UNEXPECTED;
1535
      LOG_WARN("mismatch partition num in tablegroup", KR(ret), K(primary_schema), K(table_schema));
1536
    } else if (OB_FAIL(generate_ls_array_by_primary_schema(primary_schema, all_ls_id_array))) {
1537
      LOG_WARN("fail to generate_ls_array_by_primary_schema", KR(ret), K(primary_schema));
1538
    } else if (OB_FAIL(extract_one_level_ls_array_by_primary_schema(primary_schema, all_ls_id_array, pre_ls_id_array))) {
1539
      LOG_WARN("fail to extract_one_level_ls_array_by_primary_schema", KR(ret), K(primary_schema));
1540
    } else {
1541
      /*
1542
       * keep align with one level partition
1543
       */
1544
      for (int64_t i = 0; i < table_schema.get_partition_num() && OB_SUCC(ret); i++) {
1545
        ObLSID dest_ls_id = pre_ls_id_array.at(i);
1546
        if (schema::PARTITION_LEVEL_ONE == table_schema.get_part_level()) {
1547
          if (OB_FAIL(ls_id_array_.push_back(dest_ls_id))) {
1548
            LOG_WARN("failed to push_back", KR(ret), K(i));
1549
          }
1550
        } else if (schema::PARTITION_LEVEL_TWO == table_schema.get_part_level()) {
1551
          const schema::ObPartition *partition = NULL;
1552
          if (OB_FAIL(table_schema.get_partition_by_partition_index(i, schema::CHECK_PARTITION_MODE_NORMAL, partition))) {
1553
            LOG_WARN("get_partition_by_partition_index fail", KR(ret), K(i), K(table_schema));
1554
          } else if (OB_ISNULL(partition)) {
1555
            ret = OB_ERR_UNEXPECTED;
1556
            LOG_WARN("part ptr is null", KR(ret), K(i), K(table_schema));
1557
          } else {
1558
            for (int64_t sp = 0; OB_SUCC(ret) && sp < partition->get_subpartition_num(); sp++) {
1559
              if (OB_FAIL(ls_id_array_.push_back(dest_ls_id))) {
1560
                LOG_WARN("failed to push ls_id to array", KR(ret), K(dest_ls_id));
1561
              }
1562
            }
1563
          }
1564
        } else {
1565
          ret = OB_ERR_UNEXPECTED;
1566
          LOG_WARN("unknow table part_level", KR(ret), K(table_schema));
1567
        }
1568
      }
1569
    }
1570
  } else if (tablegroup_schema.get_sharding() == OB_PARTITION_SHARDING_ADAPTIVE) {
1571
    if (primary_schema.get_all_part_num() != table_schema.get_all_part_num()) {
1572
      ret = OB_ERR_UNEXPECTED;
1573
      LOG_WARN("mismatch partition in tablegroup", KR(ret), K(table_schema), K(primary_schema));
1574
    } else if (OB_FAIL(alloc_tablet_by_primary_schema(primary_schema))) {
1575
      LOG_WARN("fail to alloc tablet by primary_schema", KR(ret), K(primary_schema));
1576
    }
1577
  } else {
1578
    ret = OB_ERR_UNEXPECTED;
1579
    LOG_WARN("unknow sharding option", KR(ret), K(tablegroup_schema.get_sharding()));
1580
  }
1581
  return ret;
1582
}
1583

1584
int ObNewTableTabletAllocator::alloc_ls_for_normal_table_tablet(
1585
    const share::schema::ObTableSchema &table_schema)
1586
{
1587
  int ret = OB_SUCCESS;
1588
  LOG_INFO("alloc ls for normal table tablet",
1589
           "tenant_id", table_schema.get_tenant_id(),
1590
           "table_id", table_schema.get_table_id());
1591
  if (OB_UNLIKELY(!inited_)) {
1592
    ret = OB_NOT_INIT;
1593
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1594
  } else if (OB_FAIL(alloc_tablet_by_count_balance(table_schema))) {
1595
    LOG_WARN("fail to alloc tablet by count balance", KR(ret));
1596
  }
1597
  return ret;
1598
}
1599

1600
int ObNewTableTabletAllocator::wait_ls_elect_leader_(
1601
    const uint64_t tenant_id,
1602
    const ObLSID &ls_id)
1603
{
1604
  int ret = OB_SUCCESS;
1605
  ObTimeoutCtx ctx;
1606
  if (OB_UNLIKELY(!inited_)) {
1607
    ret = OB_NOT_INIT;
1608
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret), K(tenant_id), K(ls_id));
1609
  } else if (OB_ISNULL(GCTX.location_service_)
1610
             || OB_UNLIKELY(OB_INVALID_TENANT_ID == tenant_id || !ls_id.is_valid())) {
1611
    ret = OB_INVALID_ARGUMENT;
1612
    LOG_WARN("invalid argument", KR(ret), K(tenant_id), K(ls_id));
1613
  } else if (OB_FAIL(ObShareUtil::set_default_timeout_ctx(ctx, GCONF.internal_sql_execute_timeout))) {
1614
    LOG_WARN("failed to set default timeout", KR(ret));
1615
  } else {
1616
    bool has_leader = false;
1617
    ObAddr ls_leader;
1618
    while (OB_SUCC(ret) && !has_leader) {
1619
      int tmp_ret = OB_SUCCESS;
1620
      ls_leader.reset();
1621
      const share::ObLSReplica *leader_replica = nullptr;
1622
      if (0 > ctx.get_timeout()) {
1623
        ret = OB_TIMEOUT;
1624
        LOG_WARN("wait ls elect leader timeout", KR(ret));
1625
      } else if (OB_TMP_FAIL(GCTX.location_service_->nonblock_get_leader(GCONF.cluster_id, tenant_id, ls_id, ls_leader))) {
1626
        LOG_WARN("fail to get ls leader", KR(ret), K(tenant_id), K(ls_id), K(ls_leader));
1627
      } else {
1628
        has_leader = true;
1629
      }
1630
      if (OB_SUCC(ret) && !has_leader) {
1631
        LOG_WARN("fail to wait log stream elect leader, need retry", K(tenant_id), K(ls_id), K(ls_leader));
1632
        ob_usleep(WAIT_INTERVAL_US);
1633
      }
1634
    }
1635
  }
1636
  return ret;
1637
}
1638

1639
int ObNewTableTabletAllocator::alloc_ls_for_duplicate_table_(
1640
    const share::schema::ObTableSchema &table_schema)
1641
{
1642
  int ret = OB_SUCCESS;
1643
  uint64_t tenant_id = table_schema.get_tenant_id();
1644
  LOG_INFO("alloc ls for duplicate table tablet",
1645
           "tenant_id", table_schema.get_tenant_id(),
1646
           "table_id", table_schema.get_table_id());
1647
  share::ObLSStatusOperator ls_status_operator;
1648
  share::ObLSStatusInfo duplicate_ls_status_info;
1649
  ObTimeoutCtx ctx;
1650
  if (OB_UNLIKELY(!inited_)) {
1651
    ret = OB_NOT_INIT;
1652
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1653
  } else if (OB_ISNULL(GCTX.sql_proxy_)
1654
             || OB_ISNULL(GCTX.location_service_)
1655
             || OB_ISNULL(GCTX.srv_rpc_proxy_)) {
1656
    ret = OB_INVALID_ARGUMENT;
1657
    LOG_WARN("invalid argument", KR(ret));
1658
  } else if (OB_FAIL(ObShareUtil::set_default_timeout_ctx(ctx, GCONF.internal_sql_execute_timeout))) {
1659
    LOG_WARN("failed to set default timeout", KR(ret));
1660
  } else {
1661
    obrpc::ObCreateDupLSArg arg;
1662
    obrpc::ObCreateDupLSResult result;
1663
    while (OB_SUCC(ret)) {
1664
      int tmp_ret = OB_SUCCESS;
1665
      duplicate_ls_status_info.reset();
1666
      if (0 > ctx.get_timeout()) {
1667
        ret = OB_TIMEOUT;
1668
        LOG_WARN("wait creating duplicate log stream timeout", KR(ret));
1669
      } else if (OB_TMP_FAIL(ls_status_operator.get_duplicate_ls_status_info(
1670
                             tenant_id,
1671
                             *GCTX.sql_proxy_,
1672
                             duplicate_ls_status_info,
1673
                             share::OBCG_DEFAULT/*group_id*/))) {
1674
        if (OB_ENTRY_NOT_EXIST == tmp_ret) {
1675
          LOG_INFO("duplicate log stream not exist, should create one duplicate log stream");
1676
          tmp_ret = OB_SUCCESS;
1677
          // create duplicate ls
1678
          ObAddr leader;
1679
          const int64_t timeout = ctx.get_timeout();
1680
          if (OB_TMP_FAIL(GCTX.location_service_->get_leader(GCONF.cluster_id, tenant_id,
1681
                                                             SYS_LS, FALSE, leader))) {
1682
            LOG_WARN("failed to get leader", KR(tmp_ret), K(tenant_id));
1683
          } else if (OB_TMP_FAIL(arg.init(tenant_id))) {
1684
            LOG_WARN("failed to init arg", KR(ret), K(tenant_id));
1685
          } else if (OB_TMP_FAIL(GCTX.srv_rpc_proxy_->to(leader).timeout(timeout).notify_create_duplicate_ls(arg, result))) {
1686
            LOG_WARN("failed to create tenant duplicate ls", KR(tmp_ret), K(tenant_id), K(leader), K(arg), K(timeout));
1687
            if (OB_CONFLICT_WITH_CLONE == tmp_ret) {
1688
              ret = tmp_ret;
1689
              LOG_WARN("tenant is in clone procedure, can not create new log stream for now", KR(ret), K(tenant_id), K(arg));
1690
            }
1691
          }
1692
        } else {
1693
          LOG_WARN("fail to get duplicate log stream from table", KR(tmp_ret), K(tenant_id));
1694
        }
1695
      } else if (!duplicate_ls_status_info.ls_is_normal()) {
1696
        LOG_TRACE("duplicate log stream is not in normal status", K(duplicate_ls_status_info));
1697
      } else if (OB_FAIL(wait_ls_elect_leader_(
1698
                             duplicate_ls_status_info.tenant_id_,
1699
                             duplicate_ls_status_info.ls_id_))) {
1700
        LOG_WARN("fail to wait duplicate ls elect leader", KR(ret), K(duplicate_ls_status_info));
1701
      } else {
1702
        for (int64_t i = 0; i < table_schema.get_all_part_num() && OB_SUCC(ret); i++) {
1703
          if (OB_FAIL(ls_id_array_.push_back(duplicate_ls_status_info.ls_id_))) {
1704
            LOG_WARN("failed to push_back", KR(ret), K(i), K(duplicate_ls_status_info));
1705
          }
1706
        }
1707
        break;
1708
      }
1709
      if (OB_SUCC(ret)) {
1710
        LOG_WARN("fail to get duplicate log stream, need retry", K(tenant_id), K(duplicate_ls_status_info));
1711
        ob_usleep(WAIT_INTERVAL_US);
1712
      }
1713
    }
1714
  }
1715
  return ret;
1716
}
1717

1718
int ObNewTableTabletAllocator::check_and_replace_ls_(
1719
    ObMySQLTransaction &trans,
1720
    const uint64_t tenant_id)
1721
{
1722
  int ret = OB_SUCCESS;
1723
  ObArray<ObLSID> locked_ls_id_array;
1724
  if (OB_UNLIKELY(!inited_) || OB_ISNULL(sql_proxy_)) {
1725
    ret = OB_NOT_INIT;
1726
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1727
  } else if (OB_FAIL(locked_ls_id_array.reserve(ls_id_array_.count()))) {
1728
    LOG_WARN("reserve failed", KR(ret), K(tenant_id), K_(ls_id_array));
1729
  } else {
1730
    ARRAY_FOREACH(ls_id_array_, idx) {
1731
      ObLSID prev_ls_id;
1732
      ObLSID new_ls_id;
1733
      ObLSAttr new_ls_attr;
1734
      ObLSAttr curr_ls_attr;
1735
      const ObLSID curr_ls_id = ls_id_array_.at(idx);
1736
      if (idx > 0) {
1737
        int64_t index = OB_INVALID_INDEX;
1738
        find_last_user_ls_(locked_ls_id_array, index);
1739
        if (index >= 0 && index < locked_ls_id_array.count()) {
1740
          prev_ls_id = locked_ls_id_array.at(index);
1741
        }
1742
      }
1743
      if (curr_ls_id.is_sys_ls()) { // do not lock sys ls
1744
        new_ls_id = curr_ls_id;
1745
      } else if (OB_FAIL(lock_and_check_ls_(
1746
          trans,
1747
          tenant_id,
1748
          locked_ls_id_array,
1749
          curr_ls_id,
1750
          curr_ls_attr))) {
1751
        if (OB_STATE_NOT_MATCH == ret) {
1752
          if (OB_FAIL(choose_new_ls_(tenant_id, curr_ls_attr, prev_ls_id, new_ls_id))) {
1753
            LOG_WARN("choose new ls failed", KR(ret),
1754
                K(tenant_id), K(curr_ls_attr), K(prev_ls_id), K(new_ls_id));
1755
          } else if (OB_FAIL(lock_and_check_ls_(
1756
              trans,
1757
              tenant_id,
1758
              locked_ls_id_array,
1759
              new_ls_id,
1760
              new_ls_attr))) {
1761
            // new ls should not be OB_STATE_NOT_MATCH
1762
            LOG_WARN("check and lock ls failed", KR(ret),
1763
                K(tenant_id), K(locked_ls_id_array), K(new_ls_id), K(new_ls_attr));
1764
          } else {
1765
            LOG_INFO("the ls allocated for tablet creating has changed",
1766
                KR(ret), K(tenant_id), "old_ls_id", curr_ls_id, K(new_ls_id));
1767
          }
1768
        } else {
1769
          LOG_WARN("check and lock ls failed", KR(ret),
1770
              K(tenant_id), K(locked_ls_id_array), K(curr_ls_id), K(curr_ls_attr));
1771
        }
1772
      } else { // lock user ls successfully
1773
        new_ls_id = curr_ls_id;
1774
      }
1775
      if (FAILEDx(locked_ls_id_array.push_back(new_ls_id))){
1776
        LOG_WARN("push back failed", KR(ret), K(new_ls_id), K(locked_ls_id_array));
1777
      }
1778
    }
1779
    if (OB_FAIL(ret)) {
1780
    } else if (locked_ls_id_array.count() != ls_id_array_.count()) {
1781
      ret = OB_ERR_UNEXPECTED;
1782
      LOG_WARN("ls_id_array count not match", KR(ret), K(tenant_id),
1783
          "tmp_ls_id_arry count", locked_ls_id_array.count(),
1784
          "ls_id_array_ count", ls_id_array_.count(), K(locked_ls_id_array), K_(ls_id_array));
1785
    } else if (OB_FAIL(ls_id_array_.assign(locked_ls_id_array))) {
1786
      LOG_WARN("assign failed", KR(ret), K(locked_ls_id_array), K_(ls_id_array));
1787
    }
1788
  }
1789
  return ret;
1790
}
1791

1792
void ObNewTableTabletAllocator::find_last_user_ls_(
1793
    const ObIArray<ObLSID> &ls_id_array,
1794
    int64_t &index)
1795
{
1796
  index = OB_INVALID_INDEX;
1797
  for (int64_t i = ls_id_array.count() - 1; i >= 0; --i) {
1798
    const ObLSID &curr_ls = ls_id_array.at(i);
1799
    if (curr_ls.id() > ObLSID::MIN_USER_LS_ID) {
1800
      index = i;
1801
      break;
1802
    }
1803
  }
1804
}
1805

1806
int ObNewTableTabletAllocator::lock_and_check_ls_(
1807
    ObMySQLTransaction &trans,
1808
    const uint64_t tenant_id,
1809
    const ObIArray<ObLSID> &locked_ls_id_array,
1810
    const ObLSID &ls_id,
1811
    ObLSAttr &ls_attr)
1812
{
1813
  int ret = OB_SUCCESS;
1814
  ls_attr.reset();
1815
  if (OB_UNLIKELY(!inited_) || OB_ISNULL(sql_proxy_)) {
1816
    ret = OB_NOT_INIT;
1817
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1818
  } else if (!ls_id.is_valid_with_tenant(tenant_id)) {
1819
    ret = OB_INVALID_ARGUMENT;
1820
    LOG_WARN("invalid args", KR(ret), K(tenant_id), K(ls_id));
1821
  } else if (common::has_exist_in_array(locked_ls_id_array, ls_id)) {
1822
    // ls has been locked
1823
  } else {
1824
    ObLSAttrOperator ls_operator(tenant_id, sql_proxy_);
1825
    if (OB_FAIL(ObLSObjLockUtil::lock_ls_in_trans(
1826
        trans,
1827
        tenant_id,
1828
        ls_id,
1829
        SHARE))) {
1830
      LOG_WARN("lock ls in trans failed", KR(ret), K(tenant_id), K(ls_id));
1831
    } else if (OB_FAIL(ls_operator.get_ls_attr(ls_id, false/*for_update*/, trans, ls_attr))) {
1832
      if (OB_ENTRY_NOT_EXIST == ret) {
1833
        ls_attr.reset();
1834
        ret = OB_STATE_NOT_MATCH;
1835
        LOG_INFO("ls has been deleted when creating tablet", KR(ret), K(ls_id));
1836
      } else {
1837
        LOG_WARN("get ls attr failed", KR(ret), K(ls_id), K(ls_attr));
1838
      }
1839
    } else if (!ls_attr.ls_is_normal() || ls_attr.get_ls_flag().is_block_tablet_in()) {
1840
      ret = OB_STATE_NOT_MATCH;
1841
      LOG_TRACE("can not create tablet on this ls beacuse it is not in normal status or is block tablet in",
1842
          KR(ret), K(tenant_id), K(ls_id), K(ls_attr));
1843
    }
1844
  }
1845
  return ret;
1846
}
1847

1848
int ObNewTableTabletAllocator::choose_new_ls_(
1849
    const uint64_t tenant_id,
1850
    const ObLSAttr &old_ls_attr,
1851
    const ObLSID &prev_ls_id,
1852
    ObLSID &new_ls_id)
1853
{
1854
  int ret = OB_SUCCESS;
1855
  if (OB_UNLIKELY(!inited_) || OB_ISNULL(sql_proxy_)) {
1856
    ret = OB_NOT_INIT;
1857
    LOG_WARN("ObNewTableTabletAllocator not init", KR(ret));
1858
  } else if (!old_ls_attr.is_valid() || !old_ls_attr.ls_is_normal()) {
1859
    if (prev_ls_id.is_valid()) {
1860
      new_ls_id = prev_ls_id;
1861
    } else {
1862
      ObLSAttrOperator ls_operator(tenant_id, sql_proxy_);
1863
      if (OB_FAIL(ls_operator.get_random_normal_user_ls(new_ls_id))) {
1864
        LOG_WARN("get random normal user ls failed", KR(ret), K(tenant_id), K(new_ls_id));
1865
      }
1866
    }
1867
  } else if (old_ls_attr.get_ls_flag().is_block_tablet_in()) {
1868
    //only in 4200 canbe block tablet in, no need process data_version
1869
    if (OB_FAIL(ObBalanceTaskTableOperator::get_merge_task_dest_ls_by_src_ls(
1870
        *sql_proxy_,
1871
        tenant_id,
1872
        old_ls_attr.get_ls_id(),
1873
        new_ls_id))) {
1874
      LOG_WARN("get dest ls by src ls failed", KR(ret), K(tenant_id), K(old_ls_attr), K(new_ls_id));
1875
    }
1876
  } else {
1877
    ret = OB_ERR_UNEXPECTED;
1878
    LOG_WARN("unexpected ls_attr", KR(ret), K(old_ls_attr));
1879
  }
1880
  return ret;
1881
}
1882

1883
}//end namespace rootserver
1884
}//end namespace oceanbase
1885

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.