-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
owner(ticdc): Add support for region-label to enable meta-region isol…
- Loading branch information
1 parent
b373dbb
commit 01dc212
Showing
11 changed files
with
271 additions
and
14 deletions.
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
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
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
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,116 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pdutil | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/log" | ||
"github.com/pingcap/tiflow/pkg/config" | ||
"github.com/pingcap/tiflow/pkg/httputil" | ||
"github.com/pingcap/tiflow/pkg/retry" | ||
pd "github.com/tikv/pd/client" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
regionLabelPrefix = "/pd/api/v1/config/region-label/rules" | ||
|
||
// Split the default rule by `6e000000000000000000f8` to keep metadata region | ||
// isolated from the normal data area. | ||
addMetaJSON = `{ | ||
"sets": [ | ||
{ | ||
"id": "ticdc/meta", | ||
"labels": [ | ||
{ | ||
"key": "data-type", | ||
"value": "meta" | ||
} | ||
], | ||
"rule_type": "key-range", | ||
"data": [ | ||
{ | ||
"start_key": "6d00000000000000f8", | ||
"end_key": "6e00000000000000f8" | ||
} | ||
] | ||
} | ||
] | ||
}` | ||
) | ||
|
||
var defaultMaxRetry uint64 = 3 | ||
|
||
// pdAPIClient is api client of Placement Driver. | ||
type pdAPIClient struct { | ||
pdClient pd.Client | ||
dialClient *httputil.Client | ||
} | ||
|
||
// newPDApiClient create a new pdAPIClient. | ||
func newPDApiClient(pdClient pd.Client) (*pdAPIClient, error) { | ||
conf := config.GetGlobalServerConfig() | ||
dialClient, err := httputil.NewClient(conf.Security) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
return &pdAPIClient{ | ||
pdClient: pdClient, | ||
dialClient: dialClient, | ||
}, nil | ||
} | ||
|
||
// UpdateMetaLabel is a reentrant function that updates the meta-region label of upstream cluster. | ||
func UpdateMetaLabel(ctx context.Context, pdClient pd.Client) error { | ||
pc, err := newPDApiClient(pdClient) | ||
if err != nil { | ||
return err | ||
} | ||
defer pc.dialClient.CloseIdleConnections() | ||
|
||
err = retry.Do(ctx, func() error { | ||
err = pc.patchMetaLabel(ctx) | ||
if err != nil { | ||
log.Error("Fail to add meta region label to PD", zap.Error(err)) | ||
return err | ||
} | ||
|
||
log.Info("Succeed to add meta region label to PD") | ||
return nil | ||
}, retry.WithMaxTries(defaultMaxRetry), retry.WithIsRetryableErr(func(err error) bool { | ||
switch errors.Cause(err) { | ||
case context.Canceled: | ||
return false | ||
} | ||
return true | ||
})) | ||
return err | ||
} | ||
|
||
func (pc *pdAPIClient) patchMetaLabel(ctx context.Context) error { | ||
url := pc.pdClient.GetLeaderAddr() + regionLabelPrefix | ||
header := http.Header{"Content-Type": {"application/json"}} | ||
content := []byte(addMetaJSON) | ||
|
||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
defer cancel() | ||
_, err := pc.dialClient.DoRequest(ctx, url, http.MethodPatch, | ||
header, bytes.NewReader(content)) | ||
return errors.Trace(err) | ||
} |
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,85 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pdutil | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
cerror "github.com/pingcap/tiflow/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
pd "github.com/tikv/pd/client" | ||
) | ||
|
||
type mockPDClient struct { | ||
pd.Client | ||
testServer *httptest.Server | ||
url string | ||
} | ||
|
||
func (m *mockPDClient) GetLeaderAddr() string { | ||
return m.url | ||
} | ||
|
||
func newMockPDClient(normal bool) *mockPDClient { | ||
mock := &mockPDClient{} | ||
status := http.StatusOK | ||
if !normal { | ||
status = http.StatusNotFound | ||
} | ||
mock.testServer = httptest.NewServer(http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(status) | ||
}, | ||
)) | ||
mock.url = mock.testServer.URL | ||
|
||
return mock | ||
} | ||
|
||
func TestMetaLabelNormal(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
mockClient := newMockPDClient(true) | ||
|
||
err := UpdateMetaLabel(ctx, mockClient) | ||
require.Nil(t, err) | ||
mockClient.testServer.Close() | ||
} | ||
|
||
func TestMetaLabelFail(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
mockClient := newMockPDClient(false) | ||
pc, err := newPDApiClient(mockClient) | ||
require.Nil(t, err) | ||
mockClient.url = "http://127.0.1.1:2345" | ||
|
||
// test url error | ||
err = pc.patchMetaLabel(ctx) | ||
require.NotNil(t, err) | ||
|
||
// test 404 | ||
mockClient.url = mockClient.testServer.URL | ||
err = pc.patchMetaLabel(ctx) | ||
require.Regexp(t, ".*404.*", err) | ||
|
||
err = UpdateMetaLabel(ctx, mockClient) | ||
require.ErrorIs(t, err, cerror.ErrReachMaxTry) | ||
mockClient.testServer.Close() | ||
} |
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
Oops, something went wrong.