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.

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