@@ -88,24 +88,20 @@ func (db *Olric) selectVersionForMerge(dm *dmap, hkey uint64, vdata *storage.VDa
8888}
8989
9090func (db * Olric ) mergeDMaps (part * partition , data * dmapbox ) error {
91- str , err := storage . Import ( data .Payload )
91+ dm , err := db . getOrCreateDMap ( part , data .Name )
9292 if err != nil {
9393 return err
9494 }
9595
96- tmp , exist := part .m .Load (data .Name )
97- if ! exist {
98- // create a new dmap if it doesn't exist.
99- tmp , err = db .createDMap (part , data .Name , str )
100- if err != nil {
101- return err
102- }
103- }
104-
10596 // Acquire dmap's lock. No one should work on it.
106- dm := tmp .(* dmap )
10797 dm .Lock ()
10898 defer dm .Unlock ()
99+ defer part .m .Store (data .Name , dm )
100+
101+ str , err := storage .Import (data .Payload )
102+ if err != nil {
103+ return err
104+ }
109105
110106 // Merge accessLog.
111107 if dm .cache != nil && dm .cache .accessLog != nil {
@@ -118,11 +114,14 @@ func (db *Olric) mergeDMaps(part *partition, data *dmapbox) error {
118114 dm .cache .Unlock ()
119115 }
120116
121- // We do not need the following loop if the dmap is created here.
122- if ! exist {
117+ if dm .storage .Len () == 0 {
118+ // DMap has no keys. Set the imported storage instance.
119+ // The old one will be garbage collected.
120+ dm .storage = str
123121 return nil
124122 }
125123
124+ // DMap has some keys. Merge with the new one.
126125 var mergeErr error
127126 str .Range (func (hkey uint64 , vdata * storage.VData ) bool {
128127 winner , err := db .selectVersionForMerge (dm , hkey , vdata )
@@ -171,11 +170,11 @@ func (db *Olric) rebalancePrimaryPartitions() {
171170 }
172171 // This is a previous owner. Move the keys.
173172 part .m .Range (func (name , dm interface {}) bool {
174- db .log .V (2 ).Printf ("[INFO] Moving dmap : %s (backup: %v) on PartID: %d to %s" ,
173+ db .log .V (2 ).Printf ("[INFO] Moving DMap : %s (backup: %v) on PartID: %d to %s" ,
175174 name , part .backup , partID , owner )
176175 err := db .moveDMap (part , name .(string ), dm .(* dmap ), owner )
177176 if err != nil {
178- db .log .V (2 ).Printf ("[ERROR] Failed to move dmap : %s on PartID: %d to %s: %v" ,
177+ db .log .V (2 ).Printf ("[ERROR] Failed to move DMap : %s on PartID: %d to %s: %v" ,
179178 name , partID , owner , err )
180179 }
181180 // if this returns true, the iteration continues
@@ -232,11 +231,11 @@ func (db *Olric) rebalanceBackupPartitions() {
232231 }
233232
234233 part .m .Range (func (name , dm interface {}) bool {
235- db .log .V (2 ).Printf ("[INFO] Moving dmap : %s (backup: %v) on PartID: %d to %s" ,
234+ db .log .V (2 ).Printf ("[INFO] Moving DMap : %s (backup: %v) on PartID: %d to %s" ,
236235 name , part .backup , partID , owner )
237236 err := db .moveDMap (part , name .(string ), dm .(* dmap ), owner )
238237 if err != nil {
239- db .log .V (2 ).Printf ("[ERROR] Failed to move backup dmap : %s on PartID: %d to %s: %v" ,
238+ db .log .V (2 ).Printf ("[ERROR] Failed to move backup DMap : %s on PartID: %d to %s: %v" ,
240239 name , partID , owner , err )
241240 }
242241 // if this returns true, the iteration continues
@@ -294,20 +293,19 @@ func (db *Olric) moveDMapOperation(w, r protocol.EncodeDecoder) {
294293 }
295294 // Check ownership before merging. This is useful to prevent data corruption in network partitioning case.
296295 if ! db .checkOwnership (part ) {
297- db .log .V (2 ).Printf ("[ERROR] Received dmap : %s on PartID: %d (backup: %v) doesn't belong to me" ,
296+ db .log .V (2 ).Printf ("[ERROR] Received DMap : %s on PartID: %d (backup: %v) doesn't belong to me" ,
298297 box .Name , box .PartID , box .Backup )
299-
300298 err := fmt .Errorf ("partID: %d (backup: %v) doesn't belong to %s: %w" , box .PartID , box .Backup , db .this , ErrInvalidArgument )
301299 db .errorResponse (w , err )
302300 return
303301 }
304302
305- db .log .V (2 ).Printf ("[INFO] Received dmap (backup:%v): %s on PartID: %d" ,
303+ db .log .V (2 ).Printf ("[INFO] Received DMap (backup:%v): %s on PartID: %d" ,
306304 box .Backup , box .Name , box .PartID )
307305
308306 err = db .mergeDMaps (part , box )
309307 if err != nil {
310- db .log .V (2 ).Printf ("[ERROR] Failed to merge dmap : %v" , err )
308+ db .log .V (2 ).Printf ("[ERROR] Failed to merge DMap : %v" , err )
311309 db .errorResponse (w , err )
312310 return
313311 }
0 commit comments