root / snf-pithos-app / pithos / api / management / commands / pithos-usage.py @ 9c0c8aa9
History | View | Annotate | Download (7.9 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from django.core.management.base import NoArgsCommand, CommandError |
35 |
|
36 |
from collections import namedtuple |
37 |
from optparse import make_option |
38 |
from sqlalchemy import func |
39 |
from sqlalchemy.sql import select, and_, or_ |
40 |
|
41 |
from synnefo.util.number import strbigdec |
42 |
|
43 |
from pithos.api.util import get_backend |
44 |
from pithos.backends.modular import ( |
45 |
CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED |
46 |
) |
47 |
clusters = (CLUSTER_NORMAL, CLUSTER_HISTORY, CLUSTER_DELETED) |
48 |
|
49 |
Statistics = namedtuple('Statistics', ('node', 'path', 'size', 'cluster')) |
50 |
|
51 |
class ResetHoldingPayload(namedtuple('ResetHoldingPayload', ( |
52 |
'entity', 'resource', 'key', 'imported', 'exported', 'returned', 'released' |
53 |
))): |
54 |
__slots__ = () |
55 |
|
56 |
def __str__(self): |
57 |
return '%s: %s' % (self.entity, self.imported) |
58 |
|
59 |
|
60 |
ENTITY_KEY = '1'
|
61 |
|
62 |
backend = get_backend() |
63 |
table = {} |
64 |
table['nodes'] = backend.node.nodes
|
65 |
table['versions'] = backend.node.versions
|
66 |
table['statistics'] = backend.node.statistics
|
67 |
table['policy'] = backend.node.policy
|
68 |
conn = backend.node.conn |
69 |
|
70 |
def _retrieve_user_nodes(users=()): |
71 |
s = select([table['nodes'].c.path, table['nodes'].c.node]) |
72 |
s = s.where(and_(table['nodes'].c.node != 0, |
73 |
table['nodes'].c.parent == 0)) |
74 |
if users:
|
75 |
s = s.where(table['nodes'].c.path.in_(users))
|
76 |
return conn.execute(s).fetchall()
|
77 |
|
78 |
def _compute_statistics(nodes): |
79 |
statistics = [] |
80 |
append = statistics.append |
81 |
for path, node in nodes: |
82 |
select_children = select( |
83 |
[table['nodes'].c.node]).where(table['nodes'].c.parent == node) |
84 |
select_descendants = select([table['nodes'].c.node]).where(
|
85 |
or_(table['nodes'].c.parent.in_(select_children),
|
86 |
table['nodes'].c.node.in_(select_children)))
|
87 |
s = select([table['versions'].c.cluster,
|
88 |
func.sum(table['versions'].c.size)])
|
89 |
s = s.group_by(table['versions'].c.cluster)
|
90 |
s = s.where(table['nodes'].c.node == table['versions'].c.node) |
91 |
s = s.where(table['nodes'].c.node.in_(select_descendants))
|
92 |
s = s.where(table['versions'].c.cluster == CLUSTER_NORMAL)
|
93 |
d2 = dict(conn.execute(s).fetchall())
|
94 |
|
95 |
try:
|
96 |
size = d2[CLUSTER_NORMAL] |
97 |
except KeyError: |
98 |
size = 0
|
99 |
append(Statistics( |
100 |
node=node, |
101 |
path=path, |
102 |
size=size, |
103 |
cluster=CLUSTER_NORMAL)) |
104 |
return statistics
|
105 |
|
106 |
def _verify_statistics(item): |
107 |
"""Verify statistics"""
|
108 |
s = select([table['statistics'].c.size])
|
109 |
s = s.where(table['statistics'].c.node == item.node)
|
110 |
s = s.where(table['statistics'].c.cluster == item.cluster)
|
111 |
db_item = conn.execute(s).fetchone() |
112 |
if not db_item: |
113 |
return
|
114 |
try:
|
115 |
assert item.size == db_item.size, \
|
116 |
'%d[%s][%d], size: %d != %d' % (
|
117 |
item.node, item.path, item.cluster, |
118 |
item.size, db_item.size) |
119 |
except AssertionError, e: |
120 |
print e
|
121 |
|
122 |
def _prepare_reset_holding(statistics, verify=False): |
123 |
"""Verify statistics and set quotaholder user usage"""
|
124 |
reset_holding = [] |
125 |
append = reset_holding.append |
126 |
for item in statistics: |
127 |
if verify:
|
128 |
_verify_statistics(item) |
129 |
if item.cluster == CLUSTER_NORMAL:
|
130 |
append(ResetHoldingPayload( |
131 |
entity=item.path, |
132 |
resource='pithos+.diskspace',
|
133 |
key=ENTITY_KEY, |
134 |
imported=item.size, |
135 |
exported=0,
|
136 |
returned=0,
|
137 |
released=0))
|
138 |
return reset_holding
|
139 |
|
140 |
|
141 |
class Command(NoArgsCommand): |
142 |
help = "List and reset pithos usage"
|
143 |
|
144 |
option_list = NoArgsCommand.option_list + ( |
145 |
make_option('--list',
|
146 |
dest='list',
|
147 |
action="store_true",
|
148 |
default=True,
|
149 |
help="List usage for all or specified user"),
|
150 |
make_option('--reset',
|
151 |
dest='reset',
|
152 |
action="store_true",
|
153 |
default=False,
|
154 |
help="Reset usage for all or specified users"),
|
155 |
make_option('--verify',
|
156 |
dest='verify',
|
157 |
action="store_true",
|
158 |
default=False,
|
159 |
help=("Verify statistics consistency for all "
|
160 |
"or specified users")),
|
161 |
make_option('--user',
|
162 |
dest='users',
|
163 |
action='append',
|
164 |
metavar='USER_UUID',
|
165 |
help="Specify which users --list or --reset applies. This option can be repeated several times. If no user is specified --list or --reset will be applied globally."),
|
166 |
) |
167 |
|
168 |
def handle_noargs(self, **options): |
169 |
try:
|
170 |
if options['list']: |
171 |
user_nodes = _retrieve_user_nodes(options['users'])
|
172 |
if not user_nodes: |
173 |
raise CommandError('No users found.') |
174 |
statistics = _compute_statistics(user_nodes) |
175 |
reset_holding = _prepare_reset_holding( |
176 |
statistics, verify=options['verify']
|
177 |
) |
178 |
print '\n'.join([str(i) for i in reset_holding]) |
179 |
|
180 |
if options['reset']: |
181 |
if not backend.quotaholder_enabled: |
182 |
raise CommandError('Quotaholder component is not enabled') |
183 |
|
184 |
if not backend.quotaholder_url: |
185 |
raise CommandError('Quotaholder url is not set') |
186 |
|
187 |
if not backend.quotaholder_token: |
188 |
raise CommandError('Quotaholder token is not set') |
189 |
|
190 |
while True: |
191 |
result = backend.quotaholder.reset_holding( |
192 |
context={}, |
193 |
clientkey='pithos',
|
194 |
reset_holding=reset_holding) |
195 |
|
196 |
if not result: |
197 |
break
|
198 |
|
199 |
missing_entities = [reset_holding[x].entity for x in result] |
200 |
self.stdout.write(
|
201 |
'Unknown quotaholder users: %s\n' %
|
202 |
', '.join(missing_entities))
|
203 |
m = 'Retrying sending quota usage for the rest...\n'
|
204 |
self.stdout.write(m)
|
205 |
missing_indexes = set(result)
|
206 |
reset_holding = [x for i, x in enumerate(reset_holding) |
207 |
if i not in missing_indexes] |
208 |
finally:
|
209 |
backend.close() |