5. 关键代码片段
以下代码展示如何使用SDK修改实例参数:
Copied!
func main() {
if int32(len(os.Args)) != 7 {
log.Println("Illegal Arguments")
}
iamEndpoint := os.Args[1]
endpoint := os.Args[2]
ak := os.Args[3]
sk := os.Args[4]
instanceId := os.Args[5]
regionId := os.Args[6]
entityId := os.Args[7]
auth := coreBasic.NewCredentialsBuilder().WithIamEndpointOverride(iamEndpoint).WithAk(ak).WithSk(sk).Build()
client := *dds.NewDdsClient(dds.DdsClientBuilder().
WithCredential(auth).
WithRegion(&coreRegion.Region{Id: regionId, Endpoint: endpoint}).
Build())
paramsConfigDemoQueryInstanceParamGroup(client, instanceId, entityId)
paramsConfigDemoUpdateInstanceParamGroup(client, instanceId, entityId)
paramsConfigDemoRestartInstance(client, instanceId)
paramsConfigDemoQueryInstanceParamGroup(client, instanceId, entityId)
}
func paramsConfigDemoQueryInstanceDetail(client dds.DdsClient, instanceId string) {
request := ddsModel.ListInstancesRequest{}
request.Id = &instanceId
response, err := client.ListInstances(&request)
if err != nil {
fmt.Println(err)
return
}
log.Println(response.String())
}
func paramsConfigDemoRestartInstance(client dds.DdsClient, instanceId string) {
request := ddsModel.RestartInstanceRequest{}
body := ddsModel.RestartInstanceRequestBody{}
body.TargetId = instanceId
request.InstanceId = instanceId
request.Body = &body
response, err := client.RestartInstance(&request)
if err != nil {
fmt.Println(err)
return
}
log.Println(response.String())
}
func paramsConfigDemoUpdateInstanceParamGroup(client dds.DdsClient, instanceId string, entityId string) {
request := ddsModel.UpdateEntityConfigurationRequest{}
body := ddsModel.UpdateConfigurationParameterResult{}
paramMap := make(map[string]string)
paramMap["net.maxIncomingConnections"] = "400"
paramMap["connPoolMaxConnsPerHost"] = "600"
body.ParameterValues = paramMap
body.EntityId = entityId
request.InstanceId = instanceId
request.Body = &body
response, err := client.UpdateEntityConfiguration(&request)
if err != nil {
fmt.Println(err)
return
}
log.Println(response.String())
}
func paramsConfigDemoQueryInstanceParamGroup(client dds.DdsClient, instanceId string, entityId string) {
request := ddsModel.ShowEntityConfigurationRequest{}
request.InstanceId = instanceId
request.EntityId = entityId
response, err := client.ShowEntityConfiguration(&request)
if err != nil {
fmt.Println(err)
return
}
log.Println(response.String())
}
6. 返回结果示例
- 查询指定实例的参数信息(ShowEntityConfiguration)接口的返回值:
Copied!
class ShowEntityConfigurationResponse {
datastoreVersion: 3.4
datastoreName: mongodb
created: 2022-10-11T08:22:50+0000
updated: 2022-10-20T02:15:49+0000
parameters: [class EntityConfigurationParametersResult {
name: connPoolMaxConnsPerHost
value: 500
valueRange: 200-2000
restartRequired: true
readonly: false
type: integer
description: Maximum size of the connection pools for connections to other mongod instances.
}, class EntityConfigurationParametersResult {
name: cursorTimeoutMillis
value: 600000
valueRange: 600000-1000000
restartRequired: false
readonly: false
type: integer
description: Expiration threshold for idle cursors before DDS removes them.
}, class EntityConfigurationParametersResult {
name: disableJavaScriptJIT
value: true
valueRange: true|false
restartRequired: false
readonly: false
type: boolean
description: Enable or disable JavaScriptJIT.
}, class EntityConfigurationParametersResult {
name: failIndexKeyTooLong
value: true
valueRange: true|false
restartRequired: false
readonly: true
type: boolean
description: If the length of an indexed field value is longer than Index Key Length Limit, an error will be reported.
}, class EntityConfigurationParametersResult {
name: net.maxIncomingConnections
value: 300
valueRange: 200-500
restartRequired: false
readonly: false
type: integer
description: Maximum number of simultaneous connections that mongos or mongod will accept. The default value depends on system architecture.
}, class EntityConfigurationParametersResult {
name: operationProfiling.mode
value: slowOp
valueRange: off|slowOp|all
restartRequired: true
readonly: false
type: string
description: Level of database profiling.
}, class EntityConfigurationParametersResult {
name: operationProfiling.slowOpThresholdMs
value: 500
valueRange: 10-10000
restartRequired: false
readonly: false
type: integer
description: Slow request threshold. If there is no special requirement, it is recommended to use the default 500ms. Requests that exceed this value will be recorded in the system.profile collection of the corresponding db.
}, class EntityConfigurationParametersResult {
name: replication.enableMajorityReadConcern
value: false
valueRange: true|false
restartRequired: true
readonly: true
type: boolean
description: Whether to enable enableMajorityReadConcern.
}, class EntityConfigurationParametersResult {
name: security.authorization
value: enabled
valueRange: disabled|enabled
restartRequired: true
readonly: true
type: string
description: Enable or disable Access Control.
}, class EntityConfigurationParametersResult {
name: storage.directoryPerDB
value: true
valueRange: true|false
restartRequired: true
readonly: true
type: boolean
description: Whether DDS uses a separate directory to store data for each database.
}, class EntityConfigurationParametersResult {
name: storage.engine
value: wiredTiger
valueRange: mmapv1|wiredTiger|inmem
restartRequired: true
readonly: true
type: string
description: Storage engine of mongod.
}, class EntityConfigurationParametersResult {
name: storage.indexBuildRetry
value: true
valueRange: true|false
restartRequired: true
readonly: false
type: boolean
description: Whether mongod rebuilds indexes on the next start up after an index building failure.
}, class EntityConfigurationParametersResult {
name: storage.journal.commitIntervalMs
value: 100
valueRange: 1-500
restartRequired: false
readonly: false
type: integer
description: Interval in milliseconds between journal operations.
}, class EntityConfigurationParametersResult {
name: storage.journal.enabled
value: true
valueRange: true|false
restartRequired: true
readonly: true
type: boolean
description: Enable or disable the durability journal.
}, class EntityConfigurationParametersResult {
name: storage.syncPeriodSecs
value: 60
valueRange: 60
restartRequired: false
readonly: true
type: integer
description: Interval in seconds at which DDS flushes modified data to the data files via an fsync operation.
}, class EntityConfigurationParametersResult {
name: storage.wiredTiger.engineConfig.cacheSizeGB
value: 1.5
valueRange: 1.2-1.5
restartRequired: false
readonly: false
type: float
description: Maximum size of the internal cache used by WiredTiger.
}, class EntityConfigurationParametersResult {
name: storage.wiredTiger.engineConfig.directoryForIndexes
value: true
valueRange: true|false
restartRequired: true
readonly: true
type: boolean
description: Whether mongod stores indexes and collections in separate directories.
}, class EntityConfigurationParametersResult {
name: storage.wiredTiger.engineConfig.journalCompressor
value: snappy
valueRange: none|snappy|zlib
restartRequired: true
readonly: true
type: string
description: Compression type used to compress WiredTiger journal.
}, class EntityConfigurationParametersResult {
name: wiredTigerConcurrentWriteTransactions
value: 128
valueRange: 64-256
restartRequired: false
readonly: false
type: integer
description: Maximum number of concurrent write transactions allowed into the WiredTiger storage engine.
}]
}
- 修改需要重启生效的参数(UpdateEntityConfiguration)接口的返回值:
Copied!
class UpdateEntityConfigurationResponse {
jobId: b523ca1a-1d3b-4751-802c-3312e0b34e5b
restartRequired: true
}
- 重启实例(RestartInstance)接口的返回值:
Copied!
class RestartInstanceResponse {
jobId: da4f001f-3abc-4c99-9bbc-a85ae9e2ec33
}
- 查询指定实例的参数信息(ShowEntityConfiguration)接口的返回值:
Copied!
class ShowEntityConfigurationResponse {
datastoreVersion: 3.4
datastoreName: mongodb
created: 2022-10-11T08:22:50+0000
updated: 2022-10-20T02:15:49+0000
parameters: [class EntityConfigurationParametersResult {
name: connPoolMaxConnsPerHost
value: 600
valueRange: 200-2000
restartRequired: true
readonly: false
type: integer
description: Maximum size of the connection pools for connections to other mongod instances.
}, class EntityConfigurationParametersResult {
name: cursorTimeoutMillis
value: 600000
valueRange: 600000-1000000
restartRequired: false
readonly: false
type: integer
description: Expiration threshold for idle cursors before DDS removes them.
}, class EntityConfigurationParametersResult {
name: disableJavaScriptJIT
value: true
valueRange: true|false
restartRequired: false
readonly: false
type: boolean
description: Enable or disable JavaScriptJIT.
}, class EntityConfigurationParametersResult {
name: failIndexKeyTooLong
value: true
valueRange: true|false
restartRequired: false
readonly: true
type: boolean
description: If the length of an indexed field value is longer than Index Key Length Limit, an error will be reported.
}, class EntityConfigurationParametersResult {
name: net.maxIncomingConnections
value: 400
valueRange: 200-500
restartRequired: false
readonly: false
type: integer
description: Maximum number of simultaneous connections that mongos or mongod will accept. The default value depends on system architecture.
}, class EntityConfigurationParametersResult {
name: operationProfiling.mode
value: slowOp
valueRange: off|slowOp|all
restartRequired: true
readonly: false
type: string
description: Level of database profiling.
}, class EntityConfigurationParametersResult {
name: operationProfiling.slowOpThresholdMs
value: 500
valueRange: 10-10000
restartRequired: false
readonly: false
type: integer
description: Slow request threshold. If there is no special requirement, it is recommended to use the default 500ms. Requests that exceed this value will be recorded in the system.profile collection of the corresponding db.
}, class EntityConfigurationParametersResult {
name: replication.enableMajorityReadConcern
value: false
valueRange: true|false
restartRequired: true
readonly: true
type: boolean
description: Whether to enable enableMajorityReadConcern.
}, class EntityConfigurationParametersResult {
name: security.authorization
value: enabled
valueRange: disabled|enabled
restartRequired: true
readonly: true
type: string
description: Enable or disable Access Control.
}, class EntityConfigurationParametersResult {
name: storage.directoryPerDB
value: true
valueRange: true|false
restartRequired: true
readonly: true
type: boolean
description: Whether DDS uses a separate directory to store data for each database.
}, class EntityConfigurationParametersResult {
name: storage.engine
value: wiredTiger
valueRange: mmapv1|wiredTiger|inmem
restartRequired: true
readonly: true
type: string
description: Storage engine of mongod.
}, class EntityConfigurationParametersResult {
name: storage.indexBuildRetry
value: true
valueRange: true|false
restartRequired: true
readonly: false
type: boolean
description: Whether mongod rebuilds indexes on the next start up after an index building failure.
}, class EntityConfigurationParametersResult {
name: storage.journal.commitIntervalMs
value: 100
valueRange: 1-500
restartRequired: false
readonly: false
type: integer
description: Interval in milliseconds between journal operations.
}, class EntityConfigurationParametersResult {
name: storage.journal.enabled
value: true
valueRange: true|false
restartRequired: true
readonly: true
type: boolean
description: Enable or disable the durability journal.
}, class EntityConfigurationParametersResult {
name: storage.syncPeriodSecs
value: 60
valueRange: 60
restartRequired: false
readonly: true
type: integer
description: Interval in seconds at which DDS flushes modified data to the data files via an fsync operation.
}, class EntityConfigurationParametersResult {
name: storage.wiredTiger.engineConfig.cacheSizeGB
value: 1.5
valueRange: 1.2-1.5
restartRequired: false
readonly: false
type: float
description: Maximum size of the internal cache used by WiredTiger.
}, class EntityConfigurationParametersResult {
name: storage.wiredTiger.engineConfig.directoryForIndexes
value: true
valueRange: true|false
restartRequired: true
readonly: true
type: boolean
description: Whether mongod stores indexes and collections in separate directories.
}, class EntityConfigurationParametersResult {
name: storage.wiredTiger.engineConfig.journalCompressor
value: snappy
valueRange: none|snappy|zlib
restartRequired: true
readonly: true
type: string
description: Compression type used to compress WiredTiger journal.
}, class EntityConfigurationParametersResult {
name: wiredTigerConcurrentWriteTransactions
value: 128
valueRange: 64-256
restartRequired: false
readonly: false
type: integer
description: Maximum number of concurrent write transactions allowed into the WiredTiger storage engine.
}]
}
1. 介绍
文档数据库服务(document database service,简称dds)完全兼容mongodb协议,提供安全、高可用、高可靠、弹性伸缩和易用的数据库服务,同时提供一键部署、弹性扩容、容灾、备份、恢复、监控和告警等功能。
2. 流程图
3. 前置条件
1.已 注册 华为云,并完成 实名认证 。
2.获取华为云开发工具包(SDK),您也可以查看安装GO SDK。
3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥 。
4.已具备开发环境 ,支持GO 1.16及其以上版本。
4. SDK获取和安装
具体的SDK版本号请参见 SDK开发中心 。
5. 关键代码片段
以下代码展示如何使用SDK修改实例参数:
func main() { if int32(len(os.Args)) != 7 { log.Println("Illegal Arguments") } iamEndpoint := os.Args[1] endpoint := os.Args[2] // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; ak := os.Args[3] sk := os.Args[4] instanceId := os.Args[5] regionId := os.Args[6] entityId := os.Args[7] auth := coreBasic.NewCredentialsBuilder().WithIamEndpointOverride(iamEndpoint).WithAk(ak).WithSk(sk).Build() client := *dds.NewDdsClient(dds.DdsClientBuilder(). WithCredential(auth). WithRegion(&coreRegion.Region{Id: regionId, Endpoint: endpoint}). Build()) // 查询指定实例的参数信息 paramsConfigDemoQueryInstanceParamGroup(client, instanceId, entityId) // 修改需要重启生效的参数 paramsConfigDemoUpdateInstanceParamGroup(client, instanceId, entityId) // 重启实例 被修改参数如果需要重启则调用该方法,否则可以不调用该方法 paramsConfigDemoRestartInstance(client, instanceId) // 重启完毕后再次查询指定实例的参数信息 paramsConfigDemoQueryInstanceParamGroup(client, instanceId, entityId) } func paramsConfigDemoQueryInstanceDetail(client dds.DdsClient, instanceId string) { request := ddsModel.ListInstancesRequest{} request.Id = &instanceId response, err := client.ListInstances(&request) if err != nil { fmt.Println(err) return } log.Println(response.String()) } func paramsConfigDemoRestartInstance(client dds.DdsClient, instanceId string) { request := ddsModel.RestartInstanceRequest{} body := ddsModel.RestartInstanceRequestBody{} body.TargetId = instanceId request.InstanceId = instanceId request.Body = &body response, err := client.RestartInstance(&request) if err != nil { fmt.Println(err) return } log.Println(response.String()) } func paramsConfigDemoUpdateInstanceParamGroup(client dds.DdsClient, instanceId string, entityId string) { request := ddsModel.UpdateEntityConfigurationRequest{} body := ddsModel.UpdateConfigurationParameterResult{} paramMap := make(map[string]string) paramMap["net.maxIncomingConnections"] = "400" paramMap["connPoolMaxConnsPerHost"] = "600" body.ParameterValues = paramMap body.EntityId = entityId request.InstanceId = instanceId request.Body = &body response, err := client.UpdateEntityConfiguration(&request) if err != nil { fmt.Println(err) return } log.Println(response.String()) } func paramsConfigDemoQueryInstanceParamGroup(client dds.DdsClient, instanceId string, entityId string) { request := ddsModel.ShowEntityConfigurationRequest{} request.InstanceId = instanceId request.EntityId = entityId response, err := client.ShowEntityConfiguration(&request) if err != nil { fmt.Println(err) return } log.Println(response.String()) }
6. 返回结果示例
7.参考链接
请见 获取指定实例的参数API 您可以在 API Explorer 中直接运行调试该接口。
请见 修改指定实例的参数API 您可以在 API Explorer 中直接运行调试该接口。
请见 重启实例的数据库服务API 您可以在 API Explorer 中直接运行调试该接口。
修订记录