Skip to content

Commit

Permalink
Merge branch 'add-block-cache' of https://github.com/weekface/tidb-op…
Browse files Browse the repository at this point in the history
…erator into add-block-cache
  • Loading branch information
weekface committed Dec 26, 2018
2 parents 1e34a20 + 4628b25 commit cc77d60
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 54 deletions.
2 changes: 1 addition & 1 deletion charts/tidb-backup/templates/scripts/_start_backup.sh.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set -euo pipefail
dirname=backup-`date +%Y-%m-%dT%H%M%S`-${MY_POD_NAME}
host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]'`
host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]'` | tr '-' '_'

mkdir -p /data/${dirname}/
cp /savepoint-dir/savepoint /data/${dirname}/
Expand Down
2 changes: 1 addition & 1 deletion charts/tidb-backup/templates/scripts/_start_restore.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -euo pipefail
dirname=restore-`date +%Y-%m-%dT%H%M%S`-${MY_POD_NAME}
dataDir=/data/${dirname}
mkdir -p ${dataDir}/
host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]'`
host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]'` | tr '-' '_'

{{- if .Values.restore.gcp }}
downloader \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set -euo pipefail
dirname=scheduled-backup-`date +%Y-%m-%dT%H%M%S`-${MY_POD_NAME}
host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]'`
host=`echo {{ .Values.clusterName }}_TIDB_SERVICE_HOST | tr '[a-z]' '[A-Z]'` | tr '-' '_'

mkdir -p /data/${dirname}/
cp /savepoint-dir/savepoint /data/${dirname}/
Expand Down
46 changes: 44 additions & 2 deletions pkg/controller/pd_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,20 @@ func (pc *pdClient) GetStore(storeID uint64) (*StoreInfo, error) {
}

func (pc *pdClient) DeleteStore(storeID uint64) error {
var exist bool
stores, err := pc.GetStores()
if err != nil {
return err
}
for _, store := range stores.Stores {
if store.Store.GetId() == storeID {
exist = true
break
}
}
if !exist {
return nil
}
apiURL := fmt.Sprintf("%s/%s/%d", pc.url, storePrefix, storeID)
req, err := http.NewRequest("DELETE", apiURL, nil)
if err != nil {
Expand All @@ -316,7 +330,7 @@ func (pc *pdClient) DeleteStore(storeID uint64) error {
defer DeferClose(res.Body, &err)

// Remove an offline store should returns http.StatusOK
if res.StatusCode == http.StatusOK {
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusNotFound {
return nil
}
body, err := ioutil.ReadAll(res.Body)
Expand All @@ -328,6 +342,20 @@ func (pc *pdClient) DeleteStore(storeID uint64) error {
}

func (pc *pdClient) DeleteMemberByID(memberID uint64) error {
var exist bool
members, err := pc.GetMembers()
if err != nil {
return err
}
for _, member := range members.Members {
if member.MemberId == memberID {
exist = true
break
}
}
if !exist {
return nil
}
apiURL := fmt.Sprintf("%s/%s/id/%d", pc.url, membersPrefix, memberID)
req, err := http.NewRequest("DELETE", apiURL, nil)
if err != nil {
Expand All @@ -338,14 +366,28 @@ func (pc *pdClient) DeleteMemberByID(memberID uint64) error {
return err
}
defer DeferClose(res.Body, &err)
if res.StatusCode == http.StatusOK {
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusNotFound {
return nil
}
err2 := readErrorBody(res.Body)
return fmt.Errorf("failed %v to delete member %d: %v", res.StatusCode, memberID, err2)
}

func (pc *pdClient) DeleteMember(name string) error {
var exist bool
members, err := pc.GetMembers()
if err != nil {
return err
}
for _, member := range members.Members {
if member.Name == name {
exist = true
break
}
}
if !exist {
return nil
}
apiURL := fmt.Sprintf("%s/%s/name/%s", pc.url, membersPrefix, name)
req, err := http.NewRequest("DELETE", apiURL, nil)
if err != nil {
Expand Down
221 changes: 183 additions & 38 deletions pkg/controller/pd_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,29 +359,78 @@ func TestSetStoreLabels(t *testing.T) {
func TestDeleteMember(t *testing.T) {
g := NewGomegaWithT(t)
name := "testMember"
member := &pdpb.Member{Name: name, MemberId: uint64(1)}
membersExist := &MembersInfo{
Members: []*pdpb.Member{
member,
},
Leader: member,
EtcdLeader: member,
}
membersExistBytes, err := json.Marshal(membersExist)
g.Expect(err).NotTo(HaveOccurred())

membersNotExist := &MembersInfo{
Members: []*pdpb.Member{},
}
membersNotExistBytes, err := json.Marshal(membersNotExist)
g.Expect(err).NotTo(HaveOccurred())

tcs := []struct {
caseName string
path string
method string
want bool
caseName string
prePath string
preMethod string
preResp []byte
exist bool
path string
method string
want bool
}{{
caseName: "success_DeleteMember",
path: fmt.Sprintf("/%s/name/%s", membersPrefix, name),
method: "DELETE",
want: true,
caseName: "success_DeleteMember",
prePath: fmt.Sprintf("/%s", membersPrefix),
preMethod: "GET",
preResp: membersExistBytes,
exist: true,
path: fmt.Sprintf("/%s/name/%s", membersPrefix, name),
method: "DELETE",
want: true,
}, {
caseName: "failed_DeleteMember",
path: fmt.Sprintf("/%s/name/%s", membersPrefix, name),
method: "DELETE",
want: false,
caseName: "failed_DeleteMember",
prePath: fmt.Sprintf("/%s", membersPrefix),
preMethod: "GET",
preResp: membersExistBytes,
exist: true,
path: fmt.Sprintf("/%s/name/%s", membersPrefix, name),
method: "DELETE",
want: false,
}, {
caseName: "delete_not_exist_member",
prePath: fmt.Sprintf("/%s", membersPrefix),
preMethod: "GET",
preResp: membersNotExistBytes,
exist: false,
path: fmt.Sprintf("/%s/name/%s", membersPrefix, name),
method: "DELETE",
want: true,
},
}

for _, tc := range tcs {
count := 1
svc := getClientServer(func(w http.ResponseWriter, request *http.Request) {
if count == 1 {
g.Expect(request.Method).To(Equal(tc.preMethod), "check method")
g.Expect(request.URL.Path).To(Equal(tc.prePath), "check url")
w.Header().Set("Content-Type", ContentTypeJSON)
w.WriteHeader(http.StatusOK)
w.Write(tc.preResp)
count++
return
}

g.Expect(tc.exist).To(BeTrue())
g.Expect(request.Method).To(Equal(tc.method), "check method")
g.Expect(request.URL.Path).To(Equal(tc.path), "check url")

w.Header().Set("Content-Type", ContentTypeJSON)
if tc.want {
w.WriteHeader(http.StatusOK)
Expand All @@ -404,29 +453,78 @@ func TestDeleteMember(t *testing.T) {
func TestDeleteMemberByID(t *testing.T) {
g := NewGomegaWithT(t)
id := uint64(1)
member := &pdpb.Member{Name: "test", MemberId: id}
membersExist := &MembersInfo{
Members: []*pdpb.Member{
member,
},
Leader: member,
EtcdLeader: member,
}
membersExistBytes, err := json.Marshal(membersExist)
g.Expect(err).NotTo(HaveOccurred())

membersNotExist := &MembersInfo{
Members: []*pdpb.Member{},
}
membersNotExistBytes, err := json.Marshal(membersNotExist)
g.Expect(err).NotTo(HaveOccurred())

tcs := []struct {
caseName string
path string
method string
want bool
caseName string
prePath string
preMethod string
preResp []byte
exist bool
path string
method string
want bool
}{{
caseName: "success_DeleteMemberByID",
path: fmt.Sprintf("/%s/id/%d", membersPrefix, id),
method: "DELETE",
want: true,
caseName: "success_DeleteMemberByID",
prePath: fmt.Sprintf("/%s", membersPrefix),
preMethod: "GET",
preResp: membersExistBytes,
exist: true,
path: fmt.Sprintf("/%s/id/%d", membersPrefix, id),
method: "DELETE",
want: true,
}, {
caseName: "failed_DeleteMemberByID",
path: fmt.Sprintf("/%s/id/%d", membersPrefix, id),
method: "DELETE",
want: false,
caseName: "failed_DeleteMemberByID",
prePath: fmt.Sprintf("/%s", membersPrefix),
preMethod: "GET",
preResp: membersExistBytes,
exist: true,
path: fmt.Sprintf("/%s/id/%d", membersPrefix, id),
method: "DELETE",
want: false,
}, {
caseName: "delete_not_exit_member",
prePath: fmt.Sprintf("/%s", membersPrefix),
preMethod: "GET",
preResp: membersNotExistBytes,
exist: false,
path: fmt.Sprintf("/%s/id/%d", membersPrefix, id),
method: "DELETE",
want: true,
},
}

for _, tc := range tcs {
count := 1
svc := getClientServer(func(w http.ResponseWriter, request *http.Request) {
if count == 1 {
g.Expect(request.Method).To(Equal(tc.preMethod), "check method")
g.Expect(request.URL.Path).To(Equal(tc.prePath), "check url")
w.Header().Set("Content-Type", ContentTypeJSON)
w.WriteHeader(http.StatusOK)
w.Write(tc.preResp)
count++
return
}

g.Expect(tc.exist).To(BeTrue())
g.Expect(request.Method).To(Equal(tc.method), "check method")
g.Expect(request.URL.Path).To(Equal(tc.path), "check url")

w.Header().Set("Content-Type", ContentTypeJSON)
if tc.want {
w.WriteHeader(http.StatusOK)
Expand All @@ -449,26 +547,73 @@ func TestDeleteMemberByID(t *testing.T) {
func TestDeleteStore(t *testing.T) {
g := NewGomegaWithT(t)
storeID := uint64(1)
store := &StoreInfo{
Store: &MetaStore{Store: &metapb.Store{Id: storeID, State: metapb.StoreState_Up}},
Status: &StoreStatus{},
}
stores := &StoresInfo{
Count: 1,
Stores: []*StoreInfo{
store,
},
}

storesBytes, err := json.Marshal(stores)
g.Expect(err).NotTo(HaveOccurred())

tcs := []struct {
caseName string
path string
method string
want bool
caseName string
prePath string
preMethod string
preResp []byte
exist bool
path string
method string
want bool
}{{
caseName: "success_DeleteStore",
path: fmt.Sprintf("/%s/%d", storePrefix, storeID),
method: "DELETE",
want: true,
caseName: "success_DeleteStore",
prePath: fmt.Sprintf("/%s", storesPrefix),
preMethod: "GET",
preResp: storesBytes,
exist: true,
path: fmt.Sprintf("/%s/%d", storePrefix, storeID),
method: "DELETE",
want: true,
}, {
caseName: "failed_DeleteStore",
path: fmt.Sprintf("/%s/%d", storePrefix, storeID),
method: "DELETE",
want: false,
caseName: "failed_DeleteStore",
prePath: fmt.Sprintf("/%s", storesPrefix),
preMethod: "GET",
preResp: storesBytes,
exist: true,
path: fmt.Sprintf("/%s/%d", storePrefix, storeID),
method: "DELETE",
want: false,
}, {
caseName: "delete_not_exist_store",
prePath: fmt.Sprintf("/%s", storesPrefix),
preMethod: "GET",
preResp: storesBytes,
exist: true,
path: fmt.Sprintf("/%s/%d", storePrefix, storeID),
method: "DELETE",
want: true,
},
}

for _, tc := range tcs {
count := 1
svc := getClientServer(func(w http.ResponseWriter, request *http.Request) {
if count == 1 {
g.Expect(request.Method).To(Equal(tc.preMethod), "check method")
g.Expect(request.URL.Path).To(Equal(tc.prePath), "check url")
w.Header().Set("Content-Type", ContentTypeJSON)
w.WriteHeader(http.StatusOK)
w.Write(tc.preResp)
count++
return
}

g.Expect(tc.exist).To(BeTrue())
g.Expect(request.Method).To(Equal(tc.method), "check method")
g.Expect(request.URL.Path).To(Equal(tc.path), "check url")

Expand Down
4 changes: 2 additions & 2 deletions pkg/manager/member/pd_failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ func (pf *pdFailover) tryToDeleteAFailureMember(tc *v1alpha1.TidbCluster) error
return nil
}

memberID, err := strconv.Atoi(failureMember.MemberID)
memberID, err := strconv.ParseUint(failureMember.MemberID, 10, 64)
if err != nil {
return err
}
// invoke deleteMember api to delete a member from the pd cluster
err = pf.pdControl.GetPDClient(tc).DeleteMemberByID(uint64(memberID))
err = pf.pdControl.GetPDClient(tc).DeleteMemberByID(memberID)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit cc77d60

Please sign in to comment.