You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

558 lines
13 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
8 years ago
8 years ago
fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
8 years ago
fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
8 years ago
8 years ago
8 years ago
cs: sync WAL more frequently (#3300) As per #3043, this adds a ticker to sync the WAL every 2s while the WAL is running. * Flush WAL every 2s This adds a ticker that flushes the WAL every 2s while the WAL is running. This is related to #3043. * Fix spelling * Increase timeout to 2mins for slower build environments * Make WAL sync interval configurable * Add TODO to replace testChan with more comprehensive testBus * Remove extraneous debug statement * Remove testChan in favour of using system time As per https://github.com/tendermint/tendermint/pull/3300#discussion_r255886586, this removes the `testChan` WAL member and replaces the approach with a system time-oriented one. In this new approach, we keep track of the system time at which each flush and periodic flush successfully occurred. The naming of the various functions is also updated here to be more consistent with "flushing" as opposed to "sync'ing". * Update naming convention and ensure lock for timestamp update * Add Flush method as part of WAL interface Adds a `Flush` method as part of the WAL interface to enforce the idea that we can manually trigger a WAL flush from outside of the WAL. This is employed in the consensus state management to flush the WAL prior to signing votes/proposals, as per https://github.com/tendermint/tendermint/issues/3043#issuecomment-453853630 * Update CHANGELOG_PENDING * Remove mutex approach and replace with DI The dependency injection approach to dealing with testing concerns could allow similar effects to some kind of "testing bus"-based approach. This commit introduces an example of this, where instead of relying on (potentially fragile) timing of things between the code and the test, we inject code into the function under test that can signal the test through a channel. This allows us to avoid the `time.Sleep()`-based approach previously employed. * Update comment on WAL flushing during vote signing Co-Authored-By: thanethomson <connect@thanethomson.com> * Simplify flush interval definition Co-Authored-By: thanethomson <connect@thanethomson.com> * Expand commentary on WAL disk flushing Co-Authored-By: thanethomson <connect@thanethomson.com> * Add broken test to illustrate WAL sync test problem Removes test-related state (dependency injection code) from the WAL data structure and adds test code to illustrate the problem with using `WALGenerateNBlocks` and `wal.SearchForEndHeight` to test periodic sync'ing. * Fix test error messages * Use WAL group buffer size to check for flush A function is added to `libs/autofile/group.go#Group` in order to return the size of the buffered data (i.e. data that has not yet been flushed to disk). The test now checks that, prior to a `time.Sleep`, the group buffer has data in it. After the `time.Sleep` (during which time the periodic flush should have been called), the buffer should be empty. * Remove config root dir removal from #3291 * Add godoc for NewWAL mentioning periodic sync
6 years ago
8 years ago
fix non deterministic test failures and race in privval socket (#3258) * node: decrease retry conn timeout in test Should fix #3256 The retry timeout was set to the default, which is the same as the accept timeout, so it's no wonder this would fail. Here we decrease the retry timeout so we can try many times before the accept timeout. * p2p: increase handshake timeout in test This fails sometimes, presumably because the handshake timeout is so low (only 50ms). So increase it to 1s. Should fix #3187 * privval: fix race with ping. closes #3237 Pings happen in a go-routine and can happen concurrently with other messages. Since we use a request/response protocol, we expect to send a request and get back the corresponding response. But with pings happening concurrently, this assumption could be violated. We were using a mutex, but only a RWMutex, where the RLock was being held for sending messages - this was to allow the underlying connection to be replaced if it fails. Turns out we actually need to use a full lock (not just a read lock) to prevent multiple requests from happening concurrently. * node: fix test name. DelayedStop -> DelayedStart * autofile: Wait() method In the TestWALTruncate in consensus/wal_test.go we remove the WAL directory at the end of the test. However the wal.Stop() does not properly wait for the autofile group to finish shutting down. Hence it was possible that the group's go-routine is still running when the cleanup happens, which causes a panic since the directory disappeared. Here we add a Wait() method to properly wait until the go-routine exits so we can safely clean up. This fixes #2852.
6 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package autofile
  2. import (
  3. "bufio"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "os"
  9. "path/filepath"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/tendermint/tendermint/libs/log"
  16. "github.com/tendermint/tendermint/libs/service"
  17. )
  18. const (
  19. defaultGroupCheckDuration = 5000 * time.Millisecond
  20. defaultHeadSizeLimit = 10 * 1024 * 1024 // 10MB
  21. defaultTotalSizeLimit = 1 * 1024 * 1024 * 1024 // 1GB
  22. maxFilesToRemove = 4 // needs to be greater than 1
  23. )
  24. /*
  25. You can open a Group to keep restrictions on an AutoFile, like
  26. the maximum size of each chunk, and/or the total amount of bytes
  27. stored in the group.
  28. The first file to be written in the Group.Dir is the head file.
  29. Dir/
  30. - <HeadPath>
  31. Once the Head file reaches the size limit, it will be rotated.
  32. Dir/
  33. - <HeadPath>.000 // First rolled file
  34. - <HeadPath> // New head path, starts empty.
  35. // The implicit index is 001.
  36. As more files are written, the index numbers grow...
  37. Dir/
  38. - <HeadPath>.000 // First rolled file
  39. - <HeadPath>.001 // Second rolled file
  40. - ...
  41. - <HeadPath> // New head path
  42. The Group can also be used to binary-search for some line,
  43. assuming that marker lines are written occasionally.
  44. */
  45. type Group struct {
  46. service.BaseService
  47. logger log.Logger
  48. ID string
  49. Head *AutoFile // The head AutoFile to write to
  50. headBuf *bufio.Writer
  51. Dir string // Directory that contains .Head
  52. ticker *time.Ticker
  53. mtx sync.Mutex
  54. headSizeLimit int64
  55. totalSizeLimit int64
  56. groupCheckDuration time.Duration
  57. minIndex int // Includes head
  58. maxIndex int // Includes head, where Head will move to
  59. // close this when the processTicks routine is done.
  60. // this ensures we can cleanup the dir after calling Stop
  61. // and the routine won't be trying to access it anymore
  62. doneProcessTicks chan struct{}
  63. // TODO: When we start deleting files, we need to start tracking GroupReaders
  64. // and their dependencies.
  65. }
  66. // OpenGroup creates a new Group with head at headPath. It returns an error if
  67. // it fails to open head file.
  68. func OpenGroup(ctx context.Context, logger log.Logger, headPath string, groupOptions ...func(*Group)) (*Group, error) {
  69. dir, err := filepath.Abs(filepath.Dir(headPath))
  70. if err != nil {
  71. return nil, err
  72. }
  73. head, err := OpenAutoFile(ctx, headPath)
  74. if err != nil {
  75. return nil, err
  76. }
  77. g := &Group{
  78. logger: logger,
  79. ID: "group:" + head.ID,
  80. Head: head,
  81. headBuf: bufio.NewWriterSize(head, 4096*10),
  82. Dir: dir,
  83. headSizeLimit: defaultHeadSizeLimit,
  84. totalSizeLimit: defaultTotalSizeLimit,
  85. groupCheckDuration: defaultGroupCheckDuration,
  86. minIndex: 0,
  87. maxIndex: 0,
  88. doneProcessTicks: make(chan struct{}),
  89. }
  90. for _, option := range groupOptions {
  91. option(g)
  92. }
  93. g.BaseService = *service.NewBaseService(logger, "Group", g)
  94. gInfo := g.readGroupInfo()
  95. g.minIndex = gInfo.MinIndex
  96. g.maxIndex = gInfo.MaxIndex
  97. return g, nil
  98. }
  99. // GroupCheckDuration allows you to overwrite default groupCheckDuration.
  100. func GroupCheckDuration(duration time.Duration) func(*Group) {
  101. return func(g *Group) {
  102. g.groupCheckDuration = duration
  103. }
  104. }
  105. // GroupHeadSizeLimit allows you to overwrite default head size limit - 10MB.
  106. func GroupHeadSizeLimit(limit int64) func(*Group) {
  107. return func(g *Group) {
  108. g.headSizeLimit = limit
  109. }
  110. }
  111. // GroupTotalSizeLimit allows you to overwrite default total size limit of the group - 1GB.
  112. func GroupTotalSizeLimit(limit int64) func(*Group) {
  113. return func(g *Group) {
  114. g.totalSizeLimit = limit
  115. }
  116. }
  117. // OnStart implements service.Service by starting the goroutine that checks file
  118. // and group limits.
  119. func (g *Group) OnStart(ctx context.Context) error {
  120. g.ticker = time.NewTicker(g.groupCheckDuration)
  121. go g.processTicks(ctx)
  122. return nil
  123. }
  124. // OnStop implements service.Service by stopping the goroutine described above.
  125. // NOTE: g.Head must be closed separately using Close.
  126. func (g *Group) OnStop() {
  127. g.ticker.Stop()
  128. if err := g.FlushAndSync(); err != nil {
  129. g.logger.Error("error flushing to disk", "err", err)
  130. }
  131. }
  132. // Wait blocks until all internal goroutines are finished. Supposed to be
  133. // called after Stop.
  134. func (g *Group) Wait() {
  135. // wait for processTicks routine to finish
  136. <-g.doneProcessTicks
  137. }
  138. // Close closes the head file. The group must be stopped by this moment.
  139. func (g *Group) Close() {
  140. if err := g.FlushAndSync(); err != nil {
  141. g.logger.Error("error flushing to disk", "err", err)
  142. }
  143. g.mtx.Lock()
  144. _ = g.Head.Close()
  145. g.mtx.Unlock()
  146. }
  147. // HeadSizeLimit returns the current head size limit.
  148. func (g *Group) HeadSizeLimit() int64 {
  149. g.mtx.Lock()
  150. defer g.mtx.Unlock()
  151. return g.headSizeLimit
  152. }
  153. // TotalSizeLimit returns total size limit of the group.
  154. func (g *Group) TotalSizeLimit() int64 {
  155. g.mtx.Lock()
  156. defer g.mtx.Unlock()
  157. return g.totalSizeLimit
  158. }
  159. // MaxIndex returns index of the last file in the group.
  160. func (g *Group) MaxIndex() int {
  161. g.mtx.Lock()
  162. defer g.mtx.Unlock()
  163. return g.maxIndex
  164. }
  165. // MinIndex returns index of the first file in the group.
  166. func (g *Group) MinIndex() int {
  167. g.mtx.Lock()
  168. defer g.mtx.Unlock()
  169. return g.minIndex
  170. }
  171. // Write writes the contents of p into the current head of the group. It
  172. // returns the number of bytes written. If nn < len(p), it also returns an
  173. // error explaining why the write is short.
  174. // NOTE: Writes are buffered so they don't write synchronously
  175. // TODO: Make it halt if space is unavailable
  176. func (g *Group) Write(p []byte) (nn int, err error) {
  177. g.mtx.Lock()
  178. defer g.mtx.Unlock()
  179. return g.headBuf.Write(p)
  180. }
  181. // WriteLine writes line into the current head of the group. It also appends "\n".
  182. // NOTE: Writes are buffered so they don't write synchronously
  183. // TODO: Make it halt if space is unavailable
  184. func (g *Group) WriteLine(line string) error {
  185. g.mtx.Lock()
  186. defer g.mtx.Unlock()
  187. _, err := g.headBuf.Write([]byte(line + "\n"))
  188. return err
  189. }
  190. // Buffered returns the size of the currently buffered data.
  191. func (g *Group) Buffered() int {
  192. g.mtx.Lock()
  193. defer g.mtx.Unlock()
  194. return g.headBuf.Buffered()
  195. }
  196. // FlushAndSync writes any buffered data to the underlying file and commits the
  197. // current content of the file to stable storage (fsync).
  198. func (g *Group) FlushAndSync() error {
  199. g.mtx.Lock()
  200. defer g.mtx.Unlock()
  201. err := g.headBuf.Flush()
  202. if err == nil {
  203. err = g.Head.Sync()
  204. }
  205. return err
  206. }
  207. func (g *Group) processTicks(ctx context.Context) {
  208. defer close(g.doneProcessTicks)
  209. for {
  210. select {
  211. case <-ctx.Done():
  212. return
  213. case <-g.ticker.C:
  214. g.checkHeadSizeLimit(ctx)
  215. g.checkTotalSizeLimit(ctx)
  216. }
  217. }
  218. }
  219. // NOTE: this function is called manually in tests.
  220. func (g *Group) checkHeadSizeLimit(ctx context.Context) {
  221. limit := g.HeadSizeLimit()
  222. if limit == 0 {
  223. return
  224. }
  225. size, err := g.Head.Size()
  226. if err != nil {
  227. g.logger.Error("Group's head may grow without bound", "head", g.Head.Path, "err", err)
  228. return
  229. }
  230. if size >= limit {
  231. g.rotateFile(ctx)
  232. }
  233. }
  234. func (g *Group) checkTotalSizeLimit(ctx context.Context) {
  235. g.mtx.Lock()
  236. defer g.mtx.Unlock()
  237. if g.totalSizeLimit == 0 {
  238. return
  239. }
  240. gInfo := g.readGroupInfo()
  241. totalSize := gInfo.TotalSize
  242. for i := 0; i < maxFilesToRemove; i++ {
  243. index := gInfo.MinIndex + i
  244. if totalSize < g.totalSizeLimit {
  245. return
  246. }
  247. if index == gInfo.MaxIndex {
  248. // Special degenerate case, just do nothing.
  249. g.logger.Error("Group's head may grow without bound", "head", g.Head.Path)
  250. return
  251. }
  252. pathToRemove := filePathForIndex(g.Head.Path, index, gInfo.MaxIndex)
  253. fInfo, err := os.Stat(pathToRemove)
  254. if err != nil {
  255. g.logger.Error("Failed to fetch info for file", "file", pathToRemove)
  256. continue
  257. }
  258. if ctx.Err() != nil {
  259. return
  260. }
  261. if err = os.Remove(pathToRemove); err != nil {
  262. g.logger.Error("Failed to remove path", "path", pathToRemove)
  263. return
  264. }
  265. totalSize -= fInfo.Size()
  266. }
  267. }
  268. // rotateFile causes group to close the current head and assign it some index.
  269. func (g *Group) rotateFile(ctx context.Context) {
  270. g.mtx.Lock()
  271. defer g.mtx.Unlock()
  272. headPath := g.Head.Path
  273. if err := g.headBuf.Flush(); err != nil {
  274. panic(err)
  275. }
  276. if err := g.Head.Sync(); err != nil {
  277. panic(err)
  278. }
  279. err := g.Head.withLock(func() error {
  280. if err := ctx.Err(); err != nil {
  281. return err
  282. }
  283. if err := g.Head.unsyncCloseFile(); err != nil {
  284. return err
  285. }
  286. indexPath := filePathForIndex(headPath, g.maxIndex, g.maxIndex+1)
  287. return os.Rename(headPath, indexPath)
  288. })
  289. if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
  290. return
  291. }
  292. if err != nil {
  293. panic(err)
  294. }
  295. g.maxIndex++
  296. }
  297. // NewReader returns a new group reader.
  298. // CONTRACT: Caller must close the returned GroupReader.
  299. func (g *Group) NewReader(index int) (*GroupReader, error) {
  300. r := newGroupReader(g)
  301. err := r.SetIndex(index)
  302. if err != nil {
  303. return nil, err
  304. }
  305. return r, nil
  306. }
  307. // GroupInfo holds information about the group.
  308. type GroupInfo struct {
  309. MinIndex int // index of the first file in the group, including head
  310. MaxIndex int // index of the last file in the group, including head
  311. TotalSize int64 // total size of the group
  312. HeadSize int64 // size of the head
  313. }
  314. // Returns info after scanning all files in g.Head's dir.
  315. func (g *Group) ReadGroupInfo() GroupInfo {
  316. g.mtx.Lock()
  317. defer g.mtx.Unlock()
  318. return g.readGroupInfo()
  319. }
  320. // Index includes the head.
  321. // CONTRACT: caller should have called g.mtx.Lock
  322. func (g *Group) readGroupInfo() GroupInfo {
  323. groupDir := filepath.Dir(g.Head.Path)
  324. headBase := filepath.Base(g.Head.Path)
  325. var minIndex, maxIndex int = -1, -1
  326. var totalSize, headSize int64 = 0, 0
  327. dir, err := os.Open(groupDir)
  328. if err != nil {
  329. panic(err)
  330. }
  331. defer dir.Close()
  332. fiz, err := dir.Readdir(0)
  333. if err != nil {
  334. panic(err)
  335. }
  336. // For each file in the directory, filter by pattern
  337. for _, fileInfo := range fiz {
  338. if fileInfo.Name() == headBase {
  339. fileSize := fileInfo.Size()
  340. totalSize += fileSize
  341. headSize = fileSize
  342. continue
  343. } else if strings.HasPrefix(fileInfo.Name(), headBase) {
  344. fileSize := fileInfo.Size()
  345. totalSize += fileSize
  346. indexedFilePattern := regexp.MustCompile(`^.+\.([0-9]{3,})$`)
  347. submatch := indexedFilePattern.FindSubmatch([]byte(fileInfo.Name()))
  348. if len(submatch) != 0 {
  349. // Matches
  350. fileIndex, err := strconv.Atoi(string(submatch[1]))
  351. if err != nil {
  352. panic(err)
  353. }
  354. if maxIndex < fileIndex {
  355. maxIndex = fileIndex
  356. }
  357. if minIndex == -1 || fileIndex < minIndex {
  358. minIndex = fileIndex
  359. }
  360. }
  361. }
  362. }
  363. // Now account for the head.
  364. if minIndex == -1 {
  365. // If there were no numbered files,
  366. // then the head is index 0.
  367. minIndex, maxIndex = 0, 0
  368. } else {
  369. // Otherwise, the head file is 1 greater
  370. maxIndex++
  371. }
  372. return GroupInfo{minIndex, maxIndex, totalSize, headSize}
  373. }
  374. func filePathForIndex(headPath string, index int, maxIndex int) string {
  375. if index == maxIndex {
  376. return headPath
  377. }
  378. return fmt.Sprintf("%v.%03d", headPath, index)
  379. }
  380. //--------------------------------------------------------------------------------
  381. // GroupReader provides an interface for reading from a Group.
  382. type GroupReader struct {
  383. *Group
  384. mtx sync.Mutex
  385. curIndex int
  386. curFile *os.File
  387. curReader *bufio.Reader
  388. curLine []byte
  389. }
  390. func newGroupReader(g *Group) *GroupReader {
  391. return &GroupReader{
  392. Group: g,
  393. curIndex: 0,
  394. curFile: nil,
  395. curReader: nil,
  396. curLine: nil,
  397. }
  398. }
  399. // Close closes the GroupReader by closing the cursor file.
  400. func (gr *GroupReader) Close() error {
  401. gr.mtx.Lock()
  402. defer gr.mtx.Unlock()
  403. if gr.curReader != nil {
  404. err := gr.curFile.Close()
  405. gr.curIndex = 0
  406. gr.curReader = nil
  407. gr.curFile = nil
  408. gr.curLine = nil
  409. return err
  410. }
  411. return nil
  412. }
  413. // Read implements io.Reader, reading bytes from the current Reader
  414. // incrementing index until enough bytes are read.
  415. func (gr *GroupReader) Read(p []byte) (n int, err error) {
  416. lenP := len(p)
  417. if lenP == 0 {
  418. return 0, errors.New("given empty slice")
  419. }
  420. gr.mtx.Lock()
  421. defer gr.mtx.Unlock()
  422. // Open file if not open yet
  423. if gr.curReader == nil {
  424. if err = gr.openFile(gr.curIndex); err != nil {
  425. return 0, err
  426. }
  427. }
  428. // Iterate over files until enough bytes are read
  429. var nn int
  430. for {
  431. nn, err = gr.curReader.Read(p[n:])
  432. n += nn
  433. switch {
  434. case err == io.EOF:
  435. if n >= lenP {
  436. return n, nil
  437. }
  438. // Open the next file
  439. if err1 := gr.openFile(gr.curIndex + 1); err1 != nil {
  440. return n, err1
  441. }
  442. case err != nil:
  443. return n, err
  444. case nn == 0: // empty file
  445. return n, err
  446. }
  447. }
  448. }
  449. // IF index > gr.Group.maxIndex, returns io.EOF
  450. // CONTRACT: caller should hold gr.mtx
  451. func (gr *GroupReader) openFile(index int) error {
  452. // Lock on Group to ensure that head doesn't move in the meanwhile.
  453. gr.Group.mtx.Lock()
  454. defer gr.Group.mtx.Unlock()
  455. if index > gr.Group.maxIndex {
  456. return io.EOF
  457. }
  458. curFilePath := filePathForIndex(gr.Head.Path, index, gr.Group.maxIndex)
  459. curFile, err := os.OpenFile(curFilePath, os.O_RDONLY|os.O_CREATE, autoFilePerms)
  460. if err != nil {
  461. return err
  462. }
  463. curReader := bufio.NewReader(curFile)
  464. // Update gr.cur*
  465. if gr.curFile != nil {
  466. gr.curFile.Close() // TODO return error?
  467. }
  468. gr.curIndex = index
  469. gr.curFile = curFile
  470. gr.curReader = curReader
  471. gr.curLine = nil
  472. return nil
  473. }
  474. // CurIndex returns cursor's file index.
  475. func (gr *GroupReader) CurIndex() int {
  476. gr.mtx.Lock()
  477. defer gr.mtx.Unlock()
  478. return gr.curIndex
  479. }
  480. // SetIndex sets the cursor's file index to index by opening a file at this
  481. // position.
  482. func (gr *GroupReader) SetIndex(index int) error {
  483. gr.mtx.Lock()
  484. defer gr.mtx.Unlock()
  485. return gr.openFile(index)
  486. }