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.

540 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
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. "errors"
  5. "fmt"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. "github.com/tendermint/tendermint/libs/service"
  15. )
  16. const (
  17. defaultGroupCheckDuration = 5000 * time.Millisecond
  18. defaultHeadSizeLimit = 10 * 1024 * 1024 // 10MB
  19. defaultTotalSizeLimit = 1 * 1024 * 1024 * 1024 // 1GB
  20. maxFilesToRemove = 4 // needs to be greater than 1
  21. )
  22. /*
  23. You can open a Group to keep restrictions on an AutoFile, like
  24. the maximum size of each chunk, and/or the total amount of bytes
  25. stored in the group.
  26. The first file to be written in the Group.Dir is the head file.
  27. Dir/
  28. - <HeadPath>
  29. Once the Head file reaches the size limit, it will be rotated.
  30. Dir/
  31. - <HeadPath>.000 // First rolled file
  32. - <HeadPath> // New head path, starts empty.
  33. // The implicit index is 001.
  34. As more files are written, the index numbers grow...
  35. Dir/
  36. - <HeadPath>.000 // First rolled file
  37. - <HeadPath>.001 // Second rolled file
  38. - ...
  39. - <HeadPath> // New head path
  40. The Group can also be used to binary-search for some line,
  41. assuming that marker lines are written occasionally.
  42. */
  43. type Group struct {
  44. service.BaseService
  45. ID string
  46. Head *AutoFile // The head AutoFile to write to
  47. headBuf *bufio.Writer
  48. Dir string // Directory that contains .Head
  49. ticker *time.Ticker
  50. mtx sync.Mutex
  51. headSizeLimit int64
  52. totalSizeLimit int64
  53. groupCheckDuration time.Duration
  54. minIndex int // Includes head
  55. maxIndex int // Includes head, where Head will move to
  56. // close this when the processTicks routine is done.
  57. // this ensures we can cleanup the dir after calling Stop
  58. // and the routine won't be trying to access it anymore
  59. doneProcessTicks chan struct{}
  60. // TODO: When we start deleting files, we need to start tracking GroupReaders
  61. // and their dependencies.
  62. }
  63. // OpenGroup creates a new Group with head at headPath. It returns an error if
  64. // it fails to open head file.
  65. func OpenGroup(headPath string, groupOptions ...func(*Group)) (*Group, error) {
  66. dir, err := filepath.Abs(filepath.Dir(headPath))
  67. if err != nil {
  68. return nil, err
  69. }
  70. head, err := OpenAutoFile(headPath)
  71. if err != nil {
  72. return nil, err
  73. }
  74. g := &Group{
  75. ID: "group:" + head.ID,
  76. Head: head,
  77. headBuf: bufio.NewWriterSize(head, 4096*10),
  78. Dir: dir,
  79. headSizeLimit: defaultHeadSizeLimit,
  80. totalSizeLimit: defaultTotalSizeLimit,
  81. groupCheckDuration: defaultGroupCheckDuration,
  82. minIndex: 0,
  83. maxIndex: 0,
  84. doneProcessTicks: make(chan struct{}),
  85. }
  86. for _, option := range groupOptions {
  87. option(g)
  88. }
  89. g.BaseService = *service.NewBaseService(nil, "Group", g)
  90. gInfo := g.readGroupInfo()
  91. g.minIndex = gInfo.MinIndex
  92. g.maxIndex = gInfo.MaxIndex
  93. return g, nil
  94. }
  95. // GroupCheckDuration allows you to overwrite default groupCheckDuration.
  96. func GroupCheckDuration(duration time.Duration) func(*Group) {
  97. return func(g *Group) {
  98. g.groupCheckDuration = duration
  99. }
  100. }
  101. // GroupHeadSizeLimit allows you to overwrite default head size limit - 10MB.
  102. func GroupHeadSizeLimit(limit int64) func(*Group) {
  103. return func(g *Group) {
  104. g.headSizeLimit = limit
  105. }
  106. }
  107. // GroupTotalSizeLimit allows you to overwrite default total size limit of the group - 1GB.
  108. func GroupTotalSizeLimit(limit int64) func(*Group) {
  109. return func(g *Group) {
  110. g.totalSizeLimit = limit
  111. }
  112. }
  113. // OnStart implements service.Service by starting the goroutine that checks file
  114. // and group limits.
  115. func (g *Group) OnStart() error {
  116. g.ticker = time.NewTicker(g.groupCheckDuration)
  117. go g.processTicks()
  118. return nil
  119. }
  120. // OnStop implements service.Service by stopping the goroutine described above.
  121. // NOTE: g.Head must be closed separately using Close.
  122. func (g *Group) OnStop() {
  123. g.ticker.Stop()
  124. if err := g.FlushAndSync(); err != nil {
  125. g.Logger.Error("Error flushin to disk", "err", err)
  126. }
  127. }
  128. // Wait blocks until all internal goroutines are finished. Supposed to be
  129. // called after Stop.
  130. func (g *Group) Wait() {
  131. // wait for processTicks routine to finish
  132. <-g.doneProcessTicks
  133. }
  134. // Close closes the head file. The group must be stopped by this moment.
  135. func (g *Group) Close() {
  136. if err := g.FlushAndSync(); err != nil {
  137. g.Logger.Error("Error flushin to disk", "err", err)
  138. }
  139. g.mtx.Lock()
  140. _ = g.Head.closeFile()
  141. g.mtx.Unlock()
  142. }
  143. // HeadSizeLimit returns the current head size limit.
  144. func (g *Group) HeadSizeLimit() int64 {
  145. g.mtx.Lock()
  146. defer g.mtx.Unlock()
  147. return g.headSizeLimit
  148. }
  149. // TotalSizeLimit returns total size limit of the group.
  150. func (g *Group) TotalSizeLimit() int64 {
  151. g.mtx.Lock()
  152. defer g.mtx.Unlock()
  153. return g.totalSizeLimit
  154. }
  155. // MaxIndex returns index of the last file in the group.
  156. func (g *Group) MaxIndex() int {
  157. g.mtx.Lock()
  158. defer g.mtx.Unlock()
  159. return g.maxIndex
  160. }
  161. // MinIndex returns index of the first file in the group.
  162. func (g *Group) MinIndex() int {
  163. g.mtx.Lock()
  164. defer g.mtx.Unlock()
  165. return g.minIndex
  166. }
  167. // Write writes the contents of p into the current head of the group. It
  168. // returns the number of bytes written. If nn < len(p), it also returns an
  169. // error explaining why the write is short.
  170. // NOTE: Writes are buffered so they don't write synchronously
  171. // TODO: Make it halt if space is unavailable
  172. func (g *Group) Write(p []byte) (nn int, err error) {
  173. g.mtx.Lock()
  174. defer g.mtx.Unlock()
  175. return g.headBuf.Write(p)
  176. }
  177. // WriteLine writes line into the current head of the group. It also appends "\n".
  178. // NOTE: Writes are buffered so they don't write synchronously
  179. // TODO: Make it halt if space is unavailable
  180. func (g *Group) WriteLine(line string) error {
  181. g.mtx.Lock()
  182. defer g.mtx.Unlock()
  183. _, err := g.headBuf.Write([]byte(line + "\n"))
  184. return err
  185. }
  186. // Buffered returns the size of the currently buffered data.
  187. func (g *Group) Buffered() int {
  188. g.mtx.Lock()
  189. defer g.mtx.Unlock()
  190. return g.headBuf.Buffered()
  191. }
  192. // FlushAndSync writes any buffered data to the underlying file and commits the
  193. // current content of the file to stable storage (fsync).
  194. func (g *Group) FlushAndSync() error {
  195. g.mtx.Lock()
  196. defer g.mtx.Unlock()
  197. err := g.headBuf.Flush()
  198. if err == nil {
  199. err = g.Head.Sync()
  200. }
  201. return err
  202. }
  203. func (g *Group) processTicks() {
  204. defer close(g.doneProcessTicks)
  205. for {
  206. select {
  207. case <-g.ticker.C:
  208. g.checkHeadSizeLimit()
  209. g.checkTotalSizeLimit()
  210. case <-g.Quit():
  211. return
  212. }
  213. }
  214. }
  215. // NOTE: this function is called manually in tests.
  216. func (g *Group) checkHeadSizeLimit() {
  217. limit := g.HeadSizeLimit()
  218. if limit == 0 {
  219. return
  220. }
  221. size, err := g.Head.Size()
  222. if err != nil {
  223. g.Logger.Error("Group's head may grow without bound", "head", g.Head.Path, "err", err)
  224. return
  225. }
  226. if size >= limit {
  227. g.RotateFile()
  228. }
  229. }
  230. func (g *Group) checkTotalSizeLimit() {
  231. limit := g.TotalSizeLimit()
  232. if limit == 0 {
  233. return
  234. }
  235. gInfo := g.readGroupInfo()
  236. totalSize := gInfo.TotalSize
  237. for i := 0; i < maxFilesToRemove; i++ {
  238. index := gInfo.MinIndex + i
  239. if totalSize < limit {
  240. return
  241. }
  242. if index == gInfo.MaxIndex {
  243. // Special degenerate case, just do nothing.
  244. g.Logger.Error("Group's head may grow without bound", "head", g.Head.Path)
  245. return
  246. }
  247. pathToRemove := filePathForIndex(g.Head.Path, index, gInfo.MaxIndex)
  248. fInfo, err := os.Stat(pathToRemove)
  249. if err != nil {
  250. g.Logger.Error("Failed to fetch info for file", "file", pathToRemove)
  251. continue
  252. }
  253. err = os.Remove(pathToRemove)
  254. if err != nil {
  255. g.Logger.Error("Failed to remove path", "path", pathToRemove)
  256. return
  257. }
  258. totalSize -= fInfo.Size()
  259. }
  260. }
  261. // RotateFile causes group to close the current head and assign it some index.
  262. // Note it does not create a new head.
  263. func (g *Group) RotateFile() {
  264. g.mtx.Lock()
  265. defer g.mtx.Unlock()
  266. headPath := g.Head.Path
  267. if err := g.headBuf.Flush(); err != nil {
  268. panic(err)
  269. }
  270. if err := g.Head.Sync(); err != nil {
  271. panic(err)
  272. }
  273. if err := g.Head.closeFile(); err != nil {
  274. panic(err)
  275. }
  276. indexPath := filePathForIndex(headPath, g.maxIndex, g.maxIndex+1)
  277. if err := os.Rename(headPath, indexPath); err != nil {
  278. panic(err)
  279. }
  280. g.maxIndex++
  281. }
  282. // NewReader returns a new group reader.
  283. // CONTRACT: Caller must close the returned GroupReader.
  284. func (g *Group) NewReader(index int) (*GroupReader, error) {
  285. r := newGroupReader(g)
  286. err := r.SetIndex(index)
  287. if err != nil {
  288. return nil, err
  289. }
  290. return r, nil
  291. }
  292. // GroupInfo holds information about the group.
  293. type GroupInfo struct {
  294. MinIndex int // index of the first file in the group, including head
  295. MaxIndex int // index of the last file in the group, including head
  296. TotalSize int64 // total size of the group
  297. HeadSize int64 // size of the head
  298. }
  299. // Returns info after scanning all files in g.Head's dir.
  300. func (g *Group) ReadGroupInfo() GroupInfo {
  301. g.mtx.Lock()
  302. defer g.mtx.Unlock()
  303. return g.readGroupInfo()
  304. }
  305. // Index includes the head.
  306. // CONTRACT: caller should have called g.mtx.Lock
  307. func (g *Group) readGroupInfo() GroupInfo {
  308. groupDir := filepath.Dir(g.Head.Path)
  309. headBase := filepath.Base(g.Head.Path)
  310. var minIndex, maxIndex int = -1, -1
  311. var totalSize, headSize int64 = 0, 0
  312. dir, err := os.Open(groupDir)
  313. if err != nil {
  314. panic(err)
  315. }
  316. defer dir.Close()
  317. fiz, err := dir.Readdir(0)
  318. if err != nil {
  319. panic(err)
  320. }
  321. // For each file in the directory, filter by pattern
  322. for _, fileInfo := range fiz {
  323. if fileInfo.Name() == headBase {
  324. fileSize := fileInfo.Size()
  325. totalSize += fileSize
  326. headSize = fileSize
  327. continue
  328. } else if strings.HasPrefix(fileInfo.Name(), headBase) {
  329. fileSize := fileInfo.Size()
  330. totalSize += fileSize
  331. indexedFilePattern := regexp.MustCompile(`^.+\.([0-9]{3,})$`)
  332. submatch := indexedFilePattern.FindSubmatch([]byte(fileInfo.Name()))
  333. if len(submatch) != 0 {
  334. // Matches
  335. fileIndex, err := strconv.Atoi(string(submatch[1]))
  336. if err != nil {
  337. panic(err)
  338. }
  339. if maxIndex < fileIndex {
  340. maxIndex = fileIndex
  341. }
  342. if minIndex == -1 || fileIndex < minIndex {
  343. minIndex = fileIndex
  344. }
  345. }
  346. }
  347. }
  348. // Now account for the head.
  349. if minIndex == -1 {
  350. // If there were no numbered files,
  351. // then the head is index 0.
  352. minIndex, maxIndex = 0, 0
  353. } else {
  354. // Otherwise, the head file is 1 greater
  355. maxIndex++
  356. }
  357. return GroupInfo{minIndex, maxIndex, totalSize, headSize}
  358. }
  359. func filePathForIndex(headPath string, index int, maxIndex int) string {
  360. if index == maxIndex {
  361. return headPath
  362. }
  363. return fmt.Sprintf("%v.%03d", headPath, index)
  364. }
  365. //--------------------------------------------------------------------------------
  366. // GroupReader provides an interface for reading from a Group.
  367. type GroupReader struct {
  368. *Group
  369. mtx sync.Mutex
  370. curIndex int
  371. curFile *os.File
  372. curReader *bufio.Reader
  373. curLine []byte
  374. }
  375. func newGroupReader(g *Group) *GroupReader {
  376. return &GroupReader{
  377. Group: g,
  378. curIndex: 0,
  379. curFile: nil,
  380. curReader: nil,
  381. curLine: nil,
  382. }
  383. }
  384. // Close closes the GroupReader by closing the cursor file.
  385. func (gr *GroupReader) Close() error {
  386. gr.mtx.Lock()
  387. defer gr.mtx.Unlock()
  388. if gr.curReader != nil {
  389. err := gr.curFile.Close()
  390. gr.curIndex = 0
  391. gr.curReader = nil
  392. gr.curFile = nil
  393. gr.curLine = nil
  394. return err
  395. }
  396. return nil
  397. }
  398. // Read implements io.Reader, reading bytes from the current Reader
  399. // incrementing index until enough bytes are read.
  400. func (gr *GroupReader) Read(p []byte) (n int, err error) {
  401. lenP := len(p)
  402. if lenP == 0 {
  403. return 0, errors.New("given empty slice")
  404. }
  405. gr.mtx.Lock()
  406. defer gr.mtx.Unlock()
  407. // Open file if not open yet
  408. if gr.curReader == nil {
  409. if err = gr.openFile(gr.curIndex); err != nil {
  410. return 0, err
  411. }
  412. }
  413. // Iterate over files until enough bytes are read
  414. var nn int
  415. for {
  416. nn, err = gr.curReader.Read(p[n:])
  417. n += nn
  418. switch {
  419. case err == io.EOF:
  420. if n >= lenP {
  421. return n, nil
  422. }
  423. // Open the next file
  424. if err1 := gr.openFile(gr.curIndex + 1); err1 != nil {
  425. return n, err1
  426. }
  427. case err != nil:
  428. return n, err
  429. case nn == 0: // empty file
  430. return n, err
  431. }
  432. }
  433. }
  434. // IF index > gr.Group.maxIndex, returns io.EOF
  435. // CONTRACT: caller should hold gr.mtx
  436. func (gr *GroupReader) openFile(index int) error {
  437. // Lock on Group to ensure that head doesn't move in the meanwhile.
  438. gr.Group.mtx.Lock()
  439. defer gr.Group.mtx.Unlock()
  440. if index > gr.Group.maxIndex {
  441. return io.EOF
  442. }
  443. curFilePath := filePathForIndex(gr.Head.Path, index, gr.Group.maxIndex)
  444. curFile, err := os.OpenFile(curFilePath, os.O_RDONLY|os.O_CREATE, autoFilePerms)
  445. if err != nil {
  446. return err
  447. }
  448. curReader := bufio.NewReader(curFile)
  449. // Update gr.cur*
  450. if gr.curFile != nil {
  451. gr.curFile.Close() // TODO return error?
  452. }
  453. gr.curIndex = index
  454. gr.curFile = curFile
  455. gr.curReader = curReader
  456. gr.curLine = nil
  457. return nil
  458. }
  459. // CurIndex returns cursor's file index.
  460. func (gr *GroupReader) CurIndex() int {
  461. gr.mtx.Lock()
  462. defer gr.mtx.Unlock()
  463. return gr.curIndex
  464. }
  465. // SetIndex sets the cursor's file index to index by opening a file at this
  466. // position.
  467. func (gr *GroupReader) SetIndex(index int) error {
  468. gr.mtx.Lock()
  469. defer gr.mtx.Unlock()
  470. return gr.openFile(index)
  471. }