Class: Fluent::Plugin::LocalStorage

Inherits:
Storage show all
Defined in:
lib/fluent/plugin/storage_local.rb

Constant Summary collapse

DEFAULT_DIR_MODE =
0755
DEFAULT_FILE_MODE =
0644

Constants inherited from Storage

Storage::DEFAULT_TYPE

Constants included from Configurable

Configurable::CONFIG_TYPE_REGISTRY

Instance Attribute Summary

Attributes inherited from Storage

#log

Attributes inherited from Base

#under_plugin_development

Instance Method Summary collapse

Methods inherited from Storage

#implementation, #persistent_always?, #synchronized?, validate_key

Methods included from OwnedByMixin

#log, #owner, #owner=

Methods inherited from Base

#after_shutdown, #after_shutdown?, #after_start, #after_started?, #before_shutdown, #before_shutdown?, #close, #closed?, #configured?, #has_router?, #inspect, #shutdown, #shutdown?, #start, #started?, #stop, #stopped?, #terminate, #terminated?

Methods included from SystemConfig::Mixin

#system_config, #system_config_override

Methods included from Configurable

#config, included, lookup_type, register_type

Constructor Details

#initializeLocalStorage

Returns a new instance of LocalStorage.



36
37
38
39
# File 'lib/fluent/plugin/storage_local.rb', line 36

def initialize
  super
  @store = {}
end

Instance Method Details

#configure(conf) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fluent/plugin/storage_local.rb', line 41

def configure(conf)
  super

  @on_memory = false
  if !@path && !@_plugin_id_configured
    if @persistent
      raise Fluent::ConfigError, "Plugin @id or path for <storage> required to save data"
    else
      if @autosave
        log.warn "both of Plugin @id and path for <storage> are not specified. Using on-memory store."
      else
        log.info "both of Plugin @id and path for <storage> are not specified. Using on-memory store."
      end
      @on_memory = true
    end
  elsif @path
    # ok
  else # @_plugin_id_configured is true
    log.warn "path for <storage> is not specified. Using on-memory store temporarily, but will use file store after support global storage path"
    @on_memory = true
    ## 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: 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 '#{dir}'" unless File.writable?(dir)
    end
  end
end

#delete(key) ⇒ Object



124
125
126
# File 'lib/fluent/plugin/storage_local.rb', line 124

def delete(key)
  @store.delete(key.to_s)
end

#fetch(key, defval) ⇒ Object



116
117
118
# File 'lib/fluent/plugin/storage_local.rb', line 116

def fetch(key, defval)
  @store.fetch(key.to_s, defval)
end

#get(key) ⇒ Object



112
113
114
# File 'lib/fluent/plugin/storage_local.rb', line 112

def get(key)
  @store[key.to_s]
end

#loadObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fluent/plugin/storage_local.rb', line 83

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: e
  end
end

#put(key, value) ⇒ Object



120
121
122
# File 'lib/fluent/plugin/storage_local.rb', line 120

def put(key, value)
  @store[key.to_s] = value
end

#saveObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fluent/plugin/storage_local.rb', line 100

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: e
  end
end

#update(key, &block) ⇒ Object



128
129
130
# File 'lib/fluent/plugin/storage_local.rb', line 128

def update(key, &block)
  @store[key.to_s] = block.call(@store[key.to_s])
end