-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add some admin tool apis #798
Closed
francisoliverlee
wants to merge
2
commits into
apache:master
from
francisoliverlee:dev/0402-add-admin-apis
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package admin | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/apache/rocketmq-client-go/v2/primitive" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
var nameSrvAddr = []string{"127.0.0.1:9876"} | ||
|
||
func TestFetchAllTopicList(t *testing.T) { | ||
adminTool, err := NewAdmin(WithResolver(primitive.NewPassthroughResolver(nameSrvAddr))) | ||
assert.True(t, err == nil) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
topicNames, error := adminTool.FetchAllTopicList(context.Background()) | ||
assert.True(t, error == nil) | ||
assert.True(t, len(topicNames.TopicNameList) > 0) | ||
|
||
prettyJson, _ := json.MarshalIndent(topicNames, "", " ") | ||
fmt.Println(string(prettyJson)) | ||
} | ||
|
||
func TestGetBrokerClusterInfo(t *testing.T) { | ||
adminTool, err := NewAdmin(WithResolver(primitive.NewPassthroughResolver(nameSrvAddr))) | ||
assert.True(t, err == nil) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
cluster, err := adminTool.GetBrokerClusterInfo(context.Background()) | ||
assert.True(t, err == nil) | ||
assert.True(t, cluster != nil) | ||
|
||
prettyJson, _ := json.MarshalIndent(cluster, "", " ") | ||
fmt.Println(string(prettyJson)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package internal | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/apache/rocketmq-client-go/v2/rlog" | ||
"github.com/pkg/errors" | ||
"github.com/tidwall/gjson" | ||
"gopkg.in/yaml.v2" | ||
"strings" | ||
) | ||
|
||
type ClusterInfo struct { | ||
BrokerAddrTable map[string]BrokerData `json:"brokerAddrTable"` | ||
ClusterAddrTable map[string][]string `json:"clusterAddrTable"` | ||
} | ||
|
||
type ClusterInfoMap struct { | ||
BrokerAddrTable map[string]string `json:"brokerAddrTable"` | ||
ClusterAddrTable map[string]string `json:"clusterAddrTable"` | ||
} | ||
|
||
func ParseClusterInfo(jsonString string) (cluster *ClusterInfo, errResult error) { | ||
var brokerAddrTableResult = gjson.Get(jsonString, "brokerAddrTable") | ||
if !brokerAddrTableResult.Exists() { | ||
return nil, errors.New("json key for brokerAddrTable not exist") | ||
} | ||
var clusterAddrTableResult = gjson.Get(jsonString, "clusterAddrTable") | ||
if !clusterAddrTableResult.Exists() { | ||
return nil, errors.New("json key for clusterAddrTable not exist") | ||
} | ||
|
||
var mapAndArray, err = ParseClusterAddrTable(clusterAddrTableResult.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(mapAndArray) <= 0 { | ||
return nil, nil | ||
} | ||
|
||
var clusterInfo = &ClusterInfo{} | ||
clusterInfo.ClusterAddrTable, errResult = ParseClusterAddrTable(clusterAddrTableResult.String()) | ||
if errResult != nil { | ||
return nil, errResult | ||
} | ||
clusterInfo.BrokerAddrTable, errResult = ParseBrokerAddrTable(brokerAddrTableResult.String(), clusterInfo.ClusterAddrTable) | ||
if errResult != nil { | ||
return nil, errResult | ||
} | ||
return clusterInfo, nil | ||
} | ||
|
||
// parse broker addr table | ||
// input like : {"broker-a":{"brokerAddrs":{0:"127.0.0.1:10911"},"brokerName":"broker-a","cluster":"DefaultCluster"}} | ||
// result.key = broker name, result.value=BrokerData | ||
func ParseBrokerAddrTable(jsonString string, clusterAndBrokerNamesMap map[string][]string) (map[string]BrokerData, error) { | ||
var brokerMap = make(map[string]BrokerData) | ||
for _, brokerNames := range clusterAndBrokerNamesMap { | ||
for _, brokerName := range brokerNames { | ||
var brokerInfo = gjson.Get(jsonString, brokerName).String() | ||
var brokerAddrsString = gjson.Get(brokerInfo, "brokerAddrs").String() // {0:"x.x.x.x:10911"} | ||
var brokerName1 = gjson.Get(brokerInfo, "brokerName").String() | ||
var cluster = gjson.Get(brokerInfo, "cluster").String() | ||
|
||
bd := BrokerData{ | ||
Cluster: cluster, | ||
BrokerName: brokerName1, | ||
BrokerAddresses: ParseBrokerAddrs(brokerAddrsString), | ||
} | ||
|
||
brokerMap[brokerName1] = bd | ||
} | ||
} | ||
|
||
return brokerMap, nil | ||
} | ||
|
||
// input like : "{0:"127.0.0.1:10911"}" to map directly,can't parse, so get one by one | ||
// result.key = broker id , result.value = broker address and port | ||
func ParseBrokerAddrs1(jsonString string) map[int64]string { | ||
var resultMap = make(map[int64]string) | ||
var broker0Addr = gjson.Get(jsonString, "0") | ||
if broker0Addr.Exists() { | ||
resultMap[0] = broker0Addr.String() | ||
} | ||
var broker1Addr = gjson.Get(jsonString, "1") | ||
if broker1Addr.Exists() { | ||
resultMap[1] = broker1Addr.String() | ||
} | ||
return resultMap | ||
} | ||
|
||
// input like : "{0:"127.0.0.1:10911"}" to map directly,can't parse, so get one by one | ||
// result.key = broker id , result.value = broker address and port | ||
func ParseBrokerAddrs(jsonString string) map[int64]string { | ||
// for yaml parse, do replace | ||
jsonString = strings.ReplaceAll(jsonString, "0:", "0: ") | ||
jsonString = strings.ReplaceAll(jsonString, "1:", "1: ") | ||
|
||
var yamlmap map[int64]string | ||
err := yaml.Unmarshal([]byte(jsonString), &yamlmap) | ||
if err != nil { | ||
rlog.Error("parse addr error "+jsonString, nil) | ||
return nil | ||
} | ||
|
||
return yamlmap | ||
} | ||
|
||
// input like : {"DefaultCluster":["broker-a"]} | ||
// result.key = cluster name, result.value = broker names | ||
func ParseClusterAddrTable(jsonString string) (map[string][]string, error) { | ||
var result = make(map[string][]string) | ||
err := json.Unmarshal([]byte(jsonString), &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v2" | ||
"testing" | ||
) | ||
|
||
func TestParseBrokerAddrs(t *testing.T) { | ||
str := `{0: "127.0.0.1:1111",1: "127.0.0.1:1112"}` | ||
blob := []byte(str) | ||
var yamlmap map[int]string | ||
yaml.Unmarshal(blob, &yamlmap) | ||
for k, v := range yamlmap { | ||
fmt.Println(fmt.Sprintf("k=%d, v=%s", k, v)) | ||
} | ||
|
||
} | ||
|
||
func TestJsonToMap(t *testing.T) { | ||
var jsonString = []byte(`{"brokerAddrTable":{"broker-a":{"brokerAddrs":{0:"127.0.0.1:10911"},"brokerName":"broker-a","cluster":"DefaultCluster"}},"clusterAddrTable":{"DefaultCluster":["broker-a"]}}`) | ||
cluster, err := ParseClusterInfo(string(jsonString)) | ||
|
||
var cl = &ClusterInfo{} | ||
yaml.Unmarshal(jsonString, cl) | ||
|
||
assert.True(t, err == nil) | ||
fmt.Println(cluster) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package internal | ||
|
||
// topic list struct | ||
type TopicList struct { | ||
// topic names | ||
TopicNameList []string `json:"topicList"` | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not support in go 1.12