Skip to content

Feature/repo level settings - #2442

Merged
johnnyaug merged 29 commits into
masterfrom
feature/repo_level_settings
Sep 22, 2021
Merged

johnnyaug merged 29 commits into
masterfrom
feature/repo_level_settings

Conversation

@johnnyaug

@johnnyaug johnnyaug commented Sep 2, 2021 •

Copy link
Copy Markdown
Contributor

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.

@arielshaqed arielshaqed left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread pkg/graveler/settings/manager.go Outdated

}

func (m *Manager) Get(ctx context.Context, repositoryID graveler.RepositoryID, key string, message proto.Message) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The inspiration here was "proto.Unmarshal". I've added comments which I hope would clarify. WDYT @arielshaqed?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Documented the performance limitations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's important to note the pyramid is skipped because objects are mutable in this implementation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a cache

@johnnyaug
johnnyaug marked this pull request as ready for review September 5, 2021 14:41
@johnnyaug
johnnyaug marked this pull request as draft September 5, 2021 15:47

@itaiad200 itaiad200 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Comment thread pkg/graveler/settings/manager.go Outdated
return err
}
logger := logging.FromContext(ctx)
if logger.IsTracing() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why this check?

Comment thread pkg/graveler/settings/manager.go Outdated
return err
}
logger := logging.FromContext(ctx)
if logger.IsTracing() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why checking? Isn't this supported out of the box when you use TRACE?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can use the logSetting function you defined below

Comment thread pkg/graveler/settings/manager.go Outdated
}
err = m.blockAdapter.Put(ctx, block.ObjectPointer{
StorageNamespace: string(repo.StorageNamespace),
Identifier: fmt.Sprintf(settingsSuffixTemplate, m.committedBlockStoragePrefix, key),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Identifier starts with a slash? Not again 😱

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment thread pkg/graveler/settings/manager.go Outdated
if err != nil {
return nil, err
}
update()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't update get the read value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The value is loaded into the given message variable, and update should use it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread pkg/graveler/settings/manager.go Outdated
// 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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The lock here is for doing the update atomically, not to use it for the settings.
Not sure I understand the comment

@arielshaqed arielshaqed left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread pkg/graveler/settings/manager.go Outdated
return err
}
logger := logging.FromContext(ctx)
if logger.IsTracing() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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...

@johnnyaug
johnnyaug marked this pull request as ready for review September 12, 2021 11:39
@johnnyaug
johnnyaug force-pushed the feature/repo_level_settings branch from 99934d5 to b219f03 Compare September 12, 2021 15:44

@arielshaqed arielshaqed left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread pkg/graveler/settings/manager.go Outdated
logSetting(logging.FromContext(ctx), repositoryID, key, message)
e = &entry{
message: message,
expiry: m.timeGetter().Add(1 * time.Second),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Expiry should be a cache parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed

Comment thread pkg/graveler/settings/manager.go Outdated
return rCache
}

func logSetting(logger logging.Logger, repositoryID graveler.RepositoryID, key string, message proto.Message) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't understand the name of this function. It's called from a get operation...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Renamed to logFetchedSetting

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree with Ariel, I would expect this function to be called fro "save" instead of "get".

Comment thread pkg/cache/cache.go Resolved
Comment thread pkg/graveler/settings/manager.go Outdated
branchLock: branchLock,
blockAdapter: blockAdapter,
committedBlockStoragePrefix: committedBlockStoragePrefix,
cache: cache.NewCache(cacheSize, defaultCacheExpiry, func() time.Duration {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread pkg/graveler/settings/manager.go Outdated

}

func (m *Manager) Get(ctx context.Context, repositoryID graveler.RepositoryID, key string, message proto.Message) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Comment thread pkg/graveler/settings/manager.go Outdated
// 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{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Change to use the struct as the key

Comment thread pkg/graveler/settings/manager.go Outdated
if err != nil {
return err
}
messageBytes, err := m.cache.GetOrSet(string(qualifiedKey), func() (v interface{}, err error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Change to use the struct as the key

Comment thread pkg/graveler/settings/manager.go Outdated
if err != nil {
return nil, err
}
update()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@johnnyaug
johnnyaug requested a review from guy-har September 13, 2021 15:06
Comment thread pkg/graveler/settings/manager.go Resolved
Comment thread pkg/graveler/settings/manager.go Outdated

const (
settingsSuffixTemplate = "%s/settings/%s.json"
cacheSize = 100_000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this the only cache size you would like to allow? if not, then consider calling it defaultCacheSize

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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).

Comment thread pkg/graveler/settings/manager.go Outdated
return rCache
}

func logSetting(logger logging.Logger, repositoryID graveler.RepositoryID, key string, message proto.Message) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree with Ariel, I would expect this function to be called fro "save" instead of "get".

Comment thread pkg/graveler/settings/manager.go Outdated
return err
}
logger := logging.FromContext(ctx)
if logger.IsTracing() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can use the logSetting function you defined below

Comment thread pkg/graveler/settings/manager.go Outdated
} else if !errors.Is(err, graveler.ErrNotFound) {
return nil, err
}
logFetchedSetting(logging.FromContext(ctx), repositoryID, key, message)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done: added a logMessage argument

@arielshaqed arielshaqed left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for being patient with me over all the rounds!

Comment thread pkg/logging/logger.go Outdated
func (*logrusEntryWrapper) IsTracing() bool {
return logrus.IsLevelEnabled(logrus.TraceLevel)
func (l *logrusEntryWrapper) IsTracing() bool {
return l.e.Logger.Level >= logrus.TraceLevel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Comment thread pkg/graveler/settings/manager.go Outdated
}

// 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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
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.

Comment thread pkg/graveler/settings/manager.go Outdated
RepositoryID: repositoryID,
Key: key,
}
message, err := m.cache.GetOrSet(k, func() (v interface{}, err error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@johnnyaug
johnnyaug requested a review from talSofer September 14, 2021 07:40

@talSofer talSofer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very cool!

Comment thread pkg/graveler/settings/manager_test.go Outdated
t.Fatal("got unexpected settings:", diff)
}
// after sleeping, we should get the new settings:
time.Sleep(100 * time.Millisecond)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not use 1000 * time.Millisecond to guarantee that 1 sec passed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great point. Done.

}
}

func TestEmpty(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure that I understand what this test is checking? can you please explain?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added comments

@johnnyaug
johnnyaug requested a review from talSofer September 19, 2021 12:22

@talSofer talSofer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good! thanks!

@johnnyaug
johnnyaug merged commit 5157ef0 into master Sep 22, 2021
@johnnyaug
johnnyaug deleted the feature/repo_level_settings branch September 22, 2021 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants