Skip to content

Commit

Permalink
revert #4334
Browse files Browse the repository at this point in the history
Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>
  • Loading branch information
CabinfeverB committed Dec 21, 2021
1 parent b1da421 commit ca3e099
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 117 deletions.
63 changes: 7 additions & 56 deletions server/api/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,62 +36,10 @@ import (
"github.com/unrolled/render"
)

// MetaStore contains meta information about a store which needed to show.
// MetaStore contains meta information about a store.
type MetaStore struct {
Id uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Address string `protobuf:"bytes,2,opt,name=address,proto3" json:"address,omitempty"`
Labels []*metapb.StoreLabel `protobuf:"bytes,4,rep,name=labels" json:"labels,omitempty"`
Version string `protobuf:"bytes,5,opt,name=version,proto3" json:"version,omitempty"`
PeerAddress string `protobuf:"bytes,6,opt,name=peer_address,json=peerAddress,proto3" json:"peer_address,omitempty"`
StatusAddress string `protobuf:"bytes,7,opt,name=status_address,json=statusAddress,proto3" json:"status_address,omitempty"`
GitHash string `protobuf:"bytes,8,opt,name=git_hash,json=gitHash,proto3" json:"git_hash,omitempty"`
StartTimestamp int64 `protobuf:"varint,9,opt,name=start_timestamp,json=startTimestamp,proto3" json:"start_timestamp,omitempty"`
DeployPath string `protobuf:"bytes,10,opt,name=deploy_path,json=deployPath,proto3" json:"deploy_path,omitempty"`
LastHeartbeat int64 `protobuf:"varint,11,opt,name=last_heartbeat,json=lastHeartbeat,proto3" json:"last_heartbeat,omitempty"`
PhysicallyDestroyed bool `protobuf:"varint,12,opt,name=physically_destroyed,json=physicallyDestroyed,proto3" json:"physically_destroyed,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
StateName string `json:"state_name"`
}

func NewMetaStore(store *metapb.Store, StateName string) *MetaStore {
metaStore := &MetaStore{StateName: StateName}
metaStore.Id = store.GetId()
metaStore.Address = store.GetAddress()
metaStore.Labels = store.GetLabels()
metaStore.Version = store.GetVersion()
metaStore.PeerAddress = store.GetPeerAddress()
metaStore.StatusAddress = store.GetStatusAddress()
metaStore.GitHash = store.GetGitHash()
metaStore.StartTimestamp = store.GetStartTimestamp()
metaStore.DeployPath = store.GetDeployPath()
metaStore.LastHeartbeat = store.GetLastHeartbeat()
metaStore.PhysicallyDestroyed = store.GetPhysicallyDestroyed()
metaStore.XXX_NoUnkeyedLiteral = store.XXX_NoUnkeyedLiteral
metaStore.XXX_unrecognized = store.XXX_unrecognized
metaStore.XXX_sizecache = store.XXX_sizecache
return metaStore
}

func (m *MetaStore) ConvertToMetapbStore() *metapb.Store {
metapbStore := &metapb.Store{
Id: m.Id,
Address: m.Address,
State: metapb.StoreState(metapb.StoreState_value[m.StateName]),
Labels: m.Labels,
Version: m.Version,
PeerAddress: m.PeerAddress,
StatusAddress: m.StatusAddress,
GitHash: m.GitHash,
StartTimestamp: m.StartTimestamp,
DeployPath: m.DeployPath,
LastHeartbeat: m.LastHeartbeat,
XXX_NoUnkeyedLiteral: m.XXX_NoUnkeyedLiteral,
XXX_unrecognized: m.XXX_unrecognized,
XXX_sizecache: m.XXX_sizecache,
}
return metapbStore
*metapb.Store
StateName string `json:"state_name"`
}

// StoreStatus contains status about a store.
Expand Down Expand Up @@ -129,7 +77,10 @@ const (

func newStoreInfo(opt *config.ScheduleConfig, store *core.StoreInfo) *StoreInfo {
s := &StoreInfo{
Store: NewMetaStore(store.GetMeta(), store.GetState().String()),
Store: &MetaStore{
Store: store.GetMeta(),
StateName: store.GetState().String(),
},
Status: &StoreStatus{
Capacity: typeutil.ByteSize(store.GetCapacity()),
Available: typeutil.ByteSize(store.GetAvailable()),
Expand Down
62 changes: 8 additions & 54 deletions server/api/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"strings"
"time"

"github.com/tikv/pd/server/versioninfo"

"github.com/docker/go-units"
"github.com/gogo/protobuf/proto"
. "github.com/pingcap/check"
Expand Down Expand Up @@ -116,8 +114,7 @@ func checkStoresInfo(c *C, ss []*StoreInfo, want []*metapb.Store) {
}
}
for _, s := range ss {
metapbStore := s.Store.ConvertToMetapbStore()
obtained := proto.Clone(metapbStore).(*metapb.Store)
obtained := proto.Clone(s.Store.Store).(*metapb.Store)
expected := proto.Clone(mapWant[obtained.Id]).(*metapb.Store)
// Ignore lastHeartbeat
obtained.LastHeartbeat, expected.LastHeartbeat = 0, 0
Expand Down Expand Up @@ -169,49 +166,6 @@ func (s *testStoreSuite) TestStoreGet(c *C) {
checkStoresInfo(c, []*StoreInfo{info}, s.stores[:1])
}

func (s *testStoreSuite) TestStoreInfoGet(c *C) {
timeStamp := time.Now().Unix()
url := fmt.Sprintf("%s/store/1112", s.urlPrefix)
_, errPut := s.grpcSvr.PutStore(context.Background(), &pdpb.PutStoreRequest{
Header: &pdpb.RequestHeader{ClusterId: s.svr.ClusterID()},
Store: &metapb.Store{
Id: 1112,
Address: fmt.Sprintf("tikv%d", 1112),
State: 1,
Labels: nil,
Version: versioninfo.MinSupportedVersion(versioninfo.Version5_0).String(),
StatusAddress: fmt.Sprintf("tikv%d", 1112),
GitHash: "45ce5b9584d618bc777877bea77cb94f61b8410",
StartTimestamp: timeStamp,
DeployPath: "/home/cabinfever/test",
LastHeartbeat: timeStamp,
},
})
c.Assert(errPut, IsNil)

info := new(StoreInfo)

err := readJSON(testDialClient, url, info)
c.Assert(err, IsNil)
c.Assert(info.Store.StateName, Equals, metapb.StoreState_Offline.String())
c.Assert(info.Store.Id, Equals, uint64(1112))
c.Assert(info.Store.Address, Equals, "tikv1112")
c.Assert(info.Store.Version, Equals, versioninfo.MinSupportedVersion(versioninfo.Version5_0).String())
c.Assert(info.Store.StatusAddress, Equals, fmt.Sprintf("tikv%d", 1112))
c.Assert(info.Store.GitHash, Equals, "45ce5b9584d618bc777877bea77cb94f61b8410")
c.Assert(info.Store.StartTimestamp, Equals, timeStamp)
c.Assert(info.Store.DeployPath, Equals, "/home/cabinfever/test")
c.Assert(info.Store.LastHeartbeat, Equals, timeStamp)

resp, err := testDialClient.Get(url)
c.Assert(err, IsNil)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
c.Assert(err, IsNil)
str := string(b)
c.Assert(strings.Contains(str, "\"state\""), Equals, false)
}

func (s *testStoreSuite) TestStoreLabel(c *C) {
url := fmt.Sprintf("%s/store/1", s.urlPrefix)
var info StoreInfo
Expand Down Expand Up @@ -308,7 +262,7 @@ func (s *testStoreSuite) TestStoreDelete(c *C) {
err := readJSON(testDialClient, url, store)
c.Assert(err, IsNil)
c.Assert(store.Store.PhysicallyDestroyed, IsFalse)
c.Assert(store.Store.StateName, Equals, metapb.StoreState_Offline.String())
c.Assert(store.Store.State, Equals, metapb.StoreState_Offline)

// up store success because it is offline but not physically destroyed
status := requestStatusBody(c, testDialClient, http.MethodPost, fmt.Sprintf("%s/state?state=Up", url))
Expand All @@ -319,15 +273,15 @@ func (s *testStoreSuite) TestStoreDelete(c *C) {
store = new(StoreInfo)
err = readJSON(testDialClient, url, store)
c.Assert(err, IsNil)
c.Assert(store.Store.StateName, Equals, metapb.StoreState_Up.String())
c.Assert(store.Store.State, Equals, metapb.StoreState_Up)
c.Assert(store.Store.PhysicallyDestroyed, IsFalse)

// offline store with physically destroyed
status = requestStatusBody(c, testDialClient, http.MethodDelete, fmt.Sprintf("%s?force=true", url))
c.Assert(status, Equals, http.StatusOK)
err = readJSON(testDialClient, url, store)
c.Assert(err, IsNil)
c.Assert(store.Store.StateName, Equals, metapb.StoreState_Offline.String())
c.Assert(store.Store.State, Equals, metapb.StoreState_Offline)
c.Assert(store.Store.PhysicallyDestroyed, IsTrue)

// try to up store again failed because it is physically destroyed
Expand All @@ -343,15 +297,15 @@ func (s *testStoreSuite) TestStoreSetState(c *C) {
info := StoreInfo{}
err := readJSON(testDialClient, url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.StateName, Equals, metapb.StoreState_Up.String())
c.Assert(info.Store.State, Equals, metapb.StoreState_Up)

// Set to Offline.
info = StoreInfo{}
err = postJSON(testDialClient, url+"/state?state=Offline", nil)
c.Assert(err, IsNil)
err = readJSON(testDialClient, url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.StateName, Equals, metapb.StoreState_Offline.String())
c.Assert(info.Store.State, Equals, metapb.StoreState_Offline)

// store not found
info = StoreInfo{}
Expand All @@ -366,7 +320,7 @@ func (s *testStoreSuite) TestStoreSetState(c *C) {
c.Assert(err, NotNil)
err = readJSON(testDialClient, url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.StateName, Equals, metapb.StoreState_Offline.String())
c.Assert(info.Store.State, Equals, metapb.StoreState_Offline)
}

// Set back to Up.
Expand All @@ -375,7 +329,7 @@ func (s *testStoreSuite) TestStoreSetState(c *C) {
c.Assert(err, IsNil)
err = readJSON(testDialClient, url, &info)
c.Assert(err, IsNil)
c.Assert(info.Store.StateName, Equals, metapb.StoreState_Up.String())
c.Assert(info.Store.State, Equals, metapb.StoreState_Up)
}

func (s *testStoreSuite) TestUrlStoreFilter(c *C) {
Expand Down
4 changes: 2 additions & 2 deletions server/api/trend.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func (h *trendHandler) getTrendStores() ([]trendStore, error) {
for _, store := range stores {
info := newStoreInfo(h.svr.GetScheduleConfig(), store)
s := trendStore{
ID: info.Store.Id,
Address: info.Store.Address,
ID: info.Store.GetId(),
Address: info.Store.GetAddress(),
StateName: info.Store.StateName,
Capacity: uint64(info.Status.Capacity),
Available: uint64(info.Status.Available),
Expand Down
3 changes: 1 addition & 2 deletions tests/pdctl/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func CheckStoresInfo(c *check.C, stores []*api.StoreInfo, want []*metapb.Store)
}
}
for _, s := range stores {
metapbStore := s.Store.ConvertToMetapbStore()
obtained := proto.Clone(metapbStore).(*metapb.Store)
obtained := proto.Clone(s.Store.Store).(*metapb.Store)
expected := proto.Clone(mapWant[obtained.Id]).(*metapb.Store)
// Ignore lastHeartbeat
obtained.LastHeartbeat, expected.LastHeartbeat = 0, 0
Expand Down
6 changes: 3 additions & 3 deletions tests/pdctl/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,15 @@ func (s *storeTestSuite) TestStore(c *C) {
c.Assert(ok, IsFalse)

// store delete <store_id> command
c.Assert(storeInfo.Store.StateName, Equals, metapb.StoreState_Up.String())
c.Assert(storeInfo.Store.State, Equals, metapb.StoreState_Up)
args = []string{"-u", pdAddr, "store", "delete", "1"}
_, err = pdctl.ExecuteCommand(cmd, args...)
c.Assert(err, IsNil)
args = []string{"-u", pdAddr, "store", "1"}
output, err = pdctl.ExecuteCommand(cmd, args...)
c.Assert(err, IsNil)
c.Assert(json.Unmarshal(output, &storeInfo), IsNil)
c.Assert(storeInfo.Store.StateName, Equals, metapb.StoreState_Offline.String())
c.Assert(storeInfo.Store.State, Equals, metapb.StoreState_Offline)

// store check status
args = []string{"-u", pdAddr, "store", "check", "Offline"}
Expand Down Expand Up @@ -285,7 +285,7 @@ func (s *storeTestSuite) TestStore(c *C) {
output, err = pdctl.ExecuteCommand(cmd, args...)
c.Assert(err, IsNil)
c.Assert(json.Unmarshal(output, &storeInfo), IsNil)
c.Assert(storeInfo.Store.StateName, Equals, metapb.StoreState_Offline.String())
c.Assert(storeInfo.Store.State, Equals, metapb.StoreState_Offline)

// store remove-tombstone
args = []string{"-u", pdAddr, "store", "remove-tombstone"}
Expand Down

0 comments on commit ca3e099

Please sign in to comment.