2017-01-02 09:34:55 +00:00
|
|
|
|
package tsdb
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
2017-01-02 13:41:13 +00:00
|
|
|
|
"path/filepath"
|
2017-01-03 14:43:26 +00:00
|
|
|
|
"time"
|
2017-01-02 09:34:55 +00:00
|
|
|
|
|
2017-01-02 13:41:13 +00:00
|
|
|
|
"github.com/coreos/etcd/pkg/fileutil"
|
2017-01-02 09:34:55 +00:00
|
|
|
|
"github.com/fabxc/tsdb/labels"
|
2017-01-03 14:43:26 +00:00
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-01-02 09:34:55 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type compactor struct {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
metrics *compactorMetrics
|
2017-01-18 05:18:32 +00:00
|
|
|
|
opts *compactorOptions
|
2017-01-02 09:34:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 14:43:26 +00:00
|
|
|
|
type compactorMetrics struct {
|
2017-01-06 11:37:28 +00:00
|
|
|
|
ran prometheus.Counter
|
|
|
|
|
failed prometheus.Counter
|
|
|
|
|
duration prometheus.Histogram
|
2017-01-03 14:43:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 10:40:09 +00:00
|
|
|
|
func newCompactorMetrics(r prometheus.Registerer) *compactorMetrics {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
m := &compactorMetrics{}
|
|
|
|
|
|
|
|
|
|
m.ran = prometheus.NewCounter(prometheus.CounterOpts{
|
2017-01-06 10:40:09 +00:00
|
|
|
|
Name: "tsdb_compactions_total",
|
|
|
|
|
Help: "Total number of compactions that were executed for the partition.",
|
2017-01-03 14:43:26 +00:00
|
|
|
|
})
|
|
|
|
|
m.failed = prometheus.NewCounter(prometheus.CounterOpts{
|
2017-01-06 10:40:09 +00:00
|
|
|
|
Name: "tsdb_compactions_failed_total",
|
|
|
|
|
Help: "Total number of compactions that failed for the partition.",
|
2017-01-03 14:43:26 +00:00
|
|
|
|
})
|
|
|
|
|
m.duration = prometheus.NewHistogram(prometheus.HistogramOpts{
|
2017-01-06 10:40:09 +00:00
|
|
|
|
Name: "tsdb_compaction_duration",
|
|
|
|
|
Help: "Duration of compaction runs.",
|
2017-01-03 14:43:26 +00:00
|
|
|
|
})
|
|
|
|
|
|
2017-01-06 10:40:09 +00:00
|
|
|
|
if r != nil {
|
|
|
|
|
r.MustRegister(
|
|
|
|
|
m.ran,
|
|
|
|
|
m.failed,
|
|
|
|
|
m.duration,
|
|
|
|
|
)
|
|
|
|
|
}
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 05:18:32 +00:00
|
|
|
|
type compactorOptions struct {
|
|
|
|
|
maxBlocks uint8
|
|
|
|
|
maxBlockRange uint64
|
|
|
|
|
maxSize uint64
|
2017-01-06 12:53:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 05:18:32 +00:00
|
|
|
|
func newCompactor(r prometheus.Registerer, opts *compactorOptions) *compactor {
|
|
|
|
|
return &compactor{
|
|
|
|
|
opts: opts,
|
2017-01-09 18:14:21 +00:00
|
|
|
|
metrics: newCompactorMetrics(r),
|
2017-01-02 09:34:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 05:18:32 +00:00
|
|
|
|
// pick returns a range [i, j] in the blocks that are suitable to be compacted
|
|
|
|
|
// into a single block at position i.
|
|
|
|
|
func (c *compactor) pick(bs []Block) (i, j int, ok bool) {
|
|
|
|
|
last := len(bs) - 1
|
2017-01-03 14:43:26 +00:00
|
|
|
|
if len(bs) == 0 {
|
2017-01-18 05:18:32 +00:00
|
|
|
|
return 0, 0, false
|
2017-01-03 14:43:26 +00:00
|
|
|
|
}
|
2017-01-18 05:18:32 +00:00
|
|
|
|
|
|
|
|
|
// Make sure we always compact the last block if unpersisted.
|
|
|
|
|
if !bs[last].Persisted() {
|
|
|
|
|
if len(bs) >= 3 && compactionMatch(bs[last-2:last+1]) {
|
|
|
|
|
return last - 2, last, true
|
2017-01-06 12:53:05 +00:00
|
|
|
|
}
|
2017-01-18 05:18:32 +00:00
|
|
|
|
return last, last, true
|
2017-01-06 12:53:05 +00:00
|
|
|
|
}
|
2017-01-03 14:43:26 +00:00
|
|
|
|
|
2017-01-06 12:53:05 +00:00
|
|
|
|
for i := 0; i+2 < len(bs); i += 3 {
|
|
|
|
|
tpl := bs[i : i+3]
|
2017-01-04 20:11:15 +00:00
|
|
|
|
if compactionMatch(tpl) {
|
2017-01-18 05:18:32 +00:00
|
|
|
|
return i, i + 2, true
|
2017-01-03 14:43:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-18 05:18:32 +00:00
|
|
|
|
return 0, 0, false
|
2017-01-04 20:11:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 14:28:22 +00:00
|
|
|
|
func compactionMatch(blocks []Block) bool {
|
2017-01-04 20:11:15 +00:00
|
|
|
|
// TODO(fabxc): check whether combined size is below maxCompactionSize.
|
|
|
|
|
// Apply maximum time range? or number of series? – might already be covered by size implicitly.
|
2017-01-03 14:43:26 +00:00
|
|
|
|
|
2017-01-06 12:53:05 +00:00
|
|
|
|
// Naively check whether both blocks have roughly the same number of samples
|
|
|
|
|
// and whether the total sample count doesn't exceed 2GB chunk file size
|
|
|
|
|
// by rough approximation.
|
2017-01-19 10:22:47 +00:00
|
|
|
|
n := float64(blocks[0].Meta().Stats.NumSamples)
|
2017-01-06 12:53:05 +00:00
|
|
|
|
t := n
|
|
|
|
|
|
|
|
|
|
for _, b := range blocks[1:] {
|
2017-01-19 10:22:47 +00:00
|
|
|
|
m := float64(b.Meta().Stats.NumSamples)
|
2017-01-06 12:53:05 +00:00
|
|
|
|
|
2017-01-18 05:18:32 +00:00
|
|
|
|
if m < 0.7*n || m > 1.3*n {
|
2017-01-06 12:53:05 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
t += m
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pessimistic 10 bytes/sample should do.
|
|
|
|
|
return t < 10*200e6
|
2017-01-03 09:09:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 10:22:47 +00:00
|
|
|
|
func mergeBlockMetas(blocks ...Block) (res BlockMeta) {
|
|
|
|
|
res.MinTime = blocks[0].Meta().MinTime
|
|
|
|
|
res.MaxTime = blocks[len(blocks)-1].Meta().MaxTime
|
2017-01-03 14:43:26 +00:00
|
|
|
|
|
|
|
|
|
for _, b := range blocks {
|
2017-01-19 10:22:47 +00:00
|
|
|
|
res.Stats.NumSamples += b.Meta().Stats.NumSamples
|
2017-01-02 09:34:55 +00:00
|
|
|
|
}
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return res
|
|
|
|
|
}
|
2017-01-02 09:34:55 +00:00
|
|
|
|
|
2017-01-10 14:28:22 +00:00
|
|
|
|
func (c *compactor) compact(dir string, blocks ...Block) (err error) {
|
2017-01-06 11:37:28 +00:00
|
|
|
|
start := time.Now()
|
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.metrics.failed.Inc()
|
|
|
|
|
}
|
|
|
|
|
c.metrics.duration.Observe(time.Since(start).Seconds())
|
|
|
|
|
}()
|
2017-01-03 14:43:26 +00:00
|
|
|
|
|
|
|
|
|
// Write to temporary directory to make persistence appear atomic.
|
2017-01-06 11:37:28 +00:00
|
|
|
|
if fileutil.Exist(dir) {
|
|
|
|
|
if err = os.RemoveAll(dir); err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-19 07:40:15 +00:00
|
|
|
|
if err = os.MkdirAll(dir, 0755); err != nil {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 11:37:28 +00:00
|
|
|
|
chunkf, err := fileutil.LockFile(chunksFileName(dir), os.O_WRONLY|os.O_CREATE, 0666)
|
2017-01-02 10:12:28 +00:00
|
|
|
|
if err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "create chunk file")
|
2017-01-02 10:12:28 +00:00
|
|
|
|
}
|
2017-01-06 11:37:28 +00:00
|
|
|
|
indexf, err := fileutil.LockFile(indexFileName(dir), os.O_WRONLY|os.O_CREATE, 0666)
|
2017-01-02 10:12:28 +00:00
|
|
|
|
if err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "create index file")
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 10:22:47 +00:00
|
|
|
|
indexw, err := newIndexWriter(indexf)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "open index writer")
|
|
|
|
|
}
|
2017-01-03 14:43:26 +00:00
|
|
|
|
chunkw := newSeriesWriter(chunkf, indexw)
|
|
|
|
|
|
2017-01-06 11:37:28 +00:00
|
|
|
|
if err = c.write(blocks, indexw, chunkw); err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "write compaction")
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 11:37:28 +00:00
|
|
|
|
if err = chunkw.Close(); err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "close chunk writer")
|
|
|
|
|
}
|
2017-01-06 11:37:28 +00:00
|
|
|
|
if err = indexw.Close(); err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "close index writer")
|
|
|
|
|
}
|
2017-01-06 11:37:28 +00:00
|
|
|
|
if err = fileutil.Fsync(chunkf.File); err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "fsync chunk file")
|
|
|
|
|
}
|
2017-01-06 11:37:28 +00:00
|
|
|
|
if err = fileutil.Fsync(indexf.File); err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "fsync index file")
|
|
|
|
|
}
|
2017-01-06 11:37:28 +00:00
|
|
|
|
if err = chunkf.Close(); err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "close chunk file")
|
|
|
|
|
}
|
2017-01-06 11:37:28 +00:00
|
|
|
|
if err = indexf.Close(); err != nil {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
return errors.Wrap(err, "close index file")
|
|
|
|
|
}
|
2017-01-06 11:37:28 +00:00
|
|
|
|
return nil
|
2017-01-03 14:43:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-10 14:28:22 +00:00
|
|
|
|
func (c *compactor) write(blocks []Block, indexw IndexWriter, chunkw SeriesWriter) error {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
var set compactionSet
|
2017-01-18 05:18:32 +00:00
|
|
|
|
|
2017-01-03 14:43:26 +00:00
|
|
|
|
for i, b := range blocks {
|
2017-01-10 14:28:22 +00:00
|
|
|
|
all, err := b.Index().Postings("", "")
|
2017-01-03 14:43:26 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-01-05 14:13:01 +00:00
|
|
|
|
// TODO(fabxc): find more transparent way of handling this.
|
2017-01-10 14:28:22 +00:00
|
|
|
|
if hb, ok := b.(*headBlock); ok {
|
2017-01-05 14:13:01 +00:00
|
|
|
|
all = hb.remapPostings(all)
|
|
|
|
|
}
|
2017-01-10 14:28:22 +00:00
|
|
|
|
s := newCompactionSeriesSet(b.Index(), b.Series(), all)
|
2017-01-03 14:43:26 +00:00
|
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
|
set = s
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
set, err = newCompactionMerger(set, s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2017-01-02 10:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-02 09:34:55 +00:00
|
|
|
|
// We fully rebuild the postings list index from merged series.
|
|
|
|
|
var (
|
|
|
|
|
postings = &memPostings{m: make(map[term][]uint32, 512)}
|
|
|
|
|
values = map[string]stringset{}
|
|
|
|
|
i = uint32(0)
|
2017-01-19 10:22:47 +00:00
|
|
|
|
meta = mergeBlockMetas(blocks...)
|
2017-01-02 09:34:55 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
for set.Next() {
|
|
|
|
|
lset, chunks := set.At()
|
2017-01-02 15:58:47 +00:00
|
|
|
|
if err := chunkw.WriteSeries(i, lset, chunks); err != nil {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-19 10:22:47 +00:00
|
|
|
|
meta.Stats.NumChunks += uint64(len(chunks))
|
|
|
|
|
meta.Stats.NumSeries++
|
2017-01-02 09:34:55 +00:00
|
|
|
|
|
|
|
|
|
for _, l := range lset {
|
|
|
|
|
valset, ok := values[l.Name]
|
|
|
|
|
if !ok {
|
|
|
|
|
valset = stringset{}
|
|
|
|
|
values[l.Name] = valset
|
|
|
|
|
}
|
|
|
|
|
valset.set(l.Value)
|
|
|
|
|
|
|
|
|
|
postings.add(i, term{name: l.Name, value: l.Value})
|
|
|
|
|
}
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
if set.Err() != nil {
|
|
|
|
|
return set.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s := make([]string, 0, 256)
|
|
|
|
|
for n, v := range values {
|
|
|
|
|
s = s[:0]
|
|
|
|
|
|
|
|
|
|
for x := range v {
|
|
|
|
|
s = append(s, x)
|
|
|
|
|
}
|
2017-01-02 15:58:47 +00:00
|
|
|
|
if err := indexw.WriteLabelIndex([]string{n}, s); err != nil {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for t := range postings.m {
|
2017-01-02 15:58:47 +00:00
|
|
|
|
if err := indexw.WritePostings(t.name, t.value, postings.get(t)); err != nil {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Write a postings list containing all series.
|
|
|
|
|
all := make([]uint32, i)
|
|
|
|
|
for i := range all {
|
|
|
|
|
all[i] = uint32(i)
|
|
|
|
|
}
|
2017-01-02 15:58:47 +00:00
|
|
|
|
if err := indexw.WritePostings("", "", newListPostings(all)); err != nil {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 14:43:26 +00:00
|
|
|
|
type compactionSet interface {
|
|
|
|
|
Next() bool
|
|
|
|
|
At() (labels.Labels, []ChunkMeta)
|
|
|
|
|
Err() error
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-02 09:34:55 +00:00
|
|
|
|
type compactionSeriesSet struct {
|
|
|
|
|
p Postings
|
|
|
|
|
index IndexReader
|
|
|
|
|
series SeriesReader
|
|
|
|
|
|
|
|
|
|
l labels.Labels
|
|
|
|
|
c []ChunkMeta
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newCompactionSeriesSet(i IndexReader, s SeriesReader, p Postings) *compactionSeriesSet {
|
|
|
|
|
return &compactionSeriesSet{
|
|
|
|
|
index: i,
|
|
|
|
|
series: s,
|
|
|
|
|
p: p,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *compactionSeriesSet) Next() bool {
|
|
|
|
|
if !c.p.Next() {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-02 12:27:52 +00:00
|
|
|
|
c.l, c.c, c.err = c.index.Series(c.p.At())
|
2017-01-02 09:34:55 +00:00
|
|
|
|
if c.err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for i := range c.c {
|
|
|
|
|
chk := &c.c[i]
|
|
|
|
|
|
|
|
|
|
chk.Chunk, c.err = c.series.Chunk(chk.Ref)
|
|
|
|
|
if c.err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *compactionSeriesSet) Err() error {
|
|
|
|
|
if c.err != nil {
|
|
|
|
|
return c.err
|
|
|
|
|
}
|
|
|
|
|
return c.p.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *compactionSeriesSet) At() (labels.Labels, []ChunkMeta) {
|
|
|
|
|
return c.l, c.c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type compactionMerger struct {
|
2017-01-03 14:43:26 +00:00
|
|
|
|
a, b compactionSet
|
2017-01-02 09:34:55 +00:00
|
|
|
|
|
2017-01-04 20:11:15 +00:00
|
|
|
|
aok, bok bool
|
|
|
|
|
l labels.Labels
|
|
|
|
|
c []ChunkMeta
|
2017-01-02 09:34:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type compactionSeries struct {
|
|
|
|
|
labels labels.Labels
|
|
|
|
|
chunks []ChunkMeta
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 14:43:26 +00:00
|
|
|
|
func newCompactionMerger(a, b compactionSet) (*compactionMerger, error) {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
c := &compactionMerger{
|
|
|
|
|
a: a,
|
|
|
|
|
b: b,
|
|
|
|
|
}
|
|
|
|
|
// Initialize first elements of both sets as Next() needs
|
|
|
|
|
// one element look-ahead.
|
2017-01-04 20:11:15 +00:00
|
|
|
|
c.aok = c.a.Next()
|
|
|
|
|
c.bok = c.b.Next()
|
2017-01-02 09:34:55 +00:00
|
|
|
|
|
|
|
|
|
return c, c.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *compactionMerger) compare() int {
|
2017-01-04 20:11:15 +00:00
|
|
|
|
if !c.aok {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
return 1
|
|
|
|
|
}
|
2017-01-04 20:11:15 +00:00
|
|
|
|
if !c.bok {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
a, _ := c.a.At()
|
|
|
|
|
b, _ := c.b.At()
|
|
|
|
|
return labels.Compare(a, b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *compactionMerger) Next() bool {
|
2017-01-04 20:11:15 +00:00
|
|
|
|
if !c.aok && !c.bok || c.Err() != nil {
|
2017-01-02 09:34:55 +00:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d := c.compare()
|
|
|
|
|
// Both sets contain the current series. Chain them into a single one.
|
|
|
|
|
if d > 0 {
|
|
|
|
|
c.l, c.c = c.b.At()
|
2017-01-04 20:11:15 +00:00
|
|
|
|
c.bok = c.b.Next()
|
2017-01-02 09:34:55 +00:00
|
|
|
|
} else if d < 0 {
|
|
|
|
|
c.l, c.c = c.a.At()
|
2017-01-04 20:11:15 +00:00
|
|
|
|
c.aok = c.a.Next()
|
2017-01-02 09:34:55 +00:00
|
|
|
|
} else {
|
|
|
|
|
l, ca := c.a.At()
|
|
|
|
|
_, cb := c.b.At()
|
|
|
|
|
|
|
|
|
|
c.l = l
|
|
|
|
|
c.c = append(ca, cb...)
|
|
|
|
|
|
2017-01-04 20:11:15 +00:00
|
|
|
|
c.aok = c.a.Next()
|
|
|
|
|
c.bok = c.b.Next()
|
2017-01-02 09:34:55 +00:00
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *compactionMerger) Err() error {
|
|
|
|
|
if c.a.Err() != nil {
|
|
|
|
|
return c.a.Err()
|
|
|
|
|
}
|
|
|
|
|
return c.b.Err()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *compactionMerger) At() (labels.Labels, []ChunkMeta) {
|
|
|
|
|
return c.l, c.c
|
|
|
|
|
}
|
2017-01-02 13:41:13 +00:00
|
|
|
|
|
|
|
|
|
func renameDir(from, to string) error {
|
|
|
|
|
if err := os.RemoveAll(to); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err := os.Rename(from, to); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Directory was renamed; sync parent dir to persist rename.
|
|
|
|
|
pdir, err := fileutil.OpenDir(filepath.Dir(to))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = fileutil.Fsync(pdir); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if err = pdir.Close(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|