Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

WIP: PostgreSQL LISTEN/NOTIFY: invalidate wit cache #2172

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

kwk
Copy link
Collaborator

@kwk kwk commented Jul 17, 2018

PLEASE, DO NOT MERGE AS THIS MIGHT NEED APPROVAL FROM A POTENTIALLY LARGE AUDIENCE!!!

This change uses PostgreSQL's LISTEN/NOTIFY concept as an inter-process communication to signal that all core pods shall reload their work item cache after a space template was updated.

Here's the piece of code that subscribes to a channel:

// Ensure we delete the work item cache when we receive a notification from postgres
gormsupport.SetupDatabaseListener(*config, map[string]gormsupport.SubscriberFunc{
	gormsupport.ChanSpaceTemplateUpdates: func(channel, extra string) {
		workitem.ClearGlobalWorkItemTypeCache()
	},
})

This concept can be reused very easily by adding new listeners for the individual event channels.

TODO:

  • more negative tests

@kwk kwk self-assigned this Jul 17, 2018
@alien-ike alien-ike changed the title WIP: Invalidate wit cache Invalidate wit cache Jul 17, 2018
@alien-ike alien-ike changed the title WIP: Invalidate wit cache Invalidate wit cache Jul 17, 2018
@kwk kwk changed the title Invalidate wit cache WIP: Invalidate wit cache Jul 17, 2018
@kwk kwk requested review from xcoulon and alexeykazakov July 17, 2018 03:40
@codecov-io
Copy link

codecov-io commented Jul 17, 2018

Codecov Report

Merging #2172 into master will decrease coverage by 0.03%.
The diff coverage is 46.93%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2172      +/-   ##
==========================================
- Coverage   69.59%   69.56%   -0.04%     
==========================================
  Files         170      171       +1     
  Lines       15681    15730      +49     
==========================================
+ Hits        10913    10942      +29     
- Misses       3774     3794      +20     
  Partials      994      994
Impacted Files Coverage Δ
migration/migration.go 59.28% <0%> (-1.39%) ⬇️
configuration/configuration.go 69.45% <100%> (+0.53%) ⬆️
gormsupport/listener.go 47.22% <47.22%> (ø)
controller/workitem.go 79.84% <0%> (+1.18%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b6c848...68f445a. Read the comment docs.


// Send a notification from a completely different connection than the
// one we established to listen to channels.
s.DB.Debug().Exec("SELECT pg_notify($1, $2)", channelName, payload)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mental note for myself: I need to check for db.Error here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done in b227b9f


// This will send a notification on the
// gormsupport.ChanSpaceTemplateUpdates channel
migration.PopulateCommonTypes(nil, s.DB)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mental note for myself: I need to check for err here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done in b227b9f

@kwk
Copy link
Collaborator Author

kwk commented Jul 17, 2018

[test]

@jarifibrahim
Copy link
Member

@kwk I think this would fix openshiftio/openshift.io#3930.

@baijum baijum requested a review from bartoszmajsak July 17, 2018 07:04
@baijum
Copy link
Contributor

baijum commented Jul 17, 2018

Why don't we use a cache server like Memcached?

@kwk
Copy link
Collaborator Author

kwk commented Jul 17, 2018

Why don't we use a cache server like Memcached?

@baijum I think the effort to maintain a memcached server and have it deployed is outweighed by the simplicity and tight connection we already have with postgres. Also note that we might want to use PostgreSQL's notification system for other stuff in the future, so it is not only about caching although it is the first use-case for it. One can easily think of a use case in which we use it to trigger web socket notifications to clients for example. But that's not written in stone.

@jarifibrahim
Copy link
Member

@kwk
A new migration/update is performed when the spacetemplate is updated. This means that a new pod was spawned and doesn't have the old cache state.
If there are 4 pods they all will be eventually updated. So why do we need to notify the other pods?

I agree there might be an inconsistent state but that wouldn't last for long. As soon as all the pods are updated we will have an empty cache in all the pods.

@kwk
Copy link
Collaborator Author

kwk commented Jul 19, 2018

@kwk
A new migration/update is performed when the spacetemplate is updated. This means that a new pod was spawned and doesn't have the old cache state.
If there are 4 pods they all will be eventually updated. So why do we need to notify the other pods?

I agree there might be an inconsistent state but that wouldn't last for long. As soon as all the pods are updated we will have an empty cache in all the pods.

@jarifibrahim in the future the templates can be changed at run-time without requiring a restart.

@kwk kwk changed the title WIP: Invalidate wit cache WIP: PostgreSQL LISTEN/NOTIFY: invalidate wit cache Jul 19, 2018

// A SubscriberFunc describes the function signature that a subscriber needs to
// have. The channel parameter is just an arbitrary identifier string the
// identities a channel. The extra parameter is can contain optional data that
Copy link
Member

Choose a reason for hiding this comment

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

The extra parameter is can contain

@kwk kwk added the idea An idea is a PR that shall not be deleted and captures a brain dump of some sort. label Aug 23, 2018
@@ -140,6 +143,13 @@ func main() {
os.Exit(0)
}

// Ensure we delete the work item cache when we receive a notification from postgres
gormsupport.SetupDatabaseListener(*config, map[string]gormsupport.SubscriberFunc{
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens when SetupDatabaseListener function return an error? The error value is not handled here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch. I will have a look when this PR becomes more relevant.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💣 test database 🏠 architecture idea An idea is a PR that shall not be deleted and captures a brain dump of some sort. space_templates work-in-progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants