Class: Braavos::Service::Cassandra

Inherits:
Object
  • Object
show all
Defined in:
lib/braavos/service/cassandra.rb

Constant Summary collapse

DEFAULT_KEYSPACES_SYSTEM =
['system', 'system_auth', 'system_traces']

Instance Method Summary collapse

Instance Method Details

#backup_fullObject



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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/braavos/service/cassandra.rb', line 43

def backup_full
  backup_id = Braavos::Service.full_backup_id
  backup_path = File.join(Braavos.config.backup_path, 'full', backup_id, Braavos.storage.find_node_id)
  data_path = File.join(Braavos.config.backup_path, 'data', Braavos.storage.find_node_id)

  if Braavos.storage.has_success?(backup_path)
    raise "Backup currently exists: #{backup_id} - #{Braavos.storage.script_path(backup_path)}"
  else
    Braavos.storage.clear_result(backup_path)
  end

  Dir.mktmpdir('brav-bkup') do |tmpd|
    File.write(File.join(tmpd, 'cluster.json'), JSON.pretty_generate(Braavos.storage.get_cluster))

    write_whoami(tmpd)
    keyspaces_data.each do |keyspace|
      write_describering(tmpd, keyspace)
    end

    Braavos.command.execute(Braavos.template.load_template('cassandra/system_bundle.sh.erb'), [tmpd, Braavos.config.data_loc, *keyspaces_system])

    Braavos.parallel.execute(Braavos.template.load_template('cassandra/dump_schema.sh.erb', local_host_ip: Braavos::Service.local_host_ip), keyspaces_data.map{|k| [k, tmpd]})

    Dir["#{tmpd}/*"].each do |f|
      Braavos.storage.write_file(File.join(backup_path, File.basename(f)), file: f)
    end
  end

  script = Braavos.template.load_template('cassandra/create_snapshot.sh.erb')
  keyspaces_data.each do |keyspace|
    Braavos.command.execute(script, [keyspace, backup_id])
  end

  table_list = list_tables("snapshots/#{backup_id}")

  contents = Hash[tables: table_list.map do |t| "#{t[1]}.tgz" end]
  Braavos.storage.write_file(File.join(backup_path, 'contents.json'), JSON.pretty_generate(contents))

  # remove tables that are unchanged
  data_listings = Braavos.storage.list_dir(data_path).map do |k, v|
    k.sub(/^#{data_path}\//, '').sub(/\.tgz\Z/, '')
  end
  table_list.delete_if do |t|
    data_listings.include? t[1]
  end
  Braavos.logger.debug("executing table_list: #{table_list}")
  Braavos.logger.info("Processing Table Count: #{table_list.size}")

  script = Braavos.template.load_template('cassandra/table_bundle_upload.sh.erb')
  script_input = table_list.map do |t|
    [t[0], Braavos.storage.script_path(File.join(data_path, "#{t[1]}.tgz"))]
  end
  Braavos.parallel.execute(script, script_input) if script_input.size > 0

  Braavos.storage.write_file(File.join(backup_path, '_COMPLETED'), '')
rescue => e
  begin
    Braavos.storage.write_file(File.join(backup_path, '_FAILED'), "#{e.message}\n#{e.backtrace}")
  rescue => ig
    Braavos.logger.error("_FAILED failed, ignoring")
  end
  raise e
ensure
  script = Braavos.template.load_template('cassandra/clear_snapshot.sh.erb')
  keyspaces_data.each do |keyspace|
    begin
      Braavos.command.execute(script, [keyspace, backup_id])
    rescue => e
      Braavos.logger.error("clear snapshot #{keyspace} failed, ignoring")
    end
  end
end

#find_snapshotsObject



156
157
158
159
# File 'lib/braavos/service/cassandra.rb', line 156

def find_snapshots
  results = `find #{Braavos.config.data_loc} -type d -wholename '*/snapshots/*'`.split("\n").select {|s| not s =~ /\/_.*$/ }

end

#keyspacesObject



11
12
13
14
15
16
17
18
19
# File 'lib/braavos/service/cassandra.rb', line 11

def keyspaces
  @keyspaces ||= -> do
    result = Dir[File.join(Braavos.config.data_loc, '*')].map do |d|
      d.sub(/\A#{Braavos.config.data_loc}\//, '')
    end
    Braavos.logger.debug("Found cassandra keyspaces: #{result}")
    result
  end.call
end

#keyspaces_dataObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/braavos/service/cassandra.rb', line 21

def keyspaces_data
  @keyspaces_data ||= -> do
    if regex = Braavos.config.get_regex('cassandra_keyspaces_data')
      Braavos.logger.debug("Cassandra Data Keyspaces: regex = #{regex}")
      keyspaces.select do |k| regex =~ k end
    else
      keyspaces - DEFAULT_KEYSPACES_SYSTEM
    end
  end.call
end

#keyspaces_systemObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/braavos/service/cassandra.rb', line 32

def keyspaces_system
  @keyspaces_system ||= -> do
    if regex = Braavos.config.get_regex('cassandra_keyspaces_system')
      Braavos.logger.debug("Cassandra System Keyspaces: regex = #{regex}")
      keyspaces.select do |k| regex =~ k end
    else
      DEFAULT_KEYSPACES_SYSTEM & keyspaces
    end
  end.call
end

#list_tables(location) ⇒ Object

return [[disk_loc, ‘table_name’]]



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/braavos/service/cassandra.rb', line 138

def list_tables(location)
  # Example for table_name:
  # /usr/local/var/lib/cassandra/data/place_directory_development/Places/snapshots/TODAY/place_directory_development-Places-ib-1-CompressionInfo.db
  # returned as "place_directory_development/Places/place_directory_development-Places-ib-1"
  tables = Set.new
  Dir[File.join(Braavos.config.data_loc, '**', location, '*')].each do |file|
    next if file =~ /.json\Z/ # Some versions of cassandra use Table and Table.index json files in the sstable storage location
    file.sub!(/-[\w.]+\Z/, '')
    if match = file.match(/\A#{Braavos.config.data_loc}\/([\w\/]+)\/#{location}\/([-\w\.]+)\Z/)
      ks_table, ssfile = match.captures
      tables << [file, File.join(ks_table, ssfile)]
    else
      Braavos.logger.warn("Found unexpected file in snapshot: #{file}")
    end
  end
  tables.to_a
end

#restore(backup_loc, restore_loc) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/braavos/service/cassandra.rb', line 116

def restore(backup_loc, restore_loc)
  backup_path = File.join(Braavos.config.backup_path, backup_loc, Braavos.storage.find_node_id)
  data_path = File.join(Braavos.config.backup_path, 'data', Braavos.storage.find_node_id)

  Braavos.storage.load_file(File.join(backup_path, '_COMPLETED'))

  contents = JSON.parse(Braavos.storage.load_file(File.join(backup_path, 'contents.json')))

  script = Braavos.template.load_template('cassandra/table_bundle_restore.sh.erb')
  script_input = contents['tables'].map do |t|
    [Braavos.storage.script_path(File.join(data_path, t)), File.join(restore_loc, File.dirname(t))]
  end
  Braavos.parallel.execute(script, script_input) if script_input.size > 0

  Braavos.logger.info("Restore completed: #{backup_loc} to #{restore_loc}")
end

#restore_incrObject



133
134
135
# File 'lib/braavos/service/cassandra.rb', line 133

def restore_incr

end

#tokenObject



7
8
9
# File 'lib/braavos/service/cassandra.rb', line 7

def token
  @token ||= safely_retrieve_token
end