journal: change omap get func to handle multiple keys at once

Taking this appraoch means that any function that must get more than one
key's value from the same oid can be more efficient by calling out to
ceph only once.

To be cautious and avoid missing things we always request ceph return
more keys than we actually expect to be set on the oid. If there are
unexpected keys there, we will not miss the keys we want if we first hit
an unexpected key if we were to limit ourselves to iterating only over
the number of keys we're expecting to be on the object.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
This commit is contained in:
John Mulligan
2020-05-15 14:02:51 -04:00
committed by mergify[bot]
parent cd24bb3f5c
commit 0ac5f40d09
2 changed files with 84 additions and 70 deletions

View File

@ -25,14 +25,18 @@ import (
"k8s.io/klog"
)
func getOneOMapValue(
// listExcess is the number of false-positive key-value pairs we will
// accept from ceph when getting omap values.
var listExcess = 32
func getOMapValues(
ctx context.Context,
conn *Connection,
poolName, namespace, oMapName, oMapKey string) (string, error) {
poolName, namespace, oid, prefix string, keys []string) (map[string]string, error) {
// fetch and configure the rados ioctx
ioctx, err := conn.conn.GetIoctx(poolName)
if err != nil {
return "", omapPoolError(poolName, err)
return nil, omapPoolError(poolName, err)
}
defer ioctx.Destroy()
@ -40,34 +44,37 @@ func getOneOMapValue(
ioctx.SetNamespace(namespace)
}
pairs, err := ioctx.GetOmapValues(
oMapName, // oid (name of object)
"", // startAfter (ignored)
oMapKey, // filterPrefix - match only keys with this prefix
1, // maxReturn - fetch no more than N values
results := map[string]string{}
// want is our "lookup map" that ensures O(1) checks for keys
// while iterating, without needing to complicate the caller.
want := make(map[string]bool, len(keys))
for i := range keys {
want[keys[i]] = true
}
err = ioctx.ListOmapValues(
oid, "", prefix, int64(len(want)+listExcess),
func(key string, value []byte) {
if want[key] {
results[key] = string(value)
}
},
)
switch err {
case nil:
case rados.ErrNotFound:
klog.Errorf(
util.Log(ctx, "omap not found (pool=%q, namespace=%q, name=%q, key=%q): %v"),
poolName, namespace, oMapName, oMapKey, err)
return "", util.NewErrKeyNotFound(oMapKey, err)
util.Log(ctx, "omap not found (pool=%q, namespace=%q, name=%q): %v"),
poolName, namespace, oid, err)
return nil, util.NewErrKeyNotFound(oid, err)
default:
return "", err
return nil, err
}
result, found := pairs[oMapKey]
if !found {
klog.Errorf(
util.Log(ctx, "key not found in omap (pool=%q, namespace=%q, name=%q, key=%q): %v"),
poolName, namespace, oMapName, oMapKey, err)
return "", util.NewErrKeyNotFound(oMapKey, nil)
}
klog.Infof(
util.Log(ctx, "XXX key found in omap! (pool=%q, namespace=%q, name=%q, key=%q): %v"),
poolName, namespace, oMapName, oMapKey, result)
return string(result), nil
klog.V(4).Infof(
util.Log(ctx, "got omap values: (pool=%q, namespace=%q, name=%q): %+v"),
poolName, namespace, oid, results)
return results, nil
}
func removeOneOMapKey(