in aliyun/log/logclient_operator.py [0:0]
def arrange_shard(client, project, logstore, count, stategy=None):
total_length = TOTAL_SHARD_SIZE
avg_len = total_length * 1.0 / count
res = client.list_shards(project, logstore)
current_shard_count = res.count
rw_shards = [shard for shard in res.shards if shard['status'].lower() == 'readwrite']
split_left = count - len(rw_shards)
while split_left > 0:
current_rw_shards = [{'id': shard['shardID'],
'start': int('0x' + shard['inclusiveBeginKey'], base=16),
'end': int('0x' + shard['exclusiveEndKey'], base=16),
'length': int('0x' + shard['exclusiveEndKey'], base=16)
- int('0x' + shard['inclusiveBeginKey'], base=16),
'info': shard}
for shard in rw_shards]
# need to split shard
updated_rw_shards = []
for i, shard in enumerate(sorted(current_rw_shards, key=lambda x: x['length'], reverse=True)):
if split_left <= 0: # no need to split any more
break
sp_cnt = int(shard['length'] // avg_len)
if sp_cnt <= 1 and i == 0: # cannot split, but be the first one, should split it
sp_cnt = 2
elif sp_cnt <= 0:
sp_cnt = 1
new_rw_shards, increased_shard_count = _split_one_shard_to_multiple(client, project, logstore, shard,
sp_cnt, current_shard_count)
updated_rw_shards.extend(new_rw_shards)
current_shard_count += increased_shard_count
split_left -= len(new_rw_shards) - 1
# update current rw shards
rw_shards = updated_rw_shards
return ''