Skip to content

Instantly share code, notes, and snippets.

@portante
Last active December 8, 2017 22:15
Show Gist options
  • Save portante/a5a63e776358144c1d7caf161d5cda01 to your computer and use it in GitHub Desktop.
Save portante/a5a63e776358144c1d7caf161d5cda01 to your computer and use it in GitHub Desktop.
Example re-work of get_pod_metadata.
def get_pod_metadata(key, namespace_name, pod_name, record_create_time)
metadata = nil
ids = @id_cache[key]
if !ids.nil?
# FAST PATH
# Cache hit, fetch metadata from the cache
metadata = @cache.fetch(ids[:pod_id]) do
@stats.bump(:pod_cache_miss)
m = fetch_pod_metadata(namespace_name, pod_name)
(m.nil? || m.empty?) ? {'pod_id'=>ids[:pod_id]} : m
end
metadata.merge!(@namespace_cache.fetch(ids[:namespace_id]) do
@stats.bump(:namespace_cache_miss)
m = fetch_namespace_metadata(namespace_name)
(m.nil? || m.empty?) ? {'namespace_id'=>ids[:namespace_id]} : m
end)
else
# SLOW PATH
@stats.bump(:id_cache_miss)
pod_metadata = fetch_pod_metadata(namespace_name, pod_name)
namespace_metadata = fetch_namespace_metadata(namespace_name)
ids = { :pod_id=> pod_metadata['pod_id'], :namespace_id => namespace_metadata['namespace_id'] }
if !ids[:pod_id].nil? && !ids[:namespace_id].nil?
# pod found and namespace found
metadata = pod_metadata
metdata.merge!(namespace_metadata)
else
if ids[:pod_id].nil? && !ids[:namespace_id].nil?
# pod not found, but namespace found
@stats.bump(:id_cache_pod_not_found_namespace)
ns_time = Time.parse(namespace_metadata['creation_timestamp'])
if ns_time <= record_create_time
# namespace is older then record for pod
ids[:pod_id] = key
metadata = @cache.fetch(ids[:pod_id]) do
m = { 'pod_id' => ids[:pod_id] }
end
end
metdata.merge!(namespace_metadata)
else
if !ids[:pod_id].nil? && ids[:namespace_id].nil?
# pod found, but namespace NOT found
@stats.bump(:id_cache_namespace_not_found_pod)
else
# nothing found
@stats.bump(:id_cache_orphaned_record)
end
if @allow_orphans
log.trace("orphaning message for: #{namespace_name}/#{pod_name} ") if log.trace?
metadata = {
'orphaned_namespace' => namespace_name,
'namespace_name' => @orphaned_namespace_name,
'namespace_id' => @orphaned_namespace_id
}
else
metadata = {}
end
end
end
@id_cache[key] = ids
end
# remove namespace info that is only used for comparison
metadata.delete('creation_timestamp')
metadata
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment