Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/api_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request,
}
if (request.method == "GET" && request.destination.find(kPrefix) == 0) {
std::string id = request.destination.substr(kPrefix.size());
std::lock_guard<std::mutex> lock(agent_.mu_);
std::lock_guard<std::mutex> lock(agent_.resource_mu_);
const auto result = agent_.resource_map_.find(id);
if (result == agent_.resource_map_.end()) {
// TODO: This could be considered log spam.
Expand Down Expand Up @@ -286,9 +286,8 @@ MetadataAgent::MetadataAgent(const MetadataAgentConfiguration& config)
MetadataAgent::~MetadataAgent() {}

void MetadataAgent::UpdateResource(const std::vector<std::string>& resource_ids,
const MonitoredResource& resource,
Metadata&& entry) {
std::lock_guard<std::mutex> lock(mu_);
const MonitoredResource& resource) {
std::lock_guard<std::mutex> lock(resource_mu_);
// TODO: How do we handle deleted resources?
// TODO: Do we care if the value was already there?
for (const std::string& id : resource_ids) {
Expand All @@ -297,6 +296,11 @@ void MetadataAgent::UpdateResource(const std::vector<std::string>& resource_ids,
}
resource_map_.emplace(id, resource);
}
}

void MetadataAgent::UpdateMetadata(const MonitoredResource& resource,

Choose a reason for hiding this comment

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

Just curious, why is this second map not keyed by resource_id as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because that map is sent to to the Resource Metadata API, which neither knows nor cares about the local resource ids.

Choose a reason for hiding this comment

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

Ah, got it. Makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

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

Curious - how is the local resource_id different from the monitored resource ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The local resource_id is an easily discoverable identifier for the resource that is unique within the current context (e.g., a Kubernetes node), but may not be globally unique, whereas the monitored resource (which actually is an id, so "monitored resource id" is redundant) is globally unique.

Metadata&& entry) {
std::lock_guard<std::mutex> lock(metadata_mu_);
if (config_.VerboseLogging()) {
LOG(INFO) << "Updating metadata map " << resource << "->{"
<< "version: " << entry.version << ", "
Expand All @@ -316,7 +320,7 @@ void MetadataAgent::UpdateResource(const std::vector<std::string>& resource_ids,

std::map<MonitoredResource, MetadataAgent::Metadata>
MetadataAgent::GetMetadataMap() const {
std::lock_guard<std::mutex> lock(mu_);
std::lock_guard<std::mutex> lock(metadata_mu_);

std::map<MonitoredResource, Metadata> result;
for (const auto& kv : metadata_map_) {
Expand Down
17 changes: 11 additions & 6 deletions src/api_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ class MetadataAgent {
MetadataAgent(const MetadataAgentConfiguration& config);
~MetadataAgent();

// Updates metadata for a given resource.
// Updates the local resource map entry for a given resource.
// Each local id in `resource_ids` is effectively an alias for `resource`.
// Adds a resource mapping from each of the `resource_ids` to the `resource`
// and a metadata mapping from the `resource` to the metadata `entry`.
// Adds a resource mapping from each of the `resource_ids` to the `resource`.
void UpdateResource(const std::vector<std::string>& resource_ids,
const MonitoredResource& resource,
const MonitoredResource& resource);

// Updates metadata for a given resource.
// Adds a metadata mapping from the `resource` to the metadata `entry`.
Copy link
Contributor

Choose a reason for hiding this comment

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

What does "metadata mapping" mean. Is just "mapping" sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The metadata agent maintains two mappings:

  • local resource map, from a locally unique id to the monitored resource (there may be multiple locally unique ids, all referring to the same monitored resource), which is exposed via a local endpoint to other agents so they don't have to discover all parts of the monitored resource themselves, and
  • metadata map, from the monitored resource to the metadata blob, which is sent to the Resource Metadata API.

void UpdateMetadata(const MonitoredResource& resource,
Metadata&& entry);

// Starts serving.
Expand All @@ -104,10 +107,12 @@ class MetadataAgent {

const MetadataAgentConfiguration& config_;

// A lock that guards access to the maps.
mutable std::mutex mu_;
// A lock that guards access to the local resource map.
mutable std::mutex resource_mu_;
// A map from a locally unique id to MonitoredResource.
std::map<std::string, MonitoredResource> resource_map_;
// A lock that guards access to the metadata map.
mutable std::mutex metadata_mu_;
// A map from MonitoredResource to (JSON) resource metadata.
std::map<MonitoredResource, Metadata> metadata_map_;

Expand Down
4 changes: 2 additions & 2 deletions src/docker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ constexpr const char resource_type_separator[] = ".";
DockerReader::DockerReader(const MetadataAgentConfiguration& config)
: config_(config), environment_(config) {}

std::vector<PollingMetadataUpdater::ResourceMetadata>
std::vector<MetadataUpdater::ResourceMetadata>
DockerReader::MetadataQuery() const {
if (config_.VerboseLogging()) {
LOG(INFO) << "Docker Query called";
Expand All @@ -58,7 +58,7 @@ std::vector<PollingMetadataUpdater::ResourceMetadata>
http::local_client client;
http::local_client::request list_request(
docker_endpoint + "/json?all=true" + container_filter);
std::vector<PollingMetadataUpdater::ResourceMetadata> result;
std::vector<MetadataUpdater::ResourceMetadata> result;
try {
http::local_client::response list_response = client.get(list_request);
Timestamp collected_at = std::chrono::system_clock::now();
Expand Down
12 changes: 11 additions & 1 deletion src/docker.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ class DockerReader {
public:
DockerReader(const MetadataAgentConfiguration& config);
// A Docker metadata query function.
std::vector<PollingMetadataUpdater::ResourceMetadata> MetadataQuery() const;
std::vector<MetadataUpdater::ResourceMetadata> MetadataQuery() const;

private:
const MetadataAgentConfiguration& config_;
Environment environment_;
};

class DockerUpdater : public PollingMetadataUpdater {
public:
DockerUpdater(MetadataAgent* server)
: reader_(server->config()), PollingMetadataUpdater(
server, server->config().DockerUpdaterIntervalSeconds(),
std::bind(&google::DockerReader::MetadataQuery, &reader_)) { }
private:
DockerReader reader_;
};

}

#endif // DOCKER_H_
Loading