Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue #3558 Initialize cache after DCP start #3607

Merged
merged 24 commits into from May 30, 2018

Conversation

tleyden
Copy link
Contributor

@tleyden tleyden commented May 25, 2018

Fixes #3558

Summary of changes:

  • Get current (latest) global doc sequence after starting DCP feed, to eliminate chance of race
  • Locks change cache for a brief period of time while the global doc sequence is looked up, during which time any incoming DCP events should have processing blocked
  • Rename onChange -> notifyChange
  • There was an error being ignored in the Clear() method -- this now propagates the error to callers.

Integration tests

http://uberjenkins.sc.couchbase.com/view/Build/job/sync-gateway-integration-master/725/
http://uberjenkins.sc.couchbase.com/view/Build/job/sync-gateway-integration-master/726/

@tleyden
Copy link
Contributor Author

tleyden commented May 26, 2018

Ready for a review pass -- builds passing

@@ -169,14 +175,25 @@ func (c *changeCache) IsStopped() bool {
return c.stopped
}

// Forgets all cached changes for all channels.
func (c *changeCache) Clear() {
// Empty out all channel caches. This doesn't touch
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment looks like it was truncated

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

//
// - Manage collection of channel caches
// - Receive DCP changes via callbacks
// - Perform sequence buffering to handle skipped sequences
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is misleading - sequence buffering is primarily about getting documents in sequence order. Skipped sequence handling is a separate concern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

nextSequence uint64 // Next consecutive sequence number to add
initialSequence uint64 // DB's current sequence at startup time
nextSequence uint64 // Next consecutive sequence number to add. State variable for sequence buffering tracking. Should use getNextSequence() rather than accessing directly.
initialSequence uint64 // DB's current sequence at startup time. Should use getInititalSequence() rather than accessing directly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My editor isn't flagging anything .. nor is it jumping out at me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed getInitialSequence()

}

// Set the initial sequence.
// Think of this as _SetInitialSequence() -- since it premumes that change chache is locked. It is not safe to
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

if event.Synchronous {
c.DocChangedSynchronous(event)
} else {
go c.DocChangedSynchronous(event)
}
}

// Set's the initial sequence, assume's that the caller is already holding a lock on the changeCache
func (c *changeCache) _setInitialSequence(initialSequence uint64) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this differ from SetInitialSequence below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed extraneous

db/database.go Outdated
// Find the current global doc sequence and use that for the initial sequence for the change cache
lastSequence, err := context.LastSequence()
if err != nil {
return nil, err
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to unlock the changeCache on error here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed.

db/database.go Outdated

if err != nil {
// In the case of an error, unlock the change cache despite the fact it's in an unitialized state.
context.changeCache.StartupUnlock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this again, I'm still not thrilled about trying to manage the changeCache's internal lock from an external location. Would this approach work:

  • changeCache.Init() leaves the cache locked (not started)
  • A subsequent call to changeCache.Start() does the following:
    • retrieves and sets initial/next sequence
    • unlocks the cache
      This would also let us push everything from line 313 to 324 down into changeCache.Start(), which I think makes more sense (it's cache-related code).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed that it's awkward to have the external locking semantics. Fixed in 8ee61c5

@tleyden tleyden assigned adamcfraser and unassigned adamcfraser May 30, 2018
Traun Leyden added 21 commits May 30, 2018 11:35
WARNING: DATA RACE
Write at 0x00c4201fa2e0 by goroutine 9:
  github.com/couchbase/sync_gateway/db.(*changeCache)._addToCache()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:689 +0x4ff
  github.com/couchbase/sync_gateway/db.(*changeCache).processEntry()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:650 +0x1f6
  github.com/couchbase/sync_gateway/db.(*changeCache).DocChangedSynchronous()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:549 +0x20a4

Previous read at 0x00c4201fa2e0 by goroutine 42:
  github.com/couchbase/sync_gateway/db.(*changeCache).waitForSequence()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/util_testing.go:25 +0xbd
  github.com/couchbase/sync_gateway/db.TestDocDeletionFromChannelCoalesced()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/changes_test.go:233 +0x5fe
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:746 +0x16c
@tleyden tleyden force-pushed the feature/issue_3558_int_cache_post_dcp branch from 8ee61c5 to 0be977f Compare May 30, 2018 18:35
@tleyden
Copy link
Contributor Author

tleyden commented May 30, 2018

@adamcfraser
Copy link
Collaborator

LGTM - can merge if integration tests are clean.

@tleyden tleyden merged commit b55a593 into dev May 30, 2018
@tleyden tleyden deleted the feature/issue_3558_int_cache_post_dcp branch May 30, 2018 21:40
bbrks pushed a commit that referenced this pull request Jun 7, 2018
* Add experimental dockerfile

* Fix attempt for build err

* Trying to figure out how to get source commit/branch

* Revert "Trying to figure out how to get source commit/branch"

This reverts commit bd5e502.

* Add artificial delay after getting last seq, but before starting dcp feed

* Reload c.initialSequence in DocChanged() as long as c.initialSequenceLazyLoaded == false

* Notes from call w/ Adam

* Add c.getInitialSequence() + _getNextSequence()

* Fixes

* Add comments

* Notes from call w/ Adam

* Sketch out changes and test

* Change the changeindex iface

* Fix data race

WARNING: DATA RACE
Write at 0x00c4201fa2e0 by goroutine 9:
  github.com/couchbase/sync_gateway/db.(*changeCache)._addToCache()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:689 +0x4ff
  github.com/couchbase/sync_gateway/db.(*changeCache).processEntry()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:650 +0x1f6
  github.com/couchbase/sync_gateway/db.(*changeCache).DocChangedSynchronous()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:549 +0x20a4

Previous read at 0x00c4201fa2e0 by goroutine 42:
  github.com/couchbase/sync_gateway/db.(*changeCache).waitForSequence()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/util_testing.go:25 +0xbd
  github.com/couchbase/sync_gateway/db.TestDocDeletionFromChannelCoalesced()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/changes_test.go:233 +0x5fe
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:746 +0x16c

* Use getNextSequence() in a few more places

* Fix another race

https://gist.github.com/tleyden/dccaa3cf2be85160051a793333c24467

* Remove dockerfile, moved to a separate PR

* Gofmt

* PR cleanup

* PR cleanup

* Remove check that’s leftover from first attempt

* Add comments

* First round of PR feedback

* Address PR feedback about avoiding external locking
adamcfraser pushed a commit that referenced this pull request Oct 4, 2018
* Add experimental dockerfile

* Fix attempt for build err

* Trying to figure out how to get source commit/branch

* Revert "Trying to figure out how to get source commit/branch"

This reverts commit bd5e502.

* Add artificial delay after getting last seq, but before starting dcp feed

* Reload c.initialSequence in DocChanged() as long as c.initialSequenceLazyLoaded == false

* Notes from call w/ Adam

* Add c.getInitialSequence() + _getNextSequence()

* Fixes

* Add comments

* Notes from call w/ Adam

* Sketch out changes and test

* Change the changeindex iface

* Fix data race

WARNING: DATA RACE
Write at 0x00c4201fa2e0 by goroutine 9:
  github.com/couchbase/sync_gateway/db.(*changeCache)._addToCache()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:689 +0x4ff
  github.com/couchbase/sync_gateway/db.(*changeCache).processEntry()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:650 +0x1f6
  github.com/couchbase/sync_gateway/db.(*changeCache).DocChangedSynchronous()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:549 +0x20a4

Previous read at 0x00c4201fa2e0 by goroutine 42:
  github.com/couchbase/sync_gateway/db.(*changeCache).waitForSequence()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/util_testing.go:25 +0xbd
  github.com/couchbase/sync_gateway/db.TestDocDeletionFromChannelCoalesced()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/changes_test.go:233 +0x5fe
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:746 +0x16c

* Use getNextSequence() in a few more places

* Fix another race

https://gist.github.com/tleyden/dccaa3cf2be85160051a793333c24467

* Remove dockerfile, moved to a separate PR

* Gofmt

* PR cleanup

* PR cleanup

* Remove check that’s leftover from first attempt

* Add comments

* First round of PR feedback

* Address PR feedback about avoiding external locking
adamcfraser added a commit that referenced this pull request Oct 4, 2018
* Add experimental dockerfile

* Fix attempt for build err

* Trying to figure out how to get source commit/branch

* Revert "Trying to figure out how to get source commit/branch"

This reverts commit bd5e502.

* Add artificial delay after getting last seq, but before starting dcp feed

* Reload c.initialSequence in DocChanged() as long as c.initialSequenceLazyLoaded == false

* Notes from call w/ Adam

* Add c.getInitialSequence() + _getNextSequence()

* Fixes

* Add comments

* Notes from call w/ Adam

* Sketch out changes and test

* Change the changeindex iface

* Fix data race

WARNING: DATA RACE
Write at 0x00c4201fa2e0 by goroutine 9:
  github.com/couchbase/sync_gateway/db.(*changeCache)._addToCache()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:689 +0x4ff
  github.com/couchbase/sync_gateway/db.(*changeCache).processEntry()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:650 +0x1f6
  github.com/couchbase/sync_gateway/db.(*changeCache).DocChangedSynchronous()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/change_cache.go:549 +0x20a4

Previous read at 0x00c4201fa2e0 by goroutine 42:
  github.com/couchbase/sync_gateway/db.(*changeCache).waitForSequence()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/util_testing.go:25 +0xbd
  github.com/couchbase/sync_gateway/db.TestDocDeletionFromChannelCoalesced()
      /drone/godeps/src/github.com/couchbase/sync_gateway/db/changes_test.go:233 +0x5fe
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:746 +0x16c

* Use getNextSequence() in a few more places

* Fix another race

https://gist.github.com/tleyden/dccaa3cf2be85160051a793333c24467

* Remove dockerfile, moved to a separate PR

* Gofmt

* PR cleanup

* PR cleanup

* Remove check that’s leftover from first attempt

* Add comments

* First round of PR feedback

* Address PR feedback about avoiding external locking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants