Merge shards
You can call the MergeShard operation to merge two adjacent shards into one by using Simple Log Service SDK for Python.
Prerequisites
-
Simple Log Service is activated. For more information, see Activate Simple Log Service.
-
The Simple Log Service SDK for Python is initialized. For more information, see Initialize Simple Log Service SDK for Python.
Parameter description
def merge_shard(self, project_name, logstore_name, shardId):
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
|
project_name |
String |
Yes |
The name of the project. |
ali-test-project |
|
logstore_name |
String |
Yes |
The name of the logstore. |
test-logstore |
|
shardId |
int |
Yes |
The ID of the shard. Important
When merging shards, two contiguous shards with a status of readwrite are required. The shardId parameter must be the ID of the first of these two contiguous shards. After the merge, both contiguous shards become readonly, and the hash range of the new merged shard is the union of the original contiguous shards. |
2 |
Sample code
from aliyun.log import LogClient
import os
def main():
# In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# Specify a Simple Log Service endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
project_name = "ali-test-project"
logstore_name = "xmltest"
shardId = 2
client = LogClient(endpoint, accessKeyId, accessKey, "")
res = client.merge_shard(project_name, logstore_name, shardId)
res.log_print()
if __name__ == '__main__':
main()
Sample response
In this example, Shard 2 is merged with its adjacent Shard 3, which Simple Log Service automatically identifies. After the merge, both original shards become readonly.
The following response shows the shard information before the merge:
ListShardResponse:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '668', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Wed, 30 Oct 2024 07:06:50 GMT', 'x-log-time': '1730272010', 'x-log-requestid': '6721DB0AE05BB30ACE5654AE'}
res:
[{'shardID': 2, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '40000000000000000000000000000000', 'createTime': 1730271813},
{'shardID': 3, 'status': 'readwrite', 'inclusiveBeginKey': '40000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1730271813},
{'shardID': 1, 'status': 'readwrite', 'inclusiveBeginKey': '80000000000000000000000000000000', 'exclusiveEndKey': 'ffffffffffffffffffffffffffffffff', 'createTime': 1716966695},
{'shardID': 0, 'status': 'readonly', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1716966695}]
Process finished with exit code 0
The following response shows the shard information after the merge:
ListShardResponse:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '500', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Wed, 30 Oct 2024 07:14:23 GMT', 'x-log-time': '1730272463', 'x-log-requestid': '6721DCCF4CCA1EE42CF839A5'}
res: [{'shardID': 4, 'status': 'readwrite', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1730272463}, {'shardID': 2, 'status': 'readonly', 'inclusiveBeginKey': '00000000000000000000000000000000', 'exclusiveEndKey': '40000000000000000000000000000000', 'createTime': 1730271813}, {'shardID': 3, 'status': 'readonly', 'inclusiveBeginKey': '40000000000000000000000000000000', 'exclusiveEndKey': '80000000000000000000000000000000', 'createTime': 1730271813}]
Process finished with exit code 0
References
-
For more information about using command-line interface (CLI) to manage shards, see the following topics:
-
For more information about shard management APIs, see the following topics:
-
The aliyun-log-python-sdk on GitHub repository provides additional samples.
-
Simple Log Service uses shards to manage the read and write capacities of logstores. For more information, see Shard.