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.

572 lines
14 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 err := ctx.Err(); err != nil {
  238. return
  239. }
  240. if g.totalSizeLimit == 0 {
  241. return
  242. }
  243. gInfo := g.readGroupInfo()
  244. totalSize := gInfo.TotalSize
  245. for i := 0; i < maxFilesToRemove; i++ {
  246. index := gInfo.MinIndex + i
  247. if totalSize < g.totalSizeLimit {
  248. return
  249. }
  250. if index == gInfo.MaxIndex {
  251. // Special degenerate case, just do nothing.
  252. g.logger.Error("Group's head may grow without bound", "head", g.Head.Path)
  253. return
  254. }
  255. if ctx.Err() != nil {
  256. return
  257. }
  258. pathToRemove := filePathForIndex(g.Head.Path, index, gInfo.MaxIndex)
  259. fInfo, err := os.Stat(pathToRemove)
  260. if err != nil {
  261. g.logger.Error("Failed to fetch info for file", "file", pathToRemove)
  262. continue
  263. }
  264. if ctx.Err() != nil {
  265. return
  266. }
  267. if err = os.Remove(pathToRemove); err != nil {
  268. g.logger.Error("Failed to remove path", "path", pathToRemove)
  269. return
  270. }
  271. totalSize -= fInfo.Size()
  272. }
  273. }
  274. // rotateFile causes group to close the current head and assign it
  275. // some index. Panics if it encounters an error.
  276. func (g *Group) rotateFile(ctx context.Context) {
  277. g.mtx.Lock()
  278. defer g.mtx.Unlock()
  279. if err := ctx.Err(); err != nil {
  280. return
  281. }
  282. headPath := g.Head.Path
  283. if err := g.headBuf.Flush(); err != nil {
  284. panic(err)
  285. }
  286. if err := g.Head.Sync(); err != nil {
  287. panic(err)
  288. }
  289. err := g.Head.withLock(func() error {
  290. if err := ctx.Err(); err != nil {
  291. return err
  292. }
  293. if err := g.Head.unsyncCloseFile(); err != nil {
  294. return err
  295. }
  296. indexPath := filePathForIndex(headPath, g.maxIndex, g.maxIndex+1)
  297. return os.Rename(headPath, indexPath)
  298. })
  299. if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
  300. return
  301. }
  302. if err != nil {
  303. panic(err)
  304. }
  305. g.maxIndex++
  306. }
  307. // NewReader returns a new group reader.
  308. // CONTRACT: Caller must close the returned GroupReader.
  309. func (g *Group) NewReader(index int) (*GroupReader, error) {
  310. r := newGroupReader(g)
  311. err := r.SetIndex(index)
  312. if err != nil {
  313. return nil, err
  314. }
  315. return r, nil
  316. }
  317. // GroupInfo holds information about the group.
  318. type GroupInfo struct {
  319. MinIndex int // index of the first file in the group, including head
  320. MaxIndex int // index of the last file in the group, including head
  321. TotalSize int64 // total size of the group
  322. HeadSize int64 // size of the head
  323. }
  324. // Returns info after scanning all files in g.Head's dir.
  325. func (g *Group) ReadGroupInfo() GroupInfo {
  326. g.mtx.Lock()
  327. defer g.mtx.Unlock()
  328. return g.readGroupInfo()
  329. }
  330. // Index includes the head.
  331. // CONTRACT: caller should have called g.mtx.Lock
  332. func (g *Group) readGroupInfo() GroupInfo {
  333. groupDir := filepath.Dir(g.Head.Path)
  334. headBase := filepath.Base(g.Head.Path)
  335. var minIndex, maxIndex int = -1, -1
  336. var totalSize, headSize int64 = 0, 0
  337. dir, err := os.Open(groupDir)
  338. if err != nil {
  339. panic(err)
  340. }
  341. defer dir.Close()
  342. fiz, err := dir.Readdir(0)
  343. if err != nil {
  344. panic(err)
  345. }
  346. // For each file in the directory, filter by pattern
  347. for _, fileInfo := range fiz {
  348. if fileInfo.Name() == headBase {
  349. fileSize := fileInfo.Size()
  350. totalSize += fileSize
  351. headSize = fileSize
  352. continue
  353. } else if strings.HasPrefix(fileInfo.Name(), headBase) {
  354. fileSize := fileInfo.Size()
  355. totalSize += fileSize
  356. indexedFilePattern := regexp.MustCompile(`^.+\.([0-9]{3,})$`)
  357. submatch := indexedFilePattern.FindSubmatch([]byte(fileInfo.Name()))
  358. if len(submatch) != 0 {
  359. // Matches
  360. fileIndex, err := strconv.Atoi(string(submatch[1]))
  361. if err != nil {
  362. panic(err)
  363. }
  364. if maxIndex < fileIndex {
  365. maxIndex = fileIndex
  366. }
  367. if minIndex == -1 || fileIndex < minIndex {
  368. minIndex = fileIndex
  369. }
  370. }
  371. }
  372. }
  373. // Now account for the head.
  374. if minIndex == -1 {
  375. // If there were no numbered files,
  376. // then the head is index 0.
  377. minIndex, maxIndex = 0, 0
  378. } else {
  379. // Otherwise, the head file is 1 greater
  380. maxIndex++
  381. }
  382. return GroupInfo{minIndex, maxIndex, totalSize, headSize}
  383. }
  384. func filePathForIndex(headPath string, index int, maxIndex int) string {
  385. if index == maxIndex {
  386. return headPath
  387. }
  388. return fmt.Sprintf("%v.%03d", headPath, index)
  389. }
  390. //--------------------------------------------------------------------------------
  391. // GroupReader provides an interface for reading from a Group.
  392. type GroupReader struct {
  393. *Group
  394. mtx sync.Mutex
  395. curIndex int
  396. curFile *os.File
  397. curReader *bufio.Reader
  398. curLine []byte
  399. }
  400. func newGroupReader(g *Group) *GroupReader {
  401. return &GroupReader{
  402. Group: g,
  403. curIndex: 0,
  404. curFile: nil,
  405. curReader: nil,
  406. curLine: nil,
  407. }
  408. }
  409. // Close closes the GroupReader by closing the cursor file.
  410. func (gr *GroupReader) Close() error {
  411. gr.mtx.Lock()
  412. defer gr.mtx.Unlock()
  413. if gr.curReader != nil {
  414. err := gr.curFile.Close()
  415. gr.curIndex = 0
  416. gr.curReader = nil
  417. gr.curFile = nil
  418. gr.curLine = nil
  419. return err
  420. }
  421. return nil
  422. }
  423. // Read implements io.Reader, reading bytes from the current Reader
  424. // incrementing index until enough bytes are read.
  425. func (gr *GroupReader) Read(p []byte) (n int, err error) {
  426. lenP := len(p)
  427. if lenP == 0 {
  428. return 0, errors.New("given empty slice")
  429. }
  430. gr.mtx.Lock()
  431. defer gr.mtx.Unlock()
  432. // Open file if not open yet
  433. if gr.curReader == nil {
  434. if err = gr.openFile(gr.curIndex); err != nil {
  435. return 0, err
  436. }
  437. }
  438. // Iterate over files until enough bytes are read
  439. var nn int
  440. for {
  441. nn, err = gr.curReader.Read(p[n:])
  442. n += nn
  443. switch {
  444. case err == io.EOF:
  445. if n >= lenP {
  446. return n, nil
  447. }
  448. // Open the next file
  449. if err1 := gr.openFile(gr.curIndex + 1); err1 != nil {
  450. return n, err1
  451. }
  452. case err != nil:
  453. return n, err
  454. case nn == 0: // empty file
  455. return n, err
  456. }
  457. }
  458. }
  459. // IF index > gr.Group.maxIndex, returns io.EOF
  460. // CONTRACT: caller should hold gr.mtx
  461. func (gr *GroupReader) openFile(index int) error {
  462. // Lock on Group to ensure that head doesn't move in the meanwhile.
  463. gr.Group.mtx.Lock()
  464. defer gr.Group.mtx.Unlock()
  465. if index > gr.Group.maxIndex {
  466. return io.EOF
  467. }
  468. curFilePath := filePathForIndex(gr.Head.Path, index, gr.Group.maxIndex)
  469. curFile, err := os.OpenFile(curFilePath, os.O_RDONLY|os.O_CREATE, autoFilePerms)
  470. if err != nil {
  471. return err
  472. }
  473. curReader := bufio.NewReader(curFile)
  474. // Update gr.cur*
  475. if gr.curFile != nil {
  476. gr.curFile.Close() // TODO return error?
  477. }
  478. gr.curIndex = index
  479. gr.curFile = curFile
  480. gr.curReader = curReader
  481. gr.curLine = nil
  482. return nil
  483. }
  484. // CurIndex returns cursor's file index.
  485. func (gr *GroupReader) CurIndex() int {
  486. gr.mtx.Lock()
  487. defer gr.mtx.Unlock()
  488. return gr.curIndex
  489. }
  490. // SetIndex sets the cursor's file index to index by opening a file at this
  491. // position.
  492. func (gr *GroupReader) SetIndex(index int) error {
  493. gr.mtx.Lock()
  494. defer gr.mtx.Unlock()
  495. return gr.openFile(index)
  496. }