Skip to content

Commit 1354cc3

Browse files
authored
Merge pull request #14 from mbilal92/main
FIX: concurrent map read and map write for `globalChunkTbl`, added mutex for globalChunkTbl read/write
2 parents 2a78831 + 2c2ef2a commit 1354cc3

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

drsm/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ func (d *Drsm) ReleaseInt32ID(id int32) error {
110110
}
111111

112112
func (d *Drsm) FindOwnerInt32ID(id int32) (*PodId, error) {
113-
mutex.Lock()
114-
defer mutex.Unlock()
113+
d.globalChunkTblMutex.Lock()
114+
defer d.globalChunkTblMutex.Unlock()
115115
chunkId := id >> 10
116116
chunk, found := d.globalChunkTbl[chunkId]
117117
if found == true {

drsm/chunk.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func (d *Drsm) GetNewChunk() (*chunk, error) {
3131
for {
3232
for {
3333
cn = rand.Int31n(d.chunkIdRange)
34+
d.globalChunkTblMutex.Lock()
3435
_, found := d.globalChunkTbl[cn]
36+
d.globalChunkTblMutex.Unlock()
3537
if found == true {
3638
continue
3739
}

drsm/claim.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ func (d *Drsm) podDownDetected() {
1919
// Given Pod find out current Chunks owned by this POD
2020
pd := d.podMap[p]
2121
for k, _ := range pd.podChunks {
22+
d.globalChunkTblMutex.Lock()
2223
c, found := d.globalChunkTbl[k]
24+
d.globalChunkTblMutex.Unlock()
2325
logger.AppLog.Debugf("Found : %v chunk : %v ", found, c)
2426
go c.claimChunk(d)
2527
}

drsm/drsm.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,23 @@ type podData struct {
4545
}
4646

4747
type Drsm struct {
48-
mu sync.Mutex
49-
sharedPoolName string
50-
clientId PodId
51-
db DbInfo
52-
mode DrsmMode
53-
resIdSize int32
54-
localChunkTbl map[int32]*chunk // chunkid to chunk
55-
globalChunkTbl map[int32]*chunk // chunkid to chunk
56-
podMap map[string]*podData // podId to podData
57-
podDown chan string
58-
scanChunks map[int32]*chunk
59-
chunkIdRange int32
60-
resourceValidCb func(int32) bool
61-
ipModule ipam.Ipamer
62-
prefix map[string]*ipam.Prefix
63-
mongo *MongoDBLibrary.MongoClient
48+
mu sync.Mutex
49+
sharedPoolName string
50+
clientId PodId
51+
db DbInfo
52+
mode DrsmMode
53+
resIdSize int32
54+
localChunkTbl map[int32]*chunk // chunkid to chunk
55+
globalChunkTbl map[int32]*chunk // chunkid to chunk
56+
podMap map[string]*podData // podId to podData
57+
podDown chan string
58+
scanChunks map[int32]*chunk
59+
chunkIdRange int32
60+
resourceValidCb func(int32) bool
61+
ipModule ipam.Ipamer
62+
prefix map[string]*ipam.Prefix
63+
mongo *MongoDBLibrary.MongoClient
64+
globalChunkTblMutex sync.Mutex
6465
}
6566

6667
func (d *Drsm) DeletePod(podInstance string) {
@@ -87,6 +88,7 @@ func (d *Drsm) ConstuctDrsm(opt *Options) {
8788
d.podMap = make(map[string]*podData)
8889
d.podDown = make(chan string, 10)
8990
d.scanChunks = make(map[int32]*chunk)
91+
d.globalChunkTblMutex = sync.Mutex{}
9092
t := time.Now().UnixNano()
9193
rand.Seed(t)
9294
d.initIpam(opt)

drsm/updates.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ func iterateChangeStream(d *Drsm, routineCtx context.Context, stream *mongo.Chan
157157
// looks like chunk owner getting change
158158
owner := s.Update.UpdFields.PodId
159159
c := getChunIdFromDocId(s.DId.Id)
160+
d.globalChunkTblMutex.Lock()
160161
cp := d.globalChunkTbl[c]
162+
d.globalChunkTblMutex.Unlock()
161163
// TODO update IP address as well.
162164
cp.Owner.PodName = owner
163165
cp.Owner.PodIp = s.Update.UpdFields.PodIp
@@ -255,7 +257,10 @@ func (d *Drsm) addChunk(full *FullStream) {
255257
c.resourceValidCb = d.resourceValidCb
256258

257259
pod.podChunks[cid] = c
260+
261+
d.globalChunkTblMutex.Lock()
258262
d.globalChunkTbl[cid] = c
263+
d.globalChunkTblMutex.Unlock()
259264

260265
logger.AppLog.Infof("Chunk id %v, podChunks %v ", cid, pod.podChunks)
261266
}

0 commit comments

Comments
 (0)