-
Notifications
You must be signed in to change notification settings - Fork 0
/
eafsmetadata.py
383 lines (318 loc) · 15 KB
/
eafsmetadata.py
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# -*- coding: utf-8 -*-
'''
* EAFS
* Copyright (C) 2009-2011 Adam Etienne <[email protected]>
*
* 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 version 3.
*
* 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 math,uuid,os,time,operator,argparse,base64,sqlite3,threading,apsw
import MySQLdb
class EAFSMetaDataCursor:
def __init__(self, parent):
self.parent = parent
self.db = self.parent.db
self.cursor = self.begin()
#print "[BEGIN] Cursor: ", self.cursor
class EAFSMetaData:
def __init__(self, db_path):
self.db_path = db_path
self.db = None
class EAFSMetaDataSQLiteCursor(EAFSMetaDataCursor):
def begin(self):
c = self.db.cursor()
c.execute("""PRAGMA synchronous = OFF""")
c.execute("begin")
return c
def commit(self):
#print "[COMMIT] Cursor: ", self.cursor
self.cursor.execute("commit")
self.cursor.close()
class EAFSMetaDataSQLite(EAFSMetaData):
def connect(self):
self.db = apsw.Connection(self.db_path)
self.db.setbusytimeout(500)
def get_cursor(self):
return EAFSMetaDataSQLiteCursor( self )
def init(self):
c = self.db.cursor()
try:
c.execute("begin")
c.execute('DROP TABLE IF EXISTS inode')
c.execute('DROP TABLE IF EXISTS chunk')
c.execute('DROP TABLE IF EXISTS inode_chunk')
c.execute('DROP TABLE IF EXISTS server')
c.execute('DROP TABLE IF EXISTS chunk_server')
#self.db.commit()
c.execute("commit")
except:
pass
c.execute("begin")
c.execute('CREATE TABLE inode (id INTEGER PRIMARY KEY AUTOINCREMENT, parent INTEGER, name text, type char(1), perms text, uid int, gid int, attrs text, ctime text, mtime text, atime text, links int, size int, UNIQUE(parent, name))')
c.execute('CREATE TABLE chunk (uuid text, alloc_time INTEGER, md5 TEXT, size INTEGER, PRIMARY KEY(uuid))')
c.execute('CREATE TABLE inode_chunk (inode_id INTEGER, chunk_uuid text, UNIQUE(inode_id,chunk_uuid))')
c.execute('CREATE TABLE server (uuid text, address text, available INTEGER, last_seen DATETIME, size_total INTEGER, size_available INTEGER, PRIMARY KEY(uuid))')
c.execute('CREATE TABLE chunk_server (chunk_uuid text, server_uuid text, UNIQUE(chunk_uuid,server_uuid))')
#self.db.commit()
c.execute("commit")
c.close()
def get_inodes(self):
c = self.db.cursor()
c.execute('select * from inode')
rows = []
for row in c:
rows.append( row )
return rows
def get_inode_chunks(self):
c = self.db.cursor()
c.execute('select ic.* from inode_chunk ic left join chunk c on (c.uuid=ic.chunk_uuid) order by alloc_time')
rows = []
for row in c:
rows.append( row )
return rows
def get_chunks_and_servers(self):
c = self.db.cursor()
c.execute('select chunk_server.chunk_uuid, chunk_server.server_uuid, chunk.md5 from chunk_server left join chunk on (chunk.uuid=chunk_server.chunk_uuid)')
rows = []
for row in c:
rows.append( row )
return rows
def get_servers(self):
c = self.db.cursor()
c.execute('select * from server')
rows = []
for row in c:
rows.append( row )
return rows
def add_server(self, chunkserver_uuid, chunkserver_address):
c = self.db.cursor()
c.execute("begin")
c.execute("""insert into server (uuid, address) values (?,?)""", (chunkserver_uuid, chunkserver_address))
c.execute("commit")
c.close()
def add_chunk_in_server(self, chunk_uuid, chunkserver_uuid, cursor=None):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""insert into chunk_server values (?, ?)""", (chunk_uuid, chunkserver_uuid))
def add_chunk(self, chunk_uuid, chunk_timestamp, chunk_md5, cursor=None):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""insert into chunk values (?,?,?)""", (chunk_uuid, chunk_timestamp, chunk_md5))
def set_chunk(self, chunk_uuid, attr, val, cursor=None):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""update chunk set """+attr+"""=? where uuid=?)""", (val, chunk_uuid))
def search_inode_with_parent(self, parent_inode_id, filename):
c = self.db.cursor()
c.execute("""select * from inode where parent=? and name=?""", (parent_inode_id, filename) )
rows = []
for row in c:
rows.append( row )
return rows
def del_chunk( self, chunkuuid, cursor=None ):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""delete from chunk where uuid=?""", (chunkuuid, ))
def del_chunk_server( self, chunkuuid, cursor=None ):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""delete from chunk_server where chunk_uuid=?""", (chunkuuid, ))
def del_inode_chunk( self, inode_id, cursor=None ):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""delete from inode_chunk where inode_id=?""", (inode_id, ))
def del_inode( self, inode_id, cursor=None ):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""delete from inode where id=?""", (inode_id, ))
def add_inode( self, parent_inode_id, filename, inode, cursor=None ):
self_commit = False
if cursor is None:
cursor = self.get_cursor()
self_commit = True
cursor.cursor.execute("""insert into inode (parent,name,type,perms,uid,gid,attrs,ctime,mtime,atime,links,size) values (?,?,?,?,?,?,?,?,?,?,?,?)""", (parent_inode_id, filename, inode.type, "755", 0, 0, inode.attrs, inode.ctime, inode.mtime, inode.atime, 0, inode.size))
#print "Create: ", "insert into inode (parent,name,type,perms,uid,gid,attrs,ctime,mtime,atime,links) values (%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" % (parent_inode_id, create_filename, attributes["type"], "wrxwrxwrx", 0, 0, attributes["attrs"], attributes["ctime"], attributes["mtime"], attributes["atime"], 0)
if self_commit:
cursor.commit()
def add_inode_chunk( self, inode_id, chunkuuid, cursor=None ):
self_commit = False
if cursor is None:
cursor = self.get_cursor()
self_commit = True
cursor.cursor.execute("""insert into inode_chunk values (?,?)""", (inode_id, chunkuuid))
if self_commit:
cursor.commit()
def set_inode_size( self, inode_id, inode_size, cursor=None ):
self_commit = False
if cursor is None:
cursor = self.get_cursor()
self_commit = True
cursor.cursor.execute("""update inode set size=? where id=?""", (inode_size, inode_id))
if self_commit:
cursor.commit()
def set_server( self, chunkserver, cursor ):
cursor.cursor.execute("""update server set last_seen=?, available=?, size_total=?, size_available=? where uuid=?""", (chunkserver.last_seen, chunkserver.available, chunkserver.size_total, chunkserver.size_available, chunkserver.uuid))
def get_missing_replicate_chunks_and_servers( self ):
c = self.db.cursor()
c.execute('select chunk_uuid, alloc_time, count(*) as c from chunk_server left join chunk on (chunk.uuid=chunk_server.chunk_uuid) left join server on (server.uuid=chunk_server.server_uuid) where available=1 group by chunk_uuid, alloc_time having c<=1')
rows = []
for row in c:
rows.append( row )
return rows
class EAFSMetaDataMySQLCursor(EAFSMetaDataCursor):
def begin(self):
try:
c = self.parent.db.cursor()
except (AttributeError, MySQLdb.OperationalError):
self.parent.connect()
c = self.parent.db.cursor()
#c.execute("begin")
return c
def commit(self):
#print "[COMMIT] Cursor: ", self.cursor
self.parent.db.commit()
#self.db.close()
class EAFSMetaDataMySQL(EAFSMetaData):
def connect(self):
self.db = MySQLdb.connect( host = "localhost", user = "eafs", passwd = "iopiop", db = "eafs" )
def get_cursor(self):
return EAFSMetaDataMySQLCursor( self )
def get_read_cursor(self):
try:
c = self.db.cursor()
c.execute("""show tables""")
except (AttributeError, MySQLdb.OperationalError):
self.connect()
c = self.db.cursor()
return c
def init(self):
try:
cursor = self.get_cursor()
cursor.cursor.execute("begin")
cursor.cursor.execute('DROP TABLE IF EXISTS inode')
cursor.cursor.execute('DROP TABLE IF EXISTS chunk')
cursor.cursor.execute('DROP TABLE IF EXISTS inode_chunk')
cursor.cursor.execute('DROP TABLE IF EXISTS server')
cursor.cursor.execute('DROP TABLE IF EXISTS chunk_server')
cursor.commit()
except:
pass
cursor = self.get_cursor()
cursor.cursor.execute('CREATE TABLE inode (id INTEGER PRIMARY KEY AUTO_INCREMENT, parent INTEGER, name VARCHAR(900), type char(1), perms VARCHAR(16), uid int, gid int, attrs VARCHAR(64), ctime VARCHAR(64), mtime VARCHAR(64), atime VARCHAR(68), links int, size int, UNIQUE(parent, name))')
cursor.cursor.execute('CREATE TABLE chunk (uuid VARCHAR(128), alloc_time BIGINT, md5 VARCHAR(128), size INTEGER, PRIMARY KEY(uuid))')
cursor.cursor.execute('CREATE TABLE inode_chunk (inode_id INTEGER, chunk_uuid VARCHAR(128), UNIQUE(inode_id,chunk_uuid))')
cursor.cursor.execute('CREATE TABLE server (uuid VARCHAR(128), address VARCHAR(2048), available INTEGER, last_seen TIMESTAMP, size_total INTEGER, size_available INTEGER, PRIMARY KEY(uuid))')
cursor.cursor.execute('CREATE TABLE chunk_server (chunk_uuid VARCHAR(128), server_uuid VARCHAR(128), UNIQUE(chunk_uuid,server_uuid))')
cursor.commit()
def get_inodes(self):
c = self.get_read_cursor()
c.execute('select * from inode')
rows = c.fetchall()
return rows
def get_inode_chunks(self):
c = self.get_read_cursor()
c.execute('select ic.* from inode_chunk ic left join chunk c on (c.uuid=ic.chunk_uuid) order by alloc_time')
rows = c.fetchall()
return rows
def get_chunks_and_servers(self):
c = self.get_read_cursor()
c.execute('select chunk_server.chunk_uuid, chunk_server.server_uuid, chunk.md5 from chunk_server left join chunk on (chunk.uuid=chunk_server.chunk_uuid)')
rows = c.fetchall()
return rows
def get_servers(self):
c = self.get_read_cursor()
c.execute('select uuid, address, available, UNIX_TIMESTAMP(last_seen) AS last_seen, size_total, size_available from server')
rows = c.fetchall()
return rows
def add_server(self, chunkserver_uuid, chunkserver_address):
cursor = self.get_cursor()
cursor.cursor.execute("""insert into server (uuid, address) values (%s,%s)""", (chunkserver_uuid, chunkserver_address))
cursor.commit()
def add_chunk_in_server(self, chunk_uuid, chunkserver_uuid, cursor=None):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""insert into chunk_server values (%s, %s)""", (chunk_uuid, chunkserver_uuid))
def add_chunk(self, chunk_uuid, chunk_timestamp, chunk_md5, cursor=None):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("insert into chunk values ('%s',%d,'%s',0)" % (chunk_uuid, int(chunk_timestamp), chunk_md5))
def set_chunk(self, chunk_uuid, attr, val, cursor=None):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""update chunk set """+attr+"""=%s where uuid=%s""", (val, chunk_uuid))
def search_inode_with_parent(self, parent_inode_id, filename):
c = self.get_read_cursor()
c.execute("""select * from inode where parent=%s and name=%s""", (parent_inode_id, filename) )
rows = c.fetchall()
return rows
def get_inode( self, inode_id ):
c = self.get_read_cursor()
c.execute("""select * from inode where id=%d""", (inode_id) )
row = c.fetchone()
return row
def get_inode_size_from_chunk( self, chunk_uuid ):
c = self.get_read_cursor()
c.execute("""select i.id from inode i left join inode_chunk ic on (ic.inode_id=i.id) where ic.chunk_uuid=%s""", (chunk_uuid, ) )
row = c.fetchone()
inode_id = int(row[0])
c.execute("""select CASE WHEN SUM(c.size) IS NULL THEN 0 ELSE SUM(c.size) END AS size from inode i left join inode_chunk ic on (ic.inode_id=i.id) left join chunk c on (c.uuid=ic.chunk_uuid) where i.id=%s GROUP BY i.id""", (str(inode_id), ) )
row = c.fetchone()
return (inode_id, int(row[0]))
def del_chunk( self, chunkuuid, cursor=None ):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""delete from chunk where uuid=%s""", (chunkuuid, ))
def del_chunk_server( self, chunkuuid, cursor=None ):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""delete from chunk_server where chunk_uuid=%s""", (chunkuuid, ))
def del_inode_chunk( self, inode_id, cursor=None ):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""delete from inode_chunk where inode_id=%s""", (inode_id, ))
def del_inode( self, inode_id, cursor=None ):
if cursor is None:
cursor = self.get_cursor()
cursor.cursor.execute("""delete from inode where id=%s""", (inode_id, ))
def add_inode( self, parent_inode_id, filename, inode, cursor=None ):
self_commit = False
if cursor is None:
cursor = self.get_cursor()
self_commit = True
cursor.cursor.execute("""insert into inode (parent,name,type,perms,uid,gid,attrs,ctime,mtime,atime,links,size) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", (parent_inode_id, filename, inode.type, "755", 0, 0, inode.attrs, inode.ctime, inode.mtime, inode.atime, 0, inode.size))
#print "Create: ", "insert into inode (parent,name,type,perms,uid,gid,attrs,ctime,mtime,atime,links) values (%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" % (parent_inode_id, create_filename, attributes["type"], "wrxwrxwrx", 0, 0, attributes["attrs"], attributes["ctime"], attributes["mtime"], attributes["atime"], 0)
if self_commit:
cursor.commit()
def add_inode_chunk( self, inode_id, chunkuuid, cursor=None ):
self_commit = False
if cursor is None:
cursor = self.get_cursor()
self_commit = True
cursor.cursor.execute("insert into inode_chunk values (%d,'%s')" % (int(inode_id), chunkuuid))
if self_commit:
cursor.commit()
def set_inode_size( self, inode_id, inode_size, cursor=None ):
self_commit = False
if cursor is None:
cursor = self.get_cursor()
self_commit = True
cursor.cursor.execute("update inode set size=%d where id=%d" % (int(inode_size), int(inode_id)))
if self_commit:
cursor.commit()
def set_server( self, chunkserver, cursor ):
#print "update server set last_seen=FROM_UNIXTIME(%s), available=%s, size_total=%s, size_available=%s where uuid=%s" % (chunkserver.last_seen, chunkserver.available, chunkserver.size_total, chunkserver.size_available, chunkserver.uuid)
cursor.cursor.execute("""update server set last_seen=FROM_UNIXTIME(%s), address=%s, available=%s, size_total=%s, size_available=%s where uuid=%s""", (chunkserver.last_seen, chunkserver.address, chunkserver.available, chunkserver.size_total, chunkserver.size_available, chunkserver.uuid))
def get_missing_replicate_chunks_and_servers( self ):
c = self.get_read_cursor()
c.execute('select chunk_uuid, alloc_time, count(*) as c from chunk_server left join chunk on (chunk.uuid=chunk_server.chunk_uuid) left join server on (server.uuid=chunk_server.server_uuid) where available=1 group by chunk_uuid, alloc_time having c<=1')
rows = c.fetchall()
return rows