Fix compacting disable/enable
Enabling and disabling compaction no longer blocks are potentially causes panics.
This commit is contained in:
		
							parent
							
								
									3065be97d8
								
							
						
					
					
						commit
						74f67e8271
					
				
							
								
								
									
										23
									
								
								db.go
								
								
								
								
							
							
						
						
									
										23
									
								
								db.go
								
								
								
								
							|  | @ -100,7 +100,6 @@ type DB struct { | ||||||
| 	opts    *Options | 	opts    *Options | ||||||
| 
 | 
 | ||||||
| 	// Mutex for that must be held when modifying the general block layout.
 | 	// Mutex for that must be held when modifying the general block layout.
 | ||||||
| 	// cmtx must be held before acquiring it.
 |  | ||||||
| 	mtx    sync.RWMutex | 	mtx    sync.RWMutex | ||||||
| 	blocks []Block | 	blocks []Block | ||||||
| 
 | 
 | ||||||
|  | @ -118,7 +117,7 @@ type DB struct { | ||||||
| 
 | 
 | ||||||
| 	// cmtx is used to control compactions and deletions.
 | 	// cmtx is used to control compactions and deletions.
 | ||||||
| 	cmtx               sync.Mutex | 	cmtx               sync.Mutex | ||||||
| 	compacting bool | 	compactionsEnabled bool | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type dbMetrics struct { | type dbMetrics struct { | ||||||
|  | @ -203,7 +202,7 @@ func Open(dir string, l log.Logger, r prometheus.Registerer, opts *Options) (db | ||||||
| 		compactc:           make(chan struct{}, 1), | 		compactc:           make(chan struct{}, 1), | ||||||
| 		donec:              make(chan struct{}), | 		donec:              make(chan struct{}), | ||||||
| 		stopc:              make(chan struct{}), | 		stopc:              make(chan struct{}), | ||||||
| 		compacting: true, | 		compactionsEnabled: true, | ||||||
| 	} | 	} | ||||||
| 	db.metrics = newDBMetrics(db, r) | 	db.metrics = newDBMetrics(db, r) | ||||||
| 
 | 
 | ||||||
|  | @ -371,6 +370,10 @@ func (db *DB) compact() (changes bool, err error) { | ||||||
| 	db.cmtx.Lock() | 	db.cmtx.Lock() | ||||||
| 	defer db.cmtx.Unlock() | 	defer db.cmtx.Unlock() | ||||||
| 
 | 
 | ||||||
|  | 	if !db.compactionsEnabled { | ||||||
|  | 		return false, nil | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	// Check whether we have pending head blocks that are ready to be persisted.
 | 	// Check whether we have pending head blocks that are ready to be persisted.
 | ||||||
| 	// They have the highest priority.
 | 	// They have the highest priority.
 | ||||||
| 	for _, h := range db.completedHeads() { | 	for _, h := range db.completedHeads() { | ||||||
|  | @ -579,20 +582,20 @@ func (db *DB) Close() error { | ||||||
| 
 | 
 | ||||||
| // DisableCompactions disables compactions.
 | // DisableCompactions disables compactions.
 | ||||||
| func (db *DB) DisableCompactions() { | func (db *DB) DisableCompactions() { | ||||||
| 	if db.compacting { |  | ||||||
| 	db.cmtx.Lock() | 	db.cmtx.Lock() | ||||||
| 		db.compacting = false | 	defer db.cmtx.Unlock() | ||||||
|  | 
 | ||||||
|  | 	db.compactionsEnabled = false | ||||||
| 	db.logger.Log("msg", "compactions disabled") | 	db.logger.Log("msg", "compactions disabled") | ||||||
| 	} |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // EnableCompactions enables compactions.
 | // EnableCompactions enables compactions.
 | ||||||
| func (db *DB) EnableCompactions() { | func (db *DB) EnableCompactions() { | ||||||
| 	if !db.compacting { | 	db.cmtx.Lock() | ||||||
| 		db.cmtx.Unlock() | 	defer db.cmtx.Unlock() | ||||||
| 		db.compacting = true | 
 | ||||||
|  | 	db.compactionsEnabled = true | ||||||
| 	db.logger.Log("msg", "compactions enabled") | 	db.logger.Log("msg", "compactions enabled") | ||||||
| 	} |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Snapshot writes the current data to the directory.
 | // Snapshot writes the current data to the directory.
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue