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.

782 lines
18 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
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
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 Service by starting the goroutine that checks file and
  112. // 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 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.Flush() // flush any uncommitted data
  123. }
  124. func (g *Group) Wait() {
  125. // wait for processTicks routine to finish
  126. <-g.doneProcessTicks
  127. }
  128. // Close closes the head file. The group must be stopped by this moment.
  129. func (g *Group) Close() {
  130. g.Flush() // flush any uncommitted data
  131. g.mtx.Lock()
  132. _ = g.Head.closeFile()
  133. g.mtx.Unlock()
  134. }
  135. // HeadSizeLimit returns the current head size limit.
  136. func (g *Group) HeadSizeLimit() int64 {
  137. g.mtx.Lock()
  138. defer g.mtx.Unlock()
  139. return g.headSizeLimit
  140. }
  141. // TotalSizeLimit returns total size limit of the group.
  142. func (g *Group) TotalSizeLimit() int64 {
  143. g.mtx.Lock()
  144. defer g.mtx.Unlock()
  145. return g.totalSizeLimit
  146. }
  147. // MaxIndex returns index of the last file in the group.
  148. func (g *Group) MaxIndex() int {
  149. g.mtx.Lock()
  150. defer g.mtx.Unlock()
  151. return g.maxIndex
  152. }
  153. // MinIndex returns index of the first file in the group.
  154. func (g *Group) MinIndex() int {
  155. g.mtx.Lock()
  156. defer g.mtx.Unlock()
  157. return g.minIndex
  158. }
  159. // Write writes the contents of p into the current head of the group. It
  160. // returns the number of bytes written. If nn < len(p), it also returns an
  161. // error explaining why the write is short.
  162. // NOTE: Writes are buffered so they don't write synchronously
  163. // TODO: Make it halt if space is unavailable
  164. func (g *Group) Write(p []byte) (nn int, err error) {
  165. g.mtx.Lock()
  166. defer g.mtx.Unlock()
  167. return g.headBuf.Write(p)
  168. }
  169. // WriteLine writes line into the current head of the group. It also appends "\n".
  170. // NOTE: Writes are buffered so they don't write synchronously
  171. // TODO: Make it halt if space is unavailable
  172. func (g *Group) WriteLine(line string) error {
  173. g.mtx.Lock()
  174. defer g.mtx.Unlock()
  175. _, err := g.headBuf.Write([]byte(line + "\n"))
  176. return err
  177. }
  178. // Buffered returns the size of the currently buffered data.
  179. func (g *Group) Buffered() int {
  180. g.mtx.Lock()
  181. defer g.mtx.Unlock()
  182. return g.headBuf.Buffered()
  183. }
  184. // Flush writes any buffered data to the underlying file and commits the
  185. // current content of the file to stable storage.
  186. func (g *Group) Flush() error {
  187. g.mtx.Lock()
  188. defer g.mtx.Unlock()
  189. err := g.headBuf.Flush()
  190. if err == nil {
  191. err = g.Head.Sync()
  192. }
  193. return err
  194. }
  195. func (g *Group) processTicks() {
  196. defer close(g.doneProcessTicks)
  197. for {
  198. select {
  199. case <-g.ticker.C:
  200. g.checkHeadSizeLimit()
  201. g.checkTotalSizeLimit()
  202. case <-g.Quit():
  203. return
  204. }
  205. }
  206. }
  207. // NOTE: this function is called manually in tests.
  208. func (g *Group) checkHeadSizeLimit() {
  209. limit := g.HeadSizeLimit()
  210. if limit == 0 {
  211. return
  212. }
  213. size, err := g.Head.Size()
  214. if err != nil {
  215. g.Logger.Error("Group's head may grow without bound", "head", g.Head.Path, "err", err)
  216. return
  217. }
  218. if size >= limit {
  219. g.RotateFile()
  220. }
  221. }
  222. func (g *Group) checkTotalSizeLimit() {
  223. limit := g.TotalSizeLimit()
  224. if limit == 0 {
  225. return
  226. }
  227. gInfo := g.readGroupInfo()
  228. totalSize := gInfo.TotalSize
  229. for i := 0; i < maxFilesToRemove; i++ {
  230. index := gInfo.MinIndex + i
  231. if totalSize < limit {
  232. return
  233. }
  234. if index == gInfo.MaxIndex {
  235. // Special degenerate case, just do nothing.
  236. g.Logger.Error("Group's head may grow without bound", "head", g.Head.Path)
  237. return
  238. }
  239. pathToRemove := filePathForIndex(g.Head.Path, index, gInfo.MaxIndex)
  240. fInfo, err := os.Stat(pathToRemove)
  241. if err != nil {
  242. g.Logger.Error("Failed to fetch info for file", "file", pathToRemove)
  243. continue
  244. }
  245. err = os.Remove(pathToRemove)
  246. if err != nil {
  247. g.Logger.Error("Failed to remove path", "path", pathToRemove)
  248. return
  249. }
  250. totalSize -= fInfo.Size()
  251. }
  252. }
  253. // RotateFile causes group to close the current head and assign it some index.
  254. // Note it does not create a new head.
  255. func (g *Group) RotateFile() {
  256. g.mtx.Lock()
  257. defer g.mtx.Unlock()
  258. headPath := g.Head.Path
  259. if err := g.headBuf.Flush(); err != nil {
  260. panic(err)
  261. }
  262. if err := g.Head.Sync(); err != nil {
  263. panic(err)
  264. }
  265. if err := g.Head.closeFile(); err != nil {
  266. panic(err)
  267. }
  268. indexPath := filePathForIndex(headPath, g.maxIndex, g.maxIndex+1)
  269. if err := os.Rename(headPath, indexPath); err != nil {
  270. panic(err)
  271. }
  272. g.maxIndex++
  273. }
  274. // NewReader returns a new group reader.
  275. // CONTRACT: Caller must close the returned GroupReader.
  276. func (g *Group) NewReader(index int) (*GroupReader, error) {
  277. r := newGroupReader(g)
  278. err := r.SetIndex(index)
  279. if err != nil {
  280. return nil, err
  281. }
  282. return r, nil
  283. }
  284. // Returns -1 if line comes after, 0 if found, 1 if line comes before.
  285. type SearchFunc func(line string) (int, error)
  286. // Searches for the right file in Group, then returns a GroupReader to start
  287. // streaming lines.
  288. // Returns true if an exact match was found, otherwise returns the next greater
  289. // line that starts with prefix.
  290. // CONTRACT: Caller must close the returned GroupReader
  291. func (g *Group) Search(prefix string, cmp SearchFunc) (*GroupReader, bool, error) {
  292. g.mtx.Lock()
  293. minIndex, maxIndex := g.minIndex, g.maxIndex
  294. g.mtx.Unlock()
  295. // Now minIndex/maxIndex may change meanwhile,
  296. // but it shouldn't be a big deal
  297. // (maybe we'll want to limit scanUntil though)
  298. for {
  299. curIndex := (minIndex + maxIndex + 1) / 2
  300. // Base case, when there's only 1 choice left.
  301. if minIndex == maxIndex {
  302. r, err := g.NewReader(maxIndex)
  303. if err != nil {
  304. return nil, false, err
  305. }
  306. match, err := scanUntil(r, prefix, cmp)
  307. if err != nil {
  308. r.Close()
  309. return nil, false, err
  310. }
  311. return r, match, err
  312. }
  313. // Read starting roughly at the middle file,
  314. // until we find line that has prefix.
  315. r, err := g.NewReader(curIndex)
  316. if err != nil {
  317. return nil, false, err
  318. }
  319. foundIndex, line, err := scanNext(r, prefix)
  320. r.Close()
  321. if err != nil {
  322. return nil, false, err
  323. }
  324. // Compare this line to our search query.
  325. val, err := cmp(line)
  326. if err != nil {
  327. return nil, false, err
  328. }
  329. if val < 0 {
  330. // Line will come later
  331. minIndex = foundIndex
  332. } else if val == 0 {
  333. // Stroke of luck, found the line
  334. r, err := g.NewReader(foundIndex)
  335. if err != nil {
  336. return nil, false, err
  337. }
  338. match, err := scanUntil(r, prefix, cmp)
  339. if !match {
  340. panic("Expected match to be true")
  341. }
  342. if err != nil {
  343. r.Close()
  344. return nil, false, err
  345. }
  346. return r, true, err
  347. } else {
  348. // We passed it
  349. maxIndex = curIndex - 1
  350. }
  351. }
  352. }
  353. // Scans and returns the first line that starts with 'prefix'
  354. // Consumes line and returns it.
  355. func scanNext(r *GroupReader, prefix string) (int, string, error) {
  356. for {
  357. line, err := r.ReadLine()
  358. if err != nil {
  359. return 0, "", err
  360. }
  361. if !strings.HasPrefix(line, prefix) {
  362. continue
  363. }
  364. index := r.CurIndex()
  365. return index, line, nil
  366. }
  367. }
  368. // Returns true iff an exact match was found.
  369. // Pushes line, does not consume it.
  370. func scanUntil(r *GroupReader, prefix string, cmp SearchFunc) (bool, error) {
  371. for {
  372. line, err := r.ReadLine()
  373. if err != nil {
  374. return false, err
  375. }
  376. if !strings.HasPrefix(line, prefix) {
  377. continue
  378. }
  379. val, err := cmp(line)
  380. if err != nil {
  381. return false, err
  382. }
  383. if val < 0 {
  384. continue
  385. } else if val == 0 {
  386. r.PushLine(line)
  387. return true, nil
  388. } else {
  389. r.PushLine(line)
  390. return false, nil
  391. }
  392. }
  393. }
  394. // Searches backwards for the last line in Group with prefix.
  395. // Scans each file forward until the end to find the last match.
  396. func (g *Group) FindLast(prefix string) (match string, found bool, err error) {
  397. g.mtx.Lock()
  398. minIndex, maxIndex := g.minIndex, g.maxIndex
  399. g.mtx.Unlock()
  400. r, err := g.NewReader(maxIndex)
  401. if err != nil {
  402. return "", false, err
  403. }
  404. defer r.Close()
  405. // Open files from the back and read
  406. GROUP_LOOP:
  407. for i := maxIndex; i >= minIndex; i-- {
  408. err := r.SetIndex(i)
  409. if err != nil {
  410. return "", false, err
  411. }
  412. // Scan each line and test whether line matches
  413. for {
  414. line, err := r.ReadLine()
  415. if err == io.EOF {
  416. if found {
  417. return match, found, nil
  418. }
  419. continue GROUP_LOOP
  420. } else if err != nil {
  421. return "", false, err
  422. }
  423. if strings.HasPrefix(line, prefix) {
  424. match = line
  425. found = true
  426. }
  427. if r.CurIndex() > i {
  428. if found {
  429. return match, found, nil
  430. }
  431. continue GROUP_LOOP
  432. }
  433. }
  434. }
  435. return
  436. }
  437. // GroupInfo holds information about the group.
  438. type GroupInfo struct {
  439. MinIndex int // index of the first file in the group, including head
  440. MaxIndex int // index of the last file in the group, including head
  441. TotalSize int64 // total size of the group
  442. HeadSize int64 // size of the head
  443. }
  444. // Returns info after scanning all files in g.Head's dir.
  445. func (g *Group) ReadGroupInfo() GroupInfo {
  446. g.mtx.Lock()
  447. defer g.mtx.Unlock()
  448. return g.readGroupInfo()
  449. }
  450. // Index includes the head.
  451. // CONTRACT: caller should have called g.mtx.Lock
  452. func (g *Group) readGroupInfo() GroupInfo {
  453. groupDir := filepath.Dir(g.Head.Path)
  454. headBase := filepath.Base(g.Head.Path)
  455. var minIndex, maxIndex int = -1, -1
  456. var totalSize, headSize int64 = 0, 0
  457. dir, err := os.Open(groupDir)
  458. if err != nil {
  459. panic(err)
  460. }
  461. defer dir.Close()
  462. fiz, err := dir.Readdir(0)
  463. if err != nil {
  464. panic(err)
  465. }
  466. // For each file in the directory, filter by pattern
  467. for _, fileInfo := range fiz {
  468. if fileInfo.Name() == headBase {
  469. fileSize := fileInfo.Size()
  470. totalSize += fileSize
  471. headSize = fileSize
  472. continue
  473. } else if strings.HasPrefix(fileInfo.Name(), headBase) {
  474. fileSize := fileInfo.Size()
  475. totalSize += fileSize
  476. indexedFilePattern := regexp.MustCompile(`^.+\.([0-9]{3,})$`)
  477. submatch := indexedFilePattern.FindSubmatch([]byte(fileInfo.Name()))
  478. if len(submatch) != 0 {
  479. // Matches
  480. fileIndex, err := strconv.Atoi(string(submatch[1]))
  481. if err != nil {
  482. panic(err)
  483. }
  484. if maxIndex < fileIndex {
  485. maxIndex = fileIndex
  486. }
  487. if minIndex == -1 || fileIndex < minIndex {
  488. minIndex = fileIndex
  489. }
  490. }
  491. }
  492. }
  493. // Now account for the head.
  494. if minIndex == -1 {
  495. // If there were no numbered files,
  496. // then the head is index 0.
  497. minIndex, maxIndex = 0, 0
  498. } else {
  499. // Otherwise, the head file is 1 greater
  500. maxIndex++
  501. }
  502. return GroupInfo{minIndex, maxIndex, totalSize, headSize}
  503. }
  504. func filePathForIndex(headPath string, index int, maxIndex int) string {
  505. if index == maxIndex {
  506. return headPath
  507. }
  508. return fmt.Sprintf("%v.%03d", headPath, index)
  509. }
  510. //--------------------------------------------------------------------------------
  511. // GroupReader provides an interface for reading from a Group.
  512. type GroupReader struct {
  513. *Group
  514. mtx sync.Mutex
  515. curIndex int
  516. curFile *os.File
  517. curReader *bufio.Reader
  518. curLine []byte
  519. }
  520. func newGroupReader(g *Group) *GroupReader {
  521. return &GroupReader{
  522. Group: g,
  523. curIndex: 0,
  524. curFile: nil,
  525. curReader: nil,
  526. curLine: nil,
  527. }
  528. }
  529. // Close closes the GroupReader by closing the cursor file.
  530. func (gr *GroupReader) Close() error {
  531. gr.mtx.Lock()
  532. defer gr.mtx.Unlock()
  533. if gr.curReader != nil {
  534. err := gr.curFile.Close()
  535. gr.curIndex = 0
  536. gr.curReader = nil
  537. gr.curFile = nil
  538. gr.curLine = nil
  539. return err
  540. }
  541. return nil
  542. }
  543. // Read implements io.Reader, reading bytes from the current Reader
  544. // incrementing index until enough bytes are read.
  545. func (gr *GroupReader) Read(p []byte) (n int, err error) {
  546. lenP := len(p)
  547. if lenP == 0 {
  548. return 0, errors.New("given empty slice")
  549. }
  550. gr.mtx.Lock()
  551. defer gr.mtx.Unlock()
  552. // Open file if not open yet
  553. if gr.curReader == nil {
  554. if err = gr.openFile(gr.curIndex); err != nil {
  555. return 0, err
  556. }
  557. }
  558. // Iterate over files until enough bytes are read
  559. var nn int
  560. for {
  561. nn, err = gr.curReader.Read(p[n:])
  562. n += nn
  563. if err == io.EOF {
  564. if n >= lenP {
  565. return n, nil
  566. }
  567. // Open the next file
  568. if err1 := gr.openFile(gr.curIndex + 1); err1 != nil {
  569. return n, err1
  570. }
  571. } else if err != nil {
  572. return n, err
  573. } else if nn == 0 { // empty file
  574. return n, err
  575. }
  576. }
  577. }
  578. // ReadLine reads a line (without delimiter).
  579. // just return io.EOF if no new lines found.
  580. func (gr *GroupReader) ReadLine() (string, error) {
  581. gr.mtx.Lock()
  582. defer gr.mtx.Unlock()
  583. // From PushLine
  584. if gr.curLine != nil {
  585. line := string(gr.curLine)
  586. gr.curLine = nil
  587. return line, nil
  588. }
  589. // Open file if not open yet
  590. if gr.curReader == nil {
  591. err := gr.openFile(gr.curIndex)
  592. if err != nil {
  593. return "", err
  594. }
  595. }
  596. // Iterate over files until line is found
  597. var linePrefix string
  598. for {
  599. bytesRead, err := gr.curReader.ReadBytes('\n')
  600. if err == io.EOF {
  601. // Open the next file
  602. if err1 := gr.openFile(gr.curIndex + 1); err1 != nil {
  603. return "", err1
  604. }
  605. if len(bytesRead) > 0 && bytesRead[len(bytesRead)-1] == byte('\n') {
  606. return linePrefix + string(bytesRead[:len(bytesRead)-1]), nil
  607. }
  608. linePrefix += string(bytesRead)
  609. continue
  610. } else if err != nil {
  611. return "", err
  612. }
  613. return linePrefix + string(bytesRead[:len(bytesRead)-1]), nil
  614. }
  615. }
  616. // IF index > gr.Group.maxIndex, returns io.EOF
  617. // CONTRACT: caller should hold gr.mtx
  618. func (gr *GroupReader) openFile(index int) error {
  619. // Lock on Group to ensure that head doesn't move in the meanwhile.
  620. gr.Group.mtx.Lock()
  621. defer gr.Group.mtx.Unlock()
  622. if index > gr.Group.maxIndex {
  623. return io.EOF
  624. }
  625. curFilePath := filePathForIndex(gr.Head.Path, index, gr.Group.maxIndex)
  626. curFile, err := os.OpenFile(curFilePath, os.O_RDONLY|os.O_CREATE, autoFilePerms)
  627. if err != nil {
  628. return err
  629. }
  630. curReader := bufio.NewReader(curFile)
  631. // Update gr.cur*
  632. if gr.curFile != nil {
  633. gr.curFile.Close() // TODO return error?
  634. }
  635. gr.curIndex = index
  636. gr.curFile = curFile
  637. gr.curReader = curReader
  638. gr.curLine = nil
  639. return nil
  640. }
  641. // PushLine makes the given line the current one, so the next time somebody
  642. // calls ReadLine, this line will be returned.
  643. // panics if called twice without calling ReadLine.
  644. func (gr *GroupReader) PushLine(line string) {
  645. gr.mtx.Lock()
  646. defer gr.mtx.Unlock()
  647. if gr.curLine == nil {
  648. gr.curLine = []byte(line)
  649. } else {
  650. panic("PushLine failed, already have line")
  651. }
  652. }
  653. // CurIndex returns cursor's file index.
  654. func (gr *GroupReader) CurIndex() int {
  655. gr.mtx.Lock()
  656. defer gr.mtx.Unlock()
  657. return gr.curIndex
  658. }
  659. // SetIndex sets the cursor's file index to index by opening a file at this
  660. // position.
  661. func (gr *GroupReader) SetIndex(index int) error {
  662. gr.mtx.Lock()
  663. defer gr.mtx.Unlock()
  664. return gr.openFile(index)
  665. }
  666. //--------------------------------------------------------------------------------
  667. // A simple SearchFunc that assumes that the marker is of form
  668. // <prefix><number>.
  669. // For example, if prefix is '#HEIGHT:', the markers of expected to be of the form:
  670. //
  671. // #HEIGHT:1
  672. // ...
  673. // #HEIGHT:2
  674. // ...
  675. func MakeSimpleSearchFunc(prefix string, target int) SearchFunc {
  676. return func(line string) (int, error) {
  677. if !strings.HasPrefix(line, prefix) {
  678. return -1, fmt.Errorf("Marker line did not have prefix: %v", prefix)
  679. }
  680. i, err := strconv.Atoi(line[len(prefix):])
  681. if err != nil {
  682. return -1, fmt.Errorf("Failed to parse marker line: %v", err.Error())
  683. }
  684. if target < i {
  685. return 1, nil
  686. } else if target == i {
  687. return 0, nil
  688. } else {
  689. return -1, nil
  690. }
  691. }
  692. }