Class: Droonga::EngineNode

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/droonga/engine_node.rb

Constant Summary collapse

DEFAULT_AUTO_CLOSE_TIMEOUT_SECONDS =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ EngineNode

Returns a new instance of EngineNode.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/droonga/engine_node.rb', line 33

def initialize(params)
  @loop  = params[:loop]
  @name  = params[:name]
  @state = params[:state]
  logger.trace("initialize: start")

  @buffer = create_buffer

  @node_name = NodeName.parse(@name)

  @sender = nil
  @auto_close_timer = nil
  @auto_close_timeout = params[:auto_close_timeout] ||
                          DEFAULT_AUTO_CLOSE_TIMEOUT_SECONDS

  logger.trace("initialize: done")
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/droonga/engine_node.rb', line 31

def name
  @name
end

Instance Method Details

#bounce(message) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/droonga/engine_node.rb', line 100

def bounce(message)
  destination = {
    "to"   => name,
    "type" => message["type"],
  }
  output(message, destination)
end

#forward(message, destination) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/droonga/engine_node.rb', line 77

def forward(message, destination)
  if read_message?(message)
    # A node can receive read messages for other nodes,
    # while changing its role. They must not be buffered.
    output(message, destination)
    return
  end

  unless really_writable?
    # The target node is not ready. We should send the message later.
    @buffer.add(message, destination)
    return
  end

  # The target node is ready.
  if @buffer.empty?
    output(message, destination)
  else
    @buffer.add(message, destination)
    @buffer.start_forward
  end
end

#forwardable?Boolean

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/droonga/engine_node.rb', line 121

def forwardable?
  return false unless live?
  role == sender_role
end

#live?Boolean

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/droonga/engine_node.rb', line 116

def live?
  @state.nil? or
    @state["live"] == true
end

#readable?Boolean

Returns:

  • (Boolean)


126
127
128
129
# File 'lib/droonga/engine_node.rb', line 126

def readable?
  forwardable? and @buffer.empty? and
    (complete_service_provider? or not service_provider?)
end

#refresh_connectionObject



70
71
72
73
74
75
# File 'lib/droonga/engine_node.rb', line 70

def refresh_connection
  logger.trace("refresh_connection: start")
  shutdown
  sender # instantiate new sender
  logger.trace("refresh_connection: done")
end

#resumeObject



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/droonga/engine_node.rb', line 165

def resume
  logger.trace("resume: start")
  sender.resume
  unless @buffer.empty?
    if really_writable?
      logger.info("Target becomes writable. Start to forwarding.")
      @buffer.start_forward
    else
      logger.info("Target is still unwritable.")
    end
  end
  logger.trace("resume: done")
end

#roleObject



108
109
110
111
112
113
114
# File 'lib/droonga/engine_node.rb', line 108

def role
  if @state
    NodeRole.normalize(@state["role"])
  else
    NodeRole::SERVICE_PROVIDER
  end
end

#shutdownObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/droonga/engine_node.rb', line 57

def shutdown
  logger.trace("shutdown: start")
  if @sender
    @sender.shutdown
    @sender = nil
  end
  if @auto_close_timer
    @auto_close_timer.detach
    @auto_close_timer = nil
  end
  logger.trace("shutdown: done")
end

#startObject



51
52
53
54
55
# File 'lib/droonga/engine_node.rb', line 51

def start
  logger.trace("start: start")
  resume
  logger.trace("start: done")
end

#statusObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/droonga/engine_node.rb', line 144

def status
  if readable?
    "active"
  elsif forwardable?
    "inactive"
  elsif dead?
    "dead"
  else
    "inactive"
  end
end

#to_jsonObject



156
157
158
159
160
161
162
163
# File 'lib/droonga/engine_node.rb', line 156

def to_json
  {
    "name"   => name,
    "role"   => role,
    "live"   => live?,
    "status" => status,
  }
end

#writable?Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/droonga/engine_node.rb', line 131

def writable?
  case sender_role
  when NodeRole::SERVICE_PROVIDER
    true
  when NodeRole::ABSORB_SOURCE
    absorb_source?
  when NodeRole::ABSORB_DESTINATION
    absorb_destination?
  else
    false
  end
end