Skip to content

Commit 88b357c

Browse files
committed
allow only one ongoing SetIntentRPC per datastore
1 parent 1680121 commit 88b357c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

pkg/datastore/datastore_rpc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"strings"
8+
"sync"
89
"time"
910

1011
"github.com/iptecharch/cache/proto/cachepb"
@@ -44,6 +45,12 @@ type Datastore struct {
4445

4546
// stop cancel func
4647
cfn context.CancelFunc
48+
49+
// intent semaphore.
50+
// Used by SetIntent to guarantee that
51+
// only one SetIntent
52+
// is applied at a time.
53+
intentMutex *sync.Mutex
4754
}
4855

4956
// New creates a new datastore, its schema server client and initializes the SBI target
@@ -53,6 +60,7 @@ func New(ctx context.Context, c *config.DatastoreConfig, scc schema.Client, cc c
5360
config: c,
5461
schemaClient: scc,
5562
cacheClient: cc,
63+
intentMutex: new(sync.Mutex),
5664
}
5765
if c.Sync != nil {
5866
ds.synCh = make(chan *target.SyncUpdate, c.Sync.Buffer)

pkg/datastore/intent_rpc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/iptecharch/cache/proto/cachepb"
1313
sdcpb "github.com/iptecharch/sdc-protos/sdcpb"
1414
log "github.com/sirupsen/logrus"
15+
"google.golang.org/grpc/codes"
16+
"google.golang.org/grpc/status"
1517
"google.golang.org/protobuf/encoding/prototext"
1618
"google.golang.org/protobuf/proto"
1719

@@ -45,6 +47,11 @@ func (d *Datastore) GetIntent(ctx context.Context, req *sdcpb.GetIntentRequest)
4547
}
4648

4749
func (d *Datastore) SetIntent(ctx context.Context, req *sdcpb.SetIntentRequest) (*sdcpb.SetIntentResponse, error) {
50+
if !d.intentMutex.TryLock() {
51+
return nil, status.Errorf(codes.ResourceExhausted, "datastore %s has an ongoing SetIntentRequest", d.Name())
52+
}
53+
defer d.intentMutex.Unlock()
54+
4855
log.Infof("received SetIntentRequest: ds=%s intent=%s", req.GetName(), req.GetIntent())
4956
now := time.Now().UnixNano()
5057
candidateName := fmt.Sprintf("%s-%d", req.GetIntent(), now)

0 commit comments

Comments
 (0)