Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar_network/node/sync_master.py
blob: bb2f70e422de58e2a08a972f1d25f07ffa8e4b41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
115
116
117
118
119
120
121
122
123
124
125
126
127
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
190
191
192
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright (C) 2012 Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import json
import base64
import hashlib
import logging
from Cookie import SimpleCookie
from os.path import exists, join

from pylru import lrucache

import active_document as ad
from sugar_network import node, toolkit
from sugar_network.toolkit.sneakernet import InPacket, OutBufferPacket, \
        OutPacket, DiskFull
from sugar_network.toolkit.collection import Sequence
from sugar_network.toolkit.files_sync import Seeders
from sugar_network.node import stats
from active_toolkit import coroutine, util, enforce


_PULL_QUEUE_SIZE = 256

_logger = logging.getLogger('node.sync_master')


class SyncCommands(object):

    _guid = None
    volume = None

    def __init__(self):
        self._file_syncs = Seeders(node.sync_dirs.value,
                join(node.data_root.value, 'sync'), self.volume.seqno)
        self._pull_queue = lrucache(_PULL_QUEUE_SIZE,
                lambda key, pull: pull.unlink())

    @ad.volume_command(method='POST', cmd='push')
    def push(self, request, response):
        with InPacket(stream=request) as in_packet:
            enforce('src' in in_packet.header and
                    in_packet.header['src'] != self._guid,
                    'Misaddressed packet')
            enforce('dst' in in_packet.header and
                    in_packet.header['dst'] == self._guid,
                    'Misaddressed packet')

            out_packet = OutBufferPacket(src=self._guid,
                    dst=in_packet.header['src'],
                    filename='ack.' + in_packet.header.get('filename'))
            pushed = Sequence()
            merged = Sequence()
            cookie = _Cookie()
            stats_pushed = {}

            for record in in_packet.records(dst=self._guid):
                cmd = record.get('cmd')
                if cmd == 'sn_push':
                    seqno = self.volume.merge(record)
                    merged.include(seqno, seqno)
                elif cmd == 'sn_commit':
                    _logger.debug('Merged %r commit', record)
                    pushed.include(record['sequence'])
                elif cmd == 'sn_pull':
                    cookie['sn_pull'].include(record['sequence'])
                elif cmd == 'files_pull':
                    cookie[record['directory']].include(record['sequence'])
                elif cmd == 'stats_push':
                    db = record['db']
                    user = record['user']

                    rrd = stats.get_rrd(user)
                    rrd.put(db, record['values'], record['timestamp'])

                    user_seq = stats_pushed.setdefault(user, {})
                    db_seq = user_seq.setdefault(db, Sequence())
                    db_seq.include(record['sequence'])

            enforce(not merged or pushed,
                    '"sn_push" record without "sn_commit"')
            if pushed:
                out_packet.push(cmd='sn_ack', sequence=pushed, merged=merged)
            if stats_pushed:
                out_packet.push(cmd='stats_ack', sequence=stats_pushed)

            cookie['sn_pull'].exclude(merged)
            # Read passed cookie only after excluding `merged`.
            # If there is sn_pull out of currently pushed packet, excluding
            # `merged` should not affect it.
            cookie.include(_Cookie(request))
            cookie.store(response)

            response.content_type = out_packet.content_type
            if not out_packet.empty:
                return out_packet.pop()

    @ad.volume_command(method='GET', cmd='pull',
            mime_type='application/octet-stream',
            arguments={'accept_length': ad.to_int})
    def pull(self, request, response, accept_length=None, **pulls):
        cookie = _Cookie(request)
        for key, seq in pulls.items():
            cookie[key][:] = json.loads(seq)
        if not cookie:
            _logger.debug('Clone full dump')
            cookie['sn_pull'].include(1, None)

        pull_key = hashlib.sha1(json.dumps(cookie)).hexdigest()
        pull = None
        content = None

        if pull_key in self._pull_queue:
            pull = self._pull_queue[pull_key]
            if accept_length is not None and pull.length > accept_length:
                _logger.debug('Cached %r pull is bigger than requested '
                        'length, will recreate it', cookie)
                pull.unlink()
                del self._pull_queue[pull_key]
                pull = None

        if pull is None:
            pull = self._pull_queue[pull_key] = _Pull(pull_key, cookie,
                    self._pull, src=self._guid, seqno=self.volume.seqno.value,
                    limit=accept_length)

        if pull.exception is not None:
            del self._pull_queue[pull_key]
            raise pull.exception

        if pull.ready:
            _logger.debug('Response with ready %r pull', cookie)
            content = pull.content
            response.content_type = pull.content_type
            cookie = pull.cookie
        else:
            _logger.debug('Pull %r is not yet ready', cookie)
            cookie.delay = pull.seconds_remained

        cookie.store(response)
        return content

    def _pull(self, cookie, packet):
        sn_pull = cookie['sn_pull']
        if sn_pull:
            self.volume.diff(sn_pull, packet)

        for directory, seq in cookie.items():
            sync = self._file_syncs.get(directory)
            if sync is None or not sync.pending(seq):
                continue
            sync.pull(seq, packet)


class _Pull(object):

    def __init__(self, pull_key, cookie, pull_cb, **packet_args):
        self.cookie = cookie
        self.exception = None
        self.seconds_remained = 0
        self.content_type = None
        self._path = join(toolkit.tmpdir.value, pull_key + '.pull')
        self._job = None

        if exists(self._path):
            try:
                with InPacket(self._path) as packet:
                    self.content_type = packet.content_type
                    self.cookie = _Cookie()
                    self.cookie.update(packet.header['cookie'])
            except Exception:
                util.exception('Cannot open cached packet for %r, recreate',
                        self._path)
                os.unlink(self._path)

        if not exists(self._path):
            packet = OutPacket(stream=file(self._path, 'wb+'), **packet_args)
            self.content_type = packet.content_type
            # TODO Might be useful to set meaningful value here
            self.seconds_remained = node.pull_timeout.value
            self._job = coroutine.spawn(self._pull, packet, pull_cb)

    @property
    def ready(self):
        # pylint: disable-msg=E1101
        return self._job is None or self._job.dead

    @property
    def content(self):
        if exists(self._path):
            return file(self._path, 'rb')

    @property
    def length(self):
        if exists(self._path):
            return os.stat(self._path).st_size

    def unlink(self):
        if self._job is not None:
            self._job.kill()
        if exists(self._path):
            _logger.debug('Eject %r pull from queue', self._path)
            os.unlink(self._path)

    def _pull(self, packet, cb):
        try:
            cb(self.cookie, packet)
        except DiskFull:
            pass
        except Exception, exception:
            util.exception('Error while making %r pull', self.cookie)
            self.exception = exception
            self.unlink()
        else:
            self.cookie.clear()
        packet.header['cookie'] = self.cookie
        packet.close()


class _Cookie(dict):

    def __init__(self, request=None):
        dict.__init__(self)

        if request is not None:
            value = self._get_cookie(request, 'sugar_network_sync')
            for key, seq in (value or {}).items():
                self[key] = Sequence(seq)

        self.delay = 0

    def include(self, cookie):
        for key, seq in cookie.items():
            self[key].include(seq)

    def store(self, response):
        to_store = {}
        for key, value in self.items():
            if value:
                to_store[key] = value

        if to_store:
            _logger.debug('Postpone %r pull in cookie', to_store)
            to_store = base64.b64encode(json.dumps(to_store))
            self._set_cookie(response, 'sugar_network_sync', to_store)
            self._set_cookie(response, 'sugar_network_delay', self.delay)
        else:
            self._unset_cookie(response, 'sugar_network_sync')
            self._unset_cookie(response, 'sugar_network_delay')

    def __getitem__(self, key):
        seq = self.get(key)
        if seq is None:
            seq = self[key] = Sequence()
        return seq

    def _get_cookie(self, request, name):
        cookie_str = request.environ.get('HTTP_COOKIE')
        if not cookie_str:
            return
        cookie = SimpleCookie()
        cookie.load(cookie_str)
        if name not in cookie:
            return
        value = cookie.get(name).value
        if value != 'unset_%s' % name:
            return json.loads(base64.b64decode(value))

    def _set_cookie(self, response, name, value, age=3600):
        response.setdefault('Set-Cookie', [])
        cookie = '%s=%s; Max-Age=%s; HttpOnly' % (name, value, age)
        response['Set-Cookie'].append(cookie)

    def _unset_cookie(self, response, name):
        self._set_cookie(response, name, 'unset_%s' % name, 0)