Browse Source

wal: Export SegmentSizeBytes as a variable

In test situations, it's useful to create smaller than usual WAL files
to test rotation and to avoid the overhead of preallocation on old-style
filesystems that don't handle it efficiently. This commit changes
segmentSizeBytes to an exported variable so that tests can override it
from an init() function.
Aaron Lehmann 10 years ago
parent
commit
2b996b6038
1 changed files with 9 additions and 7 deletions
  1. 9 7
      wal/wal.go

+ 9 - 7
wal/wal.go

@@ -41,16 +41,18 @@ const (
 	crcType
 	crcType
 	snapshotType
 	snapshotType
 
 
-	// the expected size of each wal segment file.
-	// the actual size might be bigger than it.
-	segmentSizeBytes = 64 * 1000 * 1000 // 64MB
-
 	// warnSyncDuration is the amount of time allotted to an fsync before
 	// warnSyncDuration is the amount of time allotted to an fsync before
 	// logging a warning
 	// logging a warning
 	warnSyncDuration = time.Second
 	warnSyncDuration = time.Second
 )
 )
 
 
 var (
 var (
+	// SegmentSizeBytes is the preallocated size of each wal segment file.
+	// The actual size might be larger than this. In general, the default
+	// value should be used, but this is defined as an exported variable
+	// so that tests can set a different segment size.
+	SegmentSizeBytes int64 = 64 * 1000 * 1000 // 64MB
+
 	plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "wal")
 	plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "wal")
 
 
 	ErrMetadataConflict = errors.New("wal: conflicting metadata found")
 	ErrMetadataConflict = errors.New("wal: conflicting metadata found")
@@ -109,7 +111,7 @@ func Create(dirpath string, metadata []byte) (*WAL, error) {
 	if _, err := f.Seek(0, os.SEEK_END); err != nil {
 	if _, err := f.Seek(0, os.SEEK_END); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
-	if err := fileutil.Preallocate(f.File, segmentSizeBytes, true); err != nil {
+	if err := fileutil.Preallocate(f.File, SegmentSizeBytes, true); err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
@@ -219,7 +221,7 @@ func openAtIndex(dirpath string, snap walpb.Snapshot, write bool) (*WAL, error)
 			closer()
 			closer()
 			return nil, err
 			return nil, err
 		}
 		}
-		w.fp = newFilePipeline(w.dir, segmentSizeBytes)
+		w.fp = newFilePipeline(w.dir, SegmentSizeBytes)
 	}
 	}
 
 
 	return w, nil
 	return w, nil
@@ -526,7 +528,7 @@ func (w *WAL) Save(st raftpb.HardState, ents []raftpb.Entry) error {
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
-	if curOff < segmentSizeBytes {
+	if curOff < SegmentSizeBytes {
 		if mustSync {
 		if mustSync {
 			return w.sync()
 			return w.sync()
 		}
 		}