aboutsummaryrefslogtreecommitdiffstats
path: root/src/py/imc/blobserver.py
blob: 73bc143a817df3e12d2e18f448cffbfc630a95b0 (plain) (blame)
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
#! /usr/bin/env python

import os

from imc.auth import Auth
import imc.async
from imc.proxy import Proxy

from collections import Counter

class BlobServer:
    def __init__(self, proxy, auth, idendesc, link,
            location, blobtable, BlobHandle):

        self._proxy = proxy
        self._auth = auth
        self._idendesc = idendesc
        self._link = link
        self._location = location
        self._blobtable = blobtable
        self.BlobHandle = BlobHandle
        self._clients = {}
        self._containers = dict.fromkeys(self._blobtable.get_container_list(),
                                         dict())
        self._proxy.register_call('blobserver/', 'connect_client',
                                  self.connect_client)
        self._proxy.register_call('blobserver/', 'open_container',
                                  self.open_container)
        self._proxy.register_call('blobserver/', 'close_container',
                                  self.close_container)
        self._proxy.register_call('blobserver/', 'check_blob',
                                  self.check_blob)
        self._proxy.register_call('blobserver/', 'recv_update_result',
                                  self.recv_update_result)
        self._proxy.register_call('blobserver/', 'recv_commit',
                                  self.recv_commit)

    def __del__(self):
        self._proxy.unregister_call('blobserver/', 'connect_client')
        self._proxy.unregister_call('blobserver/', 'open_container')
        self._proxy.unregister_call('blobserver/', 'close_container')
        self._proxy.unregister_call('blobserver/', 'check_blob')
        self._proxy.unregister_call('blobserver/', 'recv_update_result')
        self._proxy.unregister_call('blobserver/', 'recv_commit')
        
    def _client_call(self, client, func, timeout=10000, *args):
        client += 'blobclient/'
        with Auth.change_current_iden(self._idendesc):
            for i in range(5):
                sta, ret = self._proxy.call(client, func, timeout, *args)
                if sta or (not sta and ret == 'Enoexist'):
                    break
        return (sta, ret)

    def _client_call_async(self, client, func, callback, 
                           timeout=10000, *args, **kwargs):
        client += 'blobclient/'
        with Auth.change_current_iden(self._idendesc):
            for i in range(5):
                sta, ret = self._proxy.call_async(client, func, timeout,
                                                  callback, *args)
                if sta or (not sta and ret == 'Enoexist'):
                    break
        return (sta, ret)

    @imc.async.caller
    def connect_client(self, client, cache_list):
        if client not in self._clients:
            self._clients.update({client: cache_list})
        else:
            self._clients[client] = cache_list

    def disconnect_client(self, client):
        try:
            self._clients.pop[client]
        except ValueError:
            raise Exception("this client doesn't exist")

    def create_container(self, container):
        self._blobtable.create_container(container)
        self._containers[container] = dict()

    def del_container(self, container):
        try:
            self._blobtable.del_container(container)
            del self._containers[container]
        except:
            raise

    @imc.async.caller
    def open_container(self, client, container, method):
        try:
            self._containers[container][client] = method
        except KeyError:
            return False
        else:
            return True

    @imc.async.caller
    def close_container(self, client, container):
        try:
            self._containers[container].pop(client)
        except KeyError:
            raise

    def update_blob(self, blobname, info):
        self._blobtable.update_blob(blobname, info)

    def del_blob(self, blobname):
        rev = self._blobtable.get_blob_info(blobname, 'rev')
        blobname_rev = ''.join([blobname, '_', str(rev)])
        self._blobtable.del_blob(blobname)
        self.del_real_blob(blobname_rev)

    def del_real_blob(self, blobname_rev):
        blobpath = self._location + blobname_rev
        self.BlobHandle.del_blob(blobpath)

    def send_blob(self, client, blobname):
        rev = str(self._blobtable.get_blob_info(blobname, 'rev'))
        blobpath = os.path.join(self._location, blobname + '_' + rev)
        return self._proxy.sendfile(client, blobpath)

    def recv_blob(self, filekey, blobname, rev):
        blobpath = os.path.join(self._location, blobname +  '_' + str(rev))
        ret =  self._proxy.recvfile(filekey, blobpath)

        return ret

    @imc.async.caller
    def check_blob(self, client, blobname, cacherev):
        rev = self._blobtable.get_blob_info(blobname, 'rev')
        if rev is None:
            return False
        elif cacherev < rev:
            result = self.send_blob(client, blobname)
            response = {'filekey': result.filekey,
                        'info': self._blobtable.get_blob_info(blobname)}
            return response
        else:
            return None

    @imc.async.caller
    def recv_update_result(self, client, blobname, result,
                           cacherev, retry=False):
        if client not in self._clients:
            return None
        else:
            if result == 'Success':
                self._clients[client].append({blobname: cacherev})
                return 'Success'
            elif retry:
                result = self.send_blob(client, blobname)
                response = {'filekey': result.filekey,
                            'info': self._blobtable.get_blob_info(blobname)}
                return response
            else:
                return 'Finish'

    def send_update(self, clients, blobname, info, written):
        result_table = dict.fromkeys(clients)
        def recv_result(result):
            nonlocal result_table
            nonlocal blobname
            nonlocal info
            sta, client = result
            # TODO:
            # limit retry
            if not sta:
                self._client_call_async(client, 'get_update',
                                        recv_result,
                                        blobname, info, 
                                        result_table[client].filekey)
            else:
                if result_table[client] is None:
                    result_table.pop(client)
                elif result_table[client].wait() != 'Success':
                    result_table[client] = self.send_blob(client, blobname)
                    self._client_call_async(client, 'get_update',
                                            recv_result,
                                            blobname, info, 
                                            result_table[client].filekey)
                else:
                    result_table.pop(client)

        for client in clients:
            if written:
                result_table[client] = self.send_blob(client, blobname)
            else:
                result_table[client] = None
            sta, ret = self._client_call(client, 'get_update',
                                         recv_result,
                                         blobname, info, 
                                         result_table[client].filekey)
            if not sta:
                # TODO:
                pass

    @imc.async.caller
    def recv_commit(self, client, commit_info, force_flag, filekey=None):
        blobname = commit_info['blobname']
        info = commit_info['info']
        rev = self._blobtable.get_blob_info(blobname, 'rev')
        if rev is None:
            if commit_info['createtag']:
                rev = 0
            else:
                return False
        elif info['rev'] < rev and not force_flag:
            return False

        if commit_info['deltag']:
            self.del_blob(blobname)
            clients = set()
            for needed_client, method in (
                self._containers[info['container']].items()
            ):
                if method == "ACTIVE":
                    clients.add(needed_client)
            clients.discard(client)
            self.send_update(clients, blobname, None, False)
            result = True
        else:
            info['rev'] = rev + 1
            if commit_info['written']:
                status = self.recv_blob(filekey, blobname, rev + 1)
                result = status.wait()
                if rev:
                    self.del_real_blob(''.join([blobname, '_', str(rev)]))
            else:
                result = True
        if result:
            self.update_blob(blobname, info)
            clients = set()
            for needed_client, method in (
                self._containers[info['container']].items()):
                if method == "ACTIVE":
                    clients.add(needed_client)
            clients.discard(client)
            self.send_update(clients, blobname, 
                             info, commit_info['written'])

            return True
        else:
            return False



################### Testing Code #######################
'''
if __name__ == '__main__':
    global blob_serv

    blob_serv = BlobServer()
    blob_serv.listen(5730)

    #http_serv = tornado.httpserver.HTTPServer(tornado.web.Application([
    #    ('/conn',WebConnHandler),
    #]))
    #http_serv.listen(83)

    tornado.ioloop.IOLoop.instance().start()
'''