]> git.openstreetmap.org Git - chef.git/blob - cookbooks/planet/files/default/replication-bin/replicate-changesets
b229c34ec841ab8d3a3029bfcffba81712eada46
[chef.git] / cookbooks / planet / files / default / replication-bin / replicate-changesets
1 #!/usr/bin/ruby
2
3 require "rubygems"
4 require "pg"
5 require "yaml"
6 require "time"
7 require "fileutils"
8 require "xml/libxml"
9 require "zlib"
10 require "set"
11
12 # after this many changes, a changeset will be closed
13 CHANGES_LIMIT = 50000
14
15 # this is the scale factor for lat/lon values stored as integers in the database
16 GEO_SCALE = 10000000
17
18 ##
19 # changeset class keeps some information about changesets downloaded from the
20 # database - enough to let us know which changesets are closed/open & recently
21 # closed.
22 class Changeset
23   attr_reader :id, :created_at, :closed_at, :num_changes
24
25   def initialize(row)
26     @id = row["id"].to_i
27     @created_at = Time.parse(row["created_at"])
28     @closed_at = Time.parse(row["closed_at"])
29     @num_changes = row["num_changes"].to_i
30   end
31
32   def closed?(t)
33     (@closed_at < t) || (@num_changes >= CHANGES_LIMIT)
34   end
35
36   def open?(t)
37     !closed?(t)
38   end
39
40   def activity_between?(t1, t2)
41     ((@closed_at >= t1) && (@closed_at < t2)) || ((@created_at >= t1) && (@created_at < t2))
42   end
43 end
44
45 ##
46 # state and connections associated with getting changeset data
47 # replicated to a file.
48 class Replicator
49   def initialize(config)
50     @config = YAML.load(File.read(config))
51     @state = YAML.load(File.read(@config["state_file"]))
52     @conn = PGconn.connect(@config["db"])
53     @now = Time.now.getutc
54   end
55
56   def open_changesets
57     last_run = @state["last_run"]
58     last_run = (@now - 60) if last_run.nil?
59     @state["last_run"] = @now
60     # pretty much all operations on a changeset will modify its closed_at
61     # time (see rails_port's changeset model). so it is probably enough
62     # for us to look at anything that was closed recently, and filter from
63     # there.
64     changesets = @conn
65       .exec("select id, created_at, closed_at, num_changes from changesets where closed_at > ((now() at time zone 'utc') - '1 hour'::interval)")
66       .map { |row| Changeset.new(row) }
67       .select { |cs| cs.activity_between?(last_run, @now) }
68
69     # set for faster presence lookups by ID
70     cs_ids = Set.new(changesets.map { |c| c.id })
71
72     # but also add any changesets which have new comments
73     new_ids = @conn
74       .exec("select distinct changeset_id from changeset_comments where created_at >= '#{last_run}' and created_at < '#{@now}' and visible")
75       .map { |row| row["changeset_id"].to_i }
76       .select { |c_id| not cs_ids.include?(c_id) }
77
78     new_ids.each do |id|
79       @conn
80         .exec("select id, created_at, closed_at, num_changes from changesets where id=#{id}")
81         .map { |row| Changeset.new(row) }
82         .each { |cs| changesets << cs }
83     end
84
85     changesets.sort_by { |cs| cs.id }
86   end
87
88   # creates an XML file containing the changeset information from the
89   # list of changesets output by open_changesets.
90   def changeset_dump(changesets)
91     doc = XML::Document.new
92     doc.root = XML::Node.new("osm")
93     { "version" => "0.6",
94       "generator" => "replicate_changesets.rb",
95       "copyright" => "OpenStreetMap and contributors",
96       "attribution" => "http://www.openstreetmap.org/copyright",
97       "license" => "http://opendatacommons.org/licenses/odbl/1-0/" }
98       .each { |k, v| doc.root[k] = v }
99
100     changesets.each do |cs|
101       xml = XML::Node.new("changeset")
102       xml["id"] = cs.id.to_s
103       xml["created_at"] = cs.created_at.getutc.xmlschema
104       xml["closed_at"] = cs.closed_at.getutc.xmlschema if cs.closed?(@now)
105       xml["open"] = cs.open?(@now).to_s
106       xml["num_changes"] = cs.num_changes.to_s
107
108       res = @conn.exec("select u.id, u.display_name, c.min_lat, c.max_lat, c.min_lon, c.max_lon from users u join changesets c on u.id=c.user_id where c.id=#{cs.id}")
109       xml["user"] = res[0]["display_name"]
110       xml["uid"] = res[0]["id"]
111
112       unless res[0]["min_lat"].nil? ||
113              res[0]["max_lat"].nil? ||
114              res[0]["min_lon"].nil? ||
115              res[0]["max_lon"].nil?
116         xml["min_lat"] = (res[0]["min_lat"].to_f / GEO_SCALE).to_s
117         xml["max_lat"] = (res[0]["max_lat"].to_f / GEO_SCALE).to_s
118         xml["min_lon"] = (res[0]["min_lon"].to_f / GEO_SCALE).to_s
119         xml["max_lon"] = (res[0]["max_lon"].to_f / GEO_SCALE).to_s
120       end
121
122       res = @conn.exec("select k, v from changeset_tags where changeset_id=#{cs.id}")
123       res.each do |row|
124         tag = XML::Node.new("tag")
125         tag["k"] = row["k"]
126         tag["v"] = row["v"]
127         xml << tag
128       end
129
130       # grab the visible changeset comments as well
131       res = @conn.exec("select cc.author_id, u.display_name as author, cc.body, cc.created_at from changeset_comments cc join users u on cc.author_id=u.id where cc.changeset_id=#{cs.id} and cc.visible order by cc.created_at asc")
132       xml["comments_count"] = res.num_tuples.to_s
133       if res.num_tuples > 0
134         discussion = XML::Node.new("discussion")
135         res.each do |row|
136           comment = XML::Node.new("comment")
137           comment["uid"] = row["author_id"]
138           comment["user"] = row["author"]
139           comment["date"] = Time.parse(row["created_at"]).getutc.xmlschema
140           text = XML::Node.new("text")
141           text.content = row["body"]
142           comment << text
143           discussion << comment
144         end
145         xml << discussion
146       end
147
148       doc.root << xml
149     end
150
151     doc.to_s
152   end
153
154   # saves new state (including the changeset dump xml)
155   def save!
156     File.open(@config["state_file"], "r") do |fl|
157       fl.flock(File::LOCK_EX)
158
159       sequence = (@state.key?("sequence") ? @state["sequence"] + 1 : 0)
160       data_file = @config["data_dir"] + format("/%03d/%03d/%03d.osm.gz", sequence / 1000000, (sequence / 1000) % 1000, (sequence % 1000))
161       tmp_state = @config["state_file"] + ".tmp"
162       tmp_data = "/tmp/changeset_data.osm.tmp"
163       # try and write the files to tmp locations and then
164       # move them into place later, to avoid in-progress
165       # clashes, or people seeing incomplete files.
166       begin
167         FileUtils.mkdir_p(File.dirname(data_file))
168         Zlib::GzipWriter.open(tmp_data) do |fh|
169           fh.write(changeset_dump(open_changesets))
170         end
171         @state["sequence"] = sequence
172         File.open(tmp_state, "w") do |fh|
173           fh.write(YAML.dump(@state))
174         end
175         FileUtils.mv(tmp_data, data_file)
176         FileUtils.mv(tmp_state, @config["state_file"])
177         fl.flock(File::LOCK_UN)
178
179       rescue
180         STDERR.puts("Error! Couldn't update state.")
181         fl.flock(File::LOCK_UN)
182         raise
183       end
184     end
185   end
186 end
187
188 rep = Replicator.new(ARGV[0])
189 rep.save!