summaryrefslogtreecommitdiffstats
path: root/glusterfs-hadoop/0.20.2/src/main/java/org/apache/hadoop/fs/glusterfs/GlusterFSXattr.java
blob: 455dda97e9c428079f667d2376c367dc3b736970 (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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/**
 *
 * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of GlusterFS.
 *
 * Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 */

package org.apache.hadoop.fs.glusterfs;

import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.hadoop.fs.BlockLocation;

public class GlusterFSXattr {

	public enum LAYOUT { D, S, R, DS, DR, SR, DSR }
        public enum CMD { GET_HINTS, GET_REPLICATION, GET_BLOCK_SIZE, CHECK_FOR_QUICK_IO }

        private static String hostname;

        public GlusterFSXattr() { }

        public static String brick2host (String brick)
        throws IOException {
                String[] hf = null;

                hf = brick.split(":");
                if (hf.length != 2) {
                        System.out.println("brick not of format hostname:path");
                        throw new IOException("Error getting hostname from brick");
                }

                return hf[0];
        }

        public static String brick2file (String brick)
        throws IOException {
                String[] hf = null;

                hf = brick.split(":");
                if (hf.length != 2) {
                        System.out.println("brick not of format hostname:path");
                        throw new IOException("Error getting hostname from brick");
                }

                return hf[1];
        }

        public static BlockLocation[] getPathInfo (String filename, long start, long len)
                throws IOException {
                HashMap<String, ArrayList<String>> vol = null;
                HashMap<String, Integer> meta          = new HashMap<String, Integer>();

                vol = execGetFattr(filename, meta, CMD.GET_HINTS);

                return getHints(vol, meta, start, len, null);
        }

        public static long getBlockSize (String filename)
                throws IOException {
                HashMap<String, ArrayList<String>> vol = null;
                HashMap<String, Integer> meta          = new HashMap<String, Integer>();

                vol = execGetFattr(filename, meta, CMD.GET_BLOCK_SIZE);

                if (!meta.containsKey("block-size"))
                        return 0;

                return (long) meta.get("block-size");

        }

        public static short getReplication (String filename)
                throws IOException {
                HashMap<String, ArrayList<String>> vol = null;
                HashMap<String, Integer> meta          = new HashMap<String, Integer>();

                vol = execGetFattr(filename, meta, CMD.GET_REPLICATION);

                return (short) getReplicationFromLayout(vol, meta);

        }

        public static TreeMap<Integer, GlusterFSBrickClass> quickIOPossible (String filename, long start,
                                                                             long len)
                throws IOException {
                String                   realpath      = null;
                HashMap<String, ArrayList<String>> vol = null;
                HashMap<String, Integer> meta          = new HashMap<String, Integer>();
                TreeMap<Integer, GlusterFSBrickClass> hnts = new TreeMap<Integer, GlusterFSBrickClass>();

                vol = execGetFattr(filename, meta, CMD.GET_HINTS);
                getHints(vol, meta, start, len, hnts);

                if (hnts.size() == 0)
                        return null; // BOOM !!

                // DEBUG - dump hnts here
                return hnts;
        }

        public static HashMap<String, ArrayList<String>> execGetFattr (String filename,
                                                                       HashMap<String, Integer> meta,
                                                                       CMD cmd)
                throws IOException {
                Process        p              = null;
                BufferedReader brInput        = null;
                String         s              = null;
                String         cmdOut         = null;
                String         getfattrCmd    = null;
                String         xlator         = null;
                String         enclosingXl    = null;
                String         enclosingXlVol = null;
                String         key            = null;
                String         layout         = "";
                int            rcount         = 0;
                int            scount         = 0;
                int            dcount         = 0;
                int            count          = 0;

                HashMap<String, ArrayList<String>> vol = new HashMap<String, ArrayList<String>>();

                getfattrCmd = "getfattr -m . -n trusted.glusterfs.pathinfo " + filename;

                p = Runtime.getRuntime().exec(getfattrCmd);
                brInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                cmdOut = "";
                while ( (s = brInput.readLine()) != null )
                        cmdOut += s;

                /**
                 * TODO: Use a single regex for extracting posix paths as well
                 * as xlator counts for layout matching.
                 */

                Pattern pattern = Pattern.compile("<(.*?)[:\\(](.*?)>");
                Matcher matcher = pattern.matcher(cmdOut);

                Pattern p_px = Pattern.compile(".*?:(.*)");
                Matcher m_px;
                String gibberish_path;

                s = null;
                while (matcher.find()) {
                        xlator = matcher.group(1);
                        if (xlator.equalsIgnoreCase("posix")) {
                                if (enclosingXl.equalsIgnoreCase("replicate"))
                                        count = rcount;
                                else if (enclosingXl.equalsIgnoreCase("stripe"))
                                        count = scount;
                                else if (enclosingXl.equalsIgnoreCase("distribute"))
                                        count = dcount;
                                else
                                        throw new IOException("Unknown Translator: " + enclosingXl);

                                key = enclosingXl + "-" + count;

                                if (vol.get(key) == null)
                                        vol.put(key, new ArrayList<String>());

                                gibberish_path = matcher.group(2);

                                /* extract posix path from the gibberish string */
                                m_px = p_px.matcher(gibberish_path);
                                if (!m_px.find())
                                        throw new IOException("Cannot extract posix path");

                                vol.get(key).add(m_px.group(1));
                                continue;
                        }

                        enclosingXl = xlator;
                        enclosingXlVol = matcher.group(2);

                        if (xlator.equalsIgnoreCase("replicate"))
                                if (rcount++ != 0)
                                        continue;

                        if (xlator.equalsIgnoreCase("stripe")) {
                                if (scount++ != 0)
                                        continue;


                                Pattern ps = Pattern.compile("\\[(\\d+)\\]");
                                Matcher ms = ps.matcher(enclosingXlVol);

                                if (ms.find()) {
                                        if (((cmd == CMD.GET_BLOCK_SIZE) || (cmd == CMD.GET_HINTS))
                                            && (meta != null))
                                            meta.put("block-size", Integer.parseInt(ms.group(1)));
                                } else
                                        throw new IOException("Cannot get stripe size");
                        }

                        if (xlator.equalsIgnoreCase("distribute"))
                                if (dcount++ != 0)
                                        continue;

                        layout += xlator.substring(0, 1);
                }

                if ((dcount == 0) && (scount == 0) && (rcount == 0))
                        throw new IOException("Cannot get layout");

                if (meta != null) {
                        meta.put("dcount", dcount);
                        meta.put("scount", scount);
                        meta.put("rcount", rcount);
                }

                vol.put("layout", new ArrayList<String>(1));
                vol.get("layout").add(layout);

                return vol;
        }

        static BlockLocation[] getHints (HashMap<String, ArrayList<String>> vol,
                                         HashMap<String, Integer> meta,
                                         long start, long len,
                                         TreeMap<Integer, GlusterFSBrickClass> hnts)
                throws IOException {
                String            brick         = null;
                String            key           = null;
                boolean           done          = false;
                int               i             = 0;
                int               counter       = 0;
                int               stripeSize    = 0;
                long              stripeStart   = 0;
                long              stripeEnd     = 0;
                int               nrAllocs      = 0;
                int               allocCtr      = 0;
                BlockLocation[] result          = null;
                ArrayList<String> brickList     = null;
                ArrayList<String> stripedBricks = null;
                Iterator<String>  it            = null;

                String[] blks = null;
                GlusterFSBrickRepl[] repl = null;
                int dcount, scount, rcount;

                LAYOUT l = LAYOUT.valueOf(vol.get("layout").get(0));
                dcount = meta.get("dcount");
                scount = meta.get("scount");
                rcount = meta.get("rcount");

                switch (l) {
                case D:
                        key = "DISTRIBUTE-" + dcount;
                        brick = vol.get(key).get(0);

                        if (hnts == null) {
                                result = new BlockLocation[1];
                                result[0] = new BlockLocation(null, new String[] {brick2host(brick)}, start, len);
                        } else
                                hnts.put(0, new GlusterFSBrickClass(brick, start, len, false, -1, -1, -1));
                        break;

                case R:
                case DR:
                        /* just the name says it's striped - the volume isn't */
                        stripedBricks = new ArrayList<String>();

                        for (i = 1; i <= rcount; i++) {
                                key = "REPLICATE-" + i;
                                brickList = vol.get(key);
                                it = brickList.iterator();
                                while (it.hasNext()) {
                                        stripedBricks.add(it.next());
                                }
                        }

                        nrAllocs = stripedBricks.size();
                        if (hnts == null) {
                                result = new BlockLocation[1];
                                blks = new String[nrAllocs];
                        }

                        for (i = 0; i < nrAllocs; i++) {
                                if (hnts == null)
                                        blks[i] = brick2host(stripedBricks.get(i));
                                else
                                        hnts.put(i, new GlusterFSBrickClass(stripedBricks.get(i), start, len, false, -1, -1, -1));
                        }

                        if (hnts == null)
                                result[0] = new BlockLocation(null, blks, start, len);

                        break;

                case SR:
                case DSR:
                        int rsize = 0;
                        ArrayList<ArrayList<String>> replicas = new ArrayList<ArrayList<String>>();

                        stripedBricks = new ArrayList<String>();

                        if (rcount == 0)
                                throw new IOException("got replicated volume with replication count 0");

                        for (i = 1; i <= rcount; i++) {
                                key = "REPLICATE-" + i;
                                brickList = vol.get(key);
                                it = brickList.iterator();
                                replicas.add(i - 1, new ArrayList<String>());
                                while (it.hasNext()) {
                                        replicas.get(i - 1).add(it.next());
                                }
                        }

                        stripeSize = meta.get("block-size");

                        nrAllocs = (int) (((len - start) / stripeSize) + 1);
                        if (hnts == null) {
                                result = new BlockLocation[nrAllocs];
                                repl = new GlusterFSBrickRepl[nrAllocs];
                        }

                        // starting stripe position
                        counter = (int) ((start / stripeSize) % rcount);
                        stripeStart = start;

                        key = null;
                        int currAlloc = 0;
                        boolean hntsDone = false;
                        while ((stripeStart < len) && !done) {
                                stripeEnd = (stripeStart - (stripeStart % stripeSize)) + stripeSize - 1;
                                if (stripeEnd > start + len) {
                                        stripeEnd = start + len - 1;
                                        done = true;
                                }

                                rsize = replicas.get(counter).size();

                                if (hnts == null)
                                        repl[allocCtr] = new GlusterFSBrickRepl(rsize, stripeStart, (stripeEnd - stripeStart));

                                for (i = 0; i < rsize; i++) {
                                        brick = replicas.get(counter).get(i);
                                        currAlloc = (allocCtr * rsize) + i;

                                        if (hnts == null)
                                                repl[allocCtr].addHost(brick2host(brick));
                                        else
                                                if (currAlloc <= (rsize * rcount) - 1) {
                                                        hnts.put(currAlloc, new GlusterFSBrickClass(brick, stripeStart,
                                                                                                    (stripeEnd - stripeStart),
                                                                                                    true, stripeSize, rcount, rsize));
                                                } else
                                                        hntsDone = true;
                                }

                                if (hntsDone)
                                        break;

                                stripeStart = stripeEnd + 1;

                                allocCtr++;
                                counter++;

                                if (counter >= replicas.size())
                                        counter = 0;
                        }

                        if (hnts == null)
                                for (int k = 0; k < nrAllocs; k++)
                                        result[k] = new BlockLocation(null, repl[k].getReplHosts(), repl[k].getStartLen(), repl[k].getOffLen());

                        break;

                case S:
                case DS:
                        if (scount == 0)
                                throw new IOException("got striped volume with stripe count 0");

                        stripedBricks = new ArrayList<String>();
                        stripeSize = meta.get("block-size");

                        key = "STRIPE-" + scount;
                        brickList = vol.get(key);
                        it = brickList.iterator();
                        while (it.hasNext()) {
                                stripedBricks.add(it.next());
                        }

                        nrAllocs = (int) ((len - start) / stripeSize) + 1;
                        if (hnts == null)
                                result = new BlockLocation[nrAllocs];

                        // starting stripe position
                        counter = (int) ((start / stripeSize) % stripedBricks.size());
                        stripeStart = start;

                        key = null;
                        while ((stripeStart < len) && !done) {
                                brick = stripedBricks.get(counter);

                                stripeEnd = (stripeStart - (stripeStart % stripeSize)) + stripeSize - 1;
                                if (stripeEnd > start + len) {
                                        stripeEnd = start + len - 1;
                                        done = true;
                                }

                                if (hnts == null)
                                        result[allocCtr] = new BlockLocation(null, new String[] {brick2host(brick)},
                                                                             stripeStart, (stripeEnd - stripeStart));
                                else
                                        if (allocCtr <= stripedBricks.size()) {
                                                hnts.put(allocCtr, new GlusterFSBrickClass(brick, stripeStart, (stripeEnd - stripeStart),
                                                                                           true, stripeSize, stripedBricks.size(), -1));
                                        } else
                                                break;

                                stripeStart = stripeEnd + 1;

                                counter++;
                                allocCtr++;

                                if (counter >= stripedBricks.size())
                                        counter = 0;
                        }

                        break;
                }

                return result;
        }

        /* TODO: use meta{dcount,scount,rcount} for checking */
        public static int getReplicationFromLayout (HashMap<String, ArrayList<String>> vol,
                                                    HashMap<String, Integer> meta)
                throws IOException {
                int replication = 0;
                LAYOUT l = LAYOUT.valueOf(vol.get("layout").get(0));

                switch (l) {
                case D:
                case S:
                case DS:
                        replication = 1;
                        break;

                case R:
                case DR:
                case SR:
                case DSR:
                        final String key = "REPLICATION-1";
                        replication = vol.get(key).size();
                }

                return replication;
        }
}