| 1 |
require "groonga-log" |
| 2 |
|
| 3 |
module Groonga |
| 4 |
module CommandLine |
| 5 |
class Grndb |
| 6 |
include Loggable |
| 7 |
|
| 8 |
module SafeStat |
| 9 |
def file_mtime(path) |
| 10 |
begin |
| 11 |
stat = File.stat(path) |
| 12 |
rescue SystemCallError => error |
| 13 |
failed("Failed to get last modified time: <#{path}>", |
| 14 |
error.message) |
| 15 |
Time.at(0) |
| 16 |
else |
| 17 |
stat.mtime |
| 18 |
end |
| 19 |
end |
| 20 |
|
| 21 |
def file_empty?(path) |
| 22 |
begin |
| 23 |
stat = File.stat(path) |
| 24 |
rescue SystemCallError => error |
| 25 |
failed("Failed to get file size: <#{path}>", |
| 26 |
error.message) |
| 27 |
true |
| 28 |
else |
| 29 |
stat.size.zero? |
| 30 |
end |
| 31 |
end |
| 32 |
end |
| 33 |
include SafeStat |
| 34 |
|
| 35 |
def initialize(argv) |
| 36 |
@program_path, *@arguments = argv |
| 37 |
@output = LocaleOutput.new($stderr) |
| 38 |
@succeeded = true |
| 39 |
@database_path = nil |
| 40 |
end |
| 41 |
|
| 42 |
def run |
| 43 |
command_line_parser = create_command_line_parser |
| 44 |
options = nil |
| 45 |
begin |
| 46 |
options = command_line_parser.parse(@arguments) |
| 47 |
rescue Slop::Error => error |
| 48 |
@output.puts(error.message) |
| 49 |
@output.puts |
| 50 |
@output.puts(command_line_parser.help_message) |
| 51 |
return false |
| 52 |
end |
| 53 |
@succeeded |
| 54 |
end |
| 55 |
|
| 56 |
private |
| 57 |
def create_command_line_parser |
| 58 |
program_name = File.basename(@program_path) |
| 59 |
parser = CommandLineParser.new(program_name) |
| 60 |
|
| 61 |
parser.add_command("check") do |command| |
| 62 |
command.description = "Check database" |
| 63 |
|
| 64 |
options = command.options |
| 65 |
options.banner += " DB_PATH" |
| 66 |
options.string("--target", "Check only the target object.") |
| 67 |
options.array("--groonga-log-path", |
| 68 |
"Path to Groonga log file to be checked. " + |
| 69 |
"You can specify multiple times to " + |
| 70 |
"specify multiple log files.") |
| 71 |
options.time("--since", |
| 72 |
"Check only objects that are modified " + |
| 73 |
"since the specified time. " + |
| 74 |
"You can use ISO 8601 format or " + |
| 75 |
"-${N}${UNIT} format such as " + |
| 76 |
"-3days and -2.5weeks.") |
| 77 |
|
| 78 |
command.add_action do |options| |
| 79 |
open_database(command, options) do |database, rest_arguments| |
| 80 |
check(database, options, rest_arguments) |
| 81 |
end |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
parser.add_command("recover") do |command| |
| 86 |
command.description = "Recover database" |
| 87 |
|
| 88 |
options = command.options |
| 89 |
options.banner += " DB_PATH" |
| 90 |
options.boolean("--force-truncate", |
| 91 |
"Force to truncate corrupted objects.") |
| 92 |
options.boolean("--force-lock-clear", |
| 93 |
"Force to clear lock of locked objects.") |
| 94 |
|
| 95 |
command.add_action do |options| |
| 96 |
open_database(command, options) do |database, rest_arguments| |
| 97 |
recover(database, options, rest_arguments) |
| 98 |
end |
| 99 |
end |
| 100 |
end |
| 101 |
|
| 102 |
parser.add_command("dump-wal") do |command| |
| 103 |
command.description = "Dump WAL in database" |
| 104 |
|
| 105 |
options = command.options |
| 106 |
options.banner += " DB_PATH" |
| 107 |
|
| 108 |
command.add_action do |options| |
| 109 |
open_database(command, options) do |database, rest_arguments| |
| 110 |
dump_wal(database, options, rest_arguments) |
| 111 |
end |
| 112 |
end |
| 113 |
end |
| 114 |
|
| 115 |
parser |
| 116 |
end |
| 117 |
|
| 118 |
def open_database(command, options) |
| 119 |
arguments = options.arguments |
| 120 |
if arguments.empty? |
| 121 |
@output.puts("Database path is missing") |
| 122 |
@output.puts |
| 123 |
@output.puts(command.help_message) |
| 124 |
@succeesed = false |
| 125 |
return |
| 126 |
end |
| 127 |
|
| 128 |
database = nil |
| 129 |
@database_path, *rest_arguments = arguments |
| 130 |
begin |
| 131 |
database = Database.open(@database_path) |
| 132 |
rescue Error => error |
| 133 |
@output.puts("Failed to open database: <#{@database_path}>") |
| 134 |
@output.puts(error.message) |
| 135 |
@succeeded = false |
| 136 |
return |
| 137 |
end |
| 138 |
|
| 139 |
begin |
| 140 |
yield(database, rest_arguments) |
| 141 |
ensure |
| 142 |
database.close |
| 143 |
end |
| 144 |
end |
| 145 |
|
| 146 |
def failed(*messages) |
| 147 |
messages.each do |message| |
| 148 |
@output.puts(message) |
| 149 |
logger.log(:error, message) |
| 150 |
end |
| 151 |
@succeeded = false |
| 152 |
end |
| 153 |
|
| 154 |
def check(database, options, arguments) |
| 155 |
message = "Checking database: <#{@database_path}>" |
| 156 |
since = options[:since] |
| 157 |
message << ": <#{since.iso8601}>" if since |
| 158 |
logger.log(:info, message) |
| 159 |
checker = Checker.new(@output) |
| 160 |
checker.program_path = @program_path |
| 161 |
checker.database_path = @database_path |
| 162 |
checker.since = since |
| 163 |
checker.database = database |
| 164 |
checker.log_paths = options[:groonga_log_path] |
| 165 |
checker.on_failure = lambda do |*messages| |
| 166 |
failed(*messages) |
| 167 |
end |
| 168 |
|
| 169 |
checker.check_log_paths |
| 170 |
|
| 171 |
checker.check_database |
| 172 |
|
| 173 |
target_name = options[:target] |
| 174 |
if target_name |
| 175 |
logger.log(:info, "Checking object: <#{target_name}>") |
| 176 |
checker.check_one(target_name) |
| 177 |
logger.log(:info, "Checked object: <#{target_name}>") |
| 178 |
else |
| 179 |
checker.check_all |
| 180 |
end |
| 181 |
logger.log(:info, "Checked database: <#{@database_path}>") |
| 182 |
end |
| 183 |
|
| 184 |
def recover(database, options, arguments) |
| 185 |
recoverer = Recoverer.new(@output) |
| 186 |
recoverer.database = database |
| 187 |
recoverer.force_truncate = options[:force_truncate] |
| 188 |
recoverer.force_lock_clear = options[:force_lock_clear] |
| 189 |
begin |
| 190 |
recoverer.recover |
| 191 |
rescue Error => error |
| 192 |
failed("Failed to recover database: <#{@database_path}>", |
| 193 |
error.message) |
| 194 |
end |
| 195 |
end |
| 196 |
|
| 197 |
def dump_wal(database, options, arguments) |
| 198 |
dumper = WALDumper.new(@output) |
| 199 |
dumper.database = database |
| 200 |
begin |
| 201 |
dumper.dump |
| 202 |
rescue Error => error |
| 203 |
failed("Failed to dump WAL in database: <#{@database_path}>", |
| 204 |
error.message) |
| 205 |
end |
| 206 |
end |
| 207 |
|
| 208 |
class Checker |
| 209 |
include Loggable |
| 210 |
include SafeStat |
| 211 |
|
| 212 |
attr_writer :program_path |
| 213 |
attr_writer :database_path |
| 214 |
attr_writer :since |
| 215 |
attr_writer :database |
| 216 |
attr_writer :log_paths |
| 217 |
attr_writer :on_failure |
| 218 |
|
| 219 |
def initialize(output) |
| 220 |
@output = output |
| 221 |
@context = Context.instance |
| 222 |
@checked = {} |
| 223 |
end |
| 224 |
|
| 225 |
def check_log_paths |
| 226 |
@log_paths.each do |log_path| |
| 227 |
begin |
| 228 |
log_file = File.new(log_path) |
| 229 |
rescue => error |
| 230 |
message = "[#{log_path}] Can't open Groonga log path: " |
| 231 |
message << "#{error.class}: #{error.message}" |
| 232 |
failed(message) |
| 233 |
next |
| 234 |
end |
| 235 |
|
| 236 |
begin |
| 237 |
check_log_file(log_file) |
| 238 |
ensure |
| 239 |
log_file.close |
| 240 |
end |
| 241 |
end |
| 242 |
end |
| 243 |
|
| 244 |
def check_database |
| 245 |
return unless target_database? |
| 246 |
|
| 247 |
check_database_orphan_inspect |
| 248 |
check_database_locked |
| 249 |
check_database_corrupt |
| 250 |
check_database_dirty |
| 251 |
check_database_empty_files |
| 252 |
end |
| 253 |
|
| 254 |
def check_one(target_name) |
| 255 |
open_object(target_name) do |target| |
| 256 |
check_object_recursive(target) |
| 257 |
end |
| 258 |
end |
| 259 |
|
| 260 |
def check_all |
| 261 |
open_database_cursor do |cursor| |
| 262 |
cursor.each do |id| |
| 263 |
next if ID.builtin?(id) |
| 264 |
next if builtin_object_name?(cursor.key) |
| 265 |
open_object(id) do |object| |
| 266 |
check_object(object) |
| 267 |
end |
| 268 |
end |
| 269 |
end |
| 270 |
end |
| 271 |
|
| 272 |
private |
| 273 |
def check_log_file(log_file) |
| 274 |
return |
| 275 |
|
| 276 |
parser = GroongaLog::Parser.new |
| 277 |
parser.parse(log_file) do |statistic| |
| 278 |
p statistic.to_h |
| 279 |
end |
| 280 |
end |
| 281 |
|
| 282 |
def check_database_orphan_inspect |
| 283 |
found = false |
| 284 |
open_database_cursor do |cursor| |
| 285 |
cursor.each do |id| |
| 286 |
if cursor.key == "inspect" and @context[id].nil? |
| 287 |
message = |
| 288 |
"Database has orphan 'inspect' object. " + |
| 289 |
"Remove it by '#{@program_path} recover #{@database_path}'." |
| 290 |
found = true |
| 291 |
failed(message) |
| 292 |
break |
| 293 |
end |
| 294 |
end |
| 295 |
end |
| 296 |
unless found |
| 297 |
logger.log(:info, |
| 298 |
"Database doesn't have orphan 'inspect' object: " + |
| 299 |
"<#{@database_path}>") |
| 300 |
end |
| 301 |
end |
| 302 |
|
| 303 |
def check_database_locked |
| 304 |
unless @database.locked? |
| 305 |
logger.log(:info, "Database is not locked: <#{@database_path}>") |
| 306 |
return |
| 307 |
end |
| 308 |
|
| 309 |
message = |
| 310 |
"Database is locked. " + |
| 311 |
"It may be broken. " + |
| 312 |
"Re-create the database." |
| 313 |
failed(message) |
| 314 |
end |
| 315 |
|
| 316 |
def check_database_corrupt |
| 317 |
unless @database.corrupt? |
| 318 |
logger.log(:info, "Database is not corrupted: <#{@database_path}>") |
| 319 |
return |
| 320 |
end |
| 321 |
|
| 322 |
message = |
| 323 |
"Database is corrupt. " + |
| 324 |
"Re-create the database." |
| 325 |
failed(message) |
| 326 |
end |
| 327 |
|
| 328 |
def check_database_dirty |
| 329 |
unless @database.dirty? |
| 330 |
logger.log(:info, "Database is not dirty: <#{@database_path}>") |
| 331 |
return |
| 332 |
end |
| 333 |
|
| 334 |
last_modified = @database.last_modified |
| 335 |
if file_mtime(@database.path) > last_modified |
| 336 |
return |
| 337 |
end |
| 338 |
|
| 339 |
open_database_cursor do |cursor| |
| 340 |
cursor.each do |id| |
| 341 |
next if ID.builtin?(id) |
| 342 |
path = object_path(id) |
| 343 |
next unless File.exist?(path) |
| 344 |
return if file_mtime(path) > last_modified |
| 345 |
end |
| 346 |
end |
| 347 |
|
| 348 |
message = |
| 349 |
"Database wasn't closed successfully. " + |
| 350 |
"It may be broken. " + |
| 351 |
"Re-create the database." |
| 352 |
failed(message) |
| 353 |
end |
| 354 |
|
| 355 |
def check_database_empty_files |
| 356 |
dirname = File.dirname(@database.path) |
| 357 |
basename = File.basename(@database.path) |
| 358 |
|
| 359 |
Dir.entries(dirname).sort.each do |path| |
| 360 |
next unless path.start_with?(basename) |
| 361 |
|
| 362 |
full_path = "#{dirname}/#{path}" |
| 363 |
if file_empty?(full_path) |
| 364 |
failed("Empty file exists: <#{full_path}>") |
| 365 |
end |
| 366 |
end |
| 367 |
end |
| 368 |
|
| 369 |
def check_object(object) |
| 370 |
return if @checked.key?(object.id) |
| 371 |
@checked[object.id] = true |
| 372 |
|
| 373 |
check_object_locked(object) |
| 374 |
check_object_corrupt(object) |
| 375 |
end |
| 376 |
|
| 377 |
def check_object_locked(object) |
| 378 |
case object |
| 379 |
when IndexColumn |
| 380 |
unless object.locked? |
| 381 |
logger.log(:info, "[#{object.name}] Index column is not locked") |
| 382 |
return |
| 383 |
end |
| 384 |
message = |
| 385 |
"[#{object.name}] Index column is locked. " + |
| 386 |
"It may be broken. " + |
| 387 |
"Re-create index by '#{@program_path} recover #{@database_path}'." |
| 388 |
failed(message) |
| 389 |
when Column |
| 390 |
unless object.locked? |
| 391 |
logger.log(:info, "[#{object.name}] Column is not locked") |
| 392 |
return |
| 393 |
end |
| 394 |
name = object.name |
| 395 |
message = |
| 396 |
"[#{name}] Data column is locked. " + |
| 397 |
"It may be broken. " + |
| 398 |
"(1) Truncate the column (truncate #{name}) or " + |
| 399 |
"clear lock of the column (lock_clear #{name}) " + |
| 400 |
"and (2) load data again." |
| 401 |
failed(message) |
| 402 |
when Table |
| 403 |
unless object.locked? |
| 404 |
logger.log(:info, "[#{object.name}] Table is not locked") |
| 405 |
return |
| 406 |
end |
| 407 |
name = object.name |
| 408 |
message = |
| 409 |
"[#{name}] Table is locked. " + |
| 410 |
"It may be broken. " + |
| 411 |
"(1) Truncate the table (truncate #{name}) or " + |
| 412 |
"clear lock of the table (lock_clear #{name}) " + |
| 413 |
"and (2) load data again." |
| 414 |
failed(message) |
| 415 |
end |
| 416 |
end |
| 417 |
|
| 418 |
def check_object_corrupt(object) |
| 419 |
case object |
| 420 |
when IndexColumn |
| 421 |
unless object.corrupt? |
| 422 |
logger.log(:info, "[#{object.name}] Index column is not corrupted") |
| 423 |
return |
| 424 |
end |
| 425 |
message = |
| 426 |
"[#{object.name}] Index column is corrupt. " + |
| 427 |
"Re-create index by '#{@program_path} recover #{@database_path}'." |
| 428 |
failed(message) |
| 429 |
when Column |
| 430 |
unless object.corrupt? |
| 431 |
logger.log(:info, "[#{object.name}] Column is not corrupted") |
| 432 |
return |
| 433 |
end |
| 434 |
name = object.name |
| 435 |
message = |
| 436 |
"[#{name}] Data column is corrupt. " + |
| 437 |
"(1) Truncate the column (truncate #{name} or " + |
| 438 |
"'#{@program_path} recover --force-truncate #{@database_path}') " + |
| 439 |
"and (2) load data again." |
| 440 |
failed(message) |
| 441 |
when Table |
| 442 |
unless object.corrupt? |
| 443 |
logger.log(:info, "[#{object.name}] Table is not corrupted") |
| 444 |
if object.is_a?(TableWithKey) |
| 445 |
check_table_with_key(object) |
| 446 |
end |
| 447 |
return |
| 448 |
end |
| 449 |
name = object.name |
| 450 |
message = |
| 451 |
"[#{name}] Table is corrupt. " + |
| 452 |
"(1) Truncate the table (truncate #{name} or " + |
| 453 |
"'#{@program_path} recover --force-truncate #{@database_path}') " + |
| 454 |
"and (2) load data again." |
| 455 |
failed(message) |
| 456 |
end |
| 457 |
end |
| 458 |
|
| 459 |
def check_table_with_key(table_with_key) |
| 460 |
name = table_with_key.name |
| 461 |
|
| 462 |
duplicated_keys = table_with_key.duplicated_keys |
| 463 |
begin |
| 464 |
if duplicated_keys.empty? |
| 465 |
logger.log(:info, "[#{name}] Table doesn't have duplicated keys") |
| 466 |
return |
| 467 |
end |
| 468 |
|
| 469 |
message = "[#{name}] Table has duplicated keys:\n" |
| 470 |
duplicated_keys.each do |id| |
| 471 |
record = Record.new(duplicated_keys, id) |
| 472 |
message << " <#{record.key}>:\n" |
| 473 |
record.records.each do |duplicated_record| |
| 474 |
message << " #{duplicated_record.id}\n" |
| 475 |
end |
| 476 |
end |
| 477 |
if table_with_key.lexicon_without_data_column? |
| 478 |
message << |
| 479 |
"Re-create lexicon by " + |
| 480 |
"'#{@program_path} recover #{@database_path}'." |
| 481 |
else |
| 482 |
message << |
| 483 |
"(1) Truncate the table (truncate #{name} or " + |
| 484 |
"'#{@program_path} recover --force-truncate #{@database_path}'" + |
| 485 |
") and (2) load data again." |
| 486 |
end |
| 487 |
failed(message) |
| 488 |
ensure |
| 489 |
duplicated_keys.close |
| 490 |
end |
| 491 |
end |
| 492 |
|
| 493 |
def check_object_recursive(target) |
| 494 |
return if @checked.key?(target.id) |
| 495 |
|
| 496 |
check_object(target) |
| 497 |
case target |
| 498 |
when Table |
| 499 |
unless target.is_a?(Groonga::Array) |
| 500 |
open_object(target.domain_id) do |domain| |
| 501 |
if domain.is_a?(Table) |
| 502 |
check_object_recursive(domain) |
| 503 |
end |
| 504 |
end |
| 505 |
end |
| 506 |
|
| 507 |
target.column_ids.each do |column_id| |
| 508 |
open_object(column_id) do |column| |
| 509 |
check_object_recursive(column) |
| 510 |
end |
| 511 |
end |
| 512 |
when FixedSizeColumn, VariableSizeColumn |
| 513 |
open_object(target.range_id) do |range| |
| 514 |
if range.is_a?(Table) |
| 515 |
check_object_recursive(range) |
| 516 |
end |
| 517 |
end |
| 518 |
|
| 519 |
lexicon_ids = [] |
| 520 |
target.indexes.each do |index_info| |
| 521 |
index = index_info.index |
| 522 |
lexicon_ids << index.domain_id |
| 523 |
next unless target_id?(index.id) |
| 524 |
check_object(index) |
| 525 |
end |
| 526 |
lexicon_ids.uniq.each do |lexicon_id| |
| 527 |
open_object(lexicon_id) do |lexicon| |
| 528 |
check_object(lexicon) |
| 529 |
end |
| 530 |
end |
| 531 |
when IndexColumn |
| 532 |
open_object(target.range_id) do |range| |
| 533 |
check_object(range) |
| 534 |
|
| 535 |
target.source_ids.each do |source_id| |
| 536 |
open_object(source_id) do |source| |
| 537 |
if source.is_a?(Column) |
| 538 |
check_object_recursive(source) |
| 539 |
end |
| 540 |
end |
| 541 |
end |
| 542 |
end |
| 543 |
end |
| 544 |
end |
| 545 |
|
| 546 |
def open_database_cursor(&block) |
| 547 |
flags = |
| 548 |
TableCursorFlags::ASCENDING | |
| 549 |
TableCursorFlags::BY_ID |
| 550 |
TableCursor.open(@database, :flags => flags, &block) |
| 551 |
end |
| 552 |
|
| 553 |
def builtin_object_name?(name) |
| 554 |
case name |
| 555 |
when "inspect" |
| 556 |
|
| 557 |
|
| 558 |
true |
| 559 |
else |
| 560 |
false |
| 561 |
end |
| 562 |
end |
| 563 |
|
| 564 |
def target_database? |
| 565 |
return true if @since.nil? |
| 566 |
|
| 567 |
suffixes = [ |
| 568 |
"", |
| 569 |
".0000000", |
| 570 |
".001", |
| 571 |
".conf", |
| 572 |
".options", |
| 573 |
] |
| 574 |
suffixes.each do |suffix| |
| 575 |
path = "#{@database.path}#{suffix}" |
| 576 |
next unless File.exist?(path) |
| 577 |
return true if file_mtime(path) >= @since |
| 578 |
end |
| 579 |
false |
| 580 |
end |
| 581 |
|
| 582 |
def object_path(id) |
| 583 |
"%s.%07X" % [@database.path, id] |
| 584 |
end |
| 585 |
|
| 586 |
def target_id?(id) |
| 587 |
return true if @since.nil? |
| 588 |
|
| 589 |
target_path = object_path(id) |
| 590 |
return false unless File.exist?(target_path) |
| 591 |
return true if file_mtime(target_path) >= @since |
| 592 |
|
| 593 |
dir_path = File.dirname(@database.path) |
| 594 |
Dir.open(dir_path) do |dir| |
| 595 |
dir.each do |path| |
| 596 |
full_path = File.join(dir_path, path) |
| 597 |
next unless full_path.start_with?(target_path) |
| 598 |
return true if file_mtime(full_path) >= @since |
| 599 |
end |
| 600 |
end |
| 601 |
false |
| 602 |
end |
| 603 |
|
| 604 |
def open_object(name_or_id) |
| 605 |
if name_or_id.is_a?(Integer) |
| 606 |
id = name_or_id |
| 607 |
return if id.nil? |
| 608 |
name = @database[id] |
| 609 |
else |
| 610 |
name = name_or_id |
| 611 |
id = @database[name_or_id] |
| 612 |
end |
| 613 |
|
| 614 |
if id.nil? |
| 615 |
failed("[#{name}] Not exist.") |
| 616 |
return |
| 617 |
end |
| 618 |
|
| 619 |
return unless target_id?(id) |
| 620 |
|
| 621 |
@context.with_temporary_open_space(true) do |
| 622 |
object = @context[name] |
| 623 |
if object.nil? |
| 624 |
failed_to_open(name) |
| 625 |
return |
| 626 |
end |
| 627 |
|
| 628 |
yield(object) |
| 629 |
end |
| 630 |
end |
| 631 |
|
| 632 |
def failed(*messages) |
| 633 |
@on_failure.call(*messages) |
| 634 |
end |
| 635 |
|
| 636 |
def failed_to_open(name) |
| 637 |
message = |
| 638 |
"[#{name}] Can't open object. " + |
| 639 |
"It's broken. " + |
| 640 |
"Re-create the object or the database." |
| 641 |
failed(message) |
| 642 |
end |
| 643 |
end |
| 644 |
|
| 645 |
class Recoverer |
| 646 |
include Loggable |
| 647 |
include SafeStat |
| 648 |
|
| 649 |
attr_writer :database |
| 650 |
attr_writer :force_truncate |
| 651 |
attr_writer :force_lock_clear |
| 652 |
|
| 653 |
def initialize(output) |
| 654 |
@output = output |
| 655 |
@context = Context.instance |
| 656 |
end |
| 657 |
|
| 658 |
def recover |
| 659 |
logger.log(:info, "Recovering database: <#{@database.path}>") |
| 660 |
if @force_truncate |
| 661 |
@database.each(use_temporary_open_space: true) do |object| |
| 662 |
next unless truncate_target?(object) |
| 663 |
truncate_broken_object(object) |
| 664 |
end |
| 665 |
end |
| 666 |
if @force_lock_clear |
| 667 |
clear_locks |
| 668 |
end |
| 669 |
remove_empty_files |
| 670 |
@database.recover |
| 671 |
logger.log(:info, "Recovered database: <#{@database.path}>") |
| 672 |
end |
| 673 |
|
| 674 |
private |
| 675 |
def truncate_target?(object) |
| 676 |
return true if object.corrupt? |
| 677 |
|
| 678 |
case object |
| 679 |
when IndexColumn |
| 680 |
|
| 681 |
false |
| 682 |
when TableWithKey |
| 683 |
if object.lexicon_without_data_column? |
| 684 |
|
| 685 |
false |
| 686 |
else |
| 687 |
return true if object.locked? |
| 688 |
return true if object.have_duplicated_keys? |
| 689 |
false |
| 690 |
end |
| 691 |
when Column, Table |
| 692 |
object.locked? |
| 693 |
else |
| 694 |
false |
| 695 |
end |
| 696 |
end |
| 697 |
|
| 698 |
def truncate_broken_object(object) |
| 699 |
name = object.name |
| 700 |
object_path = object.path |
| 701 |
object_dirname = File.dirname(object_path) |
| 702 |
object_basename = File.basename(object_path) |
| 703 |
object.truncate |
| 704 |
message = "[#{name}] Truncated broken object: <#{object_path}>" |
| 705 |
@output.puts(message) |
| 706 |
logger.log(:info, message) |
| 707 |
|
| 708 |
Dir.entries(object_dirname).sort.each do |path| |
| 709 |
next unless path.start_with?("#{object_basename}.") |
| 710 |
|
| 711 |
full_path = "#{object_dirname}/#{path}" |
| 712 |
begin |
| 713 |
File.unlink(full_path) |
| 714 |
message = |
| 715 |
"[#{name}] Removed broken object related file: <#{full_path}>" |
| 716 |
@output.puts(message) |
| 717 |
logger.log(:info, message) |
| 718 |
rescue Error => error |
| 719 |
message = |
| 720 |
"[#{name}] Failed to remove broken object related file: " + |
| 721 |
"<#{full_path}>: #{error.class}: #{error.message}" |
| 722 |
@output.puts(message) |
| 723 |
logger.log(:error, message) |
| 724 |
logger.log_error(error) |
| 725 |
end |
| 726 |
end |
| 727 |
end |
| 728 |
|
| 729 |
def clear_locks |
| 730 |
if @database.locked? |
| 731 |
@database.clear_lock |
| 732 |
logger.log(:info, "Clear locked database: <#{@database.path}>") |
| 733 |
end |
| 734 |
@database.each(use_temporary_open_space: true) do |object| |
| 735 |
case object |
| 736 |
when IndexColumn |
| 737 |
|
| 738 |
when Column, Table |
| 739 |
next unless object.locked? |
| 740 |
object.clear_lock |
| 741 |
logger.log(:info, |
| 742 |
"[#{object.name}] Clear locked object: " + |
| 743 |
"<#{object.path}>") |
| 744 |
end |
| 745 |
end |
| 746 |
end |
| 747 |
|
| 748 |
def remove_empty_files |
| 749 |
dirname = File.dirname(@database.path) |
| 750 |
basename = File.basename(@database.path) |
| 751 |
|
| 752 |
broken_ids = {} |
| 753 |
Dir.entries(dirname).sort.each do |path| |
| 754 |
next unless path.start_with?(basename) |
| 755 |
|
| 756 |
full_path = "#{dirname}/#{path}" |
| 757 |
next unless file_empty?(full_path) |
| 758 |
|
| 759 |
match_data = |
| 760 |
/\A#{Regexp.escape(basename)}\.([\da-fA-F]{7})/.match(path) |
| 761 |
if match_data |
| 762 |
id = Integer(match_data[1], 16) |
| 763 |
name = @database[id] |
| 764 |
if name |
| 765 |
broken_ids[id] = true |
| 766 |
next |
| 767 |
end |
| 768 |
end |
| 769 |
|
| 770 |
begin |
| 771 |
File.unlink(full_path) |
| 772 |
message = "Removed empty file: <#{full_path}>" |
| 773 |
@output.puts(message) |
| 774 |
logger.log(:info, message) |
| 775 |
rescue Error => error |
| 776 |
message = |
| 777 |
"Failed to remove empty file: <#{full_path}>: " + |
| 778 |
"#{error.class}: #{error.message}" |
| 779 |
logger.log(:error, message) |
| 780 |
logger.log_error(error) |
| 781 |
end |
| 782 |
end |
| 783 |
|
| 784 |
broken_ids.each_key do |id| |
| 785 |
name = @database[id] |
| 786 |
broken_basename = "%s.%07x" % [basename, id] |
| 787 |
|
| 788 |
begin |
| 789 |
Object.remove_force(name) |
| 790 |
message = |
| 791 |
"[#{name}] Remove a broken object that has empty file: " + |
| 792 |
"<#{dirname}/#{broken_basename}>" |
| 793 |
@output.puts(message) |
| 794 |
logger.log(:info, message) |
| 795 |
rescue Error => error |
| 796 |
message = |
| 797 |
"[#{name}] Failed to remove a broken object " + |
| 798 |
"that has empty file: <#{dirname}/#{broken_basename}>: " + |
| 799 |
"#{error.class}: #{error.message}" |
| 800 |
logger.log(:error, message) |
| 801 |
logger.log_error(error) |
| 802 |
next |
| 803 |
end |
| 804 |
|
| 805 |
Dir.entries(dirname).sort.each do |path| |
| 806 |
next unless path.start_with?(broken_basename) |
| 807 |
|
| 808 |
full_path = "#{dirname}/#{path}" |
| 809 |
|
| 810 |
begin |
| 811 |
File.unlink(full_path) |
| 812 |
message = |
| 813 |
"[#{name}] Removed a file for broken object " + |
| 814 |
"that has empty file: <#{full_path}>" |
| 815 |
@output.puts(message) |
| 816 |
logger.log(:info, message) |
| 817 |
rescue Error => error |
| 818 |
message = |
| 819 |
"[#{name}] Failed to removed a file for broken object " + |
| 820 |
"that has empty file: <#{full_path}>: " + |
| 821 |
"#{error.class}: #{error.message}" |
| 822 |
logger.log(:error, message) |
| 823 |
logger.log_error(error) |
| 824 |
end |
| 825 |
end |
| 826 |
end |
| 827 |
end |
| 828 |
end |
| 829 |
|
| 830 |
class WALDumper |
| 831 |
include Loggable |
| 832 |
|
| 833 |
attr_writer :database |
| 834 |
|
| 835 |
def initialize(output) |
| 836 |
@output = output |
| 837 |
@context = Context.instance |
| 838 |
end |
| 839 |
|
| 840 |
def dump |
| 841 |
@database.each(use_temporary_open_space: true) do |object| |
| 842 |
case object |
| 843 |
when Procedure |
| 844 |
when Type |
| 845 |
else |
| 846 |
object.dump_wal |
| 847 |
end |
| 848 |
end |
| 849 |
end |
| 850 |
end |
| 851 |
end |
| 852 |
end |
| 853 |
end |