Skip to content

Commit

Permalink
refactor: propose id
Browse files Browse the repository at this point in the history
Signed-off-by: LingKa <cnfty786@gmail.com>
  • Loading branch information
LingKa28 committed Jan 11, 2024
1 parent b1a87f6 commit c1fb0e4
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 135 deletions.
8 changes: 2 additions & 6 deletions client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"errors"
"fmt"

"github.com/xline-kv/go-xline/api/xline"
"github.com/xline-kv/go-xline/api/gen/xline"
"golang.org/x/crypto/pbkdf2"
)

Expand Down Expand Up @@ -488,11 +488,7 @@ func (c *authClient) RoleRevokePermission(role string, key, rangeEnd []byte) (*A

// Send request using fast path
func (c *authClient) handleReq(req *xlineapi.RequestWithToken, useFastPath bool) (*proposeRes, error) {
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Request: req, ProposeId: pid}
cmd := xlineapi.Command{Request: req}

if useFastPath {
res, err := c.curpClient.Propose(&cmd, true)
Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"math/rand"

"github.com/xline-kv/go-xline/api/xline"
"github.com/xline-kv/go-xline/api/gen/xline"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
Expand Down
2 changes: 1 addition & 1 deletion client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package client
import (
"context"

"github.com/xline-kv/go-xline/api/xline"
"github.com/xline-kv/go-xline/api/gen/xline"
"google.golang.org/grpc"
)

Expand Down
2 changes: 1 addition & 1 deletion client/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package client

import "github.com/xline-kv/go-xline/api/xline"
import "github.com/xline-kv/go-xline/api/gen/xline"

type CompareTarget int
type CompareResult int
Expand Down
2 changes: 1 addition & 1 deletion client/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

xlineapi "github.com/xline-kv/go-xline/api/xline"
"github.com/xline-kv/go-xline/api/gen/xline"
)

type CommandError struct {
Expand Down
32 changes: 10 additions & 22 deletions client/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package client

import "github.com/xline-kv/go-xline/api/xline"
import "github.com/xline-kv/go-xline/api/gen/xline"

type KV interface {
// Put puts a key-value pair into xline.
Expand Down Expand Up @@ -77,19 +77,16 @@ func NewKV(curpClient Curp, token string) KV {
// Put a key-value into the store
func (c *kvClient) Put(key, val []byte, opts ...OpOption) (*PutResponse, error) {
op := OpPut(key, val, opts...)
// TODO: buildOpFromOp
krs := []*xlineapi.KeyRange{op.toKeyRange()}
req := xlineapi.RequestWithToken{
Token: &c.token,
RequestWrapper: &xlineapi.RequestWithToken_PutRequest{
PutRequest: op.toPutReq(),
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Keys: krs, Request: &req, ProposeId: pid}
res, err := c.curpClient.Propose(&cmd, true)
cmd := &xlineapi.Command{Keys: krs, Request: &req}
res, err := c.curpClient.Propose(cmd, true)
if err != nil {
return nil, err
}
Expand All @@ -99,18 +96,15 @@ func (c *kvClient) Put(key, val []byte, opts ...OpOption) (*PutResponse, error)
// Range a range of keys from the store
func (c *kvClient) Range(key []byte, opt ...OpOption) (*RangeResponse, error) {
op := OpRange(key, opt...)
// TODO: buildOpFromOp
krs := []*xlineapi.KeyRange{op.toKeyRange()}
req := xlineapi.RequestWithToken{
Token: &c.token,
RequestWrapper: &xlineapi.RequestWithToken_RangeRequest{
RangeRequest: op.toRangeReq(),
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Keys: krs, Request: &req, ProposeId: pid}
cmd := xlineapi.Command{Keys: krs, Request: &req}
res, err := c.curpClient.Propose(&cmd, true)
if err != nil {
return nil, err
Expand All @@ -121,18 +115,15 @@ func (c *kvClient) Range(key []byte, opt ...OpOption) (*RangeResponse, error) {
// Delete a range of keys from the store
func (c *kvClient) Delete(key string, opts ...OpOption) (*DeleteResponse, error) {
op := OpDelete(key, opts...)
// TODO: buildOpFromOp
krs := []*xlineapi.KeyRange{op.toKeyRange()}
req := xlineapi.RequestWithToken{
Token: &c.token,
RequestWrapper: &xlineapi.RequestWithToken_DeleteRangeRequest{
DeleteRangeRequest: op.toDeleteReq(),
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Keys: krs, Request: &req, ProposeId: pid}
cmd := xlineapi.Command{Keys: krs, Request: &req}
res, err := c.curpClient.Propose(&cmd, false)
if err != nil {
return nil, err
Expand All @@ -152,17 +143,14 @@ func (c *kvClient) Txn() Txn {
func (c *kvClient) Compact(rev int64, opts ...OpOption) (*CompactResponse, error) {
r := OpCompact(rev, opts...).toCompactReq()
useFastPath := r.Physical
// TODO: buildOpFromOp
req := xlineapi.RequestWithToken{
Token: &c.token,
RequestWrapper: &xlineapi.RequestWithToken_CompactionRequest{
CompactionRequest: r,
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Request: &req, ProposeId: pid}
cmd := xlineapi.Command{Request: &req}
res, err := c.curpClient.Propose(&cmd, useFastPath)
if err != nil {
return nil, err
Expand Down
20 changes: 4 additions & 16 deletions client/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"time"

"github.com/xline-kv/go-xline/api/xline"
"github.com/xline-kv/go-xline/api/gen/xline"
"github.com/xline-kv/go-xline/xlog"
"go.uber.org/zap"
"google.golang.org/grpc"
Expand Down Expand Up @@ -101,11 +101,7 @@ func (c *leaseClient) Grant(ttl int64, opts ...LeaseOption) (*LeaseGrantResponse
LeaseGrantRequest: request,
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Request: &requestWithToken, ProposeId: pid}
cmd := xlineapi.Command{Request: &requestWithToken}

res, err := c.curpClient.Propose(&cmd, true)
if err != nil {
Expand All @@ -122,11 +118,7 @@ func (c *leaseClient) Revoke(id int64) (*LeaseRevokeResponse, error) {
LeaseRevokeRequest: &xlineapi.LeaseRevokeRequest{ID: id},
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Request: &requestWithToken, ProposeId: pid}
cmd := xlineapi.Command{Request: &requestWithToken}

res, err := c.curpClient.Propose(&cmd, true)
if err != nil {
Expand Down Expand Up @@ -213,11 +205,7 @@ func (c *leaseClient) Leases() (*LeaseLeasesResponse, error) {
LeaseLeasesRequest: &xlineapi.LeaseLeasesRequest{},
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Request: &requestWithToken, ProposeId: pid}
cmd := xlineapi.Command{Request: &requestWithToken}

res, err := c.curpClient.Propose(&cmd, true)
if err != nil {
Expand Down
27 changes: 5 additions & 22 deletions client/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"math"

"github.com/xline-kv/go-xline/api/xline"
"github.com/xline-kv/go-xline/api/gen/xline"
"github.com/xline-kv/go-xline/xlog"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -69,13 +69,8 @@ func (c *lockClient) lockInner(
TxnRequest: &txn,
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{
Request: &requestWithToken,
ProposeId: pid,
Request: &requestWithToken,
}
res, err := c.curpClient.Propose(&cmd, false)
if err != nil {
Expand Down Expand Up @@ -108,11 +103,7 @@ func (c *lockClient) lockInner(
RangeRequest: &rangeReq,
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Request: &requestWithToken, ProposeId: pid}
cmd := xlineapi.Command{Request: &requestWithToken}

res, err := c.curpClient.Propose(&cmd, true)
if err == nil {
Expand Down Expand Up @@ -240,11 +231,7 @@ func (c *lockClient) waitDelete(pfx string, myRev int64) {
RangeRequest: &getReq,
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return
}
cmd := xlineapi.Command{Request: &requestWithToken, ProposeId: pid}
cmd := xlineapi.Command{Request: &requestWithToken}

res, err := c.curpClient.Propose(&cmd, false)
if err != nil {
Expand Down Expand Up @@ -297,11 +284,7 @@ func (c *lockClient) deleteKey(key []byte) (*xlineapi.ResponseHeader, error) {
DeleteRangeRequest: &delReq,
},
}
pid, err := c.curpClient.GenProposeID()
if err != nil {
return nil, err
}
cmd := xlineapi.Command{Request: &requestWithToken, ProposeId: pid}
cmd := xlineapi.Command{Request: &requestWithToken}

res, err := c.curpClient.Propose(&cmd, true)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion client/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client
import (
"context"

"github.com/xline-kv/go-xline/api/xline"
"github.com/xline-kv/go-xline/api/gen/xline"
"google.golang.org/grpc"
)

Expand Down
2 changes: 1 addition & 1 deletion client/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package client

import "github.com/xline-kv/go-xline/api/xline"
import "github.com/xline-kv/go-xline/api/gen/xline"

// Op represents an Operation that kv can execute.
type Op struct {
Expand Down
Loading

0 comments on commit c1fb0e4

Please sign in to comment.