Skip to content

Commit

Permalink
Use a constants dictionary
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed Nov 25, 2024
1 parent 777867a commit cfef0be
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/tikv/pd/client/caller"
"github.com/tikv/pd/client/clients/metastorage"
"github.com/tikv/pd/client/constants"
"github.com/tikv/pd/client/errs"
"github.com/tikv/pd/client/metrics"
"github.com/tikv/pd/client/opt"
sd "github.com/tikv/pd/client/servicediscovery"
"github.com/tikv/pd/client/utils"
"github.com/tikv/pd/client/utils/tlsutil"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -263,7 +263,7 @@ func NewClientWithContext(
security SecurityOption, opts ...opt.ClientOption,
) (Client, error) {
return createClientWithKeyspace(ctx, callerComponent,
utils.NullKeyspaceID, svrAddrs, security, opts...)
constants.NullKeyspaceID, svrAddrs, security, opts...)
}

// NewClientWithKeyspace creates a client with context and the specified keyspace id.
Expand All @@ -274,9 +274,9 @@ func NewClientWithKeyspace(
keyspaceID uint32, svrAddrs []string,
security SecurityOption, opts ...opt.ClientOption,
) (Client, error) {
if keyspaceID < utils.DefaultKeyspaceID || keyspaceID > utils.MaxKeyspaceID {
if keyspaceID < constants.DefaultKeyspaceID || keyspaceID > constants.MaxKeyspaceID {
return nil, errors.Errorf("invalid keyspace id %d. It must be in the range of [%d, %d]",
keyspaceID, utils.DefaultKeyspaceID, utils.MaxKeyspaceID)
keyspaceID, constants.DefaultKeyspaceID, constants.MaxKeyspaceID)

Check warning on line 279 in client/client.go

View check run for this annotation

Codecov / codecov/patch

client/client.go#L279

Added line #L279 was not covered by tests
}
return createClientWithKeyspace(ctx, callerComponent, keyspaceID,
svrAddrs, security, opts...)
Expand Down Expand Up @@ -366,7 +366,7 @@ type apiContextV2 struct {
// NewAPIContextV2 creates a API context with the specified keyspace name for V2.
func NewAPIContextV2(keyspaceName string) APIContext {
if len(keyspaceName) == 0 {
keyspaceName = utils.DefaultKeyspaceName
keyspaceName = constants.DefaultKeyspaceName
}
return &apiContextV2{keyspaceName: keyspaceName}
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func newClientWithKeyspaceName(
inner: &innerClient{
// Create a PD service discovery with null keyspace id, then query the real id with the keyspace name,
// finally update the keyspace id to the PD service discovery for the following interactions.
keyspaceID: utils.NullKeyspaceID,
keyspaceID: constants.NullKeyspaceID,
updateTokenConnectionCh: make(chan struct{}, 1),
ctx: clientCtx,
cancel: clientCancel,
Expand Down
2 changes: 1 addition & 1 deletion client/utils/mics.go → client/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package utils
package constants

const (
// DefaultKeyspaceID is the default keyspace ID.
Expand Down
8 changes: 4 additions & 4 deletions client/servicediscovery/pd_service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/tikv/pd/client/constants"
"github.com/tikv/pd/client/errs"
"github.com/tikv/pd/client/opt"
"github.com/tikv/pd/client/retry"
"github.com/tikv/pd/client/utils"
"github.com/tikv/pd/client/utils/grpcutil"
"github.com/tikv/pd/client/utils/tlsutil"
"go.uber.org/zap"
Expand Down Expand Up @@ -454,7 +454,7 @@ func NewDefaultPDServiceDiscovery(
urls []string, tlsCfg *tls.Config,
) ServiceDiscovery {
var wg sync.WaitGroup
return NewPDServiceDiscovery(ctx, cancel, &wg, nil, nil, utils.DefaultKeyspaceID, urls, tlsCfg, opt.NewOption())
return NewPDServiceDiscovery(ctx, cancel, &wg, nil, nil, constants.DefaultKeyspaceID, urls, tlsCfg, opt.NewOption())
}

// NewPDServiceDiscovery returns a new PD service discovery-based client.
Expand Down Expand Up @@ -501,7 +501,7 @@ func (c *pdServiceDiscovery) Init() error {

// We need to update the keyspace ID before we discover and update the service mode
// so that TSO in API mode can be initialized with the correct keyspace ID.
if c.keyspaceID == utils.NullKeyspaceID && c.updateKeyspaceIDFunc != nil {
if c.keyspaceID == constants.NullKeyspaceID && c.updateKeyspaceIDFunc != nil {
if err := c.initRetry(c.updateKeyspaceIDFunc); err != nil {
return err
}
Expand Down Expand Up @@ -668,7 +668,7 @@ func (c *pdServiceDiscovery) SetKeyspaceID(keyspaceID uint32) {
// GetKeyspaceGroupID returns the ID of the keyspace group
func (*pdServiceDiscovery) GetKeyspaceGroupID() uint32 {
// PD/API service only supports the default keyspace group
return utils.DefaultKeyspaceGroupID
return constants.DefaultKeyspaceGroupID
}

// DiscoverMicroservice discovers the microservice with the specified type and returns the server urls.
Expand Down
10 changes: 5 additions & 5 deletions client/servicediscovery/tso_service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"github.com/pingcap/kvproto/pkg/tsopb"
"github.com/pingcap/log"
"github.com/tikv/pd/client/clients/metastorage"
"github.com/tikv/pd/client/constants"
"github.com/tikv/pd/client/errs"
"github.com/tikv/pd/client/opt"
"github.com/tikv/pd/client/utils"
"github.com/tikv/pd/client/utils/grpcutil"
"go.uber.org/zap"
"google.golang.org/grpc"
Expand Down Expand Up @@ -182,7 +182,7 @@ func NewTSOServiceDiscovery(
c.tsoServerDiscovery = &tsoServerDiscovery{urls: make([]string, 0)}
// Start with the default keyspace group. The actual keyspace group, to which the keyspace belongs,
// will be discovered later.
c.defaultDiscoveryKey = fmt.Sprintf(tsoSvcDiscoveryFormat, c.clusterID, utils.DefaultKeyspaceGroupID)
c.defaultDiscoveryKey = fmt.Sprintf(tsoSvcDiscoveryFormat, c.clusterID, constants.DefaultKeyspaceGroupID)

log.Info("created tso service discovery",
zap.Uint64("cluster-id", c.clusterID),
Expand Down Expand Up @@ -293,7 +293,7 @@ func (c *tsoServiceDiscovery) GetKeyspaceGroupID() uint32 {
c.keyspaceGroupSD.RLock()
defer c.keyspaceGroupSD.RUnlock()
if c.keyspaceGroupSD.group == nil {
return utils.DefaultKeyspaceGroupID
return constants.DefaultKeyspaceGroupID
}
return c.keyspaceGroupSD.group.Id
}
Expand Down Expand Up @@ -469,7 +469,7 @@ func (c *tsoServiceDiscovery) updateMember() error {
}
members[0].IsPrimary = true
keyspaceGroup = &tsopb.KeyspaceGroup{
Id: utils.DefaultKeyspaceGroupID,
Id: constants.DefaultKeyspaceGroupID,
Members: members,
}
}
Expand Down Expand Up @@ -554,7 +554,7 @@ func (c *tsoServiceDiscovery) findGroupByKeyspaceID(
Header: &tsopb.RequestHeader{
ClusterId: c.clusterID,
KeyspaceId: keyspaceID,
KeyspaceGroupId: utils.DefaultKeyspaceGroupID,
KeyspaceGroupId: constants.DefaultKeyspaceGroupID,
},
KeyspaceId: keyspaceID,
})
Expand Down
4 changes: 2 additions & 2 deletions client/tso_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (
"github.com/pingcap/kvproto/pkg/tsopb"
"github.com/pingcap/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/tikv/pd/client/constants"
"github.com/tikv/pd/client/errs"
"github.com/tikv/pd/client/metrics"
"github.com/tikv/pd/client/utils"
"go.uber.org/zap"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -145,7 +145,7 @@ func (s pdTSOStreamAdapter) Recv() (tsoRequestResult, error) {
physical: resp.GetTimestamp().GetPhysical(),
logical: resp.GetTimestamp().GetLogical(),
count: resp.GetCount(),
respKeyspaceGroupID: utils.DefaultKeyspaceGroupID,
respKeyspaceGroupID: constants.DefaultKeyspaceGroupID,
}, nil
}

Expand Down

0 comments on commit cfef0be

Please sign in to comment.