All Products
Search
Document Center

Simple Log Service:Merge shards

Last Updated:Dec 02, 2024

Call the MergeShard API to combine shards.

Prerequisites

Prerequisites

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

The sample code demonstrates the merging of Shard 2 with its contiguous Shard 3, which is automatically identified by the Simple Log Service. After the merge, the status of the original shards changes from readwrite to readonly.

Before the merge, the shard information is as follows:

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
 

After the merge, the sample response is as follows:

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