Class: Norikra::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/norikra/cli.rb

Constant Summary collapse

JVM_OPTIONS =

JVM defaults w/ ConcurrentGC:

 java -XX:+UseConcMarkSweepGC -XX:+PrintFlagsFinal -version
 (jruby does not modify flags)

CMSIncrementalMode: not recommended in Java8

CMSInitiatingOccupancyFraction: CMS threshold
CMSInitiatingOccupancyFraction = 100 - MinHeapFreeRatio + CMSTriggerRatio * MinHeapFreeRatio / 100
  default: MinHeapFreeRatio=40 CMSTriggerRatio=80 -> CMSInitiatingOccupancyFraction=92

NewRatio=7 (New:Old = 1:7)
InitialSurvivorRatio=8     MinSurvivorRatio=3     SurvivorRatio=8 (Eden:Survivor0:survivor1 = 8:1:1)

MaxTenuringThreshold=4
TargetSurvivorRatio=50
 (InitialTenuringThreshold=7 is for Parallel GC/UseAdaptiveSizePolicy)

SoftRefLRUPolicyMSPerMB=1000
 ( gc_interval > free_heap * ms_per_mb : clear softref )
[
  '-XX:-UseGCOverheadLimit',
  '-XX:+UseConcMarkSweepGC', '-XX:+UseCompressedOops',
  '-XX:CMSInitiatingOccupancyFraction=70', '-XX:+UseCMSInitiatingOccupancyOnly',
  '-XX:NewRatio=1',
  '-XX:SurvivorRatio=2', '-XX:MaxTenuringThreshold=15', '-XX:TargetSurvivorRatio=80',
  '-XX:SoftRefLRUPolicyMSPerMB=200',
]
JVM_GC_OPTIONS =
[
  '-verbose:gc', '-XX:+PrintGCDetails', '-XX:+PrintGCDateStamps',
  '-XX:+HeapDumpOnOutOfMemoryError',
]

Instance Method Summary collapse

Instance Method Details

#serverprocObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/norikra/cli.rb', line 193

def serverproc
  conf = {}

  if options[:daemonize]
    conf[:daemonize] = {outfile: options[:outfile]}
  end

  ### stat file
  conf[:stats] = {
    path: options[:stats], secondary_path: options[:'stats-secondary'],
    suppress: options[:'suppress-dump-stat'], interval: options[:'dump-stat-interval'],
  }

  ### shut off mode
  conf[:shutoff] = {
    enabled: options[:shutoff],
    threshold: options[:'shutoff-threshold'],
    interval: options[:'shutoff-check-interval'],
  }

  ### threads
  predefined_selecteds = [:micro, :small, :middle, :large].select{|sym| options[sym]}
  if predefined_selecteds.size > 1
    raise Norikra::ConfigurationError, "one of micro/small/middle/large should be specified"
  end
  conf[:thread] = {
    predefined: predefined_selecteds.first,
    micro: options[:micro], small: options[:small], middle: options[:middle], large: options[:large],
    engine: {inbound:{}, outbound:{}, route_exec:{}, timer_exec:{}},
    rpc: {},
    web: {},
  }
  [:inbound, :outbound, :route_exec, :timer_exec].each do |sym|
    opt_sym = case sym
              when :route_exec then :route
              when :timer_exec then :timer
              else sym
              end
    conf[:thread][:engine][sym][:threads] = options[:"#{opt_sym}-threads"] if options[:"#{opt_sym}-threads"]
    conf[:thread][:engine][sym][:capacity] = options[:"#{opt_sym}-thread-capacity"] if options[:"#{opt_sym}-thread-capacity"]
  end
  conf[:thread][:rpc][:threads] = options[:'rpc-threads'] if options[:'rpc-threads']
  conf[:thread][:web][:threads] = options[:'web-threads'] if options[:'web-threads']

  ### logs
  loglevel = case
             when options[:'more-verbose'] then 'TRACE'
             when options[:verbose]        then 'DEBUG'
             when options[:quiet]          then 'WARN'
             when options[:'more-quiet']   then 'ERROR'
             else nil # for default (assumed as 'INFO')
             end
  conf[:log] = {
    level: loglevel, dir: options[:logdir],
    filesize: options[:'log-filesize'], backups: options[:'log-backups'],
    bufferlines: options[:'log-buffer-lines'],
  }
  conf[:log4j_properties_path] = options[:'log4j-properties-path']

  server_options = {
    host: options[:host],
    port: options[:port],
    ui_port: options[:'ui-port'],
    ui_context_path: options[:'ui-context-path'],
  }

  server = Norikra::Server.new( server_options, conf )
  server.run
  server.shutdown
end

#start(*optargs) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/norikra/cli.rb', line 128

def start(*optargs)
  if options[:help]
    invoke :help, ["start"]
    return
  end

  ARGV.shift # shift head "start"

  argv = ["serverproc"]
  jruby_options = ['-J-server']

  unless options[:'bare-jvm']
    jruby_options += JVM_OPTIONS.map{|opt| '-J' + opt }
  end

  if options[:'gc-log']
    jruby_options += JVM_GC_OPTIONS.map{|opt| '-J' + opt }
    jruby_options.push "-J-Xloggc:#{options[:'gc-log']}"
  end

  ARGV.each do |arg|
    if arg =~ /^-X(.+)$/
      jruby_options.push('-J-X' + $1)
    elsif arg =~ /^-verbose:gc$/
      jruby_options.push('-J-verbose:gc')
    elsif arg =~ /^-javaagent:(.+)$/
      jruby_options.push('-J-javaagent:' + $1)
    else
      argv.push(arg)
    end
  end

  # norikra/lib/norikra
  binpath = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'norikra'))

  jruby_path = RbConfig.ruby
  args = jruby_options + [binpath] + argv

  if options[:daemonize]
    unless options[:logdir]
      puts "'logdir' must be specified for '--daemonize'."
      exit(1)
    end

    ## need to close/reopen STDIN/STDOUT/STDERR in child process
    outfile = options[:outfile] || File.join(options[:logdir], '/norikra.out')
    File.open(outfile, 'w'){|file| file.write 'write test on parent process'}

    pidfile = File.open(options[:pidfile], 'w')
    pid = spawn(jruby_path, *args, pgroup: 0)
    pidfile.write(pid.to_s)
    pidfile.close
    waiting_child = true
    while waiting_child && sleep(1)
      out = File.open(outfile){|f| f.read}
      waiting_child = false if out =~ /working on #{pid}/
    end
    Process.detach(pid)
  else
    exec(jruby_path, *args)
  end
end

#stopObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/norikra/cli.rb', line 268

def stop
  unless test(?r,options[:pidfile])
    puts "Cannot find pidfile at #{options[:pidfile]}"
    exit(1)
  end
  pid = File.open(options[:pidfile]){|f| f.read}.to_i
  timeout = Time.now + options[:timeout]
  waiting = true
  Process.kill(:TERM, pid)
  begin
    while waiting && Time.now < timeout
      sleep(0.5)
      status = Process.waitpid(pid, Process::WNOHANG)
      if status
        waiting = false
      end
    end
  rescue Errno::ECHILD
    waiting = false
  end
  if waiting
    puts "Faild to stop Norikra server #{pid}"
    exit(1)
  else
    File.unlink(options[:pidfile])
  end
end