async def _lease_shard(self):
prefix = self._prefix + '<STR_LIT>'<EOL>self._lease = await self._client.grant_lease(ttl=self._shard_ttl)<EOL>timestamp_bytes = (<NUM_LIT:0>).to_bytes(<NUM_LIT:4>, byteorder='<STR_LIT>')<EOL>retries = <NUM_LIT:0><EOL>while True:<EOL><INDENT>shard_subidx = len(prefix)<EOL>shard_id = <NUM_LIT:0><EOL>records = await self._client.range_keys(range_prefix(prefix))<EOL>nums = sorted(int_fromhexbytes(k[shard_subidx:]) for k, _ in records)<EOL>for i, n in enumerate(nums):<EOL><INDENT>if n > i:<EOL><INDENT>shard_id = i<EOL>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>shard_id = len(nums)<EOL><DEDENT>shard_id = int_tohex(shard_id, length=<NUM_LIT:2>)<EOL>logger.debug(f'<STR_LIT>')<EOL>shard_path = f'<STR_LIT>'<EOL>is_success, _ = await self._client.txn(compare=[<EOL>transaction.Version(shard_path) == <NUM_LIT:0> <EOL>], success=[<EOL>KV.put.txn(shard_path, timestamp_bytes, lease=self._lease)<EOL>])<EOL>if is_success:<EOL><INDENT>self._shard_id = shard_id<EOL>self._timestamp = None<EOL>self._seqnum = None<EOL>break<EOL><DEDENT>else:<EOL><INDENT>logger.debug(f'<STR_LIT>')<EOL>await _random_nap(retries)<EOL>retries += <NUM_LIT:1><EOL>if retries > <NUM_LIT:10>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT>
申请一个新分片，从小到大[0,4095]寻找一个为占用的分片