Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions include/envoy/api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ envoy_cc_library(
hdrs = ["api.h"],
deps = [
"//include/envoy/event:dispatcher_interface",
"//include/envoy/filesystem:filesystem_interface",
"//include/envoy/thread:thread_interface",
],
)
Expand Down
25 changes: 5 additions & 20 deletions include/envoy/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,17 @@ class Api {
virtual Event::DispatcherPtr allocateDispatcher(Event::TimeSystem& time_system) PURE;

/**
* Create/open a local file that supports async appending.
* @param path supplies the file path.
* @param dispatcher supplies the dispatcher uses for async flushing.
* @param lock supplies the lock to use for cross thread appends.
*/
virtual Filesystem::FileSharedPtr createFile(const std::string& path,
Event::Dispatcher& dispatcher,
Thread::BasicLockable& lock) PURE;

/**
* @return bool whether a file exists and can be opened for read on disk.
*/
virtual bool fileExists(const std::string& path) PURE;

/**
* @return file content.
* @return a reference to the ThreadFactory
*/
virtual std::string fileReadToEnd(const std::string& path) PURE;
virtual Thread::ThreadFactory& threadFactory() PURE;

/**
* @return a reference to the ThreadFactory
* @return a reference to the Filesystem::Instance
*/
virtual Thread::ThreadFactory& threadFactory() PURE;
virtual Filesystem::Instance& fileSystem() PURE;
};

typedef std::unique_ptr<Api> ApiPtr;

} // namespace Api
} // namespace Envoy
} // namespace Envoy
1 change: 1 addition & 0 deletions include/envoy/api/os_sys_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ template <typename T> struct SysCallResult {
typedef SysCallResult<int> SysCallIntResult;
typedef SysCallResult<ssize_t> SysCallSizeResult;
typedef SysCallResult<void*> SysCallPtrResult;
typedef SysCallResult<std::string> SysCallStringResult;

class OsSysCalls {
public:
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/event/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ envoy_cc_library(
":signal_interface",
"//include/envoy/common:time_interface",
"//include/envoy/event:timer_interface",
"//include/envoy/filesystem:filesystem_interface",
"//include/envoy/filesystem:watcher_interface",
"//include/envoy/network:connection_handler_interface",
"//include/envoy/network:connection_interface",
"//include/envoy/network:dns_interface",
Expand Down
2 changes: 1 addition & 1 deletion include/envoy/event/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "envoy/event/file_event.h"
#include "envoy/event/signal.h"
#include "envoy/event/timer.h"
#include "envoy/filesystem/filesystem.h"
#include "envoy/filesystem/watcher.h"
#include "envoy/network/connection.h"
#include "envoy/network/connection_handler.h"
#include "envoy/network/dns.h"
Expand Down
8 changes: 8 additions & 0 deletions include/envoy/filesystem/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ envoy_package()
envoy_cc_library(
name = "filesystem_interface",
hdrs = ["filesystem.h"],
deps = [
"//include/envoy/event:dispatcher_interface",
],
)

envoy_cc_library(
name = "watcher_interface",
hdrs = ["watcher.h"],
)
76 changes: 63 additions & 13 deletions include/envoy/filesystem/filesystem.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include <cstdint>
#include <functional>
#include <memory>
#include <string>

#include "envoy/common/pure.h"
#include "envoy/event/dispatcher.h"
#include "envoy/thread/thread.h"

#include "absl/strings/string_view.h"

Expand Down Expand Up @@ -38,25 +39,74 @@ class File {
typedef std::shared_ptr<File> FileSharedPtr;

/**
* Abstraction for a file watcher.
* Captures state, properties, and stats of a file-system.
*/
class Watcher {
class Instance {
public:
typedef std::function<void(uint32_t events)> OnChangedCb;
virtual ~Instance() {}

struct Events {
static const uint32_t MovedTo = 0x1;
};
/**
* Creates a file, overriding the flush-interval set in the class.
*
* @param path The path of the file to open.
* @param dispatcher The dispatcher used for set up timers to run flush().
* @param lock The lock.
* @param file_flush_interval_msec Number of milliseconds to delay before flushing.
*/
virtual FileSharedPtr createFile(const std::string& path, Event::Dispatcher& dispatcher,
Thread::BasicLockable& lock,
std::chrono::milliseconds file_flush_interval_msec) PURE;

/**
* Creates a file, using the default flush-interval for the class.
*
* @param path The path of the file to open.
* @param dispatcher The dispatcher used for set up timers to run flush().
* @param lock The lock.
*/
virtual FileSharedPtr createFile(const std::string& path, Event::Dispatcher& dispatcher,
Thread::BasicLockable& lock) PURE;

virtual ~Watcher() {}
/**
* @return bool whether a file exists on disk and can be opened for read.
*/
virtual bool fileExists(const std::string& path) PURE;

/**
* @return bool whether a directory exists on disk and can be opened for read.
*/
virtual bool directoryExists(const std::string& path) PURE;

/**
* @return ssize_t the size in bytes of the specified file, or -1 if the file size
* cannot be determined for any reason, including without limitation
* the non-existence of the file.
*/
virtual ssize_t fileSize(const std::string& path) PURE;

/**
* @return full file content as a string.
* @throw EnvoyException if the file cannot be read.
* Be aware, this is not most highly performing file reading method.
*/
virtual std::string fileReadToEnd(const std::string& path) PURE;

/**
* @param path some filesystem path.
* @return SysCallStringResult containing the canonical path (see realpath(3)).
*/
virtual Api::SysCallStringResult canonicalPath(const std::string& path) PURE;

/**
* Add a file watch.
* @param path supplies the path to watch.
* @param events supplies the events to watch.
* @param cb supplies the callback to invoke when a change occurs.
* Determine if the path is on a list of paths Envoy will refuse to access. This
* is a basic sanity check for users, blacklisting some clearly bad paths. Paths
* may still be problematic (e.g. indirectly leading to /dev/mem) even if this
* returns false, it is up to the user to validate that supplied paths are
* valid.
* @param path some filesystem path.
* @return is the path on the blacklist?
*/
virtual void addWatch(const std::string& path, uint32_t events, OnChangedCb cb) PURE;
virtual bool illegalPath(const std::string& path) PURE;
};

typedef std::unique_ptr<Watcher> WatcherPtr;
Expand Down
39 changes: 39 additions & 0 deletions include/envoy/filesystem/watcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <cstdint>
#include <functional>
#include <memory>
#include <string>

#include "envoy/common/platform.h"
#include "envoy/common/pure.h"

namespace Envoy {
namespace Filesystem {

/**
* Abstraction for a file watcher.
*/
class Watcher {
public:
typedef std::function<void(uint32_t events)> OnChangedCb;

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.

Prefer 'using' to 'typedef'.


struct Events {
static const uint32_t MovedTo = 0x1;
};

virtual ~Watcher() {}

/**
* Add a file watch.
* @param path supplies the path to watch.
* @param events supplies the events to watch.
* @param cb supplies the callback to invoke when a change occurs.
*/
virtual void addWatch(const std::string& path, uint32_t events, OnChangedCb cb) PURE;
};

using WatcherPtr = std::unique_ptr<Watcher>;

} // namespace Filesystem
} // namespace Envoy
1 change: 1 addition & 0 deletions include/envoy/grpc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ envoy_cc_library(
"grpc",
],
deps = [
"//include/envoy/api:api_interface",
"@envoy_api//envoy/api/v2/core:grpc_service_cc",
],
)
Expand Down
5 changes: 4 additions & 1 deletion include/envoy/grpc/google_grpc_creds.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <memory>

#include "envoy/api/api.h"
#include "envoy/api/v2/core/grpc_service.pb.h"
#include "envoy/common/pure.h"

Expand All @@ -25,11 +26,13 @@ class GoogleGrpcCredentialsFactory {
* CompositeCallCredentials to combine multiple credentials.
*
* @param grpc_service_config contains configuration options
* @param api reference to the Api object
* @return std::shared_ptr<grpc::ChannelCredentials> to be used to authenticate a Google gRPC
* channel.
*/
virtual std::shared_ptr<grpc::ChannelCredentials>
getChannelCredentials(const envoy::api::v2::core::GrpcService& grpc_service_config) PURE;
getChannelCredentials(const envoy::api::v2::core::GrpcService& grpc_service_config,
Api::Api& api) PURE;

/**
* @return std::string the identifying name for a particular implementation of
Expand Down
1 change: 1 addition & 0 deletions include/envoy/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ envoy_cc_library(
hdrs = ["resource_monitor_config.h"],
deps = [
":resource_monitor_interface",
"//include/envoy/api:api_interface",
"//include/envoy/event:dispatcher_interface",
],
)
Expand Down
5 changes: 5 additions & 0 deletions include/envoy/server/filter_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ class FactoryContext {
* @return Http::Context& a reference to the http context.
*/
virtual Http::Context& httpContext() PURE;

/**
* @return Api::Api& a reference to the api object.
*/
virtual Api::Api& api() PURE;
};

class ListenerFactoryContext : public virtual FactoryContext {
Expand Down
6 changes: 6 additions & 0 deletions include/envoy/server/resource_monitor_config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "envoy/api/api.h"
#include "envoy/common/pure.h"
#include "envoy/event/dispatcher.h"
#include "envoy/server/resource_monitor.h"
Expand All @@ -19,6 +20,11 @@ class ResourceMonitorFactoryContext {
* for all singleton processing.
*/
virtual Event::Dispatcher& dispatcher() PURE;

/**
* @return reference to the Api object
*/
virtual Api::Api& api() PURE;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions include/envoy/server/transport_socket_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class TransportSocketFactoryContext {
* @return the server's TLS slot allocator.
*/
virtual ThreadLocal::SlotAllocator& threadLocal() PURE;

/**
* @return reference to the Api object
*/
virtual Api::Api& api() PURE;
};

class TransportSocketConfigFactory {
Expand Down
2 changes: 2 additions & 0 deletions include/envoy/upstream/cluster_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <unordered_map>

#include "envoy/access_log/access_log.h"
#include "envoy/api/api.h"
#include "envoy/api/v2/cds.pb.h"
#include "envoy/config/bootstrap/v2/bootstrap.pb.h"
#include "envoy/config/grpc_mux.h"
Expand Down Expand Up @@ -319,6 +320,7 @@ class ClusterInfoFactory {
Runtime::RandomGenerator& random_;
Singleton::Manager& singleton_manager_;
ThreadLocal::SlotAllocator& tls_;
Api::Api& api_;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion source/common/access_log/access_log_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Filesystem::FileSharedPtr AccessLogManagerImpl::createAccessLog(const std::strin
return access_logs_[file_name];
}

access_logs_[file_name] = api_.createFile(file_name, dispatcher_, lock_);
access_logs_[file_name] = api_.fileSystem().createFile(file_name, dispatcher_, lock_);
return access_logs_[file_name];
}

Expand Down
12 changes: 2 additions & 10 deletions source/common/api/api_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "common/common/thread.h"
#include "common/event/dispatcher_impl.h"
#include "common/filesystem/filesystem_impl.h"

namespace Envoy {
namespace Api {
Expand All @@ -19,16 +18,9 @@ Event::DispatcherPtr Impl::allocateDispatcher(Event::TimeSystem& time_system) {
return std::make_unique<Event::DispatcherImpl>(time_system, *this);
}

Filesystem::FileSharedPtr Impl::createFile(const std::string& path, Event::Dispatcher& dispatcher,
Thread::BasicLockable& lock) {
return file_system_.createFile(path, dispatcher, lock);
}

bool Impl::fileExists(const std::string& path) { return Filesystem::fileExists(path); }

std::string Impl::fileReadToEnd(const std::string& path) { return Filesystem::fileReadToEnd(path); }

Thread::ThreadFactory& Impl::threadFactory() { return thread_factory_; }

Filesystem::Instance& Impl::fileSystem() { return file_system_; }

} // namespace Api
} // namespace Envoy
10 changes: 3 additions & 7 deletions source/common/api/api_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,19 @@ namespace Api {
/**
* Implementation of Api::Api
*/
class Impl : public Api::Api {
class Impl : public Api {
public:
Impl(std::chrono::milliseconds file_flush_interval_msec, Thread::ThreadFactory& thread_factory,
Stats::Store& stats_store);

// Api::Api
Event::DispatcherPtr allocateDispatcher(Event::TimeSystem& time_system) override;
Filesystem::FileSharedPtr createFile(const std::string& path, Event::Dispatcher& dispatcher,
Thread::BasicLockable& lock) override;
bool fileExists(const std::string& path) override;
std::string fileReadToEnd(const std::string& path) override;
Thread::ThreadFactory& threadFactory() override;
Filesystem::Instance& fileSystem() { return file_system_; }
Filesystem::Instance& fileSystem() override;

private:
Thread::ThreadFactory& thread_factory_;
Filesystem::Instance file_system_;
Filesystem::InstanceImpl file_system_;
};

} // namespace Api
Expand Down
Loading