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
|
#! /usr/bin/env python
import os
from imc.auth import Auth
import imc.async
from imc.proxy import Proxy
from collections import Counter
class BlobClient:
def __init__(self, proxy, auth, idendesc, link, serverlink,
location, cachetable, BlobHandle):
self._proxy = proxy
self._auth = auth
self._idendesc = idendesc
self._link = link
self._server = serverlink
self._is_connected = False
self._location = location
self._cachetable = cachetable
self.BlobHandle = BlobHandle
self._opencounts = Counter()
self._containers = dict()
self._deltags = set()
self.connect_server()
self._proxy.register_call('blobclient/', 'get_update',
self.get_update)
def __del__(self):
self._proxy.unregister_call('blobclient/', 'get_update')
def _server_call(self, func, *args):
server = self._server + 'blobserver/'
with Auth.change_current_iden(self._idendesc):
for i in range(5):
sta, ret = self._proxy.call(server, func, 10000,
self._link, *args)
if sta or (not sta and ret == 'Enoexist'):
break
return (sta, ret)
def _client_call(self, otherclient, func, *args):
otherclient += 'blobclient/'
with TOJAuth.change_current_iden(self._idendesc):
for i in range(5):
sta, ret = self._proxy.call(otherclient, func, 10000,
self._link, *args)
if sta or (not sta and ret == 'Enoexist'):
break
return (sta, ret)
def connect_server(self, serverlink=None):
if serverlink:
self._server = serverlink
sta, ret = self._server_call('connect_client',
self._cachetable.get_blob_list())
if sta:
if ret:
self._is_connected = True
else:
pass
else:
pass
if self._is_connected:
# TODO:
pass
def open_container(self, container, method):
sta, ret = self._server_call('open_container', container, method)
if not sta:
# TODO:
# pend operation when client can't imc call server
return None
if ret:
self._containers[container] = method
return True
else:
return False
def close_container(self, container):
sta, ret = self._server_call('close_container', container)
if not sta:
# TODO:
# pend operation when client can't imc call server
return None
if ret:
del self._containers[container]
return True
else:
return False
# TODO:
# periodically call this function to clean old data and do something else
# ex: send pending operation
def sync(self):
for blobname_rev in self._deltags:
self.del_real_blob(blobname_rev)
# for container in self._containers:
# if self._containers[container] == 'ALWAYS':
# for blob in self._cachetable.get_blob_list(container):
# self.update(blob)
@imc.async.caller
def get_update(self, blobname, info, filekey=None):
if info is None:
self.del_blob(blobname)
elif filekey is not None:
rev = info['rev']
if self.recv_blob(filekey, blobname, rev).wait() == 'Success':
self.update_blob(blobname, info)
sta, ret = self._server_call('recv_update_result',
blobname, "Success", rev)
if not sta:
# TODO:
pass
else:
self.update_blob(blobname, info)
return self._link
def update(self, blobname):
cacherev = self._cachetable.get_blob_info(blobname, 'rev')
if cacherev == None:
cacherev = 0
sta, ret = self._server_call('check_blob', blobname, cacherev)
if not sta:
# TODO:
# pend operation when client can't imc call server
return None
if ret is None:
return True
elif not ret:
self._cachetable.del_blob(blobname)
if cacherev:
self.del_real_blob(''.join([blobname, '_', str(cacherev)]))
return None
else:
info = ret['info']
rev = info['rev']
for i in range(4):
rst = self.recv_blob(ret['filekey'], blobname, rev).wait()
sta, ret = self._server_call('recv_update_result', blobname,
rst, rev, True)
if not sta:
pass
# TODO:
if 'Success' == ret:
self.update_blob(blobname, info)
return True
return False
def commit(self, commit_info, force_flag, blobhandle):
filekey = None
if not commit_info['deltag'] and commit_info['written']:
result = self.send_blob(blobhandle._tmpfile)
filekey = result.filekey
sta, ret = self._server_call('recv_commit', commit_info,
force_flag, filekey)
if not sta:
# TODO:
# pend operation when client can't imc call server
return False
else:
# TODO:
# if commit success , copy tmpfile to location
if ret:
blobhandle.copy_tmp()
# TODO:
# opencounts ?
def send_blob(self, blobpath, otherclient=None):
if otherclient is None:
return self._proxy.sendfile(self._server, blobpath)
else:
return self._proxy.sendfile(otherclient, blobpath)
def recv_blob(self, filekey, blobname, rev):
blobpath = os.path.join(self._location, blobname + '_' + str(rev))
return self._proxy.recvfile(filekey, blobpath)
def update_blob(self, blobname, info):
rev = self._cachetable.get_blob_info(blobname, 'rev')
blobname_rev = ''.join([blobname, '_', str(rev)])
self.del_real_blob(blobname_rev)
self._cachetable.update_blob(blobname, info)
def del_blob(self, blobname):
rev = self._cachetable.get_blob_info(blobname, 'rev')
blobname_rev = ''.join([blobname, '_', str(rev)])
self._cachetable.del_blob(blobname)
self.del_real_blob(blobname_rev)
def del_real_blob(self, blobname_rev):
if self._opencounts[blobname_rev] == 0:
path = os.path.join(self._location, blobname_rev)
self.BlobHandle.del_blob(path)
else:
self._deltags.add(blobname_rev)
def open(self, container, blobname, flag):
if container not in self._containers:
raise Exception("this container isn't open")
blob = container + '_' + blobname
is_existent = self.update(blob)
if is_existent is None:
if (not flag & self.BlobHandle.WRITE or
not flag & self.BlobHandle.CREATE):
raise Exception("the blob doesn't exist, so you must "
"add a create flag")
elif not is_existent:
pass
try:
info = self._cachetable.get_blob_info(blob)
if info is None:
info = {'container': container,
'rev': 0,
'metadata': '',
'size': None,
'commit_time': None}
handle = self.BlobHandle(blob, info, flag, self)
except:
raise
else:
blob += '_' + str(handle.get_rev())
self._opencounts[blob] += 1
return handle
def close(self, blobhandle):
blob = ''.join([blobhandle._name, '_',
str(blobhandle.get_rev())])
self._opencounts[blob] -= 1
|