Repository navigation
Feature/repo level settings - #2442
Conversation
arielshaqed
left a comment
There was a problem hiding this comment.
Good stuff!
I would be happier with some more clarity about the following points. It might be possible to resolve them in package-level or class-level godoc, about how application code should use settings from this library.
- Efficiency. Document that reading is slow.
- Consistency guarantees. I believe that with this implementation, callers must read repository setting only once per operation. A second read for the same operation may return different results. (E.g. in a Spark job that needs repo configuration, the driver should read relevant configuration once and broadcast it to workers performing steps. It is unsafe to read the configuration separately from each worker.)
- Observability. Operations using repo configuration should dump the configuration file (unparsed?) and/or the relevant extract parameters on start. As an operator, particularly when trying to debug why something happened, I need to know what parameters were used on that particular operation.
|
|
||
| } | ||
|
|
||
| func (m *Manager) Get(ctx context.Context, repositoryID graveler.RepositoryID, key string, message proto.Message) error { |
There was a problem hiding this comment.
This is a somewhat un-Go-like interface: it takes a pointer of an object to update. But this parameter message has 2 distinct uses:
- the return value (out)
- the expected type (in)
The Protobuf Way is often to pass in the empty message of the protobuf type as a template, and have the function clone in to generate the return message, fill that in, and return that.
There was a problem hiding this comment.
The inspiration here was "proto.Unmarshal". I've added comments which I hope would clarify. WDYT @arielshaqed?
There was a problem hiding this comment.
Reading the documentation, it is not entirely clear how to call it. IIUC the correct call is
var entry EntryProtoMessage
err := m.Get(ctx, repo, key, &entry)| Identifier: fmt.Sprintf(settingsSuffixTemplate, m.committedBlockStoragePrefix, key), | ||
| IdentifierType: block.IdentifierTypeRelative, | ||
| } | ||
| reader, err := m.blockAdapter.Get(ctx, objectPointer, -1) |
There was a problem hiding this comment.
This skips pyramid (tiered storage). So reading will have poor performance; consider documenting. An alternative might be to store the ObjectPtr for the repo settings, use a random name for that, and then you can cache the contents.
There was a problem hiding this comment.
Documented the performance limitations
There was a problem hiding this comment.
It's important to note the pyramid is skipped because objects are mutable in this implementation.
There was a problem hiding this comment.
Which also makes me wonder - if branch staging locking is done using repo-level settings, wouldn't that mean that every upload to lakeFS requires an additional query to the object store?
itaiad200
left a comment
There was a problem hiding this comment.
Neat - using graveler building blocks look like the way to go.
Commenting and not blocking because I don't want to block forever, but I do have some concerns.
| Identifier: fmt.Sprintf(settingsSuffixTemplate, m.committedBlockStoragePrefix, key), | ||
| IdentifierType: block.IdentifierTypeRelative, | ||
| } | ||
| reader, err := m.blockAdapter.Get(ctx, objectPointer, -1) |
There was a problem hiding this comment.
It's important to note the pyramid is skipped because objects are mutable in this implementation.
| Identifier: fmt.Sprintf(settingsSuffixTemplate, m.committedBlockStoragePrefix, key), | ||
| IdentifierType: block.IdentifierTypeRelative, | ||
| } | ||
| reader, err := m.blockAdapter.Get(ctx, objectPointer, -1) |
There was a problem hiding this comment.
Which also makes me wonder - if branch staging locking is done using repo-level settings, wouldn't that mean that every upload to lakeFS requires an additional query to the object store?
| return err | ||
| } | ||
| logger := logging.FromContext(ctx) | ||
| if logger.IsTracing() { |
| return err | ||
| } | ||
| logger := logging.FromContext(ctx) | ||
| if logger.IsTracing() { |
There was a problem hiding this comment.
Why checking? Isn't this supported out of the box when you use TRACE?
There was a problem hiding this comment.
Unfortunately everything up to the Trace call is called no matter what. So here you would text-format message every time, for the sake of being able to trace it. You end up paying a huge price just to be able to trace in very rare circumstances.
This is why C++ loggers hide the if in a macro, which allows them not to call any of the formatting functions (or even make a jump...). But oh no macros are too difficult...
There was a problem hiding this comment.
Can use the logSetting function you defined below
| } | ||
| err = m.blockAdapter.Put(ctx, block.ObjectPointer{ | ||
| StorageNamespace: string(repo.StorageNamespace), | ||
| Identifier: fmt.Sprintf(settingsSuffixTemplate, m.committedBlockStoragePrefix, key), |
There was a problem hiding this comment.
Identifier starts with a slash? Not again 😱
| if err != nil { | ||
| return nil, err | ||
| } | ||
| update() |
There was a problem hiding this comment.
Shouldn't update get the read value?
There was a problem hiding this comment.
The value is loaded into the given message variable, and update should use it.
There was a problem hiding this comment.
I also do not understand how update can affect the message. We get message from m, then we call update which cannot see message, then we save the exact message that we just loaded.
| // 1. Fetch the configuration under the given repository and key, and place it in message. | ||
| // 2. Call the given update function (which should presumably update the message). | ||
| // 3. Persist message to the store. | ||
| func (m *Manager) UpdateWithLock(ctx context.Context, repositoryID graveler.RepositoryID, key string, message proto.Message, update func()) error { |
There was a problem hiding this comment.
Getting the settings while locking is not very useful if you act upon the settings after releasing the lock (settings might already changed..).
Using the update for doing the operation you wanted is one way of solving it, but it leads to some limitations, like not using the branch locker. Aside from the abstraction leak, Not sure we want to switch our entire branching technique to use this settings manager instead.
There was a problem hiding this comment.
The lock here is for doing the update atomically, not to use it for the settings.
Not sure I understand the comment
arielshaqed
left a comment
There was a problem hiding this comment.
Explain why IsTracing is needed. TBH not sure why GH thought it was my turn on this review, so responding to make it Someone Else's Turn.
| return err | ||
| } | ||
| logger := logging.FromContext(ctx) | ||
| if logger.IsTracing() { |
There was a problem hiding this comment.
Unfortunately everything up to the Trace call is called no matter what. So here you would text-format message every time, for the sake of being able to trace it. You end up paying a huge price just to be able to trace in very rare circumstances.
This is why C++ loggers hide the if in a macro, which allows them not to call any of the formatting functions (or even make a jump...). But oh no macros are too difficult...
99934d5 to
b219f03
Compare
arielshaqed
left a comment
There was a problem hiding this comment.
Thanks! I am still missing some understanding of the intended use.
Also, we need to document for users of the settings library the expected behaviour of settings with regard to caching.
| logSetting(logging.FromContext(ctx), repositoryID, key, message) | ||
| e = &entry{ | ||
| message: message, | ||
| expiry: m.timeGetter().Add(1 * time.Second), |
There was a problem hiding this comment.
Expiry should be a cache parameter.
| return rCache | ||
| } | ||
|
|
||
| func logSetting(logger logging.Logger, repositoryID graveler.RepositoryID, key string, message proto.Message) { |
There was a problem hiding this comment.
I don't understand the name of this function. It's called from a get operation...
There was a problem hiding this comment.
Renamed to logFetchedSetting
There was a problem hiding this comment.
Agree with Ariel, I would expect this function to be called fro "save" instead of "get".
| branchLock: branchLock, | ||
| blockAdapter: blockAdapter, | ||
| committedBlockStoragePrefix: committedBlockStoragePrefix, | ||
| cache: cache.NewCache(cacheSize, defaultCacheExpiry, func() time.Duration { |
There was a problem hiding this comment.
I'd feel safer if we had some jitter here, even on the order of 5 ms.
Suppose we have no jitter and consider a continuously loaded system with 100 repos starting up. It immediately loads 100 repo configs, all get almost the exact same expiry time. So 1 second later, it reloads 100 repo configs, again with almost the exact same expiry time. Even worse, concurrent requests sometimes synchronize with each other, so the expiry times spread out even less than you would expect if they were independently distributed.
Jitter helps :-)
|
|
||
| } | ||
|
|
||
| func (m *Manager) Get(ctx context.Context, repositoryID graveler.RepositoryID, key string, message proto.Message) error { |
There was a problem hiding this comment.
Reading the documentation, it is not entirely clear how to call it. IIUC the correct call is
var entry EntryProtoMessage
err := m.Get(ctx, repo, key, &entry)| // The result is placed in message. | ||
| // The result is eventually consistent: it is not guaranteed to be the most up-to-date setting. The cache expiry period is 1 second. | ||
| func (m *Manager) Get(ctx context.Context, repositoryID graveler.RepositoryID, key string, message proto.Message) error { | ||
| qualifiedKey, err := json.Marshal(cacheKey{ |
There was a problem hiding this comment.
Does json.Marshal guarantee a canonical serialization? Otherwise the cache key might conceivably change. E.g. if JSON objects are implemented as maps and Marshal re-uses a map, it could encode the same JSON object in 2 different ways.
There was a problem hiding this comment.
Change to use the struct as the key
| if err != nil { | ||
| return err | ||
| } | ||
| messageBytes, err := m.cache.GetOrSet(string(qualifiedKey), func() (v interface{}, err error) { |
There was a problem hiding this comment.
Not sure I understand. GetOrSet returns a value of type interface{}; why do we need to serialize to put something in and deserialize when we get it out? E.g. auth LRUCache.GetCredential doesn't serialize.
There was a problem hiding this comment.
Change to use the struct as the key
| if err != nil { | ||
| return nil, err | ||
| } | ||
| update() |
There was a problem hiding this comment.
I also do not understand how update can affect the message. We get message from m, then we call update which cannot see message, then we save the exact message that we just loaded.
|
|
||
| const ( | ||
| settingsSuffixTemplate = "%s/settings/%s.json" | ||
| cacheSize = 100_000 |
There was a problem hiding this comment.
Is this the only cache size you would like to allow? if not, then consider calling it defaultCacheSize
There was a problem hiding this comment.
I didn't want to limit the cache by size, but it's required by the cache library that we're using. So I just gave an arbitrary, very big number (relative to the expected number of entries).
| return rCache | ||
| } | ||
|
|
||
| func logSetting(logger logging.Logger, repositoryID graveler.RepositoryID, key string, message proto.Message) { |
There was a problem hiding this comment.
Agree with Ariel, I would expect this function to be called fro "save" instead of "get".
| return err | ||
| } | ||
| logger := logging.FromContext(ctx) | ||
| if logger.IsTracing() { |
There was a problem hiding this comment.
Can use the logSetting function you defined below
| } else if !errors.Is(err, graveler.ErrNotFound) { | ||
| return nil, err | ||
| } | ||
| logFetchedSetting(logging.FromContext(ctx), repositoryID, key, message) |
There was a problem hiding this comment.
Instead of renaming the logSetting method to logFetchedSetting, consider adding an action verb parameter to the method. It would be clearer if the logs report that setting X was updated, and setting Y fetched, added etc.
There was a problem hiding this comment.
Done: added a logMessage argument
arielshaqed
left a comment
There was a problem hiding this comment.
Thanks for being patient with me over all the rounds!
| func (*logrusEntryWrapper) IsTracing() bool { | ||
| return logrus.IsLevelEnabled(logrus.TraceLevel) | ||
| func (l *logrusEntryWrapper) IsTracing() bool { | ||
| return l.e.Logger.Level >= logrus.TraceLevel |
There was a problem hiding this comment.
This is really weird, I know. But d29d82e changed it to have this global style. So the existing mostly sorta kinda works, but completely relies on the global logrus level.
Maybe do this in a separate PR, so we can have a look just at this?
| } | ||
|
|
||
| // UpdateWithLock atomically gets a setting, performs the update function, and persists the setting to the store. | ||
| func (m *Manager) UpdateWithLock(ctx context.Context, repositoryID graveler.RepositoryID, key string, emptyMessage proto.Message, update func(message proto.Message)) error { |
There was a problem hiding this comment.
| func (m *Manager) UpdateWithLock(ctx context.Context, repositoryID graveler.RepositoryID, key string, emptyMessage proto.Message, update func(message proto.Message)) error { | |
| func (m *Manager) UpdateWithLock(ctx context.Context, repositoryID graveler.RepositoryID, key string, messageTemplate proto.Message, update func(message proto.Message)) error { |
might be a bit better, to explain how the default message is used. Or document the parameter (and then the name matters less). The thing is, the way I see it, the most important thing is not that the message is empty, it's the type of the message.
| RepositoryID: repositoryID, | ||
| Key: key, | ||
| } | ||
| message, err := m.cache.GetOrSet(k, func() (v interface{}, err error) { |
There was a problem hiding this comment.
Using a struct as a cache key -- this is neat! I was curious about whether this is guaranteed to work (so no action item requested, just explaining why it works for future reference by future self). Here's the section in the spec that says it all works: https://golang.org/ref/spec#Map_types.
The comparison operators
==and!=must be fully defined for operands of the key type; thus the key type must not be a function, map, or slice. If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time panic.
Note that this also hints that our cache.Cache is not type-safe: it validates that key types are comparable at runtime by panicking. This usage is, in any case, safe.
| t.Fatal("got unexpected settings:", diff) | ||
| } | ||
| // after sleeping, we should get the new settings: | ||
| time.Sleep(100 * time.Millisecond) |
There was a problem hiding this comment.
Why not use 1000 * time.Millisecond to guarantee that 1 sec passed?
There was a problem hiding this comment.
Don't want the test to take 1 second, so changed the default expiry to 50ms
| err = m.Save(ctx, "example-repo", "settingKey", secondSettings) | ||
| testutil.Must(t, err) | ||
|
|
||
| // the result should be cached and we should get the first settings: |
There was a problem hiding this comment.
Are you relying on the assumption that less than 1 sec passed from the two get calls? (lines 27 and 37) This can potentially lead to flakiness.
Is there a way to confirm that the GetOrSet method of the cache was called with firstSetting as a parameter instead of trying to get setting 1 after saving setting 2?
There was a problem hiding this comment.
Great point. Done.
| } | ||
| } | ||
|
|
||
| func TestEmpty(t *testing.T) { |
There was a problem hiding this comment.
I'm not sure that I understand what this test is checking? can you please explain?
Implements #2406.
Utilize the underlying storage to save repository-level settings.
Settings are saved under keys and they can be any
proto.Message.The settings are cached with an expiry time of 1 second, so when requesting a setting, the result is only eventually consistent.