Skip to content

Commit 9499811

Browse files
authored
Migrate registry from previous incorrect path (#10486)
In 6.x Journalbeat placed its registry file under the wrong path ignoring the data.path settings. This patch lets users migrate from registries under such paths when upgrading from 6.x to 7.x.
1 parent 7120870 commit 9499811

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

CHANGELOG.next.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
270270

271271
*Journalbeat*
272272

273+
- Migrate registry from previously incorrect path. {pull}10486[10486]
274+
273275
*Metricbeat*
274276

275277
- Add `key` metricset to the Redis module. {issue}9582[9582] {pull}9657[9657] {pull}9746[9746]

journalbeat/checkpoint/checkpoint.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package checkpoint
2222

2323
import (
2424
"fmt"
25+
"io"
2526
"io/ioutil"
2627
"os"
2728
"path/filepath"
@@ -88,7 +89,10 @@ func NewCheckpoint(file string, maxUpdates int, interval time.Duration) (*Checkp
8889
save: make(chan JournalState, 1),
8990
}
9091

91-
c.file = paths.Resolve(paths.Data, c.file)
92+
err := c.findRegistryFile()
93+
if err != nil {
94+
return nil, fmt.Errorf("error locating the proper registry file: %+v", err)
95+
}
9296

9397
// Minimum batch size.
9498
if c.maxUpdates < 1 {
@@ -123,6 +127,53 @@ func NewCheckpoint(file string, maxUpdates int, interval time.Duration) (*Checkp
123127
return c, nil
124128
}
125129

130+
// Previously the registry file was written to the root folder. It was fixed on
131+
// 7.x but not on 6.x. Thus, migration is needed, so users avoid losing state info.
132+
func (c *Checkpoint) findRegistryFile() error {
133+
migratedPath := paths.Resolve(paths.Data, c.file)
134+
135+
fs, err := os.Stat(c.file)
136+
if os.IsNotExist(err) {
137+
c.file = migratedPath
138+
return nil
139+
} else if err != nil {
140+
return fmt.Errorf("error accessing previous registry file: %+v", err)
141+
}
142+
143+
f, err := os.Open(c.file)
144+
if err != nil {
145+
return err
146+
}
147+
defer f.Close()
148+
149+
target, err := os.OpenFile(migratedPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fs.Mode())
150+
if err != nil {
151+
return err
152+
}
153+
defer target.Close()
154+
155+
if _, err := io.Copy(target, f); err != nil {
156+
return err
157+
}
158+
159+
err = target.Sync()
160+
if err != nil {
161+
return fmt.Errorf("error while syncing new registry file to disk: %+v", err)
162+
}
163+
164+
c.file = migratedPath
165+
166+
p := filepath.Dir(migratedPath)
167+
pf, err := os.Open(p)
168+
if err != nil {
169+
return nil
170+
}
171+
defer pf.Close()
172+
pf.Sync()
173+
174+
return nil
175+
}
176+
126177
// run is worker loop that reads incoming state information from the save
127178
// channel and persists it when the number of changes reaches maxEvents or
128179
// the amount of time since the last disk write reaches flushInterval.

0 commit comments

Comments
 (0)