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.

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