Skip to content

Commit

Permalink
Add golang ci lint to CI (syndtr#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
syndtr committed Jun 14, 2022
1 parent 678c1e8 commit 64ee559
Show file tree
Hide file tree
Showing 44 changed files with 382 additions and 321 deletions.
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
linters-settings:
gocritic:
disabled-checks:
- ifElseChain
- elseif

linters:
enable:
- gofmt
- gocritic
- unconvert
14 changes: 5 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
language: go

before_install:
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2

go:
- 1.14.x
- 1.18.x
- tip

script:
- |
(set -e
if [ "$TRAVIS_GO_VERSION" != "tip" ]; then
echo "executing go vet for go verion $TRAVIS_GO_VERSION"
go vet ./...
else
echo "skipping go vet for go verion $TRAVIS_GO_VERSION"
fi
)
- go vet ./...
- golangci-lint run
- go test -short -timeout 1h ./...
- go test -timeout 30m -race -run "TestDB_(Concurrent|GoleveldbIssue74)" ./leveldb
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e h1:4nW4NLDYnU28ojHaHO8OVxFHk/aQ33U01a9cjED+pzE=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
16 changes: 0 additions & 16 deletions leveldb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func newErrBatchCorrupted(reason string) error {
const (
batchHeaderLen = 8 + 4
batchGrowLimit = 3000
batchBufioSize = 16
)

// BatchReplay wraps basic batch operations.
Expand All @@ -59,10 +58,6 @@ func (index batchIndex) v(data []byte) []byte {
return nil
}

func (index batchIndex) kv(data []byte) (key, value []byte) {
return index.k(data), index.v(data)
}

// Batch is a write batch.
type Batch struct {
data []byte
Expand Down Expand Up @@ -233,17 +228,6 @@ func (b *Batch) putMem(seq uint64, mdb *memdb.DB) error {
return nil
}

func (b *Batch) revertMem(seq uint64, mdb *memdb.DB) error {
var ik []byte
for i, index := range b.index {
ik = makeInternalKey(ik, index.k(b.data), seq+uint64(i), index.keyType)
if err := mdb.Delete(ik); err != nil {
return err
}
}
return nil
}

func newBatch() interface{} {
return &Batch{}
}
Expand Down
2 changes: 1 addition & 1 deletion leveldb/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func benchmarkBatchWrite(b *testing.B, config *BatchConfig) {
vals = append(vals, randomString(r, 100))
}
b.ResetTimer()
for round := 0; round < b.N; round += 1 {
for round := 0; round < b.N; round++ {
batch := MakeBatchWithConfig(config)
for i := 0; i < len(keys); i++ {
batch.Put(keys[i], vals[i])
Expand Down
12 changes: 0 additions & 12 deletions leveldb/corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,6 @@ func (h *dbCorruptHarness) corrupt(ft storage.FileType, fi, offset, n int) {
w.Close()
}

func (h *dbCorruptHarness) removeAll(ft storage.FileType) {
fds, err := h.stor.List(ft)
if err != nil {
h.t.Fatal("get files: ", err)
}
for _, fd := range fds {
if err := h.stor.Remove(fd); err != nil {
h.t.Error("remove file: ", err)
}
}
}

func (h *dbCorruptHarness) forceRemoveAll(ft storage.FileType) {
fds, err := h.stor.List(ft)
if err != nil {
Expand Down
33 changes: 25 additions & 8 deletions leveldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,16 @@ func openDB(s *session) (*DB, error) {
}
return nil, err
}

}

// Doesn't need to be included in the wait group.
go db.compactionError()
go db.mpoolDrain()

if readOnly {
db.SetReadOnly()
if err := db.SetReadOnly(); err != nil {
return nil, err
}
} else {
db.closeW.Add(2)
go db.tCompaction()
Expand Down Expand Up @@ -312,9 +313,17 @@ func recoverTable(s *session, o *opt.Options) error {
return
}
defer func() {
writer.Close()
if cerr := writer.Close(); cerr != nil {
if err == nil {
err = cerr
} else {
err = fmt.Errorf("error recovering table (%v); error closing (%v)", err, cerr)
}
}
if err != nil {
s.stor.Remove(tmpFd)
if rerr := s.stor.Remove(tmpFd); rerr != nil {
err = fmt.Errorf("error recovering table (%v); error removing (%v)", err, rerr)
}
tmpFd = storage.FileDesc{}
}
}()
Expand Down Expand Up @@ -531,7 +540,8 @@ func (db *DB) recoverJournal() error {
if jr == nil {
jr = journal.NewReader(fr, dropper{db.s, fd}, strict, checksum)
} else {
jr.Reset(fr, dropper{db.s, fd}, strict, checksum)
// Ignore the error here
_ = jr.Reset(fr, dropper{db.s, fd}, strict, checksum)
}

// Flush memdb and remove obsolete journal file.
Expand All @@ -551,7 +561,10 @@ func (db *DB) recoverJournal() error {
}
rec.resetAddedTables()

db.s.stor.Remove(ofd)
if err := db.s.stor.Remove(ofd); err != nil {
fr.Close()
return err
}
ofd = storage.FileDesc{}
}

Expand Down Expand Up @@ -635,7 +648,9 @@ func (db *DB) recoverJournal() error {

// Remove the last obsolete journal file.
if !ofd.Zero() {
db.s.stor.Remove(ofd)
if err := db.s.stor.Remove(ofd); err != nil {
return err
}
}

return nil
Expand Down Expand Up @@ -689,7 +704,9 @@ func (db *DB) recoverJournalRO() error {
if jr == nil {
jr = journal.NewReader(fr, dropper{db.s, fd}, strict, checksum)
} else {
jr.Reset(fr, dropper{db.s, fd}, strict, checksum)
if err := jr.Reset(fr, dropper{db.s, fd}, strict, checksum); err != nil {
return err
}
}

// Replay journal to memdb.
Expand Down
39 changes: 24 additions & 15 deletions leveldb/db_compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package leveldb

import (
"fmt"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -350,11 +351,11 @@ func (db *DB) memCompaction() {
}

type tableCompactionBuilder struct {
db *DB
s *session
c *compaction
rec *sessionRecord
stat0, stat1 *cStatStaging
db *DB
s *session
c *compaction
rec *sessionRecord
stat1 *cStatStaging

snapHasLastUkey bool
snapLastUkey []byte
Expand Down Expand Up @@ -415,14 +416,17 @@ func (b *tableCompactionBuilder) flush() error {
return nil
}

func (b *tableCompactionBuilder) cleanup() {
func (b *tableCompactionBuilder) cleanup() error {
if b.tw != nil {
b.tw.drop()
if err := b.tw.drop(); err != nil {
return err
}
b.tw = nil
}
return nil
}

func (b *tableCompactionBuilder) run(cnt *compactionTransactCounter) error {
func (b *tableCompactionBuilder) run(cnt *compactionTransactCounter) (err error) {
snapResumed := b.snapIter > 0
hasLastUkey := b.snapHasLastUkey // The key might has zero length, so this is necessary.
lastUkey := append([]byte(nil), b.snapLastUkey...)
Expand All @@ -432,7 +436,15 @@ func (b *tableCompactionBuilder) run(cnt *compactionTransactCounter) error {
// Restore compaction state.
b.c.restore()

defer b.cleanup()
defer func() {
if cerr := b.cleanup(); cerr != nil {
if err == nil {
err = cerr
} else {
err = fmt.Errorf("tableCompactionBuilder error: %v, cleanup error (%v)", err, cerr)
}
}
}()

b.stat1.startTimer()
defer b.stat1.stopTimer()
Expand Down Expand Up @@ -655,10 +667,7 @@ func (db *DB) tableNeedCompaction() bool {
func (db *DB) resumeWrite() bool {
v := db.s.version()
defer v.release()
if v.tLen(0) < db.s.o.GetWriteL0PauseTrigger() {
return true
}
return false
return v.tLen(0) < db.s.o.GetWriteL0PauseTrigger()
}

func (db *DB) pauseCompaction(ch chan<- struct{}) {
Expand All @@ -681,7 +690,7 @@ type cAuto struct {
func (r cAuto) ack(err error) {
if r.ackC != nil {
defer func() {
recover()
_ = recover()
}()
r.ackC <- err
}
Expand All @@ -696,7 +705,7 @@ type cRange struct {
func (r cRange) ack(err error) {
if r.ackC != nil {
defer func() {
recover()
_ = recover()
}()
r.ackC <- err
}
Expand Down
5 changes: 0 additions & 5 deletions leveldb/db_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package leveldb

import (
"errors"
"math/rand"
"runtime"
"sync"
Expand All @@ -18,10 +17,6 @@ import (
"github.com/syndtr/goleveldb/leveldb/util"
)

var (
errInvalidInternalKey = errors.New("leveldb: Iterator: invalid internal key")
)

type memdbReleaser struct {
once sync.Once
m *memDB
Expand Down
15 changes: 6 additions & 9 deletions leveldb/db_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ func (db *DB) newMem(n int) (mem *memDB, err error) {
if db.journal == nil {
db.journal = journal.NewWriter(w)
} else {
db.journal.Reset(w)
db.journalWriter.Close()
if err := db.journal.Reset(w); err != nil {
return nil, err
}
if err := db.journalWriter.Close(); err != nil {
return nil, err
}
db.frozenJournalFd = db.journalFd
}
db.journalWriter = w
Expand Down Expand Up @@ -181,13 +185,6 @@ func (db *DB) getEffectiveMem() *memDB {
return db.mem
}

// Check whether we has frozen memdb.
func (db *DB) hasFrozenMem() bool {
db.memMu.RLock()
defer db.memMu.RUnlock()
return db.frozenMem != nil
}

// Get frozen memdb.
func (db *DB) getFrozenMem() *memDB {
db.memMu.RLock()
Expand Down
Loading

0 comments on commit 64ee559

Please sign in to comment.