-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce plugin storage #864
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6c6ede4
Make mixin as a submodule of SystemConfig class to enclose namespaces
tagomoris 9c087bb
adding Plugin Storages
tagomoris 6c0d4ea
add for tests
tagomoris f3c84bc
add tests for plugin storages
tagomoris 36e4daa
fix APIs how to access plugin id and system configs
tagomoris 302f775
add json plugin storage as a reference (it still requires common stor…
tagomoris 938dc5e
fix test code along with fixed storage plugin API
tagomoris 4efdbe3
fix to use different name from others
tagomoris 67794f9
rename storage type from "json" to "local"
tagomoris a63ee6c
uncapitalize log messages
tagomoris 4d29491
simplify argument check and add raising error for unexpected type
tagomoris 600b98d
fix for refactor of SystemConfig mixin
tagomoris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'fluent/plugin' | ||
require 'fluent/configurable' | ||
|
||
module Fluent | ||
module Plugin | ||
class Storage | ||
include Fluent::Configurable | ||
include Fluent::SystemConfig::Mixin | ||
|
||
DEFAULT_TYPE = 'local' | ||
|
||
config_param :persistent, :bool, default: false # load/save with all operations | ||
config_param :autosave, :bool, default: true | ||
config_param :autosave_interval, :time, default: 10 | ||
config_param :save_at_shutdown, :bool, default: true | ||
|
||
def self.validate_key(key) | ||
raise ArgumentError, "key must be a string (or symbol for to_s)" unless key.is_a?(String) || key.is_a?(Symbol) | ||
key.to_s | ||
end | ||
|
||
attr_accessor :log | ||
|
||
def configure(conf) | ||
super(conf) | ||
|
||
@_owner = nil | ||
end | ||
|
||
def plugin_id(id, configured) | ||
@_plugin_id = id | ||
@_plugin_id_configured = configured | ||
end | ||
|
||
def owner=(plugin) | ||
@_owner = plugin | ||
|
||
@_plugin_id = plugin.plugin_id | ||
@_plugin_id_configured = plugin.plugin_id_configured? | ||
|
||
@log = plugin.log | ||
end | ||
|
||
def owner | ||
@_owner | ||
end | ||
|
||
def persistent_always? | ||
false | ||
end | ||
|
||
def synchronized? | ||
false | ||
end | ||
|
||
def implementation | ||
self | ||
end | ||
|
||
def load | ||
# load storage data from any data source, or initialize storage internally | ||
end | ||
|
||
def save | ||
# save internal data store into data source (to be loaded) | ||
end | ||
|
||
def get(key) | ||
raise NotImplementedError, "Implement this method in child class" | ||
end | ||
|
||
def fetch(key, defval) | ||
raise NotImplementedError, "Implement this method in child class" | ||
end | ||
|
||
def put(key, value) | ||
# return value | ||
raise NotImplementedError, "Implement this method in child class" | ||
end | ||
|
||
def delete(key) | ||
# return deleted value | ||
raise NotImplementedError, "Implement this method in child class" | ||
end | ||
|
||
def update(key, &block) # transactional get-and-update | ||
raise NotImplementedError, "Implement this method in child class" | ||
end | ||
|
||
# storage plugins has only 'close' and 'terminate' | ||
# stop: used in helper to stop autosave | ||
# shutdown: used in helper to call #save finally if needed | ||
def close; end | ||
def terminate | ||
@_owner = nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
require 'fluent/plugin' | ||
require 'fluent/plugin/storage' | ||
|
||
require 'fileutils' | ||
require 'yajl' | ||
|
||
module Fluent | ||
module Plugin | ||
class LocalStorage < Storage | ||
Fluent::Plugin.register_storage('local', self) | ||
|
||
DEFAULT_DIR_MODE = 0755 | ||
DEFAULT_FILE_MODE = 0644 | ||
|
||
config_param :path, :string, default: nil | ||
config_param :mode, :integer, default: DEFAULT_FILE_MODE | ||
config_param :dir_mode, :integer, default: DEFAULT_DIR_MODE | ||
config_param :pretty_print, :bool, default: false | ||
|
||
def initialize | ||
super | ||
@store = {} | ||
end | ||
|
||
def configure(conf, plugin) | ||
super | ||
|
||
@on_memory = false | ||
if !@path && !@_plugin_id_configured | ||
if @autosave || @persistent | ||
raise Fluent::ConfigError, "Plugin @id or path for <storage> required to save data" | ||
else | ||
log.info "both of Plugin @id and path for <storage> are not specified. Using on-memory store." | ||
@on_memory = true | ||
end | ||
elsif @path | ||
path = @path.dup | ||
else # @_plugin_id_configured | ||
## TODO: get process-wide directory for plugin storage, and generate path for this plugin storage instance | ||
# path = | ||
end | ||
|
||
if !@on_memory | ||
dir = File.dirname(@path) | ||
FileUtils.mkdir_p(dir, mode: @dir_mode) unless File.exist?(dir) | ||
if File.exist?(@path) | ||
raise Fluent::ConfigError, "Plugin storage path '#{@path}' is not readable/writable" unless File.readable?(@path) && File.writable?(@path) | ||
begin | ||
data = Yajl::Parser.parse(open(@path, 'r:utf-8'){ |io| io.read }) | ||
raise Fluent::ConfigError, "Invalid contents (not object) in plugin storage file: '#{@path}'" unless data.is_a?(Hash) | ||
rescue => e | ||
log.error "failed to read data from plugin storage file", path: @path, error_class: e.class, error: e | ||
raise Fluent::ConfigError, "Unexpected error: failed to read data from plugin storage file: '#{@path}'" | ||
end | ||
else | ||
raise Fluent::ConfigError, "Directory is not writable for plugin storage file '#{@path}'" unless File.writable?(@path) | ||
end | ||
end | ||
end | ||
|
||
def load | ||
return if @on_memory | ||
return unless File.exist?(@path) | ||
begin | ||
json_string = open(@path, 'r:utf-8'){ |io| io.read } | ||
json = Yajl::Parser.parse(json_string) | ||
unless json.is_a?(Hash) | ||
log.error "broken content for plugin storage (Hash required: ignored)", type: json.class | ||
log.debug "broken content", content: json_string | ||
return | ||
end | ||
@store = json | ||
rescue => e | ||
log.error "failed to load data for plugin storage from file", path: @path, error_class: e.class, error: e | ||
end | ||
end | ||
|
||
def save | ||
return if @on_memory | ||
tmp_path = @path + '.tmp' | ||
begin | ||
json_string = Yajl::Encoder.encode(@store, pretty: @pretty_print) | ||
open(tmp_path, 'w:utf-8', @mode){ |io| io.write json_string } | ||
File.rename(tmp_path, @path) | ||
rescue => e | ||
log.error "failed to save data for plugin storage to file", path: @path, tmp: tmp_path, error_class: e.class, error: e | ||
end | ||
end | ||
|
||
def get(key) | ||
@store[key.to_s] | ||
end | ||
|
||
def fetch(key, defval) | ||
@store.fetch(key.to_s, defval) | ||
end | ||
|
||
def put(key, value) | ||
@store[key.to_s] = value | ||
end | ||
|
||
def delete(key) | ||
@store.delete(key.to_s) | ||
end | ||
|
||
def update(key, &block) | ||
@store[key.to_s] = block.call(@store[key.to_s]) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fluentd, log message starts lower-case by default.
Other case too.